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

import gregtech.GT_Mod;
import gregtech.api.GregTech_API;
import gregtech.api.enums.Dyes;
import gregtech.api.gui.widgets.GT_GuiCoverTabLine;
import gregtech.api.gui.widgets.GT_GuiIcon;
import gregtech.api.gui.widgets.GT_GuiTooltip;
import gregtech.api.gui.widgets.GT_GuiTooltipManager;
import gregtech.api.gui.widgets.GT_GuiTabLine.DisplayStyle;
import gregtech.api.gui.widgets.GT_GuiTabLine.GT_GuiTabIconSet;
import gregtech.api.gui.widgets.GT_GuiTabLine.GT_ITabRenderer;
import gregtech.api.gui.widgets.GT_GuiTooltipManager.GT_IToolTipRenderer;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.MetaTileEntity;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.entity.RenderItem;
import gregtech.api.util.GT_TooltipDataCache;
import net.minecraft.entity.player.InventoryPlayer;

import java.util.List;

import org.lwjgl.opengl.GL11;

/**
 * NEVER INCLUDE THIS FILE IN YOUR MOD!!!
 * <p/>
 * The GUI-Container I use for all my MetaTileEntities
 */
public class GT_GUIContainerMetaTile_Machine extends GT_GUIContainer implements GT_IToolTipRenderer, GT_ITabRenderer {

    public final GT_ContainerMetaTile_Machine mContainer;
    
    protected GT_GuiTooltipManager mTooltipManager = new GT_GuiTooltipManager();
    protected GT_TooltipDataCache mTooltipCache = new GT_TooltipDataCache();

    // Cover Tabs support. Subclasses can override display position, style and visuals by overriding setupCoverTabs
    public GT_GuiCoverTabLine coverTabs;
    private static final int
        COVER_TAB_LEFT = -16,
        COVER_TAB_TOP = 1,
        COVER_TAB_HEIGHT = 20,
        COVER_TAB_WIDTH = 18,
        COVER_TAB_SPACING = 2;
    private static final DisplayStyle
        COVER_TAB_X_DIR = DisplayStyle.NONE,
        COVER_TAB_Y_DIR = DisplayStyle.NORMAL;
    private static final GT_GuiTabIconSet TAB_ICONSET =  new GT_GuiTabIconSet(
        GT_GuiIcon.TAB_NORMAL,
        GT_GuiIcon.TAB_HIGHLIGHT,
        GT_GuiIcon.TAB_DISABLED);

    public GT_GUIContainerMetaTile_Machine(GT_ContainerMetaTile_Machine aContainer, String aGUIbackground) {
        super(aContainer, aGUIbackground);
        mContainer = aContainer;
        
        DisplayStyle preferredDisplayStyle = GT_Mod.gregtechproxy.mCoverTabsVisible
        ? (GT_Mod.gregtechproxy.mCoverTabsFlipped ? DisplayStyle.INVERSE : DisplayStyle.NORMAL)
        : DisplayStyle.NONE;
        setupCoverTabs(preferredDisplayStyle);

        // Only setup tooltips if they're currently enabled.
        if (GT_Mod.gregtechproxy.mTooltipVerbosity > 0 || GT_Mod.gregtechproxy.mTooltipShiftVerbosity > 0) {
            setupTooltips();
        }
    }

    public GT_GUIContainerMetaTile_Machine(InventoryPlayer aInventoryPlayer, IGregTechTileEntity aTileEntity,
            String aGUIbackground) {
        this(new GT_ContainerMetaTile_Machine(aInventoryPlayer, aTileEntity), aGUIbackground);
    }

    /**
     * Initialize the coverTabs object according to client preferences
     */
    protected void setupCoverTabs(DisplayStyle preferredDisplayStyle) {
        coverTabs = new GT_GuiCoverTabLine(
            this,
            COVER_TAB_LEFT,
            COVER_TAB_TOP,
            COVER_TAB_HEIGHT,
            COVER_TAB_WIDTH,
            COVER_TAB_SPACING,
            COVER_TAB_X_DIR,
            COVER_TAB_Y_DIR,
            preferredDisplayStyle,
            getTabBackground(),
            getMachine().getBaseMetaTileEntity(),
            getColorization());
    }

    @Override
    public void drawScreen(int mouseX, int mouseY, float parTicks) {
        super.drawScreen(mouseX, mouseY, parTicks);
        if (mc.thePlayer.inventory.getItemStack() == null) {
            GL11.glPushMatrix();
            GL11.glTranslatef(guiLeft, guiTop, 0.0F);
            mTooltipManager.onTick(this, mouseX, mouseY);
            GL11.glPopMatrix();
        }
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float parTicks, int mouseX, int mouseY) {
        // Drawing tabs
        coverTabs.drawTabs(parTicks, mouseX, mouseY);

        // Applying machine coloration, which subclasses rely on
        Dyes color = getColorization();
        GL11.glColor3ub((byte) color.mRGBa[0], (byte) color.mRGBa[1], (byte) color.mRGBa[2]);

        // Binding machine's own texture, which subclasses rely on being set
        super.drawGuiContainerBackgroundLayer(parTicks, mouseX, mouseY);
    }

    /**
     * @return The color used to render this machine's GUI
     */
    private Dyes getColorization() {
        if (GregTech_API.sMachineMetalGUI) {
            return Dyes.MACHINE_METAL;
        } else if (GregTech_API.sColoredGUI && mContainer != null && mContainer.mTileEntity != null) {
            byte colorByte = mContainer.mTileEntity.getColorization();
            Dyes color;
            if (colorByte != -1)
                color = Dyes.get(colorByte);
            else
                color = Dyes.MACHINE_METAL;
            return color;
        } else {
            return Dyes.dyeWhite;
        }
    }

    /**
     * @return This machine's MetaTileEntity
     */
    private MetaTileEntity  getMachine() {
        return (MetaTileEntity ) mContainer.mTileEntity.getMetaTileEntity();
    }

    // Tabs support

    @Override
    protected void mouseClicked(int mouseX, int mouseY, int mouseButton)
    {
        super.mouseClicked(mouseX, mouseY, mouseButton);
        // Check for clicked tabs
        coverTabs.onMouseClicked(mouseX, mouseY, mouseButton);
    }
    
    @Override
    public void initGui() {
        super.initGui();
        // Perform layout of tabs
        coverTabs.onInit();
    }

    /**
     * @return the background textures used by this machine GUI's tabs
     */
    protected GT_GuiTabIconSet getTabBackground() {
        return TAB_ICONSET;
    }

    // Tooltips support

    /**
     * Load data for and create appropriate tooltips for this machine.
     * Only called when one of regular or shift tooltips are enabled.
     */
    protected void setupTooltips() {    }

    // GT_IToolTipRenderer and GT_ITabRenderer implementations
    @Override
    public void drawHoveringText(List text, int x, int y, FontRenderer font) {
        super.drawHoveringText(text, x, y, font);
    }
    @Override
    public int getGuiTop() {
        return guiTop;
    }
    @Override
    public int getGuiLeft() {
        return guiLeft;
    }
    @Override
    public int getXSize() {
        return xSize;
    }
    @Override
    public FontRenderer getFontRenderer() {
        return fontRendererObj;
    }    
    @Override
    public RenderItem getItemRenderer() {
        return itemRender;
    }
    @Override
    public void addToolTip(GT_GuiTooltip toolTip) {
        mTooltipManager.addToolTip(toolTip);
    }
    @Override
    public boolean removeToolTip(GT_GuiTooltip toolTip) {
        return mTooltipManager.removeToolTip(toolTip);
    }
}