com.threerings.cast
Class CharacterManager

java.lang.Object
  extended by com.threerings.cast.CharacterManager
All Implemented Interfaces:
DirectionCodes

public class CharacterManager
extends Object
implements DirectionCodes

The character manager provides facilities for constructing sprites that are used to represent characters in a scene. It also handles the compositing and caching of composited character animations.


Nested Class Summary
protected static class CharacterManager.TranslatedComponent
          Combines a component with an optional translation for shadowing or masking.
 
Field Summary
protected  ActionCache _acache
          The action animation cache, if we have one.
protected  Map<Tuple<CharacterDescriptor,String>,ActionFrames> _actionFrames
          A table of composited action sequences (these don't reference the actual image data directly and thus take up little memory).
protected  Map<String,ActionSequence> _actions
          A table of our action sequences.
protected static RuntimeAdjust.IntAdjust _cacheSize
          Register our image cache size with the runtime adjustments framework.
protected  Throttle _cacheStatThrottle
          Throttle our cache status logging to once every 30 seconds.
protected  Class<? extends CharacterSprite> _charClass
          The character class to be created.
protected  ComponentRepository _crepo
          The component repository.
protected  LRUHashMap<CompositedActionFrames.CompositedFramesKey,CompositedMultiFrameImage> _frameCache
          A cache of composited animation frames.
protected  ImageManager _imgr
          The image manager with whom we interact.
protected static int _runCacheSize
          Cache size to be used in this run.
 
Fields inherited from interface com.threerings.util.DirectionCodes
CARDINAL_DIRECTIONS, CCW, CW, DIRECTION_COUNT, DOWN, EAST, EASTNORTHEAST, EASTSOUTHEAST, FINE_DIRECTION_COUNT, HORIZONTAL, LEFT, NONE, NORTH, NORTHEAST, NORTHNORTHEAST, NORTHNORTHWEST, NORTHWEST, RIGHT, SOUTH, SOUTHEAST, SOUTHSOUTHEAST, SOUTHSOUTHWEST, SOUTHWEST, UP, VERTICAL, WEST, WESTNORTHWEST, WESTSOUTHWEST
 
Constructor Summary
CharacterManager(ImageManager imgr, ComponentRepository crepo)
          Constructs the character manager.
 
Method Summary
protected  ActionFrames compositeMask(String action, CharacterComponent ccomp, ActionFrames cframes, ArrayList<CharacterManager.TranslatedComponent> mcomps)
           
protected  CompositedActionFrames.ComponentFrames compositeShadow(String action, String sclass, ArrayList<CharacterManager.TranslatedComponent> scomps)
           
protected  ActionFrames createCompositeFrames(CharacterDescriptor descrip, String action)
          Generates the composited animation frames for the specified action for a character with the specified descriptor.
 ActionFrames getActionFrames(CharacterDescriptor descrip, String action)
          Obtains the composited animation frames for the specified action for a character with the specified descriptor.
 ActionSequence getActionSequence(String action)
          Returns the action sequence instance with the specified name or null if no such sequence exists.
 CharacterSprite getCharacter(CharacterDescriptor desc)
          Returns a CharacterSprite representing the character described by the given CharacterDescriptor, or null if an error occurs.
<T extends CharacterSprite>
T
getCharacter(CharacterDescriptor desc, Class<T> charClass)
          Returns a CharacterSprite representing the character described by the given CharacterDescriptor, or null if an error occurs.
 ComponentRepository getComponentRepository()
          Returns the component repository being used by this manager.
protected  long getEstimatedCacheMemoryUsage()
          Returns the estimated memory usage in bytes for all images currently cached by the cached action frames.
 void resolveActionSequence(CharacterDescriptor desc, String action)
          Informs the character manager that the action sequence for the given character descriptor is likely to be needed in the near future and so any efforts that can be made to load it into the action sequence cache in advance should be undertaken.
 void setActionCache(ActionCache cache)
          Instructs the character manager to use the provided cache for composited action animations.
static void setCacheSize(int cacheKilobytes)
          Sets the size of the cache used for composited animation frames.
 void setCharacterClass(Class<? extends CharacterSprite> charClass)
          Instructs the character manager to construct instances of this derived class of CharacterSprite when creating new sprites.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_imgr

protected ImageManager _imgr
The image manager with whom we interact.


_crepo

protected ComponentRepository _crepo
The component repository.


_actions

protected Map<String,ActionSequence> _actions
A table of our action sequences.


_actionFrames

protected Map<Tuple<CharacterDescriptor,String>,ActionFrames> _actionFrames
A table of composited action sequences (these don't reference the actual image data directly and thus take up little memory).


_frameCache

protected LRUHashMap<CompositedActionFrames.CompositedFramesKey,CompositedMultiFrameImage> _frameCache
A cache of composited animation frames.


_charClass

protected Class<? extends CharacterSprite> _charClass
The character class to be created.


_acache

protected ActionCache _acache
The action animation cache, if we have one.


_cacheStatThrottle

protected Throttle _cacheStatThrottle
Throttle our cache status logging to once every 30 seconds.


_cacheSize

protected static RuntimeAdjust.IntAdjust _cacheSize
Register our image cache size with the runtime adjustments framework.


_runCacheSize

protected static int _runCacheSize
Cache size to be used in this run. Adjusted by setCacheSize without affecting the stored value.

Constructor Detail

CharacterManager

public CharacterManager(ImageManager imgr,
                        ComponentRepository crepo)
Constructs the character manager.

Method Detail

setCacheSize

public static void setCacheSize(int cacheKilobytes)
Sets the size of the cache used for composited animation frames. This must be called before the CharacterManager is created.


getComponentRepository

public ComponentRepository getComponentRepository()
Returns the component repository being used by this manager.


setCharacterClass

public void setCharacterClass(Class<? extends CharacterSprite> charClass)
Instructs the character manager to construct instances of this derived class of CharacterSprite when creating new sprites.

Throws:
IllegalArgumentException - thrown if the supplied class does not derive from CharacterSprite.

setActionCache

public void setActionCache(ActionCache cache)
Instructs the character manager to use the provided cache for composited action animations.


getCharacter

public CharacterSprite getCharacter(CharacterDescriptor desc)
Returns a CharacterSprite representing the character described by the given CharacterDescriptor, or null if an error occurs.

Parameters:
desc - the character descriptor.

getCharacter

public <T extends CharacterSprite> T getCharacter(CharacterDescriptor desc,
                                                  Class<T> charClass)
Returns a CharacterSprite representing the character described by the given CharacterDescriptor, or null if an error occurs.

Parameters:
desc - the character descriptor.
charClass - the CharacterSprite derived class that should be instantiated instead of the configured default (which is set via setCharacterClass(java.lang.Class)).

getActionFrames

public ActionFrames getActionFrames(CharacterDescriptor descrip,
                                    String action)
                             throws NoSuchComponentException
Obtains the composited animation frames for the specified action for a character with the specified descriptor. The resulting composited animation will be cached.

Throws:
NoSuchComponentException - thrown if any of the components in the supplied descriptor do not exist.
IllegalArgumentException - thrown if any of the components referenced in the descriptor do not support the specified action.

resolveActionSequence

public void resolveActionSequence(CharacterDescriptor desc,
                                  String action)
Informs the character manager that the action sequence for the given character descriptor is likely to be needed in the near future and so any efforts that can be made to load it into the action sequence cache in advance should be undertaken.

This will eventually be revamped to spiffily load action sequences in the background.


getActionSequence

public ActionSequence getActionSequence(String action)
Returns the action sequence instance with the specified name or null if no such sequence exists.


getEstimatedCacheMemoryUsage

protected long getEstimatedCacheMemoryUsage()
Returns the estimated memory usage in bytes for all images currently cached by the cached action frames.


createCompositeFrames

protected ActionFrames createCompositeFrames(CharacterDescriptor descrip,
                                             String action)
                                      throws NoSuchComponentException
Generates the composited animation frames for the specified action for a character with the specified descriptor.

Throws:
NoSuchComponentException - thrown if any of the components in the supplied descriptor do not exist.
IllegalArgumentException - thrown if any of the components referenced in the descriptor do not support the specified action.

compositeShadow

protected CompositedActionFrames.ComponentFrames compositeShadow(String action,
                                                                 String sclass,
                                                                 ArrayList<CharacterManager.TranslatedComponent> scomps)

compositeMask

protected ActionFrames compositeMask(String action,
                                     CharacterComponent ccomp,
                                     ActionFrames cframes,
                                     ArrayList<CharacterManager.TranslatedComponent> mcomps)