More work on character components. Revamped tile sets to allow them
to be constructed and used directly. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@581 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: ObjectTile.java,v 1.2 2001/10/12 00:38:15 shaper Exp $
|
||||
// $Id: ObjectTile.java,v 1.3 2001/11/01 01:40:42 shaper Exp $
|
||||
|
||||
package com.threerings.media.tile;
|
||||
|
||||
@@ -27,4 +27,12 @@ public class ObjectTile extends Tile
|
||||
this.baseWidth = baseWidth;
|
||||
this.baseHeight = baseHeight;
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void toString (StringBuffer buf)
|
||||
{
|
||||
super.toString(buf);
|
||||
buf.append(", baseWidth=").append(baseWidth);
|
||||
buf.append(", baseHeight=").append(baseHeight);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: Tile.java,v 1.16 2001/10/22 18:11:25 shaper Exp $
|
||||
// $Id: Tile.java,v 1.17 2001/11/01 01:40:42 shaper Exp $
|
||||
|
||||
package com.threerings.media.tile;
|
||||
|
||||
@@ -64,8 +64,19 @@ public class Tile
|
||||
public String toString ()
|
||||
{
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("[tsid=").append(tsid);
|
||||
buf.append(", tid=").append(tid);
|
||||
buf.append("[");
|
||||
toString(buf);
|
||||
return buf.append("]").toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* This should be overridden by derived classes (which should be sure
|
||||
* to call <code>super.toString()</code>) to append the derived class
|
||||
* specific tile information to the string buffer.
|
||||
*/
|
||||
public void toString (StringBuffer buf)
|
||||
{
|
||||
buf.append("tsid=").append(tsid);
|
||||
buf.append(", tid=").append(tid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: TileManager.java,v 1.18 2001/10/11 00:41:26 shaper Exp $
|
||||
// $Id: TileManager.java,v 1.19 2001/11/01 01:40:42 shaper Exp $
|
||||
|
||||
package com.threerings.media.tile;
|
||||
|
||||
@@ -14,19 +14,17 @@ import com.threerings.media.Log;
|
||||
/**
|
||||
* The tile manager provides a simplified interface for retrieving and
|
||||
* caching tiles.
|
||||
*
|
||||
* @see TileSetManager
|
||||
*/
|
||||
public class TileManager
|
||||
{
|
||||
/**
|
||||
* Initialize the tile manager.
|
||||
* Initializes the tile manager.
|
||||
*
|
||||
* @param tilesetmgr the tileset manager.
|
||||
* @param tilesetrepo the tile set repository.
|
||||
*/
|
||||
public TileManager (TileSetManager tilesetmgr)
|
||||
public TileManager (TileSetRepository tsrepo)
|
||||
{
|
||||
_tilesetmgr = tilesetmgr;
|
||||
_tsrepo = tsrepo;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,7 +51,7 @@ public class TileManager
|
||||
}
|
||||
|
||||
// retrieve the tile from the tileset
|
||||
tile = _tilesetmgr.getTile(tsid, tid);
|
||||
tile = _tsrepo.getTileSet(tsid).getTile(tid);
|
||||
if (tile != null) {
|
||||
// Log.info("Loaded tile into cache [tsid=" + tsid +
|
||||
// ", tid=" + tid + "].");
|
||||
@@ -64,16 +62,16 @@ public class TileManager
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tile set manager used by this tile manager.
|
||||
* Returns the tile set repository used by this tile manager.
|
||||
*/
|
||||
public TileSetManager getTileSetManager ()
|
||||
public TileSetRepository getTileSetRepository ()
|
||||
{
|
||||
return _tilesetmgr;
|
||||
return _tsrepo;
|
||||
}
|
||||
|
||||
/** Cache of tiles that have been requested thus far. */
|
||||
protected HashIntMap _tiles = new HashIntMap();
|
||||
|
||||
/** The tileset manager. */
|
||||
protected TileSetManager _tilesetmgr;
|
||||
/** The tile set repository. */
|
||||
protected TileSetRepository _tsrepo;
|
||||
}
|
||||
|
||||
@@ -1,48 +1,287 @@
|
||||
//
|
||||
// $Id: TileSet.java,v 1.17 2001/10/11 00:41:26 shaper Exp $
|
||||
// $Id: TileSet.java,v 1.18 2001/11/01 01:40:42 shaper Exp $
|
||||
|
||||
package com.threerings.media.tile;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.awt.Point;
|
||||
import java.awt.image.*;
|
||||
|
||||
import com.samskivert.util.HashIntMap;
|
||||
import com.samskivert.util.StringUtil;
|
||||
|
||||
import com.threerings.media.Log;
|
||||
import com.threerings.media.ImageManager;
|
||||
|
||||
/**
|
||||
* A tileset stores information on a single logical set of tiles. It
|
||||
* provides a clean interface for the {@link TileSetManager} to
|
||||
* retrieve individual tiles from the tileset.
|
||||
* A tile set stores information on a single logical set of tiles. It
|
||||
* provides a clean interface for the {@link TileManager} to retrieve
|
||||
* individual tiles from the tile set.
|
||||
*
|
||||
* <p> Tiles are referenced by their tile id. The tile id is
|
||||
* essentially the tile number, assuming the tile at the top-left of
|
||||
* the image is tile id 0 and tiles are numbered left to right, top to
|
||||
* bottom, in ascending order.
|
||||
*/
|
||||
public interface TileSet
|
||||
public class TileSet implements Cloneable
|
||||
{
|
||||
/**
|
||||
* Return the tileset identifier.
|
||||
* Constructs a tile set object with the given image manager as
|
||||
* the source for retrieving tile images.
|
||||
*/
|
||||
public int getId ();
|
||||
public TileSet (
|
||||
ImageManager imgmgr, int tsid, String name, String imgFile,
|
||||
int tileCount[], int rowWidth[], int rowHeight[],
|
||||
int numTiles, Point offsetPos, Point gapDist,
|
||||
boolean isObjectSet, HashIntMap objects)
|
||||
{
|
||||
_imgmgr = imgmgr;
|
||||
_tsid = tsid;
|
||||
_name = name;
|
||||
_imgFile = imgFile;
|
||||
_tileCount = tileCount;
|
||||
_rowWidth = rowWidth;
|
||||
_rowHeight = rowHeight;
|
||||
_numTiles = numTiles;
|
||||
_offsetPos = offsetPos;
|
||||
_gapDist = gapDist;
|
||||
_isObjectSet = isObjectSet;
|
||||
_objects = objects;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the tileset name.
|
||||
* Returns a new tile set that is a clone of this tile set with
|
||||
* the image file updated to reference the given file name.
|
||||
*/
|
||||
public String getName ();
|
||||
public TileSet clone (String imgFile)
|
||||
throws CloneNotSupportedException
|
||||
{
|
||||
TileSet dup = (TileSet)clone();
|
||||
dup.setImageFile(imgFile);
|
||||
return dup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of tiles in the tileset.
|
||||
* Returns the tile set identifier.
|
||||
*/
|
||||
public int getNumTiles ();
|
||||
public int getId ()
|
||||
{
|
||||
return _tsid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link Tile} object from this tileset corresponding
|
||||
* to the specified tile id, or <code>null</code> if no such tile
|
||||
* id exists. The tile image is retrieved from the given image
|
||||
* manager.
|
||||
* Returns the tile set name.
|
||||
*/
|
||||
public String getName ()
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of tiles in the tile set.
|
||||
*/
|
||||
public int getNumTiles ()
|
||||
{
|
||||
return _numTiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link Tile} object from this tile set
|
||||
* corresponding to the specified tile id, or null if an error
|
||||
* occurred.
|
||||
*
|
||||
* @param imgmgr the image manager.
|
||||
* @param tid the tile identifier.
|
||||
*
|
||||
* @return the tile object, or null if no such tile exists.
|
||||
* @return the tile object, or null if an error occurred.
|
||||
*/
|
||||
public Tile getTile (ImageManager imgmgr, int tid)
|
||||
throws NoSuchTileException;
|
||||
public Tile getTile (int tid)
|
||||
throws NoSuchTileException
|
||||
{
|
||||
if (_imgmgr == null) {
|
||||
Log.warning("No default image manager [tsid=" + _tsid +
|
||||
", tid=" + tid + "].");
|
||||
return null;
|
||||
}
|
||||
|
||||
// bail if there's no such tile
|
||||
if (tid < 0 || tid > (_numTiles - 1)) {
|
||||
throw new NoSuchTileException(tid);
|
||||
}
|
||||
|
||||
// create and populate the tile object
|
||||
Tile tile = createTile(tid);
|
||||
|
||||
// retrieve the tile image
|
||||
tile.img = getTileImage(_imgmgr, tile.tid);
|
||||
if (tile.img == null) {
|
||||
Log.warning("Null tile image [tile=" + tile + "].");
|
||||
}
|
||||
|
||||
// populate the tile's dimensions
|
||||
BufferedImage bimg = (BufferedImage)tile.img;
|
||||
tile.height = (short)bimg.getHeight();
|
||||
tile.width = (short)bimg.getWidth();
|
||||
|
||||
// allow sub-classes to fill in their tile information
|
||||
populateTile(tile);
|
||||
|
||||
return tile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the image file to be used as the source for the tile
|
||||
* images produced by this tile set.
|
||||
*/
|
||||
public void setImageFile (String imgFile)
|
||||
{
|
||||
_imgFile = imgFile;
|
||||
_imgTiles = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a string representation of the tileset information.
|
||||
*/
|
||||
public String toString ()
|
||||
{
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("[name=").append(_name);
|
||||
buf.append(", file=").append(_imgFile);
|
||||
buf.append(", tsid=").append(_tsid);
|
||||
buf.append(", numtiles=").append(_numTiles);
|
||||
return buf.append("]").toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct and return a new tile object for further population
|
||||
* with tile-specific information. Derived classes can override
|
||||
* this method to create their own sub-class of <code>Tile</code>.
|
||||
*
|
||||
* @param tid the tile id for the new tile.
|
||||
*
|
||||
* @return the new tile object.
|
||||
*/
|
||||
protected Tile createTile (int tid)
|
||||
{
|
||||
// construct an object tile if the tile set was specified as such
|
||||
if (_isObjectSet) {
|
||||
// default object dimensions to (1, 1)
|
||||
int wid = 1, hei = 1;
|
||||
|
||||
// retrieve object dimensions if known
|
||||
if (_objects != null) {
|
||||
int size[] = (int[])_objects.get(tid);
|
||||
if (size != null) {
|
||||
wid = size[0];
|
||||
hei = size[1];
|
||||
}
|
||||
}
|
||||
|
||||
return new ObjectTile(_tsid, tid, wid, hei);
|
||||
}
|
||||
|
||||
// construct a basic tile
|
||||
return new Tile(_tsid, tid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* <code>super.populateTile()</code>.
|
||||
*
|
||||
* @param tile the tile to populate.
|
||||
*/
|
||||
protected void populateTile (Tile tile)
|
||||
{
|
||||
// nothing for now
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the image corresponding to the specified tile id within
|
||||
* this tile set.
|
||||
*
|
||||
* @param imgmgr the image manager.
|
||||
* @param tid the tile id.
|
||||
*
|
||||
* @return the tile image.
|
||||
*/
|
||||
protected Image getTileImage (ImageManager imgmgr, int tid)
|
||||
{
|
||||
// load the full tile image if we don't already have it
|
||||
if (_imgTiles == null) {
|
||||
if ((_imgTiles = imgmgr.getImage(_imgFile)) == null) {
|
||||
Log.warning("Failed to retrieve full tileset image " +
|
||||
"[file=" + _imgFile + "].");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// find the row number containing the sought-after tile
|
||||
int ridx, tcount, ty, tx;
|
||||
ridx = tcount = 0;
|
||||
|
||||
// start tile image position at image start offset
|
||||
tx = _offsetPos.x;
|
||||
ty = _offsetPos.y;
|
||||
|
||||
while ((tcount += _tileCount[ridx]) < tid + 1) {
|
||||
// increment tile image position by row height and gap distance
|
||||
ty += (_rowHeight[ridx++] + _gapDist.y);
|
||||
}
|
||||
|
||||
// determine the horizontal index of this tile in the row
|
||||
int xidx = tid - (tcount - _tileCount[ridx]);
|
||||
|
||||
// final image x-position is based on tile width and gap distance
|
||||
tx += (xidx * (_rowWidth[ridx] + _gapDist.x));
|
||||
|
||||
// Log.info("Retrieving tile image [tid=" + tid + ", ridx=" +
|
||||
// ridx + ", xidx=" + xidx + ", tx=" + tx +
|
||||
// ", ty=" + ty + "].");
|
||||
|
||||
// crop the tile-sized image chunk from the full image
|
||||
return imgmgr.getImageCropped(
|
||||
_imgTiles, tx, ty, _rowWidth[ridx], _rowHeight[ridx]);
|
||||
}
|
||||
|
||||
/** The tileset name. */
|
||||
protected String _name;
|
||||
|
||||
/** The tileset unique identifier. */
|
||||
protected int _tsid;
|
||||
|
||||
/** The file containing the tile images. */
|
||||
protected String _imgFile;
|
||||
|
||||
/** The width of the tiles in each row in pixels. */
|
||||
protected int[] _rowWidth;
|
||||
|
||||
/** The height of the tiles in each row in pixels. */
|
||||
protected int[] _rowHeight;
|
||||
|
||||
/** The number of tiles in each row. */
|
||||
protected int[] _tileCount;
|
||||
|
||||
/** The number of tiles in the tileset. */
|
||||
protected int _numTiles;
|
||||
|
||||
/** Whether this set produces object tiles. */
|
||||
protected boolean _isObjectSet = false;
|
||||
|
||||
/** The offset distance (x, y) in pixels from the top-left of the
|
||||
* image to the start of the first tile image. */
|
||||
protected Point _offsetPos = new Point();
|
||||
|
||||
/** 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();
|
||||
|
||||
/** Mapping of object tile ids to object dimensions. */
|
||||
protected HashIntMap _objects;
|
||||
|
||||
/** The image containing all tile images for this set. */
|
||||
protected Image _imgTiles;
|
||||
|
||||
/** The default image manager for retrieving tile images. */
|
||||
protected ImageManager _imgmgr;
|
||||
}
|
||||
|
||||
@@ -1,222 +0,0 @@
|
||||
//
|
||||
// $Id: TileSetImpl.java,v 1.2 2001/10/12 16:36:58 shaper Exp $
|
||||
|
||||
package com.threerings.media.tile;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.awt.Point;
|
||||
import java.awt.image.*;
|
||||
|
||||
import com.samskivert.util.HashIntMap;
|
||||
import com.samskivert.util.StringUtil;
|
||||
|
||||
import com.threerings.media.Log;
|
||||
import com.threerings.media.ImageManager;
|
||||
|
||||
// documentation inherited
|
||||
public class TileSetImpl implements TileSet
|
||||
{
|
||||
/** The tileset name. */
|
||||
public String name;
|
||||
|
||||
/** The tileset unique identifier. */
|
||||
public int tsid;
|
||||
|
||||
/** The file containing the tile images. */
|
||||
public String imgFile;
|
||||
|
||||
/** The width of the tiles in each row in pixels. */
|
||||
public int[] rowWidth;
|
||||
|
||||
/** The height of the tiles in each row in pixels. */
|
||||
public int[] rowHeight;
|
||||
|
||||
/** The number of tiles in each row. */
|
||||
public int[] tileCount;
|
||||
|
||||
/** The number of tiles in the tileset. */
|
||||
public int numTiles;
|
||||
|
||||
/** Whether this set produces object tiles. */
|
||||
public boolean isObjectSet = false;
|
||||
|
||||
/**
|
||||
* The offset distance (x, y) in pixels from the top-left of the
|
||||
* image to the start of the first tile image.
|
||||
*/
|
||||
public Point offsetPos = new Point();
|
||||
|
||||
/**
|
||||
* The distance (x, y) in pixels between each tile in each row
|
||||
* horizontally, and between each row of tiles vertically.
|
||||
*/
|
||||
public Point gapDist = new Point();
|
||||
|
||||
// documentation inherited
|
||||
public int getId ()
|
||||
{
|
||||
return tsid;
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public String getName ()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public int getNumTiles ()
|
||||
{
|
||||
return numTiles;
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public Tile getTile (ImageManager imgmgr, int tid)
|
||||
throws NoSuchTileException
|
||||
{
|
||||
// bail if there's no such tile
|
||||
if (tid < 0 || tid > (numTiles - 1)) {
|
||||
throw new NoSuchTileException(tid);
|
||||
}
|
||||
|
||||
// create and populate the tile object
|
||||
Tile tile = createTile(tid);
|
||||
|
||||
// retrieve the tile image
|
||||
tile.img = getTileImage(imgmgr, tile.tid);
|
||||
if (tile.img == null) {
|
||||
Log.warning("Null tile image [tile=" + tile + "].");
|
||||
}
|
||||
|
||||
// populate the tile's dimensions
|
||||
BufferedImage bimg = (BufferedImage)tile.img;
|
||||
tile.height = (short)bimg.getHeight();
|
||||
tile.width = (short)bimg.getWidth();
|
||||
|
||||
// allow sub-classes to fill in their tile information
|
||||
populateTile(tile);
|
||||
|
||||
return tile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a string representation of the tileset information.
|
||||
*/
|
||||
public String toString ()
|
||||
{
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("[name=").append(name);
|
||||
buf.append(", file=").append(imgFile);
|
||||
buf.append(", tsid=").append(tsid);
|
||||
buf.append(", numtiles=").append(numTiles);
|
||||
return buf.append("]").toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct and return a new tile object for further population
|
||||
* with tile-specific information. Derived classes can override
|
||||
* this method to create their own sub-class of <code>Tile</code>.
|
||||
*
|
||||
* @param tid the tile id for the new tile.
|
||||
*
|
||||
* @return the new tile object.
|
||||
*/
|
||||
protected Tile createTile (int tid)
|
||||
{
|
||||
// construct an object tile if the tile set was specified as such
|
||||
if (isObjectSet) {
|
||||
// default object dimensions to (1, 1)
|
||||
int wid = 1, hei = 1;
|
||||
|
||||
// retrieve object dimensions if known
|
||||
if (_objects != null) {
|
||||
int size[] = (int[])_objects.get(tid);
|
||||
if (size != null) {
|
||||
wid = size[0];
|
||||
hei = size[1];
|
||||
}
|
||||
}
|
||||
|
||||
return new ObjectTile(tsid, tid, wid, hei);
|
||||
}
|
||||
|
||||
// construct a basic tile
|
||||
return new Tile(tsid, tid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* <code>super.populateTile()</code>.
|
||||
*
|
||||
* @param tile the tile to populate.
|
||||
*/
|
||||
protected void populateTile (Tile tile)
|
||||
{
|
||||
// nothing for now
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the image corresponding to the specified tile id within
|
||||
* this tile set.
|
||||
*
|
||||
* @param imgmgr the image manager.
|
||||
* @param tid the tile id.
|
||||
*
|
||||
* @return the tile image.
|
||||
*/
|
||||
protected Image getTileImage (ImageManager imgmgr, int tid)
|
||||
{
|
||||
// load the full tile image if we don't already have it
|
||||
if (_imgTiles == null) {
|
||||
if ((_imgTiles = imgmgr.getImage(imgFile)) == null) {
|
||||
Log.warning("Failed to retrieve full tileset image " +
|
||||
"[file=" + imgFile + "].");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// find the row number containing the sought-after tile
|
||||
int ridx, tcount, ty, tx;
|
||||
ridx = tcount = 0;
|
||||
|
||||
// start tile image position at image start offset
|
||||
tx = offsetPos.x;
|
||||
ty = offsetPos.y;
|
||||
|
||||
while ((tcount += tileCount[ridx]) < tid + 1) {
|
||||
// increment tile image position by row height and gap distance
|
||||
ty += (rowHeight[ridx++] + gapDist.y);
|
||||
}
|
||||
|
||||
// determine the horizontal index of this tile in the row
|
||||
int xidx = tid - (tcount - tileCount[ridx]);
|
||||
|
||||
// final image x-position is based on tile width and gap distance
|
||||
tx += (xidx * (rowWidth[ridx] + gapDist.x));
|
||||
|
||||
// Log.info("Retrieving tile image [tid=" + tid + ", ridx=" +
|
||||
// ridx + ", xidx=" + xidx + ", tx=" + tx +
|
||||
// ", ty=" + ty + "].");
|
||||
|
||||
// crop the tile-sized image chunk from the full image
|
||||
return imgmgr.getImageCropped(
|
||||
_imgTiles, tx, ty, rowWidth[ridx], rowHeight[ridx]);
|
||||
}
|
||||
|
||||
protected void addObjectInfo (int tid, int size[])
|
||||
{
|
||||
if (_objects == null) {
|
||||
_objects = new HashIntMap();
|
||||
}
|
||||
|
||||
_objects.put(tid, size);
|
||||
}
|
||||
|
||||
/** Mapping of object tile ids to object dimensions. */
|
||||
protected HashIntMap _objects;
|
||||
|
||||
/** The image containing all tile images for this set. */
|
||||
protected Image _imgTiles;
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
//
|
||||
// $Id: TileSetManager.java,v 1.14 2001/10/12 00:38:15 shaper Exp $
|
||||
|
||||
package com.threerings.media.tile;
|
||||
|
||||
import com.threerings.media.ImageManager;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* The <code>TileSetManager</code> provides tileset management
|
||||
* functionality intended for use by the <code>TileManager</code>. It
|
||||
* provides facilities for obtaining information about individual
|
||||
* tilesets, retrieving an list of all tilesets available, and
|
||||
* retrieving the image associated with a particular tile in a set.
|
||||
*
|
||||
* @see TileManager
|
||||
*/
|
||||
public interface TileSetManager
|
||||
{
|
||||
/**
|
||||
* Return an <code>Iterator</code> over all <code>TileSet</code>
|
||||
* objects available.
|
||||
*
|
||||
* @return the tileset iterator.
|
||||
*/
|
||||
public Iterator getTileSets ();
|
||||
|
||||
/**
|
||||
* Return the tileset object corresponding to the specified
|
||||
* tileset id, or null if the tileset is not found.
|
||||
*
|
||||
* @param tsid the tileset identifier.
|
||||
* @return the tileset object.
|
||||
*/
|
||||
public TileSet getTileSet (int tsid)
|
||||
throws NoSuchTileSetException;
|
||||
|
||||
/**
|
||||
* Return the tile object corresponding to the specified tileset
|
||||
* and tile id.
|
||||
*
|
||||
* @param tsid the tileset identifier.
|
||||
* @param tid the tile identifier.
|
||||
*
|
||||
* @return the tile object.
|
||||
*/
|
||||
public Tile getTile (int tsid, int tid)
|
||||
throws NoSuchTileSetException, NoSuchTileException;
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
//
|
||||
// $Id: TileSetManagerImpl.java,v 1.14 2001/10/12 00:38:15 shaper Exp $
|
||||
|
||||
package com.threerings.media.tile;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.io.*;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
|
||||
import com.samskivert.util.*;
|
||||
|
||||
import com.threerings.media.ImageManager;
|
||||
import com.threerings.media.Log;
|
||||
|
||||
public abstract class TileSetManagerImpl implements TileSetManager
|
||||
{
|
||||
/**
|
||||
* Initialize the tile set manager.
|
||||
*
|
||||
* @param config the config object.
|
||||
* @param imgmgr the image manager.
|
||||
*/
|
||||
public void init (Config config, ImageManager imgmgr)
|
||||
{
|
||||
_imgmgr = imgmgr;
|
||||
_config = config;
|
||||
}
|
||||
|
||||
// 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 getTileSets ()
|
||||
{
|
||||
return Collections.unmodifiableMap(_tilesets).values().iterator();
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public Tile getTile (int tsid, int tid)
|
||||
throws NoSuchTileSetException, NoSuchTileException
|
||||
{
|
||||
return getTileSet(tsid).getTile(_imgmgr, tid);
|
||||
}
|
||||
|
||||
/** The config object. */
|
||||
protected Config _config;
|
||||
|
||||
/** The image manager. */
|
||||
protected ImageManager _imgmgr;
|
||||
|
||||
/** The available tilesets keyed by tileset id. */
|
||||
protected HashIntMap _tilesets = new HashIntMap();
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
//
|
||||
// $Id: TileSetParser.java,v 1.7 2001/10/15 23:53:43 shaper Exp $
|
||||
// $Id: TileSetParser.java,v 1.8 2001/11/01 01:40:42 shaper Exp $
|
||||
|
||||
package com.threerings.media.tile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import com.samskivert.util.HashIntMap;
|
||||
|
||||
/**
|
||||
* The tile set parser interface is intended to be implemented by
|
||||
@@ -13,9 +14,10 @@ import java.util.List;
|
||||
public interface TileSetParser
|
||||
{
|
||||
/**
|
||||
* Read tileset description data from the specified file and
|
||||
* append {@link TileSet} objects constructed from the data to the
|
||||
* given list.
|
||||
* 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, List tilesets) throws IOException;
|
||||
public void loadTileSets (String fname, HashIntMap tilesets)
|
||||
throws IOException;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// $Id: TileSetRepository.java,v 1.1 2001/11/01 01:40:42 shaper Exp $
|
||||
|
||||
package com.threerings.media.tile;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import com.samskivert.util.Config;
|
||||
import com.threerings.media.ImageManager;
|
||||
|
||||
/**
|
||||
* The tile set repository interface should be implemented by classes
|
||||
* that provide access to tile sets keyed on their unique tile set
|
||||
* identifier.
|
||||
*/
|
||||
public interface TileSetRepository {
|
||||
|
||||
/**
|
||||
* Initializes the tile set repository.
|
||||
*/
|
||||
public void init (Config config, ImageManager imgmgr);
|
||||
|
||||
/**
|
||||
* Returns an iterator over all {@link TileSet} objects available.
|
||||
*/
|
||||
public Iterator enumerateTileSets ();
|
||||
|
||||
/**
|
||||
* Returns the {@link TileSet} with the specified unique tile set
|
||||
* identifier.
|
||||
*/
|
||||
public TileSet getTileSet (int tsid)
|
||||
throws NoSuchTileSetException;
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
//
|
||||
// $Id: XMLTileSetParser.java,v 1.19 2001/10/22 18:11:59 shaper Exp $
|
||||
// $Id: XMLTileSetParser.java,v 1.20 2001/11/01 01:40:42 shaper Exp $
|
||||
|
||||
package com.threerings.media.tile;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.*;
|
||||
import java.util.List;
|
||||
|
||||
import org.xml.sax.*;
|
||||
|
||||
@@ -13,6 +12,7 @@ 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
|
||||
@@ -20,39 +20,45 @@ import com.threerings.media.Log;
|
||||
* on the input XML stream, though the parsing code assumes the XML
|
||||
* document is well-formed.
|
||||
*/
|
||||
public class XMLTileSetParser extends SimpleParser
|
||||
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")) {
|
||||
// construct the new tile set
|
||||
_tset = createTileSet();
|
||||
|
||||
// note whether it contains object tiles
|
||||
// note whether the tile set contains object tiles
|
||||
String str = attributes.getValue("layer");
|
||||
_tset.isObjectSet =
|
||||
_info.isObjectSet =
|
||||
(str != null && str.toLowerCase().equals(LAYER_OBJECT));
|
||||
|
||||
// get the tile set id
|
||||
_tset.tsid = parseInt(attributes.getValue("tsid"));
|
||||
_info.tsid = parseInt(attributes.getValue("tsid"));
|
||||
|
||||
// get the tile set name
|
||||
str = attributes.getValue("name");
|
||||
_tset.name = (str == null) ? DEF_NAME : str;
|
||||
_info.name = (str == null) ? DEF_NAME : str;
|
||||
|
||||
} else if (qName.equals("object")) {
|
||||
// TODO: should we bother checking to make sure we only
|
||||
// see <object> tags while within an <objects> tag?
|
||||
// get the object info
|
||||
int tid = parseInt(attributes.getValue("tid"));
|
||||
int wid = parseInt(attributes.getValue("width"));
|
||||
int hei = parseInt(attributes.getValue("height"));
|
||||
|
||||
// add the object info to the tileset object hashtable
|
||||
_tset.addObjectInfo(tid, new int[] { wid, hei });
|
||||
if (_info.objects == null) {
|
||||
_info.objects = new HashIntMap();
|
||||
}
|
||||
_info.objects.put(tid, new int[] { wid, hei });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,38 +67,44 @@ public class XMLTileSetParser extends SimpleParser
|
||||
String uri, String localName, String qName, String data)
|
||||
{
|
||||
if (qName.equals("imagefile")) {
|
||||
_tset.imgFile = data;
|
||||
_info.imgFile = data;
|
||||
|
||||
} else if (qName.equals("rowwidth")) {
|
||||
_tset.rowWidth = StringUtil.parseIntArray(data);
|
||||
_info.rowWidth = StringUtil.parseIntArray(data);
|
||||
|
||||
} else if (qName.equals("rowheight")) {
|
||||
_tset.rowHeight = StringUtil.parseIntArray(data);
|
||||
_info.rowHeight = StringUtil.parseIntArray(data);
|
||||
|
||||
} else if (qName.equals("tilecount")) {
|
||||
_tset.tileCount = StringUtil.parseIntArray(data);
|
||||
_info.tileCount = StringUtil.parseIntArray(data);
|
||||
|
||||
// calculate the total number of tiles in the tileset
|
||||
for (int ii = 0; ii < _tset.tileCount.length; ii++) {
|
||||
_tset.numTiles += _tset.tileCount[ii];
|
||||
for (int ii = 0; ii < _info.tileCount.length; ii++) {
|
||||
_info.numTiles += _info.tileCount[ii];
|
||||
}
|
||||
|
||||
} else if (qName.equals("offsetpos")) {
|
||||
parsePoint(data, _tset.offsetPos);
|
||||
parsePoint(data, _info.offsetPos);
|
||||
|
||||
} else if (qName.equals("gapdist")) {
|
||||
parsePoint(data, _tset.gapDist);
|
||||
parsePoint(data, _info.gapDist);
|
||||
|
||||
} else if (qName.equals("tileset")) {
|
||||
// add the fully-read tileset to the list of tilesets
|
||||
_tilesets.add(_tset);
|
||||
// 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, List tilesets) throws IOException
|
||||
public void loadTileSets (String fname, HashIntMap tilesets)
|
||||
throws IOException
|
||||
{
|
||||
// save off tileset list for reference while parsing
|
||||
// save off the tileset hashtable
|
||||
_tilesets = tilesets;
|
||||
|
||||
try {
|
||||
@@ -100,6 +112,7 @@ public class XMLTileSetParser extends SimpleParser
|
||||
} catch (IOException ioe) {
|
||||
Log.warning("Exception parsing tile set descriptions " +
|
||||
"[ioe=" + ioe + "].");
|
||||
Log.logStackTrace(ioe);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,9 +127,12 @@ public class XMLTileSetParser extends SimpleParser
|
||||
* may override this method to create their own sub-classes of the
|
||||
* <code>TileSet</code> object.
|
||||
*/
|
||||
protected TileSetImpl createTileSet ()
|
||||
protected TileSet createTileSet ()
|
||||
{
|
||||
return new TileSetImpl();
|
||||
return new TileSet(
|
||||
_imgmgr, _info.tsid, _info.name, _info.imgFile, _info.tileCount,
|
||||
_info.rowWidth, _info.rowHeight, _info.numTiles, _info.offsetPos,
|
||||
_info.gapDist, _info.isObjectSet, _info.objects);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -133,6 +149,24 @@ public class XMLTileSetParser extends SimpleParser
|
||||
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 int numTiles;
|
||||
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";
|
||||
|
||||
@@ -140,8 +174,11 @@ public class XMLTileSetParser extends SimpleParser
|
||||
protected static final String LAYER_OBJECT = "object";
|
||||
|
||||
/** The tilesets constructed thus far. */
|
||||
protected List _tilesets;
|
||||
protected HashIntMap _tilesets;
|
||||
|
||||
/** The tile set populated while parsing. */
|
||||
protected TileSetImpl _tset;
|
||||
/** The tile set info populated while parsing. */
|
||||
protected TileSetInfo _info = new TileSetInfo();
|
||||
|
||||
/** The image manager. */
|
||||
protected ImageManager _imgmgr;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user