diff --git a/src/java/com/threerings/cast/builder/BuilderModel.java b/src/java/com/threerings/cast/builder/BuilderModel.java index e86cb2b75..8d49be6d3 100644 --- a/src/java/com/threerings/cast/builder/BuilderModel.java +++ b/src/java/com/threerings/cast/builder/BuilderModel.java @@ -1,7 +1,7 @@ // -// $Id: BuilderModel.java,v 1.2 2001/11/02 15:39:07 shaper Exp $ +// $Id: BuilderModel.java,v 1.3 2001/11/18 04:09:21 mdb Exp $ -package com.threerings.cast.builder; +package com.threerings.cast.tools.builder; import java.util.*; diff --git a/src/java/com/threerings/cast/builder/BuilderModelListener.java b/src/java/com/threerings/cast/builder/BuilderModelListener.java index 7d6fcfa68..56b4bce50 100644 --- a/src/java/com/threerings/cast/builder/BuilderModelListener.java +++ b/src/java/com/threerings/cast/builder/BuilderModelListener.java @@ -1,7 +1,7 @@ // -// $Id: BuilderModelListener.java,v 1.1 2001/11/02 01:10:28 shaper Exp $ +// $Id: BuilderModelListener.java,v 1.2 2001/11/18 04:09:21 mdb Exp $ -package com.threerings.cast.builder; +package com.threerings.cast.tools.builder; /** * The builder model listener interface should be implemented by diff --git a/src/java/com/threerings/cast/builder/BuilderPanel.java b/src/java/com/threerings/cast/builder/BuilderPanel.java index 4ceb8ba82..572e10936 100644 --- a/src/java/com/threerings/cast/builder/BuilderPanel.java +++ b/src/java/com/threerings/cast/builder/BuilderPanel.java @@ -1,7 +1,7 @@ // -// $Id: BuilderPanel.java,v 1.3 2001/11/02 01:10:28 shaper Exp $ +// $Id: BuilderPanel.java,v 1.4 2001/11/18 04:09:21 mdb Exp $ -package com.threerings.cast.builder; +package com.threerings.cast.tools.builder; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; diff --git a/src/java/com/threerings/cast/builder/ClassEditor.java b/src/java/com/threerings/cast/builder/ClassEditor.java index fb6b9e1e0..d9ade9560 100644 --- a/src/java/com/threerings/cast/builder/ClassEditor.java +++ b/src/java/com/threerings/cast/builder/ClassEditor.java @@ -1,7 +1,7 @@ // -// $Id: ClassEditor.java,v 1.1 2001/11/02 01:10:28 shaper Exp $ +// $Id: ClassEditor.java,v 1.2 2001/11/18 04:09:21 mdb Exp $ -package com.threerings.cast.builder; +package com.threerings.cast.tools.builder; import java.util.List; diff --git a/src/java/com/threerings/cast/builder/ComponentPanel.java b/src/java/com/threerings/cast/builder/ComponentPanel.java index 7a571fe3d..fe1d552e5 100644 --- a/src/java/com/threerings/cast/builder/ComponentPanel.java +++ b/src/java/com/threerings/cast/builder/ComponentPanel.java @@ -1,7 +1,7 @@ // -// $Id: ComponentPanel.java,v 1.4 2001/11/02 15:39:07 shaper Exp $ +// $Id: ComponentPanel.java,v 1.5 2001/11/18 04:09:21 mdb Exp $ -package com.threerings.cast.builder; +package com.threerings.cast.tools.builder; import java.util.List; import java.util.Map; diff --git a/src/java/com/threerings/cast/builder/SpritePanel.java b/src/java/com/threerings/cast/builder/SpritePanel.java index 18bba8996..e8d4394b8 100644 --- a/src/java/com/threerings/cast/builder/SpritePanel.java +++ b/src/java/com/threerings/cast/builder/SpritePanel.java @@ -1,7 +1,7 @@ // -// $Id: SpritePanel.java,v 1.2 2001/11/02 01:10:28 shaper Exp $ +// $Id: SpritePanel.java,v 1.3 2001/11/18 04:09:21 mdb Exp $ -package com.threerings.cast.builder; +package com.threerings.cast.tools.builder; import java.awt.*; import javax.swing.BorderFactory; diff --git a/src/java/com/threerings/cast/tools/xml/XMLComponentParser.java b/src/java/com/threerings/cast/tools/xml/XMLComponentParser.java index aa5f6c246..5147f6bbd 100644 --- a/src/java/com/threerings/cast/tools/xml/XMLComponentParser.java +++ b/src/java/com/threerings/cast/tools/xml/XMLComponentParser.java @@ -1,41 +1,36 @@ // -// $Id: XMLComponentParser.java,v 1.3 2001/11/01 01:40:42 shaper Exp $ +// $Id: XMLComponentParser.java,v 1.4 2001/11/18 04:09:21 mdb Exp $ -package com.threerings.cast; +package com.threerings.cast.tools.xml; import java.awt.Point; import java.io.IOException; import java.util.Arrays; +import java.util.HashMap; import org.xml.sax.*; import com.samskivert.util.*; import com.samskivert.xml.SimpleParser; -import com.threerings.media.ImageManager; -import com.threerings.media.tile.*; +import com.threerings.media.tile.TileSet; +import com.threerings.media.tile.xml.XMLTileSetParser; import com.threerings.cast.Log; +import com.threerings.cast.ActionSequence; +import com.threerings.cast.ComponentClass; +import com.threerings.cast.CharacterComponent; /** * Parses an XML character component description file and populates - * hashtables with {@link ActionSequence}, {@link ComponentClass}, and - * the information necessary to construct {@link CharacterComponent} - * objects. + * hashtables with {@link ActionSequence}, {@link ComponentClass}, and the + * information necessary to construct {@link CharacterComponent} objects. * *

