Skip to content

Commit

Permalink
start working on inspector screen
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Jan 5, 2025
1 parent 3db2370 commit 08887a6
Show file tree
Hide file tree
Showing 17 changed files with 320 additions and 91 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
17 changes: 17 additions & 0 deletions src/main/java/mcp/mobius/waila/access/ClientAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import mcp.mobius.waila.api.ICommonAccessor;
import mcp.mobius.waila.api.IDataReader;
import mcp.mobius.waila.api.IEntityAccessor;
import mcp.mobius.waila.api.IPluginInfo;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.registries.BuiltInRegistries;
Expand Down Expand Up @@ -45,6 +46,22 @@ public enum ClientAccessor implements ICommonAccessor, IBlockAccessor, IEntityAc
private double rayCastMaxDistance;
private float frameTime;

private @Nullable IPluginInfo plugin;
private @Nullable Class<?> provider;

public void setOrigin(@Nullable IPluginInfo plugin, @Nullable Class<?> provider) {
this.plugin = plugin;
this.provider = provider;
}

public @Nullable IPluginInfo getPlugin() {
return plugin;
}

public @Nullable Class<?> getProvider() {
return provider;
}

@Override
public Level getWorld() {
return this.world;
Expand Down
8 changes: 8 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,13 @@ protected final void register(ArgumentBuilderBuilder<S> command) {
})
.pop("enabled", "showFps")

.then(literal("inspect"))
.executes(context -> {
Minecraft.getInstance().schedule(InspectorScreen::open);
return 1;
})
.pop("inspect")

.pop("debug");
}

Expand Down
19 changes: 14 additions & 5 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 pa = entry.instance();
var provider = pa.instance();
accessor.setOrigin(pa.origin(), provider.getClass());
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);
}
accessor.setOrigin(null, 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 pa = entry.instance();
var provider = pa.instance();
accessor.setOrigin(pa.origin(), provider.getClass());
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);
}
accessor.setOrigin(null, null);
}
}

