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

import java.awt.Rectangle;
import java.util.List;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;
import net.minecraftforge.common.util.ForgeDirection;

import org.lwjgl.opengl.GL11;

import codechicken.nei.api.API;
import codechicken.nei.api.INEIGuiAdapter;
import gregtech.api.enums.GT_Values;
import gregtech.api.gui.GT_GUIContainerMetaTile_Machine;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.net.GT_Packet_GtTileEntityGuiRequest;
import gregtech.common.GT_Proxy;

/**
 * Let's you access a GregTech IGregTechTileEntity's covers as tabs on the GUI's sides
 */
public class GT_GuiCoverTabLine extends GT_GuiTabLine {

    // Names of the block a cover could be on
    private static final String[] SIDES = new String[] { "GT5U.interface.coverTabs.down", "GT5U.interface.coverTabs.up",
        "GT5U.interface.coverTabs.north", "GT5U.interface.coverTabs.south", "GT5U.interface.coverTabs.west",
        "GT5U.interface.coverTabs.east" };

    // Not sure if there's a point in JIT translation but that's what this is
    private final String[] translatedSides;
    private final IGregTechTileEntity tile;
    private final int colorization;

    /**
     * Let's you access an IGregTechTileEntity's covers as tabs on the GUI's sides
     *
     * @param gui           GT_ITabRenderer gui which this tab line attaches to
     * @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
     * @param tile          The IGregTechTileEntity the covers of which we are accessing
     * @param colorization  The colorization of the GUI we are adding tabs to
     */
    public GT_GuiCoverTabLine(GT_GUIContainerMetaTile_Machine gui, int tabLineLeft, int tabLineTop, int tabHeight,
        int tabWidth, int tabSpacing, DisplayStyle xDir, DisplayStyle yDir, DisplayStyle displayMode,
        GT_GuiTabIconSet tabBackground, IGregTechTileEntity tile, int colorization) {
        super(gui, 6, tabLineLeft, tabLineTop, tabHeight, tabWidth, tabSpacing, xDir, yDir, displayMode, tabBackground);
        this.tile = tile;
        this.colorization = colorization;
        this.translatedSides = new String[6];
        setupTabs();
    }

    /**
     * Add a tab for each existing cover on this IGregTechTileEntity at creation time
     */
    private void setupTabs() {
        for (final ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) {
            final ItemStack cover = tile.getCoverItemAtSide(side);
            if (cover != null) {
                addCoverToTabs(side, cover);
            }
        }
    }

    @Override
    protected void drawBackground(float parTicks, int mouseX, int mouseY) {
        // Apply this tile's coloration to draw the background
        GL11.glColor3ub(
            (byte) ((colorization >> 16) & 0xFF),
            (byte) ((colorization >> 8) & 0xFF),
            (byte) (colorization & 0xFF));
        super.drawBackground(parTicks, mouseX, mouseY);
    }

    @Override
    protected void tabClicked(int tabId, int mouseButton) {
        if (mouseButton == 0 && mTabs[tabId].enabled) {
            GT_Values.NW.sendToServer(
                new GT_Packet_GtTileEntityGuiRequest(
                    this.tile.getXCoord(),
                    this.tile.getYCoord(),
                    this.tile.getZCoord(),
                    tabId + GT_Proxy.GUI_ID_COVER_SIDE_BASE,
                    this.tile.getWorld().provider.dimensionId,
                    Minecraft.getMinecraft().thePlayer.getEntityId(),
                    0));
        }
    }

    /**
     * Add the cover on this side of the IGregTechTileEntity to the tabs
     *
     * @param side
     * @param cover
     */
    private void addCoverToTabs(ForgeDirection side, ItemStack cover) {
        final boolean enabled = this.tile.getCoverBehaviorAtSideNew(side)
            .hasCoverGUI();
        final int ordinalSide = side.ordinal();
        this.setTab(ordinalSide, cover, null, getTooltipForCoverTab(side, cover, enabled));
        this.setTabEnabled(ordinalSide, enabled);
    }

    /**
     * Decorate the cover's tooltips according to the side it's on and on whether the tab is enabled or not
     *
     * @param side
     * @param cover
     * @param enabled
     * @return This cover tab's tooltip
     */
    private String[] getTooltipForCoverTab(ForgeDirection side, ItemStack cover, boolean enabled) {
        final List<String> tooltip = cover.getTooltip(Minecraft.getMinecraft().thePlayer, true);
        tooltip.set(
            0,
            (enabled ? EnumChatFormatting.UNDERLINE : EnumChatFormatting.DARK_GRAY) + getSideDescription(side)
                + (enabled ? EnumChatFormatting.RESET + ": " : ": " + EnumChatFormatting.RESET)
                + tooltip.get(0));
        return tooltip.toArray(new String[0]);
    }

    /**
     * Get the translated name for a side of the IGregTechTileEntity
     *
     * @param side
     * @return translated name for a side of the IGregTechTileEntity
     */
    private String getSideDescription(ForgeDirection side) {
        final int ordinalSide = side.ordinal();
        if (ordinalSide < SIDES.length) {
            if (this.translatedSides[ordinalSide] == null) {
                this.translatedSides[ordinalSide] = StatCollector.translateToLocal(SIDES[ordinalSide]);
            }
            return this.translatedSides[ordinalSide];
        }
        return null;
    }

    /**
     * Hide any NEI slots that would intersect with a cover tab
     */
    static class CoverTabLineNEIHandler extends INEIGuiAdapter {

        @Override
        public boolean hideItemPanelSlot(GuiContainer gui, int x, int y, int w, int h) {
            final Rectangle neiSlotArea = new Rectangle(x, y, w, h);
            if (gui instanceof GT_GUIContainerMetaTile_Machine) {
                final GT_GuiTabLine tabLine = ((GT_GUIContainerMetaTile_Machine) gui).coverTabs;
                if (!tabLine.visible) {
                    return false;
                }
                for (int i = 0; i < tabLine.mTabs.length; i++) {
                    if (tabLine.mTabs[i] != null && tabLine.mTabs[i].getBounds()
                        .intersects(neiSlotArea)) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

    static {
        API.registerNEIGuiHandler(new CoverTabLineNEIHandler());
    }
}