Skip to content

Commit 4929d08

Browse files
caoimhebyrneSychic
andauthored
UIWrappedText: Fix single-character strings not being rendered (#142)
By default, UIWrappedText will set its width to the width of the text string. When it is rendering the text, it'll check if the width of the component is less than or equal to the width of a character (in this case 'x'), this is flawed for a few reasons: 1. Different characters have different widths 2. The comment "if we are smaller than a character" doesn't match up with the behavior (less than or equal to) 3. `UGraphics.getCharWidth` (used to get the width of 'x') does not take the `fontProvider` into account, meaning that the width that it calculates for that character could be wrong. To solve this, the check has been removed, as it is flawed and doesn't seem to have an explicit error, contrary to what the comment in the code said. The only problem found with this was that `getStringSplitToWidth` would push an extra line onto the list if the container wasn't long enough to render one character. This has been fixed by adding a `isNotEmpty` check before pushing the line (that's okay to do in this case, as it's not a newline check). The only "breaking change" about this, is that if your container is less than "a character" wide, they will still render. If you for some reason want to avoid this, put a `ScissorEffect` onto your `UIWrappedText`, but this shouldn't really be necessary. Co-authored-by: Sychic <[email protected]>
1 parent 1b5dde2 commit 4929d08

File tree

2 files changed

+1
-7
lines changed

2 files changed

+1
-7
lines changed

src/main/kotlin/gg/essential/elementa/components/UIWrappedText.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,6 @@ open class UIWrappedText @JvmOverloads constructor(
147147
return super.draw(matrixStack)
148148
}
149149

150-
if (width / textScale <= charWidth) {
151-
// If we are smaller than a char, we can't physically split this string into
152-
// "width" strings, so we'll prefer a no-op to an error.
153-
return super.draw(matrixStack)
154-
}
155-
156150
if (!URenderPipeline.isRequired && !ElementaVersion.atLeastV9Active) {
157151
@Suppress("DEPRECATION")
158152
UGraphics.enableBlend()

src/main/kotlin/gg/essential/elementa/utils/text.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fun getStringSplitToWidth(
129129
pushLine()
130130

131131
for (char in word.toCharArray()) {
132-
if ((currLine.toString() + char).width(textScale, fontProvider) > maxLineWidthSpace)
132+
if (currLine.isNotEmpty() && (currLine.toString() + char).width(textScale, fontProvider) > maxLineWidthSpace)
133133
pushLine()
134134
currLine.append(char)
135135
}

0 commit comments

Comments
 (0)