Skip to content

Commit b2b95b1

Browse files
committed
more rich text fixes
1 parent 009f12d commit b2b95b1

File tree

8 files changed

+87
-46
lines changed

8 files changed

+87
-46
lines changed

src/main/java/com/cleanroommc/modularui/core/mixin/GuiUtilsMixin.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,7 @@ public class GuiUtilsMixin {
2222
at = @At("HEAD"), cancellable = true)
2323
private static void postRichTooltipEvent(ItemStack stack, List<String> textLines, int x, int y, int w, int h, int maxTextWidth, FontRenderer font, CallbackInfo ci) {
2424
if (ModularUIConfig.replaceVanillaTooltips && !textLines.isEmpty()) {
25-
RichTooltip tooltip = new RichTooltip();
26-
tooltip.parent(area -> RichTooltip.findIngredientArea(area, x, y));
27-
// Other positions don't really work due to the lack of GuiContext in non-modular uis
28-
tooltip.add(textLines.get(0)).newLine();
29-
if (!stack.isEmpty()) {
30-
tooltip.spaceLine();
31-
}
32-
for (int i = 1, n = textLines.size(); i < n; i++) {
33-
tooltip.add(textLines.get(i)).newLine();
34-
}
35-
36-
tooltip.draw(GuiContext.getDefault(), stack);
25+
RichTooltip.injectRichTooltip(stack, textLines, x, y);
3726
// Canceling vanilla tooltip rendering
3827
ci.cancel();
3928
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,19 @@ public Object getHoveringElement(FontRenderer fr, int x, int y) {
8585
}
8686
return null;
8787
}
88+
89+
@Override
90+
public String toString() {
91+
if (this.elements.isEmpty()) return "[empty]";
92+
StringBuilder builder = new StringBuilder("[");
93+
for (Object o : this.elements) {
94+
if (o instanceof String s) {
95+
builder.append('"').append(s).append('"').append(", ");
96+
} else {
97+
builder.append(o).append(", ");
98+
}
99+
}
100+
builder.delete(builder.length() - 2, builder.length());
101+
return builder.append("]").toString();
102+
}
88103
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ public Object getHoveringElement(FontRenderer fr, int x, int y) {
4040
if (x < lastX || x > lastX + getWidth()) return Boolean.FALSE; // not hovering, but we know that nothing else is hovered either
4141
return this.text;
4242
}
43+
44+
@Override
45+
public String toString() {
46+
return text;
47+
}
4348
}

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ public class TextRenderer {
2727
protected boolean shadow = false;
2828
protected int color = 0;//Theme.INSTANCE.getText();
2929
protected boolean simulate;
30-
protected float lastWidth = 0, lastHeight = 0;
30+
protected float lastActualWidth = 0;
31+
protected float lastTrimmedWidth = 0;
32+
protected float lastActualHeight = 0;
33+
protected float lastTrimmedHeight = 0;
3134
protected float lastX = 0, lastY = 0;
3235
protected boolean scrollOnOverflow = false;
3336

@@ -83,20 +86,20 @@ protected void drawMeasuredLines(List<Line> measuredLines) {
8386
draw(measuredLine.text, x0, y0);
8487
y0 += (int) getFontHeight();
8588
}
86-
this.lastWidth = this.maxWidth > 0 ? Math.min(maxW, this.maxWidth) : maxW;
87-
this.lastHeight = measuredLines.size() * getFontHeight();
88-
this.lastWidth = Math.max(0, this.lastWidth - this.scale);
89-
this.lastHeight = Math.max(0, this.lastHeight - this.scale);
89+
this.lastActualWidth = this.maxWidth > 0 ? Math.min(maxW, this.maxWidth) : maxW;
90+
this.lastActualHeight = measuredLines.size() * getFontHeight();
91+
this.lastTrimmedWidth = Math.max(0, this.lastActualWidth - this.scale);
92+
this.lastTrimmedHeight = Math.max(0, this.lastActualHeight - this.scale);
9093
}
9194

9295
public void drawSimple(String text) {
9396
float w = getFontRenderer().getStringWidth(text) * this.scale;
9497
int y = getStartYOfLines(1), x = getStartX(w);
9598
draw(text, x, y);
96-
this.lastWidth = w;
97-
this.lastHeight = getFontHeight();
98-
this.lastWidth = Math.max(0, this.lastWidth - this.scale);
99-
this.lastHeight = Math.max(0, this.lastHeight - this.scale);
99+
this.lastActualWidth = w;
100+
this.lastActualHeight = getFontHeight();
101+
this.lastTrimmedWidth = Math.max(0, this.lastActualWidth - this.scale);
102+
this.lastTrimmedHeight = Math.max(0, this.lastActualHeight - this.scale);
100103
}
101104

