From 580fdba83bc6928de921a0027aae8a095c128611 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 30 Apr 2003 00:45:02 +0000 Subject: [PATCH] Revamped animation observation. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2505 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/media/animation/Animation.java | 13 +---- .../media/animation/AnimationAdapter.java | 15 ++++++ .../animation/AnimationCompletedEvent.java | 16 ------ .../media/animation/AnimationEvent.java | 45 ---------------- .../animation/AnimationFrameSequencer.java | 39 +++++++++++--- .../media/animation/AnimationManager.java | 51 ++++++++----------- .../media/animation/AnimationObserver.java | 9 ++-- .../media/animation/AnimationSequencer.java | 11 ++-- .../media/animation/AnimationWaiter.java | 21 ++++---- .../media/animation/FrameReachedEvent.java | 42 --------------- .../animation/SequencedAnimationObserver.java | 17 +++++++ .../media/animation/SpriteAnimation.java | 24 +++++---- 12 files changed, 115 insertions(+), 188 deletions(-) create mode 100644 src/java/com/threerings/media/animation/AnimationAdapter.java delete mode 100644 src/java/com/threerings/media/animation/AnimationCompletedEvent.java delete mode 100644 src/java/com/threerings/media/animation/AnimationEvent.java delete mode 100644 src/java/com/threerings/media/animation/FrameReachedEvent.java create mode 100644 src/java/com/threerings/media/animation/SequencedAnimationObserver.java diff --git a/src/java/com/threerings/media/animation/Animation.java b/src/java/com/threerings/media/animation/Animation.java index 313186ef7..0a803ecc9 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.9 2002/10/08 21:03:37 ray Exp $ +// $Id: Animation.java,v 1.10 2003/04/30 00:45:02 mdb Exp $ package com.threerings.media.animation; @@ -60,17 +60,6 @@ public abstract class Animation extends AbstractMedia addObserver(obs); } - /** - * Notifies any animation observers that the given animation event has - * occurred. - */ - public void notifyObservers (AnimationEvent event) - { - if (_observers != null) { - _mgr.queueNotification(_observers, event); - } - } - /** Whether the animation is finished. */ protected boolean _finished = false; } diff --git a/src/java/com/threerings/media/animation/AnimationAdapter.java b/src/java/com/threerings/media/animation/AnimationAdapter.java new file mode 100644 index 000000000..6bfd9f4cb --- /dev/null +++ b/src/java/com/threerings/media/animation/AnimationAdapter.java @@ -0,0 +1,15 @@ +// +// $Id: AnimationAdapter.java,v 1.1 2003/04/30 00:45:02 mdb Exp $ + +package com.threerings.media.animation; + +/** + * An adapter class for {@link AnimationObserver}. + */ +public class AnimationAdapter implements AnimationObserver +{ + // documentation inherited from interface + public void animationCompleted (Animation anim, long when) + { + } +} diff --git a/src/java/com/threerings/media/animation/AnimationCompletedEvent.java b/src/java/com/threerings/media/animation/AnimationCompletedEvent.java deleted file mode 100644 index d36aab11d..000000000 --- a/src/java/com/threerings/media/animation/AnimationCompletedEvent.java +++ /dev/null @@ -1,16 +0,0 @@ -// -// $Id: AnimationCompletedEvent.java,v 1.2 2002/11/05 20:51:13 mdb Exp $ - -package com.threerings.media.animation; - -/** - * An animation completed event is dispatched when an animation has - * completed all of its business. - */ -public class AnimationCompletedEvent extends AnimationEvent -{ - public AnimationCompletedEvent (Animation anim, long when) - { - super(anim, when); - } -} diff --git a/src/java/com/threerings/media/animation/AnimationEvent.java b/src/java/com/threerings/media/animation/AnimationEvent.java deleted file mode 100644 index 493b67d82..000000000 --- a/src/java/com/threerings/media/animation/AnimationEvent.java +++ /dev/null @@ -1,45 +0,0 @@ -// -// $Id: AnimationEvent.java,v 1.2 2002/11/05 20:51:13 mdb Exp $ - -package com.threerings.media.animation; - -/** - * An animation event is sent to all of an animation's observers whenever - * one of the various possible animation events takes place. - */ -public class AnimationEvent -{ - /** - * Create an animation event. - * - * @param animation the involved animation. - * @param when the time at which this event took place. - */ - public AnimationEvent (Animation anim, long when) - { - _anim = anim; - _when = when; - } - - /** - * Returns the animation involved in the event. - */ - public Animation getAnimation () - { - return _anim; - } - - /** - * Returns the time associated with this event. - */ - public long getWhen () - { - return _when; - } - - /** The animation associated with this event. */ - protected Animation _anim; - - /** The time associated with this event. */ - protected long _when; -} diff --git a/src/java/com/threerings/media/animation/AnimationFrameSequencer.java b/src/java/com/threerings/media/animation/AnimationFrameSequencer.java index 0af33e137..52e269b29 100644 --- a/src/java/com/threerings/media/animation/AnimationFrameSequencer.java +++ b/src/java/com/threerings/media/animation/AnimationFrameSequencer.java @@ -1,10 +1,12 @@ // -// $Id: AnimationFrameSequencer.java,v 1.5 2003/04/14 20:36:16 ray Exp $ +// $Id: AnimationFrameSequencer.java,v 1.6 2003/04/30 00:45:02 mdb Exp $ package com.threerings.media.animation; import java.util.Arrays; +import com.samskivert.util.ObserverList; + import com.threerings.media.util.FrameSequencer; import com.threerings.media.util.MultiFrameImage; @@ -19,8 +21,9 @@ public interface AnimationFrameSequencer extends FrameSequencer public void setAnimation (Animation anim); /** - * A frame sequencer that can step through a series of frames in any order - * and speed and send events when specific frames are reached. + * A sequencer that can step through a series of frames in any order + * and speed and notify (via {@link SequencedAnimationObserver}) when + * specific frames are reached. */ public static class MultiFunction implements AnimationFrameSequencer { @@ -178,9 +181,9 @@ public interface AnimationFrameSequencer extends FrameSequencer protected void checkNotify (long tickStamp) { if (_marks[_curIdx]) { - _animation.notifyObservers( - new FrameReachedEvent( - _animation, tickStamp, _sequence[_curIdx], _curIdx)); + _animation.queueNotification( + new FrameReachedOp(_animation, tickStamp, + _sequence[_curIdx], _curIdx)); } } @@ -213,5 +216,29 @@ public interface AnimationFrameSequencer extends FrameSequencer /** The animation that we're sequencing for. */ protected Animation _animation; + + /** Used to dispatch {@link AnimationObserver#frameReached}. */ + protected static class FrameReachedOp implements ObserverList.ObserverOp + { + public FrameReachedOp (Animation anim, long when, + int frameIdx, int frameSeq) { + _anim = anim; + _when = when; + _frameIdx = frameIdx; + _frameSeq = frameSeq; + } + + public boolean apply (Object observer) { + if (observer instanceof SequencedAnimationObserver) { + ((SequencedAnimationObserver)observer).frameReached( + _anim, _when, _frameIdx, _frameSeq); + } + return true; + } + + protected Animation _anim; + protected long _when; + protected int _frameIdx, _frameSeq; + } } } diff --git a/src/java/com/threerings/media/animation/AnimationManager.java b/src/java/com/threerings/media/animation/AnimationManager.java index 8b15253db..302f2a759 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.15 2002/11/05 20:51:50 mdb Exp $ +// $Id: AnimationManager.java,v 1.16 2003/04/30 00:45:02 mdb Exp $ package com.threerings.media.animation; @@ -50,45 +50,34 @@ public class AnimationManager extends AbstractMediaManager { super.tickAllMedia(tickStamp); - // remove any finished animations - for (int ii=_media.size() - 1; ii >= 0; 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, tickStamp)); - // un-register the animation - unregisterAnimation(anim); - // let the animation clean itself up as necessary - anim.didFinish(); - // Log.info("Removed finished animation [anim=" + anim + "]."); + if (!anim.isFinished()) { + continue; } + + // as the anim is finished, remove it and notify observers + anim.queueNotification(new AnimCompletedOp(anim, tickStamp)); + unregisterAnimation(anim); + anim.didFinish(); + // Log.info("Removed finished animation " + anim + "."); } } - // documentation inherited - protected void dispatchEvent (ObserverList observers, Object event) + /** Used to dispatch {@link AnimationObserver#animationCompleted}. */ + protected static class AnimCompletedOp implements ObserverList.ObserverOp { - _dispatchOp.init((AnimationEvent)event); - observers.apply(_dispatchOp); - } - - /** Used by {@link #dispatchEvent}. */ - protected static class DispatchOp implements ObserverList.ObserverOp - { - public void init (AnimationEvent event) - { - _event = event; + public AnimCompletedOp (Animation anim, long when) { + _anim = anim; + _when = when; } - public boolean apply (Object observer) - { - ((AnimationObserver)observer).handleEvent(_event); + public boolean apply (Object observer) { + ((AnimationObserver)observer).animationCompleted(_anim, _when); return true; } - protected AnimationEvent _event; - }; - - protected DispatchOp _dispatchOp = new DispatchOp(); + protected Animation _anim; + protected long _when; + } } diff --git a/src/java/com/threerings/media/animation/AnimationObserver.java b/src/java/com/threerings/media/animation/AnimationObserver.java index 069e8e3be..f5459ee72 100644 --- a/src/java/com/threerings/media/animation/AnimationObserver.java +++ b/src/java/com/threerings/media/animation/AnimationObserver.java @@ -1,5 +1,5 @@ // -// $Id: AnimationObserver.java,v 1.1 2002/01/11 16:17:33 shaper Exp $ +// $Id: AnimationObserver.java,v 1.2 2003/04/30 00:45:02 mdb Exp $ package com.threerings.media.animation; @@ -10,10 +10,7 @@ package com.threerings.media.animation; public interface AnimationObserver { /** - * This method is called by the {@link AnimationManager} when - * something interesting involving the animation happens. - * - * @param event the animation event. + * Called when the observed animation has completed. */ - public void handleEvent (AnimationEvent event); + public void animationCompleted (Animation anim, long when); } diff --git a/src/java/com/threerings/media/animation/AnimationSequencer.java b/src/java/com/threerings/media/animation/AnimationSequencer.java index 573f87eaa..3b1ebc1d5 100644 --- a/src/java/com/threerings/media/animation/AnimationSequencer.java +++ b/src/java/com/threerings/media/animation/AnimationSequencer.java @@ -1,5 +1,5 @@ // -// $Id: AnimationSequencer.java,v 1.12 2002/11/06 07:40:05 shaper Exp $ +// $Id: AnimationSequencer.java,v 1.13 2003/04/30 00:45:02 mdb Exp $ package com.threerings.media.animation; @@ -153,8 +153,7 @@ public abstract class AnimationSequencer extends Animation implements AnimationObserver { public AnimRecord (Animation anim, long delta, AnimRecord trigger, - Runnable completionAction) - { + Runnable completionAction) { _anim = anim; _delta = delta; _trigger = trigger; @@ -218,11 +217,9 @@ public abstract class AnimationSequencer extends Animation tick(when); } - public void handleEvent (AnimationEvent event) + public void animationCompleted (Animation anim, long when) { - if (event instanceof AnimationCompletedEvent) { - fireCompletion(event.getWhen()); - } + fireCompletion(when); } public String toString () diff --git a/src/java/com/threerings/media/animation/AnimationWaiter.java b/src/java/com/threerings/media/animation/AnimationWaiter.java index 9caf0751f..730db0aa7 100644 --- a/src/java/com/threerings/media/animation/AnimationWaiter.java +++ b/src/java/com/threerings/media/animation/AnimationWaiter.java @@ -1,5 +1,5 @@ // -// $Id: AnimationWaiter.java,v 1.2 2002/08/23 23:18:14 shaper Exp $ +// $Id: AnimationWaiter.java,v 1.3 2003/04/30 00:45:02 mdb Exp $ package com.threerings.media.animation; @@ -34,19 +34,16 @@ public abstract class AnimationWaiter } } - // documentation inherited - public void handleEvent (AnimationEvent event) + // documentation inherited from interface + public void animationCompleted (Animation anim, long when) { - if (event instanceof AnimationCompletedEvent) { - // note that the animation is finished - Animation anim = event.getAnimation(); - animationDidFinish(anim); - _animCount--; + // note that the animation is finished + animationDidFinish(anim); + _animCount--; - // let derived classes know when all is done - if (_animCount == 0) { - allAnimationsFinished(); - } + // let derived classes know when all is done + if (_animCount == 0) { + allAnimationsFinished(); } } diff --git a/src/java/com/threerings/media/animation/FrameReachedEvent.java b/src/java/com/threerings/media/animation/FrameReachedEvent.java deleted file mode 100644 index a20f735c1..000000000 --- a/src/java/com/threerings/media/animation/FrameReachedEvent.java +++ /dev/null @@ -1,42 +0,0 @@ -// -// $Id: FrameReachedEvent.java,v 1.2 2002/11/05 20:51:13 mdb Exp $ - -package com.threerings.media.animation; - -/** - * Indicates that a particular frame was reached in an animation. - */ -public class FrameReachedEvent extends AnimationEvent -{ - /** - * Construct a FrameReachedEvent. - */ - public FrameReachedEvent (Animation anim, long when, - int frameIdx, int frameSeq) - { - super(anim, when); - _frameIdx = frameIdx; - _frameSeq = frameSeq; - } - - /** - * Return the index in the multi-frame animation that was reached. - */ - public int getFrameIndex () - { - return _frameIdx; - } - - /** - * Return the sequence number of the frame that was reached. - * This may be different from the index, a MultiFunction FrameSequencer - * can show a particular frame more than once. - */ - public int getFrameSequence () - { - return _frameSeq; - } - - /** Frame index and sequence for the event. */ - protected int _frameIdx, _frameSeq; -} diff --git a/src/java/com/threerings/media/animation/SequencedAnimationObserver.java b/src/java/com/threerings/media/animation/SequencedAnimationObserver.java new file mode 100644 index 000000000..3681c0bc8 --- /dev/null +++ b/src/java/com/threerings/media/animation/SequencedAnimationObserver.java @@ -0,0 +1,17 @@ +// +// $Id: SequencedAnimationObserver.java,v 1.1 2003/04/30 00:45:02 mdb Exp $ + +package com.threerings.media.animation; + +/** + * Extends the animation observer interface with extra goodies. + */ +public interface SequencedAnimationObserver extends AnimationObserver +{ + /** + * Called when the observed animation -- previously configured with an + * {@link AnimationFrameSequencer} -- reached the specified frame. + */ + public void frameReached (Animation anim, long when, + int frameIdx, int frameSeq); +} diff --git a/src/java/com/threerings/media/animation/SpriteAnimation.java b/src/java/com/threerings/media/animation/SpriteAnimation.java index b9518d022..da9897cec 100644 --- a/src/java/com/threerings/media/animation/SpriteAnimation.java +++ b/src/java/com/threerings/media/animation/SpriteAnimation.java @@ -1,20 +1,18 @@ // -// $Id: SpriteAnimation.java,v 1.2 2002/11/06 01:40:39 mdb Exp $ +// $Id: SpriteAnimation.java,v 1.3 2003/04/30 00:45:02 mdb 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; +import com.threerings.media.sprite.PathObserver; import com.threerings.media.util.Path; public class SpriteAnimation extends Animation - implements SpriteObserver + implements PathObserver { /** * Constructs a sprite animation for the given sprite. The first time @@ -54,13 +52,17 @@ public class SpriteAnimation extends Animation // nothing for now } - // documentation inherited - public void handleEvent (SpriteEvent event) + // documentation inherited from interface + public void pathCancelled (Sprite sprite, Path path) { - if (event instanceof PathCompletedEvent) { - _finished = true; - } - } + _finished = true; + } + + // documentation inherited from interface + public void pathCompleted (Sprite sprite, Path path, long when) + { + _finished = true; + } // documentation inherited protected void didFinish ()