Pulled out gobs of Sprite/Animation functionality into AbstractMedia.

Pulled out gobs of SpriteManager/AnimationManager functionality into
  AbstractMediaManager.
The big change: sprites can have their render order fine-tuned like
animations.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1787 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2002-10-08 21:03:37 +00:00
parent e804abad30
commit a0730067ec
10 changed files with 532 additions and 659 deletions
@@ -1,17 +1,14 @@
//
// $Id: AnimationManager.java,v 1.13 2002/09/20 21:28:20 mdb Exp $
// $Id: AnimationManager.java,v 1.14 2002/10/08 21:03:37 ray Exp $
package com.threerings.media.animation;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.util.Comparator;
import java.util.ArrayList;
import com.samskivert.util.SortableArrayList;
import com.threerings.media.AbstractMediaManager;
import com.threerings.media.Log;
import com.threerings.media.MediaConstants;
import com.threerings.media.RegionManager;
/**
@@ -19,8 +16,7 @@ import com.threerings.media.RegionManager;
* manager itself is ticked and generating events when animations finish
* and suchlike.
*/
public class AnimationManager
implements MediaConstants
public class AnimationManager extends AbstractMediaManager
{
/**
* Construct and initialize the animation manager which readies itself
@@ -28,7 +24,7 @@ public class AnimationManager
*/
public AnimationManager (RegionManager remgr)
{
_remgr = remgr;
super(remgr);
}
/**
@@ -37,14 +33,7 @@ public class AnimationManager
*/
public void registerAnimation (Animation anim)
{
if (_anims.contains(anim)) {
Log.warning("Attempt to register animation more than once " +
"[anim=" + anim + "].");
return;
}
anim.setAnimationManager(this);
_anims.insertSorted(anim, RENDER_ORDER);
insertMedia(anim);
}
/**
@@ -55,43 +44,17 @@ public class AnimationManager
*/
public void unregisterAnimation (Animation anim)
{
// un-register the animation
if (!_anims.remove(anim)) {
Log.warning("Attempt to un-register animation that isn't " +
"registered [anim=" + anim + "].");
return;
}
// invalidate its bounds
_remgr.invalidateRegion(anim.getBounds());
removeMedia(anim);
}
/**
* Provides access to the region manager that the animation manager is
* using to collect invalid regions every frame. This should generally
* only be used by animations that want to invalidate themselves.
*/
public RegionManager getRegionManager ()
// documentation inherited
protected void tickAllMedia (long tickStamp)
{
return _remgr;
}
/**
* Handles updating animations and generating associated events.
*
* @param tickStamp the system clock at the time of the tick.
*/
public void tick (long tickStamp)
{
// tick all of our animations
int size = _anims.size();
for (int ii = 0; ii < size; ii++) {
((Animation)_anims.get(ii)).tick(tickStamp);
}
super.tickAllMedia(tickStamp);
// remove any finished animations
for (int ii = size - 1; ii >= 0; ii--) {
Animation anim = (Animation)_anims.get(ii);
for (int ii=_media.size() - 1; ii >= 0; ii--) {
Animation anim = (Animation)_media.get(ii);
if (anim.isFinished()) {
// let any animation observers know that we're done
anim.notifyObservers(new AnimationCompletedEvent(anim));
@@ -104,68 +67,12 @@ public class AnimationManager
}
}
/**
* If the animation manager is paused for some length of time, it
* should be fast forwarded by the appropriate number of milliseconds.
* This allows animations to smoothly pick up where they left off
* rather than abruptly jumping into the future, thinking that some
* outrageous amount of time passed since their last tick.
*/
public void fastForward (long timeDelta)
// documentation inherited
protected void dispatchEvent (ArrayList observers, Object event)
{
// fast forward all of our animations
int size = _anims.size();
for (int ii = 0; ii < size; ii++) {
((Animation)_anims.get(ii)).fastForward(timeDelta);
AnimationEvent aevt = (AnimationEvent) event;
for (int ii=0, nn=observers.size(); ii < nn; ii++) {
((AnimationObserver) observers.get(ii)).handleEvent(aevt);
}
}
/**
* Renders all registered animations in the given layer that intersect
* the supplied clipping rectangle to the given graphics context.
*
* @param layer the layer to render; one of {@link #FRONT}, {@link
* #BACK}, or {@link #ALL}. The front layer contains all animations
* with a positive render order; the back layer contains all
* animations with a negative render order; all, both.
*/
public void renderAnimations (Graphics2D gfx, int layer, Shape clip)
{
// now paint them
int size = _anims.size();
for (int ii = 0; ii < size; ii++) {
Animation anim = (Animation)_anims.get(ii);
int order = anim.getRenderOrder();
try {
if (((layer == ALL) ||
(layer == FRONT && order >= 0) ||
(layer == BACK && order < 0)) &&
clip.intersects(anim.getBounds())) {
anim.paint(gfx);
}
} catch (Exception e) {
Log.warning("Failed to render animation " +
"[anim=" + anim + ", e=" + e + "].");
Log.logStackTrace(e);
}
}
}
/** Used to accumulate dirty regions. */
protected RegionManager _remgr;
/** The list of animations. */
protected SortableArrayList _anims = new SortableArrayList();
/** Used to sort animations prior to painting. */
protected static final Comparator RENDER_ORDER = new Comparator() {
public int compare (Object o1, Object o2) {
return (((Animation)o1)._renderOrder -
((Animation)o2)._renderOrder);
}
public boolean equals (Object obj) {
return this == obj;
}
};
}