From 493e1f1a8b836ed3274b86c2c71a7a6a916cdf63 Mon Sep 17 00:00:00 2001 From: Mike Thomas Date: Sat, 16 Dec 2006 00:51:03 +0000 Subject: [PATCH] New type of sprite that wraps an animation. This is useful for moving animations. It's also useful, thanks to some wackiness in the MediaPanel for making animations appear in appropriate places in the render order compared to sprites. I'd really prefer to put this in the sprite package, but putting it here means I don't have to publicize the willStart/willFinish/didFinish functions on Animation. I'm open to suggestions for better solutions to that one... git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@96 ed5b42cb-e716-0410-a449-f6a68f950b19 --- .../com/threerings/media/AbstractMedia.java | 2 +- .../media/animation/AnimationSprite.java | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/java/com/threerings/media/animation/AnimationSprite.java diff --git a/src/java/com/threerings/media/AbstractMedia.java b/src/java/com/threerings/media/AbstractMedia.java index 217d4319..b50a71b6 100644 --- a/src/java/com/threerings/media/AbstractMedia.java +++ b/src/java/com/threerings/media/AbstractMedia.java @@ -239,7 +239,7 @@ public abstract class AbstractMedia /** * Initialize the media. */ - protected final void init (AbstractMediaManager manager) + public final void init (AbstractMediaManager manager) { _mgr = manager; init(); diff --git a/src/java/com/threerings/media/animation/AnimationSprite.java b/src/java/com/threerings/media/animation/AnimationSprite.java new file mode 100644 index 00000000..3f74da1e --- /dev/null +++ b/src/java/com/threerings/media/animation/AnimationSprite.java @@ -0,0 +1,72 @@ +package com.threerings.media.animation; + +import java.awt.Graphics2D; +import java.awt.Rectangle; + +import com.threerings.media.AbstractMediaManager; +import com.threerings.media.MediaPanel; +import com.threerings.media.sprite.Sprite; +import com.threerings.media.animation.Animation; +import com.threerings.media.animation.AnimationAdapter; +import com.threerings.media.animation.AnimationObserver; + +/** + * A Sprite that wraps an animation so that a sequence of frames can be easily + * moved around the screen. Animations embedded here should not be added + * directly to a media panel as this sprite will manage them. If the enclosed + * animation completes, we remove the sprite from the media panel, since the + * animation would normally do that itself. + */ +public class AnimationSprite extends Sprite +{ + public AnimationSprite (Animation anim, MediaPanel owner) + { + super(); + _anim = anim; + _owner = owner; + } + + public void init () + { + _anim.init(_mgr); + } + + // documentation inherited + public void tick (long tickStamp) + { + super.tick(tickStamp); + _anim.tick(tickStamp); + + if (_anim.isFinished()) { + _anim.willFinish(tickStamp); + _owner.removeSprite(AnimationSprite.this); + _anim.didFinish(tickStamp); + } else { + _bounds = (Rectangle)_anim.getBounds().clone(); + } + } + + // documentation inherited. + public void setLocation (int x, int y) + { + _anim.setLocation(x, y); + super.setLocation(x, y); + } + + // documentation inherited + public void willStart (long tickStamp) + { + super.willStart(tickStamp); + _anim.willStart(tickStamp); + } + + // documentation inherited + public void paint (Graphics2D gfx) { + // Nothing to paint for ourselves. + + _anim.paint(gfx); + } + + protected Animation _anim; + protected MediaPanel _owner; +}