text element: proper size limiting with background

This commit is contained in:
Bixilon 2023-06-15 14:16:10 +02:00
parent cf4d955df0
commit 9ef20161eb
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 41 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import de.bixilon.minosoft.gui.rendering.font.renderer.element.TextRenderPropert
import de.bixilon.minosoft.gui.rendering.gui.elements.text.background.TextBackground
import de.bixilon.minosoft.gui.rendering.gui.test.GuiRenderTestUtil
import de.bixilon.minosoft.gui.rendering.gui.test.GuiRenderTestUtil.assetSize
import org.testng.Assert.assertEquals
import org.testng.annotations.Test
@Test(groups = ["font", "gui"])
@ -64,6 +65,33 @@ class TextElementTest {
element.assetSize(Vec2(9.0f, 37.0f))
}
// TODO: test on mouse (click/hover events), rendering, size limiting
fun `limited size but not actually limited`() {
val element = TextElement(GuiRenderTestUtil.create(), "bcd\nbcd\nbcd", background = null, properties = TextRenderProperties(shadow = false))
element.prefMaxSize = Vec2(10.0f, 11.0f)
element.assetSize(Vec2(5.0f, 11.0f))
}
fun `limited size`() {
val element = TextElement(GuiRenderTestUtil.create(), "bcd\nbcd\nbcd", background = null, properties = TextRenderProperties(shadow = false))
element.prefMaxSize = Vec2(3.0f, 11.0f)
element.assetSize(Vec2(2.5f, 11.0f))
assertEquals(element.info.size, Vec2(2.5f, 11.0f))
}
fun `limited size with background`() {
val element = TextElement(GuiRenderTestUtil.create(), "bcd\nbcd\nbcd", background = TextBackground(size = Vec4(1.0f)), properties = TextRenderProperties(shadow = false))
element.prefMaxSize = Vec2(5.0f, 11.0f)
element.assetSize(Vec2(0.0f, 0.0f))
assertEquals(element.info.size, Vec2(0.0f, 0.0f))
}
fun `limited size with background 2`() {
val element = TextElement(GuiRenderTestUtil.create(), "bcd\nbcd\nbcd", background = TextBackground(size = Vec4(1.0f)), properties = TextRenderProperties(shadow = false))
element.prefMaxSize = Vec2(5.0f, 13.0f)
element.assetSize(Vec2(4.5f, 13.0f))
assertEquals(element.info.size, Vec2(2.5f, 11.0f))
}
// TODO: test on mouse (click/hover events), rendering
}

View File

@ -121,17 +121,12 @@ open class TextElement(
}
private fun updateText(text: ChatComponent) {
val info = TextRenderInfo(Vec2(maxSize))
val info = TextRenderInfo(maxSize.withBackgroundSize(-1.0f))
if (!empty) {
ChatComponentRenderer.render(TextOffset(), context.font, properties, info, null, null, text)
info.rewind()
}
val size = Vec2(info.size)
val background = this.background
if (background != null && size.x > 0.0f && size.y > 0.0f) { // only add background if text is not empty
size += background.size.spaceSize
}
_size = size
_size = info.size.withBackgroundSize()
this.info = info
}
@ -281,4 +276,14 @@ open class TextElement(
override fun toString(): String {
return chatComponent.toString()
}
private fun Vec2.withBackgroundSize(sign: Float = 1.0f): Vec2 {
val size = Vec2(this)
val background = this@TextElement.background
if (background != null && size.x > 0.0f && size.y > 0.0f) { // only add background if text is not empty
size += background.size.spaceSize * sign
}
return size
}
}