Skip to content

Improve Rich Text #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.cleanroommc.modularui.theme.WidgetTheme;
import com.cleanroommc.modularui.utils.Alignment;

import com.cleanroommc.modularui.widget.sizer.Area;

import net.minecraft.client.gui.FontRenderer;

import java.util.ArrayList;
Expand Down Expand Up @@ -166,11 +168,23 @@ public void draw(GuiContext context, int x, int y, int width, int height, Widget
}

public void draw(GuiContext context, int x, int y, int width, int height, int color, boolean shadow) {
draw(renderer, context, x, y, width, height, color, shadow);
}

public void draw(TextRenderer renderer, GuiContext context, int x, int y, int width, int height, int color, boolean shadow) {
renderer.setSimulate(false);
setupRenderer(renderer, x, y, width, height, color, shadow);
this.cachedText = renderer.compileAndDraw(context, this.elements);
}

public int getLastHeight() {
return (int) renderer.getLastHeight();
}

public int getLastWidth() {
return (int) renderer.getLastWidth();
}

public void setupRenderer(TextRenderer renderer, int x, int y, float width, float height, int color, boolean shadow) {
renderer.setPos(x, y);
renderer.setScale(this.scale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ private void compile(List<Object> raw) {
if (o instanceof IKey key) {
if (key == IKey.EMPTY) continue;
if (key == IKey.SPACE) {
addLineElement(key.get());
text = key.get();
addLineElement(text);
x += fr.getStringWidth(text); // space is typically 4 pixels
continue;
}
if (key == IKey.LINE_FEED) {
Expand Down Expand Up @@ -209,7 +211,7 @@ public static String trimRight(String s) {
for (; i >= 0; i--) {
if (!Character.isWhitespace(s.charAt(i))) break;
}
if (i < s.length() - 1) s = s.substring(0, i);
if (i < s.length() - 1) s = s.substring(0, i + 1);
return s;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,10 @@ public static void drawDebugScreen(@Nullable ModularScreen muiScreen, @Nullable
} else if (hovered instanceof RichTextWidget richTextWidget) {
drawSegmentLine(lineY -= 4, color);
lineY -= 10;
context.pushMatrix();
context.translate(richTextWidget.getArea().x, richTextWidget.getArea().y);
Object hoveredElement = richTextWidget.getHoveredElement();
context.popMatrix();
GuiDraw.drawText("Hovered: " + hoveredElement, 5, lineY, 1, color, false);
}
}
Expand Down
39 changes: 15 additions & 24 deletions src/main/java/com/cleanroommc/modularui/widgets/RichTextWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ public void draw(ModularGuiContext context, WidgetTheme widgetTheme) {
@Override
public void drawForeground(ModularGuiContext context) {
super.drawForeground(context);
Object o = this.text.getHoveringElement(context.getFontRenderer(), context.getMouseX(), context.getMouseY());
//ModularUI.LOGGER.info("Mouse {}, {}", context.getMouseX(), context.getMouseY());
if (o instanceof IHoverable hoverable) {
if (getHoveredElement(context) instanceof IHoverable hoverable) {
hoverable.onHover();
RichTooltip tooltip = hoverable.getTooltip();
if (tooltip != null) {
Expand All @@ -55,83 +53,76 @@ public void drawForeground(ModularGuiContext context) {

@Override
public @NotNull Result onMousePressed(int mouseButton) {
Object o = this.text.getHoveringElement(getContext().getFontRenderer(), getContext().getMouseX(), getContext().getMouseY());
if (o instanceof Interactable interactable) {
if (getHoveredElement() instanceof Interactable interactable) {
return interactable.onMousePressed(mouseButton);
}
return Result.ACCEPT;
}

@Override
public boolean onMouseRelease(int mouseButton) {
Object o = this.text.getHoveringElement(getContext().getFontRenderer(), getContext().getMouseX(), getContext().getMouseY());
if (o instanceof Interactable interactable) {
if (getHoveredElement() instanceof Interactable interactable) {
return interactable.onMouseRelease(mouseButton);
}
return false;
}

@Override
public @NotNull Result onMouseTapped(int mouseButton) {
Object o = this.text.getHoveringElement(getContext().getFontRenderer(), getContext().getMouseX(), getContext().getMouseY());
if (o instanceof Interactable interactable) {
if (getHoveredElement() instanceof Interactable interactable) {
return interactable.onMouseTapped(mouseButton);
}
return Result.IGNORE;
}

@Override
public @NotNull Result onKeyPressed(char typedChar, int keyCode) {
Object o = this.text.getHoveringElement(getContext().getFontRenderer(), getContext().getMouseX(), getContext().getMouseY());
if (o instanceof Interactable interactable) {
if (getHoveredElement() instanceof Interactable interactable) {
return interactable.onKeyPressed(typedChar, keyCode);
}
return Result.ACCEPT;
}

@Override
public boolean onKeyRelease(char typedChar, int keyCode) {
Object o = this.text.getHoveringElement(getContext().getFontRenderer(), getContext().getMouseX(), getContext().getMouseY());
if (o instanceof Interactable interactable) {
if (getHoveredElement() instanceof Interactable interactable) {
return interactable.onKeyRelease(typedChar, keyCode);
}
return false;
}

@Override
public @NotNull Result onKeyTapped(char typedChar, int keyCode) {
Object o = this.text.getHoveringElement(getContext().getFontRenderer(), getContext().getMouseX(), getContext().getMouseY());
if (o instanceof Interactable interactable) {
if (getHoveredElement() instanceof Interactable interactable) {
return interactable.onKeyTapped(typedChar, keyCode);
}
return Result.ACCEPT;
}

@Override
public boolean onMouseScroll(ModularScreen.UpOrDown scrollDirection, int amount) {
Object o = this.text.getHoveringElement(getContext().getFontRenderer(), getContext().getMouseX(), getContext().getMouseY());
if (o instanceof Interactable interactable) {
if (getHoveredElement() instanceof Interactable interactable) {
return interactable.onMouseScroll(scrollDirection, amount);
}
return false;
}

@Override
public void onMouseDrag(int mouseButton, long timeSinceClick) {
Object o = this.text.getHoveringElement(getContext().getFontRenderer(), getContext().getMouseX(), getContext().getMouseY());
if (o instanceof Interactable interactable) {
if (getHoveredElement() instanceof Interactable interactable) {
interactable.onMouseDrag(mouseButton, timeSinceClick);
}
}

@Nullable
public Object getHoveredElement() {
return getHoveredElement(getContext());
}

@Nullable
public Object getHoveredElement(ModularGuiContext context) {
if (!isHovering()) return null;
getContext().pushMatrix();
getContext().translate(getArea().x, getArea().y);
Object o = this.text.getHoveringElement(getContext());
getContext().popMatrix();
return o;
return this.text.getHoveringElement(context);
Comment on lines -130 to +125
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you removing the transform

Copy link
Contributor Author

@ghzdude ghzdude Mar 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it's already translated for the interactable/drawing methods, translating again makes it stop working
or perhaps it's because the RichText lines are untranslated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im confused. It was working before, was it not? And you didnt change anything where it would translate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method wasn't used previously

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm, this method was only used in ClientScreenHandler, but now i'm using it for the interactable/draw methods in the widget itself. using it as is caused problems with the matrix translate. ClientScreenHandler would need the translate, but the widget's internal methods don't

}

@Override
Expand Down