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

import static gregtech.api.enums.GT_Values.RES_PATH_GUI;

import java.awt.Rectangle;

import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.StatCollector;

import gregtech.api.enums.GT_Values;
import gregtech.api.gui.widgets.GT_GuiIcon;
import gregtech.api.gui.widgets.GT_GuiSlotTooltip;
import gregtech.api.gui.widgets.GT_GuiSmartTooltip;
import gregtech.api.gui.widgets.GT_GuiSmartTooltip.TooltipVisibilityProvider;
import gregtech.api.gui.widgets.GT_GuiTabLine.GT_GuiTabIconSet;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_BasicMachine;
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_BasicMachine_Steel;
import gregtech.api.util.GT_Recipe.GT_Recipe_Map;
import gregtech.common.power.Power;
import gregtech.nei.NEI_TransferRectHost;

/**
 * NEVER INCLUDE THIS FILE IN YOUR MOD!!!
 * <p/>
 * The GUI-Container I use for all my Basic Machines
 */
public class GT_GUIContainer_BasicMachine extends GT_GUIContainerMetaTile_Machine implements NEI_TransferRectHost {

    private static final int NEEDS_STEAM_VENTING = 64;
    private static final GT_GuiTabIconSet TAB_ICONSET_BRONZE = new GT_GuiTabIconSet(
            GT_GuiIcon.TAB_NORMAL_BRONZE,
            GT_GuiIcon.TAB_HIGHLIGHT_BRONZE,
            GT_GuiIcon.TAB_DISABLED_BRONZE);
    private static final GT_GuiTabIconSet TAB_ICONSET_STEEL = new GT_GuiTabIconSet(
            GT_GuiIcon.TAB_NORMAL_STEEL,
            GT_GuiIcon.TAB_HIGHLIGHT_STEEL,
            GT_GuiIcon.TAB_DISABLED_STEEL);
    private final int textColor = this.getTextColorOrDefault("title", 0x404040);
    public final String mName, mNEI;
    public final byte mProgressBarDirection, mProgressBarAmount;

    // Tooltip localization keys
    private static final String BATTERY_SLOT_TOOLTIP = "GT5U.machines.battery_slot.tooltip",
            BATTERY_SLOT_TOOLTIP_ALT = "GT5U.machines.battery_slot.tooltip.alternative",
            UNUSED_SLOT_TOOLTIP = "GT5U.machines.unused_slot.tooltip",
            SPECIAL_SLOT_TOOLTIP = "GT5U.machines.special_slot.tooltip",
            FLUID_INPUT_TOOLTIP = "GT5U.machines.fluid_input_slot.tooltip",
            FLUID_OUTPUT_TOOLTIP = "GT5U.machines.fluid_output_slot.tooltip",
            STALLED_STUTTERING_TOOLTIP = "GT5U.machines.stalled_stuttering.tooltip",
            STALLED_VENT_TOOLTIP = "GT5U.machines.stalled_vent.tooltip",
            FLUID_TRANSFER_TOOLTIP = "GT5U.machines.fluid_transfer.tooltip",
            ITEM_TRANSFER_TOOLTIP = "GT5U.machines.item_transfer.tooltip",
            POWER_SOURCE_KEY = "GT5U.machines.powersource.";

    public GT_GUIContainer_BasicMachine(InventoryPlayer aInventoryPlayer, IGregTechTileEntity aTileEntity, String aName,
            String aTextureFile, String aNEI) {
        this(aInventoryPlayer, aTileEntity, aName, aTextureFile, aNEI, (byte) 0, (byte) 1);
    }

    public GT_GUIContainer_BasicMachine(InventoryPlayer aInventoryPlayer, IGregTechTileEntity aTileEntity, String aName,
            String aTextureFile, String aNEI, byte aProgressBarDirection, byte aProgressBarAmount) {
        super(
                new GT_Container_BasicMachine(aInventoryPlayer, aTileEntity),
                RES_PATH_GUI + "basicmachines/" + aTextureFile);
        mProgressBarDirection = aProgressBarDirection;
        mProgressBarAmount = (byte) Math.max(1, aProgressBarAmount);
        mName = aName;
        mNEI = aNEI;
    }

