Skip to content

Commit e8b0fc5

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

File tree

16 files changed

+483
-104
lines changed

16 files changed

+483
-104
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected static void onClientTick() {
9595
protected static void onItemTooltip(ItemStack stack, List<Component> tooltip) {
9696
if (PluginConfig.CLIENT.getBoolean(WailaConstants.CONFIG_SHOW_ITEM_MOD_NAME)) {
9797
for (var listener : Registrar.get().eventListeners.get(Object.class)) {
98-
var name = listener.instance().getHoveredItemModName(stack, PluginConfig.CLIENT);
98+
var name = listener.instance().instance().getHoveredItemModName(stack, PluginConfig.CLIENT);
9999
if (name != null) {
100100
tooltip.add(IWailaConfig.get().getFormatter().modName(name));
101101
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
@@ -71,7 +71,9 @@ private static void handleBlock(ClientAccessor accessor, Tooltip tooltip, Object
7171
var registrar = Registrar.get();
7272
var providers = registrar.blockComponent.get(position).get(obj);
7373
for (var entry : providers) {
74-
var provider = entry.instance();
74+
var origin = entry.instance();
75+
var provider = origin.instance();
76+
tooltip.origin = origin;
7577
try {
7678
switch (position) {
7779
case HEAD -> provider.appendHead(tooltip, accessor, PluginConfig.CLIENT);
@@ -81,6 +83,7 @@ private static void handleBlock(ClientAccessor accessor, Tooltip tooltip, Object
8183
} catch (Throwable e) {
8284
ExceptionUtil.dump(e, provider.getClass().toString(), tooltip);
8385
}
86+
tooltip.origin = null;
8487
}
8588
}
8689

@@ -121,7 +124,9 @@ public static void gatherEntity(Entity entity, ClientAccessor accessor, Tooltip
121124

122125
var providers = registrar.entityComponent.get(position).get(entity);
123126
for (var entry : providers) {
124-
var provider = entry.instance();
127+
var origin = entry.instance();
128+
var provider = origin.instance();
129+
tooltip.origin = origin;
125130
try {
126131
switch (position) {
127132
case HEAD -> provider.appendHead(tooltip, accessor, PluginConfig.CLIENT);
@@ -131,6 +136,7 @@ public static void gatherEntity(Entity entity, ClientAccessor accessor, Tooltip
131136
} catch (Throwable e) {
132137
ExceptionUtil.dump(e, provider.getClass().toString(), tooltip);
133138
}
139+
tooltip.origin = null;
134140
}
135141
}
136142

@@ -142,10 +148,9 @@ public static ITooltipComponent getIcon(HitResult target) {
142148
if (target.getType() == HitResult.Type.ENTITY) {
143149
var providers = registrar.entityIcon.get(data.getEntity());
144150
for (var provider : providers) {
145-
var icon = provider.instance().getIcon(data, config);
146-
if (icon != null) {
147-
return icon;
148-
}
151+
var origin = provider.instance();
152+
var icon = InspectComponent.maybeWrap(origin.instance().getIcon(data, config), origin, null);
153+
if (icon != null) return icon;
149154
}
150155
} else {
151156
var state = data.getBlockState();
@@ -155,9 +160,10 @@ public static ITooltipComponent getIcon(HitResult target) {
155160
var priority = 0;
156161

157162
for (var provider : registrar.blockIcon.get(state.getBlock())) {
158-
var icon = provider.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
163+
var origin = provider.instance();
164+
var icon = origin.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
159165
if (icon != null) {
160-
result = icon;
166+
result = InspectComponent.maybeWrap(icon, origin, null);
161167
priority = provider.priority();
162168
break;
163169
}
@@ -168,9 +174,10 @@ public static ITooltipComponent getIcon(HitResult target) {
168174
for (var provider : registrar.blockIcon.get(blockEntity)) {
169175
if (provider.priority() >= priority) break;
170176

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

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@
1212
import mcp.mobius.waila.api.ITooltipComponent.HorizontalGrowing;
1313
import mcp.mobius.waila.api.ITooltipLine;
1414
import mcp.mobius.waila.api.component.WrappedComponent;
15-
import mcp.mobius.waila.util.DisplayUtil;
15+
import mcp.mobius.waila.registry.PluginAware;
1616
import net.minecraft.network.chat.Component;
1717
import net.minecraft.resources.ResourceLocation;
1818
import org.jetbrains.annotations.Nullable;
1919

2020
public class Line implements ITooltipLine {
2121

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

27+
public @Nullable PluginAware<?> origin;
28+
2829
private int fixedWidth = -1;
2930
private int width = -1;
3031
private int height = -1;
@@ -38,6 +39,7 @@ public Line(@Nullable ResourceLocation tag) {
3839

3940
@Override
4041
public Line with(ITooltipComponent component) {
42+
component = InspectComponent.maybeWrap(component, origin, tag);
4143
components.add(component);
4244
if (component instanceof HorizontalGrowing growing) {
4345
growingWeight += growing.getWeight();
@@ -139,7 +141,7 @@ public int getHeight() {
139141
return height;
140142
}
141143

142-
public void render(PoseStack matrices, int x, int y, float delta) {
144+
public void render(ComponentRenderer renderer, PoseStack matrices, int x, int y, float delta) {
143145
Preconditions.checkState(width != -1 && height != -1);
144146

145147
var cx = x;
@@ -149,7 +151,7 @@ public void render(PoseStack matrices, int x, int y, float delta) {
149151
var h = heights.getInt(component);
150152

151153
var cy = y + (h < height ? (height - h) / 2 : 0);
152-
DisplayUtil.renderComponent(matrices, component, cx, cy, w, delta);
154+
renderer.render(matrices, component, cx, cy, w, h, delta);
153155
cx += w + 1;
154156
}
155157
}

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)