From b3eebe2081af7378469b54b941d9bc0ba8f2c32b Mon Sep 17 00:00:00 2001 From: Walter Korman Date: Sat, 16 Mar 2002 03:11:23 +0000 Subject: [PATCH] Added ability to render animations in two distinct layers; added mechanism whereby animations can clean up after themselves. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1130 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/media/animation/Animation.java | 33 ++++++++- .../media/animation/AnimationManager.java | 32 ++++++-- .../media/animation/SpriteAnimation.java | 73 +++++++++++++++++++ 3 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 src/java/com/threerings/media/animation/SpriteAnimation.java diff --git a/src/java/com/threerings/media/animation/Animation.java b/src/java/com/threerings/media/animation/Animation.java index 67dd93377..8bc7ec251 100644 --- a/src/java/com/threerings/media/animation/Animation.java +++ b/src/java/com/threerings/media/animation/Animation.java @@ -1,5 +1,5 @@ // -// $Id: Animation.java,v 1.3 2002/03/14 21:09:01 shaper Exp $ +// $Id: Animation.java,v 1.4 2002/03/16 03:11:23 shaper Exp $ package com.threerings.media.animation; @@ -27,6 +27,25 @@ public abstract class Animation _bounds = bounds; } + /** + * Returns the render order of this animation. + */ + public int getRenderOrder () + { + return _renderOrder; + } + + /** + * Sets the render order associated with this animation. Animations + * can be rendered in two layers; those with negative render order and + * those with positive render order. Someday animations will be + * rendered in each layer according to render order. + */ + public void setRenderOrder (int value) + { + _renderOrder = value; + } + /** * Called by the {@link AnimationManager} to allow the animation to * render itself to the given graphics context. @@ -65,6 +84,15 @@ public abstract class Animation return _finished; } + /** + * Called when the animation is finished and the animation manager has + * removed it from service. + */ + protected void didFinish () + { + // nothing for now + } + /** * Adds an animation observer to this animation's list of observers. */ @@ -137,6 +165,9 @@ public abstract class Animation /** The animation bounds. */ protected Rectangle _bounds; + /** The render order of this animation. */ + protected int _renderOrder; + /** The list of animation observers. */ protected ArrayList _observers; diff --git a/src/java/com/threerings/media/animation/AnimationManager.java b/src/java/com/threerings/media/animation/AnimationManager.java index 7aba90054..8736c33e1 100644 --- a/src/java/com/threerings/media/animation/AnimationManager.java +++ b/src/java/com/threerings/media/animation/AnimationManager.java @@ -1,5 +1,5 @@ // -// $Id: AnimationManager.java,v 1.7 2002/02/19 01:23:56 mdb Exp $ +// $Id: AnimationManager.java,v 1.8 2002/03/16 03:11:23 shaper Exp $ package com.threerings.media.animation; @@ -30,6 +30,15 @@ import com.threerings.media.util.PerformanceObserver; public class AnimationManager implements Interval, PerformanceObserver { + /** Constant for the front layer of animations. */ + public static final int FRONT = 0; + + /** Constant for the back layer of animations. */ + public static final int BACK = 1; + + /** Constant for all layers of animations. */ + public static final int ALL = 2; + /** * Construct and initialize the animation manager with a sprite * manager and the view in which the animations will take place. The @@ -150,14 +159,25 @@ public class AnimationManager } /** - * Renders all registered animations to the given graphics context. + * Renders all registered animations in the given layer 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) + public void renderAnimations (Graphics2D gfx, int layer) { int size = _anims.size(); for (int ii = 0; ii < size; ii++) { Animation anim = (Animation)_anims.get(ii); - anim.paint(gfx); + int order = anim.getRenderOrder(); + if ((layer == ALL) || + (layer == FRONT && order >= 0) || + (layer == BACK && order < 0)) { + anim.paint(gfx); + } } } @@ -268,7 +288,7 @@ public class AnimationManager /** * The tick method handles updating sprites and - * repainting the target display. + * animations, and repainting the target display. */ protected void tick () { @@ -343,6 +363,8 @@ public class AnimationManager anim.notifyObservers(new AnimationCompletedEvent(anim)); // un-register the animation unregisterAnimation(anim); + // let the animation clean itself up as necessary + anim.didFinish(); // Log.info("Removed finished animation [anim=" + anim + "]."); } } diff --git a/src/java/com/threerings/media/animation/SpriteAnimation.java b/src/java/com/threerings/media/animation/SpriteAnimation.java new file mode 100644 index 000000000..cdc10e3cb --- /dev/null +++ b/src/java/com/threerings/media/animation/SpriteAnimation.java @@ -0,0 +1,73 @@ +// +// $Id: SpriteAnimation.java,v 1.1 2002/03/16 03:11:23 shaper Exp $ + +package com.threerings.media.animation; + +import java.awt.Graphics2D; +import java.awt.Rectangle; + +import com.threerings.media.sprite.PathCompletedEvent; +import com.threerings.media.sprite.Sprite; +import com.threerings.media.sprite.SpriteEvent; +import com.threerings.media.sprite.SpriteManager; +import com.threerings.media.sprite.SpriteObserver; + +public class SpriteAnimation extends Animation + implements SpriteObserver +{ + /** + * Constructs a sprite animation for the given sprite and adds the + * sprite to the given sprite manager. If the sprite has had a path + * set, the animation will finish when the sprite completes its path. + */ + public SpriteAnimation (SpriteManager spritemgr, Sprite sprite) + { + super(new Rectangle()); + + // save things off + _spritemgr = spritemgr; + _sprite = sprite; + + // set up our sprite business + _sprite.addSpriteObserver(this); + _spritemgr.addSprite(_sprite); + } + + /** + * Derived classes can override tick if they want to end the animation + * in some other way than the sprite completing a path. + */ + public void tick (long timestamp) + { + // nothing for now + } + + // documentation inherited + public void paint (Graphics2D gfx) + { + // nothing for now + } + + // documentation inherited + public void handleEvent (SpriteEvent event) + { + if (event instanceof PathCompletedEvent) { + _finished = true; + } + } + + // documentation inherited + protected void didFinish () + { + super.didFinish(); + + _spritemgr.removeSprite(_sprite); + _sprite = null; + } + + /** The sprite associated with this animation. */ + protected Sprite _sprite; + + /** The sprite manager managing our sprite. */ + protected SpriteManager _spritemgr; +}