Skip to content

Commit 90d5f2f

Browse files
ghzdudebrachy84
andauthored
Improve Rich Text (#124)
* add overloads, methods, and improve rich text * fix off by one trim * no need to translate * increment x by space width * add back translate in ClientScreenHandler * fix --------- Co-authored-by: brachy84 <[email protected]>
1 parent 5c0d8fc commit 90d5f2f

File tree

4 files changed

+58
-27
lines changed

4 files changed

+58
-27
lines changed

src/main/java/com/cleanroommc/modularui/drawable/text/RichText.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.cleanroommc.modularui.drawable.text;
22

3-
import com.cleanroommc.modularui.api.drawable.*;
3+
import com.cleanroommc.modularui.api.drawable.IDrawable;
4+
import com.cleanroommc.modularui.api.drawable.IIcon;
5+
import com.cleanroommc.modularui.api.drawable.IKey;
6+
import com.cleanroommc.modularui.api.drawable.IRichTextBuilder;
7+
import com.cleanroommc.modularui.api.drawable.ITextLine;
48
import com.cleanroommc.modularui.screen.viewport.GuiContext;
59
import com.cleanroommc.modularui.theme.WidgetTheme;
610
import com.cleanroommc.modularui.utils.Alignment;
7-
811
import com.cleanroommc.modularui.utils.TooltipLines;
912

1013
import net.minecraft.client.gui.FontRenderer;
@@ -266,11 +269,23 @@ public void draw(GuiContext context, int x, int y, int width, int height, Widget
266269
}
267270

268271
public void draw(GuiContext context, int x, int y, int width, int height, int color, boolean shadow) {
272+
draw(renderer, context, x, y, width, height, color, shadow);
273+
}
274+
275+
public void draw(TextRenderer renderer, GuiContext context, int x, int y, int width, int height, int color, boolean shadow) {
269276
renderer.setSimulate(false);
270277
setupRenderer(renderer, x, y, width, height, color, shadow);
271278
this.cachedText = renderer.compileAndDraw(context, this.elements);
272279
}
273280

281+
public int getLastHeight() {
282+
return (int) renderer.getLastTrimmedHeight();
283+
}
284+
285+
public int getLastWidth() {
286+
return (int) renderer.getLastTrimmedWidth();
287+
}
288+
274289
public void setupRenderer(TextRenderer renderer, int x, int y, float width, float height, int color, boolean shadow) {
275290
renderer.setPos(x, y);
276291
renderer.setScale(this.scale);
@@ -286,6 +301,14 @@ public List<ITextLine> compileAndDraw(TextRenderer renderer, GuiContext context,
286301
return this.cachedText;
287302
}
288303

304+
/**
305+
* Returns the currently hovered element of this rich text or {@code null} if none is hovered.
306+
* Note that this method assumes, that the {@link com.cleanroommc.modularui.api.layout.IViewportStack IViewportStack}
307+
* is transformed to 0,0 of this {@link IDrawable}.
308+
*
309+
* @param context the viewport stack with transformation to this widget
310+
* @return hovered element or null
311+
*/
289312
public Object getHoveringElement(GuiContext context) {
290313
return getHoveringElement(context.getFontRenderer(), context.getMouseX(), context.getMouseY());
291314
}

src/main/java/com/cleanroommc/modularui/screen/ClientScreenHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,9 @@ public static void drawDebugScreen(@Nullable ModularScreen muiScreen, @Nullable
588588
} else if (hovered instanceof RichTextWidget richTextWidget) {
589589
drawSegmentLine(lineY -= 4, scale, color);
590590
lineY -= 10;
591+
locatedHovered.applyMatrix(context);
591592
Object hoveredElement = richTextWidget.getHoveredElement();
593+
locatedHovered.unapplyMatrix(context);
592594
GuiDraw.drawText("Hovered: " + hoveredElement, 5, lineY, scale, color, true);
593595
}
594596
}

src/main/java/com/cleanroommc/modularui/test/TestGuis.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class TestGuis extends CustomModularScreen {
6767

6868
@Override
6969
public @NotNull ModularPanel buildUI(ModularGuiContext context) {
70-
return buildToggleGridListUI(context);
70+
return buildRichTextUI(context);
7171
}
7272

7373
public @NotNull ModularPanel buildToggleGridListUI(ModularGuiContext context) {

src/main/java/com/cleanroommc/modularui/widgets/RichTextWidget.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)