MakeTiles: recognize .xml image specifications

with .xml image specifications, we can support layers, animations,
and (eventually) complex/conditional images.
This commit is contained in:
Jason Long 2015-01-03 20:56:54 -05:00
parent 206c12a0c4
commit f17989412e
2 changed files with 69 additions and 1 deletions

View file

@ -0,0 +1,5 @@
<?xml version="1.0"?>
<layered-image>
<image src="turf" />
<image src="trees"/>
</layered-image>

View file

@ -12,6 +12,7 @@ import javax.xml.stream.*;
import static micropolisj.engine.TileSpec.generateTileNames;
import static micropolisj.build_tool.TileImage.*;
import static micropolisj.XML_Helper.*;
public class MakeTiles
{
@ -352,9 +353,14 @@ public class MakeTiles
}
}
static SourceImage loadImage(String fileName)
static TileImage loadImage(String fileName)
throws IOException
{
File xmlFile = new File(fileName + ".xml");
if (xmlFile.exists()) {
return loadImageXml(xmlFile);
}
if (!loadedImages.containsKey(fileName)) {
loadedImages.put(fileName,
loadImageReal(fileName));
@ -408,4 +414,61 @@ public class MakeTiles
throw new IOException("File not found: "+fileName+".{svg,png}");
}
static TileImage loadImageXml(File xmlFile)
throws IOException
{
FileInputStream inStream = new FileInputStream(xmlFile);
try {
XMLStreamReader in = XMLInputFactory.newInstance().createXMLStreamReader(inStream, "UTF-8");
in.nextTag();
if (in.getEventType() != XMLStreamConstants.START_ELEMENT) {
throw new IOException("Unrecognized file format");
}
if (!in.getLocalName().equals("layered-image")) {
throw new IOException("Unrecognized file format");
}
return parseLayeredImageXml(in);
}
catch (XMLStreamException e) {
throw new IOException("XML Parse error", e);
}
}
static TileImage parseLayeredImageXml(XMLStreamReader in)
throws IOException, XMLStreamException
{
TileImageLayer result = null;
while (in.nextTag() != XMLStreamConstants.END_ELEMENT) {
assert in.isStartElement();
String tagName = in.getLocalName();
if (tagName.equals("image")) {
String src = in.getAttributeValue(null, "src");
TileImageLayer rv = new TileImageLayer();
rv.below = result;
rv.above = loadAnimation(src);
result = rv;
skipToEndElement(in);
}
else {
// unrecognized element
skipToEndElement(in);
}
}
if (result != null && result.below == null) {
return result.above;
}
else {
return result;
}
}
}