-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorTileMenu.java
More file actions
541 lines (477 loc) · 19.8 KB
/
EditorTileMenu.java
File metadata and controls
541 lines (477 loc) · 19.8 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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import com.sun.istack.internal.Nullable;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
public class EditorTileMenu extends JDialog {
//Frame Components:
private JPanel contentPane;
private JPanel west;
private JPanel south;
private JLabel selectedLabel;
private JButton btOk;
private JPanel north;
private JPanel center;
private JTabbedPane tsTabPane;
private JMenuBar menubar = new JMenuBar(); //Menüleiste erzeugen
private int selectedID;
private int selectedTileSet;
private ArrayList<EditorTileTab> tileTabs = new ArrayList<>();
private Editor belongingEditor;
private Frame owner;
private boolean firstStart = true;
public EditorTileMenu(Frame owner, boolean modal, Editor pBelongingEditor) {
super(owner, modal);
this.owner = owner;
belongingEditor = pBelongingEditor;
selectedID = 22; // Wiese
$$$setupUI$$$();
setContentPane(contentPane);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
btOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onOK();
}
});
createTileSet();
setVisible(false);
setSize(500, 300);
}
private void onOK() {
belongingEditor.maps.get(belongingEditor.selectedMap).setGraphicID(selectedID);
belongingEditor.addRecently(selectedID, tileTabs.get(selectedTileSet).getTabTileSet());
setVisible(false);
}
public void createTileSet() {
//Standard TileSet:
if (firstStart) {
TileSet ts = new TileSet("Content/Graphics/tileSets/12x12x3 - tileSet.png", 12, 12, 3);
addCustomTileSet(ts);
firstStart = false;
} else {
addTileSetThroughFileRequest();
}
}
public void selectedinLabel(JLabel anzeige, @Nullable Icon icon) {
try {
anzeige.setIcon(icon);
} catch (Exception e) {
for (int i = 0; i < tileTabs.size(); i++) {
anzeige.setIcon(new ImageIcon(tileTabs.get(selectedTileSet).getTabTileSet().tileSet[selectedID].tileImage));
}
}
}
public int findAndSetTileSetID(TileSet ts) {
for (int i = 0; i < tileTabs.size(); i++) {
if (ts.getTileSetImagePath().equals(tileTabs.get(i).getTabTileSet().getTileSetImagePath())) {
return i;
}
}
return 9999;
}
public int getSelectedID() {
return selectedID;
}
public int getSelectedTileSetIndex() {
return selectedTileSet;
}
public void setTileTabs(EditorTileTab pTileSet) {
tileTabs.add(pTileSet);
selectedTileSet = tileTabs.size() - 1;
}
public TileSet getTileSet(int index) {
return tileTabs.get(index).getTabTileSet();
}
public void setSelectedID(int selectedID) {
this.selectedID = selectedID;
}
/**
* Method generated by IntelliJ IDEA GUI Designer
* >>> IMPORTANT!! <<<
* DO NOT edit this method OR call it in your code!
*
* @noinspection ALL
*/
private void $$$setupUI$$$() {
createUIComponents();
contentPane.setLayout(new BorderLayout(0, 0));
west = new JPanel();
west.setLayout(new BorderLayout(0, 0));
west.setMinimumSize(new Dimension(50, 50));
contentPane.add(west, BorderLayout.WEST);
south = new JPanel();
south.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
contentPane.add(south, BorderLayout.SOUTH);
btOk = new JButton();
btOk.setHorizontalTextPosition(2);
btOk.setMaximumSize(new Dimension(300, 50));
btOk.setMinimumSize(new Dimension(100, 50));
btOk.setPreferredSize(new Dimension(100, 50));
btOk.setText("Bestaetigen");
south.add(btOk);
selectedLabel = new JLabel();
selectedLabel.setHorizontalAlignment(0);
selectedLabel.setHorizontalTextPosition(2);
selectedLabel.setMaximumSize(new Dimension(100, 100));
selectedLabel.setPreferredSize(new Dimension(50, 50));
selectedLabel.setText("");
selectedLabel.setVerifyInputWhenFocusTarget(false);
south.add(selectedLabel);
north = new JPanel();
north.setLayout(new BorderLayout(0, 0));
contentPane.add(north, BorderLayout.NORTH);
center = new JPanel();
center.setLayout(new BorderLayout(0, 0));
contentPane.add(center, BorderLayout.CENTER);
tsTabPane = new JTabbedPane();
center.add(tsTabPane, BorderLayout.CENTER);
}
/**
* @noinspection ALL
*/
public JComponent $$$getRootComponent$$$() {
return contentPane;
}
private void createUIComponents() {
contentPane = new JPanel();
menubar = new JMenuBar(); //Menüleiste erzeugen
setJMenuBar(menubar); //Menüleiste dem Fenster hinzufügen
JMenuItem open = new JMenuItem("Öffnen");
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
belongingEditor.loadCustomMap();
}
});
JMenuItem save = new JMenuItem("Speichern");
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
belongingEditor.saveCurrentMap(Meldungen.getFileAt("Save"));
}
});
JMenu newMap = new JMenu("Neue Map anlegen");
JMenuItem newMapWithSelection = new JMenuItem("Neue Map mit dem ausgewählten Tile erstellen");
newMapWithSelection.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Meldungen meldung = new Meldungen(owner, true, "Map");
belongingEditor.createEditorMap(Integer.parseInt(meldung.getUserInput(0)), Integer.parseInt(meldung.getUserInput(0)), tileTabs.get(selectedTileSet).getTabTileSet(), selectedID);
}
});
JMenuItem newBlankMap = new JMenuItem("Neue leere Map");
newBlankMap.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
TileSet tempTS = new TileSet("Content/Graphics/tileSets/12x12x3 - tileSet.png", 12, 12, 3);
Meldungen meldung = new Meldungen(owner, true, "Map");
belongingEditor.createBlankEditorMap(Integer.parseInt(meldung.getUserInput(0)), Integer.parseInt(meldung.getUserInput(0)), tempTS);
}
});
JMenuItem newItemMap = new JMenuItem("Neue Item Map");
newItemMap.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
TileSet tempTS = new TileSet("Content/Graphics/tileSets/16x16x0 - tileSetItems.png", 16, 16, 0);
Meldungen meldung = new Meldungen(owner, true, "Map");
belongingEditor.createEditorMap(Integer.parseInt(meldung.getUserInput(0)), Integer.parseInt(meldung.getUserInput(0)), tempTS, 0);
}
});
JMenuItem load = new JMenuItem("Neues Tile Set laden");
load.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
createTileSet();
}
});
menubar.add(newMap);
newMap.add(newMapWithSelection);
newMap.add(newBlankMap);
newMap.add(newItemMap);
menubar.add(open);
menubar.add(save);
menubar.add(load);
}
public boolean checkTileSet(TileSet ts) {
for (int i = 0; i < tileTabs.size(); i++) {
if (tileTabs.get(i).getTabTileSet().getTileSetImagePath().contentEquals(ts.getTileSetImagePath())) {
return true;
}
}
return false;
}
public void addTileSetThroughFileRequest() {
File[] f = Meldungen.getFilesAt("TileSet");
for (int i = 0; i < f.length; i++) {
addCustomTileSet(new TileSet(f[i].getPath()));
}
}
public void addCustomTileSet(TileSet ts) {
tileTabs.add(new EditorTileTab(tsTabPane, ts, Integer.toString(tileTabs.size()), false));
selectedTileSet = tileTabs.size() - 1;
}
class EditorTileTab implements MouseListener {
int columns = 5;
int gap = 2;
String name = "k.A.";
private JPanel tilePanel;
private TileSet tabTileSet;
private boolean filter;
private EditorTileButton[] tiles;
private LinkedList<Integer> filterIDs;
public EditorTileTab(JTabbedPane location, TileSet ts, String pName, boolean filter) {
tabTileSet = ts;
name = pName;
filterIDs = new LinkedList<>();
createTabComponents(location, filter);
}
public void filterImages() {
for (int i = 0; i < tiles.length; i++) {
if (!isImageEmpty(tiles[i].getImg())) {
filterIDs.add(i);
}
}
}
public void showFilter() {
if (filter) {
createTileButtons(tilePanel, false);
} else {
createTileButtons(tilePanel, true);
}
}
public void showFilter(JPanel display, boolean b) {
if (filterIDs.isEmpty()) {
filterImages();
}
if (b) {
//Nur Tiles mit FilterID:
for (int j = 0; j < filterIDs.size(); j++) {
display.add(tiles[filterIDs.get(j)]);
}
} else {
//Alle Tiles:
for (int i = 0; i < tiles.length; i++) {
display.add(tiles[i]);
}
}
filter = b;
display.revalidate();
}
public TileSet getTabTileSet() {
return tabTileSet;
}
public void zoom(boolean zoomInIsTrueZoomOutisFalse, double zoomfactor) {
if (zoomInIsTrueZoomOutisFalse) {
//Reinzoom:
zoomfactor = 1 - zoomfactor;
} else {
//Rauszoom:
zoomfactor = 1 + zoomfactor;
}
EditorTileButton.setTileButtonSize((int) Math.round(EditorTileButton.SIZE * zoomfactor));
tilePanel.removeAll();
recalculateGrid(tilePanel.getParent().getWidth());
createTileButtons(tilePanel, filter);
}
private boolean isImageEmpty(BufferedImage imgA) {
float maxTransparentAmount = 99;
int detectionQuality = 1; /*Peformance = höher ; Genauigkeit = niedriger (1)*/
int pixelTransparent = 0;
int width = imgA.getWidth();
int height = imgA.getHeight();
for (int y = 0; y < height; y += detectionQuality) {
for (int x = 0; x < width; x += detectionQuality) {
// Compare the pixels for equality.
int pixel = imgA.getRGB(x, y);
if ((pixel >> 24) == 0x00) {
pixelTransparent++;
}
}
}
float percent = (pixelTransparent * detectionQuality) * 100f / (width * height);
return percent >= maxTransparentAmount;
}
public void createTabComponents(JTabbedPane tabbedPane, boolean filter) {
tilePanel = new JPanel();
createTileButtons(tilePanel, filter);
JScrollPane scrollPane = new JScrollPane();
scrollPane.add(tilePanel);
scrollPane.setMaximumSize(scrollPane.getSize());
scrollPane.addComponentListener(new ComponentListener() {
@Override
public void componentResized(ComponentEvent e) {
tilePanel.setMaximumSize(scrollPane.getSize());
recalculateGrid(scrollPane.getWidth());
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
});
scrollPane.getVerticalScrollBar().setUnitIncrement(8);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setOpaque(true);
scrollPane.setViewportView(tilePanel);
tabbedPane.addTab("Tile Set " + name, scrollPane);
}
public JButton[] createStandardButtons(JPanel display) {
//Erstellen der Standard Buttons: Zoom BTs - TextEingabe BT- empty Button
String[] nam = {"+", "-", "<html>Filter: <br>" + filter + "</html>"};
JButton[] buttons = new JButton[nam.length];
for (int i = 0; i < nam.length; i++) {
buttons[i] = new JButton(nam[i]);
buttons[i].setMargin(new Insets(0, 0, 0, 0));
buttons[i].setBorder(null);
buttons[i].setPreferredSize(new Dimension(EditorTileButton.SIZE, EditorTileButton.SIZE));
setFontAsBigAsSize(buttons[i]);
}
buttons[0].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
zoom(false, 0.25);
}
});
buttons[1].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
zoom(true, 0.25);
}
});
buttons[2].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showFilter();
buttons[2].setText("<html>Filter:<br>" + filter + "</html>");
}
});
buttons[2].setFont(new Font(buttons[2].getFont().getName(), Font.PLAIN, 11));
return buttons;
}
public void setFontAsBigAsSize(JButton bt) {
boolean tooBig = false;
int size = 1;
String name = bt.getText();
if (name.contentEquals("<html>")) {
name = name.replaceAll("<html>", "");
}
Font f = new Font(bt.getFont().getName(), Font.PLAIN, size);
bt.setFont(f);
while (!tooBig) {
FontMetrics metric = bt.getFontMetrics(bt.getFont());
if (metric.getHeight() < EditorTileButton.SIZE && metric.stringWidth(name) < EditorTileButton.SIZE) {
size++;
f = new Font(bt.getName(), Font.PLAIN, size);
} else {
size--;
f = new Font(bt.getName(), Font.PLAIN, size);
tooBig = true;
}
bt.setFont(f);
}
}
public void createTileButtons(JPanel display, boolean pFilter) {
int size = EditorTileButton.SIZE;
JButton[] standard = createStandardButtons(display);
//Erstellen der TileButtons:
tiles = new EditorTileButton[tabTileSet.tileSet.length];
for (int i = 0; i < tabTileSet.tileSet.length; i++) {
tiles[i] = new EditorTileButton(i, tabTileSet);
tiles[i].setSize(size, size);
tiles[i].addMouseListener(this);
}
//Hinzufügen der Buttons:
JTextField txtEingabe = new JTextField();
txtEingabe.setBorder(null);
txtEingabe.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
}
@Override
public void removeUpdate(DocumentEvent e) {
}
@Override
public void changedUpdate(DocumentEvent e) {
try {
int idInTextArea = Integer.parseInt(txtEingabe.getText());
if (idInTextArea <= tabTileSet.tileSet.length) {
selectedID = Integer.parseInt(txtEingabe.getText());
selectedinLabel(selectedLabel, new ImageIcon(tabTileSet.tileSet[idInTextArea].tileImage));
} else {
txtEingabe.setText("");
JOptionPane.showMessageDialog(owner, "Es können nur Tiles von 0 bis " + tabTileSet.tileSet.length + " verwendet werden", "", JOptionPane.WARNING_MESSAGE);
}
} catch (NumberFormatException en) {
if (!txtEingabe.getText().equals("")) {
txtEingabe.setText("");
JOptionPane.showMessageDialog(owner, "Ungültige Zahl.", "", JOptionPane.WARNING_MESSAGE);
}
}
}
});
//Standard:
for (int i = 0; i < standard.length; i++) {
display.add(standard[i]);
}
display.add(txtEingabe);
EditorTileButton emptyButton = new EditorTileButton(9999, new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB_PRE));
emptyButton.addMouseListener(this);
display.add(emptyButton);
//Tiles:
showFilter(display, pFilter);
}
public void recalculateGrid(int parentWidth) {
columns = parentWidth / (EditorTileButton.SIZE + gap);
try {
tilePanel.setLayout(new GridLayout(0, columns, gap, gap));
} catch (Exception e) {
tilePanel.setLayout(new GridLayout(0, 5, gap, gap));
}
tilePanel.revalidate();
tilePanel.repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
EditorTileButton source = ((EditorTileButton) e.getSource());
selectedID = source.getId();
//Schnellauswahl
if (e.getClickCount() == 2) {
belongingEditor.maps.get(belongingEditor.selectedMap).setGraphicID(selectedID);
belongingEditor.addRecently(selectedID, tileTabs.get(selectedTileSet).getTabTileSet());
}
if (e.getClickCount() == 3) {
JOptionPane.showMessageDialog(null, "Das gewählte Tile hat die ID " + source.getId(), "Tile ID", JOptionPane.INFORMATION_MESSAGE);
}
selectedinLabel(selectedLabel, source.getIcon());
}
@Override
public void mousePressed(MouseEvent e) {
EditorTileButton source = ((EditorTileButton) e.getSource());
selectedID = source.getId();
for (int i = 0; i < tileTabs.size(); i++) {
if (source.getTileSetPath() == null) {
selectedID = 9999;
belongingEditor.maps.get(belongingEditor.selectedMap).setGraphicID(selectedID);
belongingEditor.addRecently(selectedID, tileTabs.get(selectedTileSet).getTabTileSet());
} else {
selectedID = findAndSetTileSetID(new TileSet(source.getTileSetPath()));
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
}