From f1ca745c5067b495ba6ab383eb18414433ab0f51 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sun, 24 Oct 2021 18:23:18 +0200 Subject: [PATCH] wip text scaling --- .../minosoft/gui/rendering/font/CharData.kt | 25 +++++++++++-------- .../font/renderer/TextComponentRenderer.kt | 10 ++++---- .../rendering/font/renderer/TextRenderInfo.kt | 1 + .../gui/elements/text/TextElement.kt | 16 ++++++++++-- .../gui/hud/elements/WorldInfoHUDElement.kt | 4 +-- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/font/CharData.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/font/CharData.kt index aa5465278..bd8dd06cd 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/font/CharData.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/font/CharData.kt @@ -43,10 +43,10 @@ class CharData( uvEnd = uvEnd * texture.textureArrayUV } - fun render(position: Vec2i, z: Int, style: TextStyle, consumer: GUIVertexConsumer, options: GUIVertexOptions?) { - render(position, z + 2, false, style, consumer, options) + fun render(position: Vec2i, z: Int, style: TextStyle, consumer: GUIVertexConsumer, options: GUIVertexOptions?, scale: Float) { + render(position, z + 2, false, style, consumer, options, scale) if (style.formatting.contains(PreChatFormattingCodes.SHADOWED)) { - render(position, z, true, style, consumer, options) + render(position, z, true, style, consumer, options, scale) } } @@ -70,7 +70,7 @@ class CharData( } } - private fun render(position: Vec2i, z: Int, shadow: Boolean, style: TextStyle, vertexConsumer: GUIVertexConsumer, options: GUIVertexOptions?) { + private fun render(position: Vec2i, z: Int, shadow: Boolean, style: TextStyle, vertexConsumer: GUIVertexConsumer, options: GUIVertexOptions?, scale: Float) { var color = style.color ?: ChatColors.WHITE @@ -83,12 +83,15 @@ class CharData( var boldOffset = 0.0f if (style.formatting.contains(PreChatFormattingCodes.BOLD)) { - boldOffset = BOLD_OFFSET + boldOffset = BOLD_OFFSET * scale } + val charHeight = Font.CHAR_HEIGHT * scale + val horizontalSpacing = Font.HORIZONTAL_SPACING * scale + val verticalSpacing = Font.VERTICAL_SPACING * scale - val startPosition = Vec2(position) + shadowOffset - val endPosition = startPosition + Vec2(scaledWidth, Font.CHAR_HEIGHT.toFloat()) + val startPosition = Vec2(position) + (shadowOffset * scale) + val endPosition = startPosition + (Vec2(scaledWidth * scale, charHeight)) val italic = style.formatting.contains(PreChatFormattingCodes.ITALIC) @@ -101,23 +104,23 @@ class CharData( } if (style.formatting.contains(PreChatFormattingCodes.STRIKETHROUGH)) { - vertexConsumer.addQuad(startPosition + Vec2(-Font.HORIZONTAL_SPACING, Font.CHAR_HEIGHT / 2.0f), Vec2(endPosition.x + Font.HORIZONTAL_SPACING, startPosition.y + Font.CHAR_HEIGHT / 2.0f + 1.0f), z + 1, renderWindow.WHITE_TEXTURE.texture, renderWindow.WHITE_TEXTURE.uvStart, renderWindow.WHITE_TEXTURE.uvEnd, italic, color, options) + vertexConsumer.addQuad(startPosition + Vec2(-horizontalSpacing, charHeight / 2.0f - scale / 2), Vec2(endPosition.x + horizontalSpacing, startPosition.y + charHeight / 2.0f + scale / 2), z + 1, renderWindow.WHITE_TEXTURE.texture, renderWindow.WHITE_TEXTURE.uvStart, renderWindow.WHITE_TEXTURE.uvEnd, italic, color, options) } if (style.formatting.contains(PreChatFormattingCodes.UNDERLINED)) { - vertexConsumer.addQuad(startPosition + Vec2i(-Font.HORIZONTAL_SPACING, Font.CHAR_HEIGHT), Vec2i(endPosition.x + boldOffset + Font.HORIZONTAL_SPACING, startPosition.y + Font.CHAR_HEIGHT + Font.VERTICAL_SPACING / 2.0f), z, renderWindow.WHITE_TEXTURE.texture, renderWindow.WHITE_TEXTURE.uvStart, renderWindow.WHITE_TEXTURE.uvEnd, italic, color, options) + vertexConsumer.addQuad(startPosition + Vec2i(-horizontalSpacing, charHeight), Vec2i(endPosition.x + boldOffset + horizontalSpacing, startPosition.y + charHeight + verticalSpacing / 2.0f), z, renderWindow.WHITE_TEXTURE.texture, renderWindow.WHITE_TEXTURE.uvStart, renderWindow.WHITE_TEXTURE.uvEnd, italic, color, options) } // ToDo: Obfuscated } - fun calculateWidth(style: TextStyle): Int { + fun calculateWidth(style: TextStyle, scale: Float): Int { var width = scaledWidth.toFloat() if (style.formatting.contains(PreChatFormattingCodes.SHADOWED)) { width += SHADOW_OFFSET } - return width.ceil + return (width * scale).ceil } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/font/renderer/TextComponentRenderer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/font/renderer/TextComponentRenderer.kt index 23083913a..5a43b1d9b 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/font/renderer/TextComponentRenderer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/font/renderer/TextComponentRenderer.kt @@ -72,7 +72,7 @@ object TextComponentRenderer : ChatComponentRenderer { } fun wrap(): Boolean { - if (addY(Font.TOTAL_CHAR_HEIGHT)) { + if (addY(renderInfo.charHeight)) { return true } pushLine() @@ -134,18 +134,18 @@ object TextComponentRenderer : ChatComponentRenderer { val charData = renderWindow.font[char] ?: continue - val charWidth = charData.calculateWidth(text) + val charWidth = charData.calculateWidth(text, renderInfo.scale) var width = charWidth if (offset.x != initialOffset.x + renderInfo.charMargin) { // add spacing between letters - width += if (bold) { + width += (if (bold) { Font.HORIZONTAL_SPACING_BOLD } else if (shadow) { Font.HORIZONTAL_SPACING_SHADOW } else { Font.HORIZONTAL_SPACING - } + } * renderInfo.scale).toInt() } val previousY = offset.y @@ -163,7 +163,7 @@ object TextComponentRenderer : ChatComponentRenderer { // ToDo: Remove Font.HORIZONTAL_SPACING } - consumer?.let { charData.render(letterOffset, z, text, it, options) } + consumer?.let { charData.render(letterOffset, z, text, it, options, renderInfo.scale) } if (consumer == null) { currentLineText += char diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/font/renderer/TextRenderInfo.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/font/renderer/TextRenderInfo.kt index 70fb5afca..f704c74c8 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/font/renderer/TextRenderInfo.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/font/renderer/TextRenderInfo.kt @@ -23,6 +23,7 @@ class TextRenderInfo( val fontAlignment: HorizontalAlignments, val charHeight: Int, val charMargin: Int, + val scale: Float, val lines: MutableList = mutableListOf(), var currentLineNumber: Int = 0, ) { 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 5e65bd594..49ec3c779 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,11 +39,21 @@ open class TextElement( var backgroundColor: RGBColor = RenderConstants.TEXT_BACKGROUND_COLOR, noBorder: Boolean = false, parent: Element? = null, + scale: Float = 1.0f, ) : LabeledElement(hudRenderer) { lateinit var renderInfo: TextRenderInfo private set // ToDo: Reapply if backgroundColor or fontAlignment changes + + var scale: Float = scale + set(value) { + if (field == value) { + return + } + field = value + cacheUpToDate = false + } var background: Boolean = background set(value) { if (field == value) { @@ -58,8 +68,8 @@ open class TextElement( return } field = value - charHeight = value.decide(Font.CHAR_HEIGHT, Font.TOTAL_CHAR_HEIGHT) - charMargin = value.decide(0, Font.CHAR_MARGIN) + charHeight = (value.decide(Font.CHAR_HEIGHT, Font.TOTAL_CHAR_HEIGHT) * scale).toInt() + charMargin = (value.decide(0, Font.CHAR_MARGIN) * scale).toInt() forceApply() } var charHeight: Int = 0 @@ -88,6 +98,7 @@ open class TextElement( fontAlignment = fontAlignment, charHeight = charHeight, charMargin = charMargin, + scale = scale, ) ChatComponentRenderer.render(Vec2i.EMPTY, Vec2i.EMPTY, prefSize, 0, InfiniteSizeElement(hudRenderer), renderWindow, null, null, renderInfo, value) } @@ -107,6 +118,7 @@ open class TextElement( fontAlignment = fontAlignment, charHeight = charHeight, charMargin = charMargin, + scale = scale, ) if (!emptyMessage) { ChatComponentRenderer.render(Vec2i.EMPTY, Vec2i.EMPTY, size, 0, this, renderWindow, null, null, renderInfo, chatComponent) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/WorldInfoHUDElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/WorldInfoHUDElement.kt index 964db7408..b869308b1 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/WorldInfoHUDElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/WorldInfoHUDElement.kt @@ -22,7 +22,7 @@ import de.bixilon.minosoft.util.MMath.round10 import glm_.vec2.Vec2i class WorldInfoHUDElement(hudRenderer: HUDRenderer) : HUDElement(hudRenderer), Pollable { - override val layout: TextElement = TextElement(hudRenderer, "") + override val layout: TextElement = TextElement(hudRenderer, "", scale = 3.0f) override val layoutOffset: Vec2i = Vec2i(2, 2) @@ -53,7 +53,7 @@ class WorldInfoHUDElement(hudRenderer: HUDRenderer) : HUDElement(hu layout.text = if (hide) { "" } else { - "FPS $fps" + "§aFPS $fps" } }