Combined display/runtime/editable scenes; revamped parser system.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2272 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2003-02-12 07:21:50 +00:00
parent 14dbc463a2
commit f06bb6beaa
6 changed files with 106 additions and 474 deletions
@@ -1,97 +0,0 @@
//
// $Id: EditableMisoScene.java,v 1.19 2003/01/31 23:10:45 mdb Exp $
package com.threerings.miso.tools;
import java.awt.Rectangle;
import com.threerings.media.tile.ObjectTile;
import com.threerings.media.tile.Tile;
import com.threerings.media.tile.TileUtil;
import com.threerings.miso.client.DisplayMisoScene;
import com.threerings.miso.client.DisplayObjectInfo;
import com.threerings.miso.data.MisoSceneModel;
import com.threerings.miso.tile.BaseTile;
import com.threerings.miso.tile.BaseTileSet;
/**
* The editable Miso scene interface is used in the offline scene building
* tools as well as by the tools that load those prototype scenes into the
* runtime database. Accordingly, it provides a means for modifying scene
* values and for obtaining access to the underlying scene models that
* represent the underlying scene information.
*
* @see DisplayMisoScene
*/
public interface EditableMisoScene
extends DisplayMisoScene
{
/**
* Returns the default base tile.
*/
public BaseTileSet getDefaultBaseTileSet ();
/**
* Sets the default base tile.
*
* @param defaultBaseTile the new default base tile.
* @param fqTileId the fully-qualified tile id (@see
* TileUtil#getFQTileId}) of the new default base tile.
*/
public void setDefaultBaseTileSet (BaseTileSet defaultBaseTileSet,
int setId);
/**
* Updates the tile at the specified location in the base layer.
*
* @param x the x-coordinate of the tile to set.
* @param y the y-coordinate of the tile to set.
* @param tile the tile to set.
* @param fqTileId the fully-qualified tile id (@see
* TileUtil#getFQTileId}) of the new default base tile.
*/
public void setBaseTile (int x, int y, BaseTile tile, int fqTileId);
/**
* Fill a rectangular area with random tiles from
* the specified base tileset.
*/
public void setBaseTiles (Rectangle r, BaseTileSet set, int setId);
/**
* Adds an object to this scene.
*
* @param x the object's origin x-coordinate.
* @param y the object's origin y-coordinate.
* @param tile the tile to set.
* @param fqTileId the fully-qualified tile id (@see
* TileUtil#getFQTileId}) of the new default base tile.
*
* @return the new scene object instance.
*/
public DisplayObjectInfo addObject (
ObjectTile tile, int x, int y, int fqTileId);
/**
* Clears out the tile at the specified location in the base layer.
*/
public void clearBaseTile (int x, int y);
/**
* Removes the specified object from the scene.
*/
public boolean removeObject (DisplayObjectInfo scobj);
/**
* Returns a reference to the miso scene model that reflects the
* changes that have been made to this editable miso scene.
*/
public MisoSceneModel getMisoSceneModel ();
/**
* Replaces the model in use by this editable miso scene with the
* specified model.
*/
public void setMisoSceneModel (MisoSceneModel model);
}
@@ -1,184 +0,0 @@
//
// $Id: EditableMisoSceneImpl.java,v 1.25 2003/01/31 23:10:45 mdb Exp $
package com.threerings.miso.tools;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import com.threerings.media.tile.NoSuchTileException;
import com.threerings.media.tile.NoSuchTileSetException;
import com.threerings.media.tile.ObjectTile;
import com.threerings.media.tile.Tile;
import com.threerings.media.tile.TileUtil;
import com.threerings.miso.Log;
import com.threerings.miso.tile.BaseTile;
import com.threerings.miso.tile.BaseTileSet;
import com.threerings.miso.tile.MisoTileManager;
import com.threerings.miso.client.DisplayMisoSceneImpl;
import com.threerings.miso.client.DisplayObjectInfo;
import com.threerings.miso.client.util.IsoUtil;
import com.threerings.miso.data.MisoSceneModel;
import com.threerings.miso.data.ObjectInfo;
/**
* The default implementation of the {@link EditableMisoScene} interface.
*/
public class EditableMisoSceneImpl
extends DisplayMisoSceneImpl implements EditableMisoScene
{
/**
* Constructs an instance that will be used to display and edit the
* supplied miso scene data. The tiles identified by the scene model
* will be loaded via the supplied tile manager.
*
* @param model the scene data that we'll be displaying.
* @param tmgr the tile manager from which to load our tiles.
*/
public EditableMisoSceneImpl (MisoSceneModel model, MisoTileManager tmgr)
{
super(model, tmgr);
}
/**
* Constructs an instance that will be used to display and edit the
* supplied miso scene data. The tiles identified by the scene model
* will not be loaded until a tile manager is provided via {@link
* #setTileManager}.
*
* @param model the scene data that we'll be displaying.
*/
public EditableMisoSceneImpl (MisoSceneModel model)
{
super(model);
}
// documentation inherited
public void setMisoSceneModel (MisoSceneModel model)
{
super.setMisoSceneModel(model);
}
// documentation inherited
public BaseTileSet getDefaultBaseTileSet ()
{
return _defaultBaseTileSet;
}
// documentation inherited
public void setDefaultBaseTileSet (BaseTileSet defaultBaseTileSet,
int setId)
{
_defaultBaseTileSet = defaultBaseTileSet;
_defaultBaseTileSetId = setId;
}
// documentation inherited
public void setBaseTiles (Rectangle r, BaseTileSet set, int setId)
{
int setcount = set.getTileCount();
for (int x = r.x; x < r.x + r.width; x++) {
for (int y = r.y; y < r.y + r.height; y++) {
try {
int index = _rando.nextInt(setcount);
_base.setTile(x, y, (BaseTile) set.getTile(index));
_model.setBaseTile(
x, y, TileUtil.getFQTileId(setId, index));
} catch (NoSuchTileException nste) {
// not going to happen
Log.warning("A tileset is lying to me " +
"[error=" + nste + "].");
}
}
}
_fringer.fringe(_model, _fringe, r, _rando);
}
// documentation inherited
public void setBaseTile (int x, int y, BaseTile tile, int fqTileId)
{
_base.setTile(x, y, tile);
_model.setBaseTile(x, y, fqTileId);
_fringer.fringe(_model, _fringe, new Rectangle(x, y, 1, 1), _rando);
}
// documentation inherited
public DisplayObjectInfo addObject (
ObjectTile tile, int x, int y, int fqTileId)
{
// sanity check
if (x > Short.MAX_VALUE || y > Short.MAX_VALUE ||
x < Short.MIN_VALUE || y < Short.MIN_VALUE) {
throw new IllegalArgumentException(
"Invalid tile coordinates [x=" + x + ", y=" + y + "]");
}
// create a scene object record and add it to the list
DisplayObjectInfo info = new DisplayObjectInfo(fqTileId, x, y);
initObject(info, tile);
_objects.add(info);
return info;
}
// documentation inherited
public void clearBaseTile (int x, int y)
{
// implemented as a set of one of the random base tiles
setBaseTiles(new Rectangle(x, y, 1, 1),
_defaultBaseTileSet, _defaultBaseTileSetId);
}
// documentation inherited
public boolean removeObject (DisplayObjectInfo info)
{
if (_objects.remove(info)) {
// toggle the "covered" flag off on the base tiles in this object
// tile's footprint
setObjectTileFootprint(info.tile, info.x, info.y, false);
return true;
} else {
return false;
}
}
// documentation inherited
public MisoSceneModel getMisoSceneModel ()
{
// we need to flush the object layer to the model prior to
// returning it; first split our objects into two lists
ArrayList ilist = new ArrayList();
ArrayList ulist = new ArrayList();
for (int ii = 0, ll = _objects.size(); ii < ll; ii++) {
ObjectInfo info = (ObjectInfo)_objects.get(ii);
if (info.isInteresting()) {
// convert to a plain object info record
ilist.add(new ObjectInfo(info));
} else {
ulist.add(info);
}
}
// now populate the scene model appropriately
MisoSceneModel.populateObjects(_model, ilist, ulist);
// and we're ready to roll
return _model;
}
/** The default tileset with which to fill the base layer. */
protected BaseTileSet _defaultBaseTileSet;
/** The tileset id of the default base tileset. */
protected int _defaultBaseTileSetId;
}
@@ -1,76 +0,0 @@
//
// $Id: MisoSceneParser.java,v 1.3 2003/01/31 23:10:46 mdb Exp $
package com.threerings.miso.tools.xml;
import java.io.IOException;
import java.io.FileInputStream;
import org.xml.sax.SAXException;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.RuleSet;
import com.threerings.miso.data.MisoSceneModel;
/**
* A simple class for parsing a standalone miso scene model.
*/
public class MisoSceneParser
{
/**
* Constructs a miso scene parser that parses scenes with the
* specified XML path prefix and a standard scene rule set. See the
* {@link MisoSceneRuleSet#MisoSceneRuleSet} documentation for more
* information.
*/
public MisoSceneParser (String prefix)
{
MisoSceneRuleSet set = new MisoSceneRuleSet();
set.setPrefix(prefix);
init(prefix, set);
}
/**
* Constructs a miso scene parser that parses scenes with the
* specified XML path prefix, using the supplied rule set. The rule
* set should leave a {@link MisoSceneModel} at the top of the
* digester's stack.
*/
public MisoSceneParser (String prefix, RuleSet rules)
{
init(prefix, rules);
}
/** Constructor helper function. */
protected void init (String prefix, RuleSet rules)
{
// create and configure our digester
_digester = new Digester();
_digester.addRuleSet(rules);
_digester.addSetNext(prefix, "setSceneModel",
MisoSceneModel.class.getName());
}
/**
* Parses the XML file at the specified path into a miso scene model.
*/
public MisoSceneModel parseScene (String path)
throws IOException, SAXException
{
_model = null;
_digester.push(this);
_digester.parse(new FileInputStream(path));
return _model;
}
/**
* Called by the parser once the scene is parsed.
*/
public void setSceneModel (MisoSceneModel model)
{
_model = model;
}
protected Digester _digester;
protected MisoSceneModel _model;
}
@@ -1,102 +0,0 @@
//
// $Id: MisoSceneRuleSet.java,v 1.12 2003/01/31 23:10:46 mdb Exp $
package com.threerings.miso.tools.xml;
import java.util.ArrayList;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.RuleSetBase;
import com.samskivert.xml.CallMethodSpecialRule;
import com.samskivert.xml.SetFieldRule;
import com.samskivert.xml.SetPropertyFieldsRule;
import com.threerings.miso.Log;
import com.threerings.miso.data.MisoSceneModel;
import com.threerings.miso.data.ObjectInfo;
/**
* Used to parse a {@link MisoSceneModel} from XML.
*/
public class MisoSceneRuleSet extends RuleSetBase
{
/**
* Configures the rule set to match scenes with the supplied prefix.
* For example, passing <code>scene.miso</code> will match the scene
* in the following XML file:
*
* <pre>
* &lt;scene&gt;
* &lt;miso&gt;
* &lt;width&gt;50&lt;/width&gt;
* &lt;height&gt;50&lt;/height&gt;
* &lt;!-- ... --&gt;
* &lt;/miso&gt;
* &lt;/scene&gt;
* </pre>
*/
public void setPrefix (String prefix)
{
_prefix = prefix;
}
/**
* Adds the necessary rules to the digester to parse our miso scene
* data.
*/
public void addRuleInstances (Digester digester)
{
// this creates the appropriate instance when we encounter our
// prefix tag
digester.addObjectCreate(_prefix, MisoSceneModel.class.getName());
// set up rules to parse and set our fields
digester.addRule(_prefix + "/width",
new SetFieldRule(digester, "width"));
digester.addRule(_prefix + "/height",
new SetFieldRule(digester, "height"));
digester.addRule(_prefix + "/viewwidth",
new SetFieldRule(digester, "vwidth"));
digester.addRule(_prefix + "/viewheight",
new SetFieldRule(digester, "vheight"));
digester.addRule(_prefix + "/base",
new SetFieldRule(digester, "baseTileIds"));
digester.addObjectCreate(_prefix + "/objects",
ArrayList.class.getName());
digester.addObjectCreate(_prefix + "/objects/object",
ObjectInfo.class.getName());
digester.addSetNext(_prefix + "/objects/object", "add",
Object.class.getName());
digester.addRule(_prefix + "/objects/object",
new SetPropertyFieldsRule(digester));
digester.addRule(_prefix + "/objects", new CallMethodSpecialRule(
digester) {
public void parseAndSet (String bodyText, Object target)
throws Exception
{
ArrayList ilist = (ArrayList)target;
ArrayList ulist = new ArrayList();
MisoSceneModel model = (MisoSceneModel)this.digester.peek(1);
// filter interesting and uninteresting into two lists
for (int ii = 0; ii < ilist.size(); ii++) {
ObjectInfo info = (ObjectInfo)ilist.get(ii);
if (!info.isInteresting()) {
ilist.remove(ii--);
ulist.add(info);
}
}
// now populate the model
MisoSceneModel.populateObjects(model, ilist, ulist);
}
});
}
/** The prefix at which me match our scenes. */
protected String _prefix;
}
@@ -0,0 +1,90 @@
//
// $Id: SimpleMisoSceneRuleSet.java,v 1.1 2003/02/12 07:21:50 mdb Exp $
package com.threerings.miso.tools.xml;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.Rule;
import com.samskivert.xml.CallMethodSpecialRule;
import com.samskivert.xml.SetFieldRule;
import com.samskivert.xml.SetPropertyFieldsRule;
import com.threerings.tools.xml.NestableRuleSet;
import com.threerings.miso.data.ObjectInfo;
import com.threerings.miso.data.SimpleMisoSceneModel;
/**
* Used to parse a {@link SimpleMisoSceneModel} from XML.
*/
public class SimpleMisoSceneRuleSet implements NestableRuleSet
{
// documentation inherited from interface
public String getOuterElement ()
{
return SimpleMisoSceneWriter.OUTER_ELEMENT;
}
// documentation inherited from interface
public void addRuleInstances (String prefix, Digester dig)
{
// this creates the appropriate instance when we encounter our
// prefix tag
dig.addRule(prefix, new Rule(dig) {
public void begin (Attributes attributes) throws Exception {
digester.push(createMisoSceneModel());
}
public void end () throws Exception {
digester.pop();
}
});
// set up rules to parse and set our fields
dig.addRule(prefix + "/width", new SetFieldRule(dig, "width"));
dig.addRule(prefix + "/height", new SetFieldRule(dig, "height"));
dig.addRule(prefix + "/viewwidth", new SetFieldRule(dig, "vwidth"));
dig.addRule(prefix + "/viewheight", new SetFieldRule(dig, "vheight"));
dig.addRule(prefix + "/base", new SetFieldRule(dig, "baseTileIds"));
dig.addObjectCreate(prefix + "/objects", ArrayList.class.getName());
dig.addObjectCreate(prefix + "/objects/object",
ObjectInfo.class.getName());
dig.addSetNext(prefix + "/objects/object", "add",
Object.class.getName());
dig.addRule(prefix + "/objects/object",
new SetPropertyFieldsRule(dig));
dig.addRule(prefix + "/objects", new CallMethodSpecialRule(dig) {
public void parseAndSet (String bodyText, Object target)
throws Exception
{
ArrayList ilist = (ArrayList)target;
ArrayList ulist = new ArrayList();
SimpleMisoSceneModel model = (SimpleMisoSceneModel)
digester.peek(1);
// filter interesting and uninteresting into two lists
for (int ii = 0; ii < ilist.size(); ii++) {
ObjectInfo info = (ObjectInfo)ilist.get(ii);
if (!info.isInteresting()) {
ilist.remove(ii--);
ulist.add(info);
}
}
// now populate the model
SimpleMisoSceneModel.populateObjects(model, ilist, ulist);
}
});
}
protected SimpleMisoSceneModel createMisoSceneModel ()
{
return new SimpleMisoSceneModel(0, 0, 0, 0);
}
}
@@ -1,5 +1,5 @@
//
// $Id: MisoSceneWriter.java,v 1.9 2003/01/31 23:10:46 mdb Exp $
// $Id: SimpleMisoSceneWriter.java,v 1.1 2003/02/12 07:21:50 mdb Exp $
package com.threerings.miso.tools.xml;
@@ -8,35 +8,36 @@ import org.xml.sax.helpers.AttributesImpl;
import com.megginson.sax.DataWriter;
import com.samskivert.util.StringUtil;
import com.threerings.tools.xml.NestableWriter;
import com.threerings.miso.data.MisoSceneModel;
import com.threerings.miso.data.ObjectInfo;
import com.threerings.miso.data.SimpleMisoSceneModel;
/**
* Generates an XML representation of a {@link MisoSceneModel}.
* Generates an XML representation of a {@link SimpleMisoSceneModel}.
*/
public class MisoSceneWriter
public class SimpleMisoSceneWriter implements NestableWriter
{
/**
* Writes the data for the supplied {@link MisoSceneModel} to the XML
* writer supplied. The writer will already be configured with the
* appropriate indentation level so that this writer can simply output
* its elements and allow the calling code to determine where in the
* greater scene description file the miso data should live.
*/
public void writeScene (MisoSceneModel model, DataWriter writer)
/** The element used to enclose scene models written with this
* writer. */
public static final String OUTER_ELEMENT = "miso";
// documentation inherited from interface
public void write (Object object, DataWriter writer)
throws SAXException
{
writer.startElement("miso");
SimpleMisoSceneModel model = (SimpleMisoSceneModel)object;
writer.startElement(OUTER_ELEMENT);
writeSceneData(model, writer);
writer.endElement("miso");
writer.endElement(OUTER_ELEMENT);
}
/**
* Writes just the scene data which is handy for derived classes which
* may wish to add their own scene data to the scene output.
*/
protected void writeSceneData (MisoSceneModel model, DataWriter writer)
protected void writeSceneData (SimpleMisoSceneModel model,
DataWriter writer)
throws SAXException
{
writer.dataElement("width", Integer.toString(model.width));