Skip to content

Commit

Permalink
pass 2
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Jan 8, 2025
1 parent 08887a6 commit 386b04a
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 78 deletions.
17 changes: 0 additions & 17 deletions src/main/java/mcp/mobius/waila/access/ClientAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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 @@ -46,22 +45,6 @@ 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
3 changes: 2 additions & 1 deletion src/main/java/mcp/mobius/waila/command/ClientCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ protected final void register(ArgumentBuilderBuilder<S> command) {

.then(literal("inspect"))
.executes(context -> {
Minecraft.getInstance().schedule(InspectorScreen::open);
var client = Minecraft.getInstance();
client.schedule(() -> client.setScreen(new InspectorScreen()));
return 1;
})
.pop("inspect")
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mcp.mobius.waila.gui.hud;

import java.util.Objects;
import java.util.function.Function;

import lol.bai.badpackets.api.PacketSender;
import mcp.mobius.waila.Waila;
Expand All @@ -13,6 +14,7 @@
import mcp.mobius.waila.config.PluginConfig;
import mcp.mobius.waila.network.play.c2s.BlockDataRequestPlayC2SPacket;
import mcp.mobius.waila.network.play.c2s.EntityDataRequestPlayC2SPacket;
import mcp.mobius.waila.registry.PluginAware;
import mcp.mobius.waila.registry.Registrar;
import mcp.mobius.waila.util.ExceptionUtil;
import net.minecraft.client.Minecraft;
Expand All @@ -25,6 +27,8 @@

public class ComponentHandler {

public static @Nullable Function<PluginAware<?>, Line.Wrapper> wrapperFactory;

public static void requestBlockData(ClientAccessor accessor) {
var registrar = Registrar.get();
var block = accessor.getBlock();
Expand Down Expand Up @@ -68,7 +72,7 @@ private static void handleBlock(ClientAccessor accessor, Tooltip tooltip, Object
for (var entry : providers) {
var pa = entry.instance();
var provider = pa.instance();
accessor.setOrigin(pa.origin(), provider.getClass());
if (wrapperFactory != null) tooltip.wrapper = wrapperFactory.apply(pa);
try {
switch (position) {
case HEAD -> provider.appendHead(tooltip, accessor, PluginConfig.CLIENT);
Expand All @@ -78,7 +82,7 @@ private static void handleBlock(ClientAccessor accessor, Tooltip tooltip, Object
} catch (Throwable e) {
ExceptionUtil.dump(e, provider.getClass().toString(), tooltip);
}
accessor.setOrigin(null, null);
if (wrapperFactory != null) tooltip.wrapper = null;
}
}

Expand Down Expand Up @@ -113,7 +117,7 @@ public static void gatherEntity(Entity entity, ClientAccessor accessor, Tooltip
for (var entry : providers) {
var pa = entry.instance();
var provider = pa.instance();
accessor.setOrigin(pa.origin(), provider.getClass());
if (wrapperFactory != null) tooltip.wrapper = wrapperFactory.apply(pa);
try {
switch (position) {
case HEAD -> provider.appendHead(tooltip, accessor, PluginConfig.CLIENT);
Expand All @@ -123,7 +127,7 @@ public static void gatherEntity(Entity entity, ClientAccessor accessor, Tooltip
} catch (Throwable e) {
ExceptionUtil.dump(e, provider.getClass().toString(), tooltip);
}
accessor.setOrigin(null, null);
if (wrapperFactory != null) tooltip.wrapper = null;
}
}

Expand All @@ -138,6 +142,7 @@ public static ITooltipComponent getIcon(HitResult target) {
var pa = provider.instance();
var icon = pa.instance().getIcon(data, config);
if (icon != null) {
if (wrapperFactory != null) icon = wrapperFactory.apply(pa).wrap(null, icon);
return icon;
}
}
Expand All @@ -152,6 +157,7 @@ public static ITooltipComponent getIcon(HitResult target) {
var pa = provider.instance();
var icon = pa.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
if (icon != null) {
if (wrapperFactory != null) icon = wrapperFactory.apply(pa).wrap(null, icon);
result = icon;
priority = provider.priority();
break;
Expand All @@ -166,6 +172,7 @@ public static ITooltipComponent getIcon(HitResult target) {
var pa = provider.instance();
var icon = pa.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
if (icon != null) {
if (wrapperFactory != null) icon = wrapperFactory.apply(pa).wrap(null, icon);
result = icon;
break;
}
Expand Down
40 changes: 32 additions & 8 deletions src/main/java/mcp/mobius/waila/gui/hud/ComponentRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,40 @@
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.Nullable;

public interface ComponentRenderer {
public abstract class ComponentRenderer {

Random RANDOM = new Random();
private static final Random RANDOM = new Random();

void render(GuiGraphics ctx, ITooltipComponent component, int x, int y, int cw, int ch, DeltaTracker delta);
public abstract 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);
private static @Nullable ComponentRenderer current = null;

if (WailaClient.showComponentBounds) {
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);
Expand All @@ -31,12 +54,13 @@ public interface ComponentRenderer {
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);
var color = (0xFF << 24) + Mth.hsvToRgb(RANDOM.nextFloat(), RANDOM.nextFloat(), v);
DisplayUtil.renderRectBorder(ctx.pose().last().pose(), buf, bx, by, bw, bh, 1, color, color);

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

}

}
60 changes: 60 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,60 @@
package mcp.mobius.waila.gui.hud;

import mcp.mobius.waila.api.IPluginInfo;
import mcp.mobius.waila.api.ITooltipComponent;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.resources.ResourceLocation;

@SuppressWarnings("deprecation")
public class InspectComponent implements ITooltipComponent {

public final ITooltipComponent actual;
public final IPluginInfo plugin;
public final Class<?> provider;
public final ResourceLocation tag;

public InspectComponent(ITooltipComponent actual, IPluginInfo plugin, Class<?> provider, ResourceLocation tag) {
this.actual = actual;
this.plugin = plugin;
this.provider = provider;
this.tag = 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, IPluginInfo plugin, Class<?> provider, ResourceLocation tag) {
super(actual, plugin, provider, tag);
this.actual = actual;
}

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

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

}

}
12 changes: 10 additions & 2 deletions src/main/java/mcp/mobius/waila/gui/hud/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,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 Wrapper wrapper = (t, c) -> c;

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

@Override
public Line with(ITooltipComponent component) {
component = wrapper.wrap(tag, component);
components.add(component);
if (component instanceof HorizontalGrowing growing) {
growingWeight += growing.getWeight();
Expand Down Expand Up @@ -154,4 +156,10 @@ public void render(ComponentRenderer renderer, GuiGraphics ctx, int x, int y, De
}
}

public interface Wrapper {

ITooltipComponent wrap(ResourceLocation tag, ITooltipComponent component);

}

}
8 changes: 7 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 @@ -6,10 +6,12 @@
import mcp.mobius.waila.api.ITooltip;
import mcp.mobius.waila.api.ITooltipLine;
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 Line.Wrapper wrapper;

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

@Override
public ITooltipLine getLine(int index) {
return get(index);
var line = get(index);
if (wrapper != null) line.wrapper = wrapper;
return line;
}

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

@Override
public ITooltipLine setLine(ResourceLocation tag) {
var line = new Line(tag);
if (wrapper != null) line.wrapper = wrapper;
setLine(tag, line);
return line;
}
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/mcp/mobius/waila/gui/hud/TooltipRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public static void add(Line line) {
}

for (var component : line.components) {
if (component instanceof InspectComponent wrapper) {
component = wrapper.actual;
}

if (component instanceof PairComponent pair) {
colonOffset = Math.max(pair.key.getWidth(), colonOffset);
break;
Expand All @@ -103,9 +107,10 @@ public static Rectangle endBuild() {
for (var entry : Registrar.get().eventListeners.get(Object.class)) {
var pa = entry.instance();
var listener = pa.instance();
accessor.setOrigin(pa.origin(), listener.getClass());
var wrapperFactory = ComponentHandler.wrapperFactory;
if (wrapperFactory != null) TOOLTIP.wrapper = wrapperFactory.apply(pa);
listener.onHandleTooltip(TOOLTIP, accessor, PluginConfig.CLIENT);
accessor.setOrigin(null, null);
if (wrapperFactory != null) TOOLTIP.wrapper = null;
}
}

Expand Down Expand Up @@ -186,13 +191,13 @@ public static void resetState() {
state = null;
}

public static void render(ComponentRenderer renderer, GuiGraphics ctx, DeltaTracker delta) {
public static void render(GuiGraphics ctx, DeltaTracker delta) {
try (var ignored = ProfilerUtil.profile("wthit:render")) {
_render(renderer, ctx, delta);
_render(ctx, delta);
}
}

private static void _render(ComponentRenderer renderer, GuiGraphics ctx, DeltaTracker delta) {
private static void _render(GuiGraphics ctx, DeltaTracker delta) {
var client = Minecraft.getInstance();

if (WailaClient.showFps) {
Expand All @@ -211,7 +216,7 @@ private static void _render(ComponentRenderer renderer, GuiGraphics ctx, DeltaTr
// TODO: Figure out why opacity not working properly
//noinspection ConstantValue
if (true) {
renderUncached(renderer, ctx, delta);
renderUncached(ctx, delta);
return;
}

Expand All @@ -235,7 +240,7 @@ private static void _render(ComponentRenderer renderer, GuiGraphics ctx, DeltaTr
client.getMainRenderTarget().unbindWrite();
framebuffer.clear();
framebuffer.bindWrite(true);
renderUncached(renderer, ctx, delta);
renderUncached(ctx, delta);
framebuffer.unbindWrite();
client.getMainRenderTarget().bindWrite(true);
lastFrame = now;
Expand All @@ -262,13 +267,14 @@ private static void _render(ComponentRenderer renderer, GuiGraphics ctx, DeltaTr
RenderSystem.disableBlend();
}

private static void renderUncached(ComponentRenderer renderer, GuiGraphics ctx, DeltaTracker delta) {
private static void renderUncached(GuiGraphics ctx, DeltaTracker delta) {
try (var ignored = ProfilerUtil.profile("wthit:render_uncached")) {
_renderUncached(renderer, ctx, delta);
_renderUncached(ctx, delta);
}
}

private static void _renderUncached(ComponentRenderer renderer, GuiGraphics ctx, DeltaTracker delta) {
private static void _renderUncached(GuiGraphics ctx, DeltaTracker delta) {
var renderer = ComponentRenderer.get();
var scale = state.getScale();

ctx.pose().pushPose();
Expand Down
Loading

0 comments on commit 386b04a

Please sign in to comment.