Created an icon manager for loading icons from tilesets using a

configuration file to map icon set identifiers to tileset images and
metrics.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@924 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-02-03 22:43:54 +00:00
parent 1415319aab
commit 9b17dafd03
3 changed files with 214 additions and 0 deletions
@@ -0,0 +1,161 @@
//
// $Id: IconManager.java,v 1.1 2002/02/03 22:43:54 mdb Exp $
package com.threerings.media;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import com.samskivert.util.ConfigUtil;
import com.samskivert.util.StringUtil;
import com.threerings.media.tile.NoSuchTileException;
import com.threerings.media.tile.TileManager;
import com.threerings.media.tile.TileSet;
/**
* Manages the creation of icons from tileset images. The icon manager is
* provided with a configuration file, which maps icon set identifiers to
* uniform tilesets and provides the metric information for said tilesets.
* UI code can subsequently request icons from the icon manager based on
* icon set identifier and index.
*
* <p> The configuration might look like the following:
*
* <pre>
* arrows.path = /rsrc/media/icons/arrows.png
* arrows.metrics = 4, 20, 20 # 4 icons that are 20x20
*
* smileys.path = /rsrc/media/icons/smileys.png
* smileys.metrics = 10, 16, 16 # 16 icons that are 16x16
* </pre>
*
* A user could then request an <code>arrows</code> icon like so:
*
* <pre>
* ImageIcon icon = iconmgr.getIcon("arrows", 2);
* </pre>
*/
public class IconManager
{
/**
* Creates an icon manager that will obtain tilesets from the supplied
* tile manager and which will load its configuration information from
* the specified properties file.
*
* @param tmgr the tile manager to use when fetching tilesets.
* @param configPath the path (relative to the classpath) from which
* the icon manager configuration can be loaded.
*
* @exception IOException thrown if an error occurs loading the
* configuration file.
*/
public IconManager (TileManager tmgr, String configPath)
throws IOException
{
this(tmgr, ConfigUtil.loadProperties(configPath));
}
/**
* Creates an icon manager that will obtain tilesets from the supplied
* tile manager and which will read its configuration information from
* the supplied properties file.
*/
public IconManager (TileManager tmgr, Properties config)
{
// save these for later
_tilemgr = tmgr;
_config = config;
}
/**
* Fetches the icon with the specified index from the named icon set.
*/
public Icon getIcon (String iconSet, int index)
{
try {
// see if the tileset is already loaded
TileSet set = (TileSet)_icons.get(iconSet);
// load it up if not
if (set == null) {
String path = _config.getProperty(iconSet + PATH_SUFFIX);
if (StringUtil.blank(path)) {
throw new Exception("No path specified for icon set");
}
String metstr = _config.getProperty(iconSet + METRICS_SUFFIX);
if (StringUtil.blank(metstr)) {
throw new Exception("No metrics specified for icon set");
}
int[] metrics = StringUtil.parseIntArray(metstr);
if (metrics == null || metrics.length != 3) {
throw new Exception("Invalid icon set metrics " +
"[metrics=" + metstr + "]");
}
// load up the tileset
set = _tilemgr.loadTileSet(
path, metrics[0], metrics[1], metrics[2]);
// cache it
_icons.put(iconSet, set);
}
// fetch the appropriate image and create an image icon
return new ImageIcon(set.getTileImage(index));
} catch (NoSuchTileException nste) {
Log.warning("Unable to load icon [iconSet=" + iconSet +
", index=" + index + "]. Image index out of bounds.");
} catch (Exception e) {
Log.warning("Unable to load icon [iconSet=" + iconSet +
", index=" + index + ", error=" + e + "].");
}
return _errorIcon;
}
/** The tile manager we use to load tilesets. */
protected TileManager _tilemgr;
/** Our configuration information. */
protected Properties _config;
/** A cache of our icon tilesets. TODO: use a real LRU cache instead
* of a hash map. */
protected HashMap _icons = new HashMap();
/** The icon we return when we are unable to load a requested icon. */
protected Icon _errorIcon = new Icon() {
public void paintIcon (Component c, Graphics g, int x, int y) {
g.setColor(Color.black);
g.fillRect(x, y, 16, 16);
}
public int getIconWidth () {
return 16;
}
public int getIconHeight () {
return 16;
}
};
/** The suffix we append to an icon set name to obtain the tileset
* image path configuration parameter. */
protected static final String PATH_SUFFIX = ".path";
/** The suffix we append to an icon set name to obtain the tileset
* metrics configuration parameter. */
protected static final String METRICS_SUFFIX = ".metrics";
}