11package  mezz .jei .common .gui .elements ;
22
33import  mezz .jei .api .gui .builder .ITooltipBuilder ;
4+ import  mezz .jei .api .gui .placement .HorizontalAlignment ;
5+ import  mezz .jei .api .gui .placement .VerticalAlignment ;
46import  mezz .jei .api .gui .widgets .IRecipeWidget ;
57import  mezz .jei .api .gui .widgets .ITextWidget ;
68import  mezz .jei .common .config .DebugConfig ;
7- import  mezz .jei .common .util .HorizontalAlignment ;
89import  mezz .jei .common .util .ImmutableRect2i ;
910import  mezz .jei .common .util .StringUtil ;
10- import  mezz .jei .common .util .VerticalAlignment ;
1111import  mezz .jei .core .util .Pair ;
1212import  net .minecraft .client .Minecraft ;
1313import  net .minecraft .client .gui .Font ;
2222
2323public  class  TextWidget  implements  ITextWidget , IRecipeWidget  {
2424	private  final  List <FormattedText > text ;
25- 	private  final   ImmutableRect2i  area ;
25+ 	private  ImmutableRect2i  availableArea ;
2626
2727	private  HorizontalAlignment  horizontalAlignment ;
2828	private  VerticalAlignment  verticalAlignment ;
2929	private  Font  font ;
3030	private  int  color ;
3131	private  boolean  shadow ;
3232	private  int  lineSpacing ;
33- 	 private   List < FormattedText >  tooltipText  =  List . of (); 
33+ 
3434	private  @ Nullable  List <FormattedText > wrappedText ;
35+ 	private  boolean  truncated  = false ;
3536
3637	public  TextWidget (List <FormattedText > text , int  xPos , int  yPos , int  maxWidth , int  maxHeight ) {
37- 		this .area  = new  ImmutableRect2i (xPos , yPos , maxWidth , maxHeight );
38+ 		this .availableArea  = new  ImmutableRect2i (xPos , yPos , maxWidth , maxHeight );
3839		Minecraft  minecraft  = Minecraft .getInstance ();
3940		this .font  = minecraft .font ;
4041		this .color  = 0xFF000000 ;
@@ -44,88 +45,93 @@ public TextWidget(List<FormattedText> text, int xPos, int yPos, int maxWidth, in
4445		this .verticalAlignment  = VerticalAlignment .TOP ;
4546	}
4647
47- 	@ Override 
48- 	public  ITextWidget  alignHorizontalLeft () {
49- 		this .horizontalAlignment  = HorizontalAlignment .LEFT ;
50- 		return  this ;
48+ 	private  void  invalidateCachedValues () {
49+ 		wrappedText  = null ;
50+ 		truncated  = false ;
5151	}
5252
5353	@ Override 
54- 	public  ITextWidget  alignHorizontalRight () {
55- 		this .horizontalAlignment  = HorizontalAlignment .RIGHT ;
56- 		return  this ;
54+ 	public  int  getWidth () {
55+ 		return  availableArea .width ();
5756	}
5857
5958	@ Override 
60- 	public  ITextWidget  alignHorizontalCenter () {
61- 		this .horizontalAlignment  = HorizontalAlignment .CENTER ;
62- 		return  this ;
59+ 	public  int  getHeight () {
60+ 		return  availableArea .height ();
6361	}
6462
6563	@ Override 
66- 	public  ITextWidget  alignVerticalTop () {
67- 		this .verticalAlignment  = VerticalAlignment .TOP ;
64+ 	public  TextWidget  setPosition (int  xPos , int  yPos ) {
65+ 		this .availableArea  = this .availableArea .setPosition (xPos , yPos );
66+ 		invalidateCachedValues ();
6867		return  this ;
6968	}
7069
7170	@ Override 
72- 	public  ITextWidget  alignVerticalCenter () {
73- 		this .verticalAlignment  = VerticalAlignment .CENTER ;
71+ 	public  TextWidget  setTextAlignment (HorizontalAlignment  horizontalAlignment ) {
72+ 		if  (this .horizontalAlignment .equals (horizontalAlignment )) {
73+ 			return  this ;
74+ 		}
75+ 		this .horizontalAlignment  = horizontalAlignment ;
76+ 		invalidateCachedValues ();
7477		return  this ;
7578	}
7679
7780	@ Override 
78- 	public  ITextWidget  alignVerticalBottom () {
79- 		this .verticalAlignment  = VerticalAlignment .BOTTOM ;
81+ 	public  TextWidget  setTextAlignment (VerticalAlignment  verticalAlignment ) {
82+ 		if  (this .verticalAlignment .equals (verticalAlignment )) {
83+ 			return  this ;
84+ 		}
85+ 		this .verticalAlignment  = verticalAlignment ;
86+ 		invalidateCachedValues ();
8087		return  this ;
8188	}
8289
8390	@ Override 
8491	public  ITextWidget  setFont (Font  font ) {
8592		this .font  = font ;
93+ 		invalidateCachedValues ();
8694		return  this ;
8795	}
8896
8997	@ Override 
9098	public  ITextWidget  setColor (int  color ) {
9199		this .color  = color ;
100+ 		invalidateCachedValues ();
92101		return  this ;
93102	}
94103
95104	@ Override 
96105	public  ITextWidget  setLineSpacing (int  lineSpacing ) {
97106		this .lineSpacing  = lineSpacing ;
107+ 		invalidateCachedValues ();
98108		return  this ;
99109	}
100110
101111	@ Override 
102112	public  ITextWidget  setShadow (boolean  shadow ) {
103113		this .shadow  = shadow ;
114+ 		invalidateCachedValues ();
104115		return  this ;
105116	}
106117
107118	@ Override 
108119	public  ScreenPosition  getPosition () {
109- 		return  area .getScreenPosition ();
120+ 		return  availableArea .getScreenPosition ();
110121	}
111122
112123	private  List <FormattedText > calculateWrappedText () {
113124		if  (wrappedText  != null ) {
114125			return  wrappedText ;
115126		}
116127		int  lineHeight  = getLineHeight ();
117- 		int  maxLines  = area .height () / lineHeight ;
118- 		if  (maxLines  * lineHeight  + font .lineHeight  <= area .height ()) {
128+ 		int  maxLines  = availableArea .height () / lineHeight ;
129+ 		if  (maxLines  * lineHeight  + font .lineHeight  <= availableArea .height ()) {
119130			maxLines ++;
120131		}
121- 		Pair <List <FormattedText >, Boolean > result  = StringUtil .splitLines (font , text , area .width (), maxLines );
132+ 		Pair <List <FormattedText >, Boolean > result  = StringUtil .splitLines (font , text , availableArea .width (), maxLines );
122133		this .wrappedText  = result .first ();
123- 		boolean  truncated  = result .second ();
124- 		if  (truncated ) {
125- 			this .tooltipText  = text ;
126- 		} else  {
127- 			this .tooltipText  = List .of ();
128- 		}
134+ 		this .truncated  = result .second ();
129135		return  wrappedText ;
130136	}
131137
@@ -148,38 +154,30 @@ public void drawWidget(GuiGraphics guiGraphics, double mouseX, double mouseY) {
148154		}
149155
150156		if  (DebugConfig .isDebugGuisEnabled ()) {
151- 			guiGraphics .fill (0 ,0 , area .width (), area .height (), 0xAAAAAA00 );
157+ 			guiGraphics .fill (0 ,0 , availableArea .width (), availableArea .height (), 0xAAAAAA00 );
152158		}
153159	}
154160
155161	@ Override 
156162	public  void  getTooltip (ITooltipBuilder  tooltip , double  mouseX , double  mouseY ) {
157- 		if  (mouseX  >= 0  && mouseX  < area .width () && mouseY  >= 0  && mouseY  < area .height ()) {
163+ 		if  (mouseX  >= 0  && mouseX  < availableArea .width () && mouseY  >= 0  && mouseY  < availableArea .height ()) {
158164			calculateWrappedText ();
159- 			tooltip .addAll (tooltipText );
165+ 			if  (truncated ) {
166+ 				tooltip .addAll (text );
167+ 			}
160168		}
161169	}
162170
163171	private  int  getXPos (FormattedCharSequence  text ) {
164- 		return  switch  (horizontalAlignment ) {
165- 			case  LEFT  -> 0 ;
166- 			case  RIGHT  -> this .area .width () - font .width (text );
167- 			case  CENTER  -> Math .round ((this .area .width () - font .width (text )) / 2f );
168- 		};
172+ 		return  getXPos (font .width (text ));
169173	}
170174
171- 	private  int  getYPosStart (int  lineHeight , List <FormattedText > text ) {
172- 		if  (verticalAlignment  == VerticalAlignment .TOP ) {
173- 			return  0 ;
174- 		}
175+ 	private  int  getXPos (int  lineWidth ) {
176+ 		return  horizontalAlignment .getXPos (this .availableArea .width (), lineWidth );
177+ 	}
175178
179+ 	private  int  getYPosStart (int  lineHeight , List <FormattedText > text ) {
176180		int  linesHeight  = (lineHeight  * text .size ()) - lineSpacing  - 1 ;
177- 		if  (verticalAlignment  == VerticalAlignment .BOTTOM ) {
178- 			return  area .height () - linesHeight ;
179- 		} else  if  (verticalAlignment  == VerticalAlignment .CENTER ) {
180- 			return  Math .round ((area .height () - linesHeight ) / 2f );
181- 		} else  {
182- 			throw  new  IllegalArgumentException ("Unknown verticalAlignment "  + verticalAlignment );
183- 		}
181+ 		return  verticalAlignment .getYPos (this .availableArea .height (), linesHeight );
184182	}
185183}
0 commit comments