Skip to content

Commit

Permalink
start of tank components
Browse files Browse the repository at this point in the history
  • Loading branch information
YoungOnionMC committed Dec 9, 2024
1 parent 7474028 commit 28ccaba
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 24 deletions.
148 changes: 127 additions & 21 deletions src/main/java/com/gregtechceu/gtceu/api/ui/component/TankComponent.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,162 @@
package com.gregtechceu.gtceu.api.ui.component;

import com.gregtechceu.gtceu.api.data.chemical.ChemicalHelper;
import com.gregtechceu.gtceu.api.ui.base.BaseUIComponent;
import com.gregtechceu.gtceu.api.ui.core.Color;
import com.gregtechceu.gtceu.api.ui.core.PositionedRectangle;
import com.gregtechceu.gtceu.api.ui.core.Sizing;
import com.gregtechceu.gtceu.api.ui.core.UIGuiGraphics;
import com.gregtechceu.gtceu.api.ui.parsing.UIParsing;
import com.gregtechceu.gtceu.api.ui.util.pond.UISlotExtension;
import com.gregtechceu.gtceu.client.TooltipsHandler;
import com.gregtechceu.gtceu.client.ui.screens.SyncedProperty;
import com.gregtechceu.gtceu.core.mixins.ui.accessor.SlotAccessor;

import com.gregtechceu.gtceu.utils.FormattingUtil;
import com.lowdragmc.lowdraglib.gui.util.TextFormattingUtil;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.templates.EmptyFluidHandler;

