-
Notifications
You must be signed in to change notification settings - Fork 0
/
TileChooser.java
476 lines (379 loc) · 11.6 KB
/
TileChooser.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
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.imageio.ImageIO;
public class TileChooser extends JPanel implements ActionListener, GraphicsBankChangeListener
{
//DropTarget dropTarget;
ArrayList tiles;
GridLayout layout;
GraphicsBank gfx;
int tileWidth = 32;
Tile selectedTile;
ButtonGroup group;
JPanel tilePanel;
JPanel spacer;
/* For tile properties dialog */
JDialog propertiesDialog;
Tile propertyTile;
JTextField userText;
JSpinner tileNumber;
JTextField tileName;
JTextField tileType;
JLabel tileImg;
JButton applyBtn;
JButton cancelBtn;
JButton deleteBtn;
JTextField imageFile;
FileDropHandler fileDrop;
public TileChooser(GraphicsBank gfx)
{
tilePanel = new JPanel();
layout = new GridLayout(0,5);
tilePanel.setLayout(layout);
this.setLayout(new BorderLayout());
this.add(tilePanel, BorderLayout.NORTH);
/* Put in spacer to enlarge the panel as a drop target */
spacer = new JPanel();
spacer.add(new JLabel(" Drop new tiles here"));
spacer.setToolTipText("Drop image files here to create more tiles.");
//spacer.setPreferredSize(gfx.getBaseTileSize());
this.add(spacer, BorderLayout.CENTER);
this.gfx = gfx;
reset();
fileDrop = new FileDropHandler();
setTransferHandler(fileDrop);
gfx.addChangeListener(this);
propertiesDialog = null;
}
public TileChooser(GraphicsBank gfx, JFrame dialogOwner) {
this(gfx);
createPropertiesDialog(dialogOwner);
}
void createPropertiesDialog(JFrame dialogOwner) {
/* Setup for the proeprties dialog */
propertiesDialog = new JDialog(dialogOwner, "Tile Properties");
propertiesDialog.setSize(300, 300); //to set location better
propertiesDialog.setLocationRelativeTo(null);
tileName = new JTextField("", 20);
tileType = new JTextField("", 20);
imageFile = new JTextField("", 20);
imageFile.setEditable(false);
tileNumber = new JSpinner();
userText = new JTextField("", 20);
propertyTile = null;
tileImg = new JLabel();
tileImg.setHorizontalAlignment(SwingConstants.CENTER);
tileImg.setBorder(new TitledBorder("Image"));
JPanel cp = (JPanel)propertiesDialog.getContentPane();
cp.setLayout(new BorderLayout());
JPanel p2 = new JPanel(new BorderLayout());
cp.add(p2, BorderLayout.CENTER);
cp.add(tileImg, BorderLayout.NORTH);
JPanel btns1 = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(3, 3, 3, 3);
c.gridx = 0;
c.gridy = 0;
btns1.add(new JLabel("ID"), c);
c.gridx = 1;
c.ipadx = 30;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.WEST;
btns1.add(tileNumber, c);
c.ipadx = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
btns1.add(new JLabel("Type"), c);
c.gridx = 1;
btns1.add(tileType, c);
c.gridx = 0;
c.gridy = 2;
btns1.add(new JLabel("Name"), c);
c.gridx = 1;
btns1.add(tileName, c);
c.gridx = 0;
c.gridy = 3;
btns1.add(new JLabel("User Text"), c);
c.gridx = 1;
btns1.add(userText, c);
p2.add(btns1, BorderLayout.NORTH);
/* The buttons */
applyBtn = new JButton("Save");
deleteBtn = new JButton("Delete Tile");
cancelBtn = new JButton("Cancel");
applyBtn.addActionListener(this);
cancelBtn.addActionListener(this);
deleteBtn.addActionListener(this);
JPanel btns2 = new JPanel(new GridLayout(1, 3));
btns2.add(deleteBtn);
btns2.add(applyBtn);
btns2.add(cancelBtn);
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 2;
btns1.add(btns2, c);
propertiesDialog.setSize(300, 500);
propertiesDialog.setResizable(false);
}
/**
* Rebuild the entire panel from the graphics bank
**/
public void reset() {
int count = 0;
tilePanel.removeAll();
group = new ButtonGroup();
TileButton b = new TileButton(null);
tilePanel.add(b);
group.add(b);
count ++;
Iterator i = gfx.iterator();
while(i.hasNext())
{
b = new TileButton((Tile)i.next());
tilePanel.add(b);
group.add(b);
count ++;
}
if(count <= 0) {
//spacer.setText(" No Tiles");
spacer.setPreferredSize(new Dimension(1, 100));
//spacer.setBorder(new LineBorder(Color.black));
} else {
//spacer.setText(" Drop new tiles");
spacer.setPreferredSize(new Dimension(1, 30));
}
tilePanel.revalidate();
repaint();
}
/**
* Large change to the tileset, so we just remove
* all the buttons and rebuild them
**/
public void tilesetUpdated(GraphicsBank g) {
System.out.println("tilset updated");
if(g == gfx) {
reset();
}
}
/**
* Reset. we don't keep track of the buttons so
* we have to remove everything and rebuild it.
**/
public void tileRemoved(GraphicsBank g, Tile t) {
System.out.println("tilset updated");
if(g == gfx) {
reset();
}
}
/**
* Add a single tile button
**/
public void tileAdded(GraphicsBank g, Tile t) {
System.out.println("tilset updated");
TileButton b = new TileButton(t);
tilePanel.add(b);
group.add(b);
//spacer.setText(" Drop new tiles");
spacer.setPreferredSize(new Dimension(1, 30));
tilePanel.revalidate();
repaint();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == applyBtn && propertyTile != null) {
propertyTile.name = tileName.getText();
propertyTile.type = tileType.getText();
propertyTile.number = ((Integer)tileNumber.getValue()).intValue();
propertyTile.info = userText.getText();
propertiesDialog.dispose();
propertyTile = null;
} else if(e.getSource() == cancelBtn) {
propertiesDialog.dispose();
propertyTile = null;
} else if(e.getSource() == deleteBtn) {
if(propertyTile != null) {
gfx.remove(propertyTile);
propertyTile = null;
}
propertiesDialog.dispose();
} else {
System.err.println("Unknown button fired action. "+e);
}
}
public void setWidth(int width)
{
if(width >= tileWidth+8)
{
layout.setColumns(width/(tileWidth+15));
tilePanel.revalidate();
}
}
public Tile getSelectedTile()
{
return selectedTile;
}
void showProperties(Tile t) {
propertyTile = t;
if(t != null) {
userText.setText(t.getInfo());
tileNumber.setValue(new Integer(t.getNumber()));
tileName.setText(t.getName());
tileType.setText(t.getType());
tileImg.setIcon(new ImageIcon(t.getImage()));
applyBtn.setEnabled(true);
deleteBtn.setEnabled(true);
userText.setEditable(true);
tileNumber.setEnabled(true);
tileName.setEditable(true);
tileType.setEditable(true);
} else {
userText.setText("");
tileNumber.setValue(new Integer(0));
tileName.setText("Null (Erases existing tiles)");
tileType.setText("");
tileImg.setIcon(null);
userText.setEditable(false);
tileNumber.setEnabled(false);
tileName.setEditable(false);
tileType.setEditable(false);
applyBtn.setEnabled(false);
deleteBtn.setEnabled(false);
}
propertiesDialog.pack();
propertiesDialog.setVisible(true);
}
public void importImageAsTile(File f) throws IOException {
importImageAsTile(f, 0);
}
public void importImageAsTile(File f, int level) throws IOException {
if(f.isDirectory()) {
File[] contents = f.listFiles();
for(int num = 0; num < contents.length; num++) {
importImageAsTile(contents[num]);
}
}
System.out.println("Import "+f);
try {
ImageIO.read(f);
} catch(Exception e) {
System.out.println("FAIL");
return;
}
System.out.println("getbasedir.... ahuh!");
//File base = new File(gfx.getBaseDirectory().getCanonicalPath());
System.out.println("?1");
int n = gfx.getUnusedNumber();
System.out.println("?2");
Tile t = new Tile(n, f.getAbsolutePath(), "New Tile "+n, "No Type");
System.out.println("Adding "+f);
gfx.add(t);
if(propertiesDialog != null) {
showProperties(t);
}
}
/********************************************************
* Inner class. The buttons that appear on the chooser. *
********************************************************/
class TileButton extends JToggleButton implements ActionListener, MouseListener
{
Tile tile;
public TileButton(Tile t)
{
super();
Image i2 = new BufferedImage(gfx.getBaseTileSize().width, gfx.getBaseTileSize().height, BufferedImage.TYPE_INT_ARGB);
if(t != null) {
Image i = t.getImage();
i2.getGraphics().drawImage(i, 0, 0, 32, 32, null);
setToolTipText(t.getName());
}
setIcon(new ImageIcon(i2));
setMargin(new Insets(2,2,2,2));
tile = t;
this.addMouseListener(this);
this.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
selectedTile = tile;
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{
if(SwingUtilities.isRightMouseButton(e))
{
showProperties(tile);
}
}
public Tile getTile()
{
return tile;
}
}
/**************************************
* Inner class - the transfer handler *
**************************************/
class FileDropHandler extends TransferHandler {
/**
* Determine whether we can or cannot accept the stuff the user is dragging
* onto our panel. In this case we accept only file lists.
**/
public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
//System.out.println("Got query by component: "+comp);
for(int i = 0; i < transferFlavors.length; i++) {
//System.out.println("Available flavor: "+transferFlavors[i]);
if(transferFlavors[i].equals(DataFlavor.javaFileListFlavor)) {
//System.out.println("I can use this");
return true;
}
}
return false;
}
/**
* Import info about the file.
* Makes the assumption that the "canImport" has worked correctly and
* this data can in fact be served up as a File List.
**/
public boolean importData(JComponent comp, Transferable t) {
//System.out.println("Import data");
try {
java.util.List files = (java.util.List) t.getTransferData(DataFlavor.javaFileListFlavor);
if(files.size() > 4) {
/* Does not work. Forever halts the AWT thread.
if(!(PromptDialog.ask("Really process "+files.size()+" files?", "Yes", "Cancel").equals("Yes"))) {
return false;
} */
}
Iterator itr = files.iterator();
//System.out.println("itr, size = "+files.size());
while(itr.hasNext()) {
File f = (File)itr.next();
//System.out.println("imported "+f);
importImageAsTile(f);
//System.out.println("done "+f);
}
} catch (UnsupportedFlavorException e) {
System.err.println("Unsupported drop content: " + e);
} catch (IOException e) {
System.err.println("Unexpected IO Exception while importing tile: " + e);
e.printStackTrace();
//PromptDialog.tell("Sorry, something broke. Please save your work and restart the program.", "OK");
}
return true;
}
}
/************************
* End of inner classes *
************************/
}