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

import java.awt.Rectangle;

import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import gregtech.api.gui.widgets.GT_GuiTabLine.GT_GuiTabIconSet;
import gregtech.api.gui.widgets.GT_GuiTabLine.GT_ITabRenderer;
import gregtech.api.interfaces.IGuiIcon;

/**
 * A tab to be attached to a tab line
 */
public class GT_GuiTab {

    private static final int SLOT_SIZE = 18;

    public boolean visible = true, mousedOver, enabled = true;

    private Rectangle bounds;
    private GT_GuiTabIconSet tabBackground;
    private ItemStack item;
    private GT_ITabRenderer gui;
    private GT_GuiTooltip tooltip;
    private IGuiIcon overlay;
    private boolean flipHorizontally;

    /**
     * A tab to be attached to a tab line
     *
     * @param gui              IGregTechTileEntity the tab line this tab belongs to is attached to
     * @param id               both the ID and position in the tab line of this tab
     * @param bounds           bounds of this tab
     * @param tabBackground    set of background textures
     * @param item             item to draw atop the background texture, not colored
     * @param overlay          texture to draw atop the background texture, not colored
     * @param tooltipText      tooltip of this tab
     * @param flipHorizontally whether to draw this tab on the right side of the IGregTechTileEntity
     */
    public GT_GuiTab(GT_ITabRenderer gui, int id, Rectangle bounds, GT_GuiTabIconSet tabBackground, ItemStack item,
            IGuiIcon overlay, String[] tooltipText, boolean flipHorizontally) {
        this.gui = gui;
        this.bounds = bounds;
        this.item = item;
        this.tabBackground = tabBackground;
        this.overlay = overlay;
        if (tooltipText != null) {
            setTooltipText(tooltipText);
        }
        this.flipHorizontally = flipHorizontally;
    }

    public GT_GuiTab(GT_ITabRenderer gui, int id, Rectangle bounds, GT_GuiTabIconSet tabBackground) {
        this(gui, id, bounds, tabBackground, null, null, null, false);
    }

    /**
     * Set this tab's tooltip text
     *
     * @param text
     * @return This tab for chaining
     */
    public GT_GuiTab setTooltipText(String... text) {
        if (tooltip == null) {
            tooltip = new GT_GuiTooltip(bounds, text);
            gui.addToolTip(tooltip);
        } else {
            tooltip.setToolTipText(text);
        }
        return this;
    }

    /**
     * @return This tab's tooltip object
     */
    public GT_GuiTooltip getTooltip() {
        return tooltip;
    }

    /**
     * Draw the background texture for this tab
     *
     * @param mouseX
     * @param mouseY
     * @param parTicks
     */
    public void drawBackground(int mouseX, int mouseY, float parTicks) {
        if (this.visible) {
            GT_GuiIcon.render(
                    getBackgroundTexture(),
                    bounds.x,
                    bounds.y,
                    bounds.width,
                    bounds.height,
                    1,
                    true,
                    this.flipHorizontally);
        }
    }

    /**
     * Draw overlay textures and items atop the background texture
     *
     * @param mouseX
     * @param mouseY
     * @param parTicks
     */
    public void drawOverlays(int mouseX, int mouseY, float parTicks) {
        this.mousedOver = bounds.contains(mouseX, mouseY);

        if (this.tooltip != null) {
            this.tooltip.enabled = this.visible;
        }

        if (this.visible) {
            if (overlay != null) {
                GL11.glColor4f(1, 1, 1, 1);
                GT_GuiIcon.render(overlay, bounds.x, bounds.y, bounds.width, bounds.height, 1, true);
            }
            if (item != null) {
                GL11.glPushAttrib(GL11.GL_ENABLE_BIT);

                if (item.getItem() instanceof ItemBlock) {
                    GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
                    GL11.glEnable(GL12.GL_RESCALE_NORMAL);
                }
                int margin = (bounds.height - SLOT_SIZE);
                gui.getItemRenderer()
                   .renderItemAndEffectIntoGUI(
                           gui.getFontRenderer(),
                           Minecraft.getMinecraft()
                                    .getTextureManager(),
                           item,
                           bounds.x + (this.flipHorizontally ? 0 : margin),
                           bounds.y + margin);

                if (item.getItem() instanceof ItemBlock) GL11.glPopAttrib();

                GL11.glPopAttrib();
            }
        }
    }

    /**
     * @return the texture this tab should currently use as it's background
     */
    protected IGuiIcon getBackgroundTexture() {
        if (!enabled) return tabBackground.disabled;

        return mousedOver ? tabBackground.highlight : tabBackground.normal;
    }

    /**
     * @return the screen space occupied by this tab
     */
    public Rectangle getBounds() {
        return this.bounds;
    }

    /**
     * Reposition this tab on the screen
     *
     * @param xPos
     * @param yPos
     */
    public void setPosition(int xPos, int yPos) {
        this.bounds = new Rectangle(xPos, yPos, bounds.width, bounds.height);
    }
}