Added character manager and character descriptions. Allow tile sets
that have no specified layer. Made the scene editor gracefully handle the case where there are no valid tile sets for use with the selected scene layer. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@461 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: TileSetParser.java,v 1.6 2001/10/12 16:36:58 shaper Exp $
|
||||
// $Id: TileSetParser.java,v 1.7 2001/10/15 23:53:43 shaper Exp $
|
||||
|
||||
package com.threerings.media.tile;
|
||||
|
||||
@@ -14,9 +14,8 @@ public interface TileSetParser
|
||||
{
|
||||
/**
|
||||
* Read tileset description data from the specified file and
|
||||
* construct {@link TileSet} objects to suit. Return a list of
|
||||
* all tile set objects constructed, or a zero-length list if no
|
||||
* tileset descriptions were fully parsed.
|
||||
* append {@link TileSet} objects constructed from the data to the
|
||||
* given list.
|
||||
*/
|
||||
public List loadTileSets (String fname) throws IOException;
|
||||
public void loadTileSets (String fname, List tilesets) throws IOException;
|
||||
}
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
//
|
||||
// $Id: XMLTileSetParser.java,v 1.17 2001/10/12 16:36:58 shaper Exp $
|
||||
// $Id: XMLTileSetParser.java,v 1.18 2001/10/15 23:53:43 shaper Exp $
|
||||
|
||||
package com.threerings.media.tile;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import org.xml.sax.*;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
|
||||
import com.samskivert.util.*;
|
||||
import com.samskivert.xml.XMLUtil;
|
||||
import com.samskivert.xml.SimpleParser;
|
||||
|
||||
import com.threerings.media.Log;
|
||||
|
||||
/**
|
||||
@@ -22,79 +20,36 @@ import com.threerings.media.Log;
|
||||
* on the input XML stream, though the parsing code assumes the XML
|
||||
* document is well-formed.
|
||||
*/
|
||||
public class XMLTileSetParser extends DefaultHandler
|
||||
public class XMLTileSetParser extends SimpleParser
|
||||
implements TileSetParser
|
||||
{
|
||||
/**
|
||||
* This method is called when an element is fully parsed. The
|
||||
* complete parsed data for the element is passed in the given
|
||||
* string.
|
||||
*
|
||||
* @param qName the element name.
|
||||
* @param str the full parsed data for the element.
|
||||
*/
|
||||
protected void finishElement (String qName, String str)
|
||||
{
|
||||
if (qName.equals("imagefile")) {
|
||||
_tset.imgFile = str;
|
||||
|
||||
} else if (qName.equals("rowwidth")) {
|
||||
_tset.rowWidth = StringUtil.parseIntArray(str);
|
||||
|
||||
} else if (qName.equals("rowheight")) {
|
||||
_tset.rowHeight = StringUtil.parseIntArray(str);
|
||||
|
||||
} else if (qName.equals("tilecount")) {
|
||||
_tset.tileCount = StringUtil.parseIntArray(str);
|
||||
|
||||
// calculate the total number of tiles in the tileset
|
||||
for (int ii = 0; ii < _tset.tileCount.length; ii++) {
|
||||
_tset.numTiles += _tset.tileCount[ii];
|
||||
}
|
||||
|
||||
} else if (qName.equals("offsetpos")) {
|
||||
getPoint(str, _tset.offsetPos);
|
||||
|
||||
} else if (qName.equals("gapdist")) {
|
||||
getPoint(str, _tset.gapDist);
|
||||
|
||||
} else if (qName.equals("tileset")) {
|
||||
// construct the tileset on tag close and add it to the
|
||||
// list of tilesets constructed thus far
|
||||
_tilesets.add(_tset);
|
||||
|
||||
// prepare to read another tileset object
|
||||
init();
|
||||
}
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void startElement (String uri, String localName,
|
||||
String qName, Attributes attributes)
|
||||
public void startElement (
|
||||
String uri, String localName, String qName, Attributes attributes)
|
||||
{
|
||||
_tag = qName;
|
||||
|
||||
if (_tag.equals("tileset")) {
|
||||
if (qName.equals("tileset")) {
|
||||
// construct the new tile set
|
||||
_tset = createTileSet();
|
||||
|
||||
// note whether it contains object tiles
|
||||
String str = attributes.getValue("layer");
|
||||
_tset.isObjectSet = str.toLowerCase().equals(LAYER_OBJECT);
|
||||
_tset.isObjectSet =
|
||||
(str != null && str.toLowerCase().equals(LAYER_OBJECT));
|
||||
|
||||
// get the tile set id
|
||||
_tset.tsid = getInt(attributes.getValue("tsid"));
|
||||
_tset.tsid = parseInt(attributes.getValue("tsid"));
|
||||
|
||||
// get the tile set name
|
||||
str = attributes.getValue("name");
|
||||
_tset.name = (str == null) ? DEF_NAME : str;
|
||||
|
||||
} else if (_tag.equals("object")) {
|
||||
} else if (qName.equals("object")) {
|
||||
// TODO: should we bother checking to make sure we only
|
||||
// see <object> tags while within an <objects> tag?
|
||||
int tid = getInt(attributes.getValue("tid"));
|
||||
int wid = getInt(attributes.getValue("width"));
|
||||
int hei = getInt(attributes.getValue("height"));
|
||||
int tid = parseInt(attributes.getValue("tid"));
|
||||
int wid = parseInt(attributes.getValue("width"));
|
||||
int hei = parseInt(attributes.getValue("height"));
|
||||
|
||||
// add the object info to the tileset object hashtable
|
||||
_tset.addObjectInfo(tid, new int[] { wid, hei });
|
||||
@@ -102,69 +57,56 @@ public class XMLTileSetParser extends DefaultHandler
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void endElement (String uri, String localName, String qName)
|
||||
protected void finishElement (
|
||||
String uri, String localName, String qName, String data)
|
||||
{
|
||||
// we know we've received the entirety of the character data
|
||||
// for the elements we're tracking at this point, so proceed
|
||||
// with saving off element values for use when we construct
|
||||
// the tileset object.
|
||||
finishElement(qName, _chars.toString().trim());
|
||||
if (qName.equals("imagefile")) {
|
||||
_tset.imgFile = data;
|
||||
|
||||
// note that we're not within a tag to avoid considering any
|
||||
// characters during this quiescent time
|
||||
_tag = null;
|
||||
} else if (qName.equals("rowwidth")) {
|
||||
_tset.rowWidth = StringUtil.parseIntArray(data);
|
||||
|
||||
// and clear out the character data we're gathering
|
||||
_chars = new StringBuffer();
|
||||
}
|
||||
} else if (qName.equals("rowheight")) {
|
||||
_tset.rowHeight = StringUtil.parseIntArray(data);
|
||||
|
||||
// documentation inherited
|
||||
public void characters (char ch[], int start, int length)
|
||||
{
|
||||
// bail if we're not within a meaningful tag
|
||||
if (_tag == null) {
|
||||
return;
|
||||
}
|
||||
} else if (qName.equals("tilecount")) {
|
||||
_tset.tileCount = StringUtil.parseIntArray(data);
|
||||
|
||||
_chars.append(ch, start, length);
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public List loadTileSets (String fname) throws IOException
|
||||
{
|
||||
try {
|
||||
InputStream tis = ConfigUtil.getStream(fname);
|
||||
if (tis == null) {
|
||||
Log.warning("Couldn't find file [fname=" + fname + "].");
|
||||
return null;
|
||||
// calculate the total number of tiles in the tileset
|
||||
for (int ii = 0; ii < _tset.tileCount.length; ii++) {
|
||||
_tset.numTiles += _tset.tileCount[ii];
|
||||
}
|
||||
|
||||
_tilesets = new ArrayList();
|
||||
} else if (qName.equals("offsetpos")) {
|
||||
getPoint(data, _tset.offsetPos);
|
||||
|
||||
// prepare to read a new tileset
|
||||
init();
|
||||
} else if (qName.equals("gapdist")) {
|
||||
getPoint(data, _tset.gapDist);
|
||||
|
||||
// read all tileset descriptions from the XML input stream
|
||||
XMLUtil.parse(this, tis);
|
||||
|
||||
return _tilesets;
|
||||
|
||||
} catch (ParserConfigurationException pce) {
|
||||
throw new IOException(pce.toString());
|
||||
|
||||
} catch (SAXException saxe) {
|
||||
throw new IOException(saxe.toString());
|
||||
} else if (qName.equals("tileset")) {
|
||||
// add the fully-read tileset to the list of tilesets
|
||||
_tilesets.add(_tset);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize internal member data used to gather tileset
|
||||
* information during parsing.
|
||||
*/
|
||||
protected void init ()
|
||||
// documentation inherited
|
||||
public void loadTileSets (String fname, List tilesets) throws IOException
|
||||
{
|
||||
_chars = new StringBuffer();
|
||||
_tset = null;
|
||||
// save off tileset list for reference while parsing
|
||||
_tilesets = tilesets;
|
||||
|
||||
try {
|
||||
parseFile(fname);
|
||||
} catch (IOException ioe) {
|
||||
Log.warning("Exception parsing tile set descriptions " +
|
||||
"[ioe=" + ioe + "].");
|
||||
}
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
protected InputStream getInputStream (String fname) throws IOException
|
||||
{
|
||||
return ConfigUtil.getStream(fname);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -191,34 +133,14 @@ public class XMLTileSetParser extends DefaultHandler
|
||||
point.setLocation(vals[0], vals[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the integer represented by the given string or -1 if an
|
||||
* error occurred.
|
||||
*/
|
||||
protected int getInt (String str)
|
||||
{
|
||||
try {
|
||||
return (str == null) ? -1 : Integer.parseInt(str);
|
||||
} catch (NumberFormatException nfe) {
|
||||
Log.warning("Malformed integer value [str=" + str + "].");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/** Default tileset name. */
|
||||
protected static final String DEF_NAME = "Untitled";
|
||||
|
||||
/** String constant denoting an object tile set. */
|
||||
protected static final String LAYER_OBJECT = "object";
|
||||
|
||||
/** The XML element tag currently being processed. */
|
||||
protected String _tag;
|
||||
|
||||
/** The tilesets constructed thus far. */
|
||||
protected ArrayList _tilesets = new ArrayList();
|
||||
|
||||
/** Temporary storage of character data while parsing. */
|
||||
protected StringBuffer _chars;
|
||||
protected List _tilesets;
|
||||
|
||||
/** The tile set populated while parsing. */
|
||||
protected TileSetImpl _tset;
|
||||
|
||||
Reference in New Issue
Block a user