Skip to content

Commit

Permalink
inspector screen (#320)
Browse files Browse the repository at this point in the history
* start working on inspector screen

* pass 2

* pass 3

* merge fixes

(cherry picked from commit 07cfed3)
  • Loading branch information
deirn committed Feb 24, 2025
1 parent 4db03e4 commit 131f2bd
Show file tree
Hide file tree
Showing 16 changed files with 485 additions and 103 deletions.
2 changes: 1 addition & 1 deletion src/main/java/mcp/mobius/waila/WailaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected static void onClientTick() {
protected static void onItemTooltip(ItemStack stack, List<Component> tooltip) {
if (PluginConfig.CLIENT.getBoolean(WailaConstants.CONFIG_SHOW_ITEM_MOD_NAME)) {
for (var listener : Registrar.get().eventListeners.get(Object.class)) {
var name = listener.instance().getHoveredItemModName(stack, PluginConfig.CLIENT);
var name = listener.instance().instance().getHoveredItemModName(stack, PluginConfig.CLIENT);
if (name != null) {
tooltip.add(IWailaConfig.get().getFormatter().modName(name));
return;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/mcp/mobius/waila/command/ClientCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import mcp.mobius.waila.config.ConfigEntry;
import mcp.mobius.waila.config.PluginConfig;
import mcp.mobius.waila.gui.screen.HomeScreen;
import mcp.mobius.waila.gui.screen.InspectorScreen;
import mcp.mobius.waila.plugin.PluginInfo;
import mcp.mobius.waila.plugin.PluginLoader;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -158,6 +159,14 @@ protected final void register(ArgumentBuilderBuilder<S> command) {
})
.pop("enabled", "showFps")

.then(literal("inspect"))
.executes(context -> {
var client = Minecraft.getInstance();
client.tell(() -> client.setScreen(new InspectorScreen()));
return 1;
})
.pop("inspect")

.pop("debug");
}

Expand Down
27 changes: 17 additions & 10 deletions src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ private static void handleBlock(ClientAccessor accessor, Tooltip tooltip, Object
var registrar = Registrar.get();
var providers = registrar.blockComponent.get(position).get(obj);
for (var entry : providers) {
var provider = entry.instance();
var origin = entry.instance();
var provider = origin.instance();
tooltip.origin = origin;
try {
switch (position) {
case HEAD -> provider.appendHead(tooltip, accessor, PluginConfig.CLIENT);
Expand All @@ -76,6 +78,7 @@ private static void handleBlock(ClientAccessor accessor, Tooltip tooltip, Object
} catch (Throwable e) {
ExceptionUtil.dump(e, provider.getClass().toString(), tooltip);
}
tooltip.origin = null;
}
}

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

var providers = registrar.entityComponent.get(position).get(entity);
for (var entry : providers) {
var provider = entry.instance();
var origin = entry.instance();
var provider = origin.instance();
tooltip.origin = origin;
try {
switch (position) {
case HEAD -> provider.appendHead(tooltip, accessor, PluginConfig.CLIENT);
Expand All @@ -118,6 +123,7 @@ public static void gatherEntity(Entity entity, ClientAccessor accessor, Tooltip
} catch (Throwable e) {
ExceptionUtil.dump(e, provider.getClass().toString(), tooltip);
}
tooltip.origin = null;
}
}

Expand All @@ -129,10 +135,9 @@ public static ITooltipComponent getIcon(HitResult target) {
if (target.getType() == HitResult.Type.ENTITY) {
var providers = registrar.entityIcon.get(data.getEntity());
for (var provider : providers) {
var icon = provider.instance().getIcon(data, config);
if (icon != null) {
return icon;
}
var origin = provider.instance();
var icon = InspectComponent.maybeWrap(origin.instance().getIcon(data, config), origin, null);
if (icon != null) return icon;
}
} else {
var state = data.getBlockState();
Expand All @@ -142,9 +147,10 @@ public static ITooltipComponent getIcon(HitResult target) {
var priority = 0;

for (var provider : registrar.blockIcon.get(state.getBlock())) {
var icon = provider.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
var origin = provider.instance();
var icon = origin.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
if (icon != null) {
result = icon;
result = InspectComponent.maybeWrap(icon, origin, null);
priority = provider.priority();
break;
}
Expand All @@ -155,9 +161,10 @@ public static ITooltipComponent getIcon(HitResult target) {
for (var provider : registrar.blockIcon.get(blockEntity)) {
if (provider.priority() >= priority) break;

var icon = provider.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
var origin = provider.instance();
var icon = origin.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
if (icon != null) {
result = icon;
result = InspectComponent.maybeWrap(icon, origin, null);
break;
}
}
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/mcp/mobius/waila/gui/hud/ComponentRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package mcp.mobius.waila.gui.hud;

import java.util.Random;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import mcp.mobius.waila.WailaClient;
import mcp.mobius.waila.api.ITooltipComponent;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.Nullable;

import static mcp.mobius.waila.util.DisplayUtil.renderRectBorder;

public abstract class ComponentRenderer {

private static final Random RANDOM = new Random();

public abstract void render(GuiGraphics ctx, ITooltipComponent component, int x, int y, int cw, int ch, DeltaTracker delta);

private static @Nullable ComponentRenderer current = null;

public static ComponentRenderer get() {
if (current == null) current = Default.INSTANCE;
return current;
}

public static void set(@Nullable ComponentRenderer value) {
if (value == null) value = Default.INSTANCE;
current = value;
}

public static class Default extends ComponentRenderer {

public static final Default INSTANCE = new Default();

@Override
public void render(GuiGraphics ctx, ITooltipComponent component, int x, int y, int cw, int ch, DeltaTracker delta) {
component.render(ctx, x, y, delta);

if (WailaClient.showComponentBounds) {
renderBounds(ctx, x, y, cw, ch, 1f);
}
}

public static void renderBounds(GuiGraphics ctx, int x, int y, int cw, int ch, float v) {
ctx.pose().pushPose();
var scale = (float) Minecraft.getInstance().getWindow().getGuiScale();
ctx.pose().scale(1 / scale, 1 / scale, 1);

RenderSystem.setShader(GameRenderer::getPositionColorShader);

var buf = Tesselator.getInstance().begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
var bx = Mth.floor(x * scale + 0.5);
var by = Mth.floor(y * scale + 0.5);
var bw = Mth.floor(cw * scale + 0.5);
var bh = Mth.floor(ch * scale + 0.5);
var color = (0xFF << 24) + Mth.hsvToRgb(RANDOM.nextFloat(), RANDOM.nextFloat(), v);
renderRectBorder(ctx.pose().last().pose(), buf, bx, by, bw, bh, 1, color, color);
BufferUploader.drawWithShader(buf.buildOrThrow());

ctx.pose().popPose();
}

}

}
72 changes: 72 additions & 0 deletions src/main/java/mcp/mobius/waila/gui/hud/InspectComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package mcp.mobius.waila.gui.hud;

import mcp.mobius.waila.api.ITooltipComponent;
import mcp.mobius.waila.registry.PluginAware;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

public class InspectComponent implements ITooltipComponent {

public static boolean wrap = false;

public final ITooltipComponent actual;
public final PluginAware<?> origin;
public final ResourceLocation tag;

private InspectComponent(ITooltipComponent actual, PluginAware<?> origin, ResourceLocation tag) {
this.actual = actual;
this.origin = origin;
this.tag = tag;
}

public static @Nullable ITooltipComponent maybeWrap(@Nullable ITooltipComponent actual, @Nullable PluginAware<?> origin, @Nullable ResourceLocation tag) {
if (!wrap || actual == null || origin == null || actual instanceof InspectComponent) {
return actual;
}

if (actual instanceof ITooltipComponent.HorizontalGrowing hg) {
return new InspectComponent.Growing(hg, origin, tag);
}

return new InspectComponent(actual, origin, tag);
}

@Override
public int getWidth() {
return actual.getWidth();
}

@Override
public int getHeight() {
return actual.getHeight();
}

@Override
public void render(GuiGraphics ctx, int x, int y, DeltaTracker delta) {
actual.render(ctx, x, y, delta);
}

public static class Growing extends InspectComponent implements HorizontalGrowing {

public final HorizontalGrowing actual;

public Growing(HorizontalGrowing actual, PluginAware<?> origin, ResourceLocation tag) {
super(actual, origin, tag);
this.actual = actual;
}

@Override
public int getMinimalWidth() {
return actual.getMinimalWidth();
}

@Override
public void setGrownWidth(int grownWidth) {
actual.setGrownWidth(grownWidth);
}

}

}
12 changes: 7 additions & 5 deletions src/main/java/mcp/mobius/waila/gui/hud/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import mcp.mobius.waila.api.ITooltipComponent.HorizontalGrowing;
import mcp.mobius.waila.api.ITooltipLine;
import mcp.mobius.waila.api.component.WrappedComponent;
import mcp.mobius.waila.util.DisplayUtil;
import mcp.mobius.waila.registry.PluginAware;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
Expand All @@ -20,12 +20,13 @@

public class Line implements ITooltipLine {

@Nullable
public final ResourceLocation tag;
public final @Nullable ResourceLocation tag;
public final List<ITooltipComponent> components = new ArrayList<>();
public final Object2IntOpenHashMap<ITooltipComponent> widths = new Object2IntOpenHashMap<>();
public final Object2IntMap<ITooltipComponent> heights = new Object2IntOpenHashMap<>();

public @Nullable PluginAware<?> origin;

private int fixedWidth = -1;
private int width = -1;
private int height = -1;
Expand All @@ -39,6 +40,7 @@ public Line(@Nullable ResourceLocation tag) {

@Override
public Line with(ITooltipComponent component) {
component = InspectComponent.maybeWrap(component, origin, tag);
components.add(component);
if (component instanceof HorizontalGrowing growing) {
growingWeight += growing.getWeight();
Expand Down Expand Up @@ -140,7 +142,7 @@ public int getHeight() {
return height;
}

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

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

var cy = y + (h < height ? (height - h) / 2 : 0);
DisplayUtil.renderComponent(ctx, component, cx, cy, w, delta);
renderer.render(ctx, component, cx, cy, w, h, delta);
cx += w + 1;
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/mcp/mobius/waila/gui/hud/Tooltip.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import mcp.mobius.waila.api.ITooltip;
import mcp.mobius.waila.api.ITooltipLine;
import mcp.mobius.waila.registry.PluginAware;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

public class Tooltip extends ObjectArrayList<Line> implements ITooltip {

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

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

@Override
public ITooltipLine getLine(int index) {
return get(index);
var line = get(index);
line.origin = origin;
return line;
}

@Override
public ITooltipLine addLine() {
var line = new Line(null);
line.origin = origin;
add(line);
return line;
}

@Override
public ITooltipLine setLine(ResourceLocation tag) {
var line = new Line(tag);
line.origin = origin;
setLine(tag, line);
return line;
}
Expand Down
Loading

0 comments on commit 131f2bd

Please sign in to comment.