fix text element tests

This commit is contained in:
Bixilon 2023-06-15 13:47:16 +02:00
parent 4d6117fd0b
commit 2905e170da
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
4 changed files with 65 additions and 17 deletions

View File

@ -2,9 +2,10 @@ package de.bixilon.minosoft.gui.rendering.gui.elements.text
import de.bixilon.kotlinglm.vec2.Vec2 import de.bixilon.kotlinglm.vec2.Vec2
import de.bixilon.kotlinglm.vec4.Vec4 import de.bixilon.kotlinglm.vec4.Vec4
import de.bixilon.minosoft.gui.rendering.gui.GuiRenderTestUtil import de.bixilon.minosoft.gui.rendering.font.renderer.element.TextRenderProperties
import de.bixilon.minosoft.gui.rendering.gui.GuiRenderTestUtil.assetSize
import de.bixilon.minosoft.gui.rendering.gui.elements.text.background.TextBackground 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 import org.testng.annotations.Test
@Test(groups = ["font", "gui"]) @Test(groups = ["font", "gui"])
@ -16,46 +17,51 @@ class TextElementTest {
} }
fun `size of single char`() { 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)) element.assetSize(Vec2(0.5f, 11.0f))
} }
fun `size of multiple chars`() { 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)) element.assetSize(Vec2(2.5f, 11.0f))
} }
fun `size with new line`() { 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)) element.assetSize(Vec2(2.5f, 22.0f))
} }
fun `size with background`() { 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)) element.assetSize(Vec2(4.5f, 13.0f))
} }
fun `size with background and newlines`() { fun `size with background and newline`() {
val element = TextElement(GuiRenderTestUtil.create(), "bc\nbc") val element = TextElement(GuiRenderTestUtil.create(), "bc\nbc", properties = TextRenderProperties(shadow = false))
element.assetSize(Vec2(4.5f, 24.0f)) 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`() { 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.text = "bcd\nbcd\nbcd"
element.assetSize(Vec2(6.0f, 35.0f)) element.assetSize(Vec2(7.0f, 35.0f))
} }
fun `size if background cleared`() { 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.background = null
element.assetSize(Vec2(4.0f, 33.0f)) element.assetSize(Vec2(5.0f, 33.0f))
} }
fun `size if background set`() { 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.background = TextBackground(size = Vec4(2.0f))
element.assetSize(Vec2(8.0f, 37.0f)) element.assetSize(Vec2(9.0f, 37.0f))
} }

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* 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")
}
}

View File

@ -11,7 +11,7 @@
* This software is not affiliated with Mojang AB, the original developer of Minecraft. * 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.Vec2
import de.bixilon.kotlinglm.vec2.Vec2i 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.RenderContext
import de.bixilon.minosoft.gui.rendering.font.manager.FontManager 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.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.atlas.CodeTexturePart
import de.bixilon.minosoft.gui.rendering.gui.elements.Element import de.bixilon.minosoft.gui.rendering.gui.elements.Element
import de.bixilon.minosoft.gui.rendering.system.dummy.DummyRenderSystem import de.bixilon.minosoft.gui.rendering.system.dummy.DummyRenderSystem
@ -54,6 +55,6 @@ object GuiRenderTestUtil {
fun Element.assetSize(size: Vec2) { fun Element.assetSize(size: Vec2) {
assertEquals(size, this.size) assertEquals(this.size, size)
} }
} }

View File

@ -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.vec2.Vec2Util.MAX
import de.bixilon.minosoft.gui.rendering.util.vec.vec4.Vec4Util.bottom 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.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.gui.rendering.util.vec.vec4.Vec4Util.vertical
import de.bixilon.minosoft.util.KUtil.charCount import de.bixilon.minosoft.util.KUtil.charCount
@ -125,8 +126,13 @@ open class TextElement(
ChatComponentRenderer.render(TextOffset(), context.font, properties, info, null, null, text) ChatComponentRenderer.render(TextOffset(), context.font, properties, info, null, null, text)
info.rewind() 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 this.info = info
_size = Vec2(info.size)
} }
override fun forceSilentApply() { override fun forceSilentApply() {