Skip to content

Commit

Permalink
use correct render buffers
Browse files Browse the repository at this point in the history
categorize helper methods to `util` package
  • Loading branch information
deirn committed Jan 2, 2025
1 parent f71b937 commit 439e43f
Show file tree
Hide file tree
Showing 23 changed files with 285 additions and 184 deletions.
74 changes: 19 additions & 55 deletions src/api/java/mcp/mobius/waila/api/WailaHelper.java
Original file line number Diff line number Diff line change
@@ -1,89 +1,53 @@
package mcp.mobius.waila.api;

import java.text.DecimalFormat;

import io.netty.buffer.ByteBuf;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import mcp.mobius.waila.api.util.WCodecs;
import mcp.mobius.waila.api.util.WColors;
import mcp.mobius.waila.api.util.WNumbers;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.util.ARGB;
import org.jetbrains.annotations.Nullable;

@Deprecated
public final class WailaHelper {

@Deprecated
public static String suffix(long value) {
if (value == Long.MIN_VALUE) return suffix(Long.MIN_VALUE + 1);
if (value < 0) return "-" + suffix(-value);
if (value < 1000) return Long.toString(value);

var exponent = -1;
long divisor = 0;

for (var decimal : DECIMALS) {
if (value < decimal) break;
exponent++;
divisor = decimal;
}

var truncated = (double) value / divisor;
if (truncated >= 100) truncated = Math.round(truncated);
else if (truncated >= 10) truncated = Math.round(truncated * 10) / 10d;

return SUFFIXED_FORMAT.format(truncated) + "KMGTPE".charAt(exponent);
return WNumbers.suffix(value);
}

@Deprecated
public static int getAlpha(int argb) {
return (argb >> 24) & 0xFF;
return ARGB.alpha(argb);
}

@Deprecated
public static int getRed(int rgb) {
return (rgb >> 16) & 0xFF;
return ARGB.red(rgb);
}

@Deprecated
public static int getGreen(int rgb) {
return (rgb >> 8) & 0xFF;
return ARGB.green(rgb);
}

@Deprecated
public static int getBlue(int rgb) {
return rgb & 0xFF;
return ARGB.blue(rgb);
}

@Deprecated
public static double getLuminance(int rgb) {
return (0.299 * getRed(rgb) + 0.587 * getGreen(rgb) + 0.114 * getBlue(rgb)) / 255.0;
return WColors.luminance(rgb);
}

/**
* @see ByteBufCodecs#optional(StreamCodec)
*/
@Deprecated
public static <B extends ByteBuf, V> StreamCodec<B, @Nullable V> nullable(final StreamCodec<B, V> codec) {
return new StreamCodec<>() {
@Override
@SuppressWarnings("NullableProblems")
public @Nullable V decode(B b) {
return FriendlyByteBuf.readNullable(b, codec);
}

@Override
public void encode(B o, V v) {
FriendlyByteBuf.writeNullable(o, v, codec);
}
};
return WCodecs.nullable(codec);
}

//---------------------------------------------------------------------------------------------------

// @formatter:off
private static final long[] DECIMALS = {
1_000L, // K
1_000_000L, // M
1_000_000_000L, // G
1_000_000_000_000L, // T
1_000_000_000_000_000L, // P
1_000_000_000_000_000_000L // E
};
// @formatter:on

private static final DecimalFormat SUFFIXED_FORMAT = new DecimalFormat("0.##");

private WailaHelper() {
throw new UnsupportedOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.Collection;
import java.util.List;

import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.VertexConsumer;
import mcp.mobius.waila.api.IBlacklistConfig;
import mcp.mobius.waila.api.IData;
import mcp.mobius.waila.api.IInstanceRegistry;
Expand Down Expand Up @@ -59,9 +59,9 @@ public interface IApiService {

int getFontColor();

void fillGradient(Matrix4f matrix, BufferBuilder buf, int x, int y, int w, int h, int start, int end);
void fillGradient(Matrix4f matrix, VertexConsumer buf, int x, int y, int w, int h, int start, int end);

void renderRectBorder(Matrix4f matrix, BufferBuilder buf, int x, int y, int w, int h, int s, int gradStart, int gradEnd);
void renderRectBorder(Matrix4f matrix, VertexConsumer buf, int x, int y, int w, int h, int s, int gradStart, int gradEnd);

<T extends ITheme> IThemeType.Builder<T> createThemeTypeBuilder(Class<T> clazz);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/**
* Internal package, do not touch.
*/
@ApiStatus.Internal
package mcp.mobius.waila.api.__internal__;

import org.jetbrains.annotations.ApiStatus;
61 changes: 28 additions & 33 deletions src/api/java/mcp/mobius/waila/api/component/BarComponent.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package mcp.mobius.waila.api.component;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import mcp.mobius.waila.api.ITooltipComponent;
import mcp.mobius.waila.api.WailaConstants;
import mcp.mobius.waila.api.WailaHelper;
import mcp.mobius.waila.api.__internal__.ApiSide;
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.renderer.CoreShaders;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.util.ARGB;

/**
* Component that renders a colored bar.
Expand Down Expand Up @@ -77,50 +73,49 @@ public int getHeight() {

@Override
public void render(GuiGraphics ctx, int x, int y, DeltaTracker delta) {
renderBar(ctx.pose(), x, y, WIDTH, V0_BG, U1, V1_BG, color);
renderBar(ctx.pose(), x, y, WIDTH * ratio, V0_FG, U0 + (UV_W * ratio), V1_FG, color);
renderBar(ctx, x, y, WIDTH, V0_BG, U1, V1_BG, color);
renderBar(ctx, x, y, WIDTH * ratio, V0_FG, U0 + (UV_W * ratio), V1_FG, color);
renderText(ctx, text, x, y);
}

static void renderBar(
PoseStack matrices,
GuiGraphics ctx,
int x, int y, float w,
float v0, float u1, float v1, int tint
) {
matrices.pushPose();
var ps = ctx.pose();
ps.pushPose();

RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.setShader(CoreShaders.POSITION_TEX_COLOR);
RenderSystem.setShaderTexture(0, WailaConstants.COMPONENT_TEXTURE);

var a = WailaHelper.getAlpha(tint);
var r = WailaHelper.getRed(tint);
var g = WailaHelper.getGreen(tint);
var b = WailaHelper.getBlue(tint);
var a = ARGB.alpha(tint);
var r = ARGB.red(tint);
var g = ARGB.green(tint);
var b = ARGB.blue(tint);

var tessellator = Tesselator.getInstance();
var buffer = tessellator.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX_COLOR);
var buffer = WRenders.buffer(RenderType.guiTextured(WailaConstants.COMPONENT_TEXTURE));
var pose = ps.last().pose();

buffer.addVertex(matrices.last().pose(), x, y + HEIGHT, 0).setUv(U0, v1).setColor(r, g, b, a);
buffer.addVertex(matrices.last().pose(), x + w, y + HEIGHT, 0).setUv(u1, v1).setColor(r, g, b, a);
buffer.addVertex(matrices.last().pose(), x + w, y, 0).setUv(u1, v0).setColor(r, g, b, a);
buffer.addVertex(matrices.last().pose(), x, y, 0).setUv(U0, v0).setColor(r, g, b, a);
buffer.addVertex(pose, x, y + HEIGHT, 0).setUv(U0, v1).setColor(r, g, b, a);
buffer.addVertex(pose, x + w, y + HEIGHT, 0).setUv(u1, v1).setColor(r, g, b, a);
buffer.addVertex(pose, x + w, y, 0).setUv(u1, v0).setColor(r, g, b, a);
buffer.addVertex(pose, x, y, 0).setUv(U0, v0).setColor(r, g, b, a);

BufferUploader.drawWithShader(buffer.buildOrThrow());
RenderSystem.disableBlend();
matrices.popPose();
ps.popPose();
ctx.flush();
}

static void renderText(GuiGraphics ctx, Component text, int x, int y) {
ctx.drawSpecial(bufferSource -> {
var font = Minecraft.getInstance().font;
var textWidth = font.width(text);
var textX = x + Math.max((BarComponent.WIDTH - textWidth) / 2F, 0F);
float textY = y + 2;

font.drawInBatch8xOutline(text.getVisualOrderText(), textX, textY, 0xAAAAAA, 0x292929, ctx.pose().last().pose(), bufferSource, 0xf000f0);
});
var bufferSource = WRenders.bufferSource();
var font = Minecraft.getInstance().font;
var textWidth = font.width(text);
var textX = x + Math.max((BarComponent.WIDTH - textWidth) / 2F, 0F);
float textY = y + 2;

font.drawInBatch8xOutline(text.getVisualOrderText(), textX, textY, 0xAAAAAA, 0x292929, ctx.pose().last().pose(), bufferSource, 0xf000f0);
ctx.flush();
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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 mcp.mobius.waila.api.util.WNumbers;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
Expand Down Expand Up @@ -52,7 +52,7 @@ static void renderItemDecorations(GuiGraphics ctx, ItemStack stack, int x, int y
ctx.renderItemDecorations(client.font, stack, x + 1, y + 1, "");
if (count <= 1) return;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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 mcp.mobius.waila.api.__internal__.IApiService;
import mcp.mobius.waila.api.util.WNumbers;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
Expand All @@ -24,7 +24,7 @@ public NamedItemComponent(ItemStack stack) {

var count = stack.getCount();
var name = stack.getHoverName().getString();
this.label = count > 1 ? WailaHelper.suffix(count) + " " + name : name;
this.label = count > 1 ? WNumbers.suffix(count) + " " + name : name;
}

public NamedItemComponent(ItemLike item) {
Expand Down
44 changes: 20 additions & 24 deletions src/api/java/mcp/mobius/waila/api/component/SpriteBarComponent.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package mcp.mobius.waila.api.component;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.blaze3d.vertex.VertexConsumer;
import mcp.mobius.waila.api.ITooltipComponent;
import mcp.mobius.waila.api.WailaHelper;
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.renderer.CoreShaders;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.ARGB;

/**
* Component that renders a bar with a texture as the foreground.
Expand Down Expand Up @@ -58,45 +55,44 @@ public int getHeight() {

@Override
public void render(GuiGraphics ctx, int x, int y, DeltaTracker delta) {
var matrices = ctx.pose();
var ps = ctx.pose();

BarComponent.renderBar(matrices, x, y, BarComponent.WIDTH, BarComponent.V0_BG, BarComponent.U1, BarComponent.V1_BG, 0xFFAAAAAA);
BarComponent.renderBar(ctx, x, y, BarComponent.WIDTH, BarComponent.V0_BG, BarComponent.U1, BarComponent.V1_BG, 0xFFAAAAAA);

var mx = (int) (x + BarComponent.WIDTH * ratio);
var my = y + BarComponent.HEIGHT;
ctx.enableScissor(x + 1, y + 1, mx - 1, my - 1);

matrices.pushPose();
ps.pushPose();
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.setShader(CoreShaders.POSITION_TEX_COLOR);
RenderSystem.setShaderTexture(0, texture);

var a = WailaHelper.getAlpha(spriteTint);
var r = WailaHelper.getRed(spriteTint);
var g = WailaHelper.getGreen(spriteTint);
var b = WailaHelper.getBlue(spriteTint);
var a = ARGB.alpha(spriteTint);
var r = ARGB.red(spriteTint);
var g = ARGB.green(spriteTint);
var b = ARGB.blue(spriteTint);

BufferBuilder buffer = null;
VertexConsumer buffer = null;
var pose = ps.last().pose();

for (var px1 = x; px1 < mx; px1 += regionWidth) {
var px2 = px1 + regionWidth;

for (var py1 = y; py1 < my; py1 += regionHeight) {
var py2 = py1 + regionHeight;

if (buffer == null) buffer = Tesselator.getInstance().begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX_COLOR);
buffer.addVertex(matrices.last().pose(), px1, py2, 0).setUv(u0, v1).setColor(r, g, b, a);
buffer.addVertex(matrices.last().pose(), px2, py2, 0).setUv(u1, v1).setColor(r, g, b, a);
buffer.addVertex(matrices.last().pose(), px2, py1, 0).setUv(u1, v0).setColor(r, g, b, a);
buffer.addVertex(matrices.last().pose(), px1, py1, 0).setUv(u0, v0).setColor(r, g, b, a);
if (buffer == null) buffer = WRenders.buffer(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);
buffer.addVertex(pose, px1, py1, 0).setUv(u0, v0).setColor(r, g, b, a);
}
}

if (buffer != null) BufferUploader.drawWithShader(buffer.buildOrThrow());
RenderSystem.disableBlend();
matrices.popPose();
ps.popPose();
ctx.disableScissor();
ctx.flush();

BarComponent.renderText(ctx, text, x, y);
}
Expand Down
29 changes: 29 additions & 0 deletions src/api/java/mcp/mobius/waila/api/util/WCodecs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package mcp.mobius.waila.api.util;

import io.netty.buffer.ByteBuf;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class WCodecs {

public static <B extends ByteBuf, V> @NotNull StreamCodec<B, @Nullable V> nullable(StreamCodec<B, V> codec) {
return new StreamCodec<>() {
@Override
@SuppressWarnings("NullableProblems")
public @Nullable V decode(B b) {
return FriendlyByteBuf.readNullable(b, codec);
}

@Override
public void encode(B o, V v) {
FriendlyByteBuf.writeNullable(o, v, codec);
}
};
}

private WCodecs() {
}

}
Loading

0 comments on commit 439e43f

Please sign in to comment.