import lombok.Getter;
import lombok.Setter;
import net.minecraftforge.fluids.capability.templates.FluidTank;
import org.lwjgl.opengl.GL11;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class TankComponent extends BaseUIComponent {

@Getter
protected final IFluidHandler handler;
protected IFluidHandler handler;
@Getter
@Setter
protected String handlerName;
protected final int tank;
protected int tank;
protected FluidStack lastFluidInTank;
protected int lastTankCapacity;
protected boolean showAmount = true;

protected TankComponent(IFluidHandler fluidHandler, int tank) {
this.handler = fluidHandler;
this.tank = tank;
}

public TankComponent setFluidTank(IFluidHandler handler) {
this.handler = handler;
this.tank = 0;
return this;
}

public TankComponent setFluidTank(IFluidHandler handler, int tank) {
this.handler = handler;
this.tank = tank;
return this;
}

public TankComponent showAmount(boolean show) {
showAmount = show;
return this;
}

public List<Component> getTooltips() {
List<Component> tooltips = new ArrayList<>();
var stack = lastFluidInTank;
if(stack != null && !stack.isEmpty()) {
tooltips.add(stack.getDisplayName());
if (true && showAmount) { // todo phantom stacks
tooltips.add(
Component.translatable("ldlib.fluid.amount", stack.getAmount(), lastTankCapacity)
.append(" mB"));
}
if (ChemicalHelper.getMaterial(stack.getFluid()) != null) {
TooltipsHandler.appendFluidTooltips(stack.getFluid(), stack.getAmount(), tooltips::add, null);
} else {
tooltips.add(Component.translatable("ldlib.fluid.temperature",
stack.getFluid().getFluidType().getTemperature(stack)));
tooltips.add(Component.translatable(stack.getFluid().getFluidType().isLighterThanAir() ?
"ldlib.fluid.state_gas" : "ldlib.fluid.state_liquid"));
}
} else {
tooltips.add(Component.translatable("ldlib.fluid.empty"));
if (true && showAmount) { // todo phantom stack
tooltips.add(Component.translatable("ldlib.fluid.amount", 0, lastTankCapacity).append(" mB"));
}
}

return tooltips;
}

@Override
public void draw(UIGuiGraphics context, int mouseX, int mouseY, float partialTicks, float delta) {
int[] scissor = new int[4];
GL11.glGetIntegerv(GL11.GL_SCISSOR_BOX, scissor);
public void draw(UIGuiGraphics graphics, int mouseX, int mouseY, float partialTicks, float delta) {
if(handler != null) {
FluidStack stack = handler.getFluidInTank(tank);
int capacity = handler.getTankCapacity(tank);
if(capacity != lastTankCapacity) {
lastTankCapacity = capacity;
}
if(lastFluidInTank == null) {
lastFluidInTank = stack;
}
if(!stack.isFluidEqual(lastFluidInTank)) {
lastFluidInTank = stack;
} else if( stack.getAmount() != lastFluidInTank.getAmount()) {
lastFluidInTank.setAmount(stack.getAmount());
}
}

if(lastFluidInTank != null) {
RenderSystem.disableBlend();
if(!lastFluidInTank.isEmpty()) {
double progress = lastFluidInTank.getAmount() * 1.0 /
Math.max(Math.max(lastFluidInTank.getAmount(), lastTankCapacity), 1);

int width = width();
int height = height();
int x = x();
int y = y();
graphics.drawFluid(lastFluidInTank, lastTankCapacity, x, y, width, height);
}

if(!lastFluidInTank.isEmpty()) {
graphics.pose().pushPose();
graphics.pose().scale(0.5f, 0.5f, 1.0f);
String s = FormattingUtil.formatBuckets(lastFluidInTank.getAmount());
Font f = Minecraft.getInstance().font;
graphics.drawString(f, s,
(int) ((x + width / 3.f)) * 2 - f.width(s) + 21,
(int) ((y + (height / 3.0f) + 6) * 2), Color.WHITE.argb(), true);
graphics.pose().popPose();
}
RenderSystem.enableBlend();
RenderSystem.setShaderColor(1.f, 1.f, 1.f, 1.f);
}

((UISlotExtension) this.handler).gtceu$setScissorArea(PositionedRectangle.of(
scissor[0], scissor[1], scissor[2], scissor[3]));
if(hovered) {
RenderSystem.colorMask(true, true, true, false);
graphics.drawSolidRect(x, y, width, height, Color.HOVER_GRAY.argb());
RenderSystem.colorMask(true, true, true, true);
}

if(hovered) {
RenderSystem.disableScissor();
RenderSystem.disableDepthTest();
graphics.pose().pushPose();
graphics.pose().translate(0f,0f, 200f);
graphics.renderTooltip(Minecraft.getInstance().font, getTooltips(), Optional.empty(), ItemStack.EMPTY, mouseX, mouseY);
RenderSystem.setShaderColor(1,1,1,1);
graphics.pose().popPose();
graphics.renderTooltip(Minecraft.getInstance().font, getTooltips(), Optional.empty(), ItemStack.EMPTY, mouseX, mouseY);
}
}

public static TankComponent parse(Element element) {
Expand Down Expand Up @@ -71,18 +191,4 @@ protected int determineHorizontalContentSize(Sizing sizing) {
protected int determineVerticalContentSize(Sizing sizing) {
return 16;
}

@Override
public TankComponent x(int x) {
super.x(x);
((SlotAccessor) this.handler).gtceu$setX(x);
return this;
}

@Override
public TankComponent y(int y) {
super.y(y);
((SlotAccessor) this.handler).gtceu$setY(y);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public static FluidComponent fluid(FluidStack fluid) {
return new FluidComponent(fluid);
}

public static TankComponent tank(IFluidHandler fluidHandler) {
return new TankComponent(fluidHandler, 0);
}

public static TankComponent tank(IFluidHandler fluidHandler, int tank) {
return new TankComponent(fluidHandler, tank);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/gregtechceu/gtceu/api/ui/core/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public record Color(float red, float green, float blue, float alpha) implements
public static final Color BLACK = Color.ofRgb(0);
public static final Color WHITE = Color.ofRgb(0xFFFFFF);
public static final Color RED = Color.ofRgb(0xFF0000);
public static final Color YELLOW = Color.ofRgb(0xFFFF00);
public static final Color GREEN = Color.ofRgb(0x00FF00);
public static final Color BLUE = Color.ofRgb(0x0000FF);
public static final Color HOVER_GRAY = Color.ofArgb(0x80FFFFFF);

private static final Map<String, Color> NAMED_TEXT_COLORS = Stream.of(ChatFormatting.values())
.filter(ChatFormatting::isColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ public void drawLine(int x1, int y1, int x2, int y2, double thiccness, Color col
Tesselator.getInstance().end();
}

public void drawSolidRect(int x, int y, int width, int height, int color) {
this.fill(x, y, x + width, y + height, color);
RenderSystem.enableBlend();
}

public void drawCircle(int centerX, int centerY, int segments, double radius, Color color) {
drawCircle(centerX, centerY, 0, 360, segments, radius, color);
}
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/gregtechceu/gtceu/common/data/GTItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.gregtechceu.gtceu.api.item.tool.GTToolType;
import com.gregtechceu.gtceu.api.item.tool.MaterialToolTier;
import com.gregtechceu.gtceu.api.registry.registrate.GTRegistrate;
import com.gregtechceu.gtceu.api.transfer.fluid.CustomFluidTank;
import com.gregtechceu.gtceu.api.transfer.item.CustomItemStackHandler;
import com.gregtechceu.gtceu.api.ui.component.BoxComponent;
import com.gregtechceu.gtceu.api.ui.component.ButtonComponent;
Expand Down Expand Up @@ -1805,17 +1806,18 @@ public void loadUITemplate(Player player, RootContainer rootComponent, HeldItemU
rootComponent.surface(Surface.VANILLA_TRANSLUCENT);

FlowLayout layout;
CustomFluidTank tank = new CustomFluidTank(new FluidStack(Fluids.LAVA, 8000));

rootComponent.child(layout = UIContainers.horizontalFlow(Sizing.fixed(176), Sizing.fixed(166))
.child(UIComponents.ninePatchTexture(GTCEu.id("background"))
.visibleArea(PositionedRectangle.of(0, 0, 176, 166))
.sizing(Sizing.fixed(172), Sizing.fixed(166)))
.child(UIComponents.box(Sizing.fixed(130), Sizing.fixed(100))
/*.child(UIComponents.box(Sizing.fixed(130), Sizing.fixed(100))
.startColor(Color.BLACK)
.endColor(Color.GREEN)
.direction(BoxComponent.GradientDirection.LEFT_TO_RIGHT)
.positioning(Positioning.relative(50, 20))
.cursorStyle(CursorStyle.HAND))
.cursorStyle(CursorStyle.HAND))*/
.child(UIComponents.item(new ItemStack(Items.ROTTEN_FLESH, 3))
.positioning(Positioning.absolute(80, 50))
.tooltip(Component.translatable("gtceu.alloy_smelter")))
Expand All @@ -1824,14 +1826,18 @@ public void loadUITemplate(Player player, RootContainer rootComponent, HeldItemU
.child(UIComponents.button(Component.literal("✔"), (ButtonComponent button) -> {
player.sendSystemMessage(Component.literal("AAAAAAAAAAAAAAAAAAAAAAAA"));
}).tooltip(Component.literal("AAAAAAAAAAAAAAAAAAAAAAAA"))
.positioning(Positioning.relative(75, 10))));
.positioning(Positioning.relative(75, 10)))
.child(UIComponents.tank(tank).positioning(Positioning.relative(5, 5)))
);

CustomItemStackHandler slot0 = new CustomItemStackHandler(new ItemStack(Items.ITEM_FRAME));
layout.child(
UIContainers.verticalFlow(Sizing.content(), Sizing.content())
.child(UIComponents.texture(GuiTextures.SLOT.imageLocation, 0, 0, 18, 18, 18, 18))
.child(UIComponents.slot(slot0, 0))
.positioning(Positioning.relative(25, 25)));


}
}))
.register();
Expand Down

0 comments on commit 28ccaba

Please sign in to comment.