From 9b17dafd03e6c29adeb8161245466a340f3913aa Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Sun, 3 Feb 2002 22:43:54 +0000 Subject: [PATCH] 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 --- .../com/threerings/media/IconManager.java | 161 ++++++++++++++++++ tests/rsrc/config/media/iconmgr.properties | 7 + .../com/threerings/media/TestIconManager.java | 46 +++++ 3 files changed, 214 insertions(+) create mode 100644 src/java/com/threerings/media/IconManager.java create mode 100644 tests/rsrc/config/media/iconmgr.properties create mode 100644 tests/src/java/com/threerings/media/TestIconManager.java diff --git a/src/java/com/threerings/media/IconManager.java b/src/java/com/threerings/media/IconManager.java new file mode 100644 index 000000000..dd0ac1050 --- /dev/null +++ b/src/java/com/threerings/media/IconManager.java @@ -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. + * + *

The configuration might look like the following: + * + *

+ * 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
+ * 
+ * + * A user could then request an arrows icon like so: + * + *
+ * ImageIcon icon = iconmgr.getIcon("arrows", 2);
+ * 
+ */ +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"; +} diff --git a/tests/rsrc/config/media/iconmgr.properties b/tests/rsrc/config/media/iconmgr.properties new file mode 100644 index 000000000..aaf710e9c --- /dev/null +++ b/tests/rsrc/config/media/iconmgr.properties @@ -0,0 +1,7 @@ +# +# $Id: iconmgr.properties,v 1.1 2002/02/03 22:43:54 mdb Exp $ +# +# Test properties for the icon manager + +test.path = /media/miso/tiles/node-icons.png +test.metrics = 8, 16, 16 diff --git a/tests/src/java/com/threerings/media/TestIconManager.java b/tests/src/java/com/threerings/media/TestIconManager.java new file mode 100644 index 000000000..6a42806c7 --- /dev/null +++ b/tests/src/java/com/threerings/media/TestIconManager.java @@ -0,0 +1,46 @@ +// +// $Id: TestIconManager.java,v 1.1 2002/02/03 22:43:54 mdb Exp $ + +package com.threerings.media; + +import java.awt.BorderLayout; +import javax.swing.*; + +import com.samskivert.swing.HGroupLayout; +import com.samskivert.swing.util.SwingUtil; + +import com.threerings.resource.ResourceManager; +import com.threerings.media.tile.TileManager; + +/** + * Does something extraordinary. + */ +public class TestIconManager +{ + public static void main (String[] args) + { + try { + JFrame frame = new JFrame("TestIconManager"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + ResourceManager rmgr = new ResourceManager("rsrc", null, null); + ImageManager imgr = new ImageManager(rmgr, frame); + TileManager tmgr = new TileManager(imgr); + IconManager iconmgr = new IconManager( + tmgr, "rsrc/config/media/iconmgr.properties"); + + JPanel panel = new JPanel(new HGroupLayout()); + for (int i = 0; i < 8; i++) { + panel.add(new JButton(iconmgr.getIcon("test", i))); + } + + frame.getContentPane().add(panel, BorderLayout.CENTER); + frame.pack(); + SwingUtil.centerWindow(frame); + frame.show(); + + } catch (Exception e) { + e.printStackTrace(System.err); + } + } +}