Reworking exits and locations en route to editor support for placement

within a scene.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@208 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2001-08-09 21:17:06 +00:00
parent 506623be27
commit 593d17aeb5
9 changed files with 225 additions and 141 deletions
@@ -1,5 +1,5 @@
//
// $Id: XMLSceneParser.java,v 1.5 2001/08/02 01:19:47 shaper Exp $
// $Id: XMLSceneParser.java,v 1.6 2001/08/09 21:17:06 shaper Exp $
package com.threerings.miso.scene.xml;
@@ -66,13 +66,13 @@ public class XMLSceneParser extends DefaultHandler
", known_version=" + Scene.VERSION + "].");
}
} else if (qName.equals("hotspots")) {
} else if (qName.equals("locations")) {
int vals[] = StringUtil.parseIntArray(_chars.toString());
_scHotspots = toPointArray(vals);
_scLocations = toLocationsList(vals);
} else if (qName.equals("exits")) {
String vals[] = StringUtil.parseStringArray(_chars.toString());
_scExits = toExitPointArray(vals);
_scExits = toExitList(_scLocations, vals);
} else if (qName.equals("row")) {
if (_scLnum == Scene.LAYER_BASE) {
@@ -84,7 +84,7 @@ public class XMLSceneParser extends DefaultHandler
} else if (qName.equals("scene")) {
// construct the scene object on tag close
_scene = new Scene(
_tilemgr, _scName, _scHotspots, _scExits, _scTiles);
_tilemgr, _scName, _scLocations, _scExits, _scTiles);
Log.info("Constructed parsed scene [scene=" + _scene + "].");
}
@@ -164,33 +164,37 @@ public class XMLSceneParser extends DefaultHandler
}
/**
* Given an array of integer values, return a Point array
* constructed from each successive tuple of values as (x, y) in
* the array.
* Given an array of integer values, return a list of the
* <code>Location</code> objects represented therein, constructed
* from each successive quartet of values as (spotid, x, y,
* orientation) in the integer array.
*
* @param vals the integer values.
*
* @return the point array, or null if an error occurred.
* @return the location list, or null if an error occurred.
*/
protected Point[] toPointArray (int[] vals)
protected ArrayList toLocationsList (int[] vals)
{
// make sure we have an even number of points
if ((vals.length % 2) == 1) return null;
// make sure we have a seemingly-appropriate number of points
if ((vals.length % 4) != 0) return null;
// pull the point coordinates out of the int array
Point[] points = new Point[vals.length / 2];
for (int ii = 0; ii < vals.length; ii += 2) {
int idx = ii / 2;
points[idx] = new Point(vals[ii], vals[ii + 1]);
// read in all of the locations and add to the list
ArrayList list = new ArrayList();
for (int ii = 0; ii < vals.length; ii += 4) {
Location loc = new Location(
vals[ii], vals[ii+1], vals[ii+2], vals[ii+3]);
list.add(loc);
}
return points;
return list;
}
/**
* Given an array of String values, return an ExitPoint array
* constructed from each successive triplet of values as (x, y,
* scene name) in the array.
* Given an array of string values, return a list of
* <code>Exit</code> objects constructed from each successive
* triplet of values as (locidx, scene name) in the array. The
* list of <code>Location</code> objects must have already been
* fully read previously.
*
* <p> This is something of a hack since we perhaps ought to parse
* the original String into its constituent components ourselves,
@@ -198,29 +202,25 @@ public class XMLSceneParser extends DefaultHandler
* <code>StringUtil.toString()</code> method to take care of
* tokenizing things for us, so, there you have it.
*
* @param ArrayList the location list.
* @param vals the String values.
*
* @return the ExitPoint array, or null if an error occurred.
* @return the exit list, or null if an error occurred.
*/
protected ExitPoint[] toExitPointArray (String[] vals)
protected ArrayList toExitList (ArrayList locs, String[] vals)
{
// make sure we have an appropriate number of values
if ((vals.length % 3) != 0) return null;
if ((vals.length % 2) != 0) return null;
// pull the point values out of the string array
ExitPoint[] exits = new ExitPoint[vals.length / 3];
for (int ii = 0; ii < vals.length; ii += 3) {
int idx = ii / 3;
exits[idx] = new ExitPoint();
exits[idx].x = (byte)getInt(vals[ii]);
exits[idx].y = (byte)getInt(vals[ii + 1]);
exits[idx].name = vals[ii + 2];
// read in all of the exits and add to the list
ArrayList list = new ArrayList();
for (int ii = 0; ii < vals.length; ii += 2) {
Exit exit = new Exit(
(Location)locs.get(getInt(vals[ii])), vals[ii+1]);
list.add(exit);
}
// scene id is only valid at runtime
exits[idx].sid = Scene.SID_INVALID;
}
return exits;
return list;
}
/**
@@ -237,6 +237,16 @@ public class XMLSceneParser extends DefaultHandler
}
}
protected void init ()
{
_chars = new StringBuffer();
_scene = null;
int width = Scene.TILE_WIDTH, height = Scene.TILE_HEIGHT;
_scTiles = new Tile[width][height][Scene.NUM_LAYERS];
_scLocations = null;
_scExits = null;
}
/**
* Parse the specified XML file and return a Scene object with the
* data contained therein.
@@ -255,10 +265,7 @@ public class XMLSceneParser extends DefaultHandler
BufferedInputStream bis = new BufferedInputStream(fis);
// prepare temporary data storage for parsing
_chars = new StringBuffer();
_scene = null;
int width = Scene.TILE_WIDTH, height = Scene.TILE_HEIGHT;
_scTiles = new Tile[width][height][Scene.NUM_LAYERS];
init();
// read the XML input stream and construct the scene object
XMLUtil.parse(this, bis);
@@ -289,8 +296,8 @@ public class XMLSceneParser extends DefaultHandler
// temporary storage of scene object values and data
protected StringBuffer _chars;
protected String _scName;
protected Point[] _scHotspots;
protected ExitPoint[] _scExits;
protected ArrayList _scLocations;
protected ArrayList _scExits;
protected Tile[][][] _scTiles;
protected int _scLnum, _scRownum, _scColstart;
}
@@ -1,5 +1,5 @@
//
// $Id: XMLSceneWriter.java,v 1.3 2001/07/24 20:12:18 shaper Exp $
// $Id: XMLSceneWriter.java,v 1.4 2001/08/09 21:17:06 shaper Exp $
package com.threerings.miso.scene.xml;
@@ -13,12 +13,12 @@ import org.xml.sax.helpers.AttributesImpl;
import com.megginson.sax.DataWriter;
import com.threerings.miso.Log;
import com.threerings.miso.scene.ExitPoint;
import com.threerings.miso.scene.Scene;
import com.threerings.miso.scene.*;
import com.threerings.miso.tile.Tile;
/**
* The XMLSceneWriter writes a Scene object to an XML file.
* The <code>XMLSceneWriter</code> writes a <code>Scene</code> object
* to an XML file.
*
* <p> The scene id is omitted as the scene id is assigned when the
* scene template is actually loaded into a server. Similarly, exit
@@ -51,7 +51,7 @@ public class XMLSceneWriter extends DataWriter
dataElement("name", scene.getName());
dataElement("version", "" + Scene.VERSION);
dataElement("hotspots", getHotSpotData(scene));
dataElement("locations", getLocationData(scene));
dataElement("exits", getExitData(scene));
startElement("tiles");
@@ -97,47 +97,51 @@ public class XMLSceneWriter extends DataWriter
}
/**
* Return a string representation of the exit points in the
* scene. Each exit point is specified by a comma-delimited
* triplet of (x, y, scene name) values.
* Return a string representation of the exits in the scene. Each
* exit is specified by a comma-delimited tuple of (location
* index, scene name) values.
*
* @param scene the scene object.
*
* @return the exit points in String format.
* @return the exits in String format.
*/
protected String getExitData (Scene scene)
{
ExitPoint[] exits = scene.getExitPoints();
ArrayList locs = scene.getLocations();
ArrayList exits = scene.getExits();
Object exitinfo[] = new Object[exits.length * 3];
StringBuffer buf = new StringBuffer();
for (int ii = 0; ii < exits.length; ii++) {
int idx = ii * 3;
buf.append(exits[ii].x).append(",");
buf.append(exits[ii].y).append(",");
buf.append(exits[ii].name);
if (ii < exits.length - 1) buf.append(",");
int size = exits.size();
for (int ii = 0; ii < size; ii++) {
Exit exit = (Exit)exits.get(ii);
buf.append(locs.indexOf(exit.loc)).append(",");
buf.append(exit.name);
if (ii < size - 1) buf.append(",");
}
return buf.toString();
}
/**
* Return a string representation of the hotspot points in the
* scene. Each hotspot point is specified by a comma-delimited
* tuple of (x, y) values.
* Return a string representation of the locations in the scene.
* Each location is specified by a comma-delimited quartet of
* (spot id, x, y, orientation) values.
*
* @return the hotspot points in String format.
* @return the locations in String format.
*/
protected String getHotSpotData (Scene scene)
protected String getLocationData (Scene scene)
{
StringBuffer buf = new StringBuffer();
ArrayList locs = scene.getLocations();
Point[] hotspots = scene.getHotSpots();
for (int ii = 0; ii < hotspots.length; ii++) {
buf.append(hotspots[ii].x).append(",");
buf.append(hotspots[ii].y);
if (ii < hotspots.length - 1) buf.append(",");
StringBuffer buf = new StringBuffer();
int size = locs.size();
for (int ii = 0; ii < size; ii++) {
Location loc = (Location)locs.get(ii);
buf.append(loc.spotid).append(",");
buf.append(loc.x).append(",");
buf.append(loc.y).append(",");
buf.append(loc.orient);
if (ii < size - 1) buf.append(",");
}
return buf.toString();