String filename = tilesetBaseDir + source;
//if (checkRoot(source)) {
// filename = makeUrl(source);
//}
TileSet ext = null;
try {
InputStream in = new URL(makeUrl(filename)).openStream();
ext = unmarshalTilesetFile(in, filename);
setFirstGidForTileset(ext, firstGid);
} catch (FileNotFoundException fnf) {
error = "Could not find external tileset file " + filename;
}
if (ext == null) {
error = "Tileset " + source + " was not loaded correctly!";
}
return ext;
}
else {
final int tileWidth = getAttribute(t, "tilewidth", map != null ? map.getTileWidth() : 0);
final int tileHeight = getAttribute(t, "tileheight", map != null ? map.getTileHeight() : 0);
final int tileSpacing = getAttribute(t, "spacing", 0);
final int tileMargin = getAttribute(t, "margin", 0);
final String name = getAttributeValue(t, "name");
TileSet set;
if (settings.reuseCachedTilesets) {
set = cachedTilesets.get(name);
if (set != null) {
setFirstGidForTileset(set, firstGid);
return set;
}
set = new TileSet();
cachedTilesets.put(name, set);
} else {
set = new TileSet();
}
set.setName(name);
set.setBaseDir(basedir);
setFirstGidForTileset(set, firstGid);
boolean hasTilesetImage = false;
NodeList children = t.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeName().equalsIgnoreCase("image")) {
if (hasTilesetImage) {
System.out.println("Ignoring illegal image element after tileset image.");
continue;
}
String imgSource = getAttributeValue(child, "source");
String transStr = getAttributeValue(child, "trans");
if (imgSource != null) {
// Not a shared image, but an entire set in one image
// file. There should be only one image element in this
// case.
hasTilesetImage = true;
// FIXME: importTileBitmap does not fully support URLs
String sourcePath = imgSource;
if (! new File(imgSource).isAbsolute()) {
sourcePath = tilesetBaseDir + imgSource;
}
if (transStr != null) {
if (transStr.startsWith("#"))
transStr = transStr.substring(1);
int colorInt = Integer.parseInt(transStr, 16);
Color color = new Color(colorInt);
set.setTransparentColor(color);
}
set.importTileBitmap(sourcePath, new BasicTileCutter(
tileWidth, tileHeight, tileSpacing, tileMargin));
}
}
else if (child.getNodeName().equalsIgnoreCase("tile")) {
Tile tile = unmarshalTile(set, child, tilesetBaseDir);
if (!hasTilesetImage || tile.getId() > set.getMaxTileId()) {
set.addTile(tile);
} else {
Tile myTile = set.getTile(tile.getId());
myTile.setProperties(tile.getProperties());
//TODO: there is the possibility here of overlaying images,
// which some people may want
}
}