diff options
author | Martin Robertz <dream-master@gmx.net> | 2021-12-02 16:53:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-02 16:53:40 +0100 |
commit | c0a6800447fdb87e266acfb47f35b5f765c74fe3 (patch) | |
tree | 2066554d22848e6818689d41cd4ca7f683ebebb7 /src/main/java/gregtech/api/gui | |
parent | 03b6fb248b1a20b9ccce7ac4da4c1a76875fa966 (diff) | |
download | GT5-Unofficial-c0a6800447fdb87e266acfb47f35b5f765c74fe3.tar.gz GT5-Unofficial-c0a6800447fdb87e266acfb47f35b5f765c74fe3.tar.bz2 GT5-Unofficial-c0a6800447fdb87e266acfb47f35b5f765c74fe3.zip |
add select circuit gui for machine and circuits itself (#773)
also fixed some issue with basic machine gui introduced in 9d42b299def1c41bbc7a1f01efe445be28f54399
also retrofitted volumetric flask to use the new INetworkUpdatableItem and GT_Packet_UpdateItem, deprecating MessageSetFlaskCapacity in the meanwhile.
To open the gui for machine, shift-left-click the circuit slot
To open the gui for circuit, click any block (need to be sneaking if it's chest, furnace, etc) with the circuit held in hand.
Signed-off-by: Glease <4586901+Glease@users.noreply.github.com>
Co-authored-by: Glease <4586901+Glease@users.noreply.github.com>
Diffstat (limited to 'src/main/java/gregtech/api/gui')
10 files changed, 733 insertions, 347 deletions
diff --git a/src/main/java/gregtech/api/gui/GT_Container_BasicMachine.java b/src/main/java/gregtech/api/gui/GT_Container_BasicMachine.java index 3c1d35c771..26f795de1c 100644 --- a/src/main/java/gregtech/api/gui/GT_Container_BasicMachine.java +++ b/src/main/java/gregtech/api/gui/GT_Container_BasicMachine.java @@ -28,6 +28,9 @@ public class GT_Container_BasicMachine extends GT_Container_BasicTank { mItemTransfer = false, mStuttering = false; + private Runnable circuitSlotClickCallback; + GT_Slot_Render slotCircuit; + public GT_Container_BasicMachine(InventoryPlayer aInventoryPlayer, IGregTechTileEntity aTileEntity) { super(aInventoryPlayer, aTileEntity); } @@ -38,8 +41,7 @@ public class GT_Container_BasicMachine extends GT_Container_BasicTank { addSlotToContainer(new GT_Slot_Holo(mTileEntity, 0, 26, 63, false, true, 1)); addSlotToContainer(new GT_Slot_Render(mTileEntity, 2, 107, 63)); GT_MetaTileEntity_BasicMachine machine = (GT_MetaTileEntity_BasicMachine) mTileEntity.getMetaTileEntity(); - if (machine.allowSelectCircuit()) - addSlotToContainer(new GT_Slot_Render(mTileEntity, machine.getCircuitSlot(), 153, 63)); + addSlotToContainer(slotCircuit = new GT_Slot_Render(mTileEntity, machine.getCircuitSlot(), 153, 63)); int tStartIndex = machine.getInputSlot(); @@ -186,17 +188,6 @@ public class GT_Container_BasicMachine extends GT_Container_BasicTank { addSlotToContainer(new GT_Slot_Render(mTileEntity, tStartIndex++, 53, 63)); } - private static int find(List<ItemStack> aStacks, ItemStack aStack) { - if (GT_Utility.isStackInvalid(aStack)) - return -1; - for (int i = 0, aStacksSize = aStacks.size(); i < aStacksSize; i++) { - ItemStack tStack = aStacks.get(i); - if (GT_Utility.areStacksEqual(aStack, tStack)) - return i; - } - return -1; - } - @Override public ItemStack slotClick(int aSlotIndex, int aMouseclick, int aShifthold, EntityPlayer aPlayer) { if (mTileEntity.getMetaTileEntity() == null) return null; @@ -213,15 +204,21 @@ public class GT_Container_BasicMachine extends GT_Container_BasicTank { case 3: if (machine.allowSelectCircuit() && aMouseclick < 2) { ItemStack newCircuit; - if (aMouseclick == 1 && aShifthold == 1) { - // clear - newCircuit = null; + if (aShifthold == 1) { + if (aMouseclick == 0) { + if (circuitSlotClickCallback != null) + circuitSlotClickCallback.run(); + return null; + } else { + // clear + newCircuit = null; + } } else { ItemStack cursorStack = aPlayer.inventory.getItemStack(); List<ItemStack> tCircuits = machine.getConfigurationCircuits(); - int index = find(tCircuits, cursorStack); + int index = GT_Utility.findMatchingStackInList(tCircuits, cursorStack); if (index < 0) { - int curIndex = find(tCircuits, machine.getStackInSlot(machine.getCircuitSlot())) + 1; + int curIndex = GT_Utility.findMatchingStackInList(tCircuits, machine.getStackInSlot(machine.getCircuitSlot())) + 1; if (aMouseclick == 0) { curIndex += 1; } else { @@ -315,4 +312,8 @@ public class GT_Container_BasicMachine extends GT_Container_BasicTank { public int getShiftClickSlotCount() { return ((GT_MetaTileEntity_BasicMachine) mTileEntity.getMetaTileEntity()).mInputSlotCount; } + + public void setCircuitSlotClickCallback(Runnable circuitSlotClickCallback) { + this.circuitSlotClickCallback = circuitSlotClickCallback; + } } diff --git a/src/main/java/gregtech/api/gui/GT_GUIContainer.java b/src/main/java/gregtech/api/gui/GT_GUIContainer.java index f53684f10a..b122b33a73 100644 --- a/src/main/java/gregtech/api/gui/GT_GUIContainer.java +++ b/src/main/java/gregtech/api/gui/GT_GUIContainer.java @@ -4,6 +4,7 @@ import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.renderer.Tessellator; import net.minecraft.inventory.Container; import net.minecraft.util.ResourceLocation; +import org.lwjgl.input.Mouse; /** * NEVER INCLUDE THIS FILE IN YOUR MOD!!! @@ -53,6 +54,21 @@ public class GT_GUIContainer extends GuiContainer { } } } + + @Override + public void handleMouseInput() { + int delta = Mouse.getEventDWheel(); + if (delta != 0) { + int i = Mouse.getEventX() * this.width / this.mc.displayWidth; + int j = this.height - Mouse.getEventY() * this.height / this.mc.displayHeight - 1; + onMouseWheel(i, j, delta); + } + super.handleMouseInput(); + } + + protected void onMouseWheel(int mx, int my, int delta) { + } + /* @Override protected void drawSlotInventory(Slot par1Slot) { diff --git a/src/main/java/gregtech/api/gui/GT_GUIContainer_BasicMachine.java b/src/main/java/gregtech/api/gui/GT_GUIContainer_BasicMachine.java index bd28bb11e9..df02d219fd 100644 --- a/src/main/java/gregtech/api/gui/GT_GUIContainer_BasicMachine.java +++ b/src/main/java/gregtech/api/gui/GT_GUIContainer_BasicMachine.java @@ -1,8 +1,14 @@ package gregtech.api.gui; +import gregtech.api.enums.GT_Values; import gregtech.api.interfaces.tileentity.IGregTechTileEntity; +import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_BasicMachine; import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_BasicMachine_Bronze; +import gregtech.api.net.GT_Packet_SetConfigurationCircuit; +import gregtech.api.util.GT_Utility; import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.util.StatCollector; import java.util.ArrayList; import java.util.List; @@ -33,6 +39,7 @@ public class GT_GUIContainer_BasicMachine extends GT_GUIContainerMetaTile_Machin public GT_GUIContainer_BasicMachine(InventoryPlayer aInventoryPlayer, IGregTechTileEntity aTileEntity, String aName, String aTextureFile, String aNEI, byte aProgressBarDirection, byte aProgressBarAmount) { super(new GT_Container_BasicMachine(aInventoryPlayer, aTileEntity), RES_PATH_GUI + "basicmachines/" + aTextureFile); + getContainer().setCircuitSlotClickCallback(this::openSelectCircuitDialog); mProgressBarDirection = aProgressBarDirection; mProgressBarAmount = (byte) Math.max(1, aProgressBarAmount); mName = aName; @@ -40,6 +47,27 @@ public class GT_GUIContainer_BasicMachine extends GT_GUIContainerMetaTile_Machin mRenderAutoOutputSlots = !(aTileEntity.getMetaTileEntity() instanceof GT_MetaTileEntity_BasicMachine_Bronze); } + private void openSelectCircuitDialog() { + mc.displayGuiScreen(new GT_GUIDialogSelectItem( + StatCollector.translateToLocal("GT5U.machines.select_circuit"), + null, + this, + this::onCircuitSelected, + getMachine().getConfigurationCircuits(), + GT_Utility.findMatchingStackInList(getMachine().getConfigurationCircuits(), getMachine().getStackInSlot(getMachine().getCircuitSlot())))); + } + + private void onCircuitSelected(ItemStack selected) { + GT_Values.NW.sendToServer(new GT_Packet_SetConfigurationCircuit(mContainer.mTileEntity, selected)); + // we will not do any validation on client side + // it doesn't get to actually decide what inventory contains anyway + mContainer.mTileEntity.setInventorySlotContents(getMachine().getCircuitSlot(), selected); + } + + private GT_MetaTileEntity_BasicMachine getMachine() { + return (GT_MetaTileEntity_BasicMachine) mContainer.mTileEntity.getMetaTileEntity(); + } + @Override public void drawScreen(int par1, int par2, float par3) { super.drawScreen(par1, par2, par3); @@ -72,6 +100,17 @@ public class GT_GUIContainer_BasicMachine extends GT_GUIContainerMetaTile_Machin } @Override + protected void onMouseWheel(int mx, int my, int delta) { + GT_Slot_Render slotCircuit = getContainer().slotCircuit; + if (slotCircuit != null && func_146978_c(slotCircuit.xDisplayPosition, slotCircuit.yDisplayPosition, 16, 16, mx, my)) { + // emulate click + handleMouseClick(slotCircuit, -1, delta > 0 ? 1 : 0, 0); + return; + } + super.onMouseWheel(mx, my, delta); + } + + @Override protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) { super.drawGuiContainerBackgroundLayer(par1, par2, par3); int x = (width - xSize) / 2; @@ -79,16 +118,16 @@ public class GT_GUIContainer_BasicMachine extends GT_GUIContainerMetaTile_Machin drawTexturedModalRect(x, y, 0, 0, xSize, ySize); if (mContainer != null) { if (mRenderAutoOutputSlots){ - if (((GT_Container_BasicMachine) mContainer).mFluidTransfer) + if (getContainer().mFluidTransfer) drawTexturedModalRect(x + 7, y + 62, 176, 18, 18, 18); - if (((GT_Container_BasicMachine) mContainer).mItemTransfer) + if (getContainer().mItemTransfer) drawTexturedModalRect(x + 25, y + 62, 176, 36, 18, 18); } - if (((GT_Container_BasicMachine) mContainer).mStuttering) + if (getContainer().mStuttering) drawTexturedModalRect(x + 79, y + 44, 176, 54, 18, 18); if (mContainer.mMaxProgressTime > 0) { - int tSize = (mProgressBarDirection < 2 ? 20 : 18), tProgress = Math.max(1, Math.min(tSize * mProgressBarAmount, (mContainer.mProgressTime > 0 ? 1 : 0) + (mContainer.mProgressTime * tSize * mProgressBarAmount) / mContainer.mMaxProgressTime)) % (tSize + 1); + int tSize = mProgressBarDirection < 2 ? 20 : 18, tProgress = Math.max(1, Math.min(tSize * mProgressBarAmount, (mContainer.mProgressTime > 0 ? 1 : 0) + mContainer.mProgressTime * tSize * mProgressBarAmount / mContainer.mMaxProgressTime)) % (tSize + 1); switch (mProgressBarDirection) { // yes, my OCD was mad at me before I did the Tabs. case 0: @@ -123,4 +162,8 @@ public class GT_GUIContainer_BasicMachine extends GT_GUIContainerMetaTile_Machin } } } + + private GT_Container_BasicMachine getContainer() { + return (GT_Container_BasicMachine) mContainer; + } } diff --git a/src/main/java/gregtech/api/gui/GT_GUICover.java b/src/main/java/gregtech/api/gui/GT_GUICover.java index 2e9d082ebf..5a299d7ff9 100644 --- a/src/main/java/gregtech/api/gui/GT_GUICover.java +++ b/src/main/java/gregtech/api/gui/GT_GUICover.java @@ -1,145 +1,16 @@ package gregtech.api.gui; -import gregtech.api.enums.Dyes; -import gregtech.api.gui.widgets.GT_GuiFakeItemButton; -import gregtech.api.gui.widgets.GT_GuiIntegerTextBox; -import gregtech.api.gui.widgets.GT_GuiTooltip; -import gregtech.api.gui.widgets.GT_GuiTooltipManager; -import gregtech.api.gui.widgets.GT_GuiTooltipManager.GT_IToolTipRenderer; -import gregtech.api.interfaces.IGuiScreen; import gregtech.api.interfaces.tileentity.ICoverable; -import net.minecraft.client.gui.FontRenderer; -import net.minecraft.client.gui.GuiButton; -import net.minecraft.client.gui.GuiScreen; -import net.minecraft.client.gui.GuiTextField; -import net.minecraft.client.renderer.RenderHelper; -import net.minecraft.client.renderer.entity.RenderItem; import net.minecraft.item.ItemStack; -import net.minecraft.util.ResourceLocation; -import org.lwjgl.input.Mouse; -import org.lwjgl.opengl.GL11; -import org.lwjgl.opengl.GL12; -import java.util.ArrayList; -import java.util.List; +public abstract class GT_GUICover extends GT_GUIScreen { -public abstract class GT_GUICover extends GuiScreen implements GT_IToolTipRenderer, IGuiScreen { - - protected GT_GuiTooltipManager ttManager = new GT_GuiTooltipManager(); - - protected int gui_width = 176; - protected int gui_height = 107; - protected int guiTop, guiLeft; - protected boolean drawButtons = true; - private GuiButton selectedButton; - public String header; - public GT_GuiFakeItemButton headerIcon; public final ICoverable tile; - - protected List<IGuiElement> elements = new ArrayList<>(); - protected List<GT_GuiIntegerTextBox> textBoxes = new ArrayList<>(); - public GT_GUICover(ICoverable tile, int width, int height, ItemStack cover) { + super(width, height, cover == null ? "" : cover.getDisplayName()); this.tile = tile; - this.gui_width = width; - this.gui_height = height; - this.header = (cover != null) ? cover.getDisplayName() : ""; - this.headerIcon = new GT_GuiFakeItemButton(this, 5, 5, null).setItem(cover); - } - - @Override - public void initGui() { - guiLeft = (this.width - this.gui_width) / 2; - guiTop = (this.height - this.gui_height) / 2; - - for (IGuiElement element : elements) { - if (element instanceof GuiButton) - buttonList.add(element); - if (element instanceof GT_GuiIntegerTextBox) - textBoxes.add((GT_GuiIntegerTextBox) element); - } - - onInitGui(guiLeft, guiTop, gui_width, gui_height); - - for (IGuiElement element : elements) { - element.onInit(); - } - } - - protected abstract void onInitGui(int guiLeft, int guiTop, int gui_width, int gui_height); - - public void onMouseWheel(int x, int y, int delta) { - } - - @Override - public void handleMouseInput() { - int delta = Mouse.getEventDWheel(); - if (delta != 0) { - int i = Mouse.getEventX() * this.width / this.mc.displayWidth; - int j = this.height - Mouse.getEventY() * this.height / this.mc.displayHeight - 1; - onMouseWheel(i, j, delta); - } - super.handleMouseInput(); - } - - @Override - public void drawScreen(int mouseX, int mouseY, float parTicks) { - drawDefaultBackground(); - - drawBackground(mouseX, mouseY, parTicks); - - RenderHelper.disableStandardItemLighting(); - GL11.glDisable(GL11.GL_LIGHTING); - GL11.glDisable(GL11.GL_DEPTH_TEST); - GL11.glDisable(GL12.GL_RESCALE_NORMAL); - if (drawButtons) { - RenderHelper.enableGUIStandardItemLighting(); - for (IGuiElement e : elements) - e.draw(mouseX, mouseY, parTicks); - } - GL11.glEnable(GL12.GL_RESCALE_NORMAL); - - GL11.glPushMatrix(); - GL11.glTranslatef(guiLeft, guiTop, 0.0F); - GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); - - GL11.glDisable(GL11.GL_LIGHTING); - drawForegroundLayer(mouseX, mouseY, parTicks); - GL11.glEnable(GL11.GL_LIGHTING); - - GL11.glPopMatrix(); - - GL11.glEnable(GL11.GL_LIGHTING); - GL11.glEnable(GL11.GL_DEPTH_TEST); - RenderHelper.enableStandardItemLighting(); - } - - public void drawForegroundLayer(int mouseX, int mouseY, float parTicks) { - drawExtras(mouseX, mouseY, parTicks); - ttManager.onTick(this, mouseX, mouseY); - } - - public void drawBackground(int mouseX, int mouseY, float parTicks) { - short[] color = Dyes.MACHINE_METAL.getRGBA(); - GL11.glColor3ub((byte) color[0], (byte) color[1], (byte) color[2]); - this.mc.renderEngine.bindTexture(new ResourceLocation("gregtech:textures/gui/GuiCover.png")); - drawTexturedModalRect(guiLeft, guiTop, 0,0, gui_width, gui_height); - } - - public void drawExtras(int mouseX, int mouseY, float parTicks) { - this.fontRendererObj.drawString(header, 25, 9, 0xFF222222); - } - - @Override - public boolean doesGuiPauseGame() - { - return false; - } - - public void closeScreen() { - this.mc.displayGuiScreen((GuiScreen) null); - this.mc.setIngameFocus(); + headerIcon.setItem(cover); } @Override @@ -147,179 +18,6 @@ public abstract class GT_GUICover extends GuiScreen implements GT_IToolTipRender super.updateScreen(); if (!tile.isUseableByPlayer(mc.thePlayer)) { closeScreen(); - return; - } - for (GuiTextField f : textBoxes) { - f.updateCursorCounter(); - } - } - - @Override - public void mouseClicked(int x, int y, int button) { - for (GT_GuiIntegerTextBox tBox : textBoxes) { - boolean hadFocus = tBox.isFocused(); - if (tBox.isEnabled() || hadFocus) - tBox.mouseClicked(x,y,button); - - if (tBox.isFocused() && button == 1 && tBox.isEnabled()) //rightclick -> lcear it - tBox.setText("0"); - else if (hadFocus && !tBox.isFocused()) - applyTextBox(tBox); - } - super.mouseClicked(x, y, button); - } - - @Override - public void keyTyped(char c, int key) { - GT_GuiIntegerTextBox focusedTextBox = null; - for (GT_GuiIntegerTextBox textBox : textBoxes) { - if (textBox.isFocused()) - focusedTextBox = textBox; - } - - if (key == 1) { //esc - if(focusedTextBox != null) { - resetTextBox(focusedTextBox); - setFocusedTextBox(null); - return; - } else { - closeScreen(); - } - } - - if (c == '\t') { //tab - for (int i = 0; i < textBoxes.size(); i++) { - GT_GuiIntegerTextBox box = textBoxes.get(i); - if (box.isFocused()) { - applyTextBox(box); - setFocusedTextBox(((i+1) < textBoxes.size()) ? textBoxes.get(i+1) : null); - return; - } - } - if (!textBoxes.isEmpty()) - setFocusedTextBox(textBoxes.get(0)); - return; - } - - if (focusedTextBox != null && focusedTextBox.textboxKeyTyped(c, key)){ - return; - } - - if (key == 28 && focusedTextBox != null) { // enter - applyTextBox(focusedTextBox); - setFocusedTextBox(null); - return; - } - - if (key == this.mc.gameSettings.keyBindInventory.getKeyCode()) { - if (focusedTextBox != null) { - applyTextBox(focusedTextBox); - setFocusedTextBox(null); - return; - } - closeScreen(); - return; } - super.keyTyped(c, key); - } - - /** - * Button - */ - - @Override - public void actionPerformed(GuiButton button) { - selectedButton = button; - } - - @Override - public void clearSelectedButton() { - selectedButton = null; - } - @Override - public GuiButton getSelectedButton(){return selectedButton;} - - @Override - public void buttonClicked(GuiButton button) { - - } - - /** - * TextBoxes - */ - private void setFocusedTextBox(GT_GuiIntegerTextBox boxToFocus) { - for (GT_GuiIntegerTextBox textBox : textBoxes) { - textBox.setFocused(textBox.equals(boxToFocus) && textBox.isEnabled()); - } - } - - /** - * Given textbox's value might have changed. - */ - public void applyTextBox(GT_GuiIntegerTextBox box) { - - } - - /** - * Reset the given textbox to the last valid value, <b>NOT</b> 0. - */ - public void resetTextBox(GT_GuiIntegerTextBox box) { - - } - - /** - * GT_IToolTipRenderer - */ - @Override - public void drawHoveringText(List par1List, int par2, int par3, FontRenderer render) { - super.drawHoveringText(par1List, par2, par3, render); - } - @Override - public FontRenderer getFontRenderer() { - return super.fontRendererObj; - } - @Override - public void addToolTip(GT_GuiTooltip toolTip) { - ttManager.addToolTip(toolTip); - } - @Override - public boolean removeToolTip(GT_GuiTooltip toolTip) { - return ttManager.removeToolTip(toolTip); - } - - /** - * Junk - */ - @Override - public int getGuiTop() { - return guiTop; - } - @Override - public int getGuiLeft() { - return guiLeft; - } - @Override - public int getXSize() { - return gui_width; - } - @Override - public int getYSize() { - return gui_height; - } - - @Override - public RenderItem getItemRenderer() { - return itemRender; - } - - @Override - public void addElement(IGuiElement element) { - if (elements.contains(element)) - return; - elements.add(element); - } - @Override - public boolean removeElement(IGuiElement element) { - return elements.remove(element); } } diff --git a/src/main/java/gregtech/api/gui/GT_GUIDialogSelectItem.java b/src/main/java/gregtech/api/gui/GT_GUIDialogSelectItem.java new file mode 100644 index 0000000000..2c3f77cf06 --- /dev/null +++ b/src/main/java/gregtech/api/gui/GT_GUIDialogSelectItem.java @@ -0,0 +1,217 @@ +package gregtech.api.gui; + +import gregtech.api.gui.widgets.GT_GuiFakeItemButton; +import gregtech.api.gui.widgets.GT_GuiIcon; +import gregtech.api.gui.widgets.GT_GuiIconButton; +import gregtech.api.util.GT_Utility; +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.item.ItemStack; +import net.minecraft.util.StatCollector; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +public class GT_GUIDialogSelectItem extends GT_GUIScreen { + public static final int UNSELECTED = -1; + private static final int cols = 9; + private static final int rows = 3; + private final GuiScreen parent; + private final Consumer<ItemStack> selectedCallback; + // passed in stack + private final List<ItemStack> stacks; + // all slots not including btnCurrent + private final List<GT_GuiFakeItemButton> slots = new ArrayList<>(); + // the currently selected slot content + private final GT_GuiFakeItemButton btnCurrent = new GT_GuiFakeItemButton(this, 8, 25, GT_GuiIcon.SLOT_DARKGRAY).setMimicSlot(true); + private final boolean noDeselect; + private int selected; + private int scroll = 0; + private GT_GuiIconButton btnUp; + private GT_GuiIconButton btnDown; + + + public GT_GUIDialogSelectItem(String header, ItemStack headerItem, GuiScreen parent, Consumer<ItemStack> selectedCallback, List<ItemStack> stacks) { + this(header, headerItem, parent, selectedCallback, stacks, UNSELECTED); + } + + public GT_GUIDialogSelectItem(String header, ItemStack headerItem, GuiScreen parent, Consumer<ItemStack> selectedCallback, List<ItemStack> stacks, int selected) { + this(header, headerItem, parent, selectedCallback, stacks, selected, false); + } + + /** + * Open a dialog to select an item from given list. Given callback may be called zero or more times depending on user action. + * @param header Header text + * @param headerItem ItemStack to use as Dialog icon + * @param parent open which GUIScreen when this dialog is closed. use null if it has no parent. + * @param selectedCallback callback upon selected + * @param stacks list to choose from + * @param selected preselected item. Use {@link #UNSELECTED} for unselected. Invalid selected will be clamped to 0 or highest index + * @param noDeselect true if player cannot deselect, false otherwise. If this is set to true, selectedCallback is guaranteed to be called with a nonnull stack + */ + public GT_GUIDialogSelectItem(String header, ItemStack headerItem, GuiScreen parent, Consumer<ItemStack> selectedCallback, List<ItemStack> stacks, int selected, boolean noDeselect) { + super(176, 107, header); + this.noDeselect = noDeselect; + if (headerItem != null) + this.headerIcon.setItem(headerItem); + this.parent = parent; + this.selectedCallback = selectedCallback; + this.stacks = stacks; + + if (stacks.size() > rows * cols) { + btnUp = new GT_GuiIconButton(this, 0, 134, 25, GT_GuiIcon.GREEN_ARROW_UP); + btnDown = new GT_GuiIconButton(this, 1, 152, 25, GT_GuiIcon.GREEN_ARROW_DOWN); + } + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + slots.add(new GT_GuiFakeItemButton(this, 8 + 18 * j, 44 + 18 * i, GT_GuiIcon.SLOT_GRAY).setMimicSlot(true)); + } + } + + setSelected(noDeselect ? Math.max(0, selected) : selected); + ensureSelectedDisplayed(); + } + + @Override + protected void onInitGui(int guiLeft, int guiTop, int gui_width, int gui_height) { + btnCurrent.setX(8 + 2 + fontRendererObj.getStringWidth(StatCollector.translateToLocal("GT5U.gui.select.current"))); + } + + @Override + public void closeScreen() { + selectedCallback.accept(getCandidate(getSelected())); + mc.displayGuiScreen(parent); + if (parent == null) + mc.setIngameFocus(); + } + + @Override + public void buttonClicked(GuiButton button) { + switch (button.id) { + case 0: + setScroll(scroll - 1); + return; + case 1: + setScroll(scroll + 1); + return; + } + super.buttonClicked(button); + } + + @Override + public void drawExtras(int mouseX, int mouseY, float parTicks) { + int y = 25 + (18 - getFontRenderer().FONT_HEIGHT) / 2; + getFontRenderer().drawString(StatCollector.translateToLocal("GT5U.gui.select.current"), 8, y, 0xff555555); + super.drawExtras(mouseX, mouseY, parTicks); + } + + @Override + public void mouseClicked(int x, int y, int button) { + int mx = x - guiLeft, my = y - guiTop; + if (button == 0) { + if (btnCurrent.getBounds().contains(mx, my)) { + ensureSelectedDisplayed(); + return; + } + + for (int i = 0, slotsSize = slots.size(); i < slotsSize; i++) { + GT_GuiFakeItemButton slot = slots.get(i); + if (slot.getBounds().contains(mx, my)) { + setSelected(slotIndexToListIndex(i)); + return; + } + } + } else if (button == 1 && getSelected() >= 0) { + if (btnCurrent.getBounds().contains(mx, my)) { + setSelected(UNSELECTED); + return; + } + GT_GuiFakeItemButton slot = getSlot(listIndexToSlotIndex(getSelected())); + if (slot != null && slot.getBounds().contains(mx, my)) { + setSelected(UNSELECTED); + } + } + super.mouseClicked(x, y, button); + } + + @Override + public void onMouseWheel(int x, int y, int delta) { + if (delta < 0) + setScroll(scroll + 1); + else if (delta > 0) + setScroll(scroll - 1); + } + + private void fillSlots() { + for (int i = 0, j = scroll * cols; i < slots.size(); i++, j++) { + slots.get(i) + .setItem(getCandidate(j)) + .setBgIcon(j == getSelected() ? GT_GuiIcon.SLOT_DARKGRAY : GT_GuiIcon.SLOT_GRAY); + } + } + + private void ensureSelectedDisplayed() { + if (getSelected() < scroll * cols) { + setScroll(getSelected() / cols); + } else if (getSelected() > (scroll + rows) * cols) { + setScroll((getSelected() - (rows - 1) * cols) / cols); + } else { + // called nonetheless to update button enabled states + setScroll(scroll); + } + } + + private int slotIndexToListIndex(int index) { + int mapped = scroll * cols + index; + return mapped >= stacks.size() ? UNSELECTED : mapped; + } + + private int listIndexToSlotIndex(int index) { + return index - scroll * cols; + } + + public int getSelected() { + return selected; + } + + public void setSelected(int selected) { + if (selected == this.selected) return; + int newSelected = GT_Utility.clamp(selected, UNSELECTED, stacks.size() - 1); + + if (noDeselect && newSelected == UNSELECTED) + return; + + GT_GuiFakeItemButton selectedSlot = getSlot(this.selected); + if (selectedSlot != null) + selectedSlot.setBgIcon(GT_GuiIcon.SLOT_GRAY); + + this.selected = newSelected; + + btnCurrent.setItem(getCandidate(this.selected)); + + selectedSlot = getSlot(this.selected); + if (selectedSlot != null) + selectedSlot.setBgIcon(GT_GuiIcon.SLOT_DARKGRAY); + } + + private void setScroll(int scroll) { + if (stacks.size() > rows * cols) { + int lo = 0; + int hi = (stacks.size() - rows * cols) / cols + 1; + this.scroll = GT_Utility.clamp(scroll, lo, hi); + btnUp.enabled = this.scroll != lo; + btnDown.enabled = this.scroll != hi; + } + fillSlots(); + } + + private ItemStack getCandidate(int listIndex) { + return listIndex < 0 || listIndex >= stacks.size() ? null : stacks.get(listIndex); + } + + private GT_GuiFakeItemButton getSlot(int slotIndex) { + return slotIndex < 0 || slotIndex >= slots.size() ? null : slots.get(slotIndex); + } +} diff --git a/src/main/java/gregtech/api/gui/GT_GUIScreen.java b/src/main/java/gregtech/api/gui/GT_GUIScreen.java new file mode 100644 index 0000000000..a7ab6e269b --- /dev/null +++ b/src/main/java/gregtech/api/gui/GT_GUIScreen.java @@ -0,0 +1,320 @@ +package gregtech.api.gui; + +import gregtech.api.enums.Dyes; +import gregtech.api.gui.widgets.GT_GuiFakeItemButton; +import gregtech.api.gui.widgets.GT_GuiIntegerTextBox; +import gregtech.api.gui.widgets.GT_GuiTooltip; +import gregtech.api.gui.widgets.GT_GuiTooltipManager; +import gregtech.api.gui.widgets.GT_GuiTooltipManager.GT_IToolTipRenderer; +import gregtech.api.interfaces.IGuiScreen; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.GuiTextField; +import net.minecraft.client.renderer.RenderHelper; +import net.minecraft.client.renderer.entity.RenderItem; +import net.minecraft.util.ResourceLocation; +import org.lwjgl.input.Mouse; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL12; + +import java.util.ArrayList; +import java.util.List; + +public abstract class GT_GUIScreen extends GuiScreen implements GT_IToolTipRenderer, IGuiScreen { + + protected GT_GuiTooltipManager ttManager = new GT_GuiTooltipManager(); + + protected int gui_width = 176; + protected int gui_height = 107; + protected int guiTop, guiLeft; + protected boolean drawButtons = true; + private GuiButton selectedButton; + public String header; + public GT_GuiFakeItemButton headerIcon; + + + protected List<IGuiElement> elements = new ArrayList<>(); + protected List<GT_GuiIntegerTextBox> textBoxes = new ArrayList<>(); + + public GT_GUIScreen(int width, int height, String header) { + this.gui_width = width; + this.gui_height = height; + this.header = header; + this.headerIcon = new GT_GuiFakeItemButton(this, 5, 5, null); + } + + @Override + public void initGui() { + guiLeft = (this.width - this.gui_width) / 2; + guiTop = (this.height - this.gui_height) / 2; + + for (IGuiElement element : elements) { + if (element instanceof GuiButton) + buttonList.add(element); + if (element instanceof GT_GuiIntegerTextBox) + textBoxes.add((GT_GuiIntegerTextBox) element); + } + + onInitGui(guiLeft, guiTop, gui_width, gui_height); + + for (IGuiElement element : elements) { + element.onInit(); + } + super.initGui(); + } + + protected abstract void onInitGui(int guiLeft, int guiTop, int gui_width, int gui_height); + + public void onMouseWheel(int x, int y, int delta) { + } + + @Override + public void handleMouseInput() { + int delta = Mouse.getEventDWheel(); + if (delta != 0) { + int i = Mouse.getEventX() * this.width / this.mc.displayWidth; + int j = this.height - Mouse.getEventY() * this.height / this.mc.displayHeight - 1; + onMouseWheel(i, j, delta); + } + super.handleMouseInput(); + } + + @Override + public void drawScreen(int mouseX, int mouseY, float parTicks) { + drawDefaultBackground(); + + drawBackground(mouseX, mouseY, parTicks); + + RenderHelper.disableStandardItemLighting(); + GL11.glDisable(GL11.GL_LIGHTING); + GL11.glDisable(GL11.GL_DEPTH_TEST); + GL11.glDisable(GL12.GL_RESCALE_NORMAL); + if (drawButtons) { + RenderHelper.enableGUIStandardItemLighting(); + for (IGuiElement e : elements) + e.draw(mouseX, mouseY, parTicks); + } + GL11.glEnable(GL12.GL_RESCALE_NORMAL); + + GL11.glPushMatrix(); + GL11.glTranslatef(guiLeft, guiTop, 0.0F); + GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); + + GL11.glDisable(GL11.GL_LIGHTING); + drawForegroundLayer(mouseX, mouseY, parTicks); + GL11.glEnable(GL11.GL_LIGHTING); + + GL11.glPopMatrix(); + + GL11.glEnable(GL11.GL_LIGHTING); + GL11.glEnable(GL11.GL_DEPTH_TEST); + RenderHelper.enableStandardItemLighting(); + } + + public void drawForegroundLayer(int mouseX, int mouseY, float parTicks) { + drawExtras(mouseX, mouseY, parTicks); + ttManager.onTick(this, mouseX, mouseY); + } + + public void drawBackground(int mouseX, int mouseY, float parTicks) { + short[] color = Dyes.MACHINE_METAL.getRGBA(); + GL11.glColor3ub((byte) color[0], (byte) color[1], (byte) color[2]); + this.mc.renderEngine.bindTexture(new ResourceLocation("gregtech:textures/gui/GuiCover.png")); + drawTexturedModalRect(guiLeft, guiTop, 0,0, gui_width, gui_height); + } + + public void drawExtras(int mouseX, int mouseY, float parTicks) { + this.fontRendererObj.drawString(header, 25, 9, 0xFF222222); + } + + @Override + public boolean doesGuiPauseGame() + { + return false; + } + + public void closeScreen() { + this.mc.displayGuiScreen(null); + this.mc.setIngameFocus(); + } + + @Override + public void updateScreen() { + super.updateScreen(); + for (GuiTextField f : textBoxes) { + f.updateCursorCounter(); + } + } + + @Override + public void mouseClicked(int x, int y, int button) { + for (GT_GuiIntegerTextBox tBox : textBoxes) { + boolean hadFocus = tBox.isFocused(); + if (tBox.isEnabled() || hadFocus) + tBox.mouseClicked(x,y,button); + + if (tBox.isFocused() && button == 1 && tBox.isEnabled()) //rightclick -> lcear it + tBox.setText("0"); + else if (hadFocus && !tBox.isFocused()) + applyTextBox(tBox); + } + super.mouseClicked(x, y, button); + } + + @Override + public void keyTyped(char c, int key) { + GT_GuiIntegerTextBox focusedTextBox = null; + for (GT_GuiIntegerTextBox textBox : textBoxes) { + if (textBox.isFocused()) + focusedTextBox = textBox; + } + + if (key == 1) { //esc + if(focusedTextBox != null) { + resetTextBox(focusedTextBox); + setFocusedTextBox(null); + return; + } else { + closeScreen(); + // don't fall through to parent + return; + } + } + + if (c == '\t') { //tab + for (int i = 0; i < textBoxes.size(); i++) { + GT_GuiIntegerTextBox box = textBoxes.get(i); + if (box.isFocused()) { + applyTextBox(box); + setFocusedTextBox(((i+1) < textBoxes.size()) ? textBoxes.get(i+1) : null); + return; + } + } + if (!textBoxes.isEmpty()) + setFocusedTextBox(textBoxes.get(0)); + return; + } + + if (focusedTextBox != null && focusedTextBox.textboxKeyTyped(c, key)){ + return; + } + + if (key == 28 && focusedTextBox != null) { // enter + applyTextBox(focusedTextBox); + setFocusedTextBox(null); + return; + } + + if (key == this.mc.gameSettings.keyBindInventory.getKeyCode()) { + if (focusedTextBox != null) { + applyTextBox(focusedTextBox); + setFocusedTextBox(null); + return; + } + closeScreen(); + return; + } + super.keyTyped(c, key); + } + + /** + * Button + */ + + @Override + public void actionPerformed(GuiButton button) { + selectedButton = button; + } + + @Override + public void clearSelectedButton() { + selectedButton = null; + } + @Override + public GuiButton getSelectedButton(){return selectedButton;} + + @Override + public void buttonClicked(GuiButton button) { + + } + + /** + * TextBoxes + */ + private void setFocusedTextBox(GT_GuiIntegerTextBox boxToFocus) { + for (GT_GuiIntegerTextBox textBox : textBoxes) { + textBox.setFocused(textBox.equals(boxToFocus) && textBox.isEnabled()); + } + } + + /** + * Given textbox's value might have changed. + */ + public void applyTextBox(GT_GuiIntegerTextBox box) { + + } + + /** + * Reset the given textbox to the last valid value, <b>NOT</b> 0. + */ + public void resetTextBox(GT_GuiIntegerTextBox box) { + + } + + /** + * GT_IToolTipRenderer + */ + @Override + public void drawHoveringText(List par1List, int par2, int par3, FontRenderer render) { + super.drawHoveringText(par1List, par2, par3, render); + } + @Override + public FontRenderer getFontRenderer() { + return super.fontRendererObj; + } + @Override + public void addToolTip(GT_GuiTooltip toolTip) { + ttManager.addToolTip(toolTip); + } + @Override + public boolean removeToolTip(GT_GuiTooltip toolTip) { + return ttManager.removeToolTip(toolTip); + } + + /** + * Junk + */ + @Override + public int getGuiTop() { + return guiTop; + } + @Override + public int getGuiLeft() { + return guiLeft; + } + @Override + public int getXSize() { + return gui_width; + } + @Override + public int getYSize() { + return gui_height; + } + + @Override + public RenderItem getItemRenderer() { + return itemRender; + } + + @Override + public void addElement(IGuiElement element) { + if (elements.contains(element)) + return; + elements.add(element); + } + @Override + public boolean removeElement(IGuiElement element) { + return elements.remove(element); + } +} diff --git a/src/main/java/gregtech/api/gui/widgets/GT_GuiFakeItemButton.java b/src/main/java/gregtech/api/gui/widgets/GT_GuiFakeItemButton.java index db7029d60f..2c1d8aa886 100644 --- a/src/main/java/gregtech/api/gui/widgets/GT_GuiFakeItemButton.java +++ b/src/main/java/gregtech/api/gui/widgets/GT_GuiFakeItemButton.java @@ -1,44 +1,100 @@ package gregtech.api.gui.widgets; +import codechicken.lib.gui.GuiDraw; import gregtech.api.interfaces.IGuiScreen; +import gregtech.api.util.GT_UtilityClient; import net.minecraft.client.Minecraft; import net.minecraft.item.ItemStack; import org.lwjgl.opengl.GL11; import java.awt.*; +import java.util.List; public class GT_GuiFakeItemButton implements IGuiScreen.IGuiElement { - private final GT_GuiIcon bgIcon; + private GT_GuiIcon bgIcon; private ItemStack item; private IGuiScreen gui; - private int x0, y0, xPosition, yPosition; - private int width, height; + private int xPosition, yPosition; + private List<String> itemTooltips; + private final GT_GuiTooltip tooltip = new GT_GuiTooltip(null) { + @Override + public List<String> getToolTipText() { + return itemTooltips; + } + + @Override + public boolean isDelayed() { + return false; + } + + @Override + public Rectangle getBounds() { + return GT_GuiFakeItemButton.this.getBounds(); + } + }; + private final Rectangle rectangle; + private boolean mimicSlot; public GT_GuiFakeItemButton(IGuiScreen gui, int x, int y, GT_GuiIcon bgIcon) { this.gui = gui; - this.x0 = x; - this.y0 = y; this.bgIcon = bgIcon; item = null; - width = 18; - height = 18; + rectangle = new Rectangle(x, y, 18, 18); gui.addElement(this); } public GT_GuiFakeItemButton setItem(ItemStack i) { item = i; + if (getMimicSlot()) + updateTooltip(); return this; } + private void updateTooltip() { + itemTooltips = item == null ? null : GT_UtilityClient.getTooltip(item, true); + } + public ItemStack getItem(){ return item; } + public GT_GuiFakeItemButton setMimicSlot(boolean mimicSlot) { + if (mimicSlot != this.mimicSlot) { + if (mimicSlot) { + updateTooltip(); + gui.addToolTip(tooltip); + } else { + gui.removeToolTip(tooltip); + } + this.mimicSlot = mimicSlot; + } + return this; + } + + public boolean getMimicSlot() { + return mimicSlot; + } + + public GT_GuiIcon getBgIcon() { + return bgIcon; + } + + public GT_GuiFakeItemButton setBgIcon(GT_GuiIcon bgIcon) { + this.bgIcon = bgIcon; + return this; + } + @Override public void onInit() { - xPosition = x0 + gui.getGuiLeft(); - yPosition = y0 + gui.getGuiTop(); + xPosition = rectangle.x + gui.getGuiLeft(); + yPosition = rectangle.y + gui.getGuiTop(); + } + + @Override + public void onRemoved() { + if (mimicSlot) + gui.removeToolTip(tooltip); } @Override @@ -55,10 +111,36 @@ public class GT_GuiFakeItemButton implements IGuiScreen.IGuiElement { if (item != null) gui.getItemRenderer().renderItemAndEffectIntoGUI(gui.getFontRenderer(), Minecraft.getMinecraft().getTextureManager(), item, xPosition, yPosition); + if (getMimicSlot()) + if (getBounds().contains(mouseX - gui.getGuiLeft(), mouseY - gui.getGuiTop())) { + GL11.glDisable(GL11.GL_LIGHTING); + GL11.glDisable(GL11.GL_DEPTH_TEST); + GL11.glColorMask(true, true, true, false); + GuiDraw.drawGradientRect(xPosition, yPosition, 16, 16, 0x80ffffff, 0x80ffffff); + GL11.glColorMask(true, true, true, true); + // no glEnable, state will be recovered by glPopAttrib + } + GL11.glPopAttrib(); } public Rectangle getBounds() { - return new Rectangle(x0, y0, width, height); + return rectangle; + } + + public void setX(int x) { + rectangle.x = x; + } + + public void setY(int y) { + rectangle.y = y; + } + + public void setWidth(int width) { + rectangle.width = width; + } + + public void setHeight(int height) { + rectangle.height = height; } } diff --git a/src/main/java/gregtech/api/gui/widgets/GT_GuiIcon.java b/src/main/java/gregtech/api/gui/widgets/GT_GuiIcon.java index 41e851054d..0b9231b40f 100644 --- a/src/main/java/gregtech/api/gui/widgets/GT_GuiIcon.java +++ b/src/main/java/gregtech/api/gui/widgets/GT_GuiIcon.java @@ -24,6 +24,8 @@ public enum GT_GuiIcon { IMPORT (0, 32, 32*2), ALLOW_INPUT (0, 32*2, 32*2), BLOCK_INPUT (0, 32*3, 32*2), + GREEN_ARROW_UP (0, 32*4, 32*2), + GREEN_ARROW_DOWN (0, 32*5, 32*2), SLOT_DARKGRAY (1, 176,0,18,18), SLOT_GRAY (1, 176,18,18,18); diff --git a/src/main/java/gregtech/api/gui/widgets/GT_GuiTooltip.java b/src/main/java/gregtech/api/gui/widgets/GT_GuiTooltip.java index d24437f018..6b9e70a71b 100644 --- a/src/main/java/gregtech/api/gui/widgets/GT_GuiTooltip.java +++ b/src/main/java/gregtech/api/gui/widgets/GT_GuiTooltip.java @@ -34,4 +34,12 @@ public class GT_GuiTooltip { public List<String> getToolTipText() { return text; } + + public Rectangle getBounds() { + return bounds; + } + + public boolean isDelayed() { + return true; + } } diff --git a/src/main/java/gregtech/api/gui/widgets/GT_GuiTooltipManager.java b/src/main/java/gregtech/api/gui/widgets/GT_GuiTooltipManager.java index d0e6964abc..c098e4e2a7 100644 --- a/src/main/java/gregtech/api/gui/widgets/GT_GuiTooltipManager.java +++ b/src/main/java/gregtech/api/gui/widgets/GT_GuiTooltipManager.java @@ -21,7 +21,7 @@ public class GT_GuiTooltipManager { private final List<GT_GuiTooltip> tips = new ArrayList<>(); public void addToolTip(GT_GuiTooltip tip) { - if (!tips.contains(tip)) tips.add(tip); + if (tip != null && !tips.contains(tip)) tips.add(tip); } public boolean removeToolTip(GT_GuiTooltip tip) { @@ -37,15 +37,14 @@ public class GT_GuiTooltipManager { lastMouseX = mouseX; lastMouseY = mouseY; - if (mouseStopped > DELAY) - mouseX -= render.getGuiLeft(); - mouseY -= render.getGuiTop(); - for (GT_GuiTooltip tip : tips) { - if(tip.enabled && tip.bounds.contains(mouseX, mouseY)){ - tip.updateText(); - drawTooltip(tip, mouseX, mouseY, render); - break; - } + mouseX -= render.getGuiLeft(); + mouseY -= render.getGuiTop(); + for (GT_GuiTooltip tip : tips) { + if (tip.enabled && (!tip.isDelayed() || mouseStopped > DELAY) && tip.getBounds().contains(mouseX, mouseY)) { + tip.updateText(); + drawTooltip(tip, mouseX, mouseY, render); + break; + } } } |