From ae0abaf819d83fb024eee7776aacc62ea1817750 Mon Sep 17 00:00:00 2001 From: tterrag1098 Date: Thu, 12 Feb 2015 18:24:17 -0500 Subject: [PATCH] Fix remaining text box issues, add text box filters --- .../crazypants/enderio/gui/TextFieldEIO.java | 69 +++++++++++++++++++ .../enderio/machine/buffer/GuiBuffer.java | 14 ++-- .../enderio/machine/capbank/GuiCapBank.java | 21 +++--- .../machine/hypercube/GuiHyperCube.java | 7 +- .../machine/monitor/GuiPowerMonitor.java | 23 +++++-- .../enderio/teleport/GuiTravelAccessable.java | 9 ++- .../enderio/teleport/telepad/GuiTelePad.java | 17 +++-- .../java/crazypants/gui/GuiContainerBase.java | 9 ++- 8 files changed, 132 insertions(+), 37 deletions(-) create mode 100644 src/main/java/crazypants/enderio/gui/TextFieldEIO.java diff --git a/src/main/java/crazypants/enderio/gui/TextFieldEIO.java b/src/main/java/crazypants/enderio/gui/TextFieldEIO.java new file mode 100644 index 00000000..2d2ea1bf --- /dev/null +++ b/src/main/java/crazypants/enderio/gui/TextFieldEIO.java @@ -0,0 +1,69 @@ +package crazypants.enderio.gui; + +import crazypants.gui.IGuiScreen; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.GuiTextField; + +public class TextFieldEIO extends GuiTextField { + + public interface ICharFilter { + + boolean passesFilter(char c); + } + + public static final ICharFilter FILTER_NUMERIC = new ICharFilter() { + @Override + public boolean passesFilter(char c) { + return Character.isDigit(c); + } + }; + + public static final ICharFilter FILTER_COORDS = new ICharFilter() { + @Override + public boolean passesFilter(char c) { + return c == '-' || FILTER_NUMERIC.passesFilter(c); + } + }; + + public static ICharFilter FILTER_ALPHABETICAL = new ICharFilter() { + @Override + public boolean passesFilter(char c) { + return Character.isAlphabetic(c); + } + }; + + public static ICharFilter FILTER_ALPHANUMERIC = new ICharFilter() { + @Override + public boolean passesFilter(char c) { + return FILTER_NUMERIC.passesFilter(c) || FILTER_ALPHABETICAL.passesFilter(c); + } + }; + + private final int xOrigin; + private final int yOrigin; + private ICharFilter filter; + + public TextFieldEIO(FontRenderer fnt, int x, int y, int width, int height) { + super(fnt, x, y, width, height); + this.xOrigin = x; + this.yOrigin = y; + } + + public void init(IGuiScreen gui) { + this.xPosition = xOrigin + gui.getGuiLeft(); + this.yPosition = yOrigin + gui.getGuiTop(); + } + + public TextFieldEIO setCharFilter(ICharFilter filter) { + this.filter = filter; + return this; + } + + @Override + public boolean textboxKeyTyped(char c, int p_146201_2_) { + if(filter == null || filter.passesFilter(c) || c == '\b') { + return super.textboxKeyTyped(c, p_146201_2_); + } + return false; + } +} diff --git a/src/main/java/crazypants/enderio/machine/buffer/GuiBuffer.java b/src/main/java/crazypants/enderio/machine/buffer/GuiBuffer.java index 986cec17..b33d008d 100644 --- a/src/main/java/crazypants/enderio/machine/buffer/GuiBuffer.java +++ b/src/main/java/crazypants/enderio/machine/buffer/GuiBuffer.java @@ -2,12 +2,12 @@ package crazypants.enderio.machine.buffer; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; -import net.minecraft.client.gui.GuiTextField; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Slot; import org.lwjgl.opengl.GL11; +import crazypants.enderio.gui.TextFieldEIO; import crazypants.enderio.machine.IoMode; import crazypants.enderio.machine.gui.GuiPoweredMachineBase; import crazypants.enderio.machine.power.PowerDisplayUtil; @@ -20,8 +20,8 @@ public class GuiBuffer extends GuiPoweredMachineBase { private static final String TEXTURE_SIMPLE = "enderio:textures/gui/buffer.png"; private static final String TEXTURE_FULL = "enderio:textures/gui/buffer_full.png"; - private GuiTextField maxInput; - private GuiTextField maxOutput; + private TextFieldEIO maxInput; + private TextFieldEIO maxOutput; private int lastInput, lastOutput; @@ -36,9 +36,12 @@ public class GuiBuffer extends GuiPoweredMachineBase { int x = (isFull() ? 20 : 58); int y = guiTop + 27; - maxInput = new GuiTextField(getFontRenderer(), x, y, 60, 12); + maxInput = new TextFieldEIO(getFontRenderer(), x, y, 60, 12); y += 28; - maxOutput = new GuiTextField(getFontRenderer(), x, y, 60, 12); + maxOutput = new TextFieldEIO(getFontRenderer(), x, y, 60, 12); + + textFields.add(maxInput); + textFields.add(maxOutput); } } @@ -128,7 +131,6 @@ public class GuiBuffer extends GuiPoweredMachineBase { @Override protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) { - super.drawGuiContainerBackgroundLayer(par1, par2, par3); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); diff --git a/src/main/java/crazypants/enderio/machine/capbank/GuiCapBank.java b/src/main/java/crazypants/enderio/machine/capbank/GuiCapBank.java index aa27591f..15c90528 100644 --- a/src/main/java/crazypants/enderio/machine/capbank/GuiCapBank.java +++ b/src/main/java/crazypants/enderio/machine/capbank/GuiCapBank.java @@ -7,7 +7,6 @@ import java.util.List; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiButton; -import net.minecraft.client.gui.GuiTextField; import net.minecraft.entity.Entity; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.util.EnumChatFormatting; @@ -16,6 +15,7 @@ import org.lwjgl.opengl.GL11; import crazypants.enderio.EnderIO; import crazypants.enderio.gui.RedstoneModeButton; +import crazypants.enderio.gui.TextFieldEIO; import crazypants.enderio.machine.IRedstoneModeControlable; import crazypants.enderio.machine.IoMode; import crazypants.enderio.machine.RedstoneControlMode; @@ -65,8 +65,8 @@ public class GuiCapBank extends GuiContainerBase { private RedstoneModeButton inputRsButton; private RedstoneModeButton outputRsButton; - private GuiTextField maxInputTF; - private GuiTextField maxOutputTF; + private TextFieldEIO maxInputTF; + private TextFieldEIO maxOutputTF; private final GuiOverlayIoConfig configOverlay; private final GuiButtonIoConfig configB; @@ -172,19 +172,22 @@ public class GuiCapBank extends GuiContainerBase { FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer; - x = inputX; + x = inputX - 24; y = inputY; - maxInputTF = new GuiTextField(fontRenderer, x, y, 68, 16); + maxInputTF = new TextFieldEIO(fontRenderer, x, y, 68, 16); maxInputTF.setCanLoseFocus(true); maxInputTF.setMaxStringLength(10); maxInputTF.setFocused(false); - x = outputX; + x = outputX - 24; y = outputY; - maxOutputTF = new GuiTextField(fontRenderer, x, y, 68, 16); + maxOutputTF = new TextFieldEIO(fontRenderer, x, y, 68, 16); maxOutputTF.setCanLoseFocus(true); maxOutputTF.setMaxStringLength(10); maxOutputTF.setFocused(true); + + textFields.add(maxInputTF); + textFields.add(maxOutputTF); } @Override @@ -241,7 +244,6 @@ public class GuiCapBank extends GuiContainerBase { @Override protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) { - super.drawGuiContainerBackgroundLayer(par1, par2, par3); requestStateUpdate(); @@ -282,7 +284,8 @@ public class GuiCapBank extends GuiContainerBase { x = guiLeft + outputX - swid - 3; y = guiTop + outputY + 2; drawString(fontRenderer, str, x, y, -1); - + + super.drawGuiContainerBackgroundLayer(par1, par2, par3); } @Override diff --git a/src/main/java/crazypants/enderio/machine/hypercube/GuiHyperCube.java b/src/main/java/crazypants/enderio/machine/hypercube/GuiHyperCube.java index 9036bf68..f9fd6896 100644 --- a/src/main/java/crazypants/enderio/machine/hypercube/GuiHyperCube.java +++ b/src/main/java/crazypants/enderio/machine/hypercube/GuiHyperCube.java @@ -7,7 +7,6 @@ import java.util.List; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiButton; -import net.minecraft.client.gui.GuiTextField; import org.lwjgl.opengl.GL11; @@ -15,6 +14,7 @@ import crazypants.enderio.gui.IGuiOverlay; import crazypants.enderio.gui.IconButtonEIO; import crazypants.enderio.gui.IconEIO; import crazypants.enderio.gui.RedstoneModeButton; +import crazypants.enderio.gui.TextFieldEIO; import crazypants.enderio.gui.ToggleButtonEIO; import crazypants.enderio.machine.hypercube.TileHyperCube.IoMode; import crazypants.enderio.machine.hypercube.TileHyperCube.SubChannel; @@ -58,7 +58,7 @@ public class GuiHyperCube extends GuiContainerBase { private IconButtonEIO addButton; private ToggleButtonEIO privateButton; - private GuiTextField newChannelTF; + private TextFieldEIO newChannelTF; private GuiChannelList publicChannelList; private GuiChannelList privateChannelList; @@ -167,10 +167,11 @@ public class GuiHyperCube extends GuiContainerBase { y = 12; x = 8; - newChannelTF = new GuiTextField(Minecraft.getMinecraft().fontRenderer, x, y, 103, 16); + newChannelTF = new TextFieldEIO(Minecraft.getMinecraft().fontRenderer, x, y, 103, 16); newChannelTF.setCanLoseFocus(false); newChannelTF.setMaxStringLength(32); newChannelTF.setFocused(true); + textFields.add(newChannelTF); } private void updateIoButtons() { diff --git a/src/main/java/crazypants/enderio/machine/monitor/GuiPowerMonitor.java b/src/main/java/crazypants/enderio/machine/monitor/GuiPowerMonitor.java index 1408d625..e7ccaa52 100644 --- a/src/main/java/crazypants/enderio/machine/monitor/GuiPowerMonitor.java +++ b/src/main/java/crazypants/enderio/machine/monitor/GuiPowerMonitor.java @@ -13,6 +13,7 @@ import net.minecraft.client.gui.GuiTextField; import org.lwjgl.opengl.GL11; import crazypants.enderio.gui.CheckBoxEIO; +import crazypants.enderio.gui.TextFieldEIO; import crazypants.enderio.machine.power.PowerDisplayUtil; import crazypants.enderio.network.PacketHandler; import crazypants.gui.GuiContainerBase; @@ -46,8 +47,8 @@ public class GuiPowerMonitor extends GuiContainerBase { private CheckBoxEIO enabledB; - private GuiTextField startTF; - private GuiTextField endTF; + private TextFieldEIO startTF; + private TextFieldEIO endTF; private String titleStr; @@ -105,19 +106,24 @@ public class GuiPowerMonitor extends GuiContainerBase { x = MARGIN + getFontRenderer().getStringWidth(engineTxt2) + 4; int y = MARGIN + ICON_SIZE + ICON_SIZE + getFontRenderer().FONT_HEIGHT; - startTF = new GuiTextField(getFontRenderer(), x, y, 28, 14); + startTF = new TextFieldEIO(getFontRenderer(), x, y, 28, 14); startTF.setCanLoseFocus(true); startTF.setMaxStringLength(3); startTF.setFocused(true); + startTF.setVisible(false); startTF.setText(INT_NF.format(te.asPercentInt(te.startLevel))); y = y + getFontRenderer().FONT_HEIGHT + ICON_SIZE + ICON_SIZE + 4; - x = MARGIN + getFontRenderer().getStringWidth(engineTxt5); - endTF = new GuiTextField(getFontRenderer(), x, y, 28, 14); + x = 5 + MARGIN + getFontRenderer().getStringWidth(engineTxt5); + endTF = new TextFieldEIO(getFontRenderer(), x, y, 28, 14); endTF.setCanLoseFocus(true); endTF.setMaxStringLength(3); endTF.setFocused(false); + endTF.setVisible(false); endTF.setText(INT_NF.format(te.asPercentInt(te.stopLevel))); + + textFields.add(startTF); + textFields.add(endTF); } @Override @@ -148,9 +154,13 @@ public class GuiPowerMonitor extends GuiContainerBase { if(y > 9 && y < 27) { isRedstoneMode = false; enabledB.detach(); + startTF.setVisible(false); + endTF.setVisible(false); } else if(y > 34 && y < 53) { isRedstoneMode = true; enabledB.onGuiInit(); + startTF.setVisible(true); + endTF.setVisible(true); } } @@ -159,7 +169,6 @@ public class GuiPowerMonitor extends GuiContainerBase { @Override protected void drawGuiContainerBackgroundLayer(float ptick, int mouseX, int mouseY) { - super.drawGuiContainerBackgroundLayer(ptick, mouseX, mouseY); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); RenderUtil.bindTexture("enderio:textures/gui/powerMonitor.png"); int sx = (width - xSize) / 2; @@ -177,7 +186,7 @@ public class GuiPowerMonitor extends GuiContainerBase { } checkForModifications(); - + super.drawGuiContainerBackgroundLayer(ptick, mouseX, mouseY); } private void checkForModifications() { diff --git a/src/main/java/crazypants/enderio/teleport/GuiTravelAccessable.java b/src/main/java/crazypants/enderio/teleport/GuiTravelAccessable.java index 8a9462f3..33201c8a 100644 --- a/src/main/java/crazypants/enderio/teleport/GuiTravelAccessable.java +++ b/src/main/java/crazypants/enderio/teleport/GuiTravelAccessable.java @@ -5,7 +5,6 @@ import java.awt.Color; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiButton; -import net.minecraft.client.gui.GuiTextField; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.world.World; @@ -15,6 +14,7 @@ import crazypants.enderio.api.teleport.ITravelAccessable; import crazypants.enderio.api.teleport.ITravelAccessable.AccessMode; import crazypants.enderio.gui.CheckBoxEIO; import crazypants.enderio.gui.IGuiOverlay; +import crazypants.enderio.gui.TextFieldEIO; import crazypants.enderio.network.PacketHandler; import crazypants.enderio.teleport.packet.PacketAccessMode; import crazypants.enderio.teleport.packet.PacketLabel; @@ -34,7 +34,7 @@ public class GuiTravelAccessable extends GuiContainerBase { private CheckBoxEIO privateCB; private CheckBoxEIO protectedCB; - private GuiTextField tf; + private TextFieldEIO tf; private String publicStr; private String privateStr; @@ -62,7 +62,7 @@ public class GuiTravelAccessable extends GuiContainerBase { FontRenderer fr = Minecraft.getMinecraft().fontRenderer; - tf = new GuiTextField(fr, 7, 12, 90, 16); + tf = new TextFieldEIO(fr, 28, 10, 90, 16); col1x = 88; col0x = (col1x - fr.getStringWidth(protectedStr) / 2) / 2; @@ -85,6 +85,8 @@ public class GuiTravelAccessable extends GuiContainerBase { publicCB.setSelected(te.getAccessMode() == AccessMode.PUBLIC); ySize = 185; + + textFields.add(tf); } @Override @@ -167,6 +169,7 @@ public class GuiTravelAccessable extends GuiContainerBase { fontRenderer.drawStringWithShadow(publicStr, x, y, col); checkLabelForChange(); + super.drawGuiContainerBackgroundLayer(f, i, j); } private void checkLabelForChange() { diff --git a/src/main/java/crazypants/enderio/teleport/telepad/GuiTelePad.java b/src/main/java/crazypants/enderio/teleport/telepad/GuiTelePad.java index c50bc183..6963cf5a 100644 --- a/src/main/java/crazypants/enderio/teleport/telepad/GuiTelePad.java +++ b/src/main/java/crazypants/enderio/teleport/telepad/GuiTelePad.java @@ -13,6 +13,7 @@ import com.google.common.collect.Lists; import crazypants.enderio.EnderIO; import crazypants.enderio.GuiHandler; import crazypants.enderio.gui.IconEIO; +import crazypants.enderio.gui.TextFieldEIO; import crazypants.enderio.network.PacketHandler; import crazypants.gui.GuiContainerBase; import crazypants.render.RenderUtil; @@ -27,7 +28,7 @@ public class GuiTelePad extends GuiContainerBase implements IToggleableGui { private World world; private TileTelePad te; - private GuiTextField x, y, z; + private TextFieldEIO x, y, z; public GuiTelePad(InventoryPlayer playerInv, TileTelePad te, World world) { super(new ContainerTelePad(playerInv)); @@ -36,9 +37,9 @@ public class GuiTelePad extends GuiContainerBase implements IToggleableGui { FontRenderer fr = Minecraft.getMinecraft().fontRenderer; - x = new GuiTextField(fr, 7, 12, 90, 16); - y = new GuiTextField(fr, 7, 32, 90, 16); - z = new GuiTextField(fr, 7, 52, 90, 16); + x = new TextFieldEIO(fr, 20, 10, 120, 16).setCharFilter(TextFieldEIO.FILTER_COORDS); + y = new TextFieldEIO(fr, 20, 35, 120, 16).setCharFilter(TextFieldEIO.FILTER_COORDS); + z = new TextFieldEIO(fr, 20, 60, 120, 16).setCharFilter(TextFieldEIO.FILTER_COORDS); textFields.addAll(Lists.newArrayList(x, y, z)); @@ -69,6 +70,14 @@ public class GuiTelePad extends GuiContainerBase implements IToggleableGui { drawTexturedModalRect(sx, sy, 0, 0, this.xSize, this.ySize); + FontRenderer fnt = getFontRenderer(); + + String[] text = { "X", "Y", "Z" }; + for (int i = 0; i < text.length; i++) { + GuiTextField f = textFields.get(i); + fnt.drawString(text[i], f.xPosition - (fnt.getStringWidth(text[i]) / 2) - 5, f.yPosition + ((f.height - fnt.FONT_HEIGHT) / 2), 0x000000); + } + super.drawGuiContainerBackgroundLayer(p_146976_1_, p_146976_2_, p_146976_3_); } diff --git a/src/main/java/crazypants/gui/GuiContainerBase.java b/src/main/java/crazypants/gui/GuiContainerBase.java index 633aaf00..17f8a783 100644 --- a/src/main/java/crazypants/gui/GuiContainerBase.java +++ b/src/main/java/crazypants/gui/GuiContainerBase.java @@ -26,6 +26,7 @@ import codechicken.nei.api.INEIGuiHandler; import codechicken.nei.api.TaggedInventoryArea; import cpw.mods.fml.common.Optional; import crazypants.enderio.gui.IGuiOverlay; +import crazypants.enderio.gui.TextFieldEIO; import crazypants.gui.ToolTipManager.ToolTipRenderer; import crazypants.render.RenderUtil; @@ -36,7 +37,7 @@ public abstract class GuiContainerBase extends GuiContainer implements ToolTipRe protected ToolTipManager ttMan = new ToolTipManager(); protected List overlays = new ArrayList(); - protected List textFields = Lists.newArrayList(); + protected List textFields = Lists.newArrayList(); protected GuiContainerBase(Container par1Container) { super(par1Container); @@ -48,10 +49,8 @@ public abstract class GuiContainerBase extends GuiContainer implements ToolTipRe for (IGuiOverlay overlay : overlays) { overlay.init(this); } - for (GuiTextField f : textFields) { - f.xPosition += getGuiLeft(); - f.yPosition += getGuiTop(); - f.setFocused(f == textFields.get(0)); + for (TextFieldEIO f : textFields) { + f.init(this); } }