-
Notifications
You must be signed in to change notification settings - Fork 497
/
GuiBuildCraft.java
263 lines (194 loc) · 6.18 KB
/
GuiBuildCraft.java
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
package buildcraft.core.gui;
import java.util.ArrayList;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.IInventory;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import org.lwjgl.opengl.GL11;
import buildcraft.core.DefaultProps;
import buildcraft.core.utils.SessionVars;
public abstract class GuiBuildCraft extends GuiContainer {
// / LEDGERS
protected LedgerManager ledgerManager = new LedgerManager(this);
protected class LedgerManager {
private GuiBuildCraft gui;
protected ArrayList<Ledger> ledgers = new ArrayList<Ledger>();
public LedgerManager(GuiBuildCraft gui) {
this.gui = gui;
}
public void add(Ledger ledger) {
this.ledgers.add(ledger);
if (SessionVars.getOpenedLedger() != null && ledger.getClass().equals(SessionVars.getOpenedLedger())) {
ledger.setFullyOpen();
}
}
/**
* Inserts a ledger into the next-to-last position.
*
* @param ledger
*/
public void insert(Ledger ledger) {
this.ledgers.add(ledgers.size() - 1, ledger);
}
protected Ledger getAtPosition(int mX, int mY) {
int xShift = ((gui.width - gui.xSize) / 2) + gui.xSize;
int yShift = ((gui.height - gui.ySize) / 2) + 8;
for (int i = 0; i < ledgers.size(); i++) {
Ledger ledger = ledgers.get(i);
if (!ledger.isVisible()) {
continue;
}
ledger.currentShiftX = xShift;
ledger.currentShiftY = yShift;
if (ledger.intersectsWith(mX, mY, xShift, yShift))
return ledger;
yShift += ledger.getHeight();
}
return null;
}
protected void drawLedgers(int mouseX, int mouseY) {
int xPos = 8;
for (Ledger ledger : ledgers) {
ledger.update();
if (!ledger.isVisible()) {
continue;
}
ledger.draw(xSize, xPos);
xPos += ledger.getHeight();
}
Ledger ledger = getAtPosition(mouseX, mouseY);
if (ledger != null) {
int startX = mouseX - ((gui.width - gui.xSize) / 2) + 12;
int startY = mouseY - ((gui.height - gui.ySize) / 2) - 12;
String tooltip = ledger.getTooltip();
int textWidth = fontRenderer.getStringWidth(tooltip);
drawGradientRect(startX - 3, startY - 3, startX + textWidth + 3, startY + 8 + 3, 0xc0000000, 0xc0000000);
fontRenderer.drawStringWithShadow(tooltip, startX, startY, -1);
}
}
public void handleMouseClicked(int x, int y, int mouseButton) {
if (mouseButton == 0) {
Ledger ledger = this.getAtPosition(x, y);
// Default action only if the mouse click was not handled by the
// ledger itself.
if (ledger != null && !ledger.handleMouseClicked(x, y, mouseButton)) {
for (Ledger other : ledgers)
if (other != ledger && other.isOpen()) {
other.toggleOpen();
}
ledger.toggleOpen();
}
}
}
}
/**
* Side ledger for guis
*/
protected abstract class Ledger {
private boolean open;
protected int overlayColor = 0xffffff;
public int currentShiftX = 0;
public int currentShiftY = 0;
protected int limitWidth = 128;
protected int maxWidth = 124;
protected int minWidth = 24;
protected int currentWidth = minWidth;
protected int maxHeight = 24;
protected int minHeight = 24;
protected int currentHeight = minHeight;
public void update() {
// Width
if (open && currentWidth < maxWidth) {
currentWidth += 4;
} else if (!open && currentWidth > minWidth) {
currentWidth -= 4;
}
// Height
if (open && currentHeight < maxHeight) {
currentHeight += 4;
} else if (!open && currentHeight > minHeight) {
currentHeight -= 4;
}
}
public int getHeight() {
return currentHeight;
}
public abstract void draw(int x, int y);
public abstract String getTooltip();
public boolean handleMouseClicked(int x, int y, int mouseButton) {
return false;
}
public boolean intersectsWith(int mouseX, int mouseY, int shiftX, int shiftY) {
if (mouseX >= shiftX && mouseX <= shiftX + currentWidth && mouseY >= shiftY && mouseY <= shiftY + getHeight())
return true;
return false;
}
public void setFullyOpen() {
open = true;
currentWidth = maxWidth;
currentHeight = maxHeight;
}
public void toggleOpen() {
if (open) {
open = false;
SessionVars.setOpenedLedger(null);
} else {
open = true;
SessionVars.setOpenedLedger(this.getClass());
}
}
public boolean isVisible() {
return true;
}
public boolean isOpen() {
return this.open;
}
protected boolean isFullyOpened() {
return currentWidth >= maxWidth;
}
protected void drawBackground(int x, int y) {
float colorR = (overlayColor >> 16 & 255) / 255.0F;
float colorG = (overlayColor >> 8 & 255) / 255.0F;
float colorB = (overlayColor & 255) / 255.0F;
GL11.glColor4f(colorR, colorG, colorB, 1.0F);
mc.renderEngine.func_98187_b(DefaultProps.TEXTURE_PATH_GUI + "/ledger.png");
drawTexturedModalRect(x, y, 0, 256 - currentHeight, 4, currentHeight);
drawTexturedModalRect(x + 4, y, 256 - currentWidth + 4, 0, currentWidth - 4, 4);
// Add in top left corner again
drawTexturedModalRect(x, y, 0, 0, 4, 4);
drawTexturedModalRect(x + 4, y + 4, 256 - currentWidth + 4, 256 - currentHeight + 4, currentWidth - 4, currentHeight - 4);
GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0F);
}
protected void drawIcon(Icon icon, int x, int y) {
GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0F);
func_94065_a(x, y, icon, 16, 16);
}
}
protected TileEntity tile;
public GuiBuildCraft(BuildCraftContainer container, IInventory inventory) {
super(container);
if (inventory instanceof TileEntity) {
tile = (TileEntity) inventory;
}
initLedgers(inventory);
}
protected void initLedgers(IInventory inventory) {
}
@Override
protected void drawGuiContainerForegroundLayer(int par1, int par2) {
ledgerManager.drawLedgers(par1, par2);
}
protected int getCenteredOffset(String string) {
return getCenteredOffset(string, xSize);
}
protected int getCenteredOffset(String string, int xWidth) {
return (xWidth - fontRenderer.getStringWidth(string)) / 2;
}
// / MOUSE CLICKS
@Override
protected void mouseClicked(int par1, int par2, int mouseButton) {
super.mouseClicked(par1, par2, mouseButton);
// / Handle ledger clicks
ledgerManager.handleMouseClicked(par1, par2, mouseButton);
}
}