50 lines
1.3 KiB
Kotlin
50 lines
1.3 KiB
Kotlin
package net.shadowfacts.cacao.view
|
|
|
|
import net.minecraft.client.MinecraftClient
|
|
import net.minecraft.client.font.TextRenderer
|
|
import net.shadowfacts.cacao.geometry.Point
|
|
import net.shadowfacts.cacao.geometry.Size
|
|
import net.shadowfacts.cacao.util.Color
|
|
|
|
/**
|
|
* A simple View that displays text. Allows for controlling the color and shadow of the text. Label cannot be used
|
|
* for multi-line text, instead use [TextView].
|
|
*
|
|
* @author shadowfacts
|
|
* @param text The text of this label.
|
|
*/
|
|
class Label(text: String): View() {
|
|
|
|
companion object {
|
|
private val textRenderer: TextRenderer
|
|
get() = MinecraftClient.getInstance().textRenderer
|
|
}
|
|
|
|
/**
|
|
* The text of this label. Mutating this field will update the intrinsic content size and trigger a layout.
|
|
*/
|
|
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(mouse: Point, delta: Float) {
|
|
textRenderer.draw(text, 0f, 0f, textColor.argb)
|
|
}
|
|
|
|
} |