    /**
     * Load data for and create appropriate tooltips for this machine
     */
    @Override
    protected void setupTooltips() {
        super.setupTooltips();
        GT_MetaTileEntity_BasicMachine machine = getMachine();
        GT_Recipe_Map recipes = machine.getRecipeList();
        GT_Container_BasicMachine container = getContainer();
        Rectangle tProblemArea = new Rectangle(this.guiLeft + 79, this.guiTop + 44, 18, 18);
        String batterySlotTooltipKey;
        Object[] batterySlotTooltipArgs;
        if (machine.isSteampowered()) {
            batterySlotTooltipKey = UNUSED_SLOT_TOOLTIP;
            batterySlotTooltipArgs = new String[0];
            addToolTip(new GT_GuiSmartTooltip(tProblemArea, new TooltipVisibilityProvider() {

                public boolean shouldShowTooltip() {
                    return hasErrorCode(NEEDS_STEAM_VENTING);
                }
            }, mTooltipCache.getData(STALLED_VENT_TOOLTIP)));
        } else {
            String pTier1 = powerTierName(machine.mTier);
            if (machine.mTier == GT_Values.VN.length - 1) {
                batterySlotTooltipKey = BATTERY_SLOT_TOOLTIP_ALT;
                batterySlotTooltipArgs = new String[] { pTier1 };
            } else {
                batterySlotTooltipKey = BATTERY_SLOT_TOOLTIP;
                batterySlotTooltipArgs = new String[] { pTier1, powerTierName((byte) (machine.mTier + 1)) };
            }
            addToolTip(
                    new GT_GuiSlotTooltip(
                            container.slotFluidTransferToggle,
                            mTooltipCache.getData(FLUID_TRANSFER_TOOLTIP)));
            addToolTip(
                    new GT_GuiSlotTooltip(
                            container.slotItemTransferToggle,
                            mTooltipCache.getData(ITEM_TRANSFER_TOOLTIP)));
        }
        if (recipes != null && recipes.hasFluidInputs()) {
            addToolTip(
                    new GT_GuiSlotTooltip(
                            container.slotFluidInput,
                            mTooltipCache.getData(FLUID_INPUT_TOOLTIP, machine.getCapacity())));
        }
        if (recipes != null && recipes.hasFluidOutputs()) {
            addToolTip(
                    new GT_GuiSlotTooltip(
                            container.slotFluidOutput,
                            mTooltipCache.getData(FLUID_OUTPUT_TOOLTIP, machine.getCapacity())));
        }
        addToolTip(
                new GT_GuiSlotTooltip(
                        getContainer().slotBattery,
                        mTooltipCache.getData(batterySlotTooltipKey, batterySlotTooltipArgs)));
        addToolTip(
                new GT_GuiSlotTooltip(
                        container.slotSpecial,
                        mTooltipCache.getData(
                                recipes != null && recipes.usesSpecialSlot() ? SPECIAL_SLOT_TOOLTIP
                                        : UNUSED_SLOT_TOOLTIP)));
        addToolTip(new GT_GuiSmartTooltip(tProblemArea, new TooltipVisibilityProvider() {

            public boolean shouldShowTooltip() {
                return container.mStuttering && !hasErrorCode(NEEDS_STEAM_VENTING);
            }
        },
                mTooltipCache.getData(
                        STALLED_STUTTERING_TOOLTIP,
                        StatCollector
                                .translateToLocal(POWER_SOURCE_KEY + (machine.isSteampowered() ? "steam" : "power")))));
    }

    /**
     * Apply proper coloration to a machine's power tier short name
     * 
     * @param machineTier
     * @return colored power tier short name
     */
    private String powerTierName(byte machineTier) {
        return GT_Values.TIER_COLORS[machineTier] + GT_Values.VN[machineTier];
    }

    private GT_MetaTileEntity_BasicMachine getMachine() {
        return (GT_MetaTileEntity_BasicMachine) mContainer.mTileEntity.getMetaTileEntity();
    }

