@@ -42,9 +42,7 @@ public void draw(ModularGuiContext context, WidgetThemeEntry<?> widgetTheme) {
4242 @ Override
4343 public void drawForeground (ModularGuiContext context ) {
4444 super .drawForeground (context );
45- Object o = this .text .getHoveringElement (context .getFontRenderer (), context .getMouseX (), context .getMouseY ());
46- //ModularUI.LOGGER.info("Mouse {}, {}", context.getMouseX(), context.getMouseY());
47- if (o instanceof IHoverable hoverable ) {
45+ if (getHoveredElement (context ) instanceof IHoverable hoverable ) {
4846 hoverable .onHover ();
4947 RichTooltip tooltip = hoverable .getTooltip ();
5048 if (tooltip != null ) {
@@ -55,83 +53,91 @@ public void drawForeground(ModularGuiContext context) {
5553
5654 @ Override
5755 public @ NotNull Result onMousePressed (int mouseButton ) {
58- Object o = this .text .getHoveringElement (getContext ().getFontRenderer (), getContext ().getMouseX (), getContext ().getMouseY ());
59- if (o instanceof Interactable interactable ) {
56+ if (getHoveredElement () instanceof Interactable interactable ) {
6057 return interactable .onMousePressed (mouseButton );
6158 }
6259 return Result .ACCEPT ;
6360 }
6461
6562 @ Override
6663 public boolean onMouseRelease (int mouseButton ) {
67- Object o = this .text .getHoveringElement (getContext ().getFontRenderer (), getContext ().getMouseX (), getContext ().getMouseY ());
68- if (o instanceof Interactable interactable ) {
64+ if (getHoveredElement () instanceof Interactable interactable ) {
6965 return interactable .onMouseRelease (mouseButton );
7066 }
7167 return false ;
7268 }
7369
7470 @ Override
7571 public @ NotNull Result onMouseTapped (int mouseButton ) {
76- Object o = this .text .getHoveringElement (getContext ().getFontRenderer (), getContext ().getMouseX (), getContext ().getMouseY ());
77- if (o instanceof Interactable interactable ) {
72+ if (getHoveredElement () instanceof Interactable interactable ) {
7873 return interactable .onMouseTapped (mouseButton );
7974 }
8075 return Result .IGNORE ;
8176 }
8277
8378 @ Override
8479 public @ NotNull Result onKeyPressed (char typedChar , int keyCode ) {
85- Object o = this .text .getHoveringElement (getContext ().getFontRenderer (), getContext ().getMouseX (), getContext ().getMouseY ());
86- if (o instanceof Interactable interactable ) {
80+ if (getHoveredElement () instanceof Interactable interactable ) {
8781 return interactable .onKeyPressed (typedChar , keyCode );
8882 }
8983 return Result .ACCEPT ;
9084 }
9185
9286 @ Override
9387 public boolean onKeyRelease (char typedChar , int keyCode ) {
94- Object o = this .text .getHoveringElement (getContext ().getFontRenderer (), getContext ().getMouseX (), getContext ().getMouseY ());
95- if (o instanceof Interactable interactable ) {
88+ if (getHoveredElement () instanceof Interactable interactable ) {
9689 return interactable .onKeyRelease (typedChar , keyCode );
9790 }
9891 return false ;
9992 }
10093
10194 @ Override
10295 public @ NotNull Result onKeyTapped (char typedChar , int keyCode ) {
103- Object o = this .text .getHoveringElement (getContext ().getFontRenderer (), getContext ().getMouseX (), getContext ().getMouseY ());
104- if (o instanceof Interactable interactable ) {
96+ if (getHoveredElement () instanceof Interactable interactable ) {
10597 return interactable .onKeyTapped (typedChar , keyCode );
10698 }
10799 return Result .ACCEPT ;
108100 }
109101
110102 @ Override
111103 public boolean onMouseScroll (UpOrDown scrollDirection , int amount ) {
112- Object o = this .text .getHoveringElement (getContext ().getFontRenderer (), getContext ().getMouseX (), getContext ().getMouseY ());
113- if (o instanceof Interactable interactable ) {
104+ if (getHoveredElement () instanceof Interactable interactable ) {
114105 return interactable .onMouseScroll (scrollDirection , amount );
115106 }
116107 return false ;
117108 }
118109
119110 @ Override
120111 public void onMouseDrag (int mouseButton , long timeSinceClick ) {
121- Object o = this .text .getHoveringElement (getContext ().getFontRenderer (), getContext ().getMouseX (), getContext ().getMouseY ());
122- if (o instanceof Interactable interactable ) {
112+ if (getHoveredElement () instanceof Interactable interactable ) {
123113 interactable .onMouseDrag (mouseButton , timeSinceClick );
124114 }
125115 }
126116
117+ /**
118+ * Returns the currently hovered element of this rich text or {@code null} if none is hovered.
119+ * Note that this method assumes, that the {@link com.cleanroommc.modularui.api.layout.IViewportStack IViewportStack}
120+ * is transformed to this widget.
121+ *
122+ * @return hovered element or null
123+ */
127124 @ Nullable
128125 public Object getHoveredElement () {
126+ return getHoveredElement (getContext ());
127+ }
128+
129+ /**
130+ * Returns the currently hovered element of this rich text or {@code null} if none is hovered.
131+ * Note that this method assumes, that the {@link com.cleanroommc.modularui.api.layout.IViewportStack IViewportStack}
132+ * is transformed to this widget.
133+ *
134+ * @param context the viewport stack with transformation to this widget
135+ * @return hovered element or null
136+ */
137+ @ Nullable
138+ public Object getHoveredElement (ModularGuiContext context ) {
129139 if (!isHovering ()) return null ;
130- getContext ().pushMatrix ();
131- getContext ().translate (getArea ().x , getArea ().y );
132- Object o = this .text .getHoveringElement (getContext ());
133- getContext ().popMatrix ();
134- return o ;
140+ return this .text .getHoveringElement (context );
135141 }
136142
137143 @ Override
0 commit comments