Add shadow to label, implement sizeToContents

This commit is contained in:
DBotThePony 2022-09-04 16:05:50 +07:00
parent 9cfbfd5d7b
commit 322e070060
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -13,9 +13,14 @@ open class Label @JvmOverloads constructor(
y: Float = 0f,
width: Float = 100f,
height: Float = 10f,
var text: Component = TextComponent("Label")
var text: Component = TextComponent("Label"),
var shadow: Boolean = false
) : EditablePanel(screen, parent, x, y, width, height) {
constructor(screen: MatteryScreen<*>, parent: EditablePanel?, text: Component) : this(screen, parent, x = 0f, text = text)
constructor(screen: MatteryScreen<*>, parent: EditablePanel?, text: Component, shadow: Boolean = false) : this(screen, parent, x = 0f, text = text, shadow = shadow)
var shadowX = 1f
var shadowY = 1f
var shadowColor = RGBAColor.BLACK
init {
scissor = true
@ -25,5 +30,19 @@ open class Label @JvmOverloads constructor(
override fun innerRender(stack: PoseStack, mouseX: Float, mouseY: Float, partialTick: Float) {
font.draw(stack, text, 0f, 0f, color.toInt())
if (shadow) {
font.draw(stack, text, shadowX, shadowY, shadowColor.toInt())
}
}
override fun sizeToContents() {
super.sizeToContents()
val w = font.width(text)
val h = font.lineHeight + 2
width = width.coerceAtLeast(w.toFloat())
height = height.coerceAtLeast(h.toFloat())
}
}