diff options
Diffstat (limited to 'src/Java/gtPlusPlus/core/gui')
-rw-r--r-- | src/Java/gtPlusPlus/core/gui/machine/GUI_VolumetricFlaskSetter.java | 106 | ||||
-rw-r--r-- | src/Java/gtPlusPlus/core/gui/widget/GuiValueField.java | 86 |
2 files changed, 142 insertions, 50 deletions
diff --git a/src/Java/gtPlusPlus/core/gui/machine/GUI_VolumetricFlaskSetter.java b/src/Java/gtPlusPlus/core/gui/machine/GUI_VolumetricFlaskSetter.java index 6bdf8f2ef5..8df6a8a18e 100644 --- a/src/Java/gtPlusPlus/core/gui/machine/GUI_VolumetricFlaskSetter.java +++ b/src/Java/gtPlusPlus/core/gui/machine/GUI_VolumetricFlaskSetter.java @@ -5,8 +5,8 @@ import org.lwjgl.opengl.GL11; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; -import gtPlusPlus.api.objects.Logger; import gtPlusPlus.core.container.Container_VolumetricFlaskSetter; +import gtPlusPlus.core.gui.widget.GuiValueField; import gtPlusPlus.core.handler.PacketHandler; import gtPlusPlus.core.lib.CORE; import gtPlusPlus.core.network.packet.Packet_VolumetricFlaskGui; @@ -14,64 +14,56 @@ import gtPlusPlus.core.tileentities.general.TileEntityVolumetricFlaskSetter; import net.minecraft.client.gui.GuiTextField; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.resources.I18n; -import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.util.ResourceLocation; @SideOnly(Side.CLIENT) public class GUI_VolumetricFlaskSetter extends GuiContainer { - private GuiTextField text; - private TileEntityVolumetricFlaskSetter aTile; - private static final ResourceLocation craftingTableGuiTextures = new ResourceLocation(CORE.MODID, "textures/gui/VolumetricFlaskSetter.png"); + private GuiTextField mText; + private TileEntityVolumetricFlaskSetter mTile; + private Container_VolumetricFlaskSetter mContainer; + private static final ResourceLocation mGuiTextures = new ResourceLocation(CORE.MODID, "textures/gui/VolumetricFlaskSetter.png"); - public GUI_VolumetricFlaskSetter(final InventoryPlayer player_inventory, final TileEntityVolumetricFlaskSetter te){ - super(new Container_VolumetricFlaskSetter(player_inventory, te)); - aTile = te; - Logger.INFO("Tile Value: "+te.getCustomValue()); + public GUI_VolumetricFlaskSetter(Container_VolumetricFlaskSetter aContainer){ + super(aContainer); + mContainer = aContainer; + mTile = mContainer.mTileEntity; } public void initGui(){ super.initGui(); - this.text = new GuiTextField(this.fontRendererObj, this.width / 2 - 62, this.height/2-52, 106, 14); - text.setMaxStringLength(5); - if (aTile != null) { - Logger.INFO("Using Value from Tile: "+aTile.getCustomValue()); - text.setText(""+aTile.getCustomValue()); - } - else { - Logger.INFO("Using default Value: 1000"); - text.setText("1000"); - } - this.text.setFocused(true); + this.mText = new GuiValueField(this.fontRendererObj, 26, 31, this.width / 2 - 62, this.height/2-52, 106, 14); + mText.setMaxStringLength(5); + mText.setText("0"); + mText.setFocused(true); } protected void keyTyped(char par1, int par2){ if (!isNumber(par1) && par2 != Keyboard.KEY_BACK && par2 != Keyboard.KEY_RETURN) { - text.setFocused(false); + mText.setFocused(false); super.keyTyped(par1, par2); } else { if (par2 == Keyboard.KEY_RETURN) { - if (text.isFocused()) { - Logger.INFO("Removing Focus."); - text.setFocused(false); + if (mText.isFocused()) { + mText.setFocused(false); } } else if (par2 == Keyboard.KEY_BACK) { String aCurrentText = getText(); if (aCurrentText.length() > 0) { - this.text.setText(aCurrentText.substring(0, aCurrentText.length() - 1)); + this.mText.setText(aCurrentText.substring(0, aCurrentText.length() - 1)); if (getText().length() <= 0) { - this.text.setText("0"); + this.mText.setText("0"); } } } else { - if (this.text.getText().equals("0")) { - this.text.setText(""+par1); + if (this.mText.getText().equals("0")) { + this.mText.setText(""+par1); } else { - this.text.textboxKeyTyped(par1, par2); + this.mText.textboxKeyTyped(par1, par2); } } sendUpdateToServer(); @@ -80,30 +72,28 @@ public class GUI_VolumetricFlaskSetter extends GuiContainer { public void updateScreen(){ super.updateScreen(); - this.text.updateCursorCounter(); + this.mText.updateCursorCounter(); } public void drawScreen(int par1, int par2, float par3){ this.drawDefaultBackground(); super.drawScreen(par1, par2, par3); - this.text.drawTextBox(); + + } protected void mouseClicked(int x, int y, int btn) { super.mouseClicked(x, y, btn); - this.text.mouseClicked(x, y, btn); + this.mText.mouseClicked(x, y, btn); } - - - - @Override protected void drawGuiContainerForegroundLayer(final int i, final int j){ super.drawGuiContainerForegroundLayer(i, j); + this.mText.drawTextBox(); this.fontRendererObj.drawString(I18n.format("container.VolumetricFlaskSetter", new Object[0]), 4, 3, 4210752); int aYVal = 49; this.fontRendererObj.drawString(I18n.format("0 = 16l", new Object[0]), 8, aYVal, 4210752); @@ -114,12 +104,29 @@ public class GUI_VolumetricFlaskSetter extends GuiContainer { this.fontRendererObj.drawString(I18n.format("6 = 864l", new Object[0]), 64, aYVal, 4210752); this.fontRendererObj.drawString(I18n.format("3 = 432l", new Object[0]), 8, aYVal+=8, 4210752); this.fontRendererObj.drawString(I18n.format("-> = Custom", new Object[0]), 59, aYVal, 4210752); + + + // Check TextBox Value is correct + short aCustomValue = 0; + if (getText().length() > 0) { + try { + aCustomValue = Short.parseShort(getText()); + short aTileValue = ((Container_VolumetricFlaskSetter) mContainer).mCustomValue; + if (mContainer != null) { + if (aTileValue != aCustomValue){ + this.mText.setText(""+aTileValue); + } + } + } catch (NumberFormatException ex) { + + } + } } @Override protected void drawGuiContainerBackgroundLayer(final float f, final int i, final int j){ GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); - this.mc.renderEngine.bindTexture(craftingTableGuiTextures); + this.mc.renderEngine.bindTexture(mGuiTextures); final int x = (this.width - this.xSize) / 2; final int y = (this.height - this.ySize) / 2; this.drawTexturedModalRect(x, y, 0, 0, this.xSize, this.ySize); @@ -130,20 +137,19 @@ public class GUI_VolumetricFlaskSetter extends GuiContainer { } protected String getText() { - return this.text.getText(); + return this.mText.getText(); } - protected void sendUpdateToServer() { - Logger.INFO("Trying to send packet to server from GUI."); - int aCustomValue = 0; - if (getText().length() > 0) - try { - aCustomValue = Integer.parseInt(getText()); - Logger.INFO("Got Value: "+aCustomValue); - PacketHandler.sendToServer(new Packet_VolumetricFlaskGui(aTile, aCustomValue)); - } catch (NumberFormatException ex) { - - } - } + protected void sendUpdateToServer() { + short aCustomValue = 0; + if (getText().length() > 0) { + try { + aCustomValue = Short.parseShort(getText()); + PacketHandler.sendToServer(new Packet_VolumetricFlaskGui(mTile, aCustomValue)); + } catch (NumberFormatException ex) { + + } + } + } }
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/gui/widget/GuiValueField.java b/src/Java/gtPlusPlus/core/gui/widget/GuiValueField.java new file mode 100644 index 0000000000..ac4c1a8aee --- /dev/null +++ b/src/Java/gtPlusPlus/core/gui/widget/GuiValueField.java @@ -0,0 +1,86 @@ +package gtPlusPlus.core.gui.widget; + +import java.lang.reflect.Field; + +import gtPlusPlus.core.util.reflect.ReflectionUtils; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.GuiTextField; + +public class GuiValueField extends GuiTextField { + + private final FontRenderer mFontRenderer; + private final int mScreenLocationX; + private final int mScreenLocationY; + + public GuiValueField(FontRenderer aFontRenderer, int aX, int aY, int aScreenLocationX, int aScreenLocationY, int aWidth, int aHeight) { + super(aFontRenderer, aX, aY, aWidth, aHeight); + mFontRenderer = aFontRenderer; + mScreenLocationX = aScreenLocationX; + mScreenLocationY = aScreenLocationY; + } + + public boolean canLoseFocus() { + Field canLoseFocus = ReflectionUtils.getField(GuiTextField.class, "canLoseFocus"); + if (canLoseFocus != null) { + return (boolean) ReflectionUtils.getFieldValue(canLoseFocus, this); + } + return true; + } + + public boolean isFocused() { + Field isFocused = ReflectionUtils.getField(GuiTextField.class, "isFocused"); + if (isFocused != null) { + return (boolean) ReflectionUtils.getFieldValue(isFocused, this); + } + return false; + } + + public boolean isBackgroundDrawingEnabled() { + Field enableBackgroundDrawing = ReflectionUtils.getField(GuiTextField.class, "enableBackgroundDrawing"); + if (enableBackgroundDrawing != null) { + return (boolean) ReflectionUtils.getFieldValue(enableBackgroundDrawing, this); + } + return true; + } + public int getLineScrollOffset() { + Field lineScrollOffset = ReflectionUtils.getField(GuiTextField.class, "lineScrollOffset"); + if (lineScrollOffset != null) { + return (int) ReflectionUtils.getFieldValue(lineScrollOffset, this); + } + return 0; + } + + /** + * Args: x, y, buttonClicked + */ + public void mouseClicked(int aX, int aY, int aButton){ + + boolean flag = aX >= this.mScreenLocationX && aX < this.mScreenLocationX + this.width && aY >= this.mScreenLocationY && aY < this.mScreenLocationY + this.height; + + //Logger.INFO("Clicked X:"+aX); + //Logger.INFO("Clicked Y:"+aY); + //Logger.INFO("ScreenPos X:"+mScreenLocationX); + //Logger.INFO("ScreenPos Y:"+mScreenLocationY); + //Logger.INFO("Render X:"+xPosition); + //Logger.INFO("Render Y:"+yPosition); + + if (canLoseFocus()) + { + this.setFocused(flag); + } + + if (isFocused() && aButton == 0) + { + int l = aX - this.mScreenLocationX; + + if (isBackgroundDrawingEnabled()) + { + l -= 4; + } + + String s = this.mFontRenderer.trimStringToWidth(this.getText().substring(getLineScrollOffset()), this.getWidth()); + this.setCursorPosition(this.mFontRenderer.trimStringToWidth(s, l).length() + getLineScrollOffset()); + } + } + +} |