- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockListGUI.java
More file actions
Latest commit
479 lines (433 loc) · 24.3 KB
/
Copy pathBlockListGUI.java
File metadata and controls
479 lines (433 loc) · 24.3 KB
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
importjavax.imageio.ImageIO;
importjavax.swing.*;
importjavax.swing.event.MouseInputAdapter;
importjava.awt.*;
importjava.awt.event.MouseWheelEvent;
importjava.awt.event.MouseWheelListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.ArrayList;
publicclassBlockListGUI {
//FIELDS
privatestaticbooleanisDropDownOpen = false; //if the drop down is open
privatestaticStringsortOrder = "asc"; //which order we want to show at the top of gui
privatestaticbooleanfirstButton = true; // if the first button of the true/false on the gui is pressed
privatestaticStringsortType = "Name"; //what we are sorting by
protectedstaticintmouseY; // the mouse cursors y position, this is to handle hovering and clicking on the gui
protectedstaticintmouseX; // the mouse cursors x position, this is to handle hovering and clicking on the gui
privatestaticintscrollDist = 0; // the distance scrolled down by the mousewheel, this allows the list to scroll and dynamically render on the canvas
privatestaticMap<String, BufferedImage> imageCache = newHashMap<>(); //this optimises the images we are rendering through buffered images, so it dosent have to create a new object each time
privatestaticString[] blocksArray; // this is an array of all the block names, from the blocks.txt file
privatestaticJPanelblankCanvasPanel; // i dont know why this is here but shit breaks if its gone
privatestaticStringbackgroundType = "HighRes/Background 1.png";
//THE GUI
publicstaticvoidblockListGUI() {
parserAndReadinparser = newparserAndReadin(); // initiallize new parser
performSortingMethodssorter = newperformSortingMethods(); // initialize new sortingmethods
ArrayList<Block> blocks = parser.readInData("data/Blocks.txt"); // read in our blocks data file
blocksArray = sorter.sortBlockParameter("name", "asc"); // Initial sorting when gui open
//CREATE THE JFRAME
JFramenewFrame = newJFrame("View Blocks");
newFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // Change default close operation so it dosen exit program entirely
newFrame.setSize(Main.FRAME_WIDTH, Main.FRAME_HEIGHT); //get dimensions of 1080x720 from main.java
newFrame.setResizable(false); //dont allow the canvas to be resizes
//CREATE A BLANK JPANNEL
blankCanvasPanel = newJPanel() {
@Override
protectedvoidpaintComponent(Graphicsg) { //use paint component to draw
super.paintComponent(g);
//BACKGROUND WITH SCROLL OFFSET
BufferedImagebackgroundImage = loadImage(backgroundType);
if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, -2000 + 50 + (scrollDist) / 3, 1080, 10000, this);
}
/*
BufferedImage backgroundDarken = loadImage("ViewPannelAssets/Darken.png"); //darken the background by putting a shitty overlay onto it
if (backgroundDarken != null) {
g.drawImage(backgroundDarken, 0, 0, 1080, 720, this);
}
*/
//LOAD THE SIDEBAR ON THE RIGHT SIDE OF THE SCREEN
BufferedImageoverlay = loadImage("ViewPannelAssets\\Sidebar.png");
if (overlay != null) {
g.drawImage(overlay, 0, 0, getWidth(), getHeight(), this);
}
//CREATE A NEW FONT
Fontfont = newFont("Arial", Font.PLAIN, 60); //create a new font for later
intnumberOfObjects = 0; //unused, would display number of rendered objects in the top bar
intsizeOfFrontPng = 0; //side of block image (it gets larger when hovered)
//RUN PARSER AGAIN (why idk)
parserAndReadinparser = newparserAndReadin();
ArrayList<Block> blocks = parser.readInData("data/Blocks.txt");
//LOAD THE BLOCK LIST (SORTED)
for (inti = 0; i < blocksArray.length; i++) { //iterate through all blocks in the sorted list
inty = (scrollDist) + (i * 100) + 100 + 20; // Adjust Y position based on loop iteration (for drwaing name and desc)
Blockblock = parser.getBlockByName(blocks, blocksArray[i]);
if (isWithinButtonRange(mouseX, mouseY, 5, 950, (scrollDist) + (i * 100) + 50, (scrollDist) + (i * 100) + 150) && !isDropDownOpen) { //if hovered and drop down isnt open
BufferedImagehighlighted = loadImage("ViewPannelAssets\\BackGroundOfBlockSelected.png"); //draw selected
if (highlighted != null) {
g.drawImage(highlighted, 0 - 5, (scrollDist) + (i * 100) - 5, getWidth() + 10, getHeight() + 10, this); //draw background
g.setColor(Color.WHITE); //set the color of the text for later, and change the size
font = newFont("Arial", Font.PLAIN, 50); //text when hovered is a bit bigger then not
sizeOfFrontPng = 70; //change side of block image
}
BufferedImageblockFront = loadImage("object\\" + block.getName() + "\\" + "front.jpg.jpg"); // add front png of block to block
if (blockFront != null) {
g.drawImage(blockFront, 25, y-60, sizeOfFrontPng, sizeOfFrontPng, this);
} else {
blockFront = loadImage("object\\" + "front.jpg.jpg");
g.drawImage(blockFront, 25, y-60, sizeOfFrontPng, sizeOfFrontPng, this);
}
} else {
BufferedImageunselected = loadImage("ViewPannelAssets\\BackGroundOfBlockUnselected.png"); //draw unselected
if (unselected != null) {
g.drawImage(unselected, 0, (scrollDist) + (i * 100), getWidth(), getHeight(), this); //draw background of object (unselected)
g.setColor(Color.LIGHT_GRAY); //set the color of the text for later, and change the size
font = newFont("Arial", Font.PLAIN, 40); //text when not hovered is a bit smaller then hovered
sizeOfFrontPng = 60; //change side of block image
}
BufferedImageblockFront = loadImage("object\\" + block.getName() + "\\" + "front.jpg.jpg"); // add front png of block to block
if (blockFront != null) {
g.drawImage(blockFront, 30, y-50, sizeOfFrontPng, sizeOfFrontPng, this);
} else {
blockFront = loadImage("object\\" + "front.jpg.jpg");
g.drawImage(blockFront, 25, y-60, sizeOfFrontPng, sizeOfFrontPng, this);
}
}
//DRAW THE TEXT FOR OBJECTS
g.setFont(font); // finish setting the done
Stringtext = String.valueOf(i + 1); //get the index of the block
g.drawString(text, 990, y); // draw the index of block on right side
text = blocksArray[i]; //get name of block
g.drawString(text, 110, y); //draw the name of each block
//DRAW DESCRIPTION
font = newFont("Arial", Font.PLAIN, 20); //font for details
g.setFont(font);
StringflammableInfo = ""; //get flammable info first
if (block.getFlammable())
flammableInfo = "This block also catches fire.";
else
flammableInfo = "This block is not flammable.";
g.drawString("It spawns in the " + block.getDimension() + ". Stackability: " + block.getStackability() +"." , 450, y-40);
g.drawString("Hardness: " + block.getHardness() + ". Blast Resistance: " + block.getBlastres() + ". Light lvl: " + block.getLuminous() + ".", 450, y+25-40);
g.drawString("Is Renewable: " + block.getRenewability() + ". " + flammableInfo + ".", 450, y+50-40 ); //bild da strings
numberOfObjects += 1; //unused for now
}
BufferedImagetextBar = loadImage("ViewPannelAssets\\TopTypeBar.png"); // add a demo typing bar before we add the actually one (if time permits)
if (textBar != null) {
g.drawImage(textBar, 0, 0, getWidth(), getHeight(), this);
}
BufferedImagequantityBG = loadImage("ViewPannelAssets\\Spacer.png"); // add spacer to top bar
if (quantityBG != null) {
g.drawImage(quantityBG, 0, 0, getWidth(), getHeight(), this);
}
StringbuttonFilePath = ""; // File path for dropdown button
StringbuttonFilePathON = ""; // File path for top
StringbuttonFilePathOFF = ""; // File path for bottom
switch (sortType) { // case statement to get file paths of sort method buttons
case"Name":
buttonFilePath = "ViewPannelAssets\\Name.png";
if (firstButton) {
buttonFilePathON = "ViewPannelAssets\\A-ZON.png";
buttonFilePathOFF = "ViewPannelAssets\\Z-AOFF.png";
} else {
buttonFilePathON = "ViewPannelAssets\\A-ZOFF.png";
buttonFilePathOFF = "ViewPannelAssets\\Z-AON.png";
}
break;
case"Stackability":
buttonFilePath = "ViewPannelAssets\\Stack.png";
if (firstButton) {
buttonFilePathOFF = "ViewPannelAssets\\StackedOFF.png";
buttonFilePathON = "ViewPannelAssets\\NotON.png";
} else {
buttonFilePathOFF = "ViewPannelAssets\\StackedON.png";
buttonFilePathON = "ViewPannelAssets\\NotOFF.png";
}
break;
case"Dimension":
buttonFilePath = "ViewPannelAssets\\Dimension.png";
if (firstButton) {
buttonFilePathON = "ViewPannelAssets\\TopON.png";
buttonFilePathOFF = "ViewPannelAssets\\BottomOFF.png";
} else {
buttonFilePathON = "ViewPannelAssets\\TopOFF.png";
buttonFilePathOFF = "ViewPannelAssets\\BottomON.png";
}
break;
case"Hardness":
buttonFilePath = "ViewPannelAssets\\Hardness.png";
if (firstButton) {
buttonFilePathOFF = "ViewPannelAssets\\HardestOFF.png";
buttonFilePathON = "ViewPannelAssets\\SoftestON.png";
} else {
buttonFilePathOFF = "ViewPannelAssets\\HardestON.png";
buttonFilePathON = "ViewPannelAssets\\SoftestOFF.png";
}
break;
case"BlastRes":
buttonFilePath = "ViewPannelAssets\\BlastRes.png";
if (firstButton) {
buttonFilePathOFF = "ViewPannelAssets\\MostOFF.png";
buttonFilePathON = "ViewPannelAssets\\LeastON.png";
} else {
buttonFilePathOFF = "ViewPannelAssets\\MostON.png";
buttonFilePathON = "ViewPannelAssets\\LeastOFF.png";
}
break;
case"Renewability":
buttonFilePath = "ViewPannelAssets\\Renewability.png";
if (firstButton) {
buttonFilePathOFF = "ViewPannelAssets\\TrueOFF.png";
buttonFilePathON = "ViewPannelAssets\\FalseON.png";
} else {
buttonFilePathOFF = "ViewPannelAssets\\TrueON.png";
buttonFilePathON = "ViewPannelAssets\\FalseOFF.png";
}
break;
case"Luminous":
buttonFilePath = "ViewPannelAssets\\Luminous.png";
if (firstButton) {
buttonFilePathOFF = "ViewPannelAssets\\MostOFF.png";
buttonFilePathON = "ViewPannelAssets\\LeastON.png";
} else {
buttonFilePathOFF = "ViewPannelAssets\\MostON.png";
buttonFilePathON = "ViewPannelAssets\\LeastOFF.png";
}
break;
case"Flammable":
buttonFilePath = "ViewPannelAssets\\Flammable.png";
if (firstButton) {
buttonFilePathOFF = "ViewPannelAssets\\TrueOFF.png";
buttonFilePathON = "ViewPannelAssets\\FalseON.png";
} else {
buttonFilePathOFF = "ViewPannelAssets\\TrueON.png";
buttonFilePathON = "ViewPannelAssets\\FalseOFF.png";
}
break;
}
BufferedImageactiveSortMethod = loadImage(buttonFilePath);
if (activeSortMethod != null) {
g.drawImage(activeSortMethod, 544, 0, 276, 50, this);
}
BufferedImagesortOption1 = loadImage(buttonFilePathON);
if (sortOption1 != null) {
g.drawImage(sortOption1, 820, 0, 130, 50, this);
}
BufferedImagesortOption2 = loadImage(buttonFilePathOFF);
if (sortOption2 != null) {
g.drawImage(sortOption2, 950, 0, 130, 50, this);
}
if (isDropDownOpen) {
StringsortOption[] = {"Name", "Stack", "Dimension", "Hardness", "BlastRes", "Renewability", "Luminous", "Flammable"}; //IGNORE CHU
intdropdownY = 50;
for (Stringoption : sortOption) { //when drop down open, load all the buttons
BufferedImagesortMethod = loadImage("ViewPannelAssets\\" + option + ".png");
if (sortMethod != null) {
g.drawImage(sortMethod, 544, dropdownY, 276, 50, this);
}
dropdownY += 50;
}
}
BufferedImagetooltip = loadImage("ViewPannelAssets\\Tooltip.png"); //load the tooltip at the bottom of the screen
if (tooltip != null) {
g.drawImage(tooltip, 0, 0, 1080, 720, this);
}
}
};
blankCanvasPanel.setPreferredSize(newDimension(Main.FRAME_WIDTH, Main.FRAME_HEIGHT));
blankCanvasPanel.addMouseListener(newMouseInputAdapter() {
@Override
publicvoidmouseClicked(java.awt.event.MouseEvente) {
handleClickEventSort(e.getX(), e.getY());
}
});
// Add mouse listener to canvas panel to track mouse movements
blankCanvasPanel.addMouseMotionListener(newMouseInputAdapter() {
@Override
publicvoidmouseMoved(java.awt.event.MouseEvente) {
mouseX = (e.getX());
mouseY = (e.getY());
}
});
blankCanvasPanel.addMouseWheelListener(newMouseWheelListener() {
@Override
publicvoidmouseWheelMoved(MouseWheelEvente) { // when scrolling update the background and object list
intscrollAmount = e.getWheelRotation();
if (scrollAmount < 0) {
if(scrollDist > 0) {
} else {
scrollDist += 20;
}
} elseif (scrollAmount > 0) {
scrollDist -= 20;
}
blankCanvasPanel.repaint();
}
});
newFrame.addWindowListener(newWindowAdapter() { //when the window closes dont close everything, just go back to the main menu
@Override
publicvoidwindowClosing(WindowEvente) {
// Action to perform when the window is closing
//SwingUtilities.invokeLater(MainMenuGUI::mainMenu); // Opens the main menu again
newFrame.dispose(); // Dispose of the current frame
}
});
newFrame.add(blankCanvasPanel);
newFrame.pack();
newFrame.setVisible(true);
Timertimer = newTimer(Main.FRAME_RATE, e -> blankCanvasPanel.repaint()); //update the screen at 60fps
timer.start();
}
privatestaticBufferedImageloadImage(Stringfilename) { //load image with debugging (thanks chu)
if (imageCache.containsKey(filename)) {
returnimageCache.get(filename);
} else {
Filefile = newFile(filename);
//System.out.println("Attempting to load image from: " + file.getAbsolutePath());
if (!file.exists()) {
//System.err.println("Error: File does not exist at path: " + file.getAbsolutePath());
returnnull;
}
if (!file.canRead()) {
//System.err.println("Error: Cannot read file at path: " + file.getAbsolutePath());
returnnull;
}
try {
BufferedImageimage = ImageIO.read(file);
if (image != null) {
imageCache.put(filename, image);
returnimage;
} else {
//System.err.println("Error: File format not supported or file is corrupt: " + file.getAbsolutePath());
returnnull;
}
} catch (IOExceptione) {
//System.err.println("IOException while reading the image file: " + file.getAbsolutePath());
//e.printStackTrace();
returnnull;
}
}
}
privatestaticbooleanisWithinButtonRange(intmouseX, intmouseY, intbuttonX, intbuttonWidth, intbuttonY, intbuttonHeight) {
returnmouseX > buttonX && mouseX < buttonWidth && mouseY > buttonY && mouseY < buttonHeight;
} //if the mouse cursor is in a button range
staticclassSortType { //handles sort button stuff
Stringtype;
StringbuttonFilePath;
StringbuttonFilePathON;
StringbuttonFilePathOFF;
publicSortType(Stringtype, StringbuttonFilePath, StringbuttonFilePathON, StringbuttonFilePathOFF) {
this.type = type;
this.buttonFilePath = buttonFilePath;
this.buttonFilePathON = buttonFilePathON;
this.buttonFilePathOFF = buttonFilePathOFF;
}
}
privatestaticvoidhandleClickEventSort(intx, inty) { //handle click event for the sorting methods
if (!isDropDownOpen) {
if (isWithinButtonRange(x, y, 544, 819, 0, 50)) {
isDropDownOpen = true;
} elseif (isWithinButtonRange(x, y, 820, 950, 0, 50)) {
firstButton = true;
performSortAction(sortType);
} elseif (isWithinButtonRange(x, y, 950, 1080, 0, 50)) {
firstButton = false;
performSortAction(sortType);
}
for (inti = 0; i < blocksArray.length; i++) { // iterate through all the blocks and figure which one user has clicked on
parserAndReadinparser = newparserAndReadin();
ArrayList<Block> blocks = parser.readInData("data/Blocks.txt");
// if hovered over the block in iteration
if(isWithinButtonRange(mouseX, mouseY, 5, 950, (scrollDist) + (i * 100) + 50, (scrollDist) + (i * 100) + 150) && !isDropDownOpen) {
System.out.println(blocksArray[i]);
Blockblock = parser.getBlockByName(blocks, blocksArray[i]); //get the name of block then
SwingUtilities.invokeLater(() -> { //run the 3d gui for that block
BlockView3dGUIview = newBlockView3dGUI();
view.blockView3dGUI(block);
});
}
}
} else { // else if the dropdown is open, allow user to click on the other sort methods
if (isWithinButtonRange(x, y, 820, 950, 0, 50)) {
firstButton = true;
performSortAction(sortType);
} elseif (isWithinButtonRange(x, y, 950, 1080, 0, 50)) {
firstButton = false;
performSortAction(sortType);
} elseif (isWithinButtonRange(x, y, 544, 819, 50, 100)) {
sortType = "Name";
isDropDownOpen = false;
performSortAction("Name");
scrollDist = 0;
backgroundType = "HighRes/Background 1.png";
} elseif (isWithinButtonRange(x, y, 544, 819, 100, 150)) {
sortType = "Stackability";
isDropDownOpen = false;
performSortAction("Stackability");
scrollDist = 0;
backgroundType = "HighRes/Background 2.png";
} elseif (isWithinButtonRange(x, y, 544, 819, 150, 200)) {
sortType = "Dimension";
isDropDownOpen = false;
performSortAction("Dimension");
scrollDist = 0;
backgroundType = "HighRes/Background 3.png";
} elseif (isWithinButtonRange(x, y, 544, 819, 200, 250)) {
sortType = "Hardness";
isDropDownOpen = false;
performSortAction("Hardness");
scrollDist = 0;
backgroundType = "HighRes/Background 4.png";
} elseif (isWithinButtonRange(x, y, 544, 819, 250, 300)) {
sortType = "BlastRes";
isDropDownOpen = false;
performSortAction("BlastRes");
scrollDist = 0;
backgroundType = "HighRes/Background 5.png";
} elseif (isWithinButtonRange(x, y, 544, 819, 300, 350)) {
sortType = "Renewability";
isDropDownOpen = false;
performSortAction("Renewability");
scrollDist = 0;
backgroundType = "HighRes/Background 6.png";
} elseif (isWithinButtonRange(x, y, 544, 819, 350, 400)) {
sortType = "Luminous";
isDropDownOpen = false;
performSortAction("Luminous");
backgroundType = "HighRes/Background 7.png";
} elseif (isWithinButtonRange(x, y, 544, 819, 400, 450)) {
sortType = "Flammable";
isDropDownOpen = false;
performSortAction("Flammable");
scrollDist = 0;
backgroundType = "HighRes/Background 8.png";
} else {
isDropDownOpen = false;
scrollDist = 0;
}
}
blankCanvasPanel.repaint();
}
// Add the action to perform when a sort type is selected
privatestaticvoidperformSortAction(StringsortType) {
performSortingMethodssorter = newperformSortingMethods();
if (firstButton == false) {
sortOrder = "desc";
} else {
sortOrder = "asc";
}
blocksArray = sorter.sortBlockParameter(sortType, sortOrder);
}
// Add logic for handling selection
privatestaticvoidhandleBlockSelection(intx, inty) {
// Add logic for handling selection
System.out.println("selection at: " + x + ", " + y);
// Determine the block based on x, y coordinates and handle accordingly
}
}