Skip to content

Commit

Permalink
Improve TOP Addons Element Tank Guage
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Jan 3, 2025
1 parent 01c637b commit 8e32df2
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public class LabsFluidNameElement implements IElement {

private final String fluidName;
private final int amount;
private final boolean showLang;
private final String translatedName;

public LabsFluidNameElement(FluidStack fluid) {
public LabsFluidNameElement(FluidStack fluid, boolean showLang) {
this.fluidName = fluid.getFluid().getName();
this.amount = fluid.amount;
this.showLang = showLang;

// Temp Translated Name, for usage if needed
this.translatedName = fluid.getUnlocalizedName();
Expand All @@ -31,12 +33,13 @@ public LabsFluidNameElement(FluidStack fluid) {
public LabsFluidNameElement(ByteBuf byteBuf) {
this.fluidName = NetworkTools.readStringUTF8(byteBuf);
this.amount = byteBuf.readInt();
this.showLang = byteBuf.readBoolean();
this.translatedName = translateFluid(fluidName, amount, "LabsFluidNameElement");
}

@Override
public int getWidth() {
return ElementTextRender.getWidth(translatedName);
return ElementTextRender.getWidth(getTranslated());
}

@Override
Expand All @@ -48,19 +51,26 @@ public int getHeight() {
public void toBytes(ByteBuf byteBuf) {
NetworkTools.writeStringUTF8(byteBuf, fluidName);
byteBuf.writeInt(amount);
byteBuf.writeBoolean(showLang);
}

@Override
public void render(int x, int y) {
ElementTextRender.render(
TextStyleClass.NAME + LabsTranslate.translate("nomilabs.gui.top_override.fluid", translatedName), x, y);
ElementTextRender.render(TextStyleClass.NAME + getTranslated(), x, y);
}

@Override
public int getID() {
return LabsTOPManager.FLUID_NAME_ELEMENT;
}

public String getTranslated() {
if (showLang)
return LabsTranslate.translate("nomilabs.gui.top_override.fluid", translatedName);
else
return translatedName;
}

public static String translateFluid(@Nullable String fluidName, int amount, String packet) {
if (fluidName == null || fluidName.isEmpty()) return ""; // Empty Tank

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class LabsTOPManager {

public static int FLUID_NAME_ELEMENT;
public static int CUSTOM_NAME_ELEMENT;
public static int FLUID_STACK_ELEMENT;

public static void register() {
ITheOneProbe TOP = TheOneProbe.theOneProbeImp;
Expand All @@ -31,6 +32,7 @@ public static void register() {

FLUID_NAME_ELEMENT = TOP.registerElementFactory(LabsFluidNameElement::new);
CUSTOM_NAME_ELEMENT = TOP.registerElementFactory(CustomNameElement::new);
FLUID_STACK_ELEMENT = TOP.registerElementFactory(LabsFluidStackElement::new);
}

public static class TOPTooltipMessage implements IProbeInfoProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private void showTranslatedFluidInfo(IProbeInfo probeInfo, ProbeConfig config, F
int maxContents, CallbackInfo ci) {
int contents = fluidStack == null ? 0 : fluidStack.amount;
if (fluidStack != null) {
probeInfo.element(new LabsFluidNameElement(fluidStack));
probeInfo.element(new LabsFluidNameElement(fluidStack, true));
}
if (config.getTankMode() == 1) {
probeInfo.progress(contents, maxContents,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import static io.github.drmanganese.topaddons.elements.ElementRenderHelper.drawSmallText;

import java.util.Objects;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;

import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -13,10 +14,12 @@

import com.nomiceu.nomilabs.integration.top.LabsFluidNameElement;

import gregtech.api.util.TextFormattingUtil;
import io.github.drmanganese.topaddons.elements.ElementTankGauge;
import io.github.drmanganese.topaddons.styles.ProgressStyleTank;
import mcjty.theoneprobe.api.IProgressStyle;
import mcjty.theoneprobe.apiimpl.client.ElementProgressRender;
import mcjty.theoneprobe.apiimpl.client.ElementTextRender;
import mcjty.theoneprobe.apiimpl.styles.ProgressStyle;
import mcjty.theoneprobe.rendering.RenderHelper;

/**
Expand Down Expand Up @@ -47,60 +50,88 @@ public class ElementTankGaugeMixin {
private String suffix;

@Shadow
@Mutable
@Final
private int color1;

@Shadow
@Final
private boolean sneaking;

@Shadow
@Final
private int color2;

@Unique
private String labs$tankFluidTitle = null;

@Unique
private String labs$capacityInfo = null;

@Inject(method = "render", at = @At(value = "HEAD"), cancellable = true)
private void newRenderLogic(int x, int y, CallbackInfo ci) {
boolean shouldRender = sneaking && capacity > 0;
boolean hasFluid = capacity > 0 && amount > 0;
boolean expand = sneaking && hasFluid;

var tankHeight = shouldRender ? 12 : 8;
int barHeight = expand ? 12 : 8;
IProgressStyle style = new ProgressStyle()
.borderColor(hasFluid ? color2 : 0xff969696)
.filledColor(color1)
.alternateFilledColor(color1)
.backgroundColor(0x44969696)
.showText(false);

if (capacity > 0) {
ElementProgressRender.render(new ProgressStyleTank().filledColor(color1).alternateFilledColor(color1),
amount, capacity, x, y, 100, tankHeight);
} else {
ElementProgressRender.render(new ProgressStyleTank(), amount, capacity, x, y, 100, tankHeight);
}
ElementProgressRender.render(style, amount, capacity, x, y, 100, barHeight);

for (int i = 1; i < 10; i++) {
RenderHelper.drawVerticalLine(x + i * 10, y + 1, y + (i == 5 ? tankHeight - 1 : tankHeight / 2),
0xff767676);
RenderHelper.drawVerticalLine(x + i * 10, y + 1, y + (i == 5 ? barHeight - 1 : barHeight / 2),
hasFluid ? color2 : 0xff767676);
}

if (shouldRender) {
ElementTextRender.render(amount + "/" + capacity + " " + suffix, x + 3, y + 2);

var tank = tankName + ": ";
var translatedName = LabsFluidNameElement.translateFluid(fluidName, amount, "ElementTankGauge");
var mc = Minecraft.getMinecraft();
labs$drawMediumText(x + 1 + mc.fontRenderer.getStringWidth(tank) * 3 / 4, y + 14, translatedName);
labs$drawMediumText(x + 1, y + 14, tank);
if (expand) {
ElementTextRender.render(labs$getCapacityInfo(), x + 3, y + 2);
ElementTextRender.render(labs$getTankFluidTitle(), x + 1, y + 14);
} else {
drawSmallText(x + 2, y + 2, tankName, 0xffffffff);
}

ci.cancel();
}

@Inject(method = "getHeight", at = @At("RETURN"), cancellable = true)
@Inject(method = "getWidth", at = @At("HEAD"), cancellable = true)
private void newWidthLogic(CallbackInfoReturnable<Integer> cir) {
if (sneaking && capacity > 0 && amount > 0)
cir.setReturnValue(
Math.max(100, Minecraft.getMinecraft().fontRenderer.getStringWidth(labs$getTankFluidTitle())));
}

@Inject(method = "getHeight", at = @At("HEAD"), cancellable = true)
private void newHeightLogic(CallbackInfoReturnable<Integer> cir) {
if (sneaking && !fluidName.isEmpty())
cir.setReturnValue(23);
cir.setReturnValue(25);
else
cir.setReturnValue(8);
}

@Unique
private static void labs$drawMediumText(int x, int y, String text) {
Minecraft mc = Minecraft.getMinecraft();
GlStateManager.pushMatrix();
GlStateManager.scale(0.75f, 0.75f, 1);
mc.fontRenderer.drawStringWithShadow(text, x * 4f / 3f, y * 4f / 3f, 0xffffffff);
GlStateManager.popMatrix();
private String labs$getTankFluidTitle() {
if (labs$tankFluidTitle != null) return labs$tankFluidTitle;

labs$tankFluidTitle = tankName + ": " +
LabsFluidNameElement.translateFluid(fluidName, amount, "ElementTankGauge");
return labs$tankFluidTitle;
}

@Unique
private String labs$getCapacityInfo() {
if (labs$capacityInfo != null) return labs$capacityInfo;

String suffixToUse = suffix;
if (Objects.equals(suffix, "mB"))
suffixToUse = "L";

labs$capacityInfo = TextFormattingUtil.formatLongToCompactString(amount) + suffixToUse + " / " +
TextFormattingUtil.formatLongToCompactString(capacity) + suffixToUse;
return labs$capacityInfo;
}
}

0 comments on commit 8e32df2

Please sign in to comment.