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
This commit is contained in:
Walter Korman
2002-03-16 03:11:23 +00:00
parent 2d90f897f1
commit b3eebe2081
3 changed files with 132 additions and 6 deletions
@@ -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;
@@ -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 <code>tick</code> 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 + "].");
}
}
@@ -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;
}