Skip to content

Commit 131f2bd

Browse files
committed
inspector screen (#320)
* start working on inspector screen * pass 2 * pass 3 * merge fixes (cherry picked from commit 07cfed3)
1 parent 4db03e4 commit 131f2bd

File tree

16 files changed

+485
-103
lines changed

16 files changed

+485
-103
lines changed

src/main/java/mcp/mobius/waila/WailaClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected static void onClientTick() {
9494
protected static void onItemTooltip(ItemStack stack, List<Component> tooltip) {
9595
if (PluginConfig.CLIENT.getBoolean(WailaConstants.CONFIG_SHOW_ITEM_MOD_NAME)) {
9696
for (var listener : Registrar.get().eventListeners.get(Object.class)) {
97-
var name = listener.instance().getHoveredItemModName(stack, PluginConfig.CLIENT);
97+
var name = listener.instance().instance().getHoveredItemModName(stack, PluginConfig.CLIENT);
9898
if (name != null) {
9999
tooltip.add(IWailaConfig.get().getFormatter().modName(name));
100100
return;

src/main/java/mcp/mobius/waila/command/ClientCommand.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import mcp.mobius.waila.config.ConfigEntry;
1313
import mcp.mobius.waila.config.PluginConfig;
1414
import mcp.mobius.waila.gui.screen.HomeScreen;
15+
import mcp.mobius.waila.gui.screen.InspectorScreen;
1516
import mcp.mobius.waila.plugin.PluginInfo;
1617
import mcp.mobius.waila.plugin.PluginLoader;
1718
import net.minecraft.client.Minecraft;
@@ -158,6 +159,14 @@ protected final void register(ArgumentBuilderBuilder<S> command) {
158159
})
159160
.pop("enabled", "showFps")
160161

162+
.then(literal("inspect"))
163+
.executes(context -> {
164+
var client = Minecraft.getInstance();
165+
client.tell(() -> client.setScreen(new InspectorScreen()));
166+
return 1;
167+
})
168+
.pop("inspect")
169+
161170
.pop("debug");
162171
}
163172

src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ private static void handleBlock(ClientAccessor accessor, Tooltip tooltip, Object
6666
var registrar = Registrar.get();
6767
var providers = registrar.blockComponent.get(position).get(obj);
6868
for (var entry : providers) {
69-
var provider = entry.instance();
69+
var origin = entry.instance();
70+
var provider = origin.instance();
71+
tooltip.origin = origin;
7072
try {
7173
switch (position) {
7274
case HEAD -> provider.appendHead(tooltip, accessor, PluginConfig.CLIENT);
@@ -76,6 +78,7 @@ private static void handleBlock(ClientAccessor accessor, Tooltip tooltip, Object
7678
} catch (Throwable e) {
7779
ExceptionUtil.dump(e, provider.getClass().toString(), tooltip);
7880
}
81+
tooltip.origin = null;
7982
}
8083
}
8184

@@ -108,7 +111,9 @@ public static void gatherEntity(Entity entity, ClientAccessor accessor, Tooltip
108111

109112
var providers = registrar.entityComponent.get(position).get(entity);
110113
for (var entry : providers) {
111-
var provider = entry.instance();
114+
var origin = entry.instance();
115+
var provider = origin.instance();
116+
tooltip.origin = origin;
112117
try {
113118
switch (position) {
114119
case HEAD -> provider.appendHead(tooltip, accessor, PluginConfig.CLIENT);
@@ -118,6 +123,7 @@ public static void gatherEntity(Entity entity, ClientAccessor accessor, Tooltip
118123
} catch (Throwable e) {
119124
ExceptionUtil.dump(e, provider.getClass().toString(), tooltip);
120125
}
126+
tooltip.origin = null;
121127
}
122128
}
123129

@@ -129,10 +135,9 @@ public static ITooltipComponent getIcon(HitResult target) {
129135
if (target.getType() == HitResult.Type.ENTITY) {
130136
var providers = registrar.entityIcon.get(data.getEntity());
131137
for (var provider : providers) {
132-
var icon = provider.instance().getIcon(data, config);
133-
if (icon != null) {
134-
return icon;
135-
}
138+
var origin = provider.instance();
139+
var icon = InspectComponent.maybeWrap(origin.instance().getIcon(data, config), origin, null);
140+
if (icon != null) return icon;
136141
}
137142
} else {
138143
var state = data.getBlockState();
@@ -142,9 +147,10 @@ public static ITooltipComponent getIcon(HitResult target) {
142147
var priority = 0;
143148

144149
for (var provider : registrar.blockIcon.get(state.getBlock())) {
145-
var icon = provider.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
150+
var origin = provider.instance();
151+
var icon = origin.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
146152
if (icon != null) {
147-
result = icon;
153+
result = InspectComponent.maybeWrap(icon, origin, null);
148154
priority = provider.priority();
149155
break;
150156
}
@@ -155,9 +161,10 @@ public static ITooltipComponent getIcon(HitResult target) {
155161
for (var provider : registrar.blockIcon.get(blockEntity)) {
156162
if (provider.priority() >= priority) break;
157163

158-
var icon = provider.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
164+
var origin = provider.instance();
165+
var icon = origin.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
159166
if (icon != null) {
160-
result = icon;
167+
result = InspectComponent.maybeWrap(icon, origin, null);
161168
break;
162169
}
163170
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package mcp.mobius.waila.gui.hud;
2+
3+
import java.util.Random;
4+
5+
import com.mojang.blaze3d.systems.RenderSystem;
6+
import com.mojang.blaze3d.vertex.BufferUploader;
7+
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
8+
import com.mojang.blaze3d.vertex.Tesselator;
9+
import com.mojang.blaze3d.vertex.VertexFormat;
10+
import mcp.mobius.waila.WailaClient;
11+
import mcp.mobius.waila.api.ITooltipComponent;
12+
import net.minecraft.client.DeltaTracker;
13+
import net.minecraft.client.Minecraft;
14+
import net.minecraft.client.gui.GuiGraphics;
15+
import net.minecraft.client.renderer.GameRenderer;
16+
import net.minecraft.util.Mth;
17+
import org.jetbrains.annotations.Nullable;
18+
19+
import static mcp.mobius.waila.util.DisplayUtil.renderRectBorder;
20+
21+
public abstract class ComponentRenderer {
22+
23+
private static final Random RANDOM = new Random();
24+
25+
public abstract void render(GuiGraphics ctx, ITooltipComponent component, int x, int y, int cw, int ch, DeltaTracker delta);
26+
27+
private static @Nullable ComponentRenderer current = null;
28+
29+
public static ComponentRenderer get() {
30+
if (current == null) current = Default.INSTANCE;
31+
return current;
32+
}
33+
34+
public static void set(@Nullable ComponentRenderer value) {
35+
if (value == null) value = Default.INSTANCE;
36+
current = value;
37+
}
38+
39+
public static class Default extends ComponentRenderer {
40+
41+
public static final Default INSTANCE = new Default();
42+
43+
@Override
44+
public void render(GuiGraphics ctx, ITooltipComponent component, int x, int y, int cw, int ch, DeltaTracker delta) {
45+
component.render(ctx, x, y, delta);
46+
47+
if (WailaClient.showComponentBounds) {
48+
renderBounds(ctx, x, y, cw, ch, 1f);
49+
}
50+
}
51+
52+
public static void renderBounds(GuiGraphics ctx, int x, int y, int cw, int ch, float v) {
53+
ctx.pose().pushPose();
54+
var scale = (float) Minecraft.getInstance().getWindow().getGuiScale();
55+
ctx.pose().scale(1 / scale, 1 / scale, 1);
56+
57+
RenderSystem.setShader(GameRenderer::getPositionColorShader);
58+
59+
var buf = Tesselator.getInstance().begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
60+
var bx = Mth.floor(x * scale + 0.5);
61+
var by = Mth.floor(y * scale + 0.5);
62+
var bw = Mth.floor(cw * scale + 0.5);
63+
var bh = Mth.floor(ch * scale + 0.5);
64+
var color = (0xFF << 24) + Mth.hsvToRgb(RANDOM.nextFloat(), RANDOM.nextFloat(), v);
65+
renderRectBorder(ctx.pose().last().pose(), buf, bx, by, bw, bh, 1, color, color);
66+
BufferUploader.drawWithShader(buf.buildOrThrow());
67+
68+
ctx.pose().popPose();
69+
}
70+
71+
}
72+
73+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package mcp.mobius.waila.gui.hud;
2+
3+
import mcp.mobius.waila.api.ITooltipComponent;
4+
import mcp.mobius.waila.registry.PluginAware;
5+
import net.minecraft.client.DeltaTracker;
6+
import net.minecraft.client.gui.GuiGraphics;
7+
import net.minecraft.resources.ResourceLocation;
8+
import org.jetbrains.annotations.Nullable;
9+
10+
public class InspectComponent implements ITooltipComponent {
11+
12+
public static boolean wrap = false;
13+
14+
public final ITooltipComponent actual;
15+
public final PluginAware<?> origin;
16+
public final ResourceLocation tag;
17+
18+
private InspectComponent(ITooltipComponent actual, PluginAware<?> origin, ResourceLocation tag) {
19+
this.actual = actual;
20+
this.origin = origin;
21+
this.tag = tag;
22+
}
23+
24+
public static @Nullable ITooltipComponent maybeWrap(@Nullable ITooltipComponent actual, @Nullable PluginAware<?> origin, @Nullable ResourceLocation tag) {
25+
if (!wrap || actual == null || origin == null || actual instanceof InspectComponent) {
26+
return actual;
27+
}
28+
29+
if (actual instanceof ITooltipComponent.HorizontalGrowing hg) {
30+
return new InspectComponent.Growing(hg, origin, tag);
31+
}
32+
33+
return new InspectComponent(actual, origin, tag);
34+
}
35+
36+
@Override
37+
public int getWidth() {
38+
return actual.getWidth();
39+
}
40+
41+
@Override
42+
public int getHeight() {
43+
return actual.getHeight();
44+
}
45+
46+
@Override
47+
public void render(GuiGraphics ctx, int x, int y, DeltaTracker delta) {
48+
actual.render(ctx, x, y, delta);
49+
}
50+
51+
public static class Growing extends InspectComponent implements HorizontalGrowing {
52+
53+
public final HorizontalGrowing actual;
54+
55+
public Growing(HorizontalGrowing actual, PluginAware<?> origin, ResourceLocation tag) {
56+
super(actual, origin, tag);
57+
this.actual = actual;
58+
}
59+
60+
@Override
61+
public int getMinimalWidth() {
62+
return actual.getMinimalWidth();
63+
}
64+
65+
@Override
66+
public void setGrownWidth(int grownWidth) {
67+
actual.setGrownWidth(grownWidth);
68+
}
69+
70+
}
71+
72+
}

src/main/java/mcp/mobius/waila/gui/hud/Line.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import mcp.mobius.waila.api.ITooltipComponent.HorizontalGrowing;
1212
import mcp.mobius.waila.api.ITooltipLine;
1313
import mcp.mobius.waila.api.component.WrappedComponent;
14-
import mcp.mobius.waila.util.DisplayUtil;
14+
import mcp.mobius.waila.registry.PluginAware;
1515
import net.minecraft.client.DeltaTracker;
1616
import net.minecraft.client.gui.GuiGraphics;
1717
import net.minecraft.network.chat.Component;
@@ -20,12 +20,13 @@
2020

2121
public class Line implements ITooltipLine {
2222

23-
@Nullable
24-
public final ResourceLocation tag;
23+
public final @Nullable ResourceLocation tag;
2524
public final List<ITooltipComponent> components = new ArrayList<>();
2625
public final Object2IntOpenHashMap<ITooltipComponent> widths = new Object2IntOpenHashMap<>();
2726
public final Object2IntMap<ITooltipComponent> heights = new Object2IntOpenHashMap<>();
2827

28+
public @Nullable PluginAware<?> origin;
29+
2930
private int fixedWidth = -1;
3031
private int width = -1;
3132
private int height = -1;
@@ -39,6 +40,7 @@ public Line(@Nullable ResourceLocation tag) {
3940

4041
@Override
4142
public Line with(ITooltipComponent component) {
43+
component = InspectComponent.maybeWrap(component, origin, tag);
4244
components.add(component);
4345
if (component instanceof HorizontalGrowing growing) {
4446
growingWeight += growing.getWeight();
@@ -140,7 +142,7 @@ public int getHeight() {
140142
return height;
141143
}
142144

143-
public void render(GuiGraphics ctx, int x, int y, DeltaTracker delta) {
145+
public void render(ComponentRenderer renderer, GuiGraphics ctx, int x, int y, DeltaTracker delta) {
144146
Preconditions.checkState(width != -1 && height != -1);
145147

146148
var cx = x;
@@ -150,7 +152,7 @@ public void render(GuiGraphics ctx, int x, int y, DeltaTracker delta) {
150152
var h = heights.getInt(component);
151153

152154
var cy = y + (h < height ? (height - h) / 2 : 0);
153-
DisplayUtil.renderComponent(ctx, component, cx, cy, w, delta);
155+
renderer.render(ctx, component, cx, cy, w, h, delta);
154156
cx += w + 1;
155157
}
156158
}

src/main/java/mcp/mobius/waila/gui/hud/Tooltip.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
66
import mcp.mobius.waila.api.ITooltip;
77
import mcp.mobius.waila.api.ITooltipLine;
8+
import mcp.mobius.waila.registry.PluginAware;
89
import net.minecraft.resources.ResourceLocation;
10+
import org.jetbrains.annotations.Nullable;
911

1012
public class Tooltip extends ObjectArrayList<Line> implements ITooltip {
1113

1214
private final Object2IntMap<ResourceLocation> tags = new Object2IntOpenHashMap<>();
15+
public @Nullable PluginAware<?> origin;
1316

1417
public void setLine(ResourceLocation tag, Line line) {
1518
if (tags.containsKey(tag)) {
@@ -27,19 +30,23 @@ public int getLineCount() {
2730

2831
@Override
2932
public ITooltipLine getLine(int index) {
30-
return get(index);
33+
var line = get(index);
34+
line.origin = origin;
35+
return line;
3136
}
3237

3338
@Override
3439
public ITooltipLine addLine() {
3540
var line = new Line(null);
41+
line.origin = origin;
3642
add(line);
3743
return line;
3844
}
3945

4046
@Override
4147
public ITooltipLine setLine(ResourceLocation tag) {
4248
var line = new Line(tag);
49+
line.origin = origin;
4350
setLine(tag, line);
4451
return line;
4552
}

0 commit comments

Comments
 (0)