2017-04-10 16:20:39 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Industrial Wires.
|
|
|
|
* Copyright (C) 2016-2017 malte0811
|
|
|
|
*
|
|
|
|
* Industrial Wires is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Industrial Wires is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Industrial Wires. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package malte0811.industrialWires.client.gui;
|
|
|
|
|
2017-04-17 19:35:14 +02:00
|
|
|
import blusunrize.immersiveengineering.client.ClientUtils;
|
|
|
|
import com.google.common.collect.ImmutableList;
|
2017-04-13 17:14:05 +02:00
|
|
|
import malte0811.industrialWires.IndustrialWires;
|
2017-04-10 16:20:39 +02:00
|
|
|
import malte0811.industrialWires.blocks.controlpanel.TileEntityPanelCreator;
|
|
|
|
import malte0811.industrialWires.containers.ContainerPanelCreator;
|
2017-04-13 17:14:05 +02:00
|
|
|
import malte0811.industrialWires.controlpanel.MessageType;
|
2017-04-17 19:35:14 +02:00
|
|
|
import malte0811.industrialWires.controlpanel.PanelComponent;
|
2017-04-27 18:15:11 +02:00
|
|
|
import malte0811.industrialWires.items.ItemPanelComponent;
|
2017-04-13 17:14:05 +02:00
|
|
|
import malte0811.industrialWires.network.MessageGUIInteract;
|
|
|
|
import net.minecraft.client.gui.Gui;
|
|
|
|
import net.minecraft.client.gui.GuiButton;
|
2017-04-10 16:20:39 +02:00
|
|
|
import net.minecraft.client.gui.inventory.GuiContainer;
|
2017-04-12 15:55:37 +02:00
|
|
|
import net.minecraft.client.renderer.GlStateManager;
|
2017-04-17 19:35:14 +02:00
|
|
|
import net.minecraft.client.resources.I18n;
|
2017-04-10 16:20:39 +02:00
|
|
|
import net.minecraft.entity.player.InventoryPlayer;
|
2017-04-13 17:14:05 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2017-04-17 19:35:14 +02:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2017-04-13 17:14:05 +02:00
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
2017-04-12 15:55:37 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2017-04-27 18:15:11 +02:00
|
|
|
import java.util.List;
|
2017-04-10 16:20:39 +02:00
|
|
|
|
|
|
|
public class GuiPanelCreator extends GuiContainer {
|
2017-04-12 15:55:37 +02:00
|
|
|
public int panelSize = 128;
|
|
|
|
private ContainerPanelCreator container;
|
2017-04-17 19:35:14 +02:00
|
|
|
private boolean snapToGrid = false;
|
|
|
|
private ResourceLocation textureLoc = new ResourceLocation(IndustrialWires.MODID, "textures/gui/panel_creator.png");
|
2017-04-12 15:55:37 +02:00
|
|
|
|
2017-04-10 16:20:39 +02:00
|
|
|
public GuiPanelCreator(InventoryPlayer ip, TileEntityPanelCreator te) {
|
|
|
|
super(new ContainerPanelCreator(ip, te));
|
2017-04-12 15:55:37 +02:00
|
|
|
container = (ContainerPanelCreator) inventorySlots;
|
2017-04-17 19:35:14 +02:00
|
|
|
ySize = 231;
|
2017-04-10 16:20:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
|
2017-04-13 17:14:05 +02:00
|
|
|
//TODO proper background
|
2017-04-17 19:35:14 +02:00
|
|
|
textureLoc = new ResourceLocation(IndustrialWires.MODID, "textures/gui/panel_creator.png");
|
|
|
|
GlStateManager.color(1,1,1,1);
|
|
|
|
mc.getTextureManager().bindTexture(textureLoc);
|
|
|
|
this.drawTexturedModalRect(guiLeft,guiTop, 0, 0, xSize, ySize);
|
2017-04-12 15:55:37 +02:00
|
|
|
int x0 = getX0();
|
|
|
|
int y0 = getY0();
|
2017-04-13 17:14:05 +02:00
|
|
|
int xRel = mouseX - x0;
|
|
|
|
int yRel = mouseY - y0;
|
2017-04-17 19:35:14 +02:00
|
|
|
if (snapToGrid) {
|
|
|
|
xRel = (int) Math.floor(xRel*16/panelSize)*panelSize/16;
|
|
|
|
yRel = (int) Math.floor(yRel*16/panelSize)*panelSize/16;
|
|
|
|
}
|
2017-04-22 18:14:49 +02:00
|
|
|
for (PanelComponent pc : container.tile.components) {
|
|
|
|
drawPanelComponent(pc, -1, -1);
|
|
|
|
}
|
2017-04-13 17:14:05 +02:00
|
|
|
PanelComponent curr = getFloatingPC();
|
|
|
|
if (curr!=null && 0 <= xRel && xRel <= panelSize && 0 <= yRel && yRel <= panelSize) {
|
|
|
|
drawPanelComponent(curr, xRel, yRel);
|
|
|
|
}
|
|
|
|
}
|
2017-04-10 16:20:39 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
|
|
|
|
super.drawScreen(mouseX, mouseY, partialTicks);
|
2017-04-17 19:35:14 +02:00
|
|
|
String tooltip = null;
|
|
|
|
if (buttonList.get(0).isMouseOver()) {
|
|
|
|
tooltip = I18n.format(IndustrialWires.MODID+".desc.create_panel");
|
|
|
|
ClientUtils.drawHoveringText(ImmutableList.of("Create a new panel"), mouseX, mouseY, mc.fontRendererObj);
|
|
|
|
} else if (buttonList.get(1).isMouseOver()) {
|
|
|
|
tooltip = I18n.format(IndustrialWires.MODID+".desc.remove_all");
|
|
|
|
} else if (buttonList.get(2).isMouseOver()) {
|
|
|
|
if (snapToGrid) {
|
|
|
|
tooltip = I18n.format(IndustrialWires.MODID+".desc.disable_snap");
|
|
|
|
} else {
|
|
|
|
tooltip = I18n.format(IndustrialWires.MODID+".desc.enable_snap");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tooltip!=null) {
|
|
|
|
ClientUtils.drawHoveringText(ImmutableList.of(tooltip), mouseX, mouseY, mc.fontRendererObj);
|
|
|
|
}
|
2017-04-12 15:55:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void drawPanelComponent(PanelComponent pc, int x, int y) {
|
2017-04-13 17:14:05 +02:00
|
|
|
if (x >= 0 && y >= 0) {
|
2017-04-12 15:55:37 +02:00
|
|
|
pc.setX(x / (float) panelSize);
|
|
|
|
pc.setY(y / (float) panelSize);
|
|
|
|
}
|
2017-04-23 17:23:10 +02:00
|
|
|
if (!pc.isValidPos(container.tile.components)) {
|
2017-04-13 17:14:05 +02:00
|
|
|
AxisAlignedBB aabb = pc.getBlockRelativeAABB();
|
|
|
|
int left = (int) (getX0()+aabb.minX*panelSize)-1;
|
|
|
|
int top = (int) (getY0()+aabb.minZ*panelSize)-1;
|
|
|
|
int right = (int) (getX0()+aabb.maxX*panelSize)+1;
|
|
|
|
int bottom = (int) (getY0()+aabb.maxZ*panelSize)+1;
|
|
|
|
Gui.drawRect(left, top, right, bottom, 0xffff0000);
|
2017-04-12 15:55:37 +02:00
|
|
|
}
|
|
|
|
pc.renderInGUI(this);
|
|
|
|
GlStateManager.color(1, 1, 1);
|
2017-04-10 16:20:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void initGui() {
|
|
|
|
super.initGui();
|
2017-04-13 17:14:05 +02:00
|
|
|
buttonList.clear();
|
2017-04-17 19:35:14 +02:00
|
|
|
int buttonTop = guiTop+62;
|
|
|
|
buttonList.add(new GuiButton(0, guiLeft+2, buttonTop, 20, 20, "C"));
|
|
|
|
buttonList.add(new GuiButton(1, guiLeft+2, buttonTop+22, 20, 20, "D"));
|
|
|
|
buttonList.add(new GuiButton(2, guiLeft+2, buttonTop+44, 20, 20, "S"));
|
2017-04-10 16:20:39 +02:00
|
|
|
}
|
2017-04-12 15:55:37 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
|
|
|
|
super.mouseClicked(mouseX, mouseY, mouseButton);
|
|
|
|
int x0 = getX0();
|
|
|
|
int y0 = getY0();
|
|
|
|
int xRel = mouseX - x0;
|
|
|
|
int yRel = mouseY - y0;
|
2017-04-13 17:14:05 +02:00
|
|
|
PanelComponent curr = getFloatingPC();
|
2017-04-27 18:15:11 +02:00
|
|
|
if (0 <= xRel && xRel <= panelSize && 0 <= yRel && yRel <= panelSize) {
|
|
|
|
List<PanelComponent> components = container.tile.components;
|
|
|
|
if (curr != null) {
|
|
|
|
if (curr.isValidPos(components)) {
|
|
|
|
NBTTagCompound nbt = new NBTTagCompound();
|
|
|
|
nbt.setFloat("x", curr.getX());
|
|
|
|
nbt.setFloat("y", curr.getY());
|
|
|
|
nbt.setInteger("type", MessageType.ADD.ordinal());
|
|
|
|
IndustrialWires.packetHandler.sendToServer(new MessageGUIInteract(container.tile, nbt));
|
|
|
|
components.add(curr.copyOf());
|
|
|
|
ItemStack currStack = mc.thePlayer.inventory.getItemStack();
|
|
|
|
if (currStack != null) {
|
|
|
|
currStack.stackSize--;
|
|
|
|
if (currStack.stackSize <= 0) {
|
|
|
|
mc.thePlayer.inventory.setItemStack(null);
|
2017-04-13 17:14:05 +02:00
|
|
|
|
2017-04-27 18:15:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (mc.thePlayer.inventory.getItemStack()==null) {
|
|
|
|
float xRelFloat = xRel/(float) panelSize;
|
|
|
|
float yRelFloat = yRel/(float) panelSize;
|
|
|
|
for (int i = 0;i<components.size();i++) {
|
|
|
|
PanelComponent pc = components.get(i);
|
|
|
|
AxisAlignedBB aabb = pc.getBlockRelativeAABB();
|
|
|
|
if (aabb.minX<=xRelFloat&&aabb.maxX>xRelFloat&&aabb.minZ<=yRelFloat&&aabb.maxZ>yRelFloat) {
|
|
|
|
PanelComponent removed = components.get(i);
|
|
|
|
ItemStack remItem = ItemPanelComponent.stackFromComponent(removed);
|
|
|
|
mc.thePlayer.inventory.setItemStack(remItem);
|
|
|
|
NBTTagCompound nbt = new NBTTagCompound();
|
|
|
|
nbt.setInteger("type", MessageType.REMOVE.ordinal());
|
|
|
|
nbt.setInteger("id", i);
|
|
|
|
IndustrialWires.packetHandler.sendToServer(new MessageGUIInteract(container.tile, nbt));
|
|
|
|
break;
|
2017-04-13 17:14:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-12 15:55:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getX0() {
|
2017-04-17 19:35:14 +02:00
|
|
|
return 30+guiLeft;
|
2017-04-12 15:55:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getY0() {
|
2017-04-17 19:35:14 +02:00
|
|
|
return 6+guiTop;
|
2017-04-12 15:55:37 +02:00
|
|
|
}
|
|
|
|
|
2017-04-13 17:14:05 +02:00
|
|
|
@Override
|
|
|
|
protected void actionPerformed(GuiButton button) throws IOException {
|
|
|
|
super.actionPerformed(button);
|
|
|
|
NBTTagCompound nbt = new NBTTagCompound();
|
|
|
|
switch (button.id) {
|
|
|
|
case 0://create panel
|
|
|
|
nbt.setInteger("type", MessageType.CREATE_PANEL.ordinal());
|
|
|
|
break;
|
|
|
|
case 1:// Delete all
|
|
|
|
nbt.setInteger("type", MessageType.REMOVE_ALL.ordinal());
|
|
|
|
break;
|
2017-04-17 19:35:14 +02:00
|
|
|
case 2:
|
|
|
|
snapToGrid = !snapToGrid;
|
|
|
|
break;
|
2017-04-13 17:14:05 +02:00
|
|
|
}
|
|
|
|
if (!nbt.hasNoTags()) {
|
|
|
|
IndustrialWires.packetHandler.sendToServer(new MessageGUIInteract(container.tile, nbt));
|
|
|
|
}
|
2017-04-12 15:55:37 +02:00
|
|
|
}
|
2017-04-13 17:14:05 +02:00
|
|
|
private ItemStack lastFloating;
|
|
|
|
private PanelComponent lastFloatingPC;
|
|
|
|
private PanelComponent getFloatingPC() {
|
|
|
|
ItemStack floating = mc.thePlayer.inventory.getItemStack();
|
|
|
|
if (floating==null||floating.getItem()!=IndustrialWires.panelComponent) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (ItemStack.areItemStacksEqual(floating, lastFloating)) {
|
|
|
|
return lastFloatingPC;
|
|
|
|
}
|
|
|
|
lastFloating = floating.copy();
|
2017-04-27 18:15:11 +02:00
|
|
|
lastFloatingPC = ItemPanelComponent.componentFromStack(floating);
|
2017-04-13 17:14:05 +02:00
|
|
|
return lastFloatingPC;
|
2017-04-12 15:55:37 +02:00
|
|
|
}
|
2017-04-13 17:14:05 +02:00
|
|
|
}
|