Expand All @@ -129,7 +135,8 @@ 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);
var pa = provider.instance();
var icon = pa.instance().getIcon(data, config);
if (icon != null) {
return icon;
}
Expand All @@ -142,7 +149,8 @@ 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 pa = provider.instance();
var icon = pa.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
if (icon != null) {
result = icon;
priority = provider.priority();
Expand All @@ -155,7 +163,8 @@ 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 pa = provider.instance();
var icon = pa.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
if (icon != null) {
result = icon;
break;
Expand Down
42 changes: 42 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,42 @@
package mcp.mobius.waila.gui.hud;

import java.util.Random;

import mcp.mobius.waila.WailaClient;
import mcp.mobius.waila.api.ITooltipComponent;
import mcp.mobius.waila.api.util.WRenders;
import mcp.mobius.waila.util.DisplayUtil;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.util.Mth;

public interface ComponentRenderer {

Random RANDOM = new Random();

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

ComponentRenderer DEFAULT = (ctx, component, x, y, cw, ch, delta) -> {
component.render(ctx, x, y, delta);

if (WailaClient.showComponentBounds) {
ctx.pose().pushPose();
var scale = (float) Minecraft.getInstance().getWindow().getGuiScale();
ctx.pose().scale(1 / scale, 1 / scale, 1);

var buf = WRenders.buffer(RenderType.gui());
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(), 1f);
DisplayUtil.renderRectBorder(ctx.pose().last().pose(), buf, bx, by, bw, bh, 1, color, color);

ctx.pose().popPose();
ctx.flush();
}
};

}
5 changes: 2 additions & 3 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,6 @@
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 net.minecraft.client.DeltaTracker;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
Expand Down Expand Up @@ -140,7 +139,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 +149,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
54 changes: 32 additions & 22 deletions src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,38 @@ private enum ProcessResult {
}

public static void tick() {
tick(STATE, false);
}

public static boolean tick(TooltipRenderer.State state, boolean inspect) {
try (var ignored = ProfilerUtil.profile("wthit:tick")) {
_tick();
return _tick(state, inspect);
}
}

private static void _tick() {
private static boolean _tick(TooltipRenderer.State state, boolean inspect) {
STATE.render = false;

var client = Minecraft.getInstance();
var config = Waila.CONFIG.get().getGeneral();

if (client.options.hideGui) return;
if (client.screen != null && !(client.screen instanceof ChatScreen)) return;
if (client.level == null || !config.isDisplayTooltip()) return;
if (config.getDisplayMode() == IWailaConfig.General.DisplayMode.HOLD_KEY && !WailaClient.keyShowOverlay.isDown()) return;
if (config.isHideFromPlayerList() && ((PlayerTabOverlayAccess) client.gui.getTabList()).wthit_isVisible()) return;
if (config.isHideFromDebug() && client.getDebugOverlay().showDebugScreen()) return;
if (client.gameMode == null) return;
if (client.level == null) return false;
if (client.gameMode == null) return false;

if (!inspect) {
if (client.options.hideGui) return false;
if (client.screen != null && !(client.screen instanceof ChatScreen)) return false;
if (!config.isDisplayTooltip()) return false;
if (config.getDisplayMode() == IWailaConfig.General.DisplayMode.HOLD_KEY && !WailaClient.keyShowOverlay.isDown()) return false;
if (config.isHideFromPlayerList() && ((PlayerTabOverlayAccess) client.gui.getTabList()).wthit_isVisible()) return false;
if (config.isHideFromDebug() && client.getDebugOverlay().showDebugScreen()) return false;
}

Player player = client.player;
if (player == null) return;
if (player == null) return false;

var camera = client.cameraEntity;
if (camera == null) return;
if (camera == null) return false;

var frameTime = client.getDeltaTracker().getGameTimeDeltaPartialTick(true);
var pickRange = Math.max(player.blockInteractionRange(), player.entityInteractionRange());
Expand All @@ -87,14 +95,16 @@ private static void _tick() {
break;
}

if (castOrigin == null) return;
if (castOrigin == null) return false;

for (var target : results) {
if (processTarget(target, client, player, castOrigin, castDirection, pickRange, config) == ProcessResult.BREAK) break;
if (processTarget(state, target, client, player, castOrigin, castDirection, pickRange, config) == ProcessResult.BREAK) break;
}

return true;
}

private static ProcessResult redirectTarget(HitResult target, TargetRedirector redirector, Minecraft client, Player player, Vec3 castOrigin, Vec3 castDirection, double pickRange, WailaConfig.General config) {
private static ProcessResult redirectTarget(TooltipRenderer.State state, HitResult target, TargetRedirector redirector, Minecraft client, Player player, Vec3 castOrigin, Vec3 castDirection, double pickRange, WailaConfig.General config) {
if (redirector.nowhere) return ProcessResult.BREAK;
if (redirector.behind) return ProcessResult.CONTINUE;

Expand All @@ -103,18 +113,18 @@ private static ProcessResult redirectTarget(HitResult target, TargetRedirector r
if (redirect.getType() == HitResult.Type.MISS) return ProcessResult.CONTINUE;

return processTarget(
redirect, client, player,
state, redirect, client, player,
castOrigin.subtract(target.getLocation().subtract(redirect.getLocation())),
castDirection, pickRange, config);
}

private static ProcessResult processTarget(HitResult target, Minecraft client, Player player, Vec3 castOrigin, Vec3 castDirection, double pickRange, WailaConfig.General config) {
private static ProcessResult processTarget(TooltipRenderer.State state, HitResult target, Minecraft client, Player player, Vec3 castOrigin, Vec3 castDirection, double pickRange, WailaConfig.General config) {
var accessor = ClientAccessor.INSTANCE;

//noinspection DataFlowIssue
accessor.set(client.level, player, target, client.cameraEntity, castOrigin, castDirection, pickRange, client.getDeltaTracker().getGameTimeDeltaPartialTick(true));

TooltipRenderer.beginBuild(STATE);
TooltipRenderer.beginBuild(state);

if (target.getType() == HitResult.Type.BLOCK) {
var block = accessor.getBlock();
Expand Down Expand Up @@ -142,7 +152,7 @@ private static ProcessResult processTarget(HitResult target, Minecraft client, P
}

if (redirectResult != null && !redirector.self) {
return redirectTarget(target, redirector, client, player, castOrigin, castDirection, pickRange, config);
return redirectTarget(state, target, redirector, client, player, castOrigin, castDirection, pickRange, config);
}

if (block instanceof LiquidBlock) {
Expand All @@ -151,10 +161,10 @@ private static ProcessResult processTarget(HitResult target, Minecraft client, P
return ProcessResult.CONTINUE;
}

var state = ComponentHandler.getOverrideBlock(target);
if (state == IBlockComponentProvider.EMPTY_BLOCK_STATE) return ProcessResult.CONTINUE;
var blockState = ComponentHandler.getOverrideBlock(target);
if (blockState == IBlockComponentProvider.EMPTY_BLOCK_STATE) return ProcessResult.CONTINUE;

accessor.setState(state);
accessor.setState(blockState);

requestBlockData(accessor);

Expand Down Expand Up @@ -187,7 +197,7 @@ private static ProcessResult processTarget(HitResult target, Minecraft client, P
}

if (redirectResult != null && !redirector.self) {
return redirectTarget(target, redirector, client, player, castOrigin, castDirection, pickRange, config);
return redirectTarget(state, target, redirector, client, player, castOrigin, castDirection, pickRange, config);
}

if (!PluginConfig.CLIENT.getBoolean(WailaConstants.CONFIG_SHOW_ENTITY)) return ProcessResult.CONTINUE;
Expand Down
Loading

0 comments on commit 08887a6

Please sign in to comment.