aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/gui/widgets/GT_GuiTabLine.java
blob: bc9f4437d78001f142aad0a34599c59eb402aefe (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
package gregtech.api.gui.widgets;

import java.awt.Rectangle;

import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.item.ItemStack;

import org.lwjgl.opengl.GL11;

import gregtech.api.interfaces.IGuiIcon;

/**
 * Draws clickable and configurable tabs on the left or right side of another GUI
 */
public class GT_GuiTabLine {

    /**
     * Defines a set of textures a tab line can use to render it's tab backgrounds
     */
    public static class GT_GuiTabIconSet {

        public IGuiIcon disabled;
        public IGuiIcon normal;
        public IGuiIcon highlight;

        public GT_GuiTabIconSet(IGuiIcon normalIcon, IGuiIcon highlightIcon, IGuiIcon disabledIcon) {
            this.normal = normalIcon;
            this.highlight = highlightIcon;
            this.disabled = disabledIcon;
        }
    }

    /**
     * Controls the rendering style of the tab line
     */
    public enum DisplayStyle {

        NONE((byte) 0),
        NORMAL((byte) 1),
        INVERSE((byte) -1);

        private byte value;

        DisplayStyle(byte value) {
            this.value = value;
        }

        public byte getValue() {
            return value;
        }
    }

    /**
     * A GUI should implement these methods as well as call the tab line's onMouseClicked, onInit and drawTabs for the
     * tab line to attach to it properly.
     */
    public interface GT_ITabRenderer {

        int getGuiLeft();

        int getGuiTop();

        int getXSize();

        RenderItem getItemRenderer();

        FontRenderer getFontRenderer();

        void addToolTip(GT_GuiTooltip tooltip);

        boolean removeToolTip(GT_GuiTooltip tooltip);
    }

    // The tabs are arranged according to their index in this array
    protected final GT_GuiTab[] mTabs;

    private int tabLineLeft, tabLineTop, tabHeight, tabWidth, tabSpacing;

    // In which direction to draw the tab line
    private DisplayStyle xDir, yDir;

    // Whether to display on the right side of the GT_ITabRenderer instead of left
    protected boolean flipHorizontally, visible;

    private GT_GuiTabIconSet tabBackground;
    private GT_ITabRenderer gui;

    /**
     * Draws clickable and configurable tabs on the left or right side of a GT_ITabRenderer
     *
     * @param gui           GT_ITabRenderer gui which this tab line attaches to
     * @param numTabs       number of tab positions in this tab line
     * @param tabLineLeft   left position of the tab line in relation to the gui
     * @param tabLineTop    top position of the tab line in relation to the gui
     * @param tabHeight     height of a tab
     * @param tabWidth      width of a tab
     * @param tabSpacing    pixels between each tab
     * @param xDir          whether to extend the line horizontally to the right (NORMAL), the left (INVERSE) or not at
     *                      all (NONE)
     * @param yDir          whether to extend the line vertically down (NORMAL), up (INVERSE) or not at all (NONE)
     * @param displayMode   whether to display on the left side of the GT_ITabRenderer (NORMAL), on it's right side
     *                      (INVERSE) or not at all (NONE)
     * @param tabBackground the set of textures used to draw this tab line's tab backgrounds
     */
    public GT_GuiTabLine(GT_ITabRenderer gui, int numTabs, int tabLineLeft, int tabLineTop, int tabHeight, int tabWidth,
        int tabSpacing, DisplayStyle xDir, DisplayStyle yDir, DisplayStyle displayMode,
        GT_GuiTabIconSet tabBackground) {
        this.gui = gui;
        this.mTabs = new GT_GuiTab[numTabs];
        this.tabLineLeft = tabLineLeft;
        this.tabLineTop = tabLineTop;
        this.tabHeight = tabHeight;
        this.tabWidth = tabWidth;
        this.tabSpacing = tabSpacing;
        this.xDir = xDir;
        this.yDir = yDir;
        this.tabBackground = tabBackground;
        this.flipHorizontally = displayMode == DisplayStyle.INVERSE;
        this.visible = !(displayMode == DisplayStyle.NONE);
    }

    /**
     * Creates a new tab at the specified position with the given parameters. This class handles the positioning.
     *
     * @param tabId
     * @param item
     * @param overlay
     * @param text
     */
    public void setTab(int tabId, ItemStack item, IGuiIcon overlay, String[] text) {
        mTabs[tabId] = new GT_GuiTab(
            this.gui,
            tabId,
            getBoundsForTab(tabId),
            this.tabBackground,
            item,
            overlay,
            text,
            this.flipHorizontally);
    }

    /**
     * Get the bounds a given tab should occupy
     *
     * @param tabId
     * @return
     */
    protected Rectangle getBoundsForTab(int tabId) {
        return new Rectangle(getTabX(tabId), getTabY(tabId), this.tabWidth, this.tabHeight);
    }

    /**
     * Enable or disable a tab. Disabled tabs have a dark background.
     *
     * @param tabId
     * @param value
     */
    public void setTabEnabled(int tabId, boolean value) {
        if (mTabs[tabId] != null) {
            mTabs[tabId].enabled = value;
        }
    }

    /**
     * Draw the tabs for this tab bar GT_ITabRenderer must call this method on drawGuiContainerBackgroundLayer or on
     * drawScreen.
     *
     * @param parTicks
     * @param mouseX
     * @param mouseY
     */
    public void drawTabs(float parTicks, int mouseX, int mouseY) {
        if (this.visible) {
            GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
            GL11.glEnable(GL11.GL_BLEND);
            GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
            drawBackground(parTicks, mouseX, mouseY);
            drawOverlays(parTicks, mouseX, mouseY);
            GL11.glPopAttrib();
        }
    }

    /**
     * Draw the tab's backgrounds first
     *
     * @param parTicks
     * @param mouseX
     * @param mouseY
     */
    protected void drawOverlays(float parTicks, int mouseX, int mouseY) {
        for (GT_GuiTab mTab : mTabs) {
            if (mTab != null) {
                mTab.drawOverlays(mouseX, mouseY, parTicks);
            }
        }
    }

    /**
     * Draw anything that overlays the tab's background texture
     *
     * @param parTicks
     * @param mouseX
     * @param mouseY
     */
    protected void drawBackground(float parTicks, int mouseX, int mouseY) {
        for (GT_GuiTab mTab : mTabs) {
            if (mTab != null) {
                mTab.drawBackground(mouseX, mouseY, parTicks);
            }
        }
    }

    /**
     * Call tabClick for every tab that was clicked. GT_ITabRenderer must call this method on mouseClicked.
     *
     * @param mouseX
     * @param mouseY
     * @param mouseButton
     */
    public void onMouseClicked(int mouseX, int mouseY, int mouseButton) {
        for (int tabId = 0; tabId < mTabs.length; tabId++) {
            if (mTabs[tabId] != null && mTabs[tabId].getBounds()
                .contains(mouseX, mouseY)) {
                tabClicked(tabId, mouseButton);
                return;
            }
        }
    }

    /**
     * Act on a tab being clicked.
     *
     * @param tabId
     * @param mouseButton
     */
    protected void tabClicked(int tabId, int mouseButton) {}

    /**
     * Reposition ourselves whenever the GT_ITabRenderer does so. GT_ITabRenderer must call this method on Init.
     */
    public void onInit() {
        for (int i = 0; i < mTabs.length; i++) {
            if (mTabs[i] != null) {
                mTabs[i].setPosition(getTabX(i), getTabY(i));
            }
        }
    }

    /**
     * Get the proper X position for a given tab
     *
     * @param tabId
     * @return
     */
    private int getTabX(int tabId) {
        return this.gui.getGuiLeft() + (flipHorizontally ? (gui.getXSize() - tabLineLeft - tabWidth) : tabLineLeft)
            + (tabId * (tabWidth + tabSpacing) * xDir.getValue());
    }

    /**
     * Get the proper Y position for a given tab
     *
     * @param tabId
     * @return
     */
    private int getTabY(int tabId) {
        return this.gui.getGuiTop() + tabLineTop + (tabId * (tabHeight + tabSpacing) * yDir.getValue());
    }
}