Does not currently perform validation on the input XML stream, * though the parsing code assumes the XML document is well-formed. */ public class XMLComponentParser extends SimpleParser { - /** - * Constructs an xml component parser. - */ - public XMLComponentParser (ImageManager imgmgr) - { - _imgmgr = imgmgr; - } - // documentation inherited public void startElement ( String uri, String localName, String qName, Attributes attributes) @@ -106,7 +101,7 @@ public class XMLComponentParser extends SimpleParser } // parse the tile set description file - XMLTileSetParser p = new XMLTileSetParser(_imgmgr); + XMLTileSetParser p = new XMLTileSetParser(); try { p.loadTileSets(path, _tilesets); } catch (IOException e) { @@ -127,11 +122,12 @@ public class XMLComponentParser extends SimpleParser as.name = attrs.getValue("name"); as.fileid = attrs.getValue("fileid"); - int tsid = parseInt(attrs.getValue("tsid")); - as.tileset = (TileSet)_tilesets.get(tsid); + String tileset = attrs.getValue("tileset"); + as.tileset = (TileSet)_tilesets.get(tileset); if (as.tileset == null) { Log.warning("Action sequence references non-existent " + - "tile set [asid=" + as.asid + ", tsid=" + tsid + "]."); + "tile set [asid=" + as.asid + + ", tileset=" + tileset + "]."); } as.fps = parseInt(attrs.getValue("fps")); @@ -240,7 +236,7 @@ public class XMLComponentParser extends SimpleParser protected String _imagedir; /** The hashtable of component tile sets. */ - protected HashIntMap _tilesets = new HashIntMap(); + protected HashMap _tilesets = new HashMap(); /** The hashtable of action sequences gathered while parsing. */ protected HashIntMap _actions; @@ -250,7 +246,4 @@ public class XMLComponentParser extends SimpleParser /** The hashtable of character components gathered while parsing. */ protected HashIntMap _components; - - /** The image manager. */ - protected ImageManager _imgmgr; } diff --git a/src/java/com/threerings/cast/tools/xml/XMLComponentRepository.java b/src/java/com/threerings/cast/tools/xml/XMLComponentRepository.java index 1a3699657..350dc0a57 100644 --- a/src/java/com/threerings/cast/tools/xml/XMLComponentRepository.java +++ b/src/java/com/threerings/cast/tools/xml/XMLComponentRepository.java @@ -1,7 +1,7 @@ // -// $Id: XMLComponentRepository.java,v 1.6 2001/11/02 15:37:51 shaper Exp $ +// $Id: XMLComponentRepository.java,v 1.7 2001/11/18 04:09:21 mdb Exp $ -package com.threerings.miso.scene.xml; +package com.threerings.cast.tools.xml; import java.io.IOException; import java.util.Collections; @@ -29,12 +29,12 @@ public class XMLComponentRepository implements ComponentRepository /** * Constructs an xml component repository. */ - public XMLComponentRepository (Config config, ImageManager imgmgr) + public XMLComponentRepository (Config config) { // load component types and components String file = config.getValue(COMPONENTS_KEY, DEFAULT_COMPONENTS); try { - XMLComponentParser p = new XMLComponentParser(imgmgr); + XMLComponentParser p = new XMLComponentParser(); p.loadComponents(file, _actions, _classes, _components); _imagedir = p.getImageDir(); diff --git a/src/java/com/threerings/cast/util/CastUtil.java b/src/java/com/threerings/cast/util/CastUtil.java index e4b498463..81f514c79 100644 --- a/src/java/com/threerings/cast/util/CastUtil.java +++ b/src/java/com/threerings/cast/util/CastUtil.java @@ -1,5 +1,5 @@ // -// $Id: CastUtil.java,v 1.1 2001/11/02 15:28:20 shaper Exp $ +// $Id: CastUtil.java,v 1.2 2001/11/18 04:09:21 mdb Exp $ package com.threerings.cast.util; @@ -7,10 +7,11 @@ import java.util.ArrayList; import java.util.Iterator; import com.samskivert.util.CollectionUtil; - import com.threerings.media.util.RandomUtil; -import com.threerings.cast.*; +import com.threerings.cast.CharacterDescriptor; +import com.threerings.cast.CharacterManager; +import com.threerings.cast.ComponentClass; /** * Miscellaneous cast utility routines. @@ -18,11 +19,11 @@ import com.threerings.cast.*; public class CastUtil { /** - * Returns a new character descriptor populated with a random set - * of components. + * Returns a new character descriptor populated with a random set of + * components. */ - public static CharacterDescriptor - getRandomDescriptor (CharacterManager charmgr) + public static CharacterDescriptor getRandomDescriptor ( + CharacterManager charmgr) { // get all available classes ArrayList classes = new ArrayList(); diff --git a/src/java/com/threerings/cast/util/TileUtil.java b/src/java/com/threerings/cast/util/TileUtil.java index d05432a8e..5cf846b00 100644 --- a/src/java/com/threerings/cast/util/TileUtil.java +++ b/src/java/com/threerings/cast/util/TileUtil.java @@ -1,5 +1,5 @@ // -// $Id: TileUtil.java,v 1.2 2001/11/08 03:04:44 mdb Exp $ +// $Id: TileUtil.java,v 1.3 2001/11/18 04:09:21 mdb Exp $ package com.threerings.cast.util; @@ -91,7 +91,7 @@ public class TileUtil Image imgs[] = new Image[frameCount]; for (int jj = 0; jj < frameCount; jj++) { int idx = (dir * frameCount) + jj; - imgs[jj] = tset.getTile(idx).img; + imgs[jj] = tset.getTileImage(idx); } // create the multi frame image diff --git a/src/java/com/threerings/media/image/ImageManager.java b/src/java/com/threerings/media/image/ImageManager.java index cc1d96e38..12aa01dca 100644 --- a/src/java/com/threerings/media/image/ImageManager.java +++ b/src/java/com/threerings/media/image/ImageManager.java @@ -1,101 +1,54 @@ // -// $Id: ImageManager.java,v 1.6 2001/11/02 01:08:52 shaper Exp $ +// $Id: ImageManager.java,v 1.7 2001/11/18 04:09:21 mdb Exp $ package com.threerings.media; -import java.awt.*; -import java.awt.image.*; +import java.awt.image.BufferedImage; import java.io.IOException; -import java.util.Hashtable; +import java.util.HashMap; + +import javax.imageio.ImageIO; import com.threerings.resource.ResourceManager; - import com.threerings.media.Log; /** - * The ImageManager class provides a single point of access for image - * retrieval and caching. - * - *

Note: The ImageManager must be constructed with a root - * AWT component, in the interest of allowing for proper preparation - * of images for optimal storage and eventual display. + * Provides a single point of access for image retrieval and caching. */ public class ImageManager { /** - * Construct an ImageManager object with the ResourceManager from - * which it will obtain its data, and a root component to which - * images will be rendered. + * Construct an image manager with the specified {@link + * ResourceManager} from which it will obtain its data. */ - public ImageManager (ResourceManager rmgr, Component root) + public ImageManager (ResourceManager rmgr) { _rmgr = rmgr; - _root = root; - _tk = root.getToolkit(); } /** - * Load the image from the specified filename and cache it for - * faster retrieval henceforth. + * Loads the image via the resource manager using the specified path + * and caches it for faster retrieval henceforth. */ - public Image getImage (String fname) + public BufferedImage getImage (String path) + throws IOException { - if (_root == null) { - Log.warning("Attempt to get image without valid root component."); - return null; - } - - Image img = (Image)_imgs.get(fname); + BufferedImage img = (BufferedImage)_imgs.get(path); if (img != null) { - // Log.info("Retrieved image from cache [fname=" + fname + "]."); + // Log.info("Retrieved image from cache [path=" + path + "]."); return img; } - // Log.info("Loading image into cache [fname=" + fname + "]."); - - try { - byte[] data = _rmgr.getResourceAsBytes(fname); - img = _tk.createImage(data); - MediaTracker tracker = new MediaTracker(_root); - tracker.addImage(img, 0); - - tracker.waitForID(0); - if (tracker.isErrorAny()) { - Log.warning("Error loading image [fname=" + fname + "]."); - return null; - - } else { - _imgs.put(fname, img); - } - - } catch (IOException ioe) { - Log.warning("Exception loading image [ioe=" + ioe + "]."); - - } catch (InterruptedException ie) { - Log.warning("Interrupted loading image [ie=" + ie + "]."); - } - - return img; - } - - /** - * Creates a new image representing the specified rectangular - * section cropped from the specified full image object. - */ - public Image getImageCropped (Image fullImg, int x, int y, - int width, int height) - { - BufferedImage img = - new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); - - Graphics2D gfx = img.createGraphics(); - gfx.drawImage(fullImg, -x, -y, null); - - return img; + // Log.info("Loading image into cache [path=" + path + "]."); + img = ImageIO.read(_rmgr.getResource(path)); + _imgs.put(path, img); + return img; } + /** A reference to the resource manager via which we load image data + * by default. */ protected ResourceManager _rmgr; - protected Component _root; - protected Hashtable _imgs = new Hashtable(); - protected Toolkit _tk = Toolkit.getDefaultToolkit(); + + /** A cache of loaded images. */ + protected HashMap _imgs = new HashMap(); } diff --git a/src/java/com/threerings/media/tile/ImageProvider.java b/src/java/com/threerings/media/tile/ImageProvider.java new file mode 100644 index 000000000..cf68d1377 --- /dev/null +++ b/src/java/com/threerings/media/tile/ImageProvider.java @@ -0,0 +1,26 @@ +// +// $Id: ImageProvider.java,v 1.1 2001/11/18 04:09:21 mdb Exp $ + +package com.threerings.media.tile; + +import java.awt.image.BufferedImage; +import java.io.IOException; + +/** + * A tileset needs to load its images from some location. That will + * generally either be via the {@link TileManager} that constructed it or + * the {@link TileSetRepository} that constructed it. The tile manager + * loads images via the resource manager, whereas the tileset repository + * will likely obtain images via its own resource bundles. + */ +public interface ImageProvider +{ + /** + * Loads the image with the specified path. + * + * @exception IOException thrown if an error occurs loading the image + * data. + */ + public BufferedImage loadImage (String path) + throws IOException; +} diff --git a/src/java/com/threerings/media/tile/ObjectTile.java b/src/java/com/threerings/media/tile/ObjectTile.java index d3afd1fe8..e036ffd6a 100644 --- a/src/java/com/threerings/media/tile/ObjectTile.java +++ b/src/java/com/threerings/media/tile/ObjectTile.java @@ -1,38 +1,77 @@ // -// $Id: ObjectTile.java,v 1.3 2001/11/01 01:40:42 shaper Exp $ +// $Id: ObjectTile.java,v 1.4 2001/11/18 04:09:21 mdb Exp $ package com.threerings.media.tile; -import java.awt.*; +import java.awt.Image; /** * An object tile extends the base tile to provide support for objects - * whose image spans more than one tile. An object tile has - * dimensions that represent its footprint or "shadow", which the - * scene containing the tile can then reference to do things like - * making the footprint tiles impassable. + * whose image spans more than one unit tile. An object tile has + * dimensions (in tile units) that represent its footprint or "shadow". */ public class ObjectTile extends Tile { - /** The object footprint dimensions in unit tile units. */ - public int baseWidth, baseHeight; + /** + * Constructs a new object tile with the specified image. The base + * width and height should be set before using this tile. + */ + public ObjectTile (Image image) + { + super(image); + } /** - * Constructs a new object tile. + * Constructs a new object tile with the specified base width and + * height. */ - public ObjectTile (int tsid, int tid, int baseWidth, int baseHeight) + public ObjectTile (Image image, int baseWidth, int baseHeight) { - super(tsid, tid); + super(image); + _baseWidth = baseWidth; + _baseHeight = baseHeight; + } - this.baseWidth = baseWidth; - this.baseHeight = baseHeight; + /** + * Returns the object footprint width in tile units. + */ + public int getBaseWidth () + { + return _baseWidth; + } + + /** + * Returns the object footprint height in tile units. + */ + public int getBaseHeight () + { + return _baseHeight; + } + + /** + * Sets the object footprint width in tile units. + */ + public void setBaseWidth (int baseWidth) + { + _baseWidth = baseWidth; + } + + /** + * Sets the object footprint height in tile units. + */ + public void setBaseHeight (int baseHeight) + { + _baseHeight = baseHeight; } // documentation inherited public void toString (StringBuffer buf) { super.toString(buf); - buf.append(", baseWidth=").append(baseWidth); - buf.append(", baseHeight=").append(baseHeight); + buf.append(", baseWidth=").append(_baseWidth); + buf.append(", baseHeight=").append(_baseHeight); } + + /** The object footprint dimensions in unit tile units. */ + protected int _baseWidth, _baseHeight; } diff --git a/src/java/com/threerings/media/tile/ObjectTileLayer.java b/src/java/com/threerings/media/tile/ObjectTileLayer.java new file mode 100644 index 000000000..142370242 --- /dev/null +++ b/src/java/com/threerings/media/tile/ObjectTileLayer.java @@ -0,0 +1,84 @@ +// +// $Id: ObjectTileLayer.java,v 1.1 2001/11/18 04:09:21 mdb Exp $ + +package com.threerings.media.tile; + +/** + * The object tile layer class is a convenience class provided to simplify + * the management of a two-dimensional array of object tiles. It takes + * care of dereferencing the tile array efficiently with methods that the + * Java compiler should inline, and prevents the caller from having to do + * the indexing multiplication by hand every time. + * + *

This is equivalent to {@link TileLayer} except that it contains + * {@link ObjectTile} instances. For efficiency's sake, we don't extend + * that class but instead provide a direct implementation. + */ +public final class ObjectTileLayer +{ + /** + * Constructs an object tile layer instance with the supplied tiles, + * width and height. The tiles should exist in row-major format (the + * first row of tiles followed by the second and so on). + * + * @exception IllegalArgumentException thrown if the size of the tiles + * array does not match the specified width and height. + */ + public ObjectTileLayer (ObjectTile[] tiles, int width, int height) + { + // sanity check + if (tiles.length != width*height) { + String errmsg = "tiles.length != width*height"; + throw new IllegalArgumentException(errmsg); + } + + _tiles = tiles; + _width = width; + _height = height; + } + + /** + * Returns the width of the layer in tiles. + */ + public int getWidth () + { + return _width; + } + + /** + * Returns the height of the layer in tiles. + */ + public int getHeight () + { + return _height; + } + + /** + * Fetches the tile at the specified row and column. Bounds checking + * is not done. Note that the parameters are column first, followed by + * row (x, y order rather than row, column order). + */ + public ObjectTile getTile (int column, int row) + { + return _tiles[row*_width+column]; + } + + /** + * Sets the tile at the specified row and column. Bounds checking is + * not done. Note that the parameters are column first, followed by + * row (x, y order rather than row, column order). + */ + public void setTile (int column, int row, ObjectTile tile) + { + _tiles[row*_width+column] = tile; + } + + /** Our tiles array. */ + private ObjectTile[] _tiles; + + /** The number of tiles in a row. */ + private int _width; + + /** The number of rows. */ + private int _height; +} diff --git a/src/java/com/threerings/media/tile/ObjectTileSet.java b/src/java/com/threerings/media/tile/ObjectTileSet.java index 347e91231..4c2e2a730 100644 --- a/src/java/com/threerings/media/tile/ObjectTileSet.java +++ b/src/java/com/threerings/media/tile/ObjectTileSet.java @@ -1,65 +1,66 @@ // -// $Id: ObjectTileSet.java,v 1.1 2001/11/08 03:04:44 mdb Exp $ +// $Id: ObjectTileSet.java,v 1.2 2001/11/18 04:09:21 mdb Exp $ package com.threerings.media.tile; import java.awt.Image; -import java.awt.Point; +import java.util.Arrays; -import com.samskivert.util.HashIntMap; - -import com.threerings.media.Log; import com.threerings.media.ImageManager; /** - * The objcet tileset supports the specification of object information for + * The object tileset supports the specification of object information for * object tiles in addition to all of the features of the swiss army * tileset. + * + * @see ObjectTile */ public class ObjectTileSet extends SwissArmyTileSet { /** - * Constructs a tileset with all of the swiss army configuration - * parameters, with the addition of object information for those tiles - * in the set that are object tiles. + * Adds object data for the tile at the specified index. * - * @param objects object information for those tiles that are objects. - * - * @see SwissArmyTileSet#SwissArmyTileSet + * @param tileIndex the tile for which we are adding object data. + * @param objWidth the width of the object in tile units. + * @param objHeight the height of the object in tile units. */ - public ObjectTileSet ( - ImageManager imgmgr, String imgPath, String name, int tsid, - int[] tileCount, int[] rowWidth, int[] rowHeight, - Point offsetPos, Point gapDist, HashIntMap objects) + public void addObjectData (int tileIndex, int objWidth, int objHeight) { - super(imgmgr, imgPath, name, tsid, tileCount, rowWidth, rowHeight, - offsetPos, gapDist); + // create our objects arrays if we've not already got them + if (_owidths == null) { + _owidths = new int[getTileCount()]; + _oheights = new int[_owidths.length]; + // initialize the default tile dimensions to one unit + Arrays.fill(_owidths, 1); + Arrays.fill(_oheights, 1); + } - // keep this for later - _objects = objects; + // now fill in the appropriate slot + _owidths[tileIndex] = objWidth; + _oheights[tileIndex] = objHeight; } /** - * Creates instances of {@link ObjectTile} which can span more than a + * Creates instances of {@link ObjectTile}, which can span more than a * single tile's space in a display. */ - protected Tile createTile (int tileId) + protected Tile createTile (int tileIndex, Image image) { // default object dimensions to (1, 1) int wid = 1, hei = 1; // retrieve object dimensions if known - if (_objects != null) { - int size[] = (int[])_objects.get(tileId); - if (size != null) { - wid = size[0]; - hei = size[1]; - } + if (_owidths != null) { + wid = _owidths[tileIndex]; + wid = _oheights[tileIndex]; } - return new ObjectTile(_tsid, tileId, wid, hei); + return new ObjectTile(image, wid, hei); } - /** Mapping of object tile ids to object dimensions. */ - protected HashIntMap _objects; + /** The width (in tile units) of our object tiles. */ + protected int[] _owidths; + + /** The height (in tile units) of our object tiles. */ + protected int[] _oheights; } diff --git a/src/java/com/threerings/media/tile/SwissArmyTileSet.java b/src/java/com/threerings/media/tile/SwissArmyTileSet.java index 2819828a4..94a9242a3 100644 --- a/src/java/com/threerings/media/tile/SwissArmyTileSet.java +++ b/src/java/com/threerings/media/tile/SwissArmyTileSet.java @@ -1,10 +1,17 @@ // -// $Id: SwissArmyTileSet.java,v 1.1 2001/11/08 03:04:44 mdb Exp $ +// $Id: SwissArmyTileSet.java,v 1.2 2001/11/18 04:09:21 mdb Exp $ package com.threerings.media.tile; import java.awt.Image; import java.awt.Point; +import java.awt.Dimension; +import java.awt.image.BufferedImage; + +import java.io.IOException; +import java.io.ObjectInputStream; + +import com.samskivert.util.StringUtil; import com.threerings.media.Log; import com.threerings.media.ImageManager; @@ -18,63 +25,88 @@ import com.threerings.media.ImageManager; */ public class SwissArmyTileSet extends TileSet { - /** - * The full monty. Constructs a swiss army tile set object with the - * full panoply of parameters, specifying everything under the sun. - * Each row in the tileset image must contain tiles of the same width - * and height, but those values can vary from row to row. Each row can - * contain an arbitrary number of tiles. The tiles can be offset from - * the upper left of the image and can have a uniform horizontal and - * vertical distance between each tile (horizontal doesn't have to be - * the same as vertical but all horizontal distances must be the same, - * for example). - * - * @param imgmgr an image manager from which the tileset image can be - * loaded. - * @param imgPath the path to the tileset image. - * @param name the name of the tileset (optional, can be null). - * @param tsid the unique integer identifier of the tileset (optional, - * can be zero if the tileset is not to be loaded by id). - * @param tileCount an array containing the number of tiles in each - * row. - * @param rowWidth an array containing the width of the tiles in each - * row. - * @param rowHeight an array containing the height of the tiles in - * each row. - * @param offsetPos the offset to the upper left of the first tile. - * @param gapDist the number of pixels (x for horizontally and y for - * vertically) in between each tile in the tileset image. - */ - public SwissArmyTileSet ( - ImageManager imgmgr, String imgPath, String name, int tsid, - int tileCount[], int rowWidth[], int rowHeight[], - Point offsetPos, Point gapDist) - { - super(imgmgr, imgPath, name, tsid); - - // keep these around - _tileCount = tileCount; - _rowWidth = rowWidth; - _rowHeight = rowHeight; - _offsetPos = offsetPos; - _gapDist = gapDist; - - // compute our number of tiles - for (int i = 0; i < tileCount.length; i++) { - _numTiles += tileCount[i]; - } - } - // documentation inherited public int getTileCount () { return _numTiles; } - // documentation inherited - protected Image getTileImage (int tileId) + /** + * Sets the tile counts which are the number of tiles in each row of + * the tileset image. Each row can have an arbitrary number of tiles. + */ + public void setTileCounts (int[] tileCounts) { - Image tsimg = getTilesetImage(); + _tileCounts = tileCounts; + + // compute our total tile count + computeTileCount(); + } + + /** + * Computes our total tile count from the individual counts for each + * row. + */ + protected void computeTileCount () + { + // compute our number of tiles + for (int i = 0; i < _tileCounts.length; i++) { + _numTiles += _tileCounts[i]; + } + } + + /** + * Sets the tile widths for each row. Each row can have tiles of a + * different width. + */ + public void setWidths (int[] widths) + { + _widths = widths; + } + + /** + * Sets the tile heights for each row. Each row can have tiles of a + * different height. + */ + public void setHeights (int[] heights) + { + _heights = heights; + } + + /** + * Sets the offset in pixels of the upper left corner of the first + * tile in the first row. If the tileset image has a border, this can + * be set to account for it. + */ + public void setOffsetPos (Point offsetPos) + { + _offsetPos = offsetPos; + } + + /** + * Sets the size of the gap between tiles (in pixels). If the tiles + * have space between them, this can be set to account for it. + */ + public void setGapSize (Dimension gapSize) + { + _gapSize = gapSize; + } + + // documentation inherited + protected void toString (StringBuffer buf) + { + super.toString(buf); + buf.append(", widths=").append(StringUtil.toString(_widths)); + buf.append(", heights=").append(StringUtil.toString(_heights)); + buf.append(", tileCounts=").append(StringUtil.toString(_tileCounts)); + buf.append(", offsetPos=").append(StringUtil.toString(_offsetPos)); + buf.append(", gapSize=").append(StringUtil.toString(_gapSize)); + } + + // documentation inherited + protected Image extractTileImage (int tileId) + { + BufferedImage tsimg = getTilesetImage(); if (tsimg == null) { return null; } @@ -87,37 +119,45 @@ public class SwissArmyTileSet extends TileSet tx = _offsetPos.x; ty = _offsetPos.y; - while ((tcount += _tileCount[ridx]) < tileId + 1) { + while ((tcount += _tileCounts[ridx]) < tileId + 1) { // increment tile image position by row height and gap distance - ty += (_rowHeight[ridx++] + _gapDist.y); + ty += (_heights[ridx++] + _gapSize.height); } // determine the horizontal index of this tile in the row - int xidx = tileId - (tcount - _tileCount[ridx]); + int xidx = tileId - (tcount - _tileCounts[ridx]); // final image x-position is based on tile width and gap distance - tx += (xidx * (_rowWidth[ridx] + _gapDist.x)); + tx += (xidx * (_widths[ridx] + _gapSize.width)); // Log.info("Retrieving tile image [tileId=" + tileId + ", ridx=" + // ridx + ", xidx=" + xidx + ", tx=" + tx + // ", ty=" + ty + "]."); // crop the tile-sized image chunk from the full image - return _imgmgr.getImageCropped( - tsimg, tx, ty, _rowWidth[ridx], _rowHeight[ridx]); + return tsimg.getSubimage(tx, ty, _widths[ridx], _heights[ridx]); + } + + private void readObject (ObjectInputStream in) + throws IOException, ClassNotFoundException + { + in.defaultReadObject(); + + // compute our total tile count + computeTileCount(); } /** The number of tiles in each row. */ - protected int[] _tileCount; + protected int[] _tileCounts; /** The number of tiles in the tileset. */ protected int _numTiles; /** The width of the tiles in each row in pixels. */ - protected int[] _rowWidth; + protected int[] _widths; /** The height of the tiles in each row in pixels. */ - protected int[] _rowHeight; + protected int[] _heights; /** The offset distance (x, y) in pixels from the top-left of the * image to the start of the first tile image. */ @@ -125,5 +165,5 @@ public class SwissArmyTileSet extends TileSet /** The distance (x, y) in pixels between each tile in each row * horizontally, and between each row of tiles vertically. */ - protected Point _gapDist = new Point(); + protected Dimension _gapSize = new Dimension(); } diff --git a/src/java/com/threerings/media/tile/Tile.java b/src/java/com/threerings/media/tile/Tile.java index 767b11217..8135a2a15 100644 --- a/src/java/com/threerings/media/tile/Tile.java +++ b/src/java/com/threerings/media/tile/Tile.java @@ -1,65 +1,62 @@ // -// $Id: Tile.java,v 1.17 2001/11/01 01:40:42 shaper Exp $ +// $Id: Tile.java,v 1.18 2001/11/18 04:09:21 mdb Exp $ package com.threerings.media.tile; -import java.awt.*; +import java.awt.Image; +import java.awt.Graphics2D; +import java.awt.Shape; +import java.awt.Rectangle; /** * A tile represents a single square in a single layer in a scene. */ public class Tile { - /** The tile image. */ - public Image img; - - /** The tile set identifier. */ - public short tsid; - - /** The tile identifier within the set. */ - public short tid; - - /** The tile width in pixels. */ - public short width; - - /** The tile height in pixels. */ - public short height; - /** - * Construct a new tile with the specified identifiers. Intended - * only for use by the TileSet. Do not call this - * method. - * - * @see TileSet#getTile + * Constructs a tile with the specified image. */ - public Tile (int tsid, int tid) + public Tile (Image image) { - this.tsid = (short) tsid; - this.tid = (short) tid; + _image = image; } /** - * Returns the fully qualified tile id for this tile. The fully - * qualified id contains both the tile set identifier and the tile - * identifier. + * Returns the width of this tile. */ - public int getTileId () + public int getWidth () { - return ((int)tsid << 16) | tid; + return _image.getWidth(null); } /** - * Render the tile image at the top-left corner of the given shape - * in the given graphics context. + * Returns the height of this tile. + */ + public int getHeight () + { + return _image.getHeight(null); + } + + /** + * Returns this tile's image. + */ + public Image getImage () + { + return _image; + } + + /** + * Render the tile image at the top-left corner of the given shape in + * the given graphics context. */ public void paint (Graphics2D gfx, Shape dest) { Rectangle bounds = dest.getBounds(); - gfx.drawImage(img, bounds.x, bounds.y, null); + gfx.drawImage(_image, bounds.x, bounds.y, null); } /** - * Return a string representation of the tile information. + * Return a string representation of this tile. */ public String toString () { @@ -76,7 +73,9 @@ public class Tile */ public void toString (StringBuffer buf) { - buf.append("tsid=").append(tsid); - buf.append(", tid=").append(tid); + buf.append("image=").append(_image); } + + /** Our tile image. */ + protected Image _image; } diff --git a/src/java/com/threerings/media/tile/TileLayer.java b/src/java/com/threerings/media/tile/TileLayer.java new file mode 100644 index 000000000..ec619ba68 --- /dev/null +++ b/src/java/com/threerings/media/tile/TileLayer.java @@ -0,0 +1,80 @@ +// +// $Id: TileLayer.java,v 1.1 2001/11/18 04:09:21 mdb Exp $ + +package com.threerings.media.tile; + +/** + * The tile layer class is a convenience class provided to simplify the + * management of a two-dimensional array of tiles. It takes care of + * dereferencing the tile array efficiently with methods that the Java + * compiler should inline, and prevents the caller from having to do the + * indexing multiplication by hand every time. + */ +public final class TileLayer +{ + /** + * Constructs a tile layer instance with the supplied tiles, width and + * height. The tiles should exist in row-major format (the first row + * of tiles followed by the second and so on). + * + * @exception IllegalArgumentException thrown if the size of the tiles + * array does not match the specified width and height. + */ + public TileLayer (Tile[] tiles, int width, int height) + { + // sanity check + if (tiles.length != width*height) { + String errmsg = "tiles.length != width*height"; + throw new IllegalArgumentException(errmsg); + } + + _tiles = tiles; + _width = width; + _height = height; + } + + /** + * Returns the width of the layer in tiles. + */ + public int getWidth () + { + return _width; + } + + /** + * Returns the height of the layer in tiles. + */ + public int getHeight () + { + return _height; + } + + /** + * Fetches the tile at the specified row and column. Bounds checking + * is not done. Note that the parameters are column first, followed by + * row (x, y order rather than row, column order). + */ + public Tile getTile (int column, int row) + { + return _tiles[row*_width+column]; + } + + /** + * Sets the tile at the specified row and column. Bounds checking is + * not done. Note that the parameters are column first, followed by + * row (x, y order rather than row, column order). + */ + public void setTile (int column, int row, Tile tile) + { + _tiles[row*_width+column] = tile; + } + + /** Our tiles array. */ + private Tile[] _tiles; + + /** The number of tiles in a row. */ + private int _width; + + /** The number of rows. */ + private int _height; +} diff --git a/src/java/com/threerings/media/tile/TileManager.java b/src/java/com/threerings/media/tile/TileManager.java index 57aa17c85..fc0721a8d 100644 --- a/src/java/com/threerings/media/tile/TileManager.java +++ b/src/java/com/threerings/media/tile/TileManager.java @@ -1,16 +1,17 @@ // -// $Id: TileManager.java,v 1.20 2001/11/08 03:04:44 mdb Exp $ +// $Id: TileManager.java,v 1.21 2001/11/18 04:09:21 mdb Exp $ package com.threerings.media.tile; -import java.awt.*; import java.awt.image.BufferedImage; -import java.util.ArrayList; +import java.io.IOException; +import javax.imageio.ImageIO; +import com.samskivert.io.PersistenceException; import com.samskivert.util.HashIntMap; +import com.threerings.resource.ResourceManager; import com.threerings.media.Log; -import com.threerings.media.ImageManager; /** * The tile manager provides a simplified interface for retrieving and @@ -37,16 +38,16 @@ import com.threerings.media.ImageManager; * The tile creation process is not hugely expensive, but does involve * extracting the tile image from the larger tileset image. */ -public class TileManager +public class TileManager implements ImageProvider { /** * Creates a tile manager and provides it with a reference to the - * image manager from which it will load its tileset images. + * resource manager from which it will load tileset image data. */ - public TileManager (ImageManager imgmgr) + public TileManager (ResourceManager rmgr) { // keep this guy around for later - _imgmgr = imgmgr; + _rmgr = rmgr; } /** @@ -56,16 +57,22 @@ public class TileManager public TileSet loadTileSet ( String imgPath, int count, int width, int height) { - return new UniformTileSet(_imgmgr, imgPath, count, width, height); + UniformTileSet uts = new UniformTileSet(); + uts.setImageProvider(this); + uts.setImagePath(imgPath); + uts.setTileCount(count); + uts.setWidth(width); + uts.setHeight(height); + return uts; } /** * Sets the tileset repository that will be used by the tile manager * when tiles are requested by tileset id. */ - public void setTileSetRepository (TileSetRepository tsrepo) + public void setTileSetRepository (TileSetRepository setrep) { - _tsrepo = tsrepo; + _setrep = setrep; } /** @@ -73,54 +80,73 @@ public class TileManager */ public TileSetRepository getTileSetRepository () { - return _tsrepo; + return _setrep; } /** - * Returns the {@link Tile} object for the specified tileset and - * tile id, or null if an error occurred. + * Returns the tileset with the specified id. Tilesets are fetched + * from the tileset repository supplied via {@link + * #setTileSetRepository}, and are subsequently cached. * - * @param tsid the tileset id. - * @param tid the tile id. + * @param tileSetId the unique identifier for the desired tileset. * - * @return the tile object, or null if an error occurred. + * @exception NoSuchTileSetException thrown if no tileset exists with + * the specified id or if an underlying error occurs with the tileset + * repository's persistence mechanism. */ - public Tile getTile (int tsid, int tid) - throws NoSuchTileSetException, NoSuchTileException + public TileSet getTileSet (int tileSetId) + throws NoSuchTileSetException { // make sure we have a repository configured - if (_tsrepo == null) { - throw new NoSuchTileSetException(tsid); + if (_setrep == null) { + throw new NoSuchTileSetException(tileSetId); } - // the fully unique tile id is the conjoined tile set and tile id - int utid = (tsid << 16) | tid; + try { + TileSet set = (TileSet)_cache.get(tileSetId); + if (set == null) { + set = _setrep.getTileSet(tileSetId); + _cache.put(tileSetId, set); + } + return set; - // look the tile up in our hash - Tile tile = (Tile) _tiles.get(utid); - if (tile != null) { - // Log.info("Retrieved tile from cache [tsid=" + tsid + - // ", tid=" + tid + "]."); - return tile; - } - - // retrieve the tile from the tileset - tile = _tsrepo.getTileSet(tsid).getTile(tid); - if (tile != null) { - // Log.info("Loaded tile into cache [tsid=" + tsid + - // ", tid=" + tid + "]."); - _tiles.put(utid, tile); - } - - return tile; + } catch (PersistenceException pe) { + Log.warning("Unable to load tileset [id=" + tileSetId + + ", error=" + pe + "]."); + throw new NoSuchTileSetException(tileSetId); + } } - /** The entity through which we load images. */ - protected ImageManager _imgmgr; + /** + * Returns the {@link Tile} object from the specified tileset at the + * specified index. + * + * @param tileSetId the tileset id. + * @param tileIndex the index of the tile to be retrieved. + * + * @return the tile object. + */ + public Tile getTile (int tileSetId, int tileIndex) + throws NoSuchTileSetException, NoSuchTileException + { + TileSet set = getTileSet(tileSetId); + return set.getTile(tileIndex); + } - /** Cache of tiles that have been requested thus far. */ - protected HashIntMap _tiles = new HashIntMap(); + // documentation inherited + public BufferedImage loadImage (String path) + throws IOException + { + // load up the image data from the resource manager + return ImageIO.read(_rmgr.getResource(path)); + } + + /** The entity through which we load image data. */ + protected ResourceManager _rmgr; + + /** Cache of tilesets that have been requested thus far. */ + protected HashIntMap _cache = new HashIntMap(); /** The tile set repository. */ - protected TileSetRepository _tsrepo; + protected TileSetRepository _setrep; } diff --git a/src/java/com/threerings/media/tile/TileSet.java b/src/java/com/threerings/media/tile/TileSet.java index 4089de465..e021b049f 100644 --- a/src/java/com/threerings/media/tile/TileSet.java +++ b/src/java/com/threerings/media/tile/TileSet.java @@ -1,14 +1,15 @@ // -// $Id: TileSet.java,v 1.19 2001/11/08 03:04:44 mdb Exp $ +// $Id: TileSet.java,v 1.20 2001/11/18 04:09:21 mdb Exp $ package com.threerings.media.tile; import java.awt.Image; -import java.awt.Point; -import java.awt.image.*; +import java.awt.image.BufferedImage; + +import java.io.IOException; +import java.io.Serializable; import com.threerings.media.Log; -import com.threerings.media.ImageManager; /** * A tileset stores information on a single logical set of tiles. It @@ -21,35 +22,22 @@ import com.threerings.media.ImageManager; * the tile number, assuming the tile at the top-left of the image is tile * id zero and tiles are numbered, in ascending order, left to right, top * to bottom. + * + *

This class is serializable and will be serialized, so derived + * classes should be sure to mark non-persistent fields as + * transient. */ -public abstract class TileSet implements Cloneable +public abstract class TileSet + implements Cloneable, Serializable { /** - * Provides the basic information needed to load a tileset image to - * the tileset base class. - * - * @param imgmgr an image manager from which the tileset image can be - * loaded. - * @param imgPath the path to the tileset image. - * @param name the name of the tileset (optional, can be null). - * @param tsid the unique integer identifier of the tileset (optional, - * can be zero if the tileset is not to be loaded by id). + * Configures this tileset with an image provider that it can use to + * load its tileset image. This will be called automatically when the + * tileset is fetched via the {@link TileManager}. */ - public TileSet (ImageManager imgmgr, String imgPath, - String name, int tsid) + public void setImageProvider (ImageProvider improv) { - _imgmgr = imgmgr; - _imgPath = imgPath; - _name = name; - _tsid = tsid; - } - - /** - * Returns the tileset identifier. - */ - public int getId () - { - return _tsid; + _improv = improv; } /** @@ -60,76 +48,141 @@ public abstract class TileSet implements Cloneable return _name; } + /** + * Specifies the tileset name. + */ + public void setName (String name) + { + _name = name; + } + + /** + * Sets the path to the image that will be used by this tileset. This + * must be called before the first call to {@link #getTile}. + */ + public void setImagePath (String imagePath) + { + _imagePath = imagePath; + + // clear out any reference to a loaded image + _tilesetImg = null; + } + /** * Returns the number of tiles in the tileset. */ public abstract int getTileCount (); - /** - * Sets the image file to be used as the source for the tile - * images produced by this tileset. - */ - public void setImageFile (String imgPath) - { - _imgPath = imgPath; - _tilesetImg = null; - } - /** * Returns a new tileset that is a clone of this tileset with the - * image file updated to reference the given file name. Useful for + * image path updated to reference the given path. Useful for * configuring a single tileset and then generating additional * tilesets with new images with the same configuration. */ - public TileSet clone (String imgPath) + public TileSet clone (String imagePath) throws CloneNotSupportedException { TileSet dup = (TileSet)clone(); - dup.setImageFile(imgPath); + dup.setImagePath(imagePath); return dup; } /** - * Creates a @link Tile} object from this tileset corresponding to the - * specified tile id and returns that tile, or null if an error - * occurred. + * Creates a {@link Tile} object from this tileset corresponding to + * the specified tile id and returns that tile. A null tile will never + * be returned, but one with an error image may be returned if a + * problem occurs loading the underlying tileset image. * - * @param tileId the tile identifier. Tile identifiers start with zero - * as the upper left tile and increase by one as the tiles move left - * to right and top to bottom over the source image. + * @param tileIndex the index of the tile in the tileset. Tile indexes + * start with zero as the upper left tile and increase by one as the + * tiles move left to right and top to bottom over the source image. * - * @return the tile object, or null if an error occurred. + * @return the tile object. * - * @exception NoSuchTileException thrown if the specified tile id is - * out of range for this tileset. + * @exception NoSuchTileException thrown if the specified tile index + * is out of range for this tileset. */ - public Tile getTile (int tileId) + public Tile getTile (int tileIndex) throws NoSuchTileException { // bail if there's no such tile - if (tileId < 0 || tileId > (getTileCount() - 1)) { - throw new NoSuchTileException(tileId); + if (tileIndex < 0 || tileIndex >= getTileCount()) { + throw new NoSuchTileException(tileIndex); } - // create and populate the tile object - Tile tile = createTile(tileId); + // create and initialize the tile object + Tile tile = createTile(tileIndex, checkedGet(tileIndex)); + initTile(tile); + return tile; + } + + /** + * Returns the tile image at the specified index. In some cases, a + * tile object is not desired or required, and so this method can be + * used to fetch the image directly. A null tile image will never be + * returned, but an error image may be returned if a problem occurs + * loading the underlying tileset image. + * + * @param tileIndex the index of the image in the tileset. + * + * @return the tile image. + * + * @exception NoSuchTileException thrown if the specified tile index + * is out of range for this tileset. + */ + public Image getTileImage (int tileIndex) + throws NoSuchTileException + { + // bail if there's no such tile + if (tileIndex < 0 || tileIndex >= getTileCount()) { + throw new NoSuchTileException(tileIndex); + } // retrieve the tile image - tile.img = getTileImage(tile.tid); - if (tile.img == null) { - Log.warning("Null tile image [tile=" + tile + "]."); - return null; + return checkedGet(tileIndex); + } + + // used to ensure TileSet derivations adhere to the extractTileImage() + // policy of not returning null + private Image checkedGet (int tileIndex) + throws NoSuchTileException + { + Image image = extractTileImage(tileIndex); + if (image == null) { + String errmsg = "TileSet implementation violated return " + + "policy for TileSet.extractTileImage()."; + throw new RuntimeException(errmsg); } + return image; + } - // populate the tile's dimensions - BufferedImage bimg = (BufferedImage)tile.img; - tile.height = (short)bimg.getHeight(); - tile.width = (short)bimg.getWidth(); + /** + * Extracts the image corresponding to the specified tile from the + * tileset image. + * + * @param tileIndex the index of the tile to be retrieved. + * + * @return the tile image. This should not return null in cases of + * failure, but should instead call {@link #createErrorImage} to + * return a valid image. + */ + protected abstract Image extractTileImage (int tileIndex); - // allow sub-classes to fill in their tile information - populateTile(tile); - - return tile; + /** + * Creates a blank image to be used in failure situations. If {@link + * #extractTileImage} is unable to return the actual tile image + * (because the tileset image could not be loaded or for some other + * reason), it should not return null. Instead it should return an + * error image created with this method. + * + * @param width the width of the error image in pixels. + * @param height the height of the error image in pixels. + */ + protected Image createErrorImage (int width, int height) + { + // return a blank image for now + return new BufferedImage(width, height, + BufferedImage.TYPE_BYTE_INDEXED); } /** @@ -137,35 +190,25 @@ public abstract class TileSet implements Cloneable * tile-specific information. Derived classes can override this method * to create their own sub-class of {@link Tile}. * - * @param tileId the tile id for the new tile. + * @param tileIndex the index of the tile being created. + * @param image the tile image. * * @return the new tile object. */ - protected Tile createTile (int tileId) + protected Tile createTile (int tileIndex, Image image) { // construct a basic tile - return new Tile(_tsid, tileId); + return new Tile(image); } /** - * Returns the image corresponding to the specified tile within this - * tileset. + * Initializes the supplied tile. Derived classes can override this + * method to add in their own tile information, but should be sure to + * call super.initTile(). * - * @param tileId the index of the tile to be retrieved. - * - * @return the tile image. + * @param tile the tile to initialize. */ - protected abstract Image getTileImage (int tileId); - - /** - * Populates the given tile object with its detailed tile - * information. Derived classes can override this method to add - * in their own tile information, but should be sure to call - * super.populateTile(). - * - * @param tile the tile to populate. - */ - protected void populateTile (Tile tile) + protected void initTile (Tile tile) { // nothing for now } @@ -177,17 +220,21 @@ public abstract class TileSet implements Cloneable * @return the tileset image or null if an error occurred loading the * image. */ - protected Image getTilesetImage () + protected BufferedImage getTilesetImage () { // return it straight away if it's already loaded if (_tilesetImg != null) { return _tilesetImg; } - // load up the tileset image via the image manager - if ((_tilesetImg = _imgmgr.getImage(_imgPath)) == null) { + // load up the tileset image via the image provider + try { + _tilesetImg = _improv.loadImage(_imagePath); + + } catch (IOException ioe) { Log.warning("Failed to retrieve tileset image " + - "[tsid=" + _tsid + ", path=" + _imgPath + "]."); + "[name=" + _name + ", path=" + _imagePath + + ", error=" + ioe + "]."); } return _tilesetImg; @@ -211,25 +258,21 @@ public abstract class TileSet implements Cloneable protected void toString (StringBuffer buf) { buf.append("name=").append(_name); - buf.append(", tsid=").append(_tsid); - buf.append(", path=").append(_imgPath); + buf.append(", path=").append(_imagePath); buf.append(", tileCount=").append(getTileCount()); } - /** The default image manager for retrieving tile images. */ - protected ImageManager _imgmgr; + /** The entity from which we obtain our tile image. */ + protected transient ImageProvider _improv; /** The path to the file containing the tile images. */ - protected String _imgPath; + protected String _imagePath; /** The tileset name. */ protected String _name; - /** The tileset unique identifier. */ - protected int _tsid; - /** The image containing all tile images for this set. This is private * because it should be accessed via {@link #getTilesetImage} even by * derived classes. */ - private Image _tilesetImg; + private transient BufferedImage _tilesetImg; } diff --git a/src/java/com/threerings/media/tile/TileSetIDBroker.java b/src/java/com/threerings/media/tile/TileSetIDBroker.java new file mode 100644 index 000000000..665455ad4 --- /dev/null +++ b/src/java/com/threerings/media/tile/TileSetIDBroker.java @@ -0,0 +1,32 @@ +// +// $Id: TileSetIDBroker.java,v 1.1 2001/11/18 04:09:21 mdb Exp $ + +package com.threerings.media.tile; + +import com.samskivert.io.PersistenceException; + +/** + * Brokers tileset ids. The tileset repository interface makes available a + * collection of tilesets based on a unique identifier. The expectation is + * that a collection of tilesets will be used to populate a repository and + * in that population process, tileset ids will be assigned to the + * tilesets. The tileset id broker system provides a means by which named + * tilesets can be mapped consistently to a set of tileset ids. Humans can + * then be responsible for assigning unique names to the tilesets and the + * broker will ensure that those names map to unique ids that won't change + * if the repository is rebuilt from the source tilesets. + */ +public interface TileSetIDBroker +{ + /** + * Returns the unique identifier for the named tileset. If no + * identifier has yet been assigned to the specified named tileset, + * one should be assigned and returned. + * + * @exception PersistenceException thrown if an error occurs + * communicating with the underlying persistence mechanism used to + * store the name to id mappings. + */ + public int getTileSetID (String tileSetName) + throws PersistenceException; +} diff --git a/src/java/com/threerings/media/tile/TileSetParser.java b/src/java/com/threerings/media/tile/TileSetParser.java deleted file mode 100644 index f9eb73d8b..000000000 --- a/src/java/com/threerings/media/tile/TileSetParser.java +++ /dev/null @@ -1,23 +0,0 @@ -// -// $Id: TileSetParser.java,v 1.8 2001/11/01 01:40:42 shaper Exp $ - -package com.threerings.media.tile; - -import java.io.IOException; - -import com.samskivert.util.HashIntMap; - -/** - * The tile set parser interface is intended to be implemented by - * classes that load tileset descriptions from a file. - */ -public interface TileSetParser -{ - /** - * Reads tileset description data from the specified file and - * populates the given hashtable with {@link TileSet} objects - * keyed on their tile set id. - */ - public void loadTileSets (String fname, HashIntMap tilesets) - throws IOException; -} diff --git a/src/java/com/threerings/media/tile/TileSetRepository.java b/src/java/com/threerings/media/tile/TileSetRepository.java index 78954f416..507ce0806 100644 --- a/src/java/com/threerings/media/tile/TileSetRepository.java +++ b/src/java/com/threerings/media/tile/TileSetRepository.java @@ -1,34 +1,33 @@ // -// $Id: TileSetRepository.java,v 1.1 2001/11/01 01:40:42 shaper Exp $ +// $Id: TileSetRepository.java,v 1.2 2001/11/18 04:09:21 mdb Exp $ package com.threerings.media.tile; import java.util.Iterator; - -import com.samskivert.util.Config; -import com.threerings.media.ImageManager; +import com.samskivert.io.PersistenceException; /** - * The tile set repository interface should be implemented by classes - * that provide access to tile sets keyed on their unique tile set - * identifier. + * The tileset repository interface should be implemented by classes that + * provide access to tilesets keyed on a unique tileset identifier. The + * tileset id space is up to the repository implementation, which may or + * may not desire to use a {@link TileSetIDBroker} to manage the space. */ -public interface TileSetRepository { - - /** - * Initializes the tile set repository. - */ - public void init (Config config, ImageManager imgmgr); - +public interface TileSetRepository +{ /** * Returns an iterator over all {@link TileSet} objects available. */ - public Iterator enumerateTileSets (); + public Iterator enumerateTileSets () + throws PersistenceException; /** - * Returns the {@link TileSet} with the specified unique tile set - * identifier. + * Returns the {@link TileSet} with the specified tile set identifier. + * + * @exception NoSuchTileSetException thrown if no tileset exists with + * the specified identifier. + * @exception PersistenceException thrown if an error occurs + * communicating with the underlying persistence mechanism. */ - public TileSet getTileSet (int tsid) - throws NoSuchTileSetException; + public TileSet getTileSet (int tileSetId) + throws NoSuchTileSetException, PersistenceException; } diff --git a/src/java/com/threerings/media/tile/TileUtil.java b/src/java/com/threerings/media/tile/TileUtil.java index e5165a862..65ddd17cf 100644 --- a/src/java/com/threerings/media/tile/TileUtil.java +++ b/src/java/com/threerings/media/tile/TileUtil.java @@ -1,5 +1,5 @@ // -// $Id: TileUtil.java,v 1.1 2001/10/11 00:41:26 shaper Exp $ +// $Id: TileUtil.java,v 1.2 2001/11/18 04:09:21 mdb Exp $ package com.threerings.media.tile; @@ -14,33 +14,36 @@ public class TileUtil { /** * Returns the image associated with the given tile from the given - * tile manager, or null if an error occurred. Any exceptions - * that occur are logged. + * tile manager, or null if an error occurred. Any exceptions that + * occur are logged. + * + * @param tilemgr the tile manager via which the tile should be + * fetched. + * @param tileSetId the id of the tileset from which to fetch the + * tile. + * @param tileIndex the index of the tile to be fetched. */ - public static Image getTileImage (TileManager tilemgr, int tsid, int tid) + public static Image getTileImage ( + TileManager tilemgr, int tileSetId, int tileIndex) { try { - Tile tile = tilemgr.getTile(tsid, tid); - return tile.img; + TileSet set = tilemgr.getTileSet(tileSetId); + return set.getTileImage(tileIndex); } catch (TileException te) { - Log.warning("Exception retrieving tile image [te=" + te + "]."); + Log.warning("Error retrieving tile image [error=" + te + "]."); return null; } } /** - * Returns the given tile from the given tile manager, or null if - * an error occurred. Any exceptions that occur are logged. + * Generates a fully-qualified tileid given the supplied tileset id + * and tile index. This fully-qualified id can be used to fetch the + * tile from the tileset repository which knows about the supplied + * tileset id. */ - public static Tile getTile (TileManager tilemgr, int tsid, int tid) + public static int getFQTileId (int tileSetId, int tileIndex) { - try { - return tilemgr.getTile(tsid, tid); - - } catch (TileException te) { - Log.warning("Exception retrieving tile [te=" + te + "]."); - return null; - } + return (tileSetId << 16) | tileIndex; } } diff --git a/src/java/com/threerings/media/tile/UniformTileSet.java b/src/java/com/threerings/media/tile/UniformTileSet.java index f9f30605d..6ddc261e7 100644 --- a/src/java/com/threerings/media/tile/UniformTileSet.java +++ b/src/java/com/threerings/media/tile/UniformTileSet.java @@ -1,9 +1,11 @@ // -// $Id: UniformTileSet.java,v 1.2 2001/11/08 06:58:57 mdb Exp $ +// $Id: UniformTileSet.java,v 1.3 2001/11/18 04:09:21 mdb Exp $ package com.threerings.media.tile; import java.awt.Image; +import java.awt.image.BufferedImage; + import com.threerings.media.Log; import com.threerings.media.ImageManager; @@ -16,39 +18,41 @@ import com.threerings.media.ImageManager; */ public class UniformTileSet extends TileSet { - /** - * Constructs a tile set object with the specified tileset - * configuration parameters. - * - * @param imgmgr the image manager via which to load the tileset - * image. - * @param imgPath the path to supply to the image manager when loading - * the tile (which will fetch the image using the resource manager). - * @param count the number of tiles in the tile image. - * @param width the width of each tile, in pixels. - * @param height the height of each tile, in pixels. - */ - public UniformTileSet (ImageManager imgmgr, String imgPath, - int count, int width, int height) - { - super(imgmgr, imgPath, null, 0); - - // keep these for later - _count = count; - _width = width; - _height = height; - } - // documentation inherited public int getTileCount () { return _count; } - // documentation inherited - protected Image getTileImage (int tileId) + /** + * Specifies the number of tiles that will be found in the tileset + * image managed by this tileset. + */ + public void setTileCount (int tileCount) { - Image tsimg = getTilesetImage(); + _count = tileCount; + } + + /** + * Specifies the width of the tiles in this tileset. + */ + public void setWidth (int width) + { + _width = width; + } + + /** + * Specifies the height of the tiles in this tileset. + */ + public void setHeight (int height) + { + _height = height; + } + + // documentation inherited + protected Image extractTileImage (int tileId) + { + BufferedImage tsimg = getTilesetImage(); if (tsimg == null) { return null; } @@ -59,8 +63,15 @@ public class UniformTileSet extends TileSet int col = tileId % tilesPerRow; // crop the tile-sized image chunk from the full image - return _imgmgr.getImageCropped( - tsimg, col * _width, row * _height, _width, _height); + return tsimg.getSubimage(_width*col, _height*row, _width, _height); + } + + // documentation inherited + protected void toString (StringBuffer buf) + { + super.toString(buf); + buf.append(", width=").append(_width); + buf.append(", height=").append(_height); } /** The total number of tiles in this tileset. */ diff --git a/src/java/com/threerings/media/tile/XMLTileSetParser.java b/src/java/com/threerings/media/tile/XMLTileSetParser.java deleted file mode 100644 index 11e77c23c..000000000 --- a/src/java/com/threerings/media/tile/XMLTileSetParser.java +++ /dev/null @@ -1,192 +0,0 @@ -// -// $Id: XMLTileSetParser.java,v 1.21 2001/11/08 03:04:44 mdb Exp $ - -package com.threerings.media.tile; - -import java.awt.Point; -import java.io.*; - -import org.xml.sax.*; - -import com.samskivert.util.*; -import com.samskivert.xml.SimpleParser; - -import com.threerings.media.Log; -import com.threerings.media.ImageManager; - -/** - * Parse an XML tileset description file and construct tileset objects - * for each valid description. Does not currently perform validation - * on the input XML stream, though the parsing code assumes the XML - * document is well-formed. - */ -public class XMLTileSetParser - extends SimpleParser - implements TileSetParser -{ - /** - * Constructs an xml tile set parser. - */ - public XMLTileSetParser (ImageManager imgmgr) - { - _imgmgr = imgmgr; - } - - // documentation inherited - public void startElement ( - String uri, String localName, String qName, Attributes attributes) - { - if (qName.equals("tileset")) { - // note whether the tile set contains object tiles - String str = attributes.getValue("layer"); - _info.isObjectSet = - (str != null && str.toLowerCase().equals(LAYER_OBJECT)); - - // get the tile set id - _info.tsid = parseInt(attributes.getValue("tsid")); - - // get the tile set name - str = attributes.getValue("name"); - _info.name = (str == null) ? DEF_NAME : str; - - } else if (qName.equals("object")) { - // get the object info - int tid = parseInt(attributes.getValue("tid")); - int wid = parseInt(attributes.getValue("width")); - int hei = parseInt(attributes.getValue("height")); - - if (_info.objects == null) { - _info.objects = new HashIntMap(); - } - _info.objects.put(tid, new int[] { wid, hei }); - } - } - - // documentation inherited - protected void finishElement ( - String uri, String localName, String qName, String data) - { - if (qName.equals("imagefile")) { - _info.imgFile = data; - - } else if (qName.equals("rowwidth")) { - _info.rowWidth = StringUtil.parseIntArray(data); - - } else if (qName.equals("rowheight")) { - _info.rowHeight = StringUtil.parseIntArray(data); - - } else if (qName.equals("tilecount")) { - _info.tileCount = StringUtil.parseIntArray(data); - - } else if (qName.equals("offsetpos")) { - parsePoint(data, _info.offsetPos); - - } else if (qName.equals("gapdist")) { - parsePoint(data, _info.gapDist); - - } else if (qName.equals("tileset")) { - // construct the tile set - TileSet tset = createTileSet(); - Log.info("Parsed tileset [tset=" + tset + "]."); - // clear the tile set info gathered while parsing - _info = new TileSetInfo(); - // add the tileset to the hashtable - _tilesets.put(tset.getId(), tset); - } - } - - // documentation inherited - public void loadTileSets (String fname, HashIntMap tilesets) - throws IOException - { - // save off the tileset hashtable - _tilesets = tilesets; - - try { - parseFile(fname); - } catch (IOException ioe) { - Log.warning("Exception parsing tile set descriptions " + - "[ioe=" + ioe + "]."); - Log.logStackTrace(ioe); - } - } - - // documentation inherited - protected InputStream getInputStream (String fname) throws IOException - { - InputStream is = ConfigUtil.getStream(fname); - if (is == null) { - String errmsg = "Can't load tileset description file from " + - "classpath [path=" + fname + "]."; - throw new FileNotFoundException(errmsg); - } - return is; - } - - /** - * Constructs and returns a new tile set object. Derived classes - * may override this method to create their own sub-classes of the - * TileSet object. - */ - protected TileSet createTileSet () - { - if (_info.isObjectSet) { - return new ObjectTileSet( - _imgmgr, _info.imgFile, _info.name, _info.tsid, - _info.tileCount, _info.rowWidth, _info.rowHeight, - _info.offsetPos, _info.gapDist, _info.objects); - - } else { - return new SwissArmyTileSet( - _imgmgr, _info.imgFile, _info.name, _info.tsid, - _info.tileCount, _info.rowWidth, _info.rowHeight, - _info.offsetPos, _info.gapDist); - } - } - - /** - * Converts a string containing values as (x, y) into the - * corresponding integer values and populates the given point - * object. - * - * @param str the point values in string format. - * @param point the point object to populate. - */ - protected void parsePoint (String str, Point point) - { - int vals[] = StringUtil.parseIntArray(str); - point.setLocation(vals[0], vals[1]); - } - - /** - * A class to hold the tile set information gathered while - * parsing. See the {@link TileSet} class for documentation on - * the various parameters. - */ - protected static class TileSetInfo - { - public int tsid; - public String name; - public String imgFile; - public int tileCount[], rowWidth[], rowHeight[]; - public Point offsetPos = new Point(); - public Point gapDist = new Point(); - public boolean isObjectSet; - public HashIntMap objects; - } - - /** 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 tilesets constructed thus far. */ - protected HashIntMap _tilesets; - - /** The tile set info populated while parsing. */ - protected TileSetInfo _info = new TileSetInfo(); - - /** The image manager. */ - protected ImageManager _imgmgr; -} diff --git a/src/java/com/threerings/media/tile/bundle/TileSetBundle.java b/src/java/com/threerings/media/tile/bundle/TileSetBundle.java new file mode 100644 index 000000000..616a2c999 --- /dev/null +++ b/src/java/com/threerings/media/tile/bundle/TileSetBundle.java @@ -0,0 +1,98 @@ +// +// $Id: TileSetBundle.java,v 1.1 2001/11/18 04:09:21 mdb Exp $ + +package com.threerings.media.tile.bundle; + +import java.awt.image.BufferedImage; +import javax.imageio.ImageIO; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +import java.util.Iterator; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import com.samskivert.util.HashIntMap; + +import com.threerings.media.tile.TileSet; +import com.threerings.media.tile.ImageProvider; + +/** + * A tileset bundle is used to load up tilesets by id from a persistent + * bundle of tilesets stored on the local filesystem. + */ +public class TileSetBundle + extends HashIntMap + implements Serializable, ImageProvider +{ + /** + * Initializes this resource bundle with a reference to the jarfile + * from which it was loaded and from which it can load image data. + */ + public void init (JarFile bundle) + { + _bundle = bundle; + } + + /** + * Adds a tileset to this tileset bundle. + */ + public final void addTileSet (int tileSetId, TileSet set) + { + put(tileSetId, set); + } + + /** + * Retrieves a tileset from this tileset bundle. + */ + public final TileSet getTileSet (int tileSetId) + { + return (TileSet)get(tileSetId); + } + + // documentation inherited + public BufferedImage loadImage (String path) + throws IOException + { + // obtain the image data from our jarfile + JarEntry entry = _bundle.getJarEntry(path); + if (entry == null) { + String errmsg = "Cannot load image resource from bundle " + + "[bundle=" + _bundle + ", path=" + path + "]."; + throw new FileNotFoundException(errmsg); + } + return ImageIO.read(_bundle.getInputStream(entry)); + } + + private void writeObject (ObjectOutputStream out) + throws IOException + { + out.writeInt(size()); + + Iterator entries = entrySet().iterator(); + while (entries.hasNext()) { + Entry entry = (Entry)entries.next(); + out.writeInt(((Integer)entry.getKey()).intValue()); + out.writeObject(entry.getValue()); + } + } + + private void readObject (ObjectInputStream in) + throws IOException, ClassNotFoundException + { + int count = in.readInt(); + + for (int i = 0; i < count; i++) { + int tileSetId = in.readInt(); + TileSet set = (TileSet)in.readObject(); + put(tileSetId, set); + } + } + + /** Our resource bundle. */ + protected JarFile _bundle; + } diff --git a/src/java/com/threerings/media/tile/tools/xml/SwissArmyTileSetRuleSet.java b/src/java/com/threerings/media/tile/tools/xml/SwissArmyTileSetRuleSet.java new file mode 100644 index 000000000..906c36144 --- /dev/null +++ b/src/java/com/threerings/media/tile/tools/xml/SwissArmyTileSetRuleSet.java @@ -0,0 +1,105 @@ +// +// $Id: SwissArmyTileSetRuleSet.java,v 1.1 2001/11/18 04:09:22 mdb Exp $ + +package com.threerings.media.tile.xml; + +import java.awt.Point; + +import org.xml.sax.Attributes; +import org.apache.commons.digester.Digester; + +import com.samskivert.util.StringUtil; +import com.samskivert.xml.CallMethodSpecialRule; + +import com.threerings.media.tile.TileSet; +import com.threerings.media.tile.SwissArmyTileSet; + +/** + * Parses {@link SwissArmyTileSet} instances from a tileset description. A + * uniform tileset description looks like so: + * + *

+ * <tileset name="Sample Swiss Army Tileset">
+ *   <imgpath>path/to/image.png</imgpath>
+ *   <!-- the widths (per row) of each tile in pixels -->
+ *   <widths>64, 64, 64, 64</widths>
+ *   <!-- the heights (per row) of each tile in pixels -->
+ *   <heights>48, 48, 48, 64</heights>
+ *   <!-- the number of tiles in each row -->
+ *   <tileCounts>16, 5, 3, 10</tileCounts>
+ *   <!-- the offset in pixels to the upper left tile -->
+ *   <offset>8, 8</offset>
+ *   <!-- the gap between tiles in pixels -->
+ *   <gap>12, 12</gap>
+ * </tileset>
+ * 
+ */ +public class SwissArmyTileSetRuleSet extends TileSetRuleSet +{ + /** + * Constructs a uniform tileset rule set that will match tilesets with + * the specified prefix. See the documentation for {@link + * TileSetRuleSet#TileSetruleSet} for more info on matching. + */ + public SwissArmyTileSetRuleSet (String prefix) + { + super(prefix); + } + + // documentation inherited + public void addRuleInstances (Digester digester) + { + super.addRuleInstances(digester); + + digester.addRule( + _prefix + "/tileset/widths", + new CallMethodSpecialRule(digester) { + public void parseAndSet (String bodyText, Object target) + { + int[] widths = StringUtil.parseIntArray(bodyText); + ((SwissArmyTileSet)target).setWidths(widths); + } + }); + + digester.addRule( + _prefix + "/tileset/heights", + new CallMethodSpecialRule(digester) { + public void parseAndSet (String bodyText, Object target) + { + int[] heights = StringUtil.parseIntArray(bodyText); + ((SwissArmyTileSet)target).setHeights(heights); + } + }); + + digester.addRule( + _prefix + "/tileset/tileCounts", + new CallMethodSpecialRule(digester) { + public void parseAndSet (String bodyText, Object target) + { + int[] tileCounts = StringUtil.parseIntArray(bodyText); + ((SwissArmyTileSet)target).setTileCounts(tileCounts); + } + }); + } + + // documentation inherited + protected TileSet createTileSet (Attributes attributes) + { + // we use uniform tilesets + return new SwissArmyTileSet(); + } + + /** + * Converts a string containing values as (x, y) into the + * corresponding integer values and populates the given point + * object. + * + * @param str the point values in string format. + * @param point the point object to populate. + */ + protected void parsePoint (String str, Point point) + { + int vals[] = StringUtil.parseIntArray(str); + point.setLocation(vals[0], vals[1]); + } +} diff --git a/src/java/com/threerings/media/tile/tools/xml/TileSetRuleSet.java b/src/java/com/threerings/media/tile/tools/xml/TileSetRuleSet.java new file mode 100644 index 000000000..7ca1b8662 --- /dev/null +++ b/src/java/com/threerings/media/tile/tools/xml/TileSetRuleSet.java @@ -0,0 +1,120 @@ +// +// $Id: TileSetRuleSet.java,v 1.1 2001/11/18 04:09:22 mdb Exp $ + +package com.threerings.media.tile.xml; + +import org.xml.sax.Attributes; + +import org.apache.commons.digester.Digester; +import org.apache.commons.digester.Rule; +import org.apache.commons.digester.RuleSetBase; + +import com.threerings.media.tile.TileSet; + +/** + * The tileset rule set is used to parse the base attributes of a tileset + * instance. Derived classes would extend this and add rules for their own + * special tilesets. + */ +public abstract class TileSetRuleSet extends RuleSetBase +{ + /** + * Constructs a tileset rule set which will match tilesets with the + * supplied prefix. For example, passing a prefix of + * tilesets.objectsets will match tilesets in the + * following XML file: + * + *
+     * <tilesets>
+     *   <objectsets>
+     *     <tileset>
+     *       // ...
+     *     </tileset>
+     *   </objectsets>
+     * </tilesets>
+     * 
+ */ + public TileSetRuleSet (String prefix) + { + _prefix = prefix; + } + + /** + * Called by the {@link XMLTileSetParser} to initialize this tileset + * rule set with the necessary back references to operate properly. + */ + protected void init (XMLTileSetParser parser) + { + _parser = parser; + } + + /** + * Adds the necessary rules to the digester to parse our tilesets. + * Derived classes should override this method, being sure to call the + * superclass method and then adding their own rule instances (which + * should register themselves relative to the _prefix + * member). + */ + public void addRuleInstances (Digester digester) + { + // this creates the appropriate instance when we encounter a + // tag + digester.addRule(_prefix + "/tileset", + new TileSetCreateRule(digester)); + + // grab the name attribute from the tag + digester.addSetProperties(_prefix + "/tileset"); + + // grab the image path from an element + digester.addCallMethod( + _prefix + "/tileset/imagePath", "setImagePath", 0); + } + + /** + * When a <tileset> element is encountered, this method is + * called to create a new instance of {@link TileSet}. Though the + * attributes are supplied (in case an attribute is needed to + * determine which derived instance of {@link TileSet} to create, this + * method should not configure the created tileset object. It should + * instead rely on the set properties rule that will be executed after + * this object is created or to custom set property rules registered + * in {@link #addDigesterRules}. + */ + protected abstract TileSet createTileSet (Attributes attributes); + + /** + * Used to process a <tileset> element. + */ + protected class TileSetCreateRule extends Rule + { + public TileSetCreateRule (Digester digester) + { + super(digester); + } + + public void begin (Attributes attributes) + throws Exception + { + // pass the torch to the XML parser to create the tileset + TileSet set = createTileSet(attributes); + // then push it onto the stack + digester.push(set); + } + + public void end () + throws Exception + { + // pop the tileset off of the stack + TileSet set = (TileSet)digester.pop(); + // and stick it into our tileset map + _parser._tilesets.put(set.getName(), set); + } + } + + /** The prefix at which me match our tilesets. */ + protected String _prefix; + + /** A reference to the XMLTileSetParser on whose behalf we are + * parsing. */ + protected XMLTileSetParser _parser; +} diff --git a/src/java/com/threerings/media/tile/tools/xml/UniformTileSetRuleSet.java b/src/java/com/threerings/media/tile/tools/xml/UniformTileSetRuleSet.java new file mode 100644 index 000000000..803804a16 --- /dev/null +++ b/src/java/com/threerings/media/tile/tools/xml/UniformTileSetRuleSet.java @@ -0,0 +1,62 @@ +// +// $Id: UniformTileSetRuleSet.java,v 1.1 2001/11/18 04:09:22 mdb Exp $ + +package com.threerings.media.tile.xml; + +import org.xml.sax.Attributes; +import org.apache.commons.digester.Digester; + +import com.threerings.media.tile.TileSet; +import com.threerings.media.tile.UniformTileSet; + +/** + * Parses {@link UniformTileSet} instances from a tileset description. A + * uniform tileset description looks like so: + * + *
+ * <tileset name="Sample Uniform Tileset">
+ *   <imgpath>path/to/image.png</imgpath>
+ *   <!-- the width of each tile in pixels -->
+ *   <width>64</width>
+ *   <!-- the height of each tile in pixels -->
+ *   <height>48</height>
+ *   <!-- the total number of tiles in the set -->
+ *   <tileCount>16</tileCount>
+ * </tileset>
+ * 
+ */ +public class UniformTileSetRuleSet extends TileSetRuleSet +{ + /** + * Constructs a uniform tileset rule set that will match tilesets with + * the specified prefix. See the documentation for {@link + * TileSetRuleSet#TileSetruleSet} for more info on matching. + */ + public UniformTileSetRuleSet (String prefix) + { + super(prefix); + } + + // documentation inherited + public void addRuleInstances (Digester digester) + { + super.addRuleInstances(digester); + + digester.addCallMethod( + _prefix + "/tileset/width", "setWidth", 0, + new Class[] { java.lang.Integer.TYPE }); + digester.addCallMethod( + _prefix + "/tileset/height", "setHeight", 0, + new Class[] { java.lang.Integer.TYPE }); + digester.addCallMethod( + _prefix + "/tileset/tileCount", "setTileCount", 0, + new Class[] { java.lang.Integer.TYPE }); + } + + // documentation inherited + protected TileSet createTileSet (Attributes attributes) + { + // we use uniform tilesets + return new UniformTileSet(); + } +} diff --git a/src/java/com/threerings/media/tile/tools/xml/XMLTileSetParser.java b/src/java/com/threerings/media/tile/tools/xml/XMLTileSetParser.java new file mode 100644 index 000000000..c127be7a8 --- /dev/null +++ b/src/java/com/threerings/media/tile/tools/xml/XMLTileSetParser.java @@ -0,0 +1,100 @@ +// +// $Id: XMLTileSetParser.java,v 1.1 2001/11/18 04:09:22 mdb Exp $ + +package com.threerings.media.tile.xml; + +import java.io.IOException; +import java.io.InputStream; +import java.io.FileNotFoundException; + +import java.util.HashMap; + +import org.xml.sax.SAXException; + +import org.apache.commons.digester.Digester; +import org.apache.commons.digester.Rule; + +import com.samskivert.util.ConfigUtil; + +import com.threerings.media.Log; + +/** + * Parse an XML tileset description file and construct tileset objects + * for each valid description. Does not currently perform validation + * on the input XML stream, though the parsing code assumes the XML + * document is well-formed. + */ +public class XMLTileSetParser +{ + /** + * Constructs an xml tile set parser. + */ + public XMLTileSetParser () + { + // create our digester + _digester = new Digester(); + // _digester.setDebug(10); + } + + /** + * Adds a ruleset to be used when parsing tiles. This should be an + * instance of a class derived from {@link TileSetRuleSet} which was + * constructed to match a particular kind of tile at a particular + * point in the XML hierarchy. For example: + * + *
+     * _parser.addRuleSet(new UniformTileSetRuleSet("tilesets"));
+     * 
+ */ + public void addRuleSet (TileSetRuleSet ruleset) + { + // provide a reference to ourselves to the ruleset + ruleset.init(this); + + // and have it set itself up with the digester + _digester.addRuleSet(ruleset); + } + + /** + * Loads all of the tilesets specified in the supplied XML tileset + * description file and places them into the supplied hashmap indexed + * by tileset name. + */ + public void loadTileSets (String path, HashMap tilesets) + throws IOException + { + // save off the tileset hashtable + _tilesets = tilesets; + + // get an input stream for this XML file + InputStream is = ConfigUtil.getStream(path); + if (is == null) { + String errmsg = "Can't load tileset description file from " + + "classpath [path=" + path + "]."; + throw new FileNotFoundException(errmsg); + } + + Log.info("Loading from " + path + "."); + + // now fire up the digester to parse the stream + try { + _digester.parse(is); + } catch (SAXException saxe) { + Log.warning("Exception parsing tile set descriptions " + + "[error=" + saxe + "]."); + Log.logStackTrace(saxe); + } + } + + /** Our XML digester. */ + protected Digester _digester; + + /** The tilesets constructed thus far. */ + protected HashMap _tilesets; + + /** Default tileset name. */ + protected static final String DEF_NAME = "Untitled"; + + /** String constant denoting an object tile set. */ + protected static final String LAYER_OBJECT = "object"; +} diff --git a/src/java/com/threerings/miso/client/Cluster.java b/src/java/com/threerings/miso/client/Cluster.java deleted file mode 100644 index e44086ee4..000000000 --- a/src/java/com/threerings/miso/client/Cluster.java +++ /dev/null @@ -1,88 +0,0 @@ -// -// $Id: Cluster.java,v 1.3 2001/09/28 01:24:54 mdb Exp $ - -package com.threerings.miso.scene; - -import java.util.List; -import java.util.ArrayList; - -import com.samskivert.util.StringUtil; - -/** - * A Cluster is a gathering of Location objects - * that represent a logical grouping for the purposes of display and - * interaction in a scene. - * - *

This class is currently just a wrapper around an - * ArrayList, but the theory is that we may soon want - * more useful functionality encapsulated herein, and the - * Cluster nomenclature is useful in any case. - */ -public class Cluster -{ - /** - * Construct a Cluster object. - */ - public Cluster () - { - _locations = new ArrayList(); - } - - /** - * Add a location to the cluster. - * - * @param loc the location. - */ - public void add (Location loc) - { - _locations.add(loc); - } - - /** - * Return whether the cluster contains the given location. - * - * @param loc the location. - */ - public boolean contains (Location loc) - { - return _locations.contains(loc); - } - - /** - * Removes the location from the cluster if present, and returns true - * if the location was present, false if not. - * - * @param loc the location. - */ - public boolean remove (Location loc) - { - return _locations.remove(loc); - } - - /** - * Return the number of locations in the cluster. - */ - public int size () - { - return _locations.size(); - } - - /** - * Return the list of locations that the cluster is made up of. - */ - public List getLocations () - { - return _locations; - } - - /** - * Return a string representation of this object. - */ - public String toString () - { - return StringUtil.toString(_locations); - } - - /** The list of locations in this cluster. */ - protected ArrayList _locations; -} diff --git a/src/java/com/threerings/miso/client/DirtyItemList.java b/src/java/com/threerings/miso/client/DirtyItemList.java index d6a8f72d1..0ed7c5763 100644 --- a/src/java/com/threerings/miso/client/DirtyItemList.java +++ b/src/java/com/threerings/miso/client/DirtyItemList.java @@ -1,5 +1,5 @@ // -// $Id: DirtyItemList.java,v 1.5 2001/10/30 16:16:01 shaper Exp $ +// $Id: DirtyItemList.java,v 1.6 2001/11/18 04:09:22 mdb Exp $ package com.threerings.miso.scene; @@ -93,8 +93,8 @@ public class DirtyItemList extends ArrayList ly = ry = oy; if (obj instanceof ObjectTile) { ObjectTile tile = (ObjectTile)obj; - lx -= (tile.baseWidth - 1); - ry -= (tile.baseHeight - 1); + lx -= (tile.getBaseWidth() - 1); + ry -= (tile.getBaseHeight() - 1); } } diff --git a/src/java/com/threerings/miso/client/DisplayMisoScene.java b/src/java/com/threerings/miso/client/DisplayMisoScene.java new file mode 100644 index 000000000..9fc372a97 --- /dev/null +++ b/src/java/com/threerings/miso/client/DisplayMisoScene.java @@ -0,0 +1,35 @@ +// +// $Id: DisplayMisoScene.java,v 1.1 2001/11/18 04:09:22 mdb Exp $ + +package com.threerings.miso.scene; + +import com.threerings.media.tile.ObjectTileLayer; +import com.threerings.media.tile.TileLayer; +import com.threerings.miso.tile.MisoTileLayer; + +/** + * Makes available the information from the {@link MisoSceneModel} in a + * form that is amenable to actually displaying the scene in a user + * interface. As with all display scene implementations, the information + * provided is read-only and should never be modified by the caller. + */ +public interface DisplayMisoScene +{ + /** + * Returns the tiles that comprise the base layer of this scene. This + * layer is read-only and not to be modified. + */ + public MisoTileLayer getBaseLayer (); + + /** + * Returns the tiles that comprise the fringe layer of this scene. + * This layer is read-only and not to be modified. + */ + public TileLayer getFringeLayer (); + + /** + * Returns the tiles that comprise the object layer of this scene. + * This layer is read-only and not to be modified. + */ + public ObjectTileLayer getObjectLayer (); +} diff --git a/src/java/com/threerings/miso/client/DisplayMisoSceneImpl.java b/src/java/com/threerings/miso/client/DisplayMisoSceneImpl.java index c6a218c5d..d1be6cbeb 100644 --- a/src/java/com/threerings/miso/client/DisplayMisoSceneImpl.java +++ b/src/java/com/threerings/miso/client/DisplayMisoSceneImpl.java @@ -1,283 +1,121 @@ // -// $Id: DisplayMisoSceneImpl.java,v 1.43 2001/11/12 20:56:55 mdb Exp $ +// $Id: DisplayMisoSceneImpl.java,v 1.44 2001/11/18 04:09:22 mdb Exp $ package com.threerings.miso.scene; -import java.awt.Point; -import java.io.*; -import java.util.ArrayList; -import java.util.List; - -import com.samskivert.util.StringUtil; - -import com.threerings.media.tile.*; +import com.threerings.media.tile.NoSuchTileException; +import com.threerings.media.tile.NoSuchTileSetException; +import com.threerings.media.tile.ObjectTile; +import com.threerings.media.tile.ObjectTileLayer; +import com.threerings.media.tile.Tile; +import com.threerings.media.tile.TileLayer; +import com.threerings.media.tile.TileManager; import com.threerings.miso.Log; -import com.threerings.miso.scene.util.ClusterUtil; import com.threerings.miso.tile.MisoTile; +import com.threerings.miso.tile.MisoTileLayer; import com.threerings.miso.tile.ShadowTile; /** - * A scene object represents the data model corresponding to a single - * screen for game play. For instance, one scene might display a portion - * of a street with several buildings scattered about on the periphery. + * The default implementation of the {@link DisplayMisoScene} interface. */ -public class MisoSceneImpl implements EditableMisoScene +public class DisplayMisoSceneImpl + implements DisplayMisoScene { - /** The scene name. */ - public String name = DEF_SCENE_NAME; - - /** The locations within the scene. */ - public ArrayList locations = new ArrayList(); - - /** The clusters within the scene. */ - public ArrayList clusters = new ArrayList(); - - /** The portals to different scenes. */ - public ArrayList portals = new ArrayList(); - - /** The default entrance portal. */ - public Portal entrance; - - /** The base tiles in the scene. */ - public MisoTile[][] baseTiles; - - /** The fringe tiles in the scene. */ - public Tile[][] fringeTiles; - - /** The object tiles in the scene. */ - public ObjectTile[][] objectTiles; - - /** All tiles in the scene. */ - public Tile[][][] tiles; - /** - * Construct a new miso scene object. The base layer tiles are - * initialized to contain tiles of the specified default tileset and - * tile id. + * Constructs an instance that will be used to display the supplied + * miso scene data. The tiles identified by the scene model will be + * loaded via the supplied tile manager. * - * Note: Be sure to call {@link - * #generateAllObjectShadows} before the scene is first used so - * that the base layer will be properly populated with shadow - * tiles in the footprint of all object tiles. + * @param model the scene data that we'll be displaying. + * @param tmgr the tile manager from which to load our tiles. * - * @param model the iso scene view model. - * @param deftile the default tile. + * @exception NoSuchTileException thrown if the model references a + * tile which is not available via the supplied tile manager. */ - public MisoSceneImpl (IsoSceneViewModel model, MisoTile deftile) + public DisplayMisoSceneImpl (MisoSceneModel model, TileManager tmgr) + throws NoSuchTileException, NoSuchTileSetException { - _model = model; - _deftile = deftile; + int swid = model.width; + int shei = model.height; - // create the individual tile layer arrays - baseTiles = new MisoTile[_model.scenewid][_model.scenehei]; - fringeTiles = new Tile[_model.scenewid][_model.scenehei]; - objectTiles = new ObjectTile[_model.scenewid][_model.scenehei]; + // create the individual tile layer objects + _base = new MisoTileLayer(new MisoTile[swid*shei], swid, shei); + _fringe = new TileLayer(new Tile[swid*shei], swid, shei); + _object = new ObjectTileLayer(new ObjectTile[swid*shei], swid, shei); - // create the conjoined array for purely utilitarian purposes - tiles = new Tile[][][] { baseTiles, fringeTiles, objectTiles }; + // populate the base and fringe layers + for (int column = 0; column < shei; column++) { + for (int row = 0; row < swid; row++) { + // first do the base layer + int tsid = model.baseTileIds[swid*row+column]; + if (tsid > 0) { + int tid = (tsid & 0xFFFF); + tsid >>= 16; + // this is a bit magical, but the tile manager will + // fetch tiles from the tileset repository and the + // tile set id from which we request this tile must + // map to a miso tile as provided by the repository, + // so we just cast it to a miso tile and know that all + // is well + MisoTile mtile = (MisoTile)tmgr.getTile(tsid, tid); + _base.setTile(column, row, mtile); + } - // initialize the always-fully-populated base layer - initBaseTiles(); - } - - // documentation inherited - public String getName () - { - return name; - } - - // documentation inherited - public void setDefaultTile (MisoTile tile) - { - _deftile = tile; - } - - // documentation inherited - public void setTile (int lnum, int x, int y, Tile tile) - { - // if the tile being replaced is an object tile, clear out its - // shadow tiles - Tile otile = tiles[lnum][x][y]; - if (otile instanceof ObjectTile) { - removeObjectShadow(x, y); + // then the fringe layer + tsid = model.fringeTileIds[swid*row+column]; + if (tsid > 0) { + int tid = (tsid & 0xFFFF); + tsid >>= 16; + Tile tile = tmgr.getTile(tsid, tid); + _fringe.setTile(column, row, tile); + } + } } - // place the new tile - tiles[lnum][x][y] = tile; + // sanity check the object layer info + int ocount = model.objectTileIds.length; + if (ocount % 3 != 0) { + throw new IllegalArgumentException( + "model.objectTileIds.length % 3 != 0"); + } - // if the tile being placed is an object tile, create the - // shadow tiles that lie in its footprint - if (tile instanceof ObjectTile) { - generateObjectShadow(x, y); + // now populate the object layer + for (int i = 0; i < ocount; i+= 3) { + int row = model.objectTileIds[i]; + int col = model.objectTileIds[i+1]; + int tsid = model.objectTileIds[i+2]; + int tid = (tsid & 0xFFFF); + tsid >>= 16; + + // create the object tile and stick it into the appropriate + // spot in the object layer + ObjectTile otile = (ObjectTile)tmgr.getTile(tsid, tid); + _object.setTile(col, row, otile); + + // we have to generate a shadow for this object tile in the + // base layer so that we can prevent sprites from walking on + // the object + generateObjectShadow(row, col); } } // documentation inherited - public int getId () + public MisoTileLayer getBaseLayer () { - return _sid; + return _base; } // documentation inherited - public int getVersion () + public TileLayer getFringeLayer () { - return _version; + return _fringe; } // documentation inherited - public int[] getNeighborIds () + public ObjectTileLayer getObjectLayer () { - return null; - } - - // documentation inherited - public Tile[][][] getTiles () - { - return tiles; - } - - // documentation inherited - public Tile[][] getTiles (int lnum) - { - return tiles[lnum]; - } - - // documentation inherited - public MisoTile[][] getBaseLayer () - { - return baseTiles; - } - - // documentation inherited - public Tile[][] getFringeLayer () - { - return fringeTiles; - } - - // documentation inherited - public ObjectTile[][] getObjectLayer () - { - return objectTiles; - } - - // documentation inherited - public MisoTile getDefaultTile () - { - return _deftile; - } - - // documentation inherited - public List getLocations () - { - return locations; - } - - // documentation inherited - public List getClusters () - { - return clusters; - } - - // documentation inherited - public List getPortals () - { - return portals; - } - - // documentation inherited - public Portal getEntrance () - { - return entrance; - } - - // documentation inherited - public void setId (int sceneId) - { - _sid = sceneId; - } - - // documentation inherited - public void setVersion (int version) - { - _version = version; - } - - // documentation inherited - public void setName (String name) - { - this.name = name; - } - - // documentation inherited - public void setEntrance (Portal entrance) - { - this.entrance = entrance; - } - - /** - * Update the specified location in the scene. If the cluster - * index number is -1, the location will be removed from any - * cluster it may reside in. - * - * @param loc the location. - * @param clusteridx the cluster index number. - */ - public void updateLocation (Location loc, int clusteridx) - { - // add the location if it's not already present - if (!locations.contains(loc)) { - locations.add(loc); - } - - // update the cluster contents - ClusterUtil.regroup(clusters, loc, clusteridx); - } - - /** - * Add the specified portal to the scene. Adds the portal to the - * location list as well if it's not already present and removes - * it from any cluster it may reside in. - * - * @param portal the portal. - */ - public void addPortal (Portal portal) - { - // make sure it's in the location list and absent from any cluster - updateLocation(portal, -1); - - // don't allow adding a portal more than once - if (portals.contains(portal)) { - Log.warning("Attempt to add already-existing portal " + - "[portal=" + portal + "]."); - return; - } - - // add it to the list - portals.add(portal); - } - - /** - * Remove the given location object from the location list, and from - * any containing cluster. If the location is a portal, it is removed - * from the portal list as well. - * - * @param loc the location object. - */ - public void removeLocation (Location loc) - { - // remove from the location list - if (!locations.remove(loc)) { - // we didn't know about it, so it can't be in a cluster or - // the portal list - return; - } - - // remove from any possible cluster - ClusterUtil.remove(clusters, loc); - - // remove from any possible existence on the portal list - portals.remove(loc); + return _object; } /** @@ -286,47 +124,15 @@ public class MisoSceneImpl implements EditableMisoScene public String toString () { StringBuffer buf = new StringBuffer(); - buf.append("[name=").append(name); - buf.append(", sid=").append(_sid); - buf.append(", locations=").append(StringUtil.toString(locations)); - buf.append(", clusters=").append(StringUtil.toString(clusters)); - buf.append(", portals=").append(StringUtil.toString(portals)); + buf.append("[width=").append(_base.getWidth()); + buf.append(", height=").append(_base.getHeight()); return buf.append("]").toString(); } /** - * Initialize the base tile layer with the default tile. - */ - protected void initBaseTiles () - { - for (int xx = 0; xx < _model.scenewid; xx++) { - for (int yy = 0; yy < _model.scenehei; yy++) { - baseTiles[xx][yy] = _deftile; - } - } - } - - /** - * Place shadow tiles in the footprint of all object tiles in the - * scene. This method should be called once the scene tiles are - * fully populated, but before the scene is used in any other - * meaningful capacity. - */ - public void generateAllObjectShadows () - { - for (int xx = 0; xx < _model.scenewid; xx++) { - for (int yy = 0; yy < _model.scenehei; yy++) { - if (objectTiles[xx][yy] != null) { - generateObjectShadow(xx, yy); - } - } - } - } - - /** - * Place shadow tiles in the footprint of the object tile at the - * given coordinates in the scene. This method should be called - * when an object tile is added to the scene. + * Place shadow tiles in the footprint of the object tile at the given + * coordinates in the scene. This method should be called when an + * object tile is added to the scene. * * @param x the tile x-coordinate. * @param y the tile y-coordinate. @@ -336,19 +142,6 @@ public class MisoSceneImpl implements EditableMisoScene setObjectTileFootprint(x, y, new ShadowTile(x, y)); } - /** - * Remove shadow tiles from the footprint of the object tile at - * the given coordinates in the scene. This method should be - * called when an object tile is removed from the scene. - * - * @param x the tile x-coordinate. - * @param y the tile y-coordinate. - */ - protected void removeObjectShadow (int x, int y) - { - setObjectTileFootprint(x, y, _deftile); - } - /** * Place the given tile in the footprint of the object tile at the * given coordinates in the scene. @@ -359,30 +152,23 @@ public class MisoSceneImpl implements EditableMisoScene */ protected void setObjectTileFootprint (int x, int y, MisoTile stamp) { - ObjectTile tile = objectTiles[x][y]; - - int endx = Math.max(0, (x - tile.baseWidth + 1)); - int endy = Math.max(0, (y - tile.baseHeight + 1)); + ObjectTile tile = _object.getTile(y, x); + int endx = Math.max(0, (x - tile.getBaseWidth() + 1)); + int endy = Math.max(0, (y - tile.getBaseHeight() + 1)); for (int xx = x; xx >= endx; xx--) { for (int yy = y; yy >= endy; yy--) { - baseTiles[xx][yy] = stamp; + _base.setTile(yy, xx, stamp); } } } - /** The default scene name. */ - protected static final String DEF_SCENE_NAME = "Untitled Scene"; + /** The base layer of tiles. */ + protected MisoTileLayer _base; - /** The unique scene id. */ - protected int _sid = SID_INVALID; + /** The fringe layer of tiles. */ + protected TileLayer _fringe; - /** The scene version. */ - protected int _version; - - /** The default tile for the base layer in the scene. */ - protected MisoTile _deftile; - - /** The iso scene view data model. */ - protected IsoSceneViewModel _model; + /** The object layer of tiles. */ + protected ObjectTileLayer _object; } diff --git a/src/java/com/threerings/miso/client/IsoSceneView.java b/src/java/com/threerings/miso/client/IsoSceneView.java index 6bd8e2105..8e99a923f 100644 --- a/src/java/com/threerings/miso/client/IsoSceneView.java +++ b/src/java/com/threerings/miso/client/IsoSceneView.java @@ -1,24 +1,40 @@ // -// $Id: IsoSceneView.java,v 1.71 2001/10/27 01:37:37 shaper Exp $ +// $Id: IsoSceneView.java,v 1.72 2001/11/18 04:09:22 mdb Exp $ package com.threerings.miso.scene; -import java.awt.*; -import java.awt.geom.*; -import java.awt.image.*; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Polygon; +import java.awt.Shape; +import java.awt.Stroke; +import java.awt.Rectangle; +import java.awt.FontMetrics; +import java.awt.Point; +import java.awt.Font; +import java.awt.BasicStroke; +import java.util.ArrayList; +import java.util.Iterator; import java.util.List; -import java.util.*; import com.samskivert.util.HashIntMap; -import com.threerings.media.sprite.*; -import com.threerings.media.tile.Tile; +import com.threerings.media.sprite.DirtyRectList; +import com.threerings.media.sprite.Path; +import com.threerings.media.sprite.SpriteManager; + import com.threerings.media.tile.ObjectTile; +import com.threerings.media.tile.ObjectTileLayer; +import com.threerings.media.tile.Tile; +import com.threerings.media.tile.TileLayer; import com.threerings.miso.Log; import com.threerings.miso.scene.DirtyItemList.DirtyItem; -import com.threerings.miso.scene.util.*; +import com.threerings.miso.scene.util.AStarPathUtil; +import com.threerings.miso.scene.util.IsoUtil; +import com.threerings.miso.tile.MisoTileLayer; /** * The iso scene view provides an isometric view of a particular @@ -54,7 +70,7 @@ public class IsoSceneView implements SceneView } // documentation inherited - public void setScene (MisoScene scene) + public void setScene (DisplayMisoScene scene) { _scene = scene; @@ -106,11 +122,6 @@ public class IsoSceneView implements SceneView _spritemgr.renderSpritePaths(gfx); } - // draw marks at each location - if (_model.showLocs) { - paintLocations(gfx); - } - // paint any extra goodies paintExtras(gfx); @@ -145,8 +156,7 @@ public class IsoSceneView implements SceneView { _dirtyRects.clear(); _dirtyItems.clear(); - - _numDirty = 0; + _numDirty = 0; for (int xx = 0; xx < _model.scenewid; xx++) { for (int yy = 0; yy < _model.scenehei; yy++) { _dirty[xx][yy] = false; @@ -207,25 +217,23 @@ public class IsoSceneView implements SceneView */ protected void renderTiles (Graphics2D gfx) { - Tile[][][] tiles = _scene.getTiles(); + MisoTileLayer base = _scene.getBaseLayer(); + TileLayer fringe = _scene.getFringeLayer(); // render the base and fringe layers - for (int yy = 0; yy < _model.scenehei; yy++) { - for (int xx = 0; xx < _model.scenewid; xx++) { - if (_dirty[xx][yy]) { - - // draw both layers at this tile position - for (int kk = MisoScene.LAYER_BASE; - kk <= MisoScene.LAYER_FRINGE; kk++) { - - // get the tile at these coordinates and layer - Tile tile = tiles[kk][xx][yy]; - if (tile != null) { - // draw the tile image - tile.paint(gfx, _polys[xx][yy]); - } - } + for (int yy = 0; yy < base.getHeight(); yy++) { + for (int xx = 0; xx < base.getWidth(); xx++) { + if (!_dirty[xx][yy]) { + continue; + } + // draw the base and fringe tile images + Tile tile; + if ((tile = base.getTile(yy, xx)) != null) { + tile.paint(gfx, _polys[xx][yy]); + } + if ((tile = fringe.getTile(yy, xx)) != null) { + tile.paint(gfx, _polys[xx][yy]); } } } @@ -251,8 +259,8 @@ public class IsoSceneView implements SceneView } /** - * Generates and stores bounding polygons for all object tiles in - * the scene for later use while rendering. + * Generates and stores bounding polygons for all object tiles in the + * scene for later use while rendering. */ protected void initAllObjectBounds () { @@ -260,10 +268,10 @@ public class IsoSceneView implements SceneView _objpolys.clear(); // generate bounding polygons for all objects - ObjectTile[][] tiles = _scene.getObjectLayer(); - for (int xx = 0; xx < _model.scenewid; xx++) { - for (int yy = 0; yy < _model.scenehei; yy++) { - ObjectTile tile = tiles[xx][yy]; + ObjectTileLayer tiles = _scene.getObjectLayer(); + for (int yy = 0; yy < tiles.getHeight(); yy++) { + for (int xx = 0; xx < tiles.getWidth(); xx++) { + ObjectTile tile = tiles.getTile(yy, xx); if (tile != null) { generateObjectBounds(tile, xx, yy); } @@ -343,47 +351,47 @@ public class IsoSceneView implements SceneView } } - /** - * Paint demarcations at all locations in the scene, with each - * location's cluster index, if any, along the right side of its - * rectangle. - * - * @param gfx the graphics context. - */ - protected void paintLocations (Graphics2D gfx) - { - List locations = _scene.getLocations(); - int size = locations.size(); +// /** +// * Paint demarcations at all locations in the scene, with each +// * location's cluster index, if any, along the right side of its +// * rectangle. +// * +// * @param gfx the graphics context. +// */ +// protected void paintLocations (Graphics2D gfx) +// { +// List locations = _scene.getLocations(); +// int size = locations.size(); - for (int ii = 0; ii < size; ii++) { - // retrieve the location - Location loc = (Location)locations.get(ii); +// for (int ii = 0; ii < size; ii++) { +// // retrieve the location +// Location loc = (Location)locations.get(ii); - // get the cluster index this location is in, if any - int clusteridx = MisoSceneUtil.getClusterIndex(_scene, loc); +// // get the cluster index this location is in, if any +// int clusteridx = MisoSceneUtil.getClusterIndex(_scene, loc); - // get the location's center coordinate - Point spos = new Point(); - IsoUtil.fullToScreen(_model, loc.x, loc.y, spos); - int cx = spos.x, cy = spos.y; +// // get the location's center coordinate +// Point spos = new Point(); +// IsoUtil.fullToScreen(_model, loc.x, loc.y, spos); +// int cx = spos.x, cy = spos.y; - // paint the location - loc.paint(gfx, cx, cy); +// // paint the location +// loc.paint(gfx, cx, cy); - if (clusteridx != -1) { - // draw the cluster index number on the right side - gfx.setFont(_font); - gfx.setColor(Color.white); - gfx.drawString(String.valueOf(clusteridx), cx + 5, cy + 3); - } +// if (clusteridx != -1) { +// // draw the cluster index number on the right side +// gfx.setFont(_font); +// gfx.setColor(Color.white); +// gfx.drawString(String.valueOf(clusteridx), cx + 5, cy + 3); +// } - // highlight the location if it's the default entrance - if (_scene.getEntrance() == loc) { - gfx.setColor(Color.cyan); - gfx.drawRect(spos.x - 5, spos.y - 5, 10, 10); - } - } - } +// // highlight the location if it's the default entrance +// if (_scene.getEntrance() == loc) { +// gfx.setColor(Color.cyan); +// gfx.drawRect(spos.x - 5, spos.y - 5, 10, 10); +// } +// } +// } // documentation inherited public void invalidateRects (DirtyRectList rects) @@ -555,7 +563,7 @@ public class IsoSceneView implements SceneView } // add any objects impacted by the dirty rectangle - ObjectTile tiles[][] = _scene.getObjectLayer(); + ObjectTileLayer tiles = _scene.getObjectLayer(); Iterator iter = _objpolys.keys(); while (iter.hasNext()) { // get the object's coordinates and bounding polygon @@ -563,13 +571,11 @@ public class IsoSceneView implements SceneView Polygon poly = (Polygon)_objpolys.get(coord); if (poly.intersects(r)) { - // get the dirty portion of the object Rectangle drect = poly.getBounds().intersection(r); - int tx = coord >> 16, ty = coord & 0x0000FFFF; _dirtyItems.appendDirtyObject( - tiles[tx][ty], poly, tx, ty, drect); + tiles.getTile(ty, tx), poly, tx, ty, drect); // Log.info("Dirtied item: Object(" + tx + ", " + // ty + ")"); } @@ -628,8 +634,8 @@ public class IsoSceneView implements SceneView /** The scene view model data. */ protected IsoSceneViewModel _model; - /** The scene object to be displayed. */ - protected MisoScene _scene; + /** The scene to be displayed. */ + protected DisplayMisoScene _scene; /** The sprite manager. */ protected SpriteManager _spritemgr; diff --git a/src/java/com/threerings/miso/client/IsoSceneViewModel.java b/src/java/com/threerings/miso/client/IsoSceneViewModel.java index 1a22f529d..d5f53ca73 100644 --- a/src/java/com/threerings/miso/client/IsoSceneViewModel.java +++ b/src/java/com/threerings/miso/client/IsoSceneViewModel.java @@ -1,10 +1,10 @@ // -// $Id: IsoSceneViewModel.java,v 1.17 2001/10/22 23:55:14 mdb Exp $ +// $Id: IsoSceneViewModel.java,v 1.18 2001/11/18 04:09:22 mdb Exp $ package com.threerings.miso.scene; -import java.awt.Rectangle; import java.awt.Point; +import java.awt.Rectangle; import java.util.ArrayList; import com.samskivert.util.Config; @@ -14,9 +14,8 @@ import com.threerings.miso.scene.util.IsoUtil; import com.threerings.miso.util.MisoUtil; /** - * The iso scene view model provides a holding place for the myriad - * parameters and bits of data that describe the details of an isometric - * view of a scene. + * Provides a holding place for the myriad parameters and bits of data + * that describe the details of an isometric view of a scene. * *

The member data are public to facilitate speedy referencing by the * {@link IsoSceneView} class. The model should only be modified through @@ -56,7 +55,7 @@ public class IsoSceneViewModel public float slopeX, slopeY; /** The x-axis line. */ - public Point lineX[]; + public Point[] lineX; /** The length between fine coordinates in pixels. */ public float finelen; @@ -70,9 +69,6 @@ public class IsoSceneViewModel /** Whether tile coordinates should be drawn. */ public boolean showCoords; - /** Whether locations in the scene should be drawn. */ - public boolean showLocs; - /** Whether sprite paths should be drawn. */ public boolean showPaths; @@ -106,7 +102,6 @@ public class IsoSceneViewModel // set our various flags showCoords = config.getValue(SHOW_COORDS_KEY, DEF_SHOW_COORDS); - showLocs = config.getValue(SHOW_COORDS_KEY, DEF_SHOW_COORDS); showPaths = config.getValue(SHOW_PATHS_KEY, DEF_SHOW_PATHS); } @@ -158,15 +153,6 @@ public class IsoSceneViewModel fy >= 0 && fy < finegran); } - /** - * Toggle whether locations in the scene should be drawn. - */ - public void toggleShowLocations () - { - showLocs = !showLocs; - notifyListeners(IsoSceneViewModelListener.SHOW_LOCATIONS_CHANGED); - } - /** * Toggle whether coordinates should be drawn for each tile. */ @@ -275,10 +261,6 @@ public class IsoSceneViewModel protected static final String SHOW_COORDS_KEY = MisoUtil.CONFIG_KEY + ".show_coords"; - /** The config key for whether to show locations. */ - protected static final String SHOW_LOCS_KEY = - MisoUtil.CONFIG_KEY + ".show_locs"; - /** The config key for whether to show sprite paths. */ protected static final String SHOW_PATHS_KEY = MisoUtil.CONFIG_KEY + ".show_paths"; @@ -293,7 +275,6 @@ public class IsoSceneViewModel protected static final int DEF_SCENE_HEIGHT = 22; protected static final int DEF_OFFSET_Y = -5; protected static final boolean DEF_SHOW_COORDS = false; - protected static final boolean DEF_SHOW_LOCS = false; protected static final boolean DEF_SHOW_PATHS = false; /** The model listeners. */ diff --git a/src/java/com/threerings/miso/client/IsoSceneViewModelListener.java b/src/java/com/threerings/miso/client/IsoSceneViewModelListener.java index 7d3c028e9..4bcf432d9 100644 --- a/src/java/com/threerings/miso/client/IsoSceneViewModelListener.java +++ b/src/java/com/threerings/miso/client/IsoSceneViewModelListener.java @@ -1,5 +1,5 @@ // -// $Id: IsoSceneViewModelListener.java,v 1.2 2001/10/26 01:40:22 mdb Exp $ +// $Id: IsoSceneViewModelListener.java,v 1.3 2001/11/18 04:09:22 mdb Exp $ package com.threerings.miso.scene; @@ -12,13 +12,13 @@ package com.threerings.miso.scene; */ public interface IsoSceneViewModelListener { + /** Notification event constant indicating that the "show coordinates" + * configuration has changed.. */ + public static final int SHOW_COORDINATES_CHANGED = 0; + /** * Called by the {@link com.threerings.miso.scene.IsoSceneView} when * the model is changed. */ public void viewChanged (int event); - - /** Notification event constants. */ - public static final int SHOW_LOCATIONS_CHANGED = 0; - public static final int SHOW_COORDINATES_CHANGED = 1; } diff --git a/src/java/com/threerings/miso/client/Location.java b/src/java/com/threerings/miso/client/Location.java deleted file mode 100644 index 2e5f55a31..000000000 --- a/src/java/com/threerings/miso/client/Location.java +++ /dev/null @@ -1,133 +0,0 @@ -// -// $Id: Location.java,v 1.8 2001/10/25 16:36:42 shaper Exp $ - -package com.threerings.miso.scene; - -import java.awt.Color; -import java.awt.Graphics2D; -import java.awt.Polygon; - -import com.threerings.media.sprite.Sprite; - -/** - * A location object represents a unique well-defined location within - * the scene at the lowest level of granularity available within the - * scene coordinate system. Locations reside at a full coordinate - * (comprised of tile coordinates and fine coordinates within the - * tile), and only one location may reside at each full coordinate in - * the scene. - */ -public class Location -{ - /** The unique identifier for this location. */ - public int id = -1; - - /** The location position in full coordinates. */ - public int x, y; - - /** The location orientation. */ - public int orient; - - /** - * Constructs a location object. - * - * @param x the x-position full coordinate. - * @param y the y-position full coordinate. - * @param orient the location orientation. - */ - public Location (int x, int y, int orient) - { - this.x = x; - this.y = y; - this.orient = orient; - } - - /** - * Constructs a location object with a default orientation. - * - * @param x the x-position full coordinate. - * @param y the y-position full coordinate. - */ - public Location (int x, int y) - { - this.x = x; - this.y = y; - this.orient = Sprite.DIR_SOUTHWEST; - } - - /** - * Renders the location centered at the given coordinates to the - * given graphics context. - * - * @param gfx the graphics context. - * @param cx the center x-coordinate. - * @param cy the center y-coordinate. - */ - public void paint (Graphics2D gfx, int cx, int cy) - { - // translate the origin to center on the location - gfx.translate(cx, cy); - - // rotate to reflect the location orientation - double rot = (Math.PI / 4.0f) * orient; - gfx.rotate(rot); - - // draw the triangle - gfx.setColor(getColor()); - gfx.fill(_tri); - - // outline the triangle in black - gfx.setColor(Color.black); - gfx.draw(_tri); - - // draw the rectangle - gfx.setColor(Color.red); - gfx.fillRect(-1, 2, 3, 3); - - // restore the original transform - gfx.rotate(-rot); - gfx.translate(-cx, -cy); - } - - /** - * Returns a string representation of the location. - */ - public String toString () - { - StringBuffer buf = new StringBuffer(); - buf.append("["); - toString(buf); - return buf.append("]").toString(); - } - - /** - * This should be overridden by derived classes (which should be sure - * to call super.toString()) to append the derived class - * specific event information to the string buffer. - */ - protected void toString (StringBuffer buf) - { - buf.append("id=").append(id); - buf.append(", x=").append(x); - buf.append(", y=").append(y); - buf.append(", orient=").append(orient); - } - - /** - * Returns the color to paint the inside of the location. - */ - protected Color getColor () - { - return Color.yellow; - } - - /** The triangle used to render a location on-screen. */ - protected static Polygon _tri; - - static { - _tri = new Polygon(); - _tri.addPoint(-3, -3); - _tri.addPoint(3, -3); - _tri.addPoint(0, 3); - }; -} diff --git a/src/java/com/threerings/miso/client/MisoCharacterSprite.java b/src/java/com/threerings/miso/client/MisoCharacterSprite.java index 3bbf13135..770090829 100644 --- a/src/java/com/threerings/miso/client/MisoCharacterSprite.java +++ b/src/java/com/threerings/miso/client/MisoCharacterSprite.java @@ -1,5 +1,5 @@ // -// $Id: MisoCharacterSprite.java,v 1.1 2001/10/26 01:17:21 shaper Exp $ +// $Id: MisoCharacterSprite.java,v 1.2 2001/11/18 04:09:22 mdb Exp $ package com.threerings.miso.scene; @@ -24,7 +24,7 @@ public class MisoCharacterSprite public boolean canTraverse (MisoTile tile) { // by default, passability is solely the province of the tile - return tile.passable; + return tile.isPassable(); } /** diff --git a/src/java/com/threerings/miso/client/MisoScene.java b/src/java/com/threerings/miso/client/MisoScene.java deleted file mode 100644 index 9d8beb06d..000000000 --- a/src/java/com/threerings/miso/client/MisoScene.java +++ /dev/null @@ -1,109 +0,0 @@ -// -// $Id: MisoScene.java,v 1.6 2001/10/17 22:21:22 shaper Exp $ - -package com.threerings.miso.scene; - -import java.util.List; - -import com.threerings.media.tile.Tile; -import com.threerings.media.tile.ObjectTile; - -import com.threerings.miso.tile.MisoTile; - -/** - * A scene object represents the data model corresponding to a single - * screen for game play. For instance, one scene might display a portion - * of a street with several buildings scattered about on the periphery. - */ -public interface MisoScene -{ - /** Scene id to denote an unset or otherwise invalid scene id. */ - public static final int SID_INVALID = -1; - - /** The total number of layers. */ - public static final int NUM_LAYERS = 3; - - /** The base layer id. */ - public static final int LAYER_BASE = 0; - - /** The fringe layer id. */ - public static final int LAYER_FRINGE = 1; - - /** The object layer id. */ - public static final int LAYER_OBJECT = 2; - - /** - * Returns the scene's unique identifier. - */ - public int getId (); - - /** - * Returns the scene's name. Every scene has a descriptive name. - */ - public String getName (); - - /** - * Returns an array of the tile layers that comprise the scene. - * The array returned by this method should not be - * modified. - */ - public Tile[][][] getTiles (); - - /** - * Returns the tile layer for the specified layer index. The - * array returned by this method should not be modified. - */ - public Tile[][] getTiles (int lnum); - - /** - * Returns the tiles that comprise the base layer of this scene. - * The array returned by this method should not be - * modified. - */ - public MisoTile[][] getBaseLayer (); - - /** - * Returns the tiles that comprise the fringe layer of this scene. - * The array returned by this method should not be - * modified. - */ - public Tile[][] getFringeLayer (); - - /** - * Returns the tiles that comprise the object layer of this scene. - * The array returned by this method should not be - * modified. - */ - public ObjectTile[][] getObjectLayer (); - - /** - * Returns the default tile for the base layer of the scene. - */ - public MisoTile getDefaultTile (); - - /** - * Returns the locations in this scene. The locations list should - * contain all locations and portals in the scene. The list - * returned by this method should not be modified. - */ - public List getLocations (); - - /** - * Returns the clusters in this scene. The clusters will reference - * all of the locations that are clustered. The list returned by - * this method should not be modified. - */ - public List getClusters (); - - /** - * Returns the portals associated with this scene. Portals should - * never be part of a cluster. The list returned by this method - * should not be modified. - */ - public List getPortals (); - - /** - * Returns the portal that is the default entrance to this scene. - */ - public Portal getEntrance (); -} diff --git a/src/java/com/threerings/miso/client/Portal.java b/src/java/com/threerings/miso/client/Portal.java deleted file mode 100644 index c60ca0c17..000000000 --- a/src/java/com/threerings/miso/client/Portal.java +++ /dev/null @@ -1,72 +0,0 @@ -// -// $Id: Portal.java,v 1.4 2001/10/25 16:36:43 shaper Exp $ - -package com.threerings.miso.scene; - -import java.awt.Color; - -/** - * The portal class represents a {@link Location} in a scene that both - * leads to a different scene and serves as a potential entrance into - * its containing scene. - */ -public class Portal extends Location -{ - /** The portal name used for binding the portal to another scene. */ - public String name; - - /** The destination scene id. */ - public int sid; - - /** The destination portal within the destination scene. */ - public Portal dest; - - /** - * Construct a portal object. - * - * @param loc the location associated with the portal. - * @param name the portal name. - */ - public Portal (Location loc, String name) - { - super(loc.x, loc.y, loc.orient); - this.name = name; - sid = MisoScene.SID_INVALID; - } - - /** - * Set the destination information for this portal. - * - * @param sid the scene id. - * @param dest the destination portal. - */ - public void setDestination (int sid, Portal dest) - { - this.sid = sid; - this.dest = dest; - } - - /** - * Return whether this portal has a valid destination scene and - * portal. - */ - public boolean hasDestination () - { - return (sid != MisoScene.SID_INVALID && dest != null); - } - - // documentation inherited - protected void toString (StringBuffer buf) - { - super.toString(buf); - buf.append(", name=").append(name); - buf.append(", sid=").append(sid); - buf.append(", dest=").append(dest); - } - - // documentation inherited - protected Color getColor () - { - return Color.green; - } -} diff --git a/src/java/com/threerings/miso/client/SceneView.java b/src/java/com/threerings/miso/client/SceneView.java index 4f03f2d12..fb1c426c7 100644 --- a/src/java/com/threerings/miso/client/SceneView.java +++ b/src/java/com/threerings/miso/client/SceneView.java @@ -1,5 +1,5 @@ // -// $Id: SceneView.java,v 1.18 2001/10/26 01:17:21 shaper Exp $ +// $Id: SceneView.java,v 1.19 2001/11/18 04:09:22 mdb Exp $ package com.threerings.miso.scene; @@ -36,7 +36,7 @@ public interface SceneView * * @param scene the scene to render in the view. */ - public void setScene (MisoScene scene); + public void setScene (DisplayMisoScene scene); /** * Returns a {@link Path} object detailing a valid path for the diff --git a/src/java/com/threerings/miso/client/SceneViewPanel.java b/src/java/com/threerings/miso/client/SceneViewPanel.java index 77a8f2537..d74f9f63c 100644 --- a/src/java/com/threerings/miso/client/SceneViewPanel.java +++ b/src/java/com/threerings/miso/client/SceneViewPanel.java @@ -1,5 +1,5 @@ // -// $Id: SceneViewPanel.java,v 1.19 2001/10/22 18:21:41 shaper Exp $ +// $Id: SceneViewPanel.java,v 1.20 2001/11/18 04:09:22 mdb Exp $ package com.threerings.miso.scene; @@ -56,7 +56,7 @@ public class SceneViewPanel extends AnimatedPanel /** * Sets the scene managed by the panel. */ - public void setScene (MisoScene scene) + public void setScene (DisplayMisoScene scene) { _view.setScene(scene); } diff --git a/src/java/com/threerings/miso/client/util/AStarPathUtil.java b/src/java/com/threerings/miso/client/util/AStarPathUtil.java index 81cc9d161..f803b32af 100644 --- a/src/java/com/threerings/miso/client/util/AStarPathUtil.java +++ b/src/java/com/threerings/miso/client/util/AStarPathUtil.java @@ -1,5 +1,5 @@ // -// $Id: AStarPathUtil.java,v 1.7 2001/10/22 21:32:13 shaper Exp $ +// $Id: AStarPathUtil.java,v 1.8 2001/11/18 04:09:22 mdb Exp $ package com.threerings.miso.scene.util; @@ -11,6 +11,7 @@ import com.threerings.media.util.MathUtil; import com.threerings.miso.Log; import com.threerings.miso.scene.Traverser; import com.threerings.miso.tile.MisoTile; +import com.threerings.miso.tile.MisoTileLayer; /** * The AStarPathUtil class provides a facility for @@ -43,7 +44,7 @@ public class AStarPathUtil * @return the list of points in the path. */ public static List getPath ( - MisoTile tiles[][], int tilewid, int tilehei, Traverser trav, + MisoTileLayer tiles, int tilewid, int tilehei, Traverser trav, int ax, int ay, int bx, int by) { AStarInfo info = new AStarInfo(tiles, tilewid, tilehei, trav, bx, by); @@ -202,7 +203,7 @@ public class AStarPathUtil protected static boolean isTraversable (AStarInfo info, int x, int y) { return (isCoordinateValid(info, x, y) && - info.trav.canTraverse(info.tiles[x][y])); + info.trav.canTraverse(info.tiles.getTile(y, x))); } /** @@ -252,8 +253,8 @@ public class AStarPathUtil */ class AStarInfo { - /** The array of tiles being traversed. */ - public MisoTile tiles[][]; + /** The tile layer being traversed. */ + public MisoTileLayer tiles; /** The tile array dimensions. */ public int tilewid, tilehei; @@ -274,7 +275,7 @@ class AStarInfo public int destx, desty; public AStarInfo ( - MisoTile tiles[][], int tilewid, int tilehei, Traverser trav, + MisoTileLayer tiles, int tilewid, int tilehei, Traverser trav, int destx, int desty) { // save off references diff --git a/src/java/com/threerings/miso/client/util/ClusterUtil.java b/src/java/com/threerings/miso/client/util/ClusterUtil.java deleted file mode 100644 index e98b6088c..000000000 --- a/src/java/com/threerings/miso/client/util/ClusterUtil.java +++ /dev/null @@ -1,125 +0,0 @@ -// -// $Id: ClusterUtil.java,v 1.3 2001/09/28 01:31:32 mdb Exp $ - -package com.threerings.miso.scene.util; - -import java.util.List; - -import com.threerings.miso.Log; -import com.threerings.miso.scene.*; - -/** - * The ClusterUtil class provides utility routines for - * working with a list of Cluster objects associated with - * a scene. - */ -public class ClusterUtil -{ - /** - * Return the cluster index number the given location is in, or -1 - * if the location is not in any cluster. - * - * @param clusters the cluster list. - * @param loc the location. - */ - public static int getClusterIndex (List clusters, Location loc) - { - int size = clusters.size(); - for (int ii = 0; ii < size; ii++) { - Cluster cluster = (Cluster)clusters.get(ii); - if (cluster.contains(loc)) { - return ii; - } - } - return -1; - } - - /** - * Remove the given location from its cluster, if any. - * - * @param clusters the cluster array. - * @param loc the location. - */ - public static void remove (List clusters, Location loc) - { - int size = clusters.size(); - for (int ii = 0; ii < size; ii++) { - Cluster cluster = (Cluster)clusters.get(ii); - - if (cluster.contains(loc)) { - cluster.remove(loc); - - // remove the cluster itself if it contains no more locations - if (cluster.size() == 0) { - clusters.remove(cluster); - } - - // we know the location can only reside in at most one cluster - break; - } - } - } - - /** - * Re-group the given location to be placed within the given cluster - * index. - * - *

If the cluster index is -1, the location is simply removed - * from any cluster it may reside in. Otherwise, the location is - * removed from any location it may already be in, and placed in - * the cluster corresponding to the requested cluster index. - * - *

The cluster index may be equal to the current number of clusters - * in the group, in which case a new cluster object will be created - * that initially contains only the given location. - * - * @param clusters the cluster list. - * @param loc the location. - * @param clusteridx the cluster index, or -1 to remove the location - * from any cluster. - */ - public static void regroup (List clusters, Location loc, int clusteridx) - { - // just remove the location if clusteridx is -1 - if (clusteridx == -1) { - remove(clusters, loc); - return; - } - - // make sure we're okay with the requested cluster index - int size = clusters.size(); - if (clusteridx > size) { - Log.warning("Attempt to regroup location to a non-contiguous " + - "cluster index [loc=" + loc + ", clusteridx=" + - clusteridx + "]."); - return; - } - - // get the cluster object the location's to be placed in - Cluster cluster = null; - if (clusteridx == size) { - // the location's being added to a new cluster, so create it - clusters.add(cluster = new Cluster()); - - } else { - // retrieve the cluster we're planning to place the location in - cluster = (Cluster)clusters.get(clusteridx); - - // this should never happen, but sanity-check anyway - if (cluster == null) { - Log.warning("Failed to retrieve cluster [clusteridx=" + - clusteridx + "]."); - return; - } - - // bail if the cluster already contains the location - if (cluster.contains(loc)) return; - } - - // remove the location from any other cluster it may already be in - remove(clusters, loc); - - // add the location to the cluster - cluster.add(loc); - } -} diff --git a/src/java/com/threerings/miso/client/util/IsoUtil.java b/src/java/com/threerings/miso/client/util/IsoUtil.java index ea0e0004e..3ac973e12 100644 --- a/src/java/com/threerings/miso/client/util/IsoUtil.java +++ b/src/java/com/threerings/miso/client/util/IsoUtil.java @@ -1,5 +1,5 @@ // -// $Id: IsoUtil.java,v 1.14 2001/10/26 01:17:21 shaper Exp $ +// $Id: IsoUtil.java,v 1.15 2001/11/18 04:09:22 mdb Exp $ package com.threerings.miso.scene.util; @@ -54,15 +54,15 @@ public class IsoUtil IsoSceneViewModel model, Polygon root, ObjectTile tile) { Rectangle bounds = root.getBounds(); - int sx = bounds.x - ((tile.baseWidth - 1) * model.tilehwid); - int sy = bounds.y - tile.height + model.tilehei; + int sx = bounds.x - ((tile.getBaseWidth() - 1) * model.tilehwid); + int sy = bounds.y - tile.getHeight() + model.tilehei; Polygon boundsPoly = new Polygon(); int rx = sx, ry = sy; // right point - rx = sx + tile.width; - ry = bounds.y - ((tile.baseHeight - 2) * model.tilehhei); + rx = sx + tile.getWidth(); + ry = bounds.y - ((tile.getBaseHeight() - 2) * model.tilehhei); boundsPoly.addPoint(rx, ry); // bottom-middle point @@ -72,17 +72,17 @@ public class IsoUtil // left point rx = sx; - ry = bounds.y - ((tile.baseWidth - 2) * model.tilehhei); + ry = bounds.y - ((tile.getBaseWidth() - 2) * model.tilehhei); boundsPoly.addPoint(rx, ry); // top-middle point - rx += (tile.baseHeight * model.tilehwid); - ry -= (tile.baseHeight * model.tilehhei); + rx += (tile.getBaseHeight() * model.tilehwid); + ry -= (tile.getBaseHeight() * model.tilehhei); boundsPoly.addPoint(rx, ry); // right point - rx = sx + tile.width; - ry = bounds.y - ((tile.baseHeight - 2) * model.tilehhei); + rx = sx + tile.getWidth(); + ry = bounds.y - ((tile.getBaseHeight() - 2) * model.tilehhei); boundsPoly.addPoint(rx, ry); return boundsPoly; @@ -104,8 +104,8 @@ public class IsoUtil IsoSceneViewModel model, Polygon root, ObjectTile tile) { Rectangle bounds = root.getBounds(); - int sx = bounds.x - ((tile.baseWidth - 1) * model.tilehwid); - int sy = bounds.y - tile.height + model.tilehei; + int sx = bounds.x - ((tile.getBaseWidth() - 1) * model.tilehwid); + int sy = bounds.y - tile.getHeight() + model.tilehei; Polygon boundsPoly = new Polygon(); int rx = sx, ry = sy; @@ -114,11 +114,11 @@ public class IsoUtil boundsPoly.addPoint(rx, ry); // top-right point - rx = sx + tile.width; + rx = sx + tile.getWidth(); boundsPoly.addPoint(rx, ry); // bottom-right point - ry = bounds.y - ((tile.baseHeight - 2) * model.tilehhei); + ry = bounds.y - ((tile.getBaseHeight() - 2) * model.tilehhei); boundsPoly.addPoint(rx, ry); // bottom-middle point @@ -128,7 +128,7 @@ public class IsoUtil // bottom-left point rx = sx; - ry = bounds.y - ((tile.baseWidth - 2) * model.tilehhei); + ry = bounds.y - ((tile.getBaseWidth() - 2) * model.tilehhei); boundsPoly.addPoint(rx, ry); // top-left point diff --git a/src/java/com/threerings/miso/client/util/MisoSceneUtil.java b/src/java/com/threerings/miso/client/util/MisoSceneUtil.java deleted file mode 100644 index 6d969fd18..000000000 --- a/src/java/com/threerings/miso/client/util/MisoSceneUtil.java +++ /dev/null @@ -1,105 +0,0 @@ -// -// $Id: MisoSceneUtil.java,v 1.4 2001/10/15 23:53:43 shaper Exp $ - -package com.threerings.miso.scene.util; - -import java.util.List; - -import com.threerings.miso.scene.*; - -/** - * Miso scene related utility functions and information. - */ -public class MisoSceneUtil -{ - /** String translations of each tile layer name. */ - public static final String[] XLATE_LAYERS = { "Base", "Fringe", "Object" }; - - /** - * Returns the layer index number for the named layer. Layer - * names are looked up via XLATE_LAYERS and are - * case-insensitive. - * - * @param name the layer name. - */ - public static int getLayerIndex (String name) - { - if (name == null) { - return DEF_LAYER; - } - - name = name.toLowerCase(); - - for (int ii = 0; ii < MisoScene.NUM_LAYERS; ii++) { - String b = MisoSceneUtil.XLATE_LAYERS[ii].toLowerCase(); - if (name.equals(b)) { - return ii; - } - } - - return DEF_LAYER; - } - - /** - * Return the location object at the given full coordinates, or null - * if no location is currently present at that location. - * - * @param scene the scene whose locations should be searched. - * @param x the full x-position coordinate. - * @param y the full y-position coordinate. - * - * @return the location object. - */ - public static Location getLocation (MisoScene scene, int x, int y) - { - List locs = scene.getLocations(); - int size = locs.size(); - for (int ii = 0; ii < size; ii++) { - Location loc = (Location)locs.get(ii); - if (loc.x == x && loc.y == y) { - return loc; - } - } - return null; - } - - /** - * Return the portal with the given name, or null if the portal isn't - * found in the portal list. - * - * @param scene the scene whose portals should be searched. - * @param name the portal name. - * - * @return the portal object. - */ - public static Portal getPortal (MisoScene scene, String name) - { - List portals = scene.getPortals(); - int size = portals.size(); - for (int ii = 0; ii < size; ii++) { - Portal portal = (Portal)portals.get(ii); - if (portal.name.equals(name)) { - return portal; - } - } - return null; - } - - /** - * Return the cluster index number the given location is in, or -1 - * if the location is not in any cluster. - * - * @param scene the containing scene. - * @param loc the location. - * - * @return the cluster index or -1 if the location is not in any - * cluster. - */ - public static int getClusterIndex (MisoScene scene, Location loc) - { - return ClusterUtil.getClusterIndex(scene.getClusters(), loc); - } - - /** The default layer index for an unknown named layer. */ - protected static final int DEF_LAYER = -1; -} diff --git a/src/java/com/threerings/miso/data/MisoSceneModel.java b/src/java/com/threerings/miso/data/MisoSceneModel.java new file mode 100644 index 000000000..941e7d028 --- /dev/null +++ b/src/java/com/threerings/miso/data/MisoSceneModel.java @@ -0,0 +1,30 @@ +// +// $Id: MisoSceneModel.java,v 1.1 2001/11/18 04:09:22 mdb Exp $ + +package com.threerings.miso.scene; + +/** + * The scene model is the bare bones representation of the data for a + * scene in the Miso system. From the scene model, one would create an + * instance of {@link DisplayMisoScene}. + */ +public class MisoSceneModel +{ + /** The width of the scene in tile units. */ + public int width; + + /** The height of the scene in tile units. */ + public int height; + + /** The combined tile ids (tile set id and tile id) of the tiles in + * the base layer, in row-major order. */ + public int[] baseTileIds; + + /** The combined tile ids (tile set id and tile id) of the tiles in + * the fringe layer, in row-major order. */ + public int[] fringeTileIds; + + /** The combined tile ids (tile set id and tile id) of the files in + * the object layer in (x, y, tile id) format. */ + public int[] objectTileIds; +} diff --git a/src/java/com/threerings/miso/tile/BaseTile.java b/src/java/com/threerings/miso/tile/BaseTile.java index f9530ce73..df358f053 100644 --- a/src/java/com/threerings/miso/tile/BaseTile.java +++ b/src/java/com/threerings/miso/tile/BaseTile.java @@ -1,23 +1,54 @@ // -// $Id: BaseTile.java,v 1.1 2001/10/08 21:04:25 shaper Exp $ +// $Id: BaseTile.java,v 1.2 2001/11/18 04:09:22 mdb Exp $ package com.threerings.miso.tile; +import java.awt.Image; import com.threerings.media.tile.Tile; /** - * The miso tile class extends the base tile class to add support for - * tile passability. + * Extends the base tile class to add support for tile passability. * * @see MisoTileSet */ public class MisoTile extends Tile { - /** Whether the tile is passable. */ - public boolean passable; - - public MisoTile (int tsid, int tid) + /** + * Constructs a new miso tile with the specified image. Passability + * will be assumed to be true. + */ + public MisoTile (Image image) { - super(tsid, tid); + super(image); } + + /** + * Constructs a new miso tile with the specified passability. + */ + public MisoTile (Image image, boolean passable) + { + super(image); + _passable = passable; + } + + /** + * Returns whether or not this tile can be walked upon by character + * sprites. + */ + public boolean isPassable () + { + return _passable; + } + + /** + * Sets whether or not this tile can be walked upon by character + * sprites. + */ + public void setPassable (boolean passable) + { + _passable = passable; + } + + /** Whether the tile is passable. */ + protected boolean _passable = true; } diff --git a/src/java/com/threerings/miso/tile/BaseTileLayer.java b/src/java/com/threerings/miso/tile/BaseTileLayer.java new file mode 100644 index 000000000..c2913ab8c --- /dev/null +++ b/src/java/com/threerings/miso/tile/BaseTileLayer.java @@ -0,0 +1,84 @@ +// +// $Id: BaseTileLayer.java,v 1.1 2001/11/18 04:09:22 mdb Exp $ + +package com.threerings.miso.tile; + +/** + * The miso tile layer class is a convenience class provided to simplify + * the management of a two-dimensional array of miso tiles. It takes care + * of dereferencing the tile array efficiently with methods that the Java + * compiler should inline, and prevents the caller from having to do the + * indexing multiplication by hand every time. + * + *

This is equivalent to {@link TileLayer} except that it contains + * {@link MisoTile} instances. For efficiency's sake, we don't extend + * that class but instead provide a direct implementation. + */ +public final class MisoTileLayer +{ + /** + * Constructs a miso tile layer instance with the supplied tiles, + * width and height. The tiles should exist in row-major format (the + * first row of tiles followed by the second and so on). + * + * @exception IllegalArgumentException thrown if the size of the tiles + * array does not match the specified width and height. + */ + public MisoTileLayer (MisoTile[] tiles, int width, int height) + { + // sanity check + if (tiles.length != width*height) { + String errmsg = "tiles.length != width*height"; + throw new IllegalArgumentException(errmsg); + } + + _tiles = tiles; + _width = width; + _height = height; + } + + /** + * Returns the width of the layer in tiles. + */ + public int getWidth () + { + return _width; + } + + /** + * Returns the height of the layer in tiles. + */ + public int getHeight () + { + return _height; + } + + /** + * Fetches the tile at the specified row and column. Bounds checking + * is not done. Note that the parameters are column first, followed by + * row (x, y order rather than row, column order). + */ + public MisoTile getTile (int column, int row) + { + return _tiles[row*_width+column]; + } + + /** + * Sets the tile at the specified row and column. Bounds checking is + * not done. Note that the parameters are column first, followed by + * row (x, y order rather than row, column order). + */ + public void setTile (int column, int row, MisoTile tile) + { + _tiles[row*_width+column] = tile; + } + + /** Our tiles array. */ + private MisoTile[] _tiles; + + /** The number of tiles in a row. */ + private int _width; + + /** The number of rows. */ + private int _height; +} diff --git a/src/java/com/threerings/miso/tile/BaseTileSet.java b/src/java/com/threerings/miso/tile/BaseTileSet.java index 65858b6a1..245be07f6 100644 --- a/src/java/com/threerings/miso/tile/BaseTileSet.java +++ b/src/java/com/threerings/miso/tile/BaseTileSet.java @@ -1,81 +1,36 @@ // -// $Id: BaseTileSet.java,v 1.5 2001/11/08 03:04:45 mdb Exp $ +// $Id: BaseTileSet.java,v 1.6 2001/11/18 04:09:22 mdb Exp $ package com.threerings.miso.tile; -import java.awt.Point; +import java.awt.Image; -import com.samskivert.util.HashIntMap; - -import com.threerings.media.ImageManager; -import com.threerings.media.tile.*; - -import com.threerings.miso.scene.MisoScene; +import com.threerings.media.tile.Tile; +import com.threerings.media.tile.SwissArmyTileSet; /** * The miso tile set extends the swiss army tile set to add support for * tile passability. Passability is used to determine whether {@link * com.threerings.miso.scene.Traverser} objects can traverse a particular - * tile in a {@link MisoScene}. + * tile in a scene. */ public class MisoTileSet extends SwissArmyTileSet { /** - * Constructs a Miso tileset with the swiss army tile set - * configuration information and additional information about tile - * passability. - * - * @param layer the layer to which this tileset is assigned. - * @param passable info on each tile indicating whether or not the - * tile is passable (can be walked on by sprites). - * - * @see SwissArmyTileSet#SwissArmyTileSet + * Sets the passability information for the tiles in this tileset. + * Each entry in the array corresponds to the tile at that tile index. */ - public MisoTileSet ( - ImageManager imgmgr, String imgFile, String name, int tsid, - int[] tileCount, int[] rowWidth, int[] rowHeight, - Point offsetPos, Point gapDist, int layer, int[] passable) + public void setPassability (boolean[] passable) { - super(imgmgr, imgFile, name, tsid, tileCount, - rowWidth, rowHeight, offsetPos, gapDist); - - _layer = layer; _passable = passable; } // documentation inherited - public Tile createTile (int tid) + public Tile createTile (Image image, int tileIndex) { - // only create miso tiles for the base layer - if (_layer != MisoScene.LAYER_BASE) { - return super.createTile(tid); - } - - return new MisoTile(_tsid, tid); + return new MisoTile(image, _passable[tileIndex]); } - // documentation inherited - protected void populateTile (Tile tile) - { - super.populateTile(tile); - - if (tile instanceof MisoTile) { - // set the tile's passability, defaulting to passable if this - // tileset has no passability specified - ((MisoTile)tile).passable = - (_passable == null || (_passable[tile.tid] == 1)); - } - } - - // documentation inherited - public int getLayerIndex () - { - return _layer; - } - - /** The miso scene layer the tiles are intended for. */ - protected int _layer; - /** Whether each tile is passable. */ - protected int _passable[]; + protected boolean[] _passable; } diff --git a/src/java/com/threerings/miso/tile/ShadowTile.java b/src/java/com/threerings/miso/tile/ShadowTile.java index d47b40ad8..d4eace733 100644 --- a/src/java/com/threerings/miso/tile/ShadowTile.java +++ b/src/java/com/threerings/miso/tile/ShadowTile.java @@ -1,5 +1,5 @@ // -// $Id: ShadowTile.java,v 1.2 2001/10/17 22:22:03 shaper Exp $ +// $Id: ShadowTile.java,v 1.3 2001/11/18 04:09:22 mdb Exp $ package com.threerings.miso.tile; @@ -7,10 +7,9 @@ import java.awt.Graphics2D; import java.awt.Shape; /** - * The shadow tile extends miso tile to provide an always-impassable - * tile that has no display image. Shadow tiles are intended for - * placement in the footprint of {@link - * com.threerings.media.tile.ObjectTile} objects. + * The shadow tile extends miso tile to provide an always-impassable tile + * that has no display image. Shadow tiles are intended for placement in + * the footprint of {@link com.threerings.media.tile.ObjectTile} objects. */ public class ShadowTile extends MisoTile { @@ -22,14 +21,14 @@ public class ShadowTile extends MisoTile */ public ShadowTile (int x, int y) { - super(SHADOW_TSID, SHADOW_TID); + super(null); // save the coordinates of our parent object tile ox = x; oy = y; // shadow tiles are always impassable - passable = false; + _passable = false; } // documentation inherited @@ -37,10 +36,4 @@ public class ShadowTile extends MisoTile { // paint nothing as we're naught but a measly shadow of a tile } - - /** The shadow tile set id. */ - protected static final int SHADOW_TSID = -1; - - /** The shadow tile id. */ - protected static final int SHADOW_TID = -1; } diff --git a/src/java/com/threerings/miso/tile/tools/xml/BaseTileSetRuleSet.java b/src/java/com/threerings/miso/tile/tools/xml/BaseTileSetRuleSet.java new file mode 100644 index 000000000..b9acaae11 --- /dev/null +++ b/src/java/com/threerings/miso/tile/tools/xml/BaseTileSetRuleSet.java @@ -0,0 +1,57 @@ +// +// $Id: BaseTileSetRuleSet.java,v 1.1 2001/11/18 04:09:23 mdb Exp $ + +package com.threerings.media.tile.xml; + +import org.xml.sax.Attributes; +import org.apache.commons.digester.Digester; + +import com.threerings.media.tile.TileSet; +import com.threerings.media.tile.xml.SwissArmyTileSetRuleSet; +import com.threerings.miso.tile.MisoTileSet; + +/** + * Parses {@link MisoTileSet} instances from a tileset description. A + * uniform tileset description looks like so: + * + *

+ * <tileset name="Sample Miso Tileset">
+ *   <imgpath>path/to/image.png</imgpath>
+ *   <!-- the width of each tile in pixels -->
+ *   <width>64</width>
+ *   <!-- the height of each tile in pixels -->
+ *   <height>48</height>
+ *   <!-- the total number of tiles in the set -->
+ *   <tileCount>16</tileCount>
+ * </tileset>
+ * 
+ */ +public class MisoTileSetRuleSet extends SwissArmyTileSetRuleSet +{ + /** + * Constructs a uniform tileset rule set that will match tilesets with + * the specified prefix. See the documentation for {@link + * TileSetRuleSet#TileSetruleSet} for more info on matching. + */ + public MisoTileSetRuleSet (String prefix) + { + super(prefix); + } + + // documentation inherited + public void addRuleInstances (Digester digester) + { + super.addRuleInstances(digester); + + digester.addCallMethod( + _prefix + "/tileset/width", "setWidth", 0, + new Class[] { java.lang.Integer.TYPE }); + } + + // documentation inherited + protected TileSet createTileSet (Attributes attributes) + { + // we use uniform tilesets + return new MisoTileSet(); + } +} diff --git a/src/java/com/threerings/miso/tile/tools/xml/XMLMisoTileSetParser.java b/src/java/com/threerings/miso/tile/tools/xml/XMLMisoTileSetParser.java deleted file mode 100644 index 427b38d2b..000000000 --- a/src/java/com/threerings/miso/tile/tools/xml/XMLMisoTileSetParser.java +++ /dev/null @@ -1,78 +0,0 @@ -// -// $Id: XMLMisoTileSetParser.java,v 1.6 2001/11/08 03:04:45 mdb Exp $ - -package com.threerings.miso.tile; - -import org.xml.sax.*; - -import com.samskivert.util.StringUtil; - -import com.threerings.media.ImageManager; -import com.threerings.media.tile.*; - -import com.threerings.miso.scene.util.MisoSceneUtil; - -/** - * Extends the base XML tile set parser to construct {@link - * MisoTileSet} tilesets that provide additional functionality - * specific to the miso layer. - */ -public class XMLMisoTileSetParser extends XMLTileSetParser -{ - // documentation inherited - public XMLMisoTileSetParser (ImageManager imgmgr) - { - super(imgmgr); - } - - // documentation inherited - public void startElement ( - String uri, String localName, String qName, Attributes attributes) - { - super.startElement(uri, localName, qName, attributes); - - if (qName.equals("tileset")) { - String val = attributes.getValue("layer"); - _layer = MisoSceneUtil.getLayerIndex(val); - } - } - - // documentation inherited - protected void finishElement ( - String uri, String localName, String qName, String data) - { - super.finishElement(uri, localName, qName, data); - - if (qName.equals("passable")) { - _passable = StringUtil.parseIntArray(data); - - } else if (qName.equals("tileset")) { - _passable = null; - _layer = -1; - } - } - - // documentation inherited - protected TileSet createTileSet () - { - if (_info.isObjectSet) { - return new ObjectTileSet( - _imgmgr, _info.imgFile, _info.name, _info.tsid, - _info.tileCount, _info.rowWidth, _info.rowHeight, - _info.offsetPos, _info.gapDist, _info.objects); - - } else { - return new MisoTileSet( - _imgmgr, _info.imgFile, _info.name, _info.tsid, - _info.tileCount, _info.rowWidth, _info.rowHeight, - _info.offsetPos, _info.gapDist, _layer, _passable); - } - } - - /** Info on whether or not the tiles are passable (can be walked on by - * sprites). */ - protected int[] _passable; - - /** The layer to which the tiles are assigned. */ - protected int _layer = -1; -} diff --git a/src/java/com/threerings/miso/tile/tools/xml/XMLTileSetRepository.java b/src/java/com/threerings/miso/tile/tools/xml/XMLTileSetRepository.java deleted file mode 100644 index dee9fb1f8..000000000 --- a/src/java/com/threerings/miso/tile/tools/xml/XMLTileSetRepository.java +++ /dev/null @@ -1,68 +0,0 @@ -// -// $Id: XMLTileSetRepository.java,v 1.2 2001/11/02 02:52:16 shaper Exp $ - -package com.threerings.miso.tile; - -import java.io.IOException; -import java.util.*; - -import com.samskivert.util.Config; -import com.samskivert.util.HashIntMap; - -import com.threerings.media.ImageManager; -import com.threerings.media.tile.*; - -import com.threerings.miso.Log; -import com.threerings.miso.util.MisoUtil; - -/** - * Extends general tile set repository functionality to read tile set - * descriptions from an XML file. - */ -public class XMLTileSetRepository implements TileSetRepository -{ - // documentation inherited - public void init (Config config, ImageManager imgmgr) - { - // get the tile set description file path - String fname = config.getValue(TILESETS_KEY, DEFAULT_TILESETS); - - // load the tilesets from the XML description file - try { - XMLMisoTileSetParser p = new XMLMisoTileSetParser(imgmgr); - p.loadTileSets(fname, _tilesets); - } catch (IOException ioe) { - Log.warning("Exception loading tile sets [ioe=" + ioe + "]."); - return; - } - } - - // documentation inherited - public TileSet getTileSet (int tsid) - throws NoSuchTileSetException - { - TileSet tset = (TileSet)_tilesets.get(tsid); - if (tset == null) { - throw new NoSuchTileSetException(tsid); - } - - return tset; - } - - // documentation inherited - public Iterator enumerateTileSets () - { - return Collections.unmodifiableMap(_tilesets).values().iterator(); - } - - /** The config key for the tileset description file. */ - protected static final String TILESETS_KEY = - MisoUtil.CONFIG_KEY + ".tilesets"; - - /** The default tileset description file. */ - protected static final String DEFAULT_TILESETS = - "rsrc/config/miso/tilesets.xml"; - - /** The available tilesets keyed by tileset id. */ - protected HashIntMap _tilesets = new HashIntMap(); -} diff --git a/src/java/com/threerings/miso/tools/EditableMisoScene.java b/src/java/com/threerings/miso/tools/EditableMisoScene.java index 86c863ff4..af5dd998a 100644 --- a/src/java/com/threerings/miso/tools/EditableMisoScene.java +++ b/src/java/com/threerings/miso/tools/EditableMisoScene.java @@ -1,74 +1,100 @@ // -// $Id: EditableMisoScene.java,v 1.4 2001/10/17 22:21:22 shaper Exp $ +// $Id: EditableMisoScene.java,v 1.5 2001/11/18 04:09:23 mdb Exp $ -package com.threerings.miso.scene; +package com.threerings.miso.tools; +import com.threerings.media.tile.ObjectTile; import com.threerings.media.tile.Tile; import com.threerings.miso.tile.MisoTile; +import com.threerings.miso.scene.DisplayMisoScene; +import com.threerings.miso.scene.MisoSceneModel; /** - * The editable Miso scene interface provides the means for modifying a - * Miso scene which is needed by things like the scene editor. This is - * separated from the read-only interface to avoid implying to users of - * the Miso scene interface that editing scenes is a safe thing to do. In - * fact it should only be done under special circumstances. + * 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 MisoScene + extends DisplayMisoScene { /** - * Updates the scene's unique identifier. + * Returns the default base tile. */ - public void setId (int sceneId); + public MisoTile getDefaultBaseTile (); /** - * Updates the scene's name. - */ - public void setName (String name); - - /** - * Updates the scene's default tile. - */ - public void setDefaultTile (MisoTile tile); - - /** - * Updates the specified tile in the scene. - */ - public void setTile (int lnum, int x, int y, Tile tile); - - /** - * Set the default entrance portal for this scene. + * Sets the default base tile. * - * @param entrance the entrance portal. + * @param defaultBaseTile the new default base tile. + * @param fqTileId the fully-qualified tile id (@see + * com.threerings.media.tile.TileUtil#getFQTileId}) of the new default + * base tile. */ - public void setEntrance (Portal entrance); + public void setDefaultBaseTile (MisoTile defaultBaseTile, int fqTileId); /** - * Update the specified location in the scene. If the cluster - * index number is -1, the location will be removed from any - * cluster it may reside in. + * Updates the tile at the specified location in the base layer. * - * @param loc the location. - * @param clusteridx the cluster index number. + * @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 + * com.threerings.media.tile.TileUtil#getFQTileId}) of the new default + * base tile. */ - public void updateLocation (Location loc, int clusteridx); + public void setBaseTile (int x, int y, MisoTile tile, int fqTileId); /** - * Add the specified portal to the scene. Adds the portal to the - * location list as well if it's not already present and removes - * it from any cluster it may reside in. + * Updates the tile at the specified location in the fringe layer. * - * @param portal the portal. + * @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 + * com.threerings.media.tile.TileUtil#getFQTileId}) of the new default + * base tile. */ - public void addPortal (Portal portal); + public void setFringeTile (int x, int y, Tile tile, int fqTileId); /** - * Remove the given location object from the location list, and - * from any containing cluster. If the location is a portal, it - * is removed from the portal list as well. + * Updates the tile at the specified location in the object layer. Any + * previous object tile at that location should be cleared out by the + * implementation of this method before the new tile is set to ensure + * that footprint tiles associated with the old object are properly + * disposed of. * - * @param loc the location object. + * @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 + * com.threerings.media.tile.TileUtil#getFQTileId}) of the new default + * base tile. */ - public void removeLocation (Location loc); + public void setObjectTile (int x, int y, ObjectTile tile, int fqTileId); + + /** + * Clears out the tile at the specified location in the base layer. + */ + public void clearBaseTile (int x, int y); + + /** + * Clears out the tile at the specified location in the fringe layer. + */ + public void clearFringeTile (int x, int y); + + /** + * Clears out the tile at the specified location in the object layer. + */ + public void clearObjectTile (int x, int y); + + /** + * Returns a reference to the miso scene model that reflects the + * changes that have been made to this editable miso scene. + */ + public MisoSceneModel getModel (); } diff --git a/src/java/com/threerings/miso/tools/EditableMisoSceneImpl.java b/src/java/com/threerings/miso/tools/EditableMisoSceneImpl.java new file mode 100644 index 000000000..56b7fe86d --- /dev/null +++ b/src/java/com/threerings/miso/tools/EditableMisoSceneImpl.java @@ -0,0 +1,139 @@ +// +// $Id: EditableMisoSceneImpl.java,v 1.1 2001/11/18 04:09:23 mdb Exp $ + +package com.threerings.miso.tools; + +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.TileManager; + +import com.threerings.miso.tile.MisoTile; + +import com.threerings.miso.scene.DisplayMisoSceneImpl; +import com.threerings.miso.scene.MisoSceneModel; + +/** + * 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. + * + * @exception NoSuchTileException thrown if the model references a + * tile which is not available via the supplied tile manager. + */ + public EditableMisoSceneImpl (MisoSceneModel model, TileManager tmgr) + throws NoSuchTileException, NoSuchTileSetException + { + super(model, tmgr); + + // we'll need to be keeping this + _model = model; + + // we need this to track object layer mods + _objectTileIds = new int[_model.baseTileIds.length]; + } + + // documentation inherited + public MisoTile getDefaultBaseTile () + { + return _defaultBaseTile; + } + + // documentation inherited + public void setDefaultBaseTile (MisoTile defaultBaseTile, int fqTileId) + { + _defaultBaseTile = defaultBaseTile; + _defaultBaseTileId = fqTileId; + } + + // documentation inherited + public void setBaseTile (int x, int y, MisoTile tile, int fqTileId) + { + _base.setTile(x, y, tile); + // update the model as well + _model.baseTileIds[_model.width*y + x] = fqTileId; + } + + // documentation inherited + public void setFringeTile (int x, int y, Tile tile, int fqTileId) + { + _fringe.setTile(x, y, tile); + // update the model as well + _model.fringeTileIds[_model.width*y + x] = fqTileId; + } + + // documentation inherited + public void setObjectTile (int x, int y, ObjectTile tile, int fqTileId) + { + ObjectTile prev = _object.getTile(x, y); + // clear out any previous tile so that shadow tiles are properly + // removed + if (prev != null) { + clearObjectTile(x, y); + } + _object.setTile(x, y, tile); + // stick this value into our non-sparse object layer + _objectTileIds[_model.width*y + x] = fqTileId; + } + + // documentation inherited + public void clearBaseTile (int x, int y) + { + _base.setTile(x, y, _defaultBaseTile); + // clear it out in the model + _model.baseTileIds[_model.width*y + x] = _defaultBaseTileId; + } + + // documentation inherited + public void clearFringeTile (int x, int y) + { + _fringe.setTile(x, y, null); + // clear it out in the model + _model.fringeTileIds[_model.width*y + x] = 0; + } + + // documentation inherited + public void clearObjectTile (int x, int y) + { + setObjectTileFootprint(x, y, _defaultBaseTile); + // clear it out in our non-sparse array + _objectTileIds[_model.width*y + x] = 0; + // we don't have to worry about setting the footprint in the model + // because footprints are always inferred from the contents of the + // object layer and the base layer in the model can simply contain + // the default tiles + } + + // documentation inherited + public MisoSceneModel getModel () + { + // we need to flush the object layer to the model prior to + // returning it + + return _model; + } + + /** Our scene model, which we always keep in sync with our display + * model data. */ + protected MisoSceneModel _model; + + /** A non-sparse array where we can keep track of the object tile + * ids. */ + protected int[] _objectTileIds; + + /** The default tile with which to fill the base layer. */ + protected MisoTile _defaultBaseTile; + + /** The fully qualified tile id of the default base tile. */ + protected int _defaultBaseTileId; +} diff --git a/src/java/com/threerings/miso/tools/xml/MisoSceneRuleSet.java b/src/java/com/threerings/miso/tools/xml/MisoSceneRuleSet.java new file mode 100644 index 000000000..dc4df12ed --- /dev/null +++ b/src/java/com/threerings/miso/tools/xml/MisoSceneRuleSet.java @@ -0,0 +1,63 @@ +// +// $Id: MisoSceneRuleSet.java,v 1.1 2001/11/18 04:09:22 mdb Exp $ + +package com.threerings.media.tile.xml; + +import org.apache.commons.digester.Digester; +import org.apache.commons.digester.RuleSetBase; + +import com.samskivert.xml.SetFieldRule; + +import com.threerings.miso.scene.MisoSceneModel; + +/** + * Used to parse a {@link MisoSceneModel} from XML. + */ +public abstract class MisoSceneRuleSet extends RuleSetBase +{ + /** + * Constructs a miso scene rule set which will match scenes with the + * supplied prefix. For example, passing scene.miso will + * match the scene in the following XML file: + * + *
+     * <scene>
+     *   <miso>
+     *     <width>50</width>
+     *     <height>50</height>
+     *     <!-- ... -->
+     *   </miso>
+     * </scene>
+     * 
+ */ + public MisoSceneRuleSet (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 + "/base", + new SetFieldRule(digester, "baseTileIds")); + digester.addRule(_prefix + "/fringe", + new SetFieldRule(digester, "fringeTileIds")); + digester.addRule(_prefix + "/object", + new SetFieldRule(digester, "objectTileIds")); + } + + /** The prefix at which me match our scenes. */ + protected String _prefix; +} diff --git a/src/java/com/threerings/miso/tools/xml/MisoSceneWriter.java b/src/java/com/threerings/miso/tools/xml/MisoSceneWriter.java new file mode 100644 index 000000000..df83574a5 --- /dev/null +++ b/src/java/com/threerings/miso/tools/xml/MisoSceneWriter.java @@ -0,0 +1,34 @@ +// +// $Id: MisoSceneWriter.java,v 1.1 2001/11/18 04:09:22 mdb Exp $ + +package com.threerings.miso.scene.xml; + +import org.xml.sax.SAXException; +import com.megginson.sax.DataWriter; +import com.samskivert.util.StringUtil; +import com.threerings.miso.scene.MisoSceneModel; + +/** + * Generates an XML representation of a {@link MisoSceneModel}. + */ +public class MisoSceneWriter +{ + /** + * 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) + throws SAXException + { + writer.startElement("miso"); + writer.dataElement("width", Integer.toString(model.width)); + writer.dataElement("height", Integer.toString(model.height)); + writer.dataElement("base", StringUtil.toString(model.baseTileIds)); + writer.dataElement("fringe", StringUtil.toString(model.fringeTileIds)); + writer.dataElement("object", StringUtil.toString(model.objectTileIds)); + writer.endElement("miso"); + } +} diff --git a/src/java/com/threerings/miso/tools/xml/XMLSceneGroupParser.java b/src/java/com/threerings/miso/tools/xml/XMLSceneGroupParser.java deleted file mode 100644 index 0c7ef995c..000000000 --- a/src/java/com/threerings/miso/tools/xml/XMLSceneGroupParser.java +++ /dev/null @@ -1,315 +0,0 @@ -// -// $Id: XMLSceneGroupParser.java,v 1.8 2001/11/12 20:56:55 mdb Exp $ - -package com.threerings.miso.scene.xml; - -import java.io.*; -import java.util.*; - -import org.xml.sax.*; - -import com.samskivert.util.*; -import com.samskivert.xml.SimpleParser; - -import com.threerings.miso.Log; -import com.threerings.miso.scene.*; -import com.threerings.miso.scene.util.MisoSceneUtil; - -/** - * 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. - */ -public class XMLSceneGroupParser extends SimpleParser -{ - // documentation inherited - public void startElement ( - String uri, String localName, String qName, Attributes attributes) - { - 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 (qName.equals("portal")) { - // pull out the portal data - String src = attributes.getValue("src"); - String destScene = attributes.getValue("destscene"); - String dest = attributes.getValue("dest"); - - // construct a new portal info object - PortalInfo pinfo = new PortalInfo(src, destScene, dest); - - // add it to the current scene's list of portal info objects - _info.curscene.portals.add(pinfo); - } - } - - // documentation inherited - public void finishElement ( - String uri, String localName, String qName, String data) - { - if (qName.equals("name")) { - _info.curscene.name = data; - - } else if (qName.equals("entrance")) { - _info.curscene.entrance = data; - - } else if (qName.equals("scene")) { - // TODO: load scene from scene repository and set - // _info.curscene.scene to the resulting scene object - - } else if (qName.equals("scenegroup")) { - // now that we've obtained all scene info and fully loaded - // the associated scene objects, resolve bindings between - // all scenes in the group. - _info.resolveBindings(); - - // warn about any remaining un-bound portals - _info.checkUnboundPortals(); - } - } - - /** - * Parse the specified XML file and return a list of miso scene - * objects that have been fully bound and loaded into the scene - * repository. - * - * @param fname the file name. - * - * @return the list of scene objects, or null if an error occurred. - */ - public List loadSceneGroup (String fname) throws IOException - { - _info = new SceneGroupInfo(); - parseFile(fname); - return _info.getScenes(); - } - - /** The scene group info gathered while parsing. */ - protected SceneGroupInfo _info; - - /** - * A class to hold information on the overall scene group while - * parsing, as well as the resulting list of scene objects - * produced when finished. - */ - class SceneGroupInfo - { - /** The scene info for all scenes described in the group. */ - public ArrayList sceneinfo; - - /** The current scene info. */ - public SceneInfo curscene; - - public SceneGroupInfo () - { - sceneinfo = new ArrayList(); - } - - /** - * Return a list of the scene objects contained within all - * scene info objects. Intended to be called once all parsing - * and resolving of portal bindings has been completed. - */ - public List getScenes () - { - ArrayList scenes = new ArrayList(); - int size = sceneinfo.size(); - for (int ii = 0; ii < size; ii++) { - scenes.add(((SceneInfo)sceneinfo.get(ii)).scene); - } - return scenes; - } - - /** - * Return the scene object associated with the named scene - * info object. - * - * @param name the scene info name. - * - * @return the scene object or null if the scene info object - * isn't found or has no scene object. - */ - public MisoScene getScene (String name) - { - int size = sceneinfo.size(); - for (int ii = 0; ii < size; ii++) { - SceneInfo sinfo = (SceneInfo)sceneinfo.get(ii); - if (sinfo.name.equals(name)) return sinfo.scene; - } - return null; - } - - /** - * Resolve the portal bindings and entrance portal for all scenes. - */ - public void resolveBindings () - { - int size = sceneinfo.size(); - for (int ii = 0; ii < size; ii++) { - // retrieve the next scene info object - SceneInfo sinfo = (SceneInfo)sceneinfo.get(ii); - - int psize = sinfo.portals.size(); - for (int jj = 0; jj < psize; jj++) { - // retrieve the next portal info object - PortalInfo pinfo = (PortalInfo)sinfo.portals.get(jj); - - // resolve the portal binding - resolvePortal(sinfo, pinfo); - - // resolve the entrance portal - resolveEntrance(sinfo); - } - } - } - - /** - * Resolve the binding for the portal in the given scene. - * - * @param sinfo the scene info. - * @param pinfo the portal info. - */ - protected void resolvePortal (SceneInfo sinfo, PortalInfo pinfo) - { - // get the source portal object - Portal src = MisoSceneUtil.getPortal(sinfo.scene, pinfo.src); - if (src == null) { - Log.warning("Missing source portal [scene=" + sinfo.name + - ", pinfo=" + pinfo + "]."); - return; - } - - // get the destination scene - MisoScene destScene = getScene(pinfo.destScene); - if (destScene == null) { - Log.warning("Missing destination scene [scene=" + sinfo.name + - ", pinfo=" + pinfo + "]."); - return; - } - - // get the destination portal object - Portal dest = MisoSceneUtil.getPortal(destScene, pinfo.dest); - if (dest == null) { - Log.warning("Missing destination portal [scene=" + sinfo.name + - ", pinfo=" + pinfo + "]."); - return; - } - - // update source portal with full destination information - src.setDestination(destScene.getId(), dest); - } - - /** - * Resolve the binding for the entrance portal in the given scene. - * - * @param sinfo the scene info. - */ - protected void resolveEntrance (SceneInfo sinfo) - { - Portal entrance = MisoSceneUtil.getPortal( - sinfo.scene, sinfo.entrance); - if (entrance == null) { - Log.warning( "Missing entrance portal [scene=" + sinfo.name + - ", entrance=" + sinfo.entrance + "]."); - return; - } - - sinfo.scene.setEntrance(entrance); - } - - /** - * Scan through all scenes and output a warning message if any - * portals are found that aren't bound to a destination - * portal. Intended for calling after {@link - * #resolveBindings} is called to make sure the scene group - * description wasn't lacking a binding for some portal; or, - * that the scenes don't contain any unintended or unnecessary - * portals. - */ - public void checkUnboundPortals () - { - int size = sceneinfo.size(); - for (int ii = 0; ii < size; ii++) { - // retrieve the next scene info object - SceneInfo sinfo = (SceneInfo)sceneinfo.get(ii); - - // get the portals in the scene - List portals = sinfo.scene.getPortals(); - - // check each portal to make sure it has a valid destination - int psize = portals.size(); - for (int jj = 0; jj < size; jj++) { - Portal portal = (Portal)portals.get(jj); - if (!portal.hasDestination()) { - Log.warning("Unbound portal [scene=" + sinfo.name + - ", portal=" + portal + "]."); - } - } - } - } - } - - /** - * A class to hold information on a single scene's bindings. - */ - class SceneInfo - { - /** The logical scene name. */ - public String name; - - /** The scene description file path. */ - public String src; - - /** The default scene entrance. */ - public String entrance; - - /** The list of portal info objects describing portal bindings. */ - public ArrayList portals; - - /** The scene object obtained from the scene repository. */ - public MisoSceneImpl scene; - - public SceneInfo (String src) - { - this.src = src; - portals = new ArrayList(); - } - } - - /** - * A class to hold information on a single portal's bindings. - */ - class PortalInfo - { - /** The source portal name. */ - public String src; - - /** The destination portal name within the destination scene. */ - public String dest; - - /** The logical destination scene name. */ - public String destScene; - - public PortalInfo (String src, String destScene, String dest) - { - this.src = src; - this.destScene = destScene; - this.dest = dest; - } - - public String toString () - { - StringBuffer buf = new StringBuffer(); - buf.append("[src=").append(src); - buf.append(", dest=").append(dest); - buf.append(", destScene=").append(destScene); - return buf.append("]").toString(); - } - } -} diff --git a/src/java/com/threerings/miso/tools/xml/XMLSceneParser.java b/src/java/com/threerings/miso/tools/xml/XMLSceneParser.java deleted file mode 100644 index 2016b72ad..000000000 --- a/src/java/com/threerings/miso/tools/xml/XMLSceneParser.java +++ /dev/null @@ -1,330 +0,0 @@ -// -// $Id: XMLSceneParser.java,v 1.24 2001/11/08 03:04:44 mdb Exp $ - -package com.threerings.miso.scene.xml; - -import java.io.*; -import java.util.ArrayList; - -import org.xml.sax.*; - -import com.samskivert.util.*; -import com.samskivert.xml.SimpleParser; - -import com.threerings.media.tile.*; - -import com.threerings.miso.Log; -import com.threerings.miso.scene.*; -import com.threerings.miso.tile.MisoTile; - -/** - * Parse an XML scene description file and construct a scene object. - * Does not currently perform validation on the input XML stream, - * though the parsing code assumes the XML document is well-formed. - * - * @see XMLSceneWriter - */ -public class XMLSceneParser extends SimpleParser -{ - public XMLSceneParser (IsoSceneViewModel model, TileManager tilemgr) - { - _model = model; - _tilemgr = tilemgr; - } - - // documentation inherited - public void startElement ( - String uri, String localName, String qName, Attributes attributes) - { - if (qName.equals("layer")) { - _info.lnum = parseInt(attributes.getValue("lnum")); - - } else if (qName.equals("row")) { - _info.rownum = parseInt(attributes.getValue("rownum")); - _info.colstart = parseInt(attributes.getValue("colstart")); - } - } - - // documentation inherited - public void finishElement ( - String uri, String localName, String qName, String data) - { - if (qName.equals("name")) { - _info.scene.name = data; - - } else if (qName.equals("version")) { - int version = parseInt(data); - if (version < 0 || version > XMLSceneVersion.VERSION) { - Log.warning( - "Unrecognized scene file format version, will attempt " + - "to continue parsing file but your mileage may vary " + - "[version=" + version + - ", known_version=" + XMLSceneVersion.VERSION + "]."); - } - - } else if (qName.equals("locations")) { - int vals[] = StringUtil.parseIntArray(data); - addLocations(_info.scene.locations, vals); - - } else if (qName.equals("cluster")) { - int vals[] = StringUtil.parseIntArray(data); - _info.scene.clusters.add(toCluster(_info.scene.locations, vals)); - - } else if (qName.equals("portals")) { - String vals[] = StringUtil.parseStringArray(data); - addPortals(_info.scene.portals, _info.scene.locations, vals); - - } else if (qName.equals("entrance")) { - _info.scene.entrance = getEntrancePortal(data); - - } else if (qName.equals("row")) { - addTileRow(_info, data); - - } else if (qName.equals("scene")) { - // nothing for now - } - } - - /** - * Add the tiles described by the given data to the scene. - */ - protected void addTileRow (SceneInfo info, String data) - { - try { - if (info.lnum == MisoScene.LAYER_BASE) { - readRowData(info, data); - } else { - readSparseRowData(info, data); - } - - } catch (TileException te) { - Log.warning("Exception reading scene tile data " + - "[te=" + te + "]."); - } - } - - /** - * Given a string of comma-delimited tuples as (tileset id, tile - * id), populate the scene info tile array with tiles to suit. - * - * @param info the scene info. - * @param data the tile data. - */ - protected void readRowData (SceneInfo info, String data) - throws TileException - { - int[] vals = StringUtil.parseIntArray(data); - - // make sure we have a suitable number of tiles - int validLen = _model.scenewid * 2; - if (vals.length != validLen) { - Log.warning( - "Invalid number of tiles in full row data set, skipping set " + - "[rownum=" + info.rownum + ", len=" + vals.length + - ", valid_len=" + validLen + ", data=" + data + "]."); - return; - } - - // create the tile objects in the tile array - Tile[][] tiles = info.scene.getTiles(info.lnum); - for (int xx = 0; xx < vals.length; xx += 2) { - Tile tile = _tilemgr.getTile(vals[xx], vals[xx + 1]); - tiles[xx / 2][info.rownum] = tile; - } - } - - /** - * Given a string of comma-delimited tuples as (tileset id, tile - * id) and having previously set up the scene info object's - * rownum and colstart member data by - * retrieving the attribute values when the row - * element tag was noted, this method then proceeds to populate - * the info object's tile array with tiles to suit. - * - * @param info the scene info. - * @param data the tile data. - */ - protected void readSparseRowData (SceneInfo info, String data) - throws TileException - { - int[] vals = StringUtil.parseIntArray(data); - - // make sure we have a suitable number of tiles - if ((vals.length % 2) == 1) { - Log.warning( - "Odd number of tiles in sparse row data set, skipping set " + - "[rownum=" + info.rownum + ", colstart=" + info.colstart + - ", len=" + vals.length + "]."); - return; - } - - // create the tile objects in the tile array - Tile[][] tiles = info.scene.getTiles(info.lnum); - for (int xx = 0; xx < vals.length; xx += 2) { - Tile tile = _tilemgr.getTile(vals[xx], vals[xx + 1]); - int xidx = info.colstart + (xx / 2); - tiles[xidx][info.rownum] = tile; - } - } - - /** - * Given an array of integer values, return a Cluster - * object containing the location objects identified by location - * index number in the integer array. - * - * @param locs the locations list. - * @param vals the integer values. - * - * @return the cluster object. - */ - protected Cluster toCluster (ArrayList locs, int[] vals) - { - Cluster cluster = new Cluster(); - for (int ii = 0; ii < vals.length; ii++) { - cluster.add((Location)locs.get(vals[ii])); - } - return cluster; - } - - /** - * Given an array of integer values, add the Location - * objects represented therein to the given list, constructed from - * each successive triplet of values as (x, y, orientation) in the - * integer array. - * - * @param vals the integer values. - */ - protected void addLocations (ArrayList list, int[] vals) - { - // make sure we have a seemingly-appropriate number of points - if ((vals.length % 3) != 0) { - return; - } - - // read in all of the locations and add to the list - for (int ii = 0; ii < vals.length; ii += 3) { - Location loc = new Location( - vals[ii], vals[ii+1], vals[ii+2]); - list.add(loc); - } - } - - /** - * Given an array of string values, add the Portal - * objects constructed from each successive triplet of values as - * (locidx, portal name) in the array to the given portal list. - * The list of Location objects must have already - * been fully read previously. - * - *

This is something of a hack since we perhaps ought to parse - * the original String into its constituent components ourselves, - * but I'm unable to restrain myself from using the handy - * StringUtil.toString() method to take care of - * tokenizing things for us, so, there you have it. - * - * @param portals the portal list. - * @param locs the location list. - * @param vals the String values. - */ - protected void addPortals ( - ArrayList portals, ArrayList locs, String[] vals) - { - // make sure we have an appropriate number of values - if ((vals.length % 2) != 0) { - return; - } - - // read in all of the portals - for (int ii = 0; ii < vals.length; ii += 2) { - int locidx = parseInt(vals[ii]); - - // create the portal and add to the list - Portal portal = new Portal((Location)locs.get(locidx), vals[ii+1]); - portals.add(portal); - - // upgrade the corresponding location in the location list - locs.set(locidx, portal); - } - } - - /** - * Returns the default entrance portal for the scene, or - * null if an error occurs. - */ - protected Portal getEntrancePortal (String data) - { - ArrayList locs = _info.scene.locations; - int size = locs.size(); - int pidx = parseInt(data); - - if (size == 0 || pidx < 0 || pidx > size - 1) { - return null; - } - - return (Portal)locs.get(pidx); - } - - /** - * Parse the specified XML file and return a miso scene object with - * the data contained therein. - * - * @param path the full path to the scene description file. - * - * @return the scene object, or null if an error occurred. - */ - public EditableMisoScene loadScene (String path) throws IOException - { - return loadScene(getInputStream(path)); - } - - /** - * Parse the specified XML file and return a miso scene object with - * the data contained therein. - * - * @param stream the input stream from which the XML scene description - * can be read. - * - * @return the scene object, or null if an error occurred. - */ - public EditableMisoScene loadScene (InputStream stream) throws IOException - { - _info = new SceneInfo(); - parseStream(stream); - // place shadow tiles for any objects in the scene - _info.scene.generateAllObjectShadows(); - // return the final scene object - return _info.scene; - } - - /** The iso scene view data model. */ - protected IsoSceneViewModel _model; - - /** The tile manager object for use in constructing scenes. */ - protected TileManager _tilemgr; - - /** Temporary storage of scene info while parsing. */ - protected SceneInfo _info; - - /** - * A class to hold the information gathered while parsing. - */ - class SceneInfo - { - /** The scene populated with data while parsing. */ - public MisoSceneImpl scene; - - /** The current layer number being processed. */ - public int lnum; - - /** The current row number being processed. */ - public int rownum; - - /** The column at which the current row data begins. */ - public int colstart; - - public SceneInfo () - { - scene = new MisoSceneImpl(_model, null); - } - } -} diff --git a/src/java/com/threerings/miso/tools/xml/XMLSceneRepository.java b/src/java/com/threerings/miso/tools/xml/XMLSceneRepository.java deleted file mode 100644 index 59ba4bffd..000000000 --- a/src/java/com/threerings/miso/tools/xml/XMLSceneRepository.java +++ /dev/null @@ -1,116 +0,0 @@ -// -// $Id: XMLSceneRepository.java,v 1.15 2001/11/08 18:36:56 mdb Exp $ - -package com.threerings.miso.scene.xml; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; - -import com.samskivert.util.Config; -import com.threerings.media.tile.TileManager; -import com.threerings.miso.Log; -import com.threerings.miso.scene.IsoSceneViewModel; -import com.threerings.miso.scene.MisoScene; -import com.threerings.miso.scene.EditableMisoScene; -import com.threerings.miso.util.MisoUtil; - -/** - * The xml scene repository provides a mechanism for reading scenes - * from and writing scenes to XML files. These files are template - * scene files from which actual runtime game scenes will be - * constructed. - */ -public class XMLSceneRepository -{ - /** - * Initializes the xml scene repository. - * - * @param config the config object. - * @param tilemgr the tile manager object. - */ - public void init (Config config, TileManager tilemgr) - { - // keep these for later - _config = config; - _tilemgr = tilemgr; - - // get path-related information - _sceneRoot = _config.getValue(SCENEROOT_KEY, DEF_SCENEROOT); - - // create an iso scene view model detailing scene dimensions - _model = new IsoSceneViewModel(config); - - // construct the scene parser and writer objects for later use - _parser = new XMLSceneParser(_model, _tilemgr); - _writer = new XMLSceneWriter(_model); - } - - /** - * Loads and returns a miso scene object for the scene described in - * the specified XML file. - * - * @param path the full pathname to the file. - * - * @return the scene object. - */ - public EditableMisoScene loadScene (String path) throws IOException - { - Log.info("Loading scene [path=" + path + "]."); - return _parser.loadScene(path); - } - - /** - * Loads and returns a miso scene object for the scene described in - * the XML file available via the supplied input stream. - * - * @param path the full pathname to the file. - * - * @return the scene object. - */ - public EditableMisoScene loadScene (InputStream stream) - throws IOException - { - return _parser.loadScene(stream); - } - - /** - * Writes a scene to the specified file in the scene root - * directory in XML format. The filename should be relative to - * the scene root directory. - * - * @param scene the scene to save. - * @param path the full path of the file to write the scene to. - */ - public void saveScene (MisoScene scene, String path) throws IOException - { - Log.info("Saving scene [path=" + path + "]."); - _writer.saveScene(scene, path); - } - - /** The config object. */ - protected Config _config; - - /** The tile manager from which the scenes obtain their tiles. */ - protected TileManager _tilemgr; - - /** The root scene directory path. */ - protected String _sceneRoot; - - /** The iso scene view data model. */ - protected IsoSceneViewModel _model; - - /** The parser object for reading scenes from files. */ - protected XMLSceneParser _parser; - - /** The writer object for writing scenes to files. */ - protected XMLSceneWriter _writer; - - /** The config key for the root scene directory. */ - protected static final String SCENEROOT_KEY = - MisoUtil.CONFIG_KEY + ".sceneroot"; - - /** The default root scene directory path. */ - protected static final String DEF_SCENEROOT = "rsrc/scenes"; -} diff --git a/src/java/com/threerings/miso/tools/xml/XMLSceneVersion.java b/src/java/com/threerings/miso/tools/xml/XMLSceneVersion.java deleted file mode 100644 index 96910e42c..000000000 --- a/src/java/com/threerings/miso/tools/xml/XMLSceneVersion.java +++ /dev/null @@ -1,15 +0,0 @@ -// -// $Id: XMLSceneVersion.java,v 1.1 2001/09/21 02:30:35 mdb Exp $ - -package com.threerings.miso.scene.xml; - -/** - * This class tracks the version number of the XML scene file format. When - * modifications are made to the file format, the version number in this - * class should be increased. - */ -public class XMLSceneVersion -{ - /** The latest scene file format version. */ - public static int VERSION = 1; -} diff --git a/src/java/com/threerings/miso/tools/xml/XMLSceneWriter.java b/src/java/com/threerings/miso/tools/xml/XMLSceneWriter.java deleted file mode 100644 index 9375bab0f..000000000 --- a/src/java/com/threerings/miso/tools/xml/XMLSceneWriter.java +++ /dev/null @@ -1,368 +0,0 @@ -// -// $Id: XMLSceneWriter.java,v 1.18 2001/10/25 16:36:43 shaper Exp $ - -package com.threerings.miso.scene.xml; - -import java.io.*; -import java.util.List; - -import com.samskivert.util.ListUtil; - -import org.xml.sax.*; -import org.xml.sax.helpers.AttributesImpl; -import com.megginson.sax.DataWriter; - -import com.threerings.media.tile.Tile; - -import com.threerings.miso.Log; -import com.threerings.miso.scene.*; -import com.threerings.miso.tile.ShadowTile; - -/** - * The XMLSceneWriter writes a {@link - * com.threerings.miso.scene.MisoScene} object to an XML file. - * - *

The scene id is omitted as the scene id is assigned when the - * scene template is actually loaded into a server. Similarly, - * portals are named and bound to their target scene ids later. - */ -public class XMLSceneWriter extends DataWriter -{ - /** - * Construct an XMLSceneWriter object. - */ - public XMLSceneWriter (IsoSceneViewModel model) - { - _model = model; - setIndentStep(2); - } - - /** - * Write the scenes to the specified output file in XML format. - * - * @param fname the file to write to. - */ - public void saveScene (MisoScene scene, String fname) throws IOException - { - FileOutputStream fos = new FileOutputStream(fname); - setOutput(new OutputStreamWriter(fos)); - - try { - startDocument(); - - startElement("scene"); - - dataElement("name", scene.getName()); - dataElement("version", "" + XMLSceneVersion.VERSION); - dataElement("locations", getLocationData(scene)); - - startElement("clusters"); - writeClusters(scene); - endElement("clusters"); - - dataElement("portals", getPortalData(scene)); - - dataElement("entrance", getEntranceData(scene)); - - startElement("tiles"); - for (int lnum = 0; lnum < MisoScene.NUM_LAYERS; lnum++) { - writeLayer(scene, lnum); - } - endElement("tiles"); - - endElement("scene"); - endDocument(); - - } catch (SAXException saxe) { - Log.warning("Exception writing scene to file " + - "[scene=" + scene + ", fname=" + fname + - ", saxe=" + saxe + "]."); - } - } - - /** - * Output XML detailing the cluster objects. Clusters are - * described by a list of location object indexes that are - * contained within the cluster. - * - * @param scene the scene object. - */ - protected void writeClusters (MisoScene scene) throws SAXException - { - List clusters = scene.getClusters(); - List locs = scene.getLocations(); - - int size = clusters.size(); - for (int ii = 0; ii < size; ii++) { - Cluster cluster = (Cluster)clusters.get(ii); - List clusterlocs = cluster.getLocations(); - - StringBuffer buf = new StringBuffer(); - int clustersize = clusterlocs.size(); - for (int jj = 0; jj < clustersize; jj++) { - Location cloc = (Location)clusterlocs.get(jj); - buf.append(locs.indexOf(cloc)); - if (jj < clustersize - 1) { - buf.append(","); - } - } - - dataElement("cluster", buf.toString()); - } - } - - /** - * Output XML detailing the tiles at the specified layer in the - * given scene. The first layer is outputted in its entirety, - * whereas subsequent layers are outputted in a sparse notation - * that details each contiguous horizontal chunk of tiles in each - * row separately. - * - * @param scene the scene object. - * @param lnum the layer number. - */ - protected void writeLayer (MisoScene scene, int lnum) throws SAXException - { - AttributesImpl attrs = new AttributesImpl(); - attrs.addAttribute("", "lnum", "", "CDATA", "" + lnum); - - startElement("", "layer", "", attrs); - for (int yy = 0; yy < _model.scenehei; yy++) { - if (lnum == MisoScene.LAYER_BASE) { - writeTileRowData(scene, yy, lnum); - } else { - writeSparseTileRowData(scene, yy, lnum); - } - } - endElement("layer"); - } - - /** - * Return a string representation of the portals in the scene. Each - * portal is specified by a comma-delimited tuple of (location - * index, portal name) values. - * - * @param scene the scene object. - * - * @return the portals in String format. - */ - protected String getPortalData (MisoScene scene) - { - List locs = scene.getLocations(); - List portals = scene.getPortals(); - - StringBuffer buf = new StringBuffer(); - int size = portals.size(); - for (int ii = 0; ii < size; ii++) { - Portal portal = (Portal)portals.get(ii); - buf.append(locs.indexOf(portal)).append(","); - buf.append(portal.name); - if (ii < size - 1) { - buf.append(","); - } - } - - return buf.toString(); - } - - /** - * Return a string representation of the locations in the scene. - * Each location is specified by a comma-delimited triplet of - * (x, y, orientation) values. - * - * @return the locations in String format. - */ - protected String getLocationData (MisoScene scene) - { - List locs = scene.getLocations(); - - StringBuffer buf = new StringBuffer(); - int size = locs.size(); - for (int ii = 0; ii < size; ii++) { - Location loc = (Location)locs.get(ii); - buf.append(loc.x).append(","); - buf.append(loc.y).append(","); - buf.append(loc.orient); - if (ii < size - 1) { - buf.append(","); - } - } - - return buf.toString(); - } - - /** - * Returns a string detailing the location index of the default - * entrance portal for the scene, or "-1" if no - * default portal is specified or an error occurs. - */ - protected String getEntranceData (MisoScene scene) - { - List locs = scene.getLocations(); - Portal entrance = scene.getEntrance(); - - if (locs.size() == 0 || entrance == null) { - return "-1"; - } - - return String.valueOf(locs.indexOf(entrance)); - } - - /** - * Return a string representation of the tiles at the specified - * row and layer in the given scene. Only len tiles - * starting at column colstart are included in the - * string. - * - * @param scene the scene object. - * @param rownum the row number. - * @param lnum the layer number. - * @param colstart the first column of data. - * @param len the number of columns of data. - * - * @return the tile data in String format. - */ - protected String getTileData (MisoScene scene, int rownum, int lnum, - int colstart, int len) - { - StringBuffer buf = new StringBuffer(); - Tile[][][] tiles = scene.getTiles(); - - int numtiles = colstart + len; - for (int ii = colstart; ii < numtiles; ii++) { - Tile tile = tiles[lnum][ii][rownum]; - if (tile == null) { - Log.warning("Null tile [x=" + ii + ", rownum=" + rownum + - ", lnum=" + lnum + "]."); - continue; - } - - // replace shadow tiles with the default tile for the - // scene since they'll be re-created when the object's - // footprint is stamped into the scene upon its - // un-serialization - if (tile instanceof ShadowTile) { - tile = scene.getDefaultTile(); - } - - buf.append(tile.tsid).append(","); - buf.append(tile.tid); - if (ii != numtiles - 1) { - buf.append(","); - } - } - - return buf.toString(); - } - - /** - * Write the row data for a specified row in the scene tile array. - * - *

The row is written as a row element. Each - * tile is specified by a comma-delimited tuple of (tile set id, - * tile id) numbers, with the associated row number detailed in - * the rownum attribute of the row - * element. - * - * @param scene the scene object. - * @param rownum the row in the scene tile array. - * @param lnum the layer number in the scene tile array. - */ - protected void writeTileRowData (MisoScene scene, int rownum, int lnum) - throws SAXException - { - // set up the attributes for this row - AttributesImpl attrs = new AttributesImpl(); - attrs.addAttribute("", "rownum", "", "CDATA", "" + rownum); - - // output the full row data element - String data = getTileData(scene, rownum, lnum, 0, _model.scenewid); - dataElement("", "row", "", attrs, data); - } - - /** - * Utility routine used by writeSparseTileRowData to - * obtain the sets of contiguous tile sets in each row. - * - *

The search for contiguous tiles starts at the column - * specified in info[0] Results are returned in the info array as - * { colstart, len }. colstart is -1 if no - * tiles were found in the rest of the row. - * - * @param scene the scene object. - * @param rownum the row number. - * @param lnum the layer number. - * @param info the info array. - * - * @return true if any tiles were found, false if not. - */ - protected boolean getSparseColumn ( - MisoScene scene, int rownum, int lnum, int info[]) - { - Tile[][][] tiles = scene.getTiles(); - int start = -1, len = 0; - for (int xx = info[0]; xx < _model.scenewid; xx++) { - Tile tile = tiles[lnum][xx][rownum]; - if (tile == null) { - if (start == -1) { - continue; - } else { - break; - } - } - - if (start == -1) { - start = xx; - } - len++; - } - - info[0] = start; - info[1] = len; - - return (start != -1); - } - - /** - * Write the row data for a specified row in the scene tile array - * in sparse row format. - * - *

Sparse row format is identical to the format detailed in - * writeTileRowData() except that a separate - * row element is outputted for each contiguous set - * of tiles, with the starting column detailed in the - * colstart attribute of the row - * element. - * - * @param scene the scene object. - * @param rownum the row in the scene tile array. - * @param lnum the layer number in the scene tile array. - */ - protected void - writeSparseTileRowData (MisoScene scene, int rownum, int lnum) - throws SAXException - { - // set up the attributes for this row - AttributesImpl attrs = new AttributesImpl(); - attrs.addAttribute("", "rownum", "", "CDATA", "" + rownum); - attrs.addAttribute("", "colstart", "", "CDATA", "0"); - - int info[] = new int[] { 0, 0 }; - while (getSparseColumn(scene, rownum, lnum, info)) { - // update the colstart attribute - attrs.setAttribute(1, "", "colstart", "", "CDATA", "" + info[0]); - - // output the partial row data element - String data = getTileData(scene, rownum, lnum, info[0], info[1]); - dataElement("", "row", "", attrs, data); - - // update the colstart value - info[0] = info[0] + info[1]; - } - } - - /** The iso scene view data model. */ - protected IsoSceneViewModel _model; -} diff --git a/src/java/com/threerings/miso/util/MisoContext.java b/src/java/com/threerings/miso/util/MisoContext.java index 22ddedc8b..ee5c75d7a 100644 --- a/src/java/com/threerings/miso/util/MisoContext.java +++ b/src/java/com/threerings/miso/util/MisoContext.java @@ -1,5 +1,5 @@ // -// $Id: MisoContext.java,v 1.6 2001/11/02 03:09:10 shaper Exp $ +// $Id: MisoContext.java,v 1.7 2001/11/18 04:09:23 mdb Exp $ package com.threerings.miso.util; @@ -10,12 +10,6 @@ import com.threerings.media.tile.TileManager; public interface MisoContext extends Context { - /** - * Returns a reference to the image manager. This reference is - * valid for the lifetime of the application. - */ - public ImageManager getImageManager (); - /** * Returns a reference to the tile manager. This reference is * valid for the lifetime of the application. diff --git a/src/java/com/threerings/miso/util/MisoUtil.java b/src/java/com/threerings/miso/util/MisoUtil.java index 202ba35df..1d4677bc4 100644 --- a/src/java/com/threerings/miso/util/MisoUtil.java +++ b/src/java/com/threerings/miso/util/MisoUtil.java @@ -1,25 +1,11 @@ // -// $Id: MisoUtil.java,v 1.14 2001/11/08 03:04:45 mdb Exp $ +// $Id: MisoUtil.java,v 1.15 2001/11/18 04:09:23 mdb Exp $ package com.threerings.miso.util; -import java.awt.Frame; -import java.io.*; -import java.net.URL; -import java.net.MalformedURLException; - -import com.samskivert.util.*; - -import com.threerings.cast.CharacterManager; - -import com.threerings.media.ImageManager; -import com.threerings.media.tile.*; - +import java.io.IOException; +import com.samskivert.util.Config; import com.threerings.miso.Log; -import com.threerings.miso.scene.*; -import com.threerings.miso.scene.xml.XMLComponentRepository; -import com.threerings.miso.scene.xml.XMLSceneRepository; -import com.threerings.miso.tile.*; /** * The miso util class provides miscellaneous routines for @@ -75,107 +61,4 @@ public class MisoUtil return config; } - - /** - * Creates a CharacterManager object. - * - * @param config the Config object. - * @param imgmgr the ImageManager object. - * - * @return the new character manager object or null if an error - * occurred. - */ - public static CharacterManager createCharacterManager ( - Config config, ImageManager imgmgr) - { - XMLComponentRepository crepo = - new XMLComponentRepository(config, imgmgr); - CharacterManager charmgr = new CharacterManager(crepo); - charmgr.setCharacterClass(MisoCharacterSprite.class); - return charmgr; - } - - /** - * Creates an XMLSceneRepository object, reading - * the name of the class to instantiate from the config object. - * - * @param config the Config object. - * - * @return the new scene repository object or null if an error - * occurred. - */ - public static XMLSceneRepository createSceneRepository ( - Config config, TileManager tilemgr) - { - try { - XMLSceneRepository scenerepo = (XMLSceneRepository) - config.instantiateValue(SCENEREPO_KEY, DEF_SCENEREPO); - scenerepo.init(config, tilemgr); - return scenerepo; - - } catch (Exception e) { - Log.warning("Failed to instantiate scene repository " + - "[e=" + e + "]."); - return null; - } - } - - /** - * Creates a TileManager object. - * - * @param config the Config object. - * @param imgmgr the ImageManager object. - * - * @return the new tile manager object or null if an error occurred. - */ - public static TileManager createTileManager ( - Config config, ImageManager imgmgr) - { - TileSetRepository tsrepo = createTileSetRepository(config, imgmgr); - TileManager tilemgr = new TileManager(imgmgr); - tilemgr.setTileSetRepository(tsrepo); - return tilemgr; - } - - /** - * Creates a TileSetRepository object, reading the - * class name to instantiate from the config object. - * - * @param config the Config object. - * @param imgmgr the ImageManager object from which - * images are obtained. - * - * @return the new tile set repository or null if an error - * occurred. - */ - protected static TileSetRepository createTileSetRepository ( - Config config, ImageManager imgmgr) - { - TileSetRepository tsrepo = null; - try { - tsrepo = (TileSetRepository)config.instantiateValue( - TILESETREPO_KEY, DEF_TILESETREPO); - tsrepo.init(config, imgmgr); - - } catch (Exception e) { - Log.warning("Failed to instantiate tile set repository " + - "[e=" + e + "]."); - } - return tsrepo; - } - - /** The default scene repository class name. */ - protected static final String DEF_SCENEREPO = - XMLSceneRepository.class.getName(); - - /** The default tile set repository class name. */ - protected static final String DEF_TILESETREPO = - XMLTileSetRepository.class.getName(); - - /** The config key for the scene repository class. */ - protected static final String SCENEREPO_KEY = CONFIG_KEY + ".scenerepo"; - - /** The config key for the tile set repository class. */ - protected static final String TILESETREPO_KEY = - CONFIG_KEY + ".tilesetrepo"; } diff --git a/src/java/com/threerings/whirled/client/DefaultDisplaySceneFactory.java b/src/java/com/threerings/whirled/client/DefaultDisplaySceneFactory.java new file mode 100644 index 000000000..478754687 --- /dev/null +++ b/src/java/com/threerings/whirled/client/DefaultDisplaySceneFactory.java @@ -0,0 +1,20 @@ +// +// $Id: DefaultDisplaySceneFactory.java,v 1.1 2001/11/18 04:09:23 mdb Exp $ + +package com.threerings.whirled.client; + +import com.threerings.crowd.data.PlaceConfig; +import com.threerings.whirled.data.SceneModel; + +/** + * The default display scene factory creates {@link DisplaySceneImpl} + * instances. + */ +public class DefaultDisplaySceneFactory implements DisplaySceneFactory +{ + // documentation inherited + public DisplayScene createScene (SceneModel model, PlaceConfig config) + { + return new DisplaySceneImpl(model, config); + } +} diff --git a/tests/src/java/com/threerings/cast/builder/TestApp.java b/tests/src/java/com/threerings/cast/builder/TestApp.java index 831204bbd..c66ce9844 100644 --- a/tests/src/java/com/threerings/cast/builder/TestApp.java +++ b/tests/src/java/com/threerings/cast/builder/TestApp.java @@ -1,7 +1,7 @@ // -// $Id: TestApp.java,v 1.4 2001/11/08 02:58:23 mdb Exp $ +// $Id: TestApp.java,v 1.5 2001/11/18 04:09:20 mdb Exp $ -package com.threerings.cast.builder; +package com.threerings.cast.tools.builder; import java.io.IOException; import javax.swing.JFrame; @@ -10,11 +10,12 @@ import com.samskivert.util.Config; import com.samskivert.swing.util.SwingUtil; import com.threerings.resource.ResourceManager; -import com.threerings.media.ImageManager; import com.threerings.cast.Log; import com.threerings.cast.CharacterManager; +import com.threerings.cast.ComponentRepository; +import com.threerings.miso.scene.MisoCharacterSprite; import com.threerings.miso.util.MisoUtil; public class TestApp @@ -29,10 +30,11 @@ public class TestApp _config = MisoUtil.createConfig(); ResourceManager rsrcmgr = new ResourceManager("rsrc"); - _imgmgr = new ImageManager(rsrcmgr, _frame); - CharacterManager charmgr = - MisoUtil.createCharacterManager(_config, _imgmgr); + // TBD: sort out component repository + ComponentRepository crepo = null; + CharacterManager charmgr = new CharacterManager(crepo); + charmgr.setCharacterClass(MisoCharacterSprite.class); // initialize the frame ((TestFrame)_frame).init(charmgr); @@ -55,7 +57,4 @@ public class TestApp /** The config object. */ protected Config _config; - - /** The image manager object. */ - protected ImageManager _imgmgr; } diff --git a/tests/src/java/com/threerings/cast/builder/TestFrame.java b/tests/src/java/com/threerings/cast/builder/TestFrame.java index 0d9660018..635757c04 100644 --- a/tests/src/java/com/threerings/cast/builder/TestFrame.java +++ b/tests/src/java/com/threerings/cast/builder/TestFrame.java @@ -1,7 +1,7 @@ // -// $Id: TestFrame.java,v 1.2 2001/11/08 02:07:36 mdb Exp $ +// $Id: TestFrame.java,v 1.3 2001/11/18 04:09:20 mdb Exp $ -package com.threerings.cast.builder; +package com.threerings.cast.tools.builder; import javax.swing.JFrame; diff --git a/tests/src/java/com/threerings/miso/viewer/ViewerApp.java b/tests/src/java/com/threerings/miso/viewer/ViewerApp.java index 86df13221..e853c8bbf 100644 --- a/tests/src/java/com/threerings/miso/viewer/ViewerApp.java +++ b/tests/src/java/com/threerings/miso/viewer/ViewerApp.java @@ -1,23 +1,20 @@ // -// $Id: ViewerApp.java,v 1.14 2001/11/08 02:58:24 mdb Exp $ +// $Id: ViewerApp.java,v 1.15 2001/11/18 04:09:20 mdb Exp $ package com.threerings.miso.viewer; -import java.awt.Frame; import java.io.IOException; import com.samskivert.swing.util.SwingUtil; import com.samskivert.util.Config; import com.threerings.resource.ResourceManager; - -import com.threerings.media.ImageManager; +import com.threerings.media.sprite.SpriteManager; import com.threerings.media.tile.TileManager; +import com.threerings.cast.CharacterManager; import com.threerings.miso.Log; -import com.threerings.miso.scene.xml.XMLSceneRepository; -import com.threerings.miso.util.MisoUtil; -import com.threerings.miso.viewer.util.ViewerContext; +import com.threerings.miso.util.MisoContext; /** * The ViewerApp is a scene viewing application that allows for trying @@ -30,72 +27,48 @@ public class ViewerApp */ public ViewerApp (String[] args) { + // we don't need to configure anything + _config = new Config(); + _rsrcmgr = new ResourceManager("rsrc"); + _tilemgr = new TileManager(_rsrcmgr); + + // create the context object + MisoContext ctx = new ContextImpl(); + + // create the various managers + SpriteManager spritemgr = new SpriteManager(); +// XMLComponentRepository crepo = new XMLComponentRepository( +// ctx.getConfig(), ctx.getImageManager()); + CharacterManager charmgr = new CharacterManager(null); + // create and size the main application frame _frame = new ViewerFrame(); _frame.setSize(WIDTH, HEIGHT); - SwingUtil.centerWindow(_frame); - // create the handles on our various services - _config = MisoUtil.createConfig( - ViewerModel.CONFIG_KEY, "rsrc/config/miso/viewer"); - _model = createModel(_config, args); - _rsrcmgr = new ResourceManager("rsrc"); - _imgmgr = new ImageManager(_rsrcmgr, _frame); - _tilemgr = MisoUtil.createTileManager(_config, _imgmgr); - _screpo = MisoUtil.createSceneRepository(_config, _tilemgr); + // create our scene view panel + _panel = new ViewerSceneViewPanel(ctx, spritemgr, charmgr); + _frame.setPanel(_panel); - // create the context object - _ctx = new ViewerContextImpl(); - - // initialize the frame with the now-prepared context - ((ViewerFrame)_frame).init(_ctx); - } - - protected ViewerModel createModel (Config config, String args[]) - { - ViewerModel model = new ViewerModel(config); - if (args.length >= 1) { - model.scenefile = args[0]; - } - return model; + // determine whether or not the user specified a scene to be + // displayed or if we'll be using the default } /** - * The implementation of the ViewerContext interface that provides + * The implementation of the MisoContext interface that provides * handles to the config and manager objects that offer commonly used * services. */ - protected class ViewerContextImpl implements ViewerContext + protected class ContextImpl implements MisoContext { public Config getConfig () { return _config; } - public ImageManager getImageManager () - { - return _imgmgr; - } - public TileManager getTileManager () { return _tilemgr; } - - public ResourceManager getResourceManager () - { - return _rsrcmgr; - } - - public XMLSceneRepository getSceneRepository () - { - return _screpo; - } - - public ViewerModel getViewerModel () - { - return _model; - } } /** @@ -104,6 +77,7 @@ public class ViewerApp public void run () { _frame.pack(); + SwingUtil.centerWindow(_frame); _frame.show(); } @@ -126,21 +100,15 @@ public class ViewerApp /** The resource manager. */ protected ResourceManager _rsrcmgr; - /** The scene repository. */ - protected XMLSceneRepository _screpo; - /** The tile manager object. */ protected TileManager _tilemgr; - /** The image manager object. */ - protected ImageManager _imgmgr; - - /** The viewer data model. */ - protected ViewerModel _model; - - /** The context object. */ - protected ViewerContext _ctx; - /** The main application window. */ - protected Frame _frame; + protected ViewerFrame _frame; + + /** The main panel. */ + protected ViewerSceneViewPanel _panel; + + /** The default scene to load and display. */ + protected static final String DEF_SCENE = "rsrc/scenes/default.xml"; } diff --git a/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java b/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java index 811c28af4..ad3f7192c 100644 --- a/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java +++ b/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java @@ -1,39 +1,37 @@ // -// $Id: ViewerFrame.java,v 1.27 2001/11/02 03:09:10 shaper Exp $ +// $Id: ViewerFrame.java,v 1.28 2001/11/18 04:09:21 mdb Exp $ package com.threerings.miso.viewer; import javax.swing.JFrame; - -import com.threerings.media.ImageManager; -import com.threerings.media.sprite.SpriteManager; - -import com.threerings.miso.Log; -import com.threerings.miso.util.MisoUtil; -import com.threerings.miso.viewer.util.ViewerContext; +import javax.swing.JPanel; /** * The viewer frame is the main application window. */ public class ViewerFrame extends JFrame { + /** + * Creates a frame in which the viewer application can operate. + */ public ViewerFrame () { super("Scene Viewer"); - setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); } - /** - * Initialize the frame with the context object. - */ - public void init (ViewerContext ctx) + public void setPanel (JPanel panel) { - // create the various managers - SpriteManager spritemgr = new SpriteManager(); + // if we had an old panel, remove it + if (_panel != null) { + getContentPane().remove(_panel); + } - // add the main panel to the frame - getContentPane().add(new ViewerSceneViewPanel(ctx, spritemgr)); + // now add the new one + _panel = panel; + getContentPane().add(_panel); } + + protected JPanel _panel; } diff --git a/tests/src/java/com/threerings/miso/viewer/ViewerModel.java b/tests/src/java/com/threerings/miso/viewer/ViewerModel.java deleted file mode 100644 index 469c700c1..000000000 --- a/tests/src/java/com/threerings/miso/viewer/ViewerModel.java +++ /dev/null @@ -1,27 +0,0 @@ -// -// $Id: ViewerModel.java,v 1.2 2001/09/05 00:45:27 shaper Exp $ - -package com.threerings.miso.viewer; - -import com.samskivert.util.Config; - -public class ViewerModel -{ - /** The config key prefix for miso viewer properties. */ - public static final String CONFIG_KEY = "miso-viewer"; - - /** The filename of the scene to display. */ - public String scenefile; - - public ViewerModel (Config config) - { - scenefile = config.getValue(SCENE_KEY, DEF_SCENE); - } - - /** The config key to obtain the default scene filename. */ - protected static final String SCENE_KEY = - CONFIG_KEY + ".default_scene"; - - /** The default scene to load and display. */ - protected static final String DEF_SCENE = "rsrc/scenes/default.xml"; -} diff --git a/tests/src/java/com/threerings/miso/viewer/ViewerSceneViewPanel.java b/tests/src/java/com/threerings/miso/viewer/ViewerSceneViewPanel.java index 6ba2fcd71..d92c8d8be 100644 --- a/tests/src/java/com/threerings/miso/viewer/ViewerSceneViewPanel.java +++ b/tests/src/java/com/threerings/miso/viewer/ViewerSceneViewPanel.java @@ -1,13 +1,11 @@ // -// $Id: ViewerSceneViewPanel.java,v 1.30 2001/11/08 02:58:24 mdb Exp $ +// $Id: ViewerSceneViewPanel.java,v 1.31 2001/11/18 04:09:21 mdb Exp $ package com.threerings.miso.viewer; import java.awt.*; -import java.awt.event.*; -import java.io.IOException; -import java.io.InputStream; -import javax.swing.JPanel; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import com.samskivert.util.Config; @@ -15,19 +13,22 @@ import com.threerings.cast.CharacterDescriptor; import com.threerings.cast.CharacterManager; import com.threerings.cast.util.CastUtil; -import com.threerings.media.ImageManager; -import com.threerings.media.sprite.*; -import com.threerings.media.tile.TileManager; +import com.threerings.media.sprite.AnimationManager; +import com.threerings.media.sprite.LineSegmentPath; +import com.threerings.media.sprite.PathCompletedEvent; +import com.threerings.media.sprite.SpriteEvent; +import com.threerings.media.sprite.SpriteManager; +import com.threerings.media.sprite.SpriteObserver; + import com.threerings.media.util.RandomUtil; import com.threerings.media.util.PerformanceMonitor; import com.threerings.media.util.PerformanceObserver; import com.threerings.miso.Log; -import com.threerings.miso.scene.*; -import com.threerings.miso.scene.xml.XMLSceneRepository; +import com.threerings.miso.scene.MisoCharacterSprite; +import com.threerings.miso.scene.SceneViewPanel; import com.threerings.miso.scene.util.IsoUtil; -import com.threerings.miso.util.*; -import com.threerings.miso.viewer.util.ViewerContext; +import com.threerings.miso.util.MisoContext; public class ViewerSceneViewPanel extends SceneViewPanel implements PerformanceObserver, SpriteObserver @@ -35,21 +36,16 @@ public class ViewerSceneViewPanel extends SceneViewPanel /** * Construct the panel and initialize it with a context. */ - public ViewerSceneViewPanel (ViewerContext ctx, SpriteManager spritemgr) + public ViewerSceneViewPanel (MisoContext ctx, SpriteManager spritemgr, + CharacterManager charmgr) { super(ctx.getConfig(), spritemgr); - _ctx = ctx; - // create an animation manager for this panel _animmgr = new AnimationManager(spritemgr, this); - // load up the initial scene - prepareStartingScene(); - - // construct the character manager from which we obtain sprites - CharacterManager charmgr = MisoUtil.createCharacterManager( - ctx.getConfig(), ctx.getImageManager()); + // configure the character manager from which we obtain sprites + charmgr.setCharacterClass(MisoCharacterSprite.class); // create the character descriptors _descUser = CastUtil.getRandomDescriptor(charmgr); @@ -105,24 +101,6 @@ public class ViewerSceneViewPanel extends SceneViewPanel } } - /** - * Load and set up the starting scene for display. - */ - protected void prepareStartingScene () - { - ViewerModel model = _ctx.getViewerModel(); - try { - XMLSceneRepository screpo = _ctx.getSceneRepository(); - InputStream scin = _ctx.getResourceManager(). - getResource(model.scenefile); - _view.setScene(screpo.loadScene(scin)); - - } catch (IOException ioe) { - Log.warning("Exception loading scene [fname=" + model.scenefile + - ", ioe=" + ioe + "]."); - } - } - // documentation inherited public void paintComponent (Graphics g) { @@ -162,14 +140,14 @@ public class ViewerSceneViewPanel extends SceneViewPanel protected boolean createPath (MisoCharacterSprite s, int x, int y) { // get the path from here to there - Path path = _view.getPath(s, x, y); + LineSegmentPath path = (LineSegmentPath)_view.getPath(s, x, y); if (path == null) { s.cancelMove(); return false; } // start the sprite moving along the path - ((LineSegmentPath)path).setVelocity(100f/1000f); + path.setVelocity(100f/1000f); s.move(path); return true; } @@ -218,7 +196,4 @@ public class ViewerSceneViewPanel extends SceneViewPanel /** The test sprites that meander about aimlessly. */ protected MisoCharacterSprite _decoys[]; - - /** The context object. */ - protected ViewerContext _ctx; } diff --git a/tests/src/java/com/threerings/miso/viewer/util/ViewerContext.java b/tests/src/java/com/threerings/miso/viewer/util/ViewerContext.java deleted file mode 100644 index 77bb76450..000000000 --- a/tests/src/java/com/threerings/miso/viewer/util/ViewerContext.java +++ /dev/null @@ -1,34 +0,0 @@ -// -// $Id: ViewerContext.java,v 1.6 2001/11/08 02:58:24 mdb Exp $ - -package com.threerings.miso.viewer.util; - -import com.samskivert.util.Context; - -import com.threerings.resource.ResourceManager; - -import com.threerings.miso.scene.xml.XMLSceneRepository; -import com.threerings.miso.util.MisoContext; -import com.threerings.miso.viewer.ViewerModel; - -/** - * A mix-in interface that combines the MisoContext and Context - * interfaces to provide an interface with the best of both worlds. - */ -public interface ViewerContext extends MisoContext, Context -{ - /** - * Returns the resource manager via which we can load resources. - */ - public ResourceManager getResourceManager (); - - /** - * Returns the scene repository that we can use to get scenes. - */ - public XMLSceneRepository getSceneRepository (); - - /** - * Returns the viewer data model. - */ - public ViewerModel getViewerModel (); -} diff --git a/tests/src/java/com/threerings/whirled/DummyClientSceneRepository.java b/tests/src/java/com/threerings/whirled/DummyClientSceneRepository.java index 936e96369..b1bcd7cdd 100644 --- a/tests/src/java/com/threerings/whirled/DummyClientSceneRepository.java +++ b/tests/src/java/com/threerings/whirled/DummyClientSceneRepository.java @@ -1,5 +1,5 @@ // -// $Id: DummyClientSceneRepository.java,v 1.4 2001/11/08 02:57:14 mdb Exp $ +// $Id: DummyClientSceneRepository.java,v 1.5 2001/11/18 04:09:21 mdb Exp $ package com.threerings.whirled; @@ -7,8 +7,7 @@ import java.io.IOException; import com.threerings.whirled.Log; import com.threerings.whirled.client.persist.SceneRepository; -import com.threerings.whirled.data.DummyScene; -import com.threerings.whirled.data.Scene; +import com.threerings.whirled.data.SceneModel; import com.threerings.whirled.util.NoSuchSceneException; /** @@ -19,15 +18,15 @@ import com.threerings.whirled.util.NoSuchSceneException; public class DummyClientSceneRepository implements SceneRepository { // documentation inherited - public Scene loadScene (int sceneId) + public SceneModel loadSceneModel (int sceneId) throws IOException, NoSuchSceneException { - Log.info("Creating dummy scene [id=" + sceneId + "]."); - return new DummyScene(sceneId); + Log.info("Creating dummy scene model [id=" + sceneId + "]."); + return new SceneModel(); } // documentation inherited - public void updateScene (Scene scene) + public void updateSceneModel (SceneModel scene) throws IOException { // nothing doing diff --git a/tests/src/java/com/threerings/whirled/TestClient.java b/tests/src/java/com/threerings/whirled/TestClient.java index 693f9c0aa..4d467c508 100644 --- a/tests/src/java/com/threerings/whirled/TestClient.java +++ b/tests/src/java/com/threerings/whirled/TestClient.java @@ -1,5 +1,5 @@ // -// $Id: TestClient.java,v 1.7 2001/11/08 02:07:36 mdb Exp $ +// $Id: TestClient.java,v 1.8 2001/11/18 04:09:21 mdb Exp $ package com.threerings.whirled; @@ -15,6 +15,7 @@ import com.threerings.crowd.client.*; import com.threerings.crowd.data.PlaceObject; import com.threerings.whirled.client.SceneDirector; +import com.threerings.whirled.client.DefaultDisplaySceneFactory; import com.threerings.whirled.client.persist.SceneRepository; import com.threerings.whirled.util.WhirledContext; @@ -31,7 +32,8 @@ public class TestClient _client = new Client(creds, this); _screp = new DummyClientSceneRepository(); _occmgr = new OccupantManager(_ctx); - _scdir = new SceneDirector(_ctx, _screp); + _scdir = new SceneDirector( + _ctx, _screp, new DefaultDisplaySceneFactory()); // we want to know about logon/logoff _client.addObserver(this);