@@ -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