Widened and genericized

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@528 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Charlie Groves
2008-06-06 21:16:45 +00:00
parent 7d613a999a
commit 70ef6a644a
@@ -21,40 +21,37 @@
package com.threerings.media.tile; package com.threerings.media.tile;
import java.lang.ref.SoftReference; import static com.threerings.media.Log.log;
import java.util.HashMap; import java.lang.ref.SoftReference;
import java.util.Map;
import com.google.common.collect.Maps;
import com.samskivert.io.PersistenceException; import com.samskivert.io.PersistenceException;
import com.threerings.media.image.ImageManager; import com.threerings.media.image.ImageManager;
import static com.threerings.media.Log.log;
/** /**
* The tile manager provides a simplified interface for retrieving and * The tile manager provides a simplified interface for retrieving and caching tiles. Tiles can be
* caching tiles. Tiles can be loaded in two different ways. An * loaded in two different ways. An application can load a tileset by hand, specifying the path to
* application can load a tileset by hand, specifying the path to the * the tileset image and all of the tileset metadata necessary for extracting the image tiles, or
* tileset image and all of the tileset metadata necessary for extracting * it can provide a tileset repository which loads up tilesets using whatever repository mechanism
* the image tiles, or it can provide a tileset repository which loads up * is implemented by the supplied repository. In the latter case, tilesets are loaded by a unique
* tilesets using whatever repository mechanism is implemented by the * identifier.
* supplied repository. In the latter case, tilesets are loaded by a *
* unique identifier. * <p> Loading tilesets by hand is intended for things like toolbar icons or games with a single
* * set of tiles (think Stratego, for example). Loading tilesets from a repository supports games
* <p> Loading tilesets by hand is intended for things like toolbar icons * with vast numbers of tiles to which more tiles may be added on the fly (think the tiles for an
* or games with a single set of tiles (think Stratego, for example). * isometric-display graphical MUD).
* Loading tilesets from a repository supports games with vast numbers of
* tiles to which more tiles may be added on the fly (think the tiles for
* an isometric-display graphical MUD).
*/ */
public class TileManager public class TileManager
{ {
/** /**
* Creates a tile manager and provides it with a reference to the * Creates a tile manager and provides it with a reference to the image manager from which it
* image manager from which it will load tileset images. * will load tileset images.
* *
* @param imgr the image manager via which the tile manager will * @param imgr the image manager via which the tile manager will decode and cache images.
* decode and cache images.
*/ */
public TileManager (ImageManager imgr) public TileManager (ImageManager imgr)
{ {
@@ -63,8 +60,7 @@ public class TileManager
} }
/** /**
* Loads up a tileset from the specified image with the specified * Loads up a tileset from the specified image with the specified metadata parameters.
* metadata parameters.
*/ */
public UniformTileSet loadTileSet (String imgPath, int width, int height) public UniformTileSet loadTileSet (String imgPath, int width, int height)
{ {
@@ -72,14 +68,12 @@ public class TileManager
} }
/** /**
* Loads up a tileset from the specified image (located in the * Loads up a tileset from the specified image (located in the specified resource set) with
* specified resource set) with the specified metadata parameters. * the specified metadata parameters.
*/ */
public UniformTileSet loadTileSet ( public UniformTileSet loadTileSet (String rset, String imgPath, int width, int height)
String rset, String imgPath, int width, int height)
{ {
return loadTileSet( return loadTileSet(getImageProvider(rset), rset, imgPath, width, height);
getImageProvider(rset), rset, imgPath, width, height);
} }
/** /**
@@ -88,8 +82,7 @@ public class TileManager
ImageProvider improv, String improvKey, String imgPath, ImageProvider improv, String improvKey, String imgPath,
int width, int height) int width, int height)
{ {
UniformTileSet uts = loadCachedTileSet( UniformTileSet uts = loadCachedTileSet(improvKey, imgPath, width, height);
improvKey, imgPath, width, height);
uts.setImageProvider(improv); uts.setImageProvider(improv);
return uts; return uts;
} }
@@ -106,20 +99,19 @@ public class TileManager
/** /**
* Used to load and cache tilesets loaded via {@link #loadTileSet}. * Used to load and cache tilesets loaded via {@link #loadTileSet}.
*/ */
protected UniformTileSet loadCachedTileSet ( protected UniformTileSet loadCachedTileSet (String bundle, String imgPath, int width,
String bundle, String imgPath, int width, int height) int height)
{ {
String key = bundle + "::" + imgPath; String key = bundle + "::" + imgPath;
SoftReference ref = (SoftReference) _handcache.get(key); SoftReference<UniformTileSet> ref = _handcache.get(key);
UniformTileSet uts = (ref == null) ? null UniformTileSet uts = (ref == null) ? null : ref.get();
: (UniformTileSet) ref.get();
if (uts == null) { if (uts == null) {
uts = new UniformTileSet(); uts = new UniformTileSet();
uts.setImageProvider(_defaultProvider); uts.setImageProvider(_defaultProvider);
uts.setImagePath(imgPath); uts.setImagePath(imgPath);
uts.setWidth(width); uts.setWidth(width);
uts.setHeight(height); uts.setHeight(height);
_handcache.put(key, new SoftReference(uts)); _handcache.put(key, new SoftReference<UniformTileSet>(uts));
} }
return uts; return uts;
} }
@@ -129,12 +121,12 @@ public class TileManager
*/ */
public void clearCache () public void clearCache ()
{ {
_handcache = new HashMap(); _handcache = Maps.newHashMap();
} }
/** /**
* Sets the tileset repository that will be used by the tile manager * Sets the tileset repository that will be used by the tile manager when tiles are requested
* when tiles are requested by tileset id. * by tileset id.
*/ */
public void setTileSetRepository (TileSetRepository setrep) public void setTileSetRepository (TileSetRepository setrep)
{ {
@@ -150,15 +142,13 @@ public class TileManager
} }
/** /**
* Returns the tileset with the specified id. Tilesets are fetched * Returns the tileset with the specified id. Tilesets are fetched from the tileset repository
* from the tileset repository supplied via {@link * supplied via {@link #setTileSetRepository}, and are subsequently cached.
* #setTileSetRepository}, and are subsequently cached. *
*
* @param tileSetId the unique identifier for the desired tileset. * @param tileSetId the unique identifier for the desired tileset.
* *
* @exception NoSuchTileSetException thrown if no tileset exists with * @exception NoSuchTileSetException thrown if no tileset exists with the specified id or if
* the specified id or if an underlying error occurs with the tileset * an underlying error occurs with the tileset repository's persistence mechanism.
* repository's persistence mechanism.
*/ */
public TileSet getTileSet (int tileSetId) public TileSet getTileSet (int tileSetId)
throws NoSuchTileSetException throws NoSuchTileSetException
@@ -171,17 +161,16 @@ public class TileManager
try { try {
return _setrep.getTileSet(tileSetId); return _setrep.getTileSet(tileSetId);
} catch (PersistenceException pe) { } catch (PersistenceException pe) {
log.warning("Failure loading tileset [id=" + tileSetId + log.warning("Failure loading tileset", "id", tileSetId, "error", pe);
", error=" + pe + "].");
throw new NoSuchTileSetException(tileSetId); throw new NoSuchTileSetException(tileSetId);
} }
} }
/** /**
* Returns the tileset with the specified name. * Returns the tileset with the specified name.
* *
* @throws NoSuchTileSetException if no tileset with the specified * @throws NoSuchTileSetException if no tileset with the specified name is available via our
* name is available via our configured tile set repository. * configured tile set repository.
*/ */
public TileSet getTileSet (String name) public TileSet getTileSet (String name)
throws NoSuchTileSetException throws NoSuchTileSetException
@@ -194,45 +183,40 @@ public class TileManager
try { try {
return _setrep.getTileSet(name); return _setrep.getTileSet(name);
} catch (PersistenceException pe) { } catch (PersistenceException pe) {
log.warning("Failure loading tileset [name=" + name + log.warning("Failure loading tileset", "name", name, "error", pe);
", error=" + pe + "].");
throw new NoSuchTileSetException(name); throw new NoSuchTileSetException(name);
} }
} }
/** /**
* Returns the {@link Tile} object with the specified fully qualified * Returns the {@link Tile} object with the specified fully qualified tile id.
* tile id. *
*
* @see TileUtil#getFQTileId * @see TileUtil#getFQTileId
*/ */
public Tile getTile (int fqTileId) public Tile getTile (int fqTileId)
throws NoSuchTileSetException throws NoSuchTileSetException
{ {
return getTile(TileUtil.getTileSetId(fqTileId), return getTile(TileUtil.getTileSetId(fqTileId), TileUtil.getTileIndex(fqTileId), null);
TileUtil.getTileIndex(fqTileId), null);
} }
/** /**
* Returns the {@link Tile} object with the specified fully qualified * Returns the {@link Tile} object with the specified fully qualified tile id. The supplied
* tile id. The supplied colorizer will be used to recolor the tile. * colorizer will be used to recolor the tile.
* *
* @see TileUtil#getFQTileId * @see TileUtil#getFQTileId
*/ */
public Tile getTile (int fqTileId, TileSet.Colorizer rizer) public Tile getTile (int fqTileId, TileSet.Colorizer rizer)
throws NoSuchTileSetException throws NoSuchTileSetException
{ {
return getTile(TileUtil.getTileSetId(fqTileId), return getTile(TileUtil.getTileSetId(fqTileId), TileUtil.getTileIndex(fqTileId), rizer);
TileUtil.getTileIndex(fqTileId), rizer);
} }
/** /**
* Returns the {@link Tile} object from the specified tileset at the * Returns the {@link Tile} object from the specified tileset at the specified index.
* specified index. *
*
* @param tileSetId the tileset id. * @param tileSetId the tileset id.
* @param tileIndex the index of the tile to be retrieved. * @param tileIndex the index of the tile to be retrieved.
* *
* @return the tile object. * @return the tile object.
*/ */
public Tile getTile (int tileSetId, int tileIndex, TileSet.Colorizer rizer) public Tile getTile (int tileSetId, int tileIndex, TileSet.Colorizer rizer)
@@ -246,7 +230,7 @@ public class TileManager
protected ImageManager _imgr; protected ImageManager _imgr;
/** A cache of tilesets that have been loaded by hand. */ /** A cache of tilesets that have been loaded by hand. */
protected HashMap _handcache = new HashMap(); protected Map<String, SoftReference<UniformTileSet>> _handcache = Maps.newHashMap();
/** The tile set repository. */ /** The tile set repository. */
protected TileSetRepository _setrep; protected TileSetRepository _setrep;