/
GuiMultiModule.java
371 lines (309 loc) · 10.5 KB
/
GuiMultiModule.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
package slimeknights.mantle.client.gui;
import com.google.common.collect.Lists;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.Optional;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.awt.*;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import slimeknights.mantle.Mantle;
import slimeknights.mantle.inventory.ContainerMultiModule;
import slimeknights.mantle.inventory.SlotWrapper;
@SideOnly(Side.CLIENT)
@Optional.Interface(iface = "codechicken.nei.api.INEIGuiHandler", modid = "NotEnoughItems")
// todo: NEI
public class GuiMultiModule extends GuiContainer { //implements INEIGuiHandler {
// NEI-stuff >:(
private static Field NEI_Manager;
static {
try {
NEI_Manager = GuiContainer.class.getDeclaredField("manager");
} catch(NoSuchFieldException e) {
NEI_Manager = null;
}
}
protected List<GuiModule> modules = Lists.newArrayList();
public int cornerX;
public int cornerY;
public int realWidth;
public int realHeight;
public GuiMultiModule(ContainerMultiModule container) {
super(container);
realWidth = -1;
realHeight = -1;
}
protected void addModule(GuiModule module) {
modules.add(module);
}
public List<Rectangle> getModuleAreas() {
List<Rectangle> areas = new ArrayList<Rectangle>(modules.size());
for(GuiModule module : modules) {
areas.add(module.getArea());
}
return areas;
}
@Override
public void initGui() {
if(realWidth > -1) {
// has to be reset before calling initGui so the position is getting retained
xSize = realWidth;
ySize = realHeight;
}
super.initGui();
this.cornerX = this.guiLeft;
this.cornerY = this.guiTop;
this.realWidth = xSize;
this.realHeight = ySize;
for(GuiModule module : modules) {
updateSubmodule(module);
}
//this.guiLeft = this.guiTop = 0;
//this.xSize = width;
//this.ySize = height;
}
@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
for(GuiModule module : modules) {
module.handleDrawGuiContainerBackgroundLayer(partialTicks, mouseX, mouseY);
}
}
@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
drawContainerName();
drawPlayerInventoryName();
for(GuiModule module : modules) {
// set correct state for the module
GlStateManager.pushMatrix();
GlStateManager.translate(-this.guiLeft, -this.guiTop, 0.0F);
GlStateManager.translate(module.guiLeft, module.guiTop, 0.0F);
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
module.handleDrawGuiContainerForegroundLayer(mouseX, mouseY);
GlStateManager.popMatrix();
}
}
protected void drawBackground(ResourceLocation background) {
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(background);
this.drawTexturedModalRect(cornerX, cornerY, 0, 0, realWidth, realHeight);
}
protected void drawContainerName() {
ContainerMultiModule multiContainer = (ContainerMultiModule) this.inventorySlots;
String localizedName = multiContainer.getInventoryDisplayName();
if(localizedName != null) {
this.fontRendererObj.drawString(localizedName, 8, 6, 0x404040);
}
}
protected void drawPlayerInventoryName() {
String localizedName = Minecraft.getMinecraft().player.inventory.getDisplayName().getUnformattedText();
this.fontRendererObj.drawString(localizedName, 8, this.ySize - 96 + 2, 0x404040);
}
@Override
public void setWorldAndResolution(Minecraft mc, int width, int height) {
super.setWorldAndResolution(mc, width, height);
// workaround for NEIs ASM hax. sigh.
try {
for(GuiModule module : modules) {
module.setWorldAndResolution(mc, width, height);
if(NEI_Manager != null) {
NEI_Manager.set(module, NEI_Manager.get(this));
}
updateSubmodule(module);
}
} catch(IllegalAccessException e) {
Mantle.logger.error(e);
}
}
@Override
public void onResize(@Nonnull Minecraft mc, int width, int height) {
super.onResize(mc, width, height);
for(GuiModule module : modules) {
module.onResize(mc, width, height);
updateSubmodule(module);
}
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
int oldX = guiLeft;
int oldY = guiTop;
int oldW = xSize;
int oldH = ySize;
guiLeft = cornerX;
guiTop = cornerY;
xSize = realWidth;
ySize = realHeight;
super.drawScreen(mouseX, mouseY, partialTicks);
guiLeft = oldX;
guiTop = oldY;
xSize = oldW;
ySize = oldH;
}
// needed to get the correct slot on clicking
@Override
protected boolean isPointInRegion(int left, int top, int right, int bottom, int pointX, int pointY) {
pointX -= this.cornerX;
pointY -= this.cornerY;
return pointX >= left - 1 && pointX < left + right + 1 && pointY >= top - 1 && pointY < top + bottom + 1;
}
protected void updateSubmodule(GuiModule module) {
module.updatePosition(this.cornerX, this.cornerY, this.realWidth, this.realHeight);
if(module.guiLeft < this.guiLeft) {
this.xSize += this.guiLeft - module.guiLeft;
this.guiLeft = module.guiLeft;
}
if(module.guiTop < this.guiTop) {
this.ySize += this.guiTop - module.guiTop;
this.guiTop = module.guiTop;
}
if(module.guiRight() > this.guiLeft + this.xSize) {
xSize = module.guiRight() - this.guiLeft;
}
if(module.guiBottom() > this.guiTop + this.ySize) {
ySize = module.guiBottom() - this.guiTop;
}
}
@Override
public void drawSlot(Slot slotIn) {
GuiModule module = getModuleForSlot(slotIn.slotNumber);
if(module != null) {
Slot slot = slotIn;
// unwrap for the call to the module
if(slotIn instanceof SlotWrapper) {
slot = ((SlotWrapper) slotIn).parent;
}
if(!module.shouldDrawSlot(slot)) {
return;
}
}
// update slot positions
if(slotIn instanceof SlotWrapper) {
slotIn.xPos = ((SlotWrapper) slotIn).parent.xPos;
slotIn.yPos = ((SlotWrapper) slotIn).parent.yPos;
}
super.drawSlot(slotIn);
}
@Override
public boolean isMouseOverSlot(Slot slotIn, int mouseX, int mouseY) {
GuiModule module = getModuleForSlot(slotIn.slotNumber);
// mouse inside the module of the slot?
if(module != null) {
Slot slot = slotIn;
// unwrap for the call to the module
if(slotIn instanceof SlotWrapper) {
slot = ((SlotWrapper) slotIn).parent;
}
if(!module.shouldDrawSlot(slot)) {
return false;
}
}
return super.isMouseOverSlot(slotIn, mouseX, mouseY);
}
@Override
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
GuiModule module = getModuleForPoint(mouseX, mouseY);
if(module != null) {
if(module.handleMouseClicked(mouseX, mouseY, mouseButton)) {
return;
}
}
super.mouseClicked(mouseX, mouseY, mouseButton);
}
@Override
protected void mouseClickMove(int mouseX, int mouseY, int clickedMouseButton, long timeSinceLastClick) {
GuiModule module = getModuleForPoint(mouseX, mouseY);
if(module != null) {
if(module.handleMouseClickMove(mouseX, mouseY, clickedMouseButton, timeSinceLastClick)) {
return;
}
}
super.mouseClickMove(mouseX, mouseY, clickedMouseButton, timeSinceLastClick);
}
@Override
protected void mouseReleased(int mouseX, int mouseY, int state) {
GuiModule module = getModuleForPoint(mouseX, mouseY);
if(module != null) {
if(module.handleMouseReleased(mouseX, mouseY, state)) {
return;
}
}
super.mouseReleased(mouseX, mouseY, state);
}
protected GuiModule getModuleForPoint(int x, int y) {
for(GuiModule module : modules) {
if(this.isPointInRegion(module.guiLeft, module.guiTop, module.guiRight(), module.guiBottom(),
x + this.cornerX, y + this.cornerY)) {
return module;
}
}
return null;
}
protected GuiModule getModuleForSlot(int slotNumber) {
return getModuleForContainer(getContainer().getSlotContainer(slotNumber));
}
protected GuiModule getModuleForContainer(Container container) {
for(GuiModule module : modules) {
if(module.inventorySlots == container) {
return module;
}
}
return null;
}
protected ContainerMultiModule getContainer() {
return (ContainerMultiModule) inventorySlots;
}
/* NEI INTEGRATION */
// todo: NEI
/*
@Override
@Optional.Method(modid = "NotEnoughItems")
public VisiblityData modifyVisiblity(GuiContainer guiContainer, VisiblityData visiblityData) {
int x = LayoutManager.stateButtons[0].x + LayoutManager.stateButtons[0].w;
int x2 = LayoutManager.timeButtons[3].x + LayoutManager.timeButtons[3].w;
int y2 = LayoutManager.heal.y + LayoutManager.heal.h;
for(GuiModule module : modules) {
if(x > module.guiLeft) {
visiblityData.showStateButtons = false;
}
if(x2 > module.guiLeft && y2 > module.guiTop) {
visiblityData.showUtilityButtons = false;
}
}
return visiblityData;
}
@Override
@Optional.Method(modid = "NotEnoughItems")
public Iterable<Integer> getItemSpawnSlots(GuiContainer guiContainer, ItemStack itemStack) {
return null;
}
@Override
@Optional.Method(modid = "NotEnoughItems")
public List<TaggedInventoryArea> getInventoryAreas(GuiContainer guiContainer) {
return Collections.EMPTY_LIST;
}
@Override
@Optional.Method(modid = "NotEnoughItems")
public boolean handleDragNDrop(GuiContainer guiContainer, int x, int y, ItemStack itemStack, int k) {
return false;
}
@Override
@Optional.Method(modid = "NotEnoughItems")
public boolean hideItemPanelSlot(GuiContainer guiContainer, int x, int y, int w, int h) {
for(GuiModule module : modules) {
// check if the panel overlaps the module (check totally not stolen from stackoverflow)
if(module.guiLeft < x + w && module.guiRight() > x &&
module.guiTop < y + h && module.guiBottom() > y) {
return true;
}
}
return false;
}
*/
}