From 2905e170da71ed6da772602d0f3aef2193ef3a31 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Thu, 15 Jun 2023 13:47:16 +0200 Subject: [PATCH] fix text element tests --- .../gui/elements/text/TextElementTest.kt | 34 ++++++++++-------- .../gui/rendering/gui/test/GUITestConsumer.kt | 35 +++++++++++++++++++ .../gui/{ => test}/GuiRenderTestUtil.kt | 5 +-- .../gui/elements/text/TextElement.kt | 8 ++++- 4 files changed, 65 insertions(+), 17 deletions(-) create mode 100644 src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/gui/test/GUITestConsumer.kt rename src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/gui/{ => test}/GuiRenderTestUtil.kt (94%) diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/gui/elements/text/TextElementTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/gui/elements/text/TextElementTest.kt index bbe4d96ba..c855c10c1 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/gui/elements/text/TextElementTest.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/gui/elements/text/TextElementTest.kt @@ -2,9 +2,10 @@ package de.bixilon.minosoft.gui.rendering.gui.elements.text import de.bixilon.kotlinglm.vec2.Vec2 import de.bixilon.kotlinglm.vec4.Vec4 -import de.bixilon.minosoft.gui.rendering.gui.GuiRenderTestUtil -import de.bixilon.minosoft.gui.rendering.gui.GuiRenderTestUtil.assetSize +import de.bixilon.minosoft.gui.rendering.font.renderer.element.TextRenderProperties 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.annotations.Test @Test(groups = ["font", "gui"]) @@ -16,46 +17,51 @@ class TextElementTest { } fun `size of single char`() { - val element = TextElement(GuiRenderTestUtil.create(), "b", background = null) + val element = TextElement(GuiRenderTestUtil.create(), "b", background = null, properties = TextRenderProperties(shadow = false)) element.assetSize(Vec2(0.5f, 11.0f)) } fun `size of multiple chars`() { - val element = TextElement(GuiRenderTestUtil.create(), "bc", background = null) + val element = TextElement(GuiRenderTestUtil.create(), "bc", background = null, properties = TextRenderProperties(shadow = false)) element.assetSize(Vec2(2.5f, 11.0f)) } fun `size with new line`() { - val element = TextElement(GuiRenderTestUtil.create(), "bc\nbc", background = null) + val element = TextElement(GuiRenderTestUtil.create(), "bc\nbc", background = null, properties = TextRenderProperties(shadow = false)) element.assetSize(Vec2(2.5f, 22.0f)) } fun `size with background`() { - val element = TextElement(GuiRenderTestUtil.create(), "bc") + val element = TextElement(GuiRenderTestUtil.create(), "bc", properties = TextRenderProperties(shadow = false)) element.assetSize(Vec2(4.5f, 13.0f)) } - fun `size with background and newlines`() { - val element = TextElement(GuiRenderTestUtil.create(), "bc\nbc") + fun `size with background and newline`() { + val element = TextElement(GuiRenderTestUtil.create(), "bc\nbc", properties = TextRenderProperties(shadow = false)) element.assetSize(Vec2(4.5f, 24.0f)) } + fun `size with background and newlines`() { + val element = TextElement(GuiRenderTestUtil.create(), "bcd\nbcd\nbcd", properties = TextRenderProperties(shadow = false)) + element.assetSize(Vec2(7.0f, 35.0f)) + } + fun `size if text changed`() { - val element = TextElement(GuiRenderTestUtil.create(), "bc\nbc") + val element = TextElement(GuiRenderTestUtil.create(), "bc\nbc", properties = TextRenderProperties(shadow = false)) element.text = "bcd\nbcd\nbcd" - element.assetSize(Vec2(6.0f, 35.0f)) + element.assetSize(Vec2(7.0f, 35.0f)) } fun `size if background cleared`() { - val element = TextElement(GuiRenderTestUtil.create(), "bcd\nbcd\nbcd") + val element = TextElement(GuiRenderTestUtil.create(), "bcd\nbcd\nbcd", properties = TextRenderProperties(shadow = false)) element.background = null - element.assetSize(Vec2(4.0f, 33.0f)) + element.assetSize(Vec2(5.0f, 33.0f)) } fun `size if background set`() { - val element = TextElement(GuiRenderTestUtil.create(), "bcd\nbcd\nbcd") + val element = TextElement(GuiRenderTestUtil.create(), "bcd\nbcd\nbcd", properties = TextRenderProperties(shadow = false)) element.background = TextBackground(size = Vec4(2.0f)) - element.assetSize(Vec2(8.0f, 37.0f)) + element.assetSize(Vec2(9.0f, 37.0f)) } diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/gui/test/GUITestConsumer.kt b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/gui/test/GUITestConsumer.kt new file mode 100644 index 000000000..6c6bc6864 --- /dev/null +++ b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/gui/test/GUITestConsumer.kt @@ -0,0 +1,35 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.gui.test + +import de.bixilon.kotlinglm.vec2.Vec2 +import de.bixilon.minosoft.data.text.formatting.color.RGBColor +import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIMeshCache +import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer +import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions +import de.bixilon.minosoft.gui.rendering.system.base.texture.ShaderIdentifiable +import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh + +class GUITestConsumer : GUIVertexConsumer { + override val order get() = Mesh.QUAD_TO_QUAD_ORDER + override fun ensureSize(size: Int) = Unit + + override fun addVertex(position: Vec2, texture: ShaderIdentifiable?, uv: Vec2, tint: RGBColor, options: GUIVertexOptions?) { + TODO("Not yet implemented") + } + + override fun addCache(cache: GUIMeshCache) { + TODO("Not yet implemented") + } +} diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/gui/GuiRenderTestUtil.kt b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/gui/test/GuiRenderTestUtil.kt similarity index 94% rename from src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/gui/GuiRenderTestUtil.kt rename to src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/gui/test/GuiRenderTestUtil.kt index 76e9f145f..e8dff0e63 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/gui/GuiRenderTestUtil.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/gui/test/GuiRenderTestUtil.kt @@ -11,7 +11,7 @@ * This software is not affiliated with Mojang AB, the original developer of Minecraft. */ -package de.bixilon.minosoft.gui.rendering.gui +package de.bixilon.minosoft.gui.rendering.gui.test import de.bixilon.kotlinglm.vec2.Vec2 import de.bixilon.kotlinglm.vec2.Vec2i @@ -21,6 +21,7 @@ import de.bixilon.minosoft.data.registries.identified.Namespaces.minosoft import de.bixilon.minosoft.gui.rendering.RenderContext import de.bixilon.minosoft.gui.rendering.font.manager.FontManager import de.bixilon.minosoft.gui.rendering.font.types.dummy.DummyFontType +import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer import de.bixilon.minosoft.gui.rendering.gui.atlas.CodeTexturePart import de.bixilon.minosoft.gui.rendering.gui.elements.Element import de.bixilon.minosoft.gui.rendering.system.dummy.DummyRenderSystem @@ -54,6 +55,6 @@ object GuiRenderTestUtil { fun Element.assetSize(size: Vec2) { - assertEquals(size, this.size) + assertEquals(this.size, size) } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/elements/text/TextElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/elements/text/TextElement.kt index e674a06db..a487dd65f 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/elements/text/TextElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/elements/text/TextElement.kt @@ -39,6 +39,7 @@ import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2Util.EMPTY import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2Util.MAX import de.bixilon.minosoft.gui.rendering.util.vec.vec4.Vec4Util.bottom import de.bixilon.minosoft.gui.rendering.util.vec.vec4.Vec4Util.offset +import de.bixilon.minosoft.gui.rendering.util.vec.vec4.Vec4Util.spaceSize import de.bixilon.minosoft.gui.rendering.util.vec.vec4.Vec4Util.vertical import de.bixilon.minosoft.util.KUtil.charCount @@ -125,8 +126,13 @@ open class TextElement( 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 this.info = info - _size = Vec2(info.size) } override fun forceSilentApply() {