Skip to content

Commit

Permalink
use the buffer from GuiGraphics, fix compatibility with Immediately…
Browse files Browse the repository at this point in the history
…Fast

closes RaphiMC/ImmediatelyFast#304
might close #311
  • Loading branch information
deirn committed Feb 8, 2025
1 parent 44dc470 commit d3cd9cc
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import mcp.mobius.waila.api.IWailaConfig;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -79,4 +80,6 @@ public interface IApiService {

boolean isDevEnv();

MultiBufferSource getBufferSource(GuiGraphics ctx);

}
4 changes: 2 additions & 2 deletions src/api/java/mcp/mobius/waila/api/component/BarComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static void renderBar(
var g = ARGB.green(tint);
var b = ARGB.blue(tint);

var buffer = WRenders.buffer(RenderType.guiTextured(WailaConstants.COMPONENT_TEXTURE));
var buffer = WRenders.buffer(ctx, RenderType.guiTextured(WailaConstants.COMPONENT_TEXTURE));
var pose = ps.last().pose();

buffer.addVertex(pose, x, y + HEIGHT, 0).setUv(U0, v1).setColor(r, g, b, a);
Expand All @@ -108,7 +108,7 @@ static void renderBar(
}

static void renderText(GuiGraphics ctx, Component text, int x, int y) {
var bufferSource = WRenders.bufferSource();
var bufferSource = WRenders.bufferSource(ctx);
var font = Minecraft.getInstance().font;
var textWidth = font.width(text);
var textX = x + Math.max((BarComponent.WIDTH - textWidth) / 2F, 0F);
Expand Down
49 changes: 25 additions & 24 deletions src/api/java/mcp/mobius/waila/api/component/ItemComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import mcp.mobius.waila.api.ITooltipComponent;
import mcp.mobius.waila.api.__internal__.ApiSide;
import mcp.mobius.waila.api.util.WNumbers;
import mcp.mobius.waila.api.util.WRenders;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
Expand Down Expand Up @@ -45,30 +46,30 @@ public void render(GuiGraphics ctx, int x, int y, DeltaTracker delta) {
}

static void renderItemDecorations(GuiGraphics ctx, ItemStack stack, int x, int y) {
ctx.drawSpecial(bufferSource -> {
var client = Minecraft.getInstance();
var count = stack.getCount();

ctx.renderItemDecorations(client.font, stack, x + 1, y + 1, "");
if (count <= 1) return;

var countText = WNumbers.suffix(count);
var actualW = client.font.width(countText);
var scale = (actualW <= 16) ? 1f : 16f / actualW;

var pose = ctx.pose();
pose.pushPose();
pose.translate(0.0, 0.0, 250.0);
pose.scale(scale, scale, 1f);

client.font.drawInBatch(countText,
((x + 17 - (actualW * scale)) / scale),
((y + 17 - (client.font.lineHeight * scale)) / scale),
0xFFFFFF, true,
pose.last().pose(), bufferSource, Font.DisplayMode.NORMAL, 0, 0xF000F0);

pose.popPose();
});
var buffer = WRenders.bufferSource(ctx);
var client = Minecraft.getInstance();
var count = stack.getCount();

ctx.renderItemDecorations(client.font, stack, x + 1, y + 1, "");
if (count <= 1) return;

var countText = WNumbers.suffix(count);
var actualW = client.font.width(countText);
var scale = (actualW <= 16) ? 1f : 16f / actualW;

var pose = ctx.pose();
pose.pushPose();
pose.translate(0.0, 0.0, 250.0);
pose.scale(scale, scale, 1f);

client.font.drawInBatch(countText,
((x + 17 - (actualW * scale)) / scale),
((y + 17 - (client.font.lineHeight * scale)) / scale),
0xFFFFFF, true,
pose.last().pose(), buffer, Font.DisplayMode.NORMAL, 0, 0xF000F0);

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void render(GuiGraphics ctx, int x, int y, DeltaTracker delta) {
for (var py1 = y; py1 < my; py1 += regionHeight) {
var py2 = py1 + regionHeight;

if (buffer == null) buffer = WRenders.buffer(RenderType.guiTextured(texture));
if (buffer == null) buffer = WRenders.buffer(ctx, RenderType.guiTextured(texture));
buffer.addVertex(pose, px1, py2, 0).setUv(u0, v1).setColor(r, g, b, a);
buffer.addVertex(pose, px2, py2, 0).setUv(u1, v1).setColor(r, g, b, a);
buffer.addVertex(pose, px2, py1, 0).setUv(u1, v0).setColor(r, g, b, a);
Expand Down
11 changes: 6 additions & 5 deletions src/api/java/mcp/mobius/waila/api/util/WRenders.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

import com.mojang.blaze3d.vertex.VertexConsumer;
import mcp.mobius.waila.api.__internal__.ApiSide;
import net.minecraft.client.Minecraft;
import mcp.mobius.waila.api.__internal__.IApiService;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;

@ApiSide.ClientOnly
public final class WRenders {

public static MultiBufferSource bufferSource() {
return Minecraft.getInstance().renderBuffers().bufferSource();
public static MultiBufferSource bufferSource(GuiGraphics ctx) {
return IApiService.INSTANCE.getBufferSource(ctx);
}

public static VertexConsumer buffer(RenderType type) {
return bufferSource().getBuffer(type);
public static VertexConsumer buffer(GuiGraphics ctx, RenderType type) {
return bufferSource(ctx).getBuffer(type);
}

private WRenders() {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/mcp/mobius/waila/service/ApiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import mcp.mobius.waila.config.JsonConfig;
import mcp.mobius.waila.gui.hud.TooltipRenderer;
import mcp.mobius.waila.gui.hud.theme.ThemeType;
import mcp.mobius.waila.mixin.GuiGraphicsAccess;
import mcp.mobius.waila.plugin.PluginInfo;
import mcp.mobius.waila.registry.InstanceRegistry;
import mcp.mobius.waila.registry.RegistryFilter;
Expand All @@ -31,6 +32,7 @@
import mcp.mobius.waila.util.ModInfo;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.core.HolderSet;
import net.minecraft.core.Registry;
import net.minecraft.core.component.DataComponents;
Expand Down Expand Up @@ -230,4 +232,9 @@ public boolean isDevEnv() {
return Waila.DEV;
}

@Override
public MultiBufferSource getBufferSource(GuiGraphics ctx) {
return ((GuiGraphicsAccess) ctx).wthit_bufferSource();
}

}
2 changes: 1 addition & 1 deletion src/main/java/mcp/mobius/waila/util/DisplayUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void renderComponent(GuiGraphics ctx, ITooltipComponent component,
var scale = (float) Minecraft.getInstance().getWindow().getGuiScale();
ctx.pose().scale(1 / scale, 1 / scale, 1);

var buf = WRenders.buffer(RenderType.gui());
var buf = WRenders.buffer(ctx, 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);
Expand Down
14 changes: 14 additions & 0 deletions src/mixin/java/mcp/mobius/waila/mixin/GuiGraphicsAccess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mcp.mobius.waila.mixin;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.MultiBufferSource;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(GuiGraphics.class)
public interface GuiGraphicsAccess {

@Accessor("bufferSource")
MultiBufferSource.BufferSource wthit_bufferSource();

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void renderTooltipBackground(GuiGraphics ctx, int x, int y, int width, in
RenderSystem.defaultBlendFunc();
RenderSystem.setShader(CoreShaders.POSITION_COLOR);

var buf = WRenders.buffer(RenderType.gui());
var buf = WRenders.buffer(ctx, RenderType.gui());
var matrix = ctx.pose().last().pose();

var a = alpha << 24;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void renderTooltipBackground(GuiGraphics ctx, int x, int y, int width, in
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();

var buf = WRenders.buffer(RenderType.guiTextured(textureId));
var buf = WRenders.buffer(ctx, RenderType.guiTextured(textureId));
var matrix = ctx.pose().last().pose();

// @formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import mcp.mobius.waila.api.ICommonAccessor;
import mcp.mobius.waila.api.IEventListener;
import mcp.mobius.waila.api.IPluginConfig;
import mcp.mobius.waila.api.util.WRenders;
import mcp.mobius.waila.mixin.MultiPlayerGameModeAccess;
import mcp.mobius.waila.plugin.vanilla.config.Options;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -47,45 +48,47 @@ public void onAfterTooltipRender(GuiGraphics ctx, Rectangle rect, ICommonAccesso
var progressChangeAmount = progressDiff * dt;
var actualProgress = Mth.clamp(lastProgress + progressChangeAmount, 0f, 1f);

final var lineLength = new float[1];
var lineLength = 0f;

if (config.getBoolean(Options.BREAKING_PROGRESS_BOTTOM_ONLY)) {
lineLength[0] = (rect.width - 2) * actualProgress;
lineLength = (rect.width - 2) * actualProgress;
} else {
lineLength[0] = ((rect.width + rect.height - 4) * 2) * actualProgress;
lineLength = ((rect.width + rect.height - 4) * 2) * actualProgress;
}

if (lineLength[0] > 0) ctx.drawSpecial(bufferSource -> {
if (lineLength > 0) {
var bufferSource = WRenders.bufferSource(ctx);
var hLength = rect.width - 2;
var vLength = rect.height - 4;

var x = rect.x + 1;
var y = rect.y + rect.height - 2;

var color = config.getInt(Options.BREAKING_PROGRESS_COLOR);
fill(ctx, bufferSource, x, y, x + Math.min(lineLength[0], hLength), y + 1, color);
lineLength[0] -= hLength;
fill(ctx, bufferSource, x, y, x + Math.min(lineLength, hLength), y + 1, color);
lineLength -= hLength;

if (lineLength[0] > 0) {
if (lineLength > 0) {
x = rect.x + rect.width - 2;
y = rect.y + rect.height - 2;
fill(ctx, bufferSource, x, y, x + 1, y - Math.min(lineLength[0], vLength), color);
lineLength[0] -= vLength;
fill(ctx, bufferSource, x, y, x + 1, y - Math.min(lineLength, vLength), color);
lineLength -= vLength;

if (lineLength[0] > 0) {
if (lineLength > 0) {
x = rect.x + rect.width - 1;
y = rect.y + 1;
fill(ctx, bufferSource, x, y, x - Math.min(lineLength[0], hLength), y + 1, color);
lineLength[0] -= hLength;
fill(ctx, bufferSource, x, y, x - Math.min(lineLength, hLength), y + 1, color);
lineLength -= hLength;

if (lineLength[0] > 0) {
if (lineLength > 0) {
x = rect.x + 1;
y = rect.y + 2;
fill(ctx, bufferSource, x, y, x + 1, y + Math.min(lineLength[0], vLength), color);
fill(ctx, bufferSource, x, y, x + 1, y + Math.min(lineLength, vLength), color);
}
}
}
});
ctx.flush();
}

wasBreaking = isBreaking;
lastProgress = actualProgress;
Expand Down
1 change: 1 addition & 0 deletions src/resources/resources/wthit.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"ClientPacketListenerMixin",
"EditBoxAccess",
"GameNarratorAccess",
"GuiGraphicsAccess",
"GuiMixin",
"KeyMappingAccess",
"MultiPlayerGameModeAccess",
Expand Down

0 comments on commit d3cd9cc

Please sign in to comment.