ASMR/src/main/kotlin/net/shadowfacts/cacao/view/Label.kt

50 lines
1.3 KiB
Kotlin
Raw Normal View History

2019-06-22 19:02:17 +00:00
package net.shadowfacts.cacao.view
2019-06-22 14:56:12 +00:00
import net.minecraft.client.MinecraftClient
import net.minecraft.client.font.TextRenderer
2019-06-23 15:41:32 +00:00
import net.shadowfacts.cacao.geometry.Point
2019-06-22 19:02:17 +00:00
import net.shadowfacts.cacao.geometry.Size
import net.shadowfacts.cacao.util.Color
2019-06-22 14:56:12 +00:00
2019-06-22 14:59:18 +00:00
/**
2019-06-22 20:08:00 +00:00
* 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].
*
2019-06-22 14:59:18 +00:00
* @author shadowfacts
2019-06-22 20:08:00 +00:00
* @param text The text of this label.
2019-06-22 14:59:18 +00:00
*/
2019-06-22 14:56:12 +00:00
class Label(text: String): View() {
companion object {
2019-06-22 20:08:00 +00:00
private val textRenderer: TextRenderer
2019-06-22 14:56:12 +00:00
get() = MinecraftClient.getInstance().textRenderer
}
2019-06-22 20:08:00 +00:00
/**
* The text of this label. Mutating this field will update the intrinsic content size and trigger a layout.
*/
2019-06-22 14:56:12 +00:00
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())
}
2019-06-23 15:41:32 +00:00
override fun drawContent(mouse: Point, delta: Float) {
2019-06-22 14:56:12 +00:00
textRenderer.draw(text, 0f, 0f, textColor.argb)
}
}