Skip to content

Commit 009f12d

Browse files
MCTian-mibrachy84
andauthored
Add option to replace vanilla tooltips with RichTooltips (#140)
* feat: replace vanilla tooltips * more work * fix tooltip width when left to mouse * find parent for vanilla tooltip & fix layout --------- Co-authored-by: brachy84 <[email protected]>
1 parent e517888 commit 009f12d

File tree

14 files changed

+407
-462
lines changed

14 files changed

+407
-462
lines changed

src/main/java/com/cleanroommc/modularui/ModularUIConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,7 @@ public class ModularUIConfig {
3838
@Config.RequiresMcRestart
3939
@Config.Comment("Enables a test overlay shown on title screen and watermark shown on every GuiContainer.")
4040
public static boolean enableTestOverlays = false;
41+
42+
@Config.Comment("If true, vanilla tooltip will be replaced with MUI's RichTooltip")
43+
public static boolean replaceVanillaTooltips = false;
4144
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.cleanroommc.modularui.core.mixin;
2+
3+
import com.cleanroommc.modularui.ModularUIConfig;
4+
import com.cleanroommc.modularui.screen.RichTooltip;
5+
import com.cleanroommc.modularui.screen.viewport.GuiContext;
6+
7+
import net.minecraft.client.gui.FontRenderer;
8+
import net.minecraft.item.ItemStack;
9+
import net.minecraftforge.fml.client.config.GuiUtils;
10+
11+
import org.spongepowered.asm.mixin.Mixin;
12+
import org.spongepowered.asm.mixin.injection.At;
13+
import org.spongepowered.asm.mixin.injection.Inject;
14+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
15+
16+
import java.util.List;
17+
18+
@Mixin(value = GuiUtils.class, remap = false)
19+
public class GuiUtilsMixin {
20+
21+
@Inject(method = "drawHoveringText(Lnet/minecraft/item/ItemStack;Ljava/util/List;IIIIILnet/minecraft/client/gui/FontRenderer;)V",
22+
at = @At("HEAD"), cancellable = true)
23+
private static void postRichTooltipEvent(ItemStack stack, List<String> textLines, int x, int y, int w, int h, int maxTextWidth, FontRenderer font, CallbackInfo ci) {
24+
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);
37+
// Canceling vanilla tooltip rendering
38+
ci.cancel();
39+
}
40+
}
41+
}

src/main/java/com/cleanroommc/modularui/drawable/GuiDraw.java

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

33
import com.cleanroommc.modularui.drawable.text.TextRenderer;
4+
import com.cleanroommc.modularui.screen.RichTooltip;
5+
import com.cleanroommc.modularui.screen.RichTooltipEvent;
46
import com.cleanroommc.modularui.utils.Color;
57

68
import net.minecraft.client.Minecraft;
@@ -19,6 +21,7 @@
1921
import net.minecraftforge.fml.relauncher.Side;
2022
import net.minecraftforge.fml.relauncher.SideOnly;
2123

24+
import org.jetbrains.annotations.Nullable;
2225
import org.lwjgl.opengl.GL11;
2326

2427
import java.util.List;
@@ -592,12 +595,17 @@ public static void drawText(String text, float x, float y, float scale, int colo
592595
GlStateManager.enableBlend();
593596
}
594597

