diff --git a/rsrc/config/miso/miso.properties b/rsrc/config/miso/miso.properties index 2746d183b..df4149232 100644 --- a/rsrc/config/miso/miso.properties +++ b/rsrc/config/miso/miso.properties @@ -1,5 +1,5 @@ # -# $Id: miso.properties,v 1.12 2001/09/28 00:43:19 shaper Exp $ +# $Id: miso.properties,v 1.13 2001/10/15 23:53:43 shaper Exp $ # # Initial test config values for miso development. # @@ -16,6 +16,9 @@ sceneroot = rsrc/scenes # the tileset descriptions tilesets = rsrc/config/miso/tilesets.xml +# the character descriptions +characters = rsrc/config/miso/characters.xml + # the list of building types buildings = Inn, Hall, Bank, Agent, Palace diff --git a/src/java/com/threerings/cast/CharacterManager.java b/src/java/com/threerings/cast/CharacterManager.java new file mode 100644 index 000000000..03883fc1a --- /dev/null +++ b/src/java/com/threerings/cast/CharacterManager.java @@ -0,0 +1,97 @@ +// +// $Id: CharacterManager.java,v 1.1 2001/10/15 23:53:43 shaper Exp $ + +package com.threerings.miso.scene; + +import java.io.IOException; + +import com.samskivert.util.Config; +import com.samskivert.util.HashIntMap; + +import com.threerings.media.sprite.MultiFrameImage; +import com.threerings.media.tile.TileManager; + +import com.threerings.miso.Log; +import com.threerings.miso.scene.xml.XMLCharacterParser; +import com.threerings.miso.tile.TileUtil; +import com.threerings.miso.util.MisoUtil; + +/** + * The character manager provides facilities for constructing sprites + * that are used to represent characters in a scene. + */ +public class CharacterManager +{ + /** + * Construct the character manager. + */ + public CharacterManager (Config config, TileManager tilemgr) + { + _tilemgr = tilemgr; + + // load character data descriptions + String file = config.getValue(CHARFILE_KEY, DEFAULT_CHARFILE); + try { + new XMLCharacterParser().loadCharacters(file, _characters); + } catch (IOException ioe) { + Log.warning("Exception loading character descriptions " + + "[ioe=" + ioe + "]."); + } + } + + /** + * Returns a sprite representing the character described by the + * given tile set id. + * + * @param the character tile set id. + */ + public AmbulatorySprite getCharacter (int tsid) + { + CharacterInfo info = (CharacterInfo)_characters.get(tsid); + if (info == null) { + // no such character + Log.warning("Unknown character requested [tsid=" + tsid + "]."); + return null; + } + + MultiFrameImage[] anims = TileUtil.getAmbulatorySpriteFrames( + _tilemgr, tsid, info.frameCount); + + AmbulatorySprite sprite = new AmbulatorySprite(0, 0, anims); + sprite.setFrameRate(info.frameRate); + + return sprite; + } + + /** The config key for the character description file. */ + protected static final String CHARFILE_KEY = + MisoUtil.CONFIG_KEY + ".characters"; + + /** The default character description file. */ + protected static final String DEFAULT_CHARFILE = + "rsrc/config/miso/characters.xml"; + + /** The hashtable of character info objects. */ + protected HashIntMap _characters = new HashIntMap(); + + /** The tile manager. */ + protected TileManager _tilemgr; + + /** + * A class that contains character description information for a + * single character for use when constructing the character's + * sprite. + */ + public static class CharacterInfo + { + public int tsid; + public int frameCount; + public int frameRate; + + public String toString () + { + return "[tsid=" + tsid + ", frameCount=" + frameCount + + ", frameRate=" + frameRate + "]"; + } + } +} diff --git a/src/java/com/threerings/cast/CharacterSprite.java b/src/java/com/threerings/cast/CharacterSprite.java index be7a11cc7..950948c9f 100644 --- a/src/java/com/threerings/cast/CharacterSprite.java +++ b/src/java/com/threerings/cast/CharacterSprite.java @@ -1,5 +1,5 @@ // -// $Id: CharacterSprite.java,v 1.11 2001/10/12 00:41:48 shaper Exp $ +// $Id: CharacterSprite.java,v 1.12 2001/10/15 23:53:43 shaper Exp $ package com.threerings.miso.scene; @@ -9,9 +9,8 @@ import com.threerings.miso.Log; import com.threerings.miso.tile.MisoTile; /** - * An AmbulatorySprite is a sprite that can face in one of - * the various compass directions and that can animate itself walking - * along some chosen path. + * An ambulatory sprite is a sprite that animates itself while walking + * about in a scene. */ public class AmbulatorySprite extends Sprite implements Traverser { @@ -46,6 +45,13 @@ public class AmbulatorySprite extends Sprite implements Traverser setFrames(_anims[_orient]); } + // documentation inherited + public void cancelMove () + { + super.cancelMove(); + halt(); + } + // documentation inherited protected void pathBeginning () { @@ -59,7 +65,15 @@ public class AmbulatorySprite extends Sprite implements Traverser protected void pathCompleted () { super.pathCompleted(); + halt(); + } + /** + * Updates the sprite animation frame to reflect the cessation of + * movement and disables any further animation. + */ + protected void halt () + { // come to a halt looking settled and at peace _frame = _frames.getFrame(_frameIdx = 0); invalidate(); diff --git a/src/java/com/threerings/media/tile/TileSetParser.java b/src/java/com/threerings/media/tile/TileSetParser.java index 741a95f54..1647fda44 100644 --- a/src/java/com/threerings/media/tile/TileSetParser.java +++ b/src/java/com/threerings/media/tile/TileSetParser.java @@ -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; } diff --git a/src/java/com/threerings/media/tile/XMLTileSetParser.java b/src/java/com/threerings/media/tile/XMLTileSetParser.java index 1076deb31..2ac91d245 100644 --- a/src/java/com/threerings/media/tile/XMLTileSetParser.java +++ b/src/java/com/threerings/media/tile/XMLTileSetParser.java @@ -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 tags while within an 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; diff --git a/src/java/com/threerings/miso/client/util/MisoSceneUtil.java b/src/java/com/threerings/miso/client/util/MisoSceneUtil.java index 21d4e6703..6d969fd18 100644 --- a/src/java/com/threerings/miso/client/util/MisoSceneUtil.java +++ b/src/java/com/threerings/miso/client/util/MisoSceneUtil.java @@ -1,5 +1,5 @@ // -// $Id: MisoSceneUtil.java,v 1.3 2001/10/11 00:41:27 shaper Exp $ +// $Id: MisoSceneUtil.java,v 1.4 2001/10/15 23:53:43 shaper Exp $ package com.threerings.miso.scene.util; @@ -101,5 +101,5 @@ public class MisoSceneUtil } /** The default layer index for an unknown named layer. */ - protected static final int DEF_LAYER = MisoScene.LAYER_BASE; + protected static final int DEF_LAYER = -1; } diff --git a/src/java/com/threerings/miso/tile/EditableTileSetManager.java b/src/java/com/threerings/miso/tile/EditableTileSetManager.java index 20905d4af..5c1e30127 100644 --- a/src/java/com/threerings/miso/tile/EditableTileSetManager.java +++ b/src/java/com/threerings/miso/tile/EditableTileSetManager.java @@ -1,10 +1,10 @@ // -// $Id: EditableTileSetManager.java,v 1.12 2001/10/12 16:38:59 shaper Exp $ +// $Id: EditableTileSetManager.java,v 1.13 2001/10/15 23:53:43 shaper Exp $ package com.threerings.miso.tile; import java.io.IOException; -import java.util.List; +import java.util.ArrayList; import com.samskivert.util.Config; @@ -28,12 +28,11 @@ public class EditableTileSetManager extends TileSetManagerImpl // load the tilesets from the XML description file String fname = config.getValue(TILESETS_KEY, (String)null); - List tilesets = null; + ArrayList tilesets = new ArrayList(); try { - tilesets = new XMLMisoTileSetParser().loadTileSets(fname); + new XMLMisoTileSetParser().loadTileSets(fname, tilesets); } catch (IOException ioe) { - Log.warning("Exception loading tileset [fname=" + fname + - ", ioe=" + ioe + "]."); + Log.warning("Exception loading tile sets [ioe=" + ioe + "]."); return; } diff --git a/src/java/com/threerings/miso/tile/TileUtil.java b/src/java/com/threerings/miso/tile/TileUtil.java index 2cae6ea11..3b2bd3a6c 100644 --- a/src/java/com/threerings/miso/tile/TileUtil.java +++ b/src/java/com/threerings/miso/tile/TileUtil.java @@ -1,5 +1,5 @@ // -// $Id: TileUtil.java,v 1.6 2001/10/11 00:41:27 shaper Exp $ +// $Id: TileUtil.java,v 1.7 2001/10/15 23:53:43 shaper Exp $ package com.threerings.miso.tile; @@ -22,7 +22,7 @@ public class TileUtil * frames of animation used to render the ambulatory sprite in * each of the directions it may face. The tileset id referenced * must contain Sprite.NUM_DIRECTIONS rows of tiles, - * with each row containing NUM_DIR_FRAMES tiles. + * with each row containing frameCount tiles. * * @param tilemgr the tile manager to retrieve tiles from. * @param tsid the tileset id containing the sprite tiles. @@ -30,15 +30,17 @@ public class TileUtil * @return the array of multi-frame sprite images. */ public static MultiFrameImage[] - getAmbulatorySpriteFrames (TileManager tilemgr, int tsid) + getAmbulatorySpriteFrames (TileManager tilemgr, int tsid, + int frameCount) { MultiFrameImage[] anims = new MultiFrameImage[Sprite.NUM_DIRECTIONS]; try { for (int ii = 0; ii < Sprite.NUM_DIRECTIONS; ii++) { - Tile[] tiles = new Tile[NUM_DIR_FRAMES]; - for (int jj = 0; jj < NUM_DIR_FRAMES; jj++) { - int idx = (ii * NUM_DIR_FRAMES) + jj; + Tile[] tiles = new Tile[frameCount]; + + for (int jj = 0; jj < frameCount; jj++) { + int idx = (ii * frameCount) + jj; tiles[jj] = tilemgr.getTile(tsid, idx); } @@ -77,7 +79,4 @@ public class TileUtil protected Tile[] _tiles; } - - /** The number of frames of animation for each direction. */ - protected static final int NUM_DIR_FRAMES = 8; } diff --git a/src/java/com/threerings/miso/tile/tools/xml/XMLMisoTileSetParser.java b/src/java/com/threerings/miso/tile/tools/xml/XMLMisoTileSetParser.java index f73a7856f..0fd545f58 100644 --- a/src/java/com/threerings/miso/tile/tools/xml/XMLMisoTileSetParser.java +++ b/src/java/com/threerings/miso/tile/tools/xml/XMLMisoTileSetParser.java @@ -1,5 +1,5 @@ // -// $Id: XMLMisoTileSetParser.java,v 1.3 2001/10/12 00:43:04 shaper Exp $ +// $Id: XMLMisoTileSetParser.java,v 1.4 2001/10/15 23:53:43 shaper Exp $ package com.threerings.miso.tile; @@ -17,8 +17,8 @@ import com.threerings.miso.scene.util.MisoSceneUtil; public class XMLMisoTileSetParser extends XMLTileSetParser { // documentation inherited - public void startElement (String uri, String localName, - String qName, Attributes attributes) + public void startElement ( + String uri, String localName, String qName, Attributes attributes) { super.startElement(uri, localName, qName, attributes); @@ -29,12 +29,13 @@ public class XMLMisoTileSetParser extends XMLTileSetParser } // documentation inherited - protected void finishElement (String qName, String str) + protected void finishElement ( + String uri, String localName, String qName, String data) { - super.finishElement(qName, str); + super.finishElement(uri, localName, qName, data); if (qName.equals("passable")) { - ((MisoTileSetImpl)_tset).passable = StringUtil.parseIntArray(str); + ((MisoTileSetImpl)_tset).passable = StringUtil.parseIntArray(data); } } diff --git a/src/java/com/threerings/miso/tools/xml/XMLSceneGroupParser.java b/src/java/com/threerings/miso/tools/xml/XMLSceneGroupParser.java index 81a49ccf0..738dfc7b3 100644 --- a/src/java/com/threerings/miso/tools/xml/XMLSceneGroupParser.java +++ b/src/java/com/threerings/miso/tools/xml/XMLSceneGroupParser.java @@ -1,45 +1,43 @@ // -// $Id: XMLSceneGroupParser.java,v 1.5 2001/09/28 01:31:32 mdb Exp $ +// $Id: XMLSceneGroupParser.java,v 1.6 2001/10/15 23:53:43 shaper Exp $ package com.threerings.miso.scene.xml; import java.io.*; import java.util.*; -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.miso.Log; import com.threerings.miso.scene.*; import com.threerings.miso.scene.util.MisoSceneUtil; + import com.threerings.whirled.data.Scene; /** * Parses an XML scene group description file, loads the referenced * scenes into the runtime scene repository, and creates bindings - * between the portals in the scenes. - * - *