102105
public List<Line> measureLines(List<String> lines) {
@@ -139,10 +142,10 @@ public void drawCompiled(GuiContext context, List<ITextLine> lines) {
139142
y0 += line.getHeight(getFontRenderer());
140143
}
141144
if (!this.simulate) GlStateManager.popMatrix();
142-
this.lastWidth = this.maxWidth > 0 ? Math.min(width * this.scale, this.maxWidth) : width * this.scale;
143-
this.lastHeight = height * this.scale;
144-
this.lastWidth = Math.max(0, this.lastWidth - this.scale);
145-
this.lastHeight = Math.max(0, this.lastHeight - this.scale);
145+
this.lastActualWidth = this.maxWidth > 0 ? Math.min(width * this.scale, this.maxWidth) : width * this.scale;
146+
this.lastActualHeight = height * this.scale;
147+
this.lastTrimmedWidth = Math.max(0, this.lastActualWidth - this.scale);
148+
this.lastTrimmedHeight = Math.max(0, this.lastActualHeight - this.scale);
146149
}
147150

148151
public void drawCut(String text) {
@@ -266,12 +269,20 @@ public float getFontHeight() {
266269
return getFontRenderer().FONT_HEIGHT * this.scale;
267270
}
268271

269-
public float getLastHeight() {
270-
return this.lastHeight;
272+
public float getLastActualHeight() {
273+
return this.lastActualHeight;
271274
}
272275

273-
public float getLastWidth() {
274-
return this.lastWidth;
276+
public float getLastActualWidth() {
277+
return this.lastActualWidth;
278+
}
279+
280+
public float getLastTrimmedWidth() {
281+
return lastTrimmedWidth;
282+
}
283+
284+
public float getLastTrimmedHeight() {
285+
return lastTrimmedHeight;
275286
}
276287

277288
@SideOnly(Side.CLIENT)

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

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.cleanroommc.modularui.api.GuiAxis;
66
import com.cleanroommc.modularui.api.MCHelper;
77
import com.cleanroommc.modularui.api.drawable.IRichTextBuilder;
8+
import com.cleanroommc.modularui.api.drawable.ITextLine;
89
import com.cleanroommc.modularui.api.widget.IWidget;
910
import com.cleanroommc.modularui.drawable.GuiDraw;
1011
import com.cleanroommc.modularui.drawable.text.RichText;
@@ -26,6 +27,7 @@
2627

2728
import mezz.jei.input.IClickedIngredient;
2829
import mezz.jei.input.IShowsRecipeFocuses;
30+
import org.jetbrains.annotations.ApiStatus;
2931
import org.jetbrains.annotations.Nullable;
3032

3133
import java.awt.*;
@@ -122,10 +124,10 @@ public void draw(GuiContext context, @Nullable ItemStack stack) {
122124

123125
Rectangle area = determineTooltipArea(copy, context, renderer, screenWidth, screenHeight, mouseX, mouseY);
124126
renderer.setPos(area.x, area.y);
125-
renderer.setAlignment(copy.getAlignment(), area.width, -1);
126-
copy.compileAndDraw(renderer, context, true);
127-
area.width = (int) renderer.getLastWidth();
128-
area.height = (int) renderer.getLastHeight();
127+
renderer.setAlignment(copy.getAlignment(), (float) Math.ceil(area.width + copy.getScale()), -1);
128+
List<ITextLine> compiledLines = copy.compileAndDraw(renderer, context, true);
129+
area.width = (int) renderer.getLastTrimmedWidth();
130+
area.height = (int) renderer.getLastTrimmedHeight();
129131

130132
GlStateManager.disableRescaleNormal();
131133
RenderHelper.disableStandardItemLighting();
@@ -140,14 +142,15 @@ public void draw(GuiContext context, @Nullable ItemStack stack) {
140142
GlStateManager.color(1f, 1f, 1f, 1f);
141143

142144
renderer.setPos(area.x, area.y);
143-
copy.compileAndDraw(renderer, context, false);
145+
renderer.setSimulate(false);
146+
renderer.drawCompiled(context, compiledLines);
144147

145148
MinecraftForge.EVENT_BUS.post(new RichTooltipEvent.PostText(stack, copy.getAsStrings(), area.x, area.y, TextRenderer.getFontRenderer(), area.width, area.height, copy));
146149
}
147150

148151
public Rectangle determineTooltipArea(RichText text, GuiContext context, TextRenderer renderer, int screenWidth, int screenHeight, int mouseX, int mouseY) {
149-
int width = (int) renderer.getLastWidth();
150-
int height = (int) renderer.getLastHeight();
152+
int width = (int) renderer.getLastTrimmedWidth();
153+
int height = (int) renderer.getLastTrimmedHeight();
151154
if (width > screenWidth - 14) {
152155
width = screenWidth - 14;
153156
}
@@ -191,8 +194,8 @@ public Rectangle determineTooltipArea(RichText text, GuiContext context, TextRen
191194
renderer.setPos(x, y);
192195
renderer.setAlignment(text.getAlignment(), width, -1);
193196
text.compileAndDraw(renderer, context, true);
194-
width = (int) renderer.getLastWidth();
195-
height = (int) renderer.getLastHeight();
197+
width = (int) renderer.getLastTrimmedWidth();
198+
height = (int) renderer.getLastTrimmedHeight();
196199
}
197200
y = MathHelper.clamp(y, padding, screenHeight - padding - height);
198201
return new Rectangle(x, y, width, height);
@@ -256,8 +259,8 @@ public Rectangle determineTooltipArea(RichText text, GuiContext context, TextRen
256259
usedMoreSpaceSide = true;
257260
renderer.setAlignment(text.getAlignment(), maxWidth);
258261
text.compileAndDraw(renderer, context, true);
259-
width = (int) renderer.getLastWidth();
260-
height = (int) renderer.getLastHeight();
262+
width = (int) renderer.getLastTrimmedWidth();
263+
height = (int) renderer.getLastTrimmedHeight();
261264
}
262265

263266
if (oPos == Pos.HORIZONTAL && !usedMoreSpaceSide) {
@@ -343,7 +346,7 @@ public RichTooltip setAutoUpdate(boolean update) {
343346

344347
public RichTooltip addFromItem(ItemStack item) {
345348
List<String> lines = MCHelper.getItemToolTip(item);
346-
add(lines.get(0));
349+
add(lines.get(0)).newLine();
347350
if (lines.size() > 1) {
348351
spaceLine();
349352
for (int i = 1, n = lines.size(); i < n; i++) {
@@ -363,7 +366,7 @@ public RichTooltip titleMargin(int margin) {
363366
return this;
364367
}
365368

366-
public static void findIngredientArea(Area area, int x, int y) {
369+
private static void findIngredientArea(Area area, int x, int y) {
367370
GuiScreen screen = MCHelper.getCurrentScreen();
368371
if (screen instanceof GuiContainer guiContainer) {
369372
Slot slot = guiContainer.getSlotUnderMouse();
@@ -392,6 +395,24 @@ public static void findIngredientArea(Area area, int x, int y) {
392395
area.set(Area.ZERO);
393396
}
394397

398+
@ApiStatus.Internal
399+
public static void injectRichTooltip(ItemStack stack, List<String> lines, int x, int y) {
400+
RichTooltip tooltip = new RichTooltip();
401+
tooltip.parent(area -> RichTooltip.findIngredientArea(area, x, y));
402+
// Other positions don't really work due to the lack of GuiContext in non-modular uis
403+
tooltip.add(lines.get(0)).newLine();
404+
if (lines.size() > 1) {
405+
if (!stack.isEmpty()) {
406+
tooltip.spaceLine();
407+
}
408+
for (int i = 1, n = lines.size(); i < n; i++) {
409+
tooltip.add(lines.get(i)).newLine();
410+
}
411+
}
412+
413+
tooltip.draw(GuiContext.getDefault(), stack);
414+
}
415+
395416
public enum Pos {
396417

397418
ABOVE(GuiAxis.Y),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public int getDefaultHeight() {
5959
}
6060
TextRenderer renderer = simulate(maxWidth);
6161
Box padding = getArea().getPadding();
62-
return Math.max(1, (int) (renderer.getLastHeight() + padding.vertical() + 0.5f));
62+
return Math.max(1, (int) (renderer.getLastActualHeight() + padding.vertical()));
6363
}
6464

6565
@Override
@@ -70,7 +70,7 @@ public int getDefaultWidth() {
7070
}
7171
TextRenderer renderer = simulate(maxWidth);
7272
Box padding = getArea().getPadding();
73-
return Math.max(1, (int) Math.ceil(renderer.getLastWidth() + padding.horizontal()));
73+
return Math.max(1, (int) Math.ceil(renderer.getLastActualWidth() + padding.horizontal()));
7474
}
7575

7676
public IKey getKey() {

src/main/java/com/cleanroommc/modularui/widgets/textfield/BaseTextFieldWidget.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected void drawText(ModularGuiContext context, WidgetTextFieldTheme widgetTh
128128
} else {
129129
this.renderer.draw(this.handler.getText());
130130
}
131-
getScrollArea().getScrollX().setScrollSize(Math.max(0, (int) (this.renderer.getLastWidth() + 0.5f)));
131+
getScrollArea().getScrollX().setScrollSize(Math.max(0, (int) (this.renderer.getLastActualWidth() + 0.5f)));
132132
}
133133

134134
@Override

src/main/java/com/cleanroommc/modularui/widgets/textfield/TextFieldHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void setMainCursor(int linePos, int charPos, boolean animate) {
9696
this.renderer.setSimulate(true);
9797
this.renderer.draw(this.text);
9898
this.renderer.setSimulate(false);
99-
this.scrollArea.getScrollX().setScrollSize((int) (this.renderer.getLastWidth() + 0.5f));
99+
this.scrollArea.getScrollX().setScrollSize((int) this.renderer.getLastActualWidth());
100100
if (this.scrollArea.getScrollX().isScrollBarActive(this.scrollArea)) {
101101
String line = this.text.get(main.y);
102102
int scrollTo = (int) this.renderer.getPosOf(this.renderer.measureLines(Collections.singletonList(line)), main).x;

0 commit comments

Comments
 (0)