aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/gui/GT_GUIScreen.java
blob: 4f15e2e607b9fb9099db4ee672f2a26231684a10 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package gregtech.api.gui;

import java.util.ArrayList;
import java.util.List;

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 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;

@Deprecated
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;
    protected ResourceLocation mGUIbackgroundLocation;

    private GuiButton selectedButton;
    private final GT_GUIColorOverride colorOverride;
    private final int textColor;
    private static final String guiTexturePath = "gregtech:textures/gui/GuiCover.png";

    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);
        this.mGUIbackgroundLocation = new ResourceLocation(guiTexturePath);
        this.colorOverride = GT_GUIColorOverride.get(guiTexturePath);
        this.textColor = getTextColorOrDefault("title", 0xFF222222);
    }

    @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 button) buttonList.add(button);
            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);

    protected int getTextColorOrDefault(String textType, int defaultColor) {
        return colorOverride.getTextColorOrDefault(textType, defaultColor);
    }

    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(mGUIbackgroundLocation);
        drawTexturedModalRect(guiLeft, guiTop, 0, 0, gui_width, gui_height);
    }

    public void drawExtras(int mouseX, int mouseY, float parTicks) {
        this.fontRendererObj.drawString(header, 25, 9, textColor);
    }

    @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);
            } 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<String> text, int mouseX, int mouseY, FontRenderer render) {
        super.drawHoveringText(text, mouseX, mouseY, 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);
    }
}