Skip to content

Commit

Permalink
Merge pull request #226 from GregTech-Intergalactical/gui-refactor
Browse files Browse the repository at this point in the history
Gui refactor
  • Loading branch information
Trinsdar authored Aug 6, 2023
2 parents 9cb0c09 + 1772432 commit 722e4c9
Show file tree
Hide file tree
Showing 36 changed files with 419 additions and 114 deletions.
1 change: 1 addition & 0 deletions common/src/main/java/muramasa/antimatter/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public String screenID() {
.noCovers();

public static void init(Side side) {
CREATIVE_GENERATOR.getGui().setBackgroundTexture("creative_generator");
if (side.isClient()){
CREATIVE_GENERATOR.addGuiCallback(t -> {
t.addButton(10, 18, 14, 14, APAD_LEFT)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package muramasa.antimatter.capability;

import muramasa.antimatter.gui.GuiData;
import muramasa.antimatter.gui.GuiInstance;
import muramasa.antimatter.gui.IGuiElement;
import muramasa.antimatter.gui.event.IGuiEvent;
Expand All @@ -16,6 +17,8 @@ default void onGuiEvent(IGuiEvent event, Player player) {
// NOOP
}

GuiData getGui();

boolean isRemote();

default void addWidgets(GuiInstance instance, IGuiElement parent) {
Expand Down
37 changes: 24 additions & 13 deletions common/src/main/java/muramasa/antimatter/gui/GuiData.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package muramasa.antimatter.gui;

import com.google.common.collect.ImmutableMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import muramasa.antimatter.Ref;
import muramasa.antimatter.gui.slot.ISlotProvider;
import muramasa.antimatter.machine.Tier;
import muramasa.antimatter.registration.IAntimatterObject;
import muramasa.antimatter.util.int2;
import muramasa.antimatter.util.int4;
import net.minecraft.resources.ResourceLocation;

import java.util.Map;

//@Environment(EnvType.CLIENT)
public class GuiData {

Expand All @@ -16,16 +21,19 @@ public class GuiData {
protected MenuHandler<?> menuHandler;
protected ImmutableMap<Tier, Tier> guiTiers;

protected Map<String, ResourceLocation> backgroundTextures = new Object2ObjectOpenHashMap<>();

protected boolean enablePlayerSlots = true;
protected int4 area = new int4(3, 3, 170, 80);
public BarDir dir = BarDir.LEFT;
public boolean barFill = true;

protected MachineWidgetData machineWidgetData = new MachineWidgetData(this);

private final int buttons = 0;
private ISlotProvider<?> slots;

public GuiData(String domain, String id) {
this.loc = new ResourceLocation(domain, id);
this.backgroundTextures.put("", new ResourceLocation(Ref.ID, "textures/gui/background/machine_basic.png"));
}

public GuiData(String domain, String id, MenuHandler menuHandler) {
Expand Down Expand Up @@ -59,11 +67,8 @@ public MenuHandler<?> getMenuHandler() {

public ResourceLocation getTexture(Tier tier, String type) {
if (override != null) return override;
if (guiTiers != null && guiTiers.get(tier) != null) {
return new ResourceLocation(loc.getNamespace(), "textures/gui/" + type + "/" + loc.getPath() + "_" + guiTiers.get(tier).getId() + ".png");
} else {
return new ResourceLocation(loc.getNamespace(), "textures/gui/" + type + "/" + loc.getPath() + ".png");
}
if (backgroundTextures.containsKey(tier.getId())) return backgroundTextures.get(tier.getId());
return backgroundTextures.get("");
}

public ResourceLocation getLoc() {
Expand All @@ -74,6 +79,10 @@ public int4 getArea() {
return area;
}

public MachineWidgetData getMachineData() {
return machineWidgetData;
}

/*public void screenCreationCallBack(AntimatterContainerScreen<? extends T> screen, IGuiHandler handler, @Nullable Object lookup) {
this.widgets.forEach(t -> screen.addWidget(t.apply(screen, handler)));
List<BiFunction<AntimatterContainerScreen<? extends T>, IGuiHandler, Widget>> wid = this.objectWidgets.get(lookup);
Expand All @@ -84,8 +93,9 @@ public boolean enablePlayerSlots() {
return enablePlayerSlots;
}

public void setEnablePlayerSlots(boolean enablePlayerSlots) {
public GuiData setEnablePlayerSlots(boolean enablePlayerSlots) {
this.enablePlayerSlots = enablePlayerSlots;
return this;
}

public GuiData setArea(int x, int y, int z, int w) {
Expand All @@ -98,11 +108,12 @@ public GuiData setOverrideLocation(ResourceLocation override) {
return this;
}

public void setDir(BarDir dir) {
this.dir = dir;
public GuiData setBackgroundTexture(String textureName){
this.backgroundTextures.put("", new ResourceLocation(loc.getNamespace(), "textures/gui/background/" + textureName + ".png"));
return this;
}

public void setBarFill(boolean barFill) {
this.barFill = barFill;
public GuiData setBackgroundTexture(Tier tier, String textureName){
this.backgroundTextures.put(tier.getId(), new ResourceLocation(loc.getNamespace(), "textures/gui/background/" + textureName + ".png"));
return this;
}
}
122 changes: 122 additions & 0 deletions common/src/main/java/muramasa/antimatter/gui/MachineWidgetData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package muramasa.antimatter.gui;

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import muramasa.antimatter.Ref;
import muramasa.antimatter.machine.Tier;
import muramasa.antimatter.util.int2;
import net.minecraft.resources.ResourceLocation;

import java.util.Map;

public class MachineWidgetData {
public BarDir dir = BarDir.LEFT;
public boolean barFill = true;
protected int2 progressSize = new int2(20, 18), progressPos = new int2(72, 18);
protected int2 ioPos = new int2(7, 62), machineStatePos = new int2(83, 43), machineStateSize = new int2(10, 11);
protected Map<String, ResourceLocation> machineStateTextures = new Object2ObjectOpenHashMap<>();
protected Map<String, ResourceLocation> progressTextures = new Object2ObjectOpenHashMap<>();

private final GuiData parent;
public MachineWidgetData(GuiData parent){
this.parent = parent;
this.machineStateTextures.put("", new ResourceLocation(Ref.ID, "textures/gui/button/machine_state.png"));
this.progressTextures.put("", new ResourceLocation(Ref.ID, "textures/gui/progress_bars/default.png"));
}

public MachineWidgetData setProgressLocation(String name){
this.progressTextures.put("", new ResourceLocation(parent.loc.getNamespace(), "textures/gui/progress_bars/" + name + ".png"));
return this;
}

public MachineWidgetData setProgressLocation(Tier tier, String name){
this.progressTextures.put(tier.getId(), new ResourceLocation(parent.loc.getNamespace(), "textures/gui/progress_bars/" + name + ".png"));
return this;
}

public MachineWidgetData setProgressSize(int width, int height){
this.progressSize = new int2(width, height);
return this;
}

public MachineWidgetData setProgressPos(int x, int y){
this.progressPos = new int2(x, y);
return this;
}

public MachineWidgetData setIoPos(int x, int y){
this.ioPos = new int2(x, y);
return this;
}

public MachineWidgetData setMachineStatePos(int x, int y){
this.machineStatePos = new int2(x, y);
return this;
}

public MachineWidgetData setMachineStateSize(int width, int height){
this.machineStateSize = new int2(width, height);
return this;
}

public MachineWidgetData setMachineStateLocation(String name){
this.progressTextures.put("", new ResourceLocation(parent.loc.getNamespace(), "textures/gui/button/" + name + ".png"));
return this;
}

public MachineWidgetData setMachineStateLocation(Tier tier, String name){
this.progressTextures.put(tier.getId(), new ResourceLocation(parent.loc.getNamespace(), "textures/gui/button/" + name + ".png"));
return this;
}

public MachineWidgetData setDir(BarDir dir) {
this.dir = dir;
return this;
}

public MachineWidgetData setBarFill(boolean barFill) {
this.barFill = barFill;
return this;
}

public int2 getProgressSize() {
return progressSize;
}

public ResourceLocation getProgressTexture(Tier tier) {
if (tier != null && progressTextures.containsKey(tier.getId())) return progressTextures.get(tier.getId());
return progressTextures.get("");
}

public int2 getProgressPos() {
return progressPos;
}

public BarDir getDir() {
return dir;
}

public GuiData getParent() {
return parent;
}

public boolean doesBarFill() {
return barFill;
}

public int2 getMachineStatePos() {
return machineStatePos;
}

public int2 getMachineStateSize() {
return machineStateSize;
}

public ResourceLocation getMachineStateTexture(Tier tier) {
if (tier != null && machineStateTextures.containsKey(tier.getId())) return machineStateTextures.get(tier.getId());
return machineStateTextures.get("");
}

public int2 getIoPos() {
return ioPos;
}
}
14 changes: 14 additions & 0 deletions common/src/main/java/muramasa/antimatter/gui/SlotData.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package muramasa.antimatter.gui;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.Slot;

public class SlotData<T extends Slot> {

private final SlotType<T> type;
private final int x;
private final int y;
private final ResourceLocation texture;
private int data = -1;

public SlotData(SlotType<T> type, int x, int y) {
this.type = type;
this.x = x;
this.y = y;
texture = new ResourceLocation(type.textureName.getNamespace(), "textures/gui/slots/" + type.textureName.getPath() + ".png");
}

public SlotData(SlotType<T> type, int x, int y, ResourceLocation texture){
this.type = type;
this.x = x;
this.y = y;
this.texture = texture;
}

public SlotData(SlotType<T> type, int x, int y, int data) {
Expand All @@ -35,4 +45,8 @@ public int getY() {
public int getData() {
return data;
}

public ResourceLocation getTexture() {
return texture;
}
}
21 changes: 14 additions & 7 deletions common/src/main/java/muramasa/antimatter/gui/SlotType.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import earth.terrarium.botarium.common.energy.util.EnergyHooks;
import earth.terrarium.botarium.common.fluid.utils.FluidHooks;
import muramasa.antimatter.AntimatterAPI;
import muramasa.antimatter.Ref;
import muramasa.antimatter.capability.IGuiHandler;
import muramasa.antimatter.capability.item.EmptyContainer;
import muramasa.antimatter.capability.machine.MachineFluidHandler;
import muramasa.antimatter.gui.slot.*;
import muramasa.antimatter.machine.event.ContentEvent;
import muramasa.antimatter.registration.IAntimatterObject;
import muramasa.antimatter.tile.TileEntityMachine;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand All @@ -22,41 +24,46 @@
public class SlotType<T extends Slot> implements IAntimatterObject {

public static SlotType<SlotInput> IT_IN = new SlotType<>("item_in", (type, gui, inv, i, d) -> new SlotInput(type, gui, inv.getOrDefault(type, new EmptyContainer()), i, d.getX(), d.getY()), new ItIn(), ContentEvent.ITEM_INPUT_CHANGED, true, false);

public static SlotType<SlotOutput> IT_OUT = new SlotType<>("item_out", (type, gui, inv, i, d) -> new SlotOutput(type, gui, inv.getOrDefault(type, new EmptyContainer()), i, d.getX(), d.getY()), (t, i) -> false, ContentEvent.ITEM_OUTPUT_CHANGED, false, true);
public static SlotType<SlotFake> DISPLAY = new SlotType<>("display", (type, gui, item, i, d) -> new SlotFake(type, gui, item.getOrDefault(type, new EmptyContainer()), i, d.getX(), d.getY(), false), (t, i) -> false, ContentEvent.ITEM_INPUT_CHANGED, false, false);
public static SlotType<SlotFake> DISPLAY_SETTABLE = new SlotType<>("display_settable", (type, gui, item, i, d) -> new SlotFake(type, gui, item.getOrDefault(type, new EmptyContainer()), i, d.getX(), d.getY(), true), (t, i) -> false, ContentEvent.ITEM_INPUT_CHANGED, true, false);
public static SlotType<AbstractSlot<?>> STORAGE = new SlotType<>("storage", (type, gui, item, i, d) -> new AbstractSlot<>(type, gui, item.getOrDefault(type, new EmptyContainer()), i, d.getX(), d.getY()), (t, i) -> true, ContentEvent.ITEM_INPUT_CHANGED);
public static SlotType<SlotFakeFluid> FL_IN = new SlotType<>("fluid_in", (type, gui, inv, i, d) -> new SlotFakeFluid(type, gui, MachineFluidHandler.FluidDirection.INPUT, i, d.getX(), d.getY()), (t, i) -> false, ContentEvent.FLUID_INPUT_CHANGED, false, false);
public static SlotType<SlotFakeFluid> FL_IN = new SlotType<>("fluid_in", (type, gui, inv, i, d) -> new SlotFakeFluid(type, gui, MachineFluidHandler.FluidDirection.INPUT, i, d.getX(), d.getY()), (t, i) -> false, ContentEvent.FLUID_INPUT_CHANGED, false, false, new ResourceLocation(Ref.ID, "fluid_in"));
//Cheat using same ID to get working counter.
public static SlotType<SlotFakeFluid> FL_OUT = new SlotType<>("fluid_out", (type, gui, inv, i, d) -> new SlotFakeFluid(type, gui, MachineFluidHandler.FluidDirection.OUTPUT, i, d.getX(), d.getY()), (t, i) -> false, ContentEvent.FLUID_OUTPUT_CHANGED, false, false);
public static SlotType<SlotCell> CELL_IN = new SlotType<>("cell_in", (type, gui, inv, i, d) -> new SlotCell(type, gui, inv.getOrDefault(type, new EmptyContainer()), i, d.getX(), d.getY()), (t, i) -> FluidHooks.isFluidContainingItem(i), ContentEvent.ITEM_CELL_CHANGED, true, false);
public static SlotType<SlotCell> CELL_OUT = new SlotType<>("cell_out", (type, gui, inv, i, d) -> new SlotCell(type, gui, inv.getOrDefault(type, new EmptyContainer()), i, d.getX(), d.getY()), (t, i) -> FluidHooks.isFluidContainingItem(i), ContentEvent.ITEM_CELL_CHANGED, false, true);
public static SlotType<SlotFakeFluid> FL_OUT = new SlotType<>("fluid_out", (type, gui, inv, i, d) -> new SlotFakeFluid(type, gui, MachineFluidHandler.FluidDirection.OUTPUT, i, d.getX(), d.getY()), (t, i) -> false, ContentEvent.FLUID_OUTPUT_CHANGED, false, false, new ResourceLocation(Ref.ID, "fluid_out"));
public static SlotType<SlotCell> CELL_IN = new SlotType<>("cell_in", (type, gui, inv, i, d) -> new SlotCell(type, gui, inv.getOrDefault(type, new EmptyContainer()), i, d.getX(), d.getY()), (t, i) -> FluidHooks.isFluidContainingItem(i), ContentEvent.ITEM_CELL_CHANGED, true, false, new ResourceLocation(Ref.ID, "cell_in"));
public static SlotType<SlotCell> CELL_OUT = new SlotType<>("cell_out", (type, gui, inv, i, d) -> new SlotCell(type, gui, inv.getOrDefault(type, new EmptyContainer()), i, d.getX(), d.getY()), (t, i) -> FluidHooks.isFluidContainingItem(i), ContentEvent.ITEM_CELL_CHANGED, false, true, new ResourceLocation(Ref.ID, "cell_out"));
public static SlotType<SlotEnergy> ENERGY = new SlotType<>("energy", (type, gui, inv, i, d) -> new SlotEnergy(type, gui, inv.getOrDefault(type, new EmptyContainer()), i, d.getX(), d.getY()), (t, i) -> {
if (t instanceof BlockEntity tile) {
return TesseractCapUtils.getEnergyHandler(tile, null).map(eh -> TesseractCapUtils.getEnergyHandlerItem(i).map(inner -> ((inner.getInputVoltage() | inner.getOutputVoltage()) == (eh.getInputVoltage() | eh.getOutputVoltage()))).orElse(EnergyHooks.isEnergyItem(i))).orElse(EnergyHooks.isEnergyContainer(tile, null) && EnergyHooks.isEnergyItem(i));
}
return true;
}, ContentEvent.ENERGY_SLOT_CHANGED, true, false);
}, ContentEvent.ENERGY_SLOT_CHANGED, true, false, new ResourceLocation(Ref.ID, "energy"));

protected final String id;
protected final ISlotSupplier<T> slotSupplier;
public final boolean output;
public final boolean input;
public final ResourceLocation textureName;
public final BiPredicate<IGuiHandler, ItemStack> tester;
public final ContentEvent ev;

public SlotType(String id, ISlotSupplier<T> slotSupplier, BiPredicate<IGuiHandler, ItemStack> validator, ContentEvent ev) {
this(id, slotSupplier, validator, ev, true, true);
this(id, slotSupplier, validator, ev, true, true, new ResourceLocation(Ref.ID, "item"));
}

public SlotType(String id, ISlotSupplier<T> slotSupplier, BiPredicate<IGuiHandler, ItemStack> validator, ContentEvent ev, boolean input, boolean output) {
this(id, slotSupplier, validator, ev, input, output, new ResourceLocation(Ref.ID, "item"));
}

public SlotType(String id, ISlotSupplier<T> slotSupplier, BiPredicate<IGuiHandler, ItemStack> validator, ContentEvent ev, boolean input, boolean output, ResourceLocation textureName) {
this.id = id;
this.slotSupplier = slotSupplier;
this.output = output;
this.tester = validator;
this.input = input;
this.ev = ev;
this.textureName = textureName;
AntimatterAPI.register(SlotType.class, this);
}

Expand Down
15 changes: 15 additions & 0 deletions common/src/main/java/muramasa/antimatter/gui/Widget.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ public boolean isInside(double mouseX, double mouseY) {
return ((mouseX >= realX && mouseX <= realX + getW()) && (mouseY >= realY && mouseY <= realY + getH()));
}

public boolean isInside(int x, int y, int w, int h, double mouseX, double mouseY) {
int realX = realX() + x;
int realY = realY() + y;
return ((mouseX >= realX && mouseX <= realX + w) && (mouseY >= realY && mouseY <= realY + h));
}

@Environment(EnvType.CLIENT)
protected void renderTooltip(PoseStack matrixStack, Component text, double mouseX, double mouseY) {
this.gui.screen.renderComponentTooltip(matrixStack, Collections.singletonList(text), (int)mouseX,(int) mouseY);
Expand Down Expand Up @@ -291,6 +297,15 @@ protected void drawTexture(PoseStack stack, ResourceLocation loc, int left, int
GuiComponent.blit(stack, left, top, 0, x, y, sizeX, sizeY, 256, 256);
}

@Environment(EnvType.CLIENT)
protected void drawTexture(PoseStack stack, ResourceLocation loc, int left, int top, int x, int y, int sizeX, int sizeY, int textureHeight, int textureWidth) {
RenderSystem.setShaderColor(1, 1, 1, 1);
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderTexture(0, loc);
//AbstractGui.blit(stack, left, top, x, y, sizeX, sizeY);
GuiComponent.blit(stack, left, top, 0, x, y, sizeX, sizeY, textureHeight, textureWidth);
}

public static WidgetSupplier builder(BiFunction<GuiInstance, IGuiElement, Widget> source) {
return new WidgetSupplier(source);
}
Expand Down
Loading

0 comments on commit 722e4c9

Please sign in to comment.