aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/core/gui/machine/GUIVolumetricFlaskSetter.java
blob: 8a57b3987f7b713699ab785c99a13d890617826b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package gtPlusPlus.core.gui.machine;

import static gregtech.api.enums.Mods.GTPlusPlus;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.resources.I18n;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.input.Keyboard;
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.ContainerVolumetricFlaskSetter;
import gtPlusPlus.core.gui.widget.GuiValueField;
import gtPlusPlus.core.handler.PacketHandler;
import gtPlusPlus.core.network.packet.PacketVolumetricFlaskGui;
import gtPlusPlus.core.tileentities.general.TileEntityVolumetricFlaskSetter;

@SideOnly(Side.CLIENT)
public class GUIVolumetricFlaskSetter extends GuiContainer {

    private static final ResourceLocation mGuiTextures = new ResourceLocation(
        GTPlusPlus.ID,
        "textures/gui/VolumetricFlaskSetter.png");
    private final ContainerVolumetricFlaskSetter mContainer;
    private boolean mIsOpen = false;
    private GuiValueField mText;
    private final TileEntityVolumetricFlaskSetter mTile;

    public GUIVolumetricFlaskSetter(ContainerVolumetricFlaskSetter aContainer) {
        super(aContainer);
        mContainer = aContainer;
        mTile = mContainer.mTileEntity;
    }

    @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(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);
    }

    @Override
    protected void drawGuiContainerForegroundLayer(final int i, final int j) {
        super.drawGuiContainerForegroundLayer(i, j);
        this.mText.drawTextBox();
        this.fontRendererObj.drawString(I18n.format("container.VolumetricFlaskSetter"), 4, 3, 4210752);
        int aYVal = 49;
        this.fontRendererObj.drawString(I18n.format("0 = 16l"), 8, aYVal, 4210752);
        this.fontRendererObj.drawString(I18n.format("4 = 576l"), 64, aYVal, 4210752);
        this.fontRendererObj.drawString(I18n.format("1 = 36l"), 8, aYVal += 8, 4210752);
        this.fontRendererObj.drawString(I18n.format("5 = 720l"), 64, aYVal, 4210752);
        this.fontRendererObj.drawString(I18n.format("2 = 144l"), 8, aYVal += 8, 4210752);
        this.fontRendererObj.drawString(I18n.format("6 = 864l"), 64, aYVal, 4210752);
        this.fontRendererObj.drawString(I18n.format("3 = 432l"), 8, aYVal += 8, 4210752);
        this.fontRendererObj.drawString(I18n.format("-> = Custom"), 59, aYVal, 4210752);
    }

    @Override
    public void drawScreen(int par1, int par2, float par3) {
        this.drawDefaultBackground();
        super.drawScreen(par1, par2, par3);
    }

    protected String getText() {
        return this.mText.getText();
    }

    @Override
    public void initGui() {
        super.initGui();
        // Keyboard.enableRepeatEvents(true);
        mIsOpen = true;
        this.mText = new GuiValueField(
            this.fontRendererObj,
            26,
            31,
            this.width / 2 - 62,
            this.height / 2 - 52,
            106,
            14,
            this);
        mText.setMaxStringLength(5);
        mText.setEnableBackgroundDrawing(true);
        mText.setText("0");
        mText.setFocused(true);
    }

    public boolean isNumber(char c) {
        boolean isNum = ((c >= 48 && c <= 57) || c == 45);
        if (isNum) {
            log("Found Digit: " + c + " | char value");
        } else {
            switch (c) {
                case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' -> {
                    log("Found Digit: " + c + " | char switch");
                    return true;
                }
            }
        }
        return isNum;
    }

    public boolean isNumber(int c) {
        switch (c) {
            case Keyboard.KEY_0, Keyboard.KEY_1, Keyboard.KEY_2, Keyboard.KEY_3, Keyboard.KEY_4, Keyboard.KEY_5, Keyboard.KEY_6, Keyboard.KEY_7, Keyboard.KEY_8, Keyboard.KEY_9, Keyboard.KEY_NUMPAD0, Keyboard.KEY_NUMPAD1, Keyboard.KEY_NUMPAD2, Keyboard.KEY_NUMPAD3, Keyboard.KEY_NUMPAD4, Keyboard.KEY_NUMPAD5, Keyboard.KEY_NUMPAD6, Keyboard.KEY_NUMPAD7, Keyboard.KEY_NUMPAD8, Keyboard.KEY_NUMPAD9 -> {
                log("Found Digit: " + Keyboard.getKeyName(c) + " | LWJGL Keybinding");
                return true;
            }
        }
        return false;
    }

    @Override
    protected void keyTyped(char par1, int par2) {
        if (mIsOpen) {
            log("Pressed " + par1 + " | " + par2);
            if (mText.isFocused()) {
                log("Text box has focus.");
                if (par2 == Keyboard.KEY_RETURN) {
                    log("Pressed Enter, unfocusing.");
                    mText.setFocused(false);
                } else if (par2 == Keyboard.KEY_BACK) {
                    log("Pressed Backspace.");
                    String aCurrentText = getText();
                    if (!aCurrentText.isEmpty()) {
                        this.mText.setText(aCurrentText.substring(0, aCurrentText.length() - 1));
                        if (getText().length() <= 0) {
                            setText(0);
                        }
                        sendUpdateToServer();
                    }
                } else {
                    if (isNumber(par2) || isNumber(par1)) {
                        log("Pressed number.");
                        this.mText.textboxKeyTyped(par1, par2);
                        sendUpdateToServer();
                    } else {
                        log("Pressed unused key.");
                        super.keyTyped(par1, par2);
                    }
                }
            } else {
                log("Text box not focused.");
                super.keyTyped(par1, par2);
            }
        } else {
            log("Gui is not open?");
        }
    }

    @Override
    protected void mouseClicked(int x, int y, int btn) {
        if (mIsOpen) {
            log("Clicked.");
            this.mText.mouseClicked(x, y, btn);
            if (!mText.didClickInTextField(x, y)) {
                log("Did not click in text box, passing to super.");
                super.mouseClicked(x, y, btn);
            }
        } else {
            log("Gui is not open?");
        }
    }

    @Override
    public void onGuiClosed() {
        mIsOpen = false;
        mText.setEnabled(false);
        mText.setVisible(false);
        super.onGuiClosed();
        // Keyboard.enableRepeatEvents(false);
    }

    public int parse(String aValue) {
        try {
            return Integer.parseInt(getText());
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    public void sendUpdateToServer() {
        if (!getText().isEmpty()) {
            PacketHandler.sendToServer(new PacketVolumetricFlaskGui(mTile, parse(getText())));
        }
    }

    public void setText(int aValue) {
        this.mText.setText("" + aValue);
    }

    @Override
    public void updateScreen() {
        super.updateScreen();
        // Update Textbox to 0 if Empty
        if (getText().length() <= 0) {
            this.mText.setText("0");
            sendUpdateToServer();
        }
        this.mText.updateCursorCounter();

        // Check TextBox Value is correct
        if (!getText().isEmpty()) {
            int aCustomValue = parse(getText());
            int aTileValue = mContainer.mCustomValue;
            if (aTileValue != aCustomValue) {
                setText(aTileValue);
            }
        }
    }

    public void log(String aString) {
        Logger.INFO("[Flask-GUI] " + aString);
    }
}