Skip to content

Commit

Permalink
suffix item count, scale it down if exceed 16 px
Browse files Browse the repository at this point in the history
closes #238
  • Loading branch information
deirn committed Nov 18, 2023
1 parent af590bd commit 298e267
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
29 changes: 28 additions & 1 deletion src/api/java/mcp/mobius/waila/api/component/ItemComponent.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package mcp.mobius.waila.api.component;

import mcp.mobius.waila.api.ITooltipComponent;
import mcp.mobius.waila.api.WailaHelper;
import mcp.mobius.waila.api.__internal__.ApiSide;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
Expand Down Expand Up @@ -38,7 +40,32 @@ public int getHeight() {
@Override
public void render(GuiGraphics ctx, int x, int y, float delta) {
ctx.renderItem(stack, x + 1, y + 1);
ctx.renderItemDecorations(Minecraft.getInstance().font, stack, x + 1, y + 1);
renderItemDecorations(ctx, stack, x + 1, y + 1);
}

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

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

var countText = WailaHelper.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(), ctx.bufferSource(), Font.DisplayMode.NORMAL, 0, 0xF000F0, client.font.isBidirectional());

pose.popPose();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.List;

import mcp.mobius.waila.api.ITooltipComponent;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.util.Mth;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -53,7 +52,7 @@ public void render(GuiGraphics ctx, int x, int y, float delta) {
var ix = x + (18 * (i % gridWidth)) + 1;
var iy = y + (18 * (i / gridWidth)) + 1;
ctx.renderItem(item, ix, iy);
ctx.renderItemDecorations(Minecraft.getInstance().font, item, ix, iy);
ItemComponent.renderItemDecorations(ctx, item, ix, iy);

if (i == maxIndex) break;
}
Expand Down

0 comments on commit 298e267

Please sign in to comment.