From 08887a61f46e0081d837ee4d07947e5d52db50c5 Mon Sep 17 00:00:00 2001 From: deirn Date: Mon, 6 Jan 2025 00:00:14 +0700 Subject: [PATCH] start working on inspector screen --- .../java/mcp/mobius/waila/WailaClient.java | 2 +- .../mobius/waila/access/ClientAccessor.java | 17 ++ .../mobius/waila/command/ClientCommand.java | 8 + .../waila/gui/hud/ComponentHandler.java | 19 ++- .../waila/gui/hud/ComponentRenderer.java | 42 +++++ .../java/mcp/mobius/waila/gui/hud/Line.java | 5 +- .../mobius/waila/gui/hud/TooltipHandler.java | 54 +++--- .../mobius/waila/gui/hud/TooltipRenderer.java | 35 ++-- .../waila/gui/screen/InspectorScreen.java | 158 ++++++++++++++++++ .../waila/gui/screen/ThemeEditorScreen.java | 3 +- .../waila/gui/screen/WailaConfigScreen.java | 3 +- .../mobius/waila/registry/PluginAware.java | 7 + .../mcp/mobius/waila/registry/Registrar.java | 20 +-- .../mcp/mobius/waila/service/ApiService.java | 3 +- .../mobius/waila/service/MixedService.java | 3 +- .../mcp/mobius/waila/util/DisplayUtil.java | 30 ---- .../resources/assets/waila/lang/en_us.json | 2 + 17 files changed, 320 insertions(+), 91 deletions(-) create mode 100644 src/main/java/mcp/mobius/waila/gui/hud/ComponentRenderer.java create mode 100644 src/main/java/mcp/mobius/waila/gui/screen/InspectorScreen.java create mode 100644 src/main/java/mcp/mobius/waila/registry/PluginAware.java diff --git a/src/main/java/mcp/mobius/waila/WailaClient.java b/src/main/java/mcp/mobius/waila/WailaClient.java index 6b34139f2..3a0cac00b 100644 --- a/src/main/java/mcp/mobius/waila/WailaClient.java +++ b/src/main/java/mcp/mobius/waila/WailaClient.java @@ -94,7 +94,7 @@ protected static void onClientTick() { protected static void onItemTooltip(ItemStack stack, List 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; diff --git a/src/main/java/mcp/mobius/waila/access/ClientAccessor.java b/src/main/java/mcp/mobius/waila/access/ClientAccessor.java index a67a4625e..f7b2f2424 100644 --- a/src/main/java/mcp/mobius/waila/access/ClientAccessor.java +++ b/src/main/java/mcp/mobius/waila/access/ClientAccessor.java @@ -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; @@ -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; diff --git a/src/main/java/mcp/mobius/waila/command/ClientCommand.java b/src/main/java/mcp/mobius/waila/command/ClientCommand.java index 54281929b..1874e424e 100644 --- a/src/main/java/mcp/mobius/waila/command/ClientCommand.java +++ b/src/main/java/mcp/mobius/waila/command/ClientCommand.java @@ -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; @@ -158,6 +159,13 @@ protected final void register(ArgumentBuilderBuilder command) { }) .pop("enabled", "showFps") + .then(literal("inspect")) + .executes(context -> { + Minecraft.getInstance().schedule(InspectorScreen::open); + return 1; + }) + .pop("inspect") + .pop("debug"); } diff --git a/src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java b/src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java index fe1fb0462..46dd697e1 100644 --- a/src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java +++ b/src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java @@ -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); @@ -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); } } @@ -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); @@ -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); } } @@ -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; } @@ -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(); @@ -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; diff --git a/src/main/java/mcp/mobius/waila/gui/hud/ComponentRenderer.java b/src/main/java/mcp/mobius/waila/gui/hud/ComponentRenderer.java new file mode 100644 index 000000000..31148ef8d --- /dev/null +++ b/src/main/java/mcp/mobius/waila/gui/hud/ComponentRenderer.java @@ -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(); + } + }; + +} diff --git a/src/main/java/mcp/mobius/waila/gui/hud/Line.java b/src/main/java/mcp/mobius/waila/gui/hud/Line.java index ad323041a..d68712dc2 100644 --- a/src/main/java/mcp/mobius/waila/gui/hud/Line.java +++ b/src/main/java/mcp/mobius/waila/gui/hud/Line.java @@ -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; @@ -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; @@ -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; } } diff --git a/src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java b/src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java index 211133abb..db4650e78 100644 --- a/src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java +++ b/src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java @@ -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()); @@ -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; @@ -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(); @@ -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) { @@ -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); @@ -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; diff --git a/src/main/java/mcp/mobius/waila/gui/hud/TooltipRenderer.java b/src/main/java/mcp/mobius/waila/gui/hud/TooltipRenderer.java index 7f1d66336..7a64c2564 100644 --- a/src/main/java/mcp/mobius/waila/gui/hud/TooltipRenderer.java +++ b/src/main/java/mcp/mobius/waila/gui/hud/TooltipRenderer.java @@ -35,8 +35,6 @@ import net.minecraft.util.Mth; import org.jetbrains.annotations.Nullable; -import static mcp.mobius.waila.util.DisplayUtil.renderComponent; - public class TooltipRenderer { private static final Tooltip TOOLTIP = new Tooltip(); @@ -99,10 +97,15 @@ public static void setIcon(ITooltipComponent icon) { public static Rectangle endBuild() { Preconditions.checkState(started); + var accessor = ClientAccessor.INSTANCE; if (state.fireEvent()) { - for (var listener : Registrar.get().eventListeners.get(Object.class)) { - listener.instance().onHandleTooltip(TOOLTIP, ClientAccessor.INSTANCE, PluginConfig.CLIENT); + for (var entry : Registrar.get().eventListeners.get(Object.class)) { + var pa = entry.instance(); + var listener = pa.instance(); + accessor.setOrigin(pa.origin(), listener.getClass()); + listener.onHandleTooltip(TOOLTIP, accessor, PluginConfig.CLIENT); + accessor.setOrigin(null, null); } } @@ -183,13 +186,13 @@ public static void resetState() { state = null; } - public static void render(GuiGraphics ctx, DeltaTracker delta) { + public static void render(ComponentRenderer renderer, GuiGraphics ctx, DeltaTracker delta) { try (var ignored = ProfilerUtil.profile("wthit:render")) { - _render(ctx, delta); + _render(renderer, ctx, delta); } } - private static void _render(GuiGraphics ctx, DeltaTracker delta) { + private static void _render(ComponentRenderer renderer, GuiGraphics ctx, DeltaTracker delta) { var client = Minecraft.getInstance(); if (WailaClient.showFps) { @@ -208,7 +211,7 @@ private static void _render(GuiGraphics ctx, DeltaTracker delta) { // TODO: Figure out why opacity not working properly //noinspection ConstantValue if (true) { - renderUncached(client, ctx, delta); + renderUncached(renderer, ctx, delta); return; } @@ -232,7 +235,7 @@ private static void _render(GuiGraphics ctx, DeltaTracker delta) { client.getMainRenderTarget().unbindWrite(); framebuffer.clear(); framebuffer.bindWrite(true); - renderUncached(client, ctx, delta); + renderUncached(renderer, ctx, delta); framebuffer.unbindWrite(); client.getMainRenderTarget().bindWrite(true); lastFrame = now; @@ -259,13 +262,13 @@ private static void _render(GuiGraphics ctx, DeltaTracker delta) { RenderSystem.disableBlend(); } - private static void renderUncached(Minecraft client, GuiGraphics ctx, DeltaTracker delta) { + private static void renderUncached(ComponentRenderer renderer, GuiGraphics ctx, DeltaTracker delta) { try (var ignored = ProfilerUtil.profile("wthit:render_uncached")) { - _renderUncached(client, ctx, delta); + _renderUncached(renderer, ctx, delta); } } - private static void _renderUncached(Minecraft client, GuiGraphics ctx, DeltaTracker delta) { + private static void _renderUncached(ComponentRenderer renderer, GuiGraphics ctx, DeltaTracker delta) { var scale = state.getScale(); ctx.pose().pushPose(); @@ -278,7 +281,7 @@ private static void _renderUncached(Minecraft client, GuiGraphics ctx, DeltaTrac var canceller = EventCanceller.INSTANCE; canceller.setCanceled(false); for (var listener : Registrar.get().eventListeners.get(Object.class)) { - listener.instance().onBeforeTooltipRender(ctx, rect, ClientAccessor.INSTANCE, PluginConfig.CLIENT, canceller); + listener.instance().instance().onBeforeTooltipRender(ctx, rect, ClientAccessor.INSTANCE, PluginConfig.CLIENT, canceller); if (canceller.isCanceled()) { ctx.pose().popPose(); RenderSystem.enableDepthTest(); @@ -305,7 +308,7 @@ private static void _renderUncached(Minecraft client, GuiGraphics ctx, DeltaTrac } for (var line : TOOLTIP) { - line.render(ctx, textX, textY, delta); + line.render(renderer, ctx, textX, textY, delta); textY += line.getHeight() + 1; } @@ -313,7 +316,7 @@ private static void _renderUncached(Minecraft client, GuiGraphics ctx, DeltaTrac if (state.fireEvent()) { for (var listener : Registrar.get().eventListeners.get(Object.class)) { - listener.instance().onAfterTooltipRender(ctx, rect, ClientAccessor.INSTANCE, PluginConfig.CLIENT); + listener.instance().instance().onAfterTooltipRender(ctx, rect, ClientAccessor.INSTANCE, PluginConfig.CLIENT); } } @@ -322,7 +325,7 @@ private static void _renderUncached(Minecraft client, GuiGraphics ctx, DeltaTrac if (iconPos == Align.Y.BOTTOM) { iconY++; } - renderComponent(ctx, icon, x + padding.left, iconY, 0, delta); + renderer.render(ctx, icon, x + padding.left, iconY, icon.getWidth(), icon.getHeight(), delta); RenderSystem.enableDepthTest(); ctx.pose().popPose(); diff --git a/src/main/java/mcp/mobius/waila/gui/screen/InspectorScreen.java b/src/main/java/mcp/mobius/waila/gui/screen/InspectorScreen.java new file mode 100644 index 000000000..f3e615289 --- /dev/null +++ b/src/main/java/mcp/mobius/waila/gui/screen/InspectorScreen.java @@ -0,0 +1,158 @@ +package mcp.mobius.waila.gui.screen; + +import mcp.mobius.waila.Waila; +import mcp.mobius.waila.WailaClient; +import mcp.mobius.waila.api.ITheme; +import mcp.mobius.waila.api.ITooltipComponent; +import mcp.mobius.waila.api.IWailaConfig; +import mcp.mobius.waila.buildconst.Tl; +import mcp.mobius.waila.gui.hud.ComponentRenderer; +import mcp.mobius.waila.gui.hud.TooltipHandler; +import mcp.mobius.waila.gui.hud.TooltipRenderer; +import net.minecraft.client.DeltaTracker; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.NotNull; + +public class InspectorScreen extends YesIAmSureTheClientInstanceIsPresentByTheTimeIUseItScreen { + + private static final Component TITLE = Component.translatable(Tl.Gui.Inspect.TITLE); + private static final State STATE = new State(); + + private final boolean initShowBounds; + private final Renderer renderer = new Renderer(); + + private InspectorScreen(boolean initShowBounds) { + super(TITLE); + this.initShowBounds = initShowBounds; + } + + public static boolean open() { + var client = Minecraft.getInstance(); + var success = TooltipHandler.tick(STATE, true); + if (!success) return false; + + client.setScreen(new InspectorScreen(WailaClient.showComponentBounds)); + WailaClient.showComponentBounds = true; + return true; + } + + @Override + protected void init() { + super.init(); + + var success = TooltipHandler.tick(STATE, true); + } + + @Override + public void onClose() { + super.onClose(); + WailaClient.showComponentBounds = initShowBounds; + } + + @Override + public void render(@NotNull GuiGraphics ctx, int mouseX, int mouseY, float tickDelta) { + super.render(ctx, mouseX, mouseY, tickDelta); + + renderer.mouseX = mouseX; + renderer.mouseY = mouseY; + renderer.hoveredComponent = null; + TooltipRenderer.render(renderer, ctx, minecraft.getDeltaTracker()); + + if (renderer.hoveredComponent != null) { + ctx.renderTooltip(minecraft.font, Component.literal(renderer.hoveredComponent.getClass().getName()), mouseX, mouseY); + } + } + + private static class Renderer implements ComponentRenderer { + + int mouseX, mouseY; + ITooltipComponent hoveredComponent; + + @Override + public void render(GuiGraphics ctx, ITooltipComponent component, int x, int y, int cw, int ch, DeltaTracker delta) { + ComponentRenderer.DEFAULT.render(ctx, component, x, y, cw, ch, delta); + + if (x < mouseX && mouseX < (x + cw) && y < mouseY && mouseY < (y + ch)) { + hoveredComponent = component; + } + } + + } + + private static class State implements TooltipRenderer.State { + + @Override + public int getX() { + return 0; + } + + @Override + public int getY() { + return 0; + } + + @Override + public boolean render() { + return true; + } + + @Override + public boolean fireEvent() { + return true; + } + + @Override + public int getFps() { + return 0; + } + + @Override + public float getScale() { + return 1; + } + + @Override + public IWailaConfig.Overlay.Position.Align.X getXAnchor() { + return IWailaConfig.Overlay.Position.Align.X.CENTER; + } + + @Override + public IWailaConfig.Overlay.Position.Align.Y getYAnchor() { + return IWailaConfig.Overlay.Position.Align.Y.MIDDLE; + } + + @Override + public IWailaConfig.Overlay.Position.Align.X getXAlign() { + return IWailaConfig.Overlay.Position.Align.X.CENTER; + } + + @Override + public IWailaConfig.Overlay.Position.Align.Y getYAlign() { + return IWailaConfig.Overlay.Position.Align.Y.MIDDLE; + } + + @Override + public boolean bossBarsOverlap() { + return false; + } + + @Override + public boolean enableTextToSpeech() { + return false; + } + + @Override + public int getBackgroundAlpha() { + return 0xFF; + } + + @Override + public ITheme getTheme() { + return Waila.CONFIG.get().getOverlay().getColor().getTheme(); + } + + } + +} diff --git a/src/main/java/mcp/mobius/waila/gui/screen/ThemeEditorScreen.java b/src/main/java/mcp/mobius/waila/gui/screen/ThemeEditorScreen.java index 16c94d80a..97e4d4c90 100644 --- a/src/main/java/mcp/mobius/waila/gui/screen/ThemeEditorScreen.java +++ b/src/main/java/mcp/mobius/waila/gui/screen/ThemeEditorScreen.java @@ -6,6 +6,7 @@ import mcp.mobius.waila.api.ITheme; import mcp.mobius.waila.api.IWailaConfig.Overlay.Position.Align; import mcp.mobius.waila.buildconst.Tl; +import mcp.mobius.waila.gui.hud.ComponentRenderer; import mcp.mobius.waila.gui.hud.TooltipRenderer; import mcp.mobius.waila.gui.hud.theme.ThemeAccessor; import mcp.mobius.waila.gui.hud.theme.ThemeDefinition; @@ -179,7 +180,7 @@ private void addTypeProperties(ConfigListWidget options) { @Override protected void renderForeground(GuiGraphics ctx, int rowLeft, int rowWidth, int mouseX, int mouseY, float partialTicks) { - TooltipRenderer.render(ctx, minecraft.getDeltaTracker()); + TooltipRenderer.render(ComponentRenderer.DEFAULT, ctx, minecraft.getDeltaTracker()); } @Override diff --git a/src/main/java/mcp/mobius/waila/gui/screen/WailaConfigScreen.java b/src/main/java/mcp/mobius/waila/gui/screen/WailaConfigScreen.java index b73370cce..88194e15e 100644 --- a/src/main/java/mcp/mobius/waila/gui/screen/WailaConfigScreen.java +++ b/src/main/java/mcp/mobius/waila/gui/screen/WailaConfigScreen.java @@ -14,6 +14,7 @@ import mcp.mobius.waila.api.component.ItemComponent; import mcp.mobius.waila.buildconst.Tl; import mcp.mobius.waila.config.WailaConfig; +import mcp.mobius.waila.gui.hud.ComponentRenderer; import mcp.mobius.waila.gui.hud.Line; import mcp.mobius.waila.gui.hud.TooltipRenderer; import mcp.mobius.waila.gui.hud.theme.ThemeDefinition; @@ -122,7 +123,7 @@ public void render(@NotNull GuiGraphics ctx, int mouseX, int mouseY, float parti } renderBackground(ctx, mouseX, mouseY, partialTicks); - TooltipRenderer.render(ctx, minecraft.getDeltaTracker()); + TooltipRenderer.render(ComponentRenderer.DEFAULT, ctx, minecraft.getDeltaTracker()); } else { TooltipRenderer.resetState(); f1held = false; diff --git a/src/main/java/mcp/mobius/waila/registry/PluginAware.java b/src/main/java/mcp/mobius/waila/registry/PluginAware.java new file mode 100644 index 000000000..f1b4a3290 --- /dev/null +++ b/src/main/java/mcp/mobius/waila/registry/PluginAware.java @@ -0,0 +1,7 @@ +package mcp.mobius.waila.registry; + +import mcp.mobius.waila.api.IPluginInfo; + +@SuppressWarnings("deprecation") +public record PluginAware(IPluginInfo origin, T instance) { +} diff --git a/src/main/java/mcp/mobius/waila/registry/Registrar.java b/src/main/java/mcp/mobius/waila/registry/Registrar.java index 7dfd9cf39..6b073a69d 100644 --- a/src/main/java/mcp/mobius/waila/registry/Registrar.java +++ b/src/main/java/mcp/mobius/waila/registry/Registrar.java @@ -60,10 +60,10 @@ public class Registrar implements ICommonRegistrar, IClientRegistrar, IRegistrar public final InstanceRegistry blockRedirect = new InstanceRegistry<>(); public final InstanceRegistry blockOverride = new InstanceRegistry<>(); - public final InstanceRegistry blockIcon = new InstanceRegistry<>(); + public final InstanceRegistry> blockIcon = new InstanceRegistry<>(); public final InstanceRegistry blockDataCtx = new InstanceRegistry<>(); public final InstanceRegistry> blockData = new InstanceRegistry<>(); - public final Map> blockComponent = Util.make(new EnumMap<>(TooltipPosition.class), map -> { + public final Map>> blockComponent = Util.make(new EnumMap<>(TooltipPosition.class), map -> { for (var key : TooltipPosition.values()) { map.put(key, new InstanceRegistry<>()); } @@ -71,16 +71,16 @@ public class Registrar implements ICommonRegistrar, IClientRegistrar, IRegistrar public final InstanceRegistry entityRedirect = new InstanceRegistry<>(); public final InstanceRegistry entityOverride = new InstanceRegistry<>(); - public final InstanceRegistry entityIcon = new InstanceRegistry<>(); + public final InstanceRegistry> entityIcon = new InstanceRegistry<>(); public final InstanceRegistry entityDataCtx = new InstanceRegistry<>(); public final InstanceRegistry> entityData = new InstanceRegistry<>(); - public final Map> entityComponent = Util.make(new EnumMap<>(TooltipPosition.class), map -> { + public final Map>> entityComponent = Util.make(new EnumMap<>(TooltipPosition.class), map -> { for (var key : TooltipPosition.values()) { map.put(key, new InstanceRegistry<>()); } }); - public final InstanceRegistry eventListeners = Util.make(new InstanceRegistry<>(), InstanceRegistry::reversed); + public final InstanceRegistry> eventListeners = Util.make(new InstanceRegistry<>(), InstanceRegistry::reversed); public final InstanceRegistry raycastVectorProviders = new InstanceRegistry<>(); public final BlacklistConfig blacklist = new BlacklistConfig(); @@ -200,7 +200,7 @@ public void configAlias(ResourceLocation actual, ResourceLocation... aliases) { public void eventListener(IEventListener listener, int priority) { if (skip()) return; assertLock(); - eventListeners.add(Object.class, listener, priority); + eventListeners.add(Object.class, new PluginAware<>(plugin, listener), priority); } @Override @@ -250,7 +250,7 @@ public void icon(IBlockComponentProvider provider, Class clazz, int prior assertLock(); assertPriority(priority); warnTargetClass(provider, clazz); - blockIcon.add(clazz, provider, priority); + blockIcon.add(clazz, new PluginAware<>(plugin, provider), priority); } } @@ -260,7 +260,7 @@ private void component(IBlockComponentProvider provider, TooltipPosition pos assertLock(); assertPriority(priority); warnTargetClass(provider, clazz); - blockComponent.get(position).add(clazz, provider, priority); + blockComponent.get(position).add(clazz, new PluginAware<>(plugin, provider), priority); } } @@ -337,7 +337,7 @@ public void icon(IEntityComponentProvider provider, Class clazz, int prio assertLock(); assertPriority(priority); warnTargetClass(provider, clazz); - entityIcon.add(clazz, provider, priority); + entityIcon.add(clazz, new PluginAware<>(plugin, provider), priority); } } @@ -347,7 +347,7 @@ private void component(IEntityComponentProvider provider, TooltipPosition po assertLock(); assertPriority(priority); warnTargetClass(provider, clazz); - entityComponent.get(position).add(clazz, provider, priority); + entityComponent.get(position).add(clazz, new PluginAware<>(plugin, provider), priority); } } diff --git a/src/main/java/mcp/mobius/waila/service/ApiService.java b/src/main/java/mcp/mobius/waila/service/ApiService.java index e905beda7..43cb58c8a 100644 --- a/src/main/java/mcp/mobius/waila/service/ApiService.java +++ b/src/main/java/mcp/mobius/waila/service/ApiService.java @@ -21,6 +21,7 @@ import mcp.mobius.waila.api.IWailaConfig; import mcp.mobius.waila.api.__internal__.IApiService; import mcp.mobius.waila.config.JsonConfig; +import mcp.mobius.waila.gui.hud.ComponentRenderer; import mcp.mobius.waila.gui.hud.TooltipRenderer; import mcp.mobius.waila.gui.hud.theme.ThemeType; import mcp.mobius.waila.plugin.PluginInfo; @@ -122,7 +123,7 @@ public IWailaConfig getConfig() { @Override public void renderComponent(GuiGraphics ctx, ITooltipComponent component, int x, int y, DeltaTracker delta) { - DisplayUtil.renderComponent(ctx, component, x, y, 0, delta); + ComponentRenderer.DEFAULT.render(ctx, component, x, y, component.getWidth(), component.getHeight(), delta); } @Override diff --git a/src/main/java/mcp/mobius/waila/service/MixedService.java b/src/main/java/mcp/mobius/waila/service/MixedService.java index 3638fc40f..a009268de 100644 --- a/src/main/java/mcp/mobius/waila/service/MixedService.java +++ b/src/main/java/mcp/mobius/waila/service/MixedService.java @@ -6,6 +6,7 @@ import mcp.mobius.waila.WailaClient; import mcp.mobius.waila.config.JsonConfig; import mcp.mobius.waila.config.PluginConfig; +import mcp.mobius.waila.gui.hud.ComponentRenderer; import mcp.mobius.waila.gui.hud.TooltipRenderer; import mcp.mobius.waila.mixed.IMixedService; import mcp.mobius.waila.registry.RegistryFilter; @@ -30,7 +31,7 @@ public void onServerLogin() { @Override public void onGuiRender(GuiGraphics ctx, DeltaTracker delta) { - TooltipRenderer.render(ctx, delta); + TooltipRenderer.render(ComponentRenderer.DEFAULT, ctx, delta); } @Override diff --git a/src/main/java/mcp/mobius/waila/util/DisplayUtil.java b/src/main/java/mcp/mobius/waila/util/DisplayUtil.java index 056c2d0c6..f7dc352d8 100644 --- a/src/main/java/mcp/mobius/waila/util/DisplayUtil.java +++ b/src/main/java/mcp/mobius/waila/util/DisplayUtil.java @@ -1,27 +1,18 @@ package mcp.mobius.waila.util; import java.util.IllegalFormatException; -import java.util.Random; import com.mojang.blaze3d.platform.Lighting; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.VertexConsumer; -import mcp.mobius.waila.WailaClient; -import mcp.mobius.waila.api.ITooltipComponent; -import mcp.mobius.waila.api.util.WRenders; -import net.minecraft.client.DeltaTracker; import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.Button; -import net.minecraft.client.renderer.RenderType; import net.minecraft.network.chat.Component; import net.minecraft.util.ARGB; -import net.minecraft.util.Mth; import org.joml.Matrix4f; public final class DisplayUtil { - private static final Random RANDOM = new Random(); private static final Minecraft CLIENT = Minecraft.getInstance(); @@ -48,27 +39,6 @@ public static void renderRectBorder(Matrix4f matrix, VertexConsumer buf, int x, // @formatter:on } - public static void renderComponent(GuiGraphics ctx, ITooltipComponent component, int x, int y, int cw, DeltaTracker 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 == 0 ? component.getWidth() : cw) * scale + 0.5); - var bh = Mth.floor(component.getHeight() * scale + 0.5); - var color = (0xFF << 24) + Mth.hsvToRgb(RANDOM.nextFloat(), RANDOM.nextFloat(), 1f); - renderRectBorder(ctx.pose().last().pose(), buf, bx, by, bw, bh, 1, color, color); - - ctx.pose().popPose(); - ctx.flush(); - } - } - public static void fillGradient(Matrix4f matrix, VertexConsumer buf, int x, int y, int w, int h, int start, int end) { var sa = ARGB.alphaFloat(start); var sr = ARGB.redFloat(start); diff --git a/src/resources/resources/assets/waila/lang/en_us.json b/src/resources/resources/assets/waila/lang/en_us.json index 6da73b459..1f1a24cab 100644 --- a/src/resources/resources/assets/waila/lang/en_us.json +++ b/src/resources/resources/assets/waila/lang/en_us.json @@ -106,6 +106,8 @@ "gui.waila.credits.authors" : "Authors", "gui.waila.credits.contributors" : "Contributors", + "gui.waila.inspect.title" : "Inspect", + "json5.waila.config.default_value" : "Default value: [%s]", "json5.waila.config.available_values" : "Available values: [%s]",