595-
public static void drawTooltipBackground(ItemStack stack, List<String> lines, int x, int y, int textWidth, int height) {
598+
public static void drawTooltipBackground(ItemStack stack, List<String> lines, int x, int y, int textWidth, int height, @Nullable RichTooltip tooltip) {
596599
// TODO theme color
597600
int backgroundColor = 0xF0100010;
598601
int borderColorStart = 0x505000FF;
599602
int borderColorEnd = (borderColorStart & 0xFEFEFE) >> 1 | borderColorStart & 0xFF000000;
600-
RenderTooltipEvent.Color colorEvent = new RenderTooltipEvent.Color(stack, lines, x, y, TextRenderer.getFontRenderer(), backgroundColor, borderColorStart, borderColorEnd);
603+
RenderTooltipEvent.Color colorEvent;
604+
if (tooltip != null) {
605+
colorEvent = new RichTooltipEvent.Color(stack, lines, x, y, TextRenderer.getFontRenderer(), backgroundColor, borderColorStart, borderColorEnd, tooltip);
606+
} else {
607+
colorEvent = new RenderTooltipEvent.Color(stack, lines, x, y, TextRenderer.getFontRenderer(), backgroundColor, borderColorStart, borderColorEnd);
608+
}
601609
MinecraftForge.EVENT_BUS.post(colorEvent);
602610
backgroundColor = colorEvent.getBackground();
603611
borderColorStart = colorEvent.getBorderStart();

src/main/java/com/cleanroommc/modularui/drawable/HoverableIcon.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Area getRenderedArea() {
4141

4242
@Override
4343
public @NotNull RichTooltip tooltip() {
44-
if (this.tooltip == null) this.tooltip = new RichTooltip(area -> area.set(getRenderedArea()));
44+
if (this.tooltip == null) this.tooltip = new RichTooltip().parent(area -> area.set(getRenderedArea()));
4545
return tooltip;
4646
}
4747

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.cleanroommc.modularui.theme.WidgetTheme;
66
import com.cleanroommc.modularui.utils.Alignment;
77

8+
import com.cleanroommc.modularui.utils.TooltipLines;
9+
810
import net.minecraft.client.gui.FontRenderer;
911

1012
import java.util.ArrayList;
@@ -15,6 +17,7 @@ public class RichText implements IDrawable, IRichTextBuilder<RichText> {
1517
private static final TextRenderer renderer = new TextRenderer();
1618

1719
private final List<Object> elements = new ArrayList<>();
20+
private TooltipLines stringList;
1821
private Alignment alignment = Alignment.CenterLeft;
1922
private float scale = 1f;
2023
private Integer color = null;
@@ -26,35 +29,17 @@ public boolean isEmpty() {
2629
return this.elements.isEmpty();
2730
}
2831

29-
public List<String> getStringRepresentation() {
30-
List<String> list = new ArrayList<>();
31-
StringBuilder builder = new StringBuilder();
32-
for (Object o : this.elements) {
33-
if (o == IKey.LINE_FEED) {
34-
list.add(builder.toString());
35-
builder.delete(0, builder.length());
36-
continue;
37-
}
38-
String s = null;
39-
if (o instanceof IKey key) {
40-
s = key.get();
41-
} else if (o instanceof String s1) {
42-
s = s1;
43-
} else if (o instanceof TextIcon ti) {
44-
s = ti.getText();
45-
}
46-
if (s != null) {
47-
for (String part : s.split("\n")) {
48-
builder.append(part);
49-
list.add(builder.toString());
50-
builder.delete(0, builder.length());
51-
}
52-
}
32+
public List<String> getAsStrings() {
33+
if (this.stringList == null) {
34+
this.stringList = new TooltipLines(this.elements);
5335
}
54-
if (!list.isEmpty() && list.get(list.size() - 1).isEmpty()) {
55-
list.remove(list.size() - 1);
36+
return this.stringList;
37+
}
38+
39+
private void clearStrings() {
40+
if (this.stringList != null) {
41+
this.stringList.clearCache();
5642
}
57-
return list;
5843
}
5944

6045
public int getMinWidth() {
@@ -95,6 +80,7 @@ public IRichTextBuilder<?> getRichText() {
9580

9681
public RichText add(String s) {
9782
this.elements.add(s);
83+
clearStrings();
9884
return this;
9985
}
10086

@@ -103,18 +89,21 @@ public RichText add(IDrawable drawable) {
10389
Object o = drawable;
10490
if (!(o instanceof IKey) && !(o instanceof IIcon)) o = drawable.asIcon();
10591
this.elements.add(o);
92+
clearStrings();
10693
return this;
10794
}
10895

10996
@Override
11097
public RichText addLine(ITextLine line) {
11198
this.elements.add(line);
99+
clearStrings();
112100
return this;
113101
}
114102

115103
@Override
116104
public RichText clearText() {
117105
this.elements.clear();
106+
clearStrings();
118107
return this;
119108
}
120109

@@ -154,6 +143,7 @@ public RichText insertTitleMargin(int margin) {
154143
} else {
155144
objects.add(i + 1, Spacer.of(margin));
156145
}
146+
clearStrings();
157147
return this;
158148
}
159149
}
@@ -201,4 +191,14 @@ public Object getHoveringElement(FontRenderer fr, int x, int y) {
201191
}
202192
return null;
203193
}
194+
195+
public RichText copy() {
196+
RichText copy = new RichText();
197+
copy.elements.addAll(this.elements);
198+
copy.alignment = this.alignment;
199+
copy.scale = this.scale;
200+
copy.color = this.color;
201+
copy.shadow = this.shadow;
202+
return copy;
203+
}
204204
}

0 commit comments

Comments
 (0)