MakeTiles: use gui's parser for xml image specs
This commit is contained in:
parent
455fc004de
commit
744e74267a
2 changed files with 33 additions and 68 deletions
|
@ -523,76 +523,12 @@ public class MakeTiles
|
|||
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);
|
||||
in.nextTag(); // get document tag
|
||||
return readTileImage(in, loaderContext);
|
||||
}
|
||||
|
||||
catch (XMLStreamException e) {
|
||||
throw new IOException("XML Parse error", e);
|
||||
}
|
||||
}
|
||||
|
||||
static TileImage parseImageXml(XMLStreamReader in)
|
||||
throws IOException, XMLStreamException
|
||||
{
|
||||
String src = in.getAttributeValue(null, "src");
|
||||
TileImage img = loadAnimation(src);
|
||||
|
||||
String tmp = in.getAttributeValue(null, "at");
|
||||
if (tmp != null) {
|
||||
String [] coords = tmp.split(",");
|
||||
if (coords.length == 2) {
|
||||
TileImageSprite sprite = new TileImageSprite(img);
|
||||
sprite.offsetX = Integer.parseInt(coords[0]);
|
||||
sprite.offsetY = Integer.parseInt(coords[1]);
|
||||
img = sprite;
|
||||
}
|
||||
else {
|
||||
throw new IOException("Invalid 'at' syntax");
|
||||
}
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
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")) {
|
||||
|
||||
TileImageLayer rv = new TileImageLayer(
|
||||
result, //below
|
||||
parseImageXml(in) //above
|
||||
);
|
||||
result = rv;
|
||||
|
||||
skipToEndElement(in);
|
||||
}
|
||||
else {
|
||||
// unrecognized element
|
||||
skipToEndElement(in);
|
||||
}
|
||||
}
|
||||
|
||||
if (result != null && result.below == null) {
|
||||
return result.above;
|
||||
}
|
||||
else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -120,7 +120,8 @@ public abstract class TileImage
|
|||
|
||||
@Override
|
||||
public void drawFragment(Graphics2D gr, int destX, int destY, int srcX, int srcY) {
|
||||
throw new UnsupportedOperationException();
|
||||
srcImage.drawFragment(gr, destX, destY,
|
||||
srcX+offsetX, srcY+offsetY);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -200,6 +201,30 @@ public abstract class TileImage
|
|||
return anim;
|
||||
}
|
||||
|
||||
static TileImage readLayeredImage(XMLStreamReader in, LoaderContext ctx)
|
||||
throws XMLStreamException
|
||||
{
|
||||
TileImageLayer result = null;
|
||||
|
||||
while (in.nextTag() != XMLStreamConstants.END_ELEMENT) {
|
||||
assert in.isStartElement();
|
||||
|
||||
TileImage newImg = readTileImage(in, ctx);
|
||||
TileImageLayer rv = new TileImageLayer(
|
||||
result, //below
|
||||
newImg //above
|
||||
);
|
||||
result = rv;
|
||||
}
|
||||
|
||||
if (result != null && result.below == null) {
|
||||
return result.above;
|
||||
}
|
||||
else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public static TileImage readTileImage(XMLStreamReader in, LoaderContext ctx)
|
||||
throws XMLStreamException
|
||||
{
|
||||
|
@ -212,6 +237,9 @@ public abstract class TileImage
|
|||
else if (tagName.equals("animation")) {
|
||||
return readAnimation(in, ctx);
|
||||
}
|
||||
else if (tagName.equals("layered-image")) {
|
||||
return readLayeredImage(in, ctx);
|
||||
}
|
||||
else {
|
||||
throw new XMLStreamException("unrecognized tag: "+tagName);
|
||||
}
|
||||
|
@ -230,7 +258,8 @@ public abstract class TileImage
|
|||
assert in.isStartElement();
|
||||
String tagName = in.getLocalName();
|
||||
if (tagName.equals("image") ||
|
||||
tagName.equals("animation"))
|
||||
tagName.equals("animation") ||
|
||||
tagName.equals("layered-image"))
|
||||
{
|
||||
img = readTileImage(in, ctx);
|
||||
}
|
||||
|
|
Reference in a new issue