2020import com .cleanroommc .modularui .theme .TextFieldTheme ;
2121import com .cleanroommc .modularui .theme .WidgetTheme ;
2222import com .cleanroommc .modularui .theme .WidgetThemeEntry ;
23+ import com .cleanroommc .modularui .utils .Alignment ;
2324import com .cleanroommc .modularui .utils .HoveredWidgetList ;
2425import com .cleanroommc .modularui .widget .Widget ;
2526import com .cleanroommc .modularui .widget .scroll .ScrollArea ;
2627import com .cleanroommc .modularui .widget .scroll .ScrollData ;
2728import com .cleanroommc .modularui .widget .sizer .Area ;
29+ import com .cleanroommc .modularui .widget .sizer .Box ;
2830import org .jetbrains .annotations .NotNull ;
2931import org .jetbrains .annotations .Nullable ;
3032
@@ -35,13 +37,13 @@ public class ScrollableTextWidget extends Widget<ScrollableTextWidget>
3537 RecipeViewerIngredientProvider {
3638
3739 private final RichText text = new RichText ();
38- private Consumer <RichText > builder ;
40+ private Consumer <IRichTextBuilder <?> > builder ;
3941 private boolean dirty = false ;
4042 private boolean autoUpdate = false ;
4143 private Object lastIngredient ;
4244
4345 private final ScrollArea scroll = new ScrollArea ();
44- private final TextRenderer renderer = new TextRenderer ();
46+ private final TextRenderer renderer = new ScrollingTextRenderer ();
4547
4648 public ScrollableTextWidget () {
4749 listenGuiAction ((IGuiAction .MouseReleased ) mouseButton -> {
@@ -176,14 +178,27 @@ private void drawText(ModularGuiContext context) {
176178 this .dirty = false ;
177179 }
178180
179- TextFieldTheme textFieldTheme = context .getTheme ().getTextFieldTheme ().getTheme ();
180- this .text .setupRenderer (this .renderer , getArea ().getPadding ().getLeft (),
181- getArea ().getPadding ().getTop () - getScrollY (), getArea ().paddedWidth (), getArea ().paddedHeight (),
182- textFieldTheme .getTextColor (), textFieldTheme .getTextShadow ());
183- this .text .compileAndDraw (this .renderer , context , false );
181+ Alignment alignment = this .text .getAlignment ();
182+ Area area = getArea ();
183+ Box padding = area .getPadding ();
184+ WidgetThemeEntry <TextFieldTheme > textThemeEntry = context .getTheme ().getTextFieldTheme ();
185+ TextFieldTheme textTheme = textThemeEntry .getTheme ();
186+
187+ this .text .compileAndDraw (this .renderer , context , true );
188+
184189 // this isn't perfect, but i hope it's good enough
185- int diff = (int ) Math .ceil ((this .renderer .getLastTrimmedHeight () - getArea ().h ()) / 2 );
186- this .scroll .getScrollY ().setScrollSize (getArea ().h () + Math .max (0 , diff ));
190+ int diff = (int ) Math .ceil ((this .renderer .getLastTrimmedHeight () - area .h ()) / 2 );
191+ this .scroll .getScrollY ().setScrollSize (area .h () + Math .max (0 , diff ));
192+
193+ // this is responsible for centering the text if there's not enough to scroll
194+ int x = padding .getLeft ();
195+ int y = (int ) (area .h () * alignment .y );
196+ y -= (int ) (this .renderer .getLastTrimmedHeight () * alignment .y );
197+ y = Math .min (Math .max (padding .getTop (), y ), area .h () - padding .getBottom ());
198+ this .text .setupRenderer (this .renderer , x , y - getScrollY (), area .paddedWidth (), area .paddedHeight (),
199+ textTheme .getTextColor (), textTheme .getTextShadow ());
200+
201+ this .text .compileAndDraw (this .renderer , context , false );
187202 }
188203
189204 @ Override
@@ -222,7 +237,7 @@ public ScrollableTextWidget autoUpdate(boolean autoUpdate) {
222237 * @param builder text builder
223238 * @return this
224239 */
225- public ScrollableTextWidget textBuilder (Consumer <RichText > builder ) {
240+ public ScrollableTextWidget textBuilder (Consumer <IRichTextBuilder <?> > builder ) {
226241 this .builder = builder ;
227242 markDirty ();
228243 return this ;
@@ -232,4 +247,25 @@ public ScrollableTextWidget textBuilder(Consumer<RichText> builder) {
232247 public @ Nullable Object getIngredient () {
233248 return this .lastIngredient ;
234249 }
250+
251+ public static class ScrollingTextRenderer extends TextRenderer {
252+
253+ @ Override
254+ protected int getStartX (float maxWidth , float lineWidth ) {
255+ return super .getStartX (this .maxWidth , lineWidth );
256+ }
257+
258+ public int getLastY () {
259+ return (int ) lastY ;
260+ }
261+
262+ public int getLastX () {
263+ return (int ) lastX ;
264+ }
265+
266+ @ Override
267+ protected int getStartY (float height ) {
268+ return this .y ; // always draw at the top
269+ }
270+ }
235271}
0 commit comments