Does not currently perform validation on the input XML stream, - * though the parsing code assumes the XML document is well-formed. + * between the portals in the scenes. Does not currently perform + * validation on the input XML stream, though the parsing code assumes + * the XML document is well-formed. */ -public class XMLSceneGroupParser extends DefaultHandler +public class XMLSceneGroupParser extends SimpleParser { - public void startElement (String uri, String localName, - String qName, Attributes attributes) + // documentation inherited + public void startElement ( + String uri, String localName, String qName, Attributes attributes) { - _tag = qName; - - if (_tag.equals("scene")) { + if (qName.equals("scene")) { // construct a new scene info object _info.curscene = new SceneInfo(attributes.getValue("src")); // add it to the list of scene info objects _info.sceneinfo.add(_info.curscene); - } else if (_tag.equals("portal")) { + } else if (qName.equals("portal")) { // pull out the portal data String src = attributes.getValue("src"); String destScene = attributes.getValue("destScene"); @@ -53,18 +51,15 @@ public class XMLSceneGroupParser extends DefaultHandler } } - public void endElement (String uri, String localName, String qName) + // documentation inherited + public 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 data for later use. - String str = _chars.toString(); - if (qName.equals("name")) { - _info.curscene.name = str; + _info.curscene.name = data; } else if (qName.equals("entrance")) { - _info.curscene.entrance = str; + _info.curscene.entrance = data; } else if (qName.equals("scene")) { // TODO: load scene from scene repository and set @@ -79,21 +74,6 @@ public class XMLSceneGroupParser extends DefaultHandler // warn about any remaining un-bound portals _info.checkUnboundPortals(); } - - // note that we're not within a tag to avoid considering any - // characters during this quiescent time - _tag = null; - - // and clear out the character data we're gathering - _chars = new StringBuffer(); - } - - public void characters (char ch[], int start, int length) - { - // bail if we're not within a meaningful tag - if (_tag == null) return; - - _chars.append(ch, start, length); } /** @@ -107,40 +87,11 @@ public class XMLSceneGroupParser extends DefaultHandler */ public List loadSceneGroup (String fname) throws IOException { - _fname = fname; - - try { - // get the file input stream - FileInputStream fis = new FileInputStream(fname); - BufferedInputStream bis = new BufferedInputStream(fis); - - // prepare temporary data storage for parsing - _chars = new StringBuffer(); - _info = new SceneGroupInfo(); - - // read the XML input stream and construct all scene objects - XMLUtil.parse(this, bis); - - // return the final list of scene objects - return _info.getScenes(); - - } catch (ParserConfigurationException pce) { - throw new IOException(pce.toString()); - - } catch (SAXException saxe) { - throw new IOException(saxe.toString()); - } + _info = new SceneGroupInfo(); + parseFile(fname); + return _info.getScenes(); } - /** The file currently being processed. */ - protected String _fname; - - /** The XML element tag currently being processed. */ - protected String _tag; - - /** Temporary storage of scene group values and data. */ - protected StringBuffer _chars; - /** The scene group info gathered while parsing. */ protected SceneGroupInfo _info; diff --git a/src/java/com/threerings/miso/tools/xml/XMLSceneParser.java b/src/java/com/threerings/miso/tools/xml/XMLSceneParser.java index 0e524f35e..38b576beb 100644 --- a/src/java/com/threerings/miso/tools/xml/XMLSceneParser.java +++ b/src/java/com/threerings/miso/tools/xml/XMLSceneParser.java @@ -1,18 +1,16 @@ // -// $Id: XMLSceneParser.java,v 1.19 2001/10/13 01:08:59 shaper Exp $ +// $Id: XMLSceneParser.java,v 1.20 2001/10/15 23:53:43 shaper Exp $ package com.threerings.miso.scene.xml; import java.awt.Point; import java.io.*; import java.util.ArrayList; -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.tile.*; @@ -27,7 +25,7 @@ import com.threerings.miso.tile.MisoTile; * * @see XMLSceneWriter */ -public class XMLSceneParser extends DefaultHandler +public class XMLSceneParser extends SimpleParser { public XMLSceneParser (IsoSceneViewModel model, TileManager tilemgr) { @@ -35,32 +33,28 @@ public class XMLSceneParser extends DefaultHandler _tilemgr = tilemgr; } - public void startElement (String uri, String localName, - String qName, Attributes attributes) + // documentation inherited + public void startElement ( + String uri, String localName, String qName, Attributes attributes) { - _tag = qName; - if (_tag.equals("layer")) { - _info.lnum = getInt(attributes.getValue("lnum")); + if (qName.equals("layer")) { + _info.lnum = parseInt(attributes.getValue("lnum")); - } else if (_tag.equals("row")) { - _info.rownum = getInt(attributes.getValue("rownum")); - _info.colstart = getInt(attributes.getValue("colstart")); + } else if (qName.equals("row")) { + _info.rownum = parseInt(attributes.getValue("rownum")); + _info.colstart = parseInt(attributes.getValue("colstart")); } } - public void endElement (String uri, String localName, String qName) + // documentation inherited + public 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 scene object. - String str = _chars.toString().trim(); - if (qName.equals("name")) { - _info.scene.name = str; + _info.scene.name = data; } else if (qName.equals("version")) { - int version = getInt(str); + int version = parseInt(data); if (version < 0 || version > XMLSceneVersion.VERSION) { Log.warning( "Unrecognized scene file format version, will attempt " + @@ -70,40 +64,23 @@ public class XMLSceneParser extends DefaultHandler } } else if (qName.equals("locations")) { - int vals[] = StringUtil.parseIntArray(str); + int vals[] = StringUtil.parseIntArray(data); addLocations(_info.scene.locations, vals); } else if (qName.equals("cluster")) { - int vals[] = StringUtil.parseIntArray(str); + int vals[] = StringUtil.parseIntArray(data); _info.scene.clusters.add(toCluster(_info.scene.locations, vals)); } else if (qName.equals("portals")) { - String vals[] = StringUtil.parseStringArray(str); + String vals[] = StringUtil.parseStringArray(data); addPortals(_info.scene.portals, _info.scene.locations, vals); } else if (qName.equals("row")) { - addTileRow(_info, str); + addTileRow(_info, data); } else if (qName.equals("scene")) { // nothing for now } - - // note that we're not within a tag to avoid considering any - // characters during this quiescent time - _tag = null; - - // and clear out the character data we're gathering - _chars = new StringBuffer(); - } - - public void characters (char ch[], int start, int length) - { - // bail if we're not within a meaningful tag - if (_tag == null) { - return; - } - - _chars.append(ch, start, length); } /** @@ -257,7 +234,7 @@ public class XMLSceneParser extends DefaultHandler // read in all of the portals for (int ii = 0; ii < vals.length; ii += 2) { - int locidx = getInt(vals[ii]); + int locidx = parseInt(vals[ii]); // create the portal and add to the list Portal portal = new Portal((Location)locs.get(locidx), vals[ii+1]); @@ -268,26 +245,6 @@ public class XMLSceneParser extends DefaultHandler } } - /** - * Parse the given string as an integer and return the integer - * value, or -1 if the string is malformed. - */ - 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; - } - } - - protected void init () - { - _chars = new StringBuffer(); - _info = new SceneInfo(); - } - /** * Parse the specified XML file and return a miso scene object with * the data contained therein. @@ -298,48 +255,20 @@ public class XMLSceneParser extends DefaultHandler */ public EditableMisoScene loadScene (String fname) throws IOException { - _fname = fname; - - try { - // get the file input stream - FileInputStream fis = new FileInputStream(fname); - BufferedInputStream bis = new BufferedInputStream(fis); - - // prepare temporary data storage for parsing - init(); - - // read the XML input stream and construct the scene object - XMLUtil.parse(this, bis); - - // place shadow tiles for any objects in the scene - _info.scene.prepareAllObjectTiles(); - - // return the final scene object - return _info.scene; - - } catch (ParserConfigurationException pce) { - throw new IOException(pce.toString()); - - } catch (SAXException saxe) { - throw new IOException(saxe.toString()); - } + _info = new SceneInfo(); + parseFile(fname); + // place shadow tiles for any objects in the scene + _info.scene.prepareAllObjectTiles(); + // return the final scene object + return _info.scene; } - /** The file currently being processed. */ - protected String _fname; - - /** The XML element tag currently being processed. */ - protected String _tag; - /** The iso scene view data model. */ protected IsoSceneViewModel _model; /** The tile manager object for use in constructing scenes. */ protected TileManager _tilemgr; - /** Temporary storage of character data while parsing. */ - protected StringBuffer _chars; - /** Temporary storage of scene info while parsing. */ protected SceneInfo _info; diff --git a/src/java/com/threerings/miso/util/MisoContext.java b/src/java/com/threerings/miso/util/MisoContext.java index 8ad4907ce..4d7a27ce7 100644 --- a/src/java/com/threerings/miso/util/MisoContext.java +++ b/src/java/com/threerings/miso/util/MisoContext.java @@ -1,16 +1,17 @@ // -// $Id: MisoContext.java,v 1.4 2001/08/16 23:14:21 mdb Exp $ +// $Id: MisoContext.java,v 1.5 2001/10/15 23:53:43 shaper Exp $ package com.threerings.miso.util; import com.samskivert.util.Context; + import com.threerings.media.tile.TileManager; public interface MisoContext extends Context { /** - * Return a reference to the TileManager. This reference is valid - * for the lifetime of the application. + * Returns a reference to the tile manager. This reference is + * valid for the lifetime of the application. */ public TileManager getTileManager (); } diff --git a/tests/rsrc/config/miso/characters.xml b/tests/rsrc/config/miso/characters.xml new file mode 100644 index 000000000..d95df1c5c --- /dev/null +++ b/tests/rsrc/config/miso/characters.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/tests/rsrc/config/miso/tilesets.xml b/tests/rsrc/config/miso/tilesets.xml index a97593e9f..6f376cb30 100644 --- a/tests/rsrc/config/miso/tilesets.xml +++ b/tests/rsrc/config/miso/tilesets.xml @@ -1,12 +1,12 @@ - + - + media/miso/tiles/character.png 94, 94, 94, 94, 94, 94, 94, 94 94, 94, 94, 94, 94, 94, 94, 94 @@ -23,14 +23,14 @@ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1 - + media/miso/tiles/node-icons.png 16 16 8 - + media/miso/tiles/editor-icons.png 22 20 diff --git a/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java b/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java index 31af2e46b..85a082933 100644 --- a/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java +++ b/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java @@ -1,5 +1,5 @@ // -// $Id: ViewerFrame.java,v 1.20 2001/10/11 16:20:06 shaper Exp $ +// $Id: ViewerFrame.java,v 1.21 2001/10/15 23:53:43 shaper Exp $ package com.threerings.miso.viewer; @@ -16,6 +16,7 @@ import com.threerings.media.tile.TileException; import com.threerings.miso.Log; import com.threerings.miso.scene.AmbulatorySprite; +import com.threerings.miso.scene.CharacterManager; import com.threerings.miso.tile.TileUtil; import com.threerings.miso.viewer.util.ViewerContext; @@ -43,8 +44,17 @@ class ViewerFrame extends JFrame implements WindowListener SpriteManager spritemgr = new SpriteManager(); TileManager tilemgr = _ctx.getTileManager(); + // construct the character manager from which we obtain our sprite + CharacterManager charmgr = new CharacterManager( + ctx.getConfig(), tilemgr); + // add the test character sprite to the sprite manager - AmbulatorySprite sprite = createSprite(tilemgr, spritemgr); + AmbulatorySprite sprite = charmgr.getCharacter(TSID_CHAR); + if (sprite != null) { + sprite.setLocation(300, 300); + spritemgr.addSprite(sprite); + Log.info("Created sprite [sprite=" + sprite + "]."); + } // create a top-level panel to manage everything JPanel top = new JPanel(); @@ -63,17 +73,6 @@ class ViewerFrame extends JFrame implements WindowListener getContentPane().add(top, BorderLayout.CENTER); } - protected AmbulatorySprite createSprite (TileManager tilemgr, - SpriteManager spritemgr) - { - MultiFrameImage[] anims = - TileUtil.getAmbulatorySpriteFrames(tilemgr, TSID_CHAR); - AmbulatorySprite sprite = new AmbulatorySprite(300, 300, anims); - spritemgr.addSprite(sprite); - - return sprite; - } - /** WindowListener interface methods */ public void windowClosing (WindowEvent e) {