Skip to content

Commit 8965fae

Browse files
committed
fix: overlapping text in some locales/scalings (#9)
1 parent 0d90ef9 commit 8965fae

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

common/src/main/java/io/github/thebossmagnus/mods/config_manager/common/screen/Gui.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,27 @@ protected void init() {
8080
this.addRenderableWidget(updateButton);
8181
this.addRenderableWidget(closeButton);
8282

83+
// Calculate label positioning (leave 15px gap on top of buttons and 5% on the window border, then use remaining space above )
84+
int buttonY = (int) ((this.height - buttonHeight) * 0.6);
85+
int availableHeight = (int) (buttonY - 15 - this.height*0.05); // Available height from top to text end position
86+
8387
updateWarningsLabel = new MultilineLabelWidget(
8488
this.font,
8589
updateWarnings,
8690
(int) ((this.width - buttonWidth) * 0.15),
87-
(int) ((this.height - buttonHeight) * 0.6) - 90,
91+
(int) (this.height*0.05),
8892
buttonWidth,
93+
availableHeight,
8994
true
9095
);
9196

9297
resetWarningsLabel = new MultilineLabelWidget(
9398
this.font,
9499
resetWarnings,
95100
(int) ((this.width - buttonWidth) * 0.85),
96-
(int) ((this.height - buttonHeight) * 0.6) - 90,
101+
(int) (this.height*0.05),
97102
buttonWidth,
103+
availableHeight,
98104
true
99105
);
100106
}

common/src/main/java/io/github/thebossmagnus/mods/config_manager/common/screen/MultilineLabelWidget.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,32 @@ public class MultilineLabelWidget implements Renderable {
1919
private final int x;
2020
private final int y;
2121
private final int width;
22+
private final int maxHeight;
2223
private final boolean centered;
2324

2425
/**
25-
* Constructs a MultilineLabelWidget.
26+
* Constructs a MultilineLabelWidget
2627
*
27-
* @param font Font renderer used for drawing text
28-
* @param text The text component to display (can contain formatting and siblings)
29-
* @param x X position of the widget
30-
* @param y Y position of the widget
31-
* @param width Width of the widget (used for wrapping and centering)
32-
* @param centered Whether to center the text horizontally
28+
* @param font Font renderer used for drawing text
29+
* @param text The text component to display (can contain formatting and siblings)
30+
* @param x X position of the widget
31+
* @param y Y position of the widget
32+
* @param width Width of the widget (used for wrapping and centering)
33+
* @param maxHeight Maximum height available (text will scale to fit)
34+
* @param centered Whether to center the text horizontally
3335
*/
34-
public MultilineLabelWidget(Font font, Component text, int x, int y, int width, boolean centered) {
36+
public MultilineLabelWidget(Font font, Component text, int x, int y, int width, int maxHeight, boolean centered) {
3537
this.font = font;
3638
this.text = text;
3739
this.x = x;
3840
this.y = y;
3941
this.width = width;
42+
this.maxHeight = maxHeight;
4043
this.centered = centered;
4144
}
4245

4346
/**
44-
* Renders the multi-line label with automatic word wrapping.
47+
* Renders the multi-line label with automatic word wrapping
4548
*
4649
* @param guiGraphics The graphics context
4750
* @param mouseX Mouse X position
@@ -64,15 +67,34 @@ public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, flo
6467
outputLines.addAll(wrapped);
6568
}
6669

67-
int currentY = y;
70+
int lineHeight = computeLineHeight(outputLines);
71+
72+
// Calculate the actual total height used by the label
73+
int totalHeight = outputLines.size() * lineHeight;
74+
75+
// Start text in a height that makes it end right and the start of the button + padding
76+
int currentY = y + maxHeight - totalHeight;
77+
6878
for (FormattedCharSequence line : outputLines) {
6979
int drawX = centered ? x + width / 2 : x;
7080
if (centered) {
7181
guiGraphics.drawCenteredString(font, line, drawX, currentY, 0xFFFFFFFF);
7282
} else {
7383
guiGraphics.drawString(font, line, drawX, currentY, 0xFFFFFFFF);
7484
}
75-
currentY += font.lineHeight + 1;
85+
currentY += lineHeight;
7686
}
7787
}
88+
89+
/**
90+
* computes a good line height based on maxHeight
91+
*
92+
* @param lines A list with all lines to be rendered
93+
*/
94+
95+
private int computeLineHeight(List<FormattedCharSequence> lines) {
96+
int lineHeight = font.lineHeight + 1; // Default line height with a small padding
97+
float scale = Math.min(1.0f, (float) maxHeight / (lines.size() * lineHeight)); //if there's enough space, use normal line height else scale down
98+
return Math.round(lineHeight * scale);
99+
}
78100
}

0 commit comments

Comments
 (0)