-
Notifications
You must be signed in to change notification settings - Fork 497
/
GuiGateInterface.java
488 lines (392 loc) · 12.7 KB
/
GuiGateInterface.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.transport.gui;
import java.util.ArrayList;
import java.util.Iterator;
import org.lwjgl.opengl.GL11;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.util.ResourceLocation;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import buildcraft.api.statements.IStatement;
import buildcraft.api.statements.IStatementParameter;
import buildcraft.api.statements.StatementMouseClick;
import buildcraft.core.gui.AdvancedSlot;
import buildcraft.core.gui.GuiAdvancedInterface;
import buildcraft.core.utils.StringUtils;
import buildcraft.transport.ActionActiveState;
import buildcraft.transport.Gate;
import buildcraft.transport.Pipe;
import buildcraft.transport.gates.GateDefinition.GateMaterial;
public class GuiGateInterface extends GuiAdvancedInterface {
IInventory playerInventory;
private final ContainerGateInterface container;
private final Pipe<?> pipe;
private Gate gate;
private abstract class StatementSlot extends AdvancedSlot {
public int slot;
public ArrayList<StatementParameterSlot> parameters = new ArrayList<StatementParameterSlot>();
public StatementSlot(int x, int y, Pipe<?> pipe, int slot) {
super(GuiGateInterface.this, x, y);
this.slot = slot;
}
@Override
public String getDescription() {
IStatement stmt = getStatement();
if (stmt != null) {
return stmt.getDescription();
} else {
return "";
}
}
@SideOnly(Side.CLIENT)
@Override
public IIcon getIcon() {
IStatement stmt = getStatement();
if (stmt != null) {
return stmt.getIcon();
} else {
return null;
}
}
@Override
public boolean isDefined() {
return getStatement() != null;
}
public abstract IStatement getStatement();
}
private class TriggerSlot extends StatementSlot {
public TriggerSlot(int x, int y, Pipe<?> pipe, int slot) {
super(x, y, pipe, slot);
}
@Override
public IStatement getStatement() {
return gate.getTrigger(slot);
}
}
private class ActionSlot extends StatementSlot {
public ActionSlot(int x, int y, Pipe<?> pipe, int slot) {
super(x, y, pipe, slot);
}
@Override
public IStatement getStatement() {
return gate.getAction(slot);
}
}
private abstract class StatementParameterSlot extends AdvancedSlot {
public Pipe<?> pipe;
public int slot;
public StatementSlot statementSlot;
public StatementParameterSlot(int x, int y, Pipe<?> pipe, int slot, StatementSlot iStatementSlot) {
super(GuiGateInterface.this, x, y);
this.pipe = pipe;
this.slot = slot;
this.statementSlot = iStatementSlot;
statementSlot.parameters.add(this);
}
@Override
public boolean isDefined() {
return getParameter() != null;
}
@Override
public String getDescription() {
IStatementParameter parameter = getParameter();
if (parameter != null) {
return parameter.getDescription() != null ? parameter.getDescription() : "";
} else {
return null;
}
}
@Override
public ItemStack getItemStack() {
IStatementParameter parameter = getParameter();
if (parameter != null) {
return parameter.getItemStack();
} else {
return null;
}
}
@Override
public IIcon getIcon() {
IStatementParameter parameter = getParameter();
if (parameter != null) {
return parameter.getIcon();
} else {
return null;
}
}
public abstract IStatementParameter getParameter();
public boolean isAllowed() {
return statementSlot.getStatement() != null && slot < statementSlot.getStatement().maxParameters();
}
public boolean isRequired() {
return statementSlot.getStatement() != null && slot < statementSlot.getStatement().minParameters();
}
public abstract void setParameter(IStatementParameter param, boolean notifyServer);
}
class TriggerParameterSlot extends StatementParameterSlot {
public TriggerParameterSlot(int x, int y, Pipe<?> pipe, int slot, StatementSlot iStatementSlot) {
super(x, y, pipe, slot, iStatementSlot);
}
@Override
public IStatementParameter getParameter() {
return gate.getTriggerParameter(statementSlot.slot, slot);
}
@Override
public void setParameter(IStatementParameter param, boolean notifyServer) {
container.setTriggerParameter(statementSlot.slot, slot, param, notifyServer);
}
}
class ActionParameterSlot extends StatementParameterSlot {
public ActionParameterSlot(int x, int y, Pipe<?> pipe, int slot, StatementSlot iStatementSlot) {
super(x, y, pipe, slot, iStatementSlot);
}
@Override
public IStatementParameter getParameter() {
return gate.getActionParameter(statementSlot.slot, slot);
}
@Override
public void setParameter(IStatementParameter param, boolean notifyServer) {
container.setActionParameter(statementSlot.slot, slot, param, notifyServer);
}
}
public GuiGateInterface(IInventory playerInventory, Pipe<?> pipe) {
super(new ContainerGateInterface(playerInventory, pipe), null, null);
container = (ContainerGateInterface) this.inventorySlots;
container.gateCallback = this;
this.pipe = pipe;
this.playerInventory = playerInventory;
}
public void setGate(Gate gate) {
this.gate = gate;
init();
}
public void init() {
if (gate == null) {
return;
}
xSize = 176;
ySize = gate.material.guiHeight;
int position = 0;
slots.clear();
if (gate.material == GateMaterial.REDSTONE) {
slots.add(new TriggerSlot(62, 26, pipe, 0));
slots.add(new ActionSlot(98, 26, pipe, 0));
} else if (gate.material == GateMaterial.IRON) {
slots.add(new TriggerSlot(62, 26, pipe, 0));
slots.add(new TriggerSlot(62, 44, pipe, 1));
slots.add(new ActionSlot(98, 26, pipe, 0));
slots.add(new ActionSlot(98, 44, pipe, 1));
} else if (gate.material == GateMaterial.GOLD) {
for (int k = 0; k < 4; ++k) {
slots.add(new TriggerSlot(53, 26 + 18 * k, pipe, position));
position++;
}
for (int k = 0; k < 4; ++k) {
slots.add(new ActionSlot(107, 26 + 18 * k, pipe, position - 4));
position++;
}
for (int k = 0; k < 4; ++k) {
slots.add(new TriggerParameterSlot(71, 26 + 18 * k, pipe, 0, (TriggerSlot) slots.get(k)));
position++;
}
} else if (gate.material == GateMaterial.DIAMOND) {
for (int k = 0; k < 4; ++k) {
slots.add(new TriggerSlot(8, 26 + 18 * k, pipe, position));
position++;
slots.add(new TriggerSlot(98, 26 + 18 * k, pipe, position));
position++;
}
for (int k = 0; k < 4; ++k) {
slots.add(new ActionSlot(62, 26 + 18 * k, pipe, position - 8));
position++;
slots.add(new ActionSlot(152, 26 + 18 * k, pipe, position - 8));
position++;
}
for (int k = 0; k < 4; ++k) {
slots.add(new TriggerParameterSlot(26, 26 + 18 * k, pipe, 0,
(TriggerSlot) slots.get(position - 16)));
position++;
slots.add(new TriggerParameterSlot(116, 26 + 18 * k, pipe, 0,
(TriggerSlot) slots.get(position - 16)));
position++;
}
} else if (gate.material == GateMaterial.EMERALD) {
int lastPos;
for (int y = 0; y < 4; ++y) {
slots.add(new TriggerSlot(8, 26 + 18 * y, pipe, y));
lastPos = position;
position++;
for (int x = 0; x < 3; ++x) {
slots.add(new TriggerParameterSlot(
8 + 18 * (x + 1),
26 + 18 * y,
pipe,
x,
(TriggerSlot) slots.get(lastPos)));
position++;
}
slots.add(new ActionSlot(98, 26 + 18 * y, pipe, y));
lastPos = position;
position++;
for (int x = 0; x < 3; ++x) {
slots.add(new ActionParameterSlot(
98 + 18 * (x + 1),
26 + 18 * y,
pipe,
x,
(ActionSlot) slots.get(lastPos)));
position++;
}
}
}
initGui();
}
@Override
protected void drawGuiContainerForegroundLayer(int par1, int par2) {
if (gate == null) {
return;
}
String name = container.getGateName();
fontRendererObj.drawString(name, getCenteredOffset(name), 10, 0x404040);
fontRendererObj.drawString(StringUtils.localize("gui.inventory"), 8, ySize - 97, 0x404040);
drawTooltipForSlotAt(par1, par2);
}
@Override
protected void drawGuiContainerBackgroundLayer(float f, int x, int y) {
container.synchronize();
if (gate == null) {
return;
}
ResourceLocation texture = container.getGateGuiFile();
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
mc.renderEngine.bindTexture(texture);
drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
for (AdvancedSlot slot : slots) {
if (slot instanceof TriggerSlot) {
boolean halfWidth = container.actionsState[((TriggerSlot) slot).slot] == ActionActiveState.Partial;
if (container.actionsState[((TriggerSlot) slot).slot] != ActionActiveState.Deactivated) {
mc.renderEngine.bindTexture(texture);
drawTexturedModalRect(guiLeft + slot.x + 17 + 18 * gate.material.numTriggerParameters, guiTop
+ slot.y + 6, 176, 18, halfWidth ? 9 : 18, 4);
}
} else if (slot instanceof StatementParameterSlot) {
StatementParameterSlot paramSlot = (StatementParameterSlot) slot;
StatementSlot statement = paramSlot.statementSlot;
mc.renderEngine.bindTexture(texture);
if (statement.isDefined()) {
if (!paramSlot.isAllowed()) {
drawTexturedModalRect(guiLeft + slot.x - 1, guiTop + slot.y - 1, 176, 0, 18, 18);
} else if (paramSlot.isRequired() && paramSlot.getItemStack() == null) {
drawTexturedModalRect(guiLeft + slot.x - 1, guiTop + slot.y - 1, 176, 22, 18, 18);
}
} else {
drawTexturedModalRect(guiLeft + slot.x - 1, guiTop + slot.y - 1, 176, 0, 18, 18);
}
}
}
drawBackgroundSlots();
}
@Override
protected void mouseClicked(int i, int j, int k) {
if (gate == null) {
return;
}
super.mouseClicked(i, j, k);
AdvancedSlot slot = getSlotAtLocation(i, j);
if (slot instanceof TriggerSlot && container.hasTriggers()) {
TriggerSlot triggerSlot = (TriggerSlot) slot;
IStatement changed = null;
if (triggerSlot.getStatement() == null) {
if (k == 0) {
changed = container.getFirstTrigger();
} else {
changed = container.getLastTrigger();
}
} else {
Iterator<IStatement> it = container.getTriggerIterator(k != 0);
for (; it.hasNext();) {
IStatement trigger = it.next();
if (!it.hasNext()) {
changed = null;
break;
}
if (trigger == triggerSlot.getStatement()) {
changed = it.next();
break;
}
}
}
if (changed == null) {
container.setTrigger(triggerSlot.slot, null, true);
} else {
container.setTrigger(triggerSlot.slot, changed.getUniqueTag(), true);
}
for (StatementParameterSlot p : triggerSlot.parameters) {
IStatementParameter parameter = null;
if (changed != null && p.slot < changed.minParameters()) {
parameter = changed.createParameter(p.slot);
}
container.setTriggerParameter(triggerSlot.slot, p.slot, parameter, true);
}
} else if (slot instanceof ActionSlot) {
ActionSlot actionSlot = (ActionSlot) slot;
IStatement changed = null;
if (actionSlot.getStatement() == null) {
if (k == 0) {
changed = container.getFirstAction();
} else {
changed = container.getLastAction();
}
} else {
Iterator<IStatement> it = container.getActionIterator(k != 0);
for (; it.hasNext();) {
IStatement action = it.next();
if (!it.hasNext()) {
changed = null;
break;
}
if (action == actionSlot.getStatement()) {
changed = it.next();
break;
}
}
}
if (changed == null) {
container.setAction(actionSlot.slot, null, true);
} else {
container.setAction(actionSlot.slot, changed.getUniqueTag(), true);
}
for (StatementParameterSlot p : actionSlot.parameters) {
IStatementParameter parameter = null;
if (changed != null && p.slot < changed.minParameters()) {
parameter = changed.createParameter(p.slot);
}
container.setActionParameter(actionSlot.slot, p.slot, parameter, true);
}
} else if (slot instanceof StatementParameterSlot) {
StatementParameterSlot paramSlot = (StatementParameterSlot) slot;
StatementSlot statement = paramSlot.statementSlot;
if (statement.isDefined() && statement.getStatement().maxParameters() != 0) {
IStatementParameter param = paramSlot.getParameter();
if (param == null) {
param = statement.getStatement().createParameter(paramSlot.slot);
}
if (param != null) {
param.onClick(gate, statement.getStatement(), mc.thePlayer.inventory.getItemStack(),
new StatementMouseClick(k, isShiftKeyDown()));
paramSlot.setParameter(param, true);
}
}
}
container.markDirty();
}
}