Abstracted character-sprite-related activity to the cast package in

preparation for character component compositing and other
character-related antics.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@575 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2001-10-26 01:17:22 +00:00
parent cf51ce8a9c
commit d102a47f13
26 changed files with 923 additions and 307 deletions
@@ -0,0 +1,80 @@
//
// $Id: XMLComponentParser.java,v 1.1 2001/10/26 01:17:21 shaper Exp $
package com.threerings.cast;
import java.awt.Point;
import java.io.IOException;
import org.xml.sax.*;
import com.samskivert.util.HashIntMap;
import com.samskivert.util.StringUtil;
import com.samskivert.xml.SimpleParser;
public class XMLComponentParser extends SimpleParser
{
// documentation inherited
public void startElement (String uri, String localName,
String qName, Attributes attributes)
{
if (qName.equals("type")) {
// construct the component type object
ComponentType ct = new ComponentType();
// retrieve character attributes
ct.ctid = parseInt(attributes.getValue("ctid"));
ct.frameCount = parseInt(attributes.getValue("frames"));
ct.fps = parseInt(attributes.getValue("fps"));
parsePoint(attributes.getValue("origin"), ct.origin);
Log.info("Parsed component type [ct=" + ct + "].");
// save the component type
_types.put(ct.ctid, ct);
} else if (qName.equals("component")) {
// save off the component info
int cid = parseInt(attributes.getValue("cid"));
int ctid = parseInt(attributes.getValue("ctid"));
_components.put(cid, new Integer(ctid));
}
}
/**
* Loads the component descriptions in the given file into {@link
* ComponentType} and {@link CharacterComponent} objects in the
* given hashtables, respectively, keyed on the type and component
* unique id, also respectively.
*/
public void loadComponents (
String file, HashIntMap types, HashIntMap components)
throws IOException
{
// save off hashtables for reference while parsing
_types = types;
_components = components;
parseFile(file);
}
/**
* 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]);
}
/** The hashtable of component types gathered while parsing. */
protected HashIntMap _types;
/** The hashtable of character components gathered while parsing. */
protected HashIntMap _components;
}
@@ -0,0 +1,159 @@
//
// $Id: XMLComponentRepository.java,v 1.1 2001/10/26 01:17:22 shaper Exp $
package com.threerings.miso.scene.xml;
import java.io.IOException;
import java.util.Iterator;
import com.samskivert.util.Config;
import com.samskivert.util.HashIntMap;
import com.threerings.cast.*;
import com.threerings.cast.CharacterComponent.ComponentFrames;
import com.threerings.media.tile.TileManager;
import com.threerings.miso.Log;
import com.threerings.miso.util.MisoUtil;
/**
* The xml file component repository provides access to character
* components obtained from component descriptions stored in an XML
* file.
*/
public class XMLFileComponentRepository implements ComponentRepository
{
/**
* Constructs an xml file component repository.
*/
public XMLFileComponentRepository (Config config, TileManager tilemgr)
{
// save off our objects
_tilemgr = tilemgr;
// load component types and components
String file = config.getValue(COMPFILE_KEY, DEFAULT_COMPFILE);
try {
new XMLComponentParser().loadComponents(
file, _types, _components);
} catch (IOException ioe) {
Log.warning("Exception loading component descriptions " +
"[ioe=" + ioe + "].");
}
}
// documentation inherited
public CharacterComponent getComponent (int cid)
throws NoSuchComponentException, NoSuchComponentTypeException
{
// get the component type id
Integer ctid = (Integer)_components.get(cid);
if (ctid == null) {
throw new NoSuchComponentException(cid);
}
// get the component type
ComponentType type = (ComponentType)_types.get(ctid.intValue());
if (type == null) {
throw new NoSuchComponentTypeException(ctid.intValue());
}
// get the character animation images
ComponentFrames frames = TileUtil.getComponentFrames(
_tilemgr, cid, type.frameCount);
// create the component
return new CharacterComponent(type, cid, frames);
}
// documentation inherited
public Iterator enumerateComponents (int ctid)
throws NoSuchComponentTypeException
{
return new ComponentTypeIterator(ctid);
}
// documentation inherited
public Iterator enumerateComponentTypes ()
{
return _types.keys();
}
/**
* Iterates over all components of a specified component type in
* the component hashtable.
*/
protected class ComponentTypeIterator implements Iterator
{
public ComponentTypeIterator (int ctid)
throws NoSuchComponentTypeException
{
_ctid = ctid;
_iter = _components.keys();
advance();
// make sure we have at least one component of the
// specified type
if (_next == null) {
throw new NoSuchComponentTypeException(ctid);
}
}
public boolean hasNext ()
{
return (_next != null);
}
public Object next ()
{
Object next = _next;
advance();
return next;
}
public void remove ()
{
throw new UnsupportedOperationException();
}
protected void advance ()
{
while (_iter.hasNext()) {
CharacterComponent c = (CharacterComponent)_iter.next();
if (c.getType().ctid == _ctid) {
_next = c;
return;
}
}
_next = null;
}
/** The component type id we're enumerating over. */
protected int _ctid;
/** The next character component of the component type id
* associated with this iterator, or null if no more exist. */
protected Object _next;
/** Iterator over the components in the repository. */
protected Iterator _iter;
}
/** The config key for the character description file. */
protected static final String COMPFILE_KEY =
MisoUtil.CONFIG_KEY + ".components";
/** The default character description file. */
protected static final String DEFAULT_COMPFILE =
"rsrc/config/miso/components.xml";
/** The hashtable of component types. */
protected HashIntMap _types = new HashIntMap();
/** The hashtable of character components. */
protected HashIntMap _components = new HashIntMap();
/** The tile manager. */
protected TileManager _tilemgr;
}