-
Notifications
You must be signed in to change notification settings - Fork 497
/
GuiZonePlan.java
executable file
·395 lines (299 loc) · 9.96 KB
/
GuiZonePlan.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
/**
* 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.commander;
import java.util.LinkedList;
import java.util.List;
import org.lwjgl.opengl.GL11;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.inventory.IInventory;
import net.minecraft.util.IIcon;
import net.minecraft.util.ResourceLocation;
import buildcraft.api.core.EnumColor;
import buildcraft.core.BCDynamicTexture;
import buildcraft.core.DefaultProps;
import buildcraft.core.ZonePlan;
import buildcraft.core.gui.AdvancedSlot;
import buildcraft.core.gui.GuiAdvancedInterface;
import buildcraft.core.network.RPCHandler;
public class GuiZonePlan extends GuiAdvancedInterface {
private int mapWidth = 200;
private int mapHeight = 100;
private TileZonePlan zonePlan;
private BCDynamicTexture newSelection;
private int selX1 = 0,
selX2 = 0,
selY1 = 0,
selY2 = 0;
private boolean inSelection = false;
private BCDynamicTexture currentSelection;
private static final ResourceLocation TMP_TEXTURE = new ResourceLocation("buildcraft",
DefaultProps.TEXTURE_PATH_GUI + "/map_gui.png");
private int mapXMin = 0;
private int mapYMin = 0;
private int zoomLevel = 1;
private int cx;
private int cz;
private AreaSlot colorSelected = null;
private float alpha = 0.8F;
private GuiButton tool;
private List inventorySlots;
private List savedButtonList;
private static class AreaSlot extends AdvancedSlot {
public EnumColor color;
public AreaSlot(GuiAdvancedInterface gui, int x, int y, EnumColor iColor) {
super(gui, x, y);
color = iColor;
}
@Override
public IIcon getIcon() {
return color.getIcon();
}
@Override
public String getDescription() {
return color.getLocalizedName();
}
}
public GuiZonePlan(IInventory inventory, TileZonePlan iZonePlan) {
super(new ContainerZonePlan(inventory, iZonePlan), inventory, TMP_TEXTURE);
getContainer().gui = this;
xSize = 256;
ySize = 220;
zonePlan = iZonePlan;
getContainer().mapTexture = new BCDynamicTexture(mapWidth, mapHeight);
getContainer().mapTexture.createDynamicTexture();
currentSelection = new BCDynamicTexture(mapWidth, mapHeight);
currentSelection.createDynamicTexture();
newSelection = new BCDynamicTexture(1, 1);
newSelection.createDynamicTexture();
getContainer().currentAreaSelection = new ZonePlan();
cx = zonePlan.xCoord;
cz = zonePlan.zCoord;
slots = new AdvancedSlot[16];
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
slots[i * 4 + j] = new AreaSlot(this, 8 + 18 * i, 138 + 18 * j, EnumColor.values()[i * 4 + j]);
}
}
colorSelected = (AreaSlot) slots[0];
newSelection.setColor(0, 0, colorSelected.color.getDarkHex(), alpha);
uploadMap();
getContainer().loadArea(colorSelected.color.ordinal());
inventorySlots = container.inventorySlots;
}
@SuppressWarnings("unchecked")
@Override
public void initGui() {
super.initGui();
int x = (width - xSize) / 2;
int y = (height - ySize) / 2;
tool = new GuiButton(0, x + 5, y + 20, 20, 20, "+");
buttonList.add(tool);
savedButtonList = buttonList;
}
private void uploadMap() {
RPCHandler.rpcServer(getContainer(), "computeMap", cx, cz, getContainer().mapTexture.width,
getContainer().mapTexture.height,
zoomLevel);
}
@Override
protected void drawGuiContainerBackgroundLayer(float f, int x, int y) {
super.drawGuiContainerBackgroundLayer(f, x, y);
int cornerX = (width - xSize) / 2;
int cornerY = (height - ySize) / 2;
mapXMin = (width - getContainer().mapTexture.width) / 2;
if (getContainer().mapTexture.height <= 200) {
mapYMin = cornerY + 20;
} else {
mapYMin = (height - getContainer().mapTexture.height) / 2;
}
getContainer().mapTexture.drawMap(mapXMin, mapYMin, zLevel);
GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
GL11.glEnable(GL11.GL_BLEND);
currentSelection.drawMap(mapXMin, mapYMin, zLevel);
GL11.glPopAttrib();
newSelection.updateDynamicTexture();
if (inSelection && selX2 != 0) {
GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
GL11.glEnable(GL11.GL_BLEND);
int x1 = selX1 < selX2 ? selX1 : selX2;
int x2 = selX1 < selX2 ? selX2 : selX1;
int y1 = selY1 < selY2 ? selY1 : selY2;
int y2 = selY1 < selY2 ? selY2 : selY1;
drawTexturedModalRect(x1, y1, 0, 0, x2 - x1 + 1, y2 - y1 + 1);
GL11.glPopAttrib();
}
if (getContainer().mapTexture.height <= 200) {
drawBackgroundSlots();
bindTexture(texture);
drawTexturedModalRect(cornerX + colorSelected.x, cornerY + colorSelected.y, 0, 220, 16, 16);
drawTexturedModalRect(cornerX + 236, cornerY + 38, 16, 220, 8,
(int) ((zonePlan.progress / (float) TileZonePlan.CRAFT_TIME) * 27));
}
}
@Override
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) {
super.mouseClicked(mouseX, mouseY, mouseButton);
int blocksX = (mouseX - mapXMin) * zoomLevel;
int blocksZ = (mouseY - mapYMin) * zoomLevel;
int blockStartX = cx - mapWidth * zoomLevel / 2;
int blockStartZ = cz - mapHeight * zoomLevel / 2;
boolean clickOnMap = mouseX >= mapXMin
&& mouseX <= mapXMin + getContainer().mapTexture.width && mouseY >= mapYMin &&
mouseY <= mapYMin + getContainer().mapTexture.height;
if (clickOnMap) {
if (mouseButton == 1) {
cx = blockStartX + blocksX;
cz = blockStartZ + blocksZ;
uploadMap();
refreshSelectedArea();
} else {
inSelection = true;
selX1 = mouseX;
selY1 = mouseY;
selX2 = 0;
selY2 = 0;
}
} else {
int cornerX = (width - xSize) / 2;
int cornerY = (height - ySize) / 2;
int position = getSlotAtLocation(mouseX - cornerX, mouseY - cornerY);
AdvancedSlot slot = null;
if (position < 0) {
return;
}
slot = slots[position];
if (slot instanceof AreaSlot) {
colorSelected = (AreaSlot) slot;
newSelection.setColor(0, 0, colorSelected.color.getDarkHex(), alpha);
getContainer().loadArea(colorSelected.color.ordinal());
}
}
}
@Override
protected void mouseClickMove(int mouseX, int mouseY, int lastButtonBlicked, long time) {
super.mouseClickMove(mouseX, mouseY, lastButtonBlicked, time);
if (inSelection
&& mouseX >= mapXMin && mouseX <= mapXMin + getContainer().mapTexture.width
&& mouseY >= mapYMin && mouseY <= mapYMin + getContainer().mapTexture.height) {
selX2 = mouseX;
selY2 = mouseY;
}
}
@Override
protected void mouseMovedOrUp(int mouseX, int mouseY, int eventType) {
super.mouseMovedOrUp(mouseX, mouseY, eventType);
if (eventType != -1 && inSelection) {
boolean val = tool.displayString.equals("+");
int blockStartX = cx - mapWidth * zoomLevel / 2;
int blockStartZ = cz - mapHeight * zoomLevel / 2;
int x1 = selX1 < selX2 ? selX1 : selX2;
int x2 = selX1 < selX2 ? selX2 : selX1;
int y1 = selY1 < selY2 ? selY1 : selY2;
int y2 = selY1 < selY2 ? selY2 : selY1;
int lengthX = (x2 - x1) * zoomLevel;
int lengthY = (y2 - y1) * zoomLevel;
for (int i = 0; i <= lengthX; ++i) {
for (int j = 0; j <= lengthY; ++j) {
int x = blockStartX + (x1 - mapXMin) * zoomLevel + i;
int z = blockStartZ + (y1 - mapYMin) * zoomLevel + j;
getContainer().currentAreaSelection.set(x, z, val);
}
}
inSelection = false;
getContainer().saveArea(colorSelected.color.ordinal());
refreshSelectedArea();
}
}
@Override
protected void keyTyped(char carac, int val) {
super.keyTyped(carac, val);
if (carac == '+' && zoomLevel > 1) {
zoomLevel--;
uploadMap();
refreshSelectedArea();
} else if (carac == '-' && zoomLevel < 6) {
zoomLevel++;
uploadMap();
refreshSelectedArea();
} else if (carac == 'm') {
mapWidth = 200;
mapHeight = 100;
getContainer().mapTexture = new BCDynamicTexture(mapWidth, mapHeight);
getContainer().mapTexture.createDynamicTexture();
currentSelection = new BCDynamicTexture(mapWidth, mapHeight);
currentSelection.createDynamicTexture();
uploadMap();
refreshSelectedArea();
container.inventorySlots = inventorySlots;
buttonList = savedButtonList;
} else if (carac == 'M') {
mapWidth = this.mc.displayWidth;
mapHeight = this.mc.displayHeight;
getContainer().mapTexture = new BCDynamicTexture(mapWidth, mapHeight);
getContainer().mapTexture.createDynamicTexture();
currentSelection = new BCDynamicTexture(mapWidth, mapHeight);
currentSelection.createDynamicTexture();
uploadMap();
refreshSelectedArea();
container.inventorySlots = new LinkedList();
buttonList = new LinkedList();
}
}
public void refreshSelectedArea() {
int color = colorSelected.color.getDarkHex();
int rAdd = (color >> 16) & 255;
int gAdd = (color >> 8) & 255;
int bAdd = color & 255;
for (int i = 0; i < currentSelection.width; ++i) {
for (int j = 0; j < currentSelection.height; ++j) {
int blockStartX = cx - mapWidth * zoomLevel / 2;
int blockStartZ = cz - mapHeight * zoomLevel / 2;
double r = 0;
double g = 0;
double b = 0;
for (int stepi = 0; stepi < zoomLevel; ++stepi) {
for (int stepj = 0; stepj < zoomLevel; ++stepj) {
int x = blockStartX + i * zoomLevel + stepi;
int z = blockStartZ + j * zoomLevel + stepj;
if (getContainer().currentAreaSelection.get(x, z)) {
r += rAdd;
g += gAdd;
b += bAdd;
}
}
}
r /= (zoomLevel * zoomLevel);
g /= (zoomLevel * zoomLevel);
b /= (zoomLevel * zoomLevel);
r /= 255F;
g /= 255F;
b /= 255F;
if (r != 0) {
currentSelection.setColor(i, j, r, g, b, alpha);
} else {
currentSelection.setColor(i, j, 0, 0, 0, 0);
}
}
}
}
@Override
protected ContainerZonePlan getContainer() {
return (ContainerZonePlan) super.getContainer();
}
@Override
protected void actionPerformed(GuiButton button) {
if (button == tool) {
if (tool.displayString.equals("+")) {
tool.displayString = "-";
} else {
tool.displayString = "+";
}
}
}
}