44import dev .ftb .mods .ftblibrary .icon .Icon ;
55import dev .ftb .mods .ftblibrary .math .Bits ;
66import dev .ftb .mods .ftblibrary .util .TooltipList ;
7+ import net .minecraft .client .gui .ActiveTextCollector ;
78import net .minecraft .client .gui .GuiGraphics ;
89import net .minecraft .network .chat .Component ;
9- import net .minecraft .network .chat .FormattedText ;
10+ import net .minecraft .network .chat .Style ;
11+ import net .minecraft .util .FormattedCharSequence ;
1012import net .minecraft .util .Mth ;
1113
14+ import java .util .Optional ;
15+
1216
1317public class TextField extends Widget {
1418 public int textFlags = 0 ;
@@ -18,7 +22,7 @@ public class TextField extends Widget {
1822 public float scale = 1.0F ;
1923 public Color4I textColor = Icon .empty ();
2024 public boolean trim = false ;
21- private FormattedText [] formattedText = new FormattedText [0 ];
25+ private FormattedCharSequence [] formattedText = new FormattedCharSequence [0 ];
2226 private Component rawText = Component .empty ();
2327 private boolean tooltip = false ;
2428
@@ -70,7 +74,7 @@ public TextField setText(Component txt) {
7074 var theme = getGui ().getTheme ();
7175
7276 rawText = txt ;
73- formattedText = theme .listFormattedStringToWidth ( Component .literal ("" ).append (txt ), maxWidth ).toArray (new FormattedText [0 ]);
77+ formattedText = theme .getFont (). split ( Component .literal ("" ).append (txt ), maxWidth ).toArray (new FormattedCharSequence [0 ]);
7478
7579 return resize (theme );
7680 }
@@ -82,7 +86,7 @@ public TextField setText(String txt) {
8286 public TextField reflow () {
8387 var theme = getGui ().getTheme ();
8488
85- formattedText = theme .listFormattedStringToWidth ( rawText , (int ) (maxWidth / scale )).toArray (new FormattedText [0 ]);
89+ formattedText = theme .getFont (). split ( rawText , (int ) (maxWidth / scale )).toArray (new FormattedCharSequence [0 ]);
8690
8791 return resize (theme );
8892 }
@@ -109,8 +113,8 @@ public void addMouseOverText(TooltipList list) {
109113 public void drawBackground (GuiGraphics graphics , Theme theme , int x , int y , int w , int h ) {
110114 }
111115
112- private FormattedText [] getDisplayedText () {
113- return trim && formattedText .length > 0 ? new FormattedText []{formattedText [0 ]} : formattedText ;
116+ private FormattedCharSequence [] getDisplayedText () {
117+ return trim && formattedText .length > 0 ? new FormattedCharSequence []{formattedText [0 ]} : formattedText ;
114118 }
115119
116120 @ Override
@@ -146,17 +150,20 @@ public void draw(GuiGraphics graphics, Theme theme, int x, int y, int w, int h)
146150 }
147151 }
148152
149- // TODO: @since 21.11: I have no idea how to fix componentStyleAtWidth as this has been completely removed.
150- // public Optional<Style> getComponentStyleAt(Theme theme, int mouseX, int mouseY) {
151- // int line = (mouseY - getY()) / theme.getFontHeight();
152- // if (line >= 0 && line < getDisplayedText().length) {
153- // boolean centered = Bits.getFlag(textFlags, Theme.CENTERED);
154- // int textWidth = theme.getFont().width(formattedText[line]);
155- // int xStart = centered ? getX() + (width - textWidth) / 2 : getX();
156- // if (mouseX >= xStart && mouseX <= xStart + textWidth) {
157- // return Optional.ofNullable(theme.getFont().getSplitter().componentStyleAtWidth(formattedText[line], mouseX - xStart));
158- // }
159- // }
160- // return Optional.empty();
161- // }
153+ public Optional <Style > getClickableStyleAt (Theme theme , int mouseX , int mouseY ) {
154+ int line = (mouseY - getY ()) / textSpacing ;
155+
156+ if (line >= 0 && line < getDisplayedText ().length ) {
157+ boolean centered = Bits .getFlag (textFlags , Theme .CENTERED );
158+ int textWidth = theme .getFont ().width (formattedText [line ]);
159+ int xStart = centered ? getX () + (width - textWidth ) / 2 : getX ();
160+
161+ if (mouseX >= xStart && mouseX <= xStart + textWidth ) {
162+ var finder = new ActiveTextCollector .ClickableStyleFinder (theme .getFont (), mouseX , mouseY );
163+ finder .accept (xStart , getY () + line * textSpacing , formattedText [line ]);
164+ return Optional .ofNullable (finder .result ());
165+ }
166+ }
167+ return Optional .empty ();
168+ }
162169}
0 commit comments