String filename = tilesetBaseDir + source;
// if (Util.checkRoot(source)) {
// filename = makeUrl(source);
// }
TileSet ext = null;
try {
// just a little check for tricky people...
String extention = source.substring(source.lastIndexOf('.') + 1);
if (!"tsx".equals(extention.toLowerCase())) {
Log.ger.warn("tileset files should end in .tsx! (" + source + ")");
}
InputStream in = new URL(makeUrl(filename)).openStream();
ext = unmarshalTilesetFile(in, filename);
} catch (FileNotFoundException fnf) {
Log.ger.error("Could not find external tileset file " + filename);
}
if (ext == null) {
Log.ger.error("tileset " + source + " was not loaded correctly!");
ext = new TileSet();
}
ext.setFirstGid(firstGid);
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);
TileSet set = new TileSet();
set.setName(getAttributeValue(t, "name"));
set.setBaseDir(basedir);
set.setFirstGid(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) {
Log.ger.warn("Ignoring illegal image element after tileset image.");
continue;
}
String imgSource = getAttributeValue(child, "source");
String id = getAttributeValue(child, "id");
String transStr = getAttributeValue(child, "trans");
if (imgSource != null && id == 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;
}
Log.ger.info("Importing " + sourcePath + "...");
if (transStr != null) {
int colorInt = Integer.parseInt(transStr, 16);
Color color = new Color(colorInt);
set.setTransparentColor(color);
}
set.importTileBitmap(sourcePath, new BasicTileCutter(tileWidth, tileHeight, tileSpacing, tileMargin));
} else {
Image image = unmarshalImage(child, tilesetBaseDir);
String idValue = getAttributeValue(child, "id");
int imageId = Integer.parseInt(idValue);
set.addImage(image, imageId);
}
} 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
}