    @Override
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
        super.drawGuiContainerForegroundLayer(mouseX, mouseY);
        fontRendererObj.drawString(mName, 8, 4, textColor);
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float parTicks, int mouseX, int mouseY) {
        super.drawGuiContainerBackgroundLayer(parTicks, mouseX, mouseY);
        int x = (width - xSize) / 2;
        int y = (height - ySize) / 2;
        drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
        if (mContainer != null) {
            if (!getMachine().isSteampowered()) {
                if (getContainer().mFluidTransfer) drawTexturedModalRect(x + 7, y + 62, 176, 18, 18, 18);
                if (getContainer().mItemTransfer) drawTexturedModalRect(x + 25, y + 62, 176, 36, 18, 18);
            }
            if (getContainer().mStuttering) drawTexturedModalRect(x + 79, y + 44, 176, 54, 18, 18);

            if (mContainer.mMaxProgressTime > 0) {
                int tSize = mProgressBarDirection < 2 ? 20 : 18;
                int tProgress = Math.max(
                        1,
                        Math.min(
                                tSize * mProgressBarAmount,
                                (mContainer.mProgressTime > 0 ? 1 : 0) + mContainer.mProgressTime * tSize
                                        * mProgressBarAmount
                                        / mContainer.mMaxProgressTime))
                        % (tSize + 1);

                switch (mProgressBarDirection) { // yes, my OCD was mad at me before I did the Tabs.
                    case 0:
                        drawTexturedModalRect(x + 78, y + 24, 176, 0, tProgress, 18);
                        break;
                    case 1:
                        drawTexturedModalRect(x + 78 + 20 - tProgress, y + 24, 176 + 20 - tProgress, 0, tProgress, 18);
                        break;
                    case 2:
                        drawTexturedModalRect(x + 78, y + 24, 176, 0, 20, tProgress);
                        break;
                    case 3:
                        drawTexturedModalRect(x + 78, y + 24 + 18 - tProgress, 176, 18 - tProgress, 20, tProgress);
                        break;
                    case 4:
                        tProgress = 20 - tProgress;
                        drawTexturedModalRect(x + 78, y + 24, 176, 0, tProgress, 18);
                        break;
                    case 5:
                        tProgress = 20 - tProgress;
                        drawTexturedModalRect(x + 78 + 20 - tProgress, y + 24, 176 + 20 - tProgress, 0, tProgress, 18);
                        break;
                    case 6:
                        tProgress = 18 - tProgress;
                        drawTexturedModalRect(x + 78, y + 24, 176, 0, 20, tProgress);
                        break;
                    case 7:
                        tProgress = 18 - tProgress;
                        drawTexturedModalRect(x + 78, y + 24 + 18 - tProgress, 176, 18 - tProgress, 20, tProgress);
                        break;
                }
            }
        }
    }

    @Override
    protected GT_GuiTabIconSet getTabBackground() {
        if (getMachine().isSteampowered()) {
            return getMachine() instanceof GT_MetaTileEntity_BasicMachine_Steel ? TAB_ICONSET_STEEL
                    : TAB_ICONSET_BRONZE;
        }
        return super.getTabBackground();
    }

    /**
     * Whether the machine currently has this error code
     */
    private boolean hasErrorCode(int errorCode) {
        return (getContainer().mDisplayErrorCode & errorCode) != 0;
    }

    private GT_Container_BasicMachine getContainer() {
        return (GT_Container_BasicMachine) mContainer;
    }

    @Override
    public String getNeiTransferRectString() {
        return mNEI;
    }

    @Override
    public String getNeiTransferRectTooltip() {
        Power powerInfo = getMachine().getPower();
        if (getMachine().isSteampowered()) {
            return powerInfo.getTierString() + " Steam recipes";
        } else {
            return "Recipes available in " + powerInfo.getTierString();
        }
    }

    @Override
    public Object[] getNeiTransferRectArgs() {
        return new Object[] { getMachine().getPower() };
    }

    @Override
    public Rectangle getNeiTransferRect() {
        return new Rectangle(65, 13, 36, 18);
    }
}