44import com .cleanroommc .modularui .api .drawable .IDrawable ;
55import com .cleanroommc .modularui .api .drawable .IIcon ;
66import com .cleanroommc .modularui .api .drawable .IKey ;
7+ import com .cleanroommc .modularui .api .widget .IWidget ;
78import com .cleanroommc .modularui .drawable .*;
89import com .cleanroommc .modularui .screen .viewport .GuiContext ;
910import com .cleanroommc .modularui .utils .Alignment ;
2728
2829public class Tooltip {
2930
31+ private final IWidget parent ;
3032 private final List <IDrawable > lines = new ArrayList <>();
3133 private List <IDrawable > additionalLines = new ArrayList <>();
32- private Area excludeArea ;
3334 private Pos pos = ModularUIConfig .tooltipPos ;
3435 private boolean customPos = false ;
3536 private Consumer <Tooltip > tooltipBuilder ;
@@ -47,6 +48,10 @@ public class Tooltip {
4748
4849 private boolean dirty = true ;
4950
51+ public Tooltip (IWidget parent ) {
52+ this .parent = parent ;
53+ }
54+
5055 public void buildTooltip () {
5156 this .dirty = false ;
5257 this .lines .clear ();
@@ -136,24 +141,24 @@ public Rectangle determineTooltipArea(GuiContext context, List<IDrawable> lines,
136141 }
137142
138143 if (this .pos == Pos .NEXT_TO_MOUSE ) {
139- final int PADDING = 8 ;
144+ final int padding = 8 ;
140145 // magic number to place tooltip nicer. Look at GuiScreen#L237
141- final int MOUSE_OFFSET = 12 ;
142- int x = mouseX + MOUSE_OFFSET , y = mouseY - MOUSE_OFFSET ;
143- if (x < PADDING ) {
144- x = PADDING ;
145- } else if (x + width + PADDING > screenWidth ) {
146- x -= MOUSE_OFFSET * 2 + width ; // flip side of cursor
147- if (x < PADDING ) {
148- x = PADDING ;
146+ final int mouseOffset = 12 ;
147+ int x = mouseX + mouseOffset , y = mouseY - mouseOffset ;
148+ if (x < padding ) {
149+ x = padding ;
150+ } else if (x + width + padding > screenWidth ) {
151+ x -= mouseOffset * 2 + width ; // flip side of cursor
152+ if (x < padding ) {
153+ x = padding ;
149154 }
150155 }
151- y = MathHelper .clamp (y , PADDING , screenHeight - PADDING - height );
156+ y = MathHelper .clamp (y , padding , screenHeight - padding - height );
152157 return new Rectangle (x , y , width , height );
153158 }
154159
155- if (this .excludeArea == null ) {
156- throw new IllegalStateException ();
160+ if (this .parent == null ) {
161+ throw new IllegalStateException ("Tooltip pos is " + this . pos . name () + ", but no widget parent is set!" );
157162 }
158163
159164 int minWidth = 0 ;
@@ -168,13 +173,16 @@ public Rectangle determineTooltipArea(GuiContext context, List<IDrawable> lines,
168173 int shiftAmount = 10 ;
169174 int padding = 7 ;
170175
176+ Area area = Area .SHARED ;
177+ area .set (this .parent .getArea ());
178+ area .setPos (0 , 0 ); // context is transformed to this widget
179+ area .transformAndRectanglerize (context );
171180 int x = 0 , y = 0 ;
172181 if (this .pos .vertical ) {
173- int xArea = this .excludeArea .x ;
174- if (width < this .excludeArea .width ) {
175- x = xArea + shiftAmount ;
182+ if (width < area .width ) {
183+ x = area .x + shiftAmount ;
176184 } else {
177- x = xArea - shiftAmount ;
185+ x = area . x - shiftAmount ;
178186 if (x < padding ) {
179187 x = padding ;
180188 } else if (x + width > screenWidth - padding ) {
@@ -188,33 +196,32 @@ public Rectangle determineTooltipArea(GuiContext context, List<IDrawable> lines,
188196
189197 Pos pos = this .pos ;
190198 if (this .pos == Pos .VERTICAL ) {
191- int bottomSpace = screenHeight - this . excludeArea .ey ();
192- pos = bottomSpace < height + padding && bottomSpace < this . excludeArea .y ? Pos .ABOVE : Pos .BELOW ;
199+ int bottomSpace = screenHeight - area .ey ();
200+ pos = bottomSpace < height + padding && bottomSpace < area .y ? Pos .ABOVE : Pos .BELOW ;
193201 }
194202
195203 if (pos == Pos .BELOW ) {
196- y = this . excludeArea . y + this . excludeArea . height + padding ;
204+ y = area . ey () + padding ;
197205 } else if (pos == Pos .ABOVE ) {
198- y = this . excludeArea .y - height - padding ;
206+ y = area .y - height - padding ;
199207 }
200208 } else if (this .pos .horizontal ) {
201209 boolean usedMoreSpaceSide = false ;
202210 Pos pos = this .pos ;
203211 if (this .pos == Pos .HORIZONTAL ) {
204- if (this . excludeArea . x > screenWidth - this . excludeArea . x - this . excludeArea . width ) {
212+ if (area . x > screenWidth - area . ex () ) {
205213 pos = Pos .LEFT ;
206214 x = 0 ;
207215 } else {
208216 pos = Pos .RIGHT ;
209- x = screenWidth - this . excludeArea . x - this . excludeArea . width + padding ;
217+ x = screenWidth - area . ex () + padding ;
210218 }
211219 }
212220
213- int yArea = this .excludeArea .y ;
214- if (height < this .excludeArea .height ) {
215- y = yArea + shiftAmount ;
221+ if (height < area .height ) {
222+ y = area .y + shiftAmount ;
216223 } else {
217- y = yArea - shiftAmount ;
224+ y = area . y - shiftAmount ;
218225 if (y < padding ) {
219226 y = padding ;
220227 }
@@ -223,9 +230,9 @@ public Rectangle determineTooltipArea(GuiContext context, List<IDrawable> lines,
223230 if (x + width > screenWidth - padding ) {
224231 int maxWidth ;
225232 if (pos == Pos .LEFT ) {
226- maxWidth = Math .max (minWidth , this . excludeArea .x - padding * 2 );
233+ maxWidth = Math .max (minWidth , area .x - padding * 2 );
227234 } else {
228- maxWidth = Math .max (minWidth , screenWidth - this . excludeArea . x - this . excludeArea . width - padding * 2 );
235+ maxWidth = Math .max (minWidth , screenWidth - area . ex () - padding * 2 );
229236 }
230237 usedMoreSpaceSide = true ;
231238 renderer .setAlignment (this .alignment , maxWidth );
@@ -235,14 +242,14 @@ public Rectangle determineTooltipArea(GuiContext context, List<IDrawable> lines,
235242 }
236243
237244 if (this .pos == Pos .HORIZONTAL && !usedMoreSpaceSide ) {
238- int rightSpace = screenWidth - this . excludeArea . x - this . excludeArea . width ;
239- pos = rightSpace < width + padding && rightSpace < this . excludeArea .x ? Pos .LEFT : Pos .RIGHT ;
245+ int rightSpace = screenWidth - area . ex () ;
246+ pos = rightSpace < width + padding && rightSpace < area .x ? Pos .LEFT : Pos .RIGHT ;
240247 }
241248
242249 if (pos == Pos .RIGHT ) {
243- x = this . excludeArea . x + this . excludeArea . width + padding ;
250+ x = area . ex () + padding ;
244251 } else if (pos == Pos .LEFT ) {
245- x = this . excludeArea .x - width - padding ;
252+ x = area .x - width - padding ;
246253 }
247254 }
248255 return new Rectangle (x , y , width , height );
@@ -259,10 +266,6 @@ public void markDirty() {
259266 this .dirty = true ;
260267 }
261268
262- public Area getExcludeArea () {
263- return this .excludeArea ;
264- }
265-
266269 public int getShowUpTimer () {
267270 return this .showUpTimer ;
268271 }
@@ -280,11 +283,6 @@ public boolean hasTitleMargin() {
280283 return this .hasTitleMargin ;
281284 }
282285
283- public Tooltip excludeArea (Area area ) {
284- this .excludeArea = area ;
285- return this ;
286- }
287-
288286 public Tooltip pos (Pos pos ) {
289287 this .customPos = true ;
290288 this .pos = pos ;
0 commit comments