Add Label
This commit is contained in:
parent
349c09f630
commit
50b91f276b
|
@ -6,6 +6,7 @@ import net.shadowfacts.shadowui.View
|
|||
import net.shadowfacts.shadowui.Window
|
||||
import net.shadowfacts.shadowui.geometry.Size
|
||||
import net.shadowfacts.shadowui.util.Color
|
||||
import net.shadowfacts.shadowui.view.Label
|
||||
|
||||
class TestScreen: Screen() {
|
||||
|
||||
|
@ -24,6 +25,9 @@ class TestScreen: Screen() {
|
|||
backgroundColor = Color(0x800080)
|
||||
})
|
||||
purple.intrinsicContentSize = Size(width = 150.0, height = 150.0)
|
||||
val label = purple.addSubview(Label("Hello, world!").apply {
|
||||
textColor = Color.WHITE
|
||||
})
|
||||
|
||||
solver.dsl {
|
||||
red.leftAnchor equalTo 0
|
||||
|
@ -45,6 +49,9 @@ class TestScreen: Screen() {
|
|||
// purple.heightAnchor equalTo 100
|
||||
purple.centerXAnchor equalTo green.centerXAnchor
|
||||
purple.centerYAnchor equalTo green.centerYAnchor
|
||||
|
||||
label.centerXAnchor equalTo purple.centerXAnchor
|
||||
label.centerYAnchor equalTo purple.centerYAnchor
|
||||
}
|
||||
|
||||
layout()
|
||||
|
|
|
@ -9,7 +9,7 @@ import net.shadowfacts.shadowui.util.RenderHelper
|
|||
import no.birkett.kiwi.Constraint
|
||||
import no.birkett.kiwi.Solver
|
||||
|
||||
class View {
|
||||
open class View {
|
||||
|
||||
lateinit var solver: Solver
|
||||
|
||||
|
@ -36,7 +36,7 @@ class View {
|
|||
private var intrinsicContentSizeWidthConstraint: Constraint? = null
|
||||
private var intrinsicContentSizeHeightConstraint: Constraint? = null
|
||||
|
||||
var backgroundColor = Color(0xff0000)
|
||||
var backgroundColor = Color.CLEAR
|
||||
|
||||
var parent: View? = null
|
||||
val subviews = mutableListOf<View>()
|
||||
|
@ -51,11 +51,11 @@ class View {
|
|||
return view
|
||||
}
|
||||
|
||||
fun wasAdded() {
|
||||
open fun wasAdded() {
|
||||
createInternalConstraints()
|
||||
}
|
||||
|
||||
fun createInternalConstraints() {
|
||||
open fun createInternalConstraints() {
|
||||
solver.dsl {
|
||||
rightAnchor equalTo (leftAnchor + widthAnchor)
|
||||
bottomAnchor equalTo (topAnchor + heightAnchor)
|
||||
|
@ -77,7 +77,7 @@ class View {
|
|||
}
|
||||
}
|
||||
|
||||
fun didLayout() {
|
||||
open fun didLayout() {
|
||||
subviews.forEach(View::didLayout)
|
||||
|
||||
val parentLeft = parent?.leftAnchor?.value ?: 0.0
|
||||
|
@ -99,6 +99,6 @@ class View {
|
|||
GlStateManager.popMatrix()
|
||||
}
|
||||
|
||||
fun drawContent() {}
|
||||
open fun drawContent() {}
|
||||
|
||||
}
|
|
@ -7,4 +7,10 @@ data class Color(val red: Int, val green: Int, val blue: Int, val alpha: Int = 2
|
|||
val argb: Int
|
||||
get() = ((alpha and 255) shl 24) or ((red and 255) shl 16) or ((green and 255) shl 8) or (blue and 255)
|
||||
|
||||
companion object {
|
||||
val CLEAR = Color(0, alpha = 0)
|
||||
val WHITE = Color(0xffffff)
|
||||
val BLACK = Color(0)
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package net.shadowfacts.shadowui.view
|
||||
|
||||
import net.minecraft.client.MinecraftClient
|
||||
import net.minecraft.client.font.TextRenderer
|
||||
import net.shadowfacts.shadowui.View
|
||||
import net.shadowfacts.shadowui.geometry.Size
|
||||
import net.shadowfacts.shadowui.util.Color
|
||||
|
||||
class Label(text: String): View() {
|
||||
|
||||
companion object {
|
||||
val textRenderer: TextRenderer
|
||||
get() = MinecraftClient.getInstance().textRenderer
|
||||
}
|
||||
|
||||
var text: String = text
|
||||
set(value) {
|
||||
field = value
|
||||
updateIntrinsicContentSize()
|
||||
}
|
||||
|
||||
var textColor = Color(0x404040)
|
||||
|
||||
override fun wasAdded() {
|
||||
super.wasAdded()
|
||||
|
||||
updateIntrinsicContentSize()
|
||||
}
|
||||
|
||||
private fun updateIntrinsicContentSize() {
|
||||
val width = textRenderer.getStringWidth(text)
|
||||
val height = textRenderer.fontHeight
|
||||
intrinsicContentSize = Size(width.toDouble(), height.toDouble())
|
||||
}
|
||||
|
||||
override fun drawContent() {
|
||||
textRenderer.draw(text, 0f, 0f, textColor.argb)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue