-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphicsBank.java
376 lines (319 loc) · 9.65 KB
/
GraphicsBank.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
import javax.swing.*;
import java.awt.Graphics.*;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.net.*;
/**
* Stores a set of tiles which can be requested by id number, or name
**/
public class GraphicsBank {
final static int DEFAULT_TILE_WIDTH = 32;
final static int DEFAULT_TILE_HEIGHT = 32;
final static String GB_VERSION = "1.0";
final static String DELIM = ",";
final static char COMMENT = '#';
final static int ID = 0;
final static int PATH = 1;
final static int NAME = 2;
final static int TYPE = 3;
final static int EXTRA = 4;
private ArrayList tiles;
private ArrayList sprites;
private ArrayList changeListeners;
File loadedFrom;
File baseDirectory;
private boolean isUnsaved;
Dimension baseTileSize;
public GraphicsBank() {
tiles = new ArrayList();
sprites = new ArrayList();
changeListeners = new ArrayList();
loadedFrom = null;
baseTileSize = new Dimension(DEFAULT_TILE_WIDTH, DEFAULT_TILE_HEIGHT);
isUnsaved = true;
}
public void addSprite(String anmFile) {
addSprite(new File(anmFile));
}
public void addSprite(File anmFile) {
}
public void loadTileset(String from) throws FileNotFoundException, IOException {
loadTileset(new File(from));
}
/**
* Load tileset from file.
* This will combine the tilesets if one has already been loaded.
* The most recently loaded tileset file becomes the "loadedFrom"
* tileset.
**/
public void loadTileset(File from) throws FileNotFoundException, IOException {
BufferedReader r;
String line;
String tokens[];
int id;
int lineCount;
File tileFile;
if(tiles.size() > 0) {
isUnsaved = true;
} else {
isUnsaved = false;
}
System.out.println(from);
baseDirectory = from.getParentFile();
this.loadedFrom = from;
lineCount = 0;
r = new BufferedReader(new FileReader(from));
while(true) {
line = r.readLine();
lineCount++;
//stop when no more lines
if(line == null) break;
line = line.trim();
if(line.length() == 0 || line.charAt(0) == COMMENT) {
continue; //skip comments and empty lines.
}
tokens = line.split(DELIM);
if(tokens.length < 4) {
System.err.println("Could not parse line "+lineCount+". :");
System.err.println(line);
System.err.println("(There are not enough tokens)");
continue;
}
try {
id = Integer.parseInt(tokens[ID].trim());
} catch(Exception e) {
System.err.println("Could not parse line "+lineCount+". :");
System.err.println(line);
System.err.println("(The tile id is not a valid number)");
continue;
}
//get file for image
tokens[PATH] = tokens[PATH].trim();
tokens[NAME] = tokens[NAME].trim();
tokens[TYPE] = tokens[TYPE].trim();
tileFile = new File(baseDirectory, tokens[PATH]);
//System.out.println("load tile image: "+tileFile);
if(!tileFile.exists()) {
tileFile = new File(tokens[PATH]);
if(tileFile.exists()) {
System.err.println("WARNING: file "+tokens[PATH]+" not within the tilemaps working directory");
} else {
r.close();
throw new FileNotFoundException("File "+tokens[PATH]+" referenced on line "+lineCount +" of "+from+" could not be found");
}
}
//System.out.println("New tile: "+id+", name = "+tokens[NAME]);
Tile t = null;
if(tokens.length > EXTRA) {
t = new Tile(id, tileFile.toString(), tokens[NAME].trim(), tokens[TYPE].trim(), tokens[EXTRA].trim());
} else {
t = new Tile(id, tileFile.toString(), tokens[NAME], tokens[TYPE]);
}
tiles.add(t);
}
} //end loadTileset
/**
* Save the tileset to the specified file.
* Will overwrite file if it exists.
**/
void saveTileset(File to) throws IOException{
File base = to.getParentFile();
PrintWriter w = new PrintWriter(new FileWriter(to));
w.println("# Generated by version "+GB_VERSION+" of GraphicsBank");
w.println("Tile Number, Image file, Tile Name, Type, Extended Info");
System.out.println("Saving "+tiles.size()+" tiles.");
Iterator i = tiles.iterator();
while(i.hasNext()) {
Tile t = (Tile)i.next();
File tf = new File(t.getPath()).getCanonicalFile();
String relPath = RelativePath.getRelativePath(new File(base.getCanonicalPath()), new File(tf.getCanonicalPath()));
w.print(""+t.getNumber() + ", "+relPath + ", " + t.getName() + ", "+t.getType());
if(t.getInfo() != null) {
w.println(", "+t.getInfo());
} else {
w.println();
}
}
w.close();
baseDirectory = base;
loadedFrom = to;
isUnsaved = false;
}
/**
* Get the tileset file this tileset was loaded from.
* May return NULL if this is a new tileset that has never been saved.
**/
File getFile() {
return loadedFrom;
}
/**
* Get a tile by number
**/
Tile getTile(int number) {
Iterator i = tiles.iterator();
while(i.hasNext()) {
Tile t = (Tile)i.next();
if(t.number == number) {
return t;
}
}
return null;
}
/**
* Get a tile by name. Case sensitive
**/
Tile getTile(String name) {
Iterator i = tiles.iterator();
while(i.hasNext()) {
Tile t = (Tile)i.next();
if(t.getName().equals(name)) {
return t;
}
}
return null;
}
/**
* Rwemove a tile from the graphics bank. Any registered change listeners
* will be notified.
**/
Tile remove(Tile t) {
Tile rm = null;
if(tiles.remove(t)) {
rm = t;
}
if(t != null) {
fireRemoveEvent(t);
isUnsaved = true;
}
return t;
}
/**
* Add a tile to the graphics bank. Any registered change listeners
* will be notified.
**/
void add(Tile t) {
/* TODO: validate the tile */
tiles.add(t);
isUnsaved = true;
fireAddEvent(t);
}
int size() {
return tiles.size();
}
Dimension getBaseTileSize() {
return baseTileSize;
}
/**
* Iterator for tile objects
**/
Iterator iterator() {
return tiles.iterator();
}
/**
* Set a global colour adjustment effect for all tiles in the
* graphics bank. This will override any individually set
* effects on all tiles.
**/
public void setEffect(float r, float g, float b, float h, float s, float z)
{
Iterator i = tiles.iterator();
while(i.hasNext()) {
((Tile)(i.next())).adjustRGBHS(r, g, b, h, s, z);
}
}
/**
* True if there have been any changes to the tileset since it
* was last saved or loaded.
**/
public boolean isUnsaved() {
if(getFile() == null) {
return true;
}
return isUnsaved;
}
/**
* Get the directory that the tileset file describing this
* tileset was found in.
**/
File getBaseDirectory() {
return baseDirectory;
}
/**
* Get an unused tile number. Returns one more than the
* highest number used.
**/
int getUnusedNumber() {
int n = 1;
Iterator i = tiles.iterator();
while(i.hasNext()) {
Tile t = (Tile)i.next();
if(n <= t.getNumber()) {
n = t.getNumber() + 1;
}
}
return n;
}
/**
* Add a GraphicsBankChangeListener to the graphics bank.
* This listener will be notified whenever the tileset
* is changed.
**/
void addChangeListener(GraphicsBankChangeListener l) {
changeListeners.add(l);
}
/**
* Remove a GraphicsBankChangeListener from the graphics bank.
**/
void removeChangeListener(GraphicsBankChangeListener l) {
changeListeners.remove(l);
}
/**
* May be called directly, and will cause
* the tilesetUpdated() method of every registered
* GraphicsBankChangeListener object to be called.
**/
public void fireChangeEvent() {
GraphicsBankChangeListener l;
Iterator i = changeListeners.iterator();
while(i.hasNext()) {
l = (GraphicsBankChangeListener)i.next();
l.tilesetUpdated(this);
}
}
/**
* Called whenever a tile is added using add(). will cause
* the tileAdded() method of every registered
* GraphicsBankChangeListener object to be called.
**/
private void fireAddEvent(Tile t) {
System.out.println("Fire add event");
GraphicsBankChangeListener l;
Iterator i = changeListeners.iterator();
while(i.hasNext()) {
l = (GraphicsBankChangeListener)i.next();
l.tileAdded(this, t);
}
}
/**
* Called whenever a tile is removed using remove().
* will cause the tileRemoved() method of every registered
* GraphicsBankChangeListener object to be called.
**/
private void fireRemoveEvent(Tile t) {
GraphicsBankChangeListener l;
Iterator i = changeListeners.iterator();
while(i.hasNext()) {
l = (GraphicsBankChangeListener)i.next();
l.tileRemoved(this, t);
}
}
}
interface GraphicsBankChangeListener {
/* Large change happened such as loading a tileset */
public void tilesetUpdated(GraphicsBank bank);
/* A single tile was removed */
public void tileRemoved(GraphicsBank bank, Tile removed);
/* A single tile was added */
public void tileAdded(GraphicsBank bank, Tile added);
}