Revamped animation observation.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2505 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2003-04-30 00:45:02 +00:00
parent 99127e4ce0
commit 580fdba83b
12 changed files with 115 additions and 188 deletions
@@ -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; package com.threerings.media.animation;
@@ -60,17 +60,6 @@ public abstract class Animation extends AbstractMedia
addObserver(obs); 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. */ /** Whether the animation is finished. */
protected boolean _finished = false; protected boolean _finished = false;
} }
@@ -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)
{
}
}
@@ -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);
}
}
@@ -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;
}
@@ -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; package com.threerings.media.animation;
import java.util.Arrays; import java.util.Arrays;
import com.samskivert.util.ObserverList;
import com.threerings.media.util.FrameSequencer; import com.threerings.media.util.FrameSequencer;
import com.threerings.media.util.MultiFrameImage; import com.threerings.media.util.MultiFrameImage;
@@ -19,8 +21,9 @@ public interface AnimationFrameSequencer extends FrameSequencer
public void setAnimation (Animation anim); public void setAnimation (Animation anim);
/** /**
* A frame sequencer that can step through a series of frames in any order * A sequencer that can step through a series of frames in any order
* and speed and send events when specific frames are reached. * and speed and notify (via {@link SequencedAnimationObserver}) when
* specific frames are reached.
*/ */
public static class MultiFunction implements AnimationFrameSequencer public static class MultiFunction implements AnimationFrameSequencer
{ {
@@ -178,9 +181,9 @@ public interface AnimationFrameSequencer extends FrameSequencer
protected void checkNotify (long tickStamp) protected void checkNotify (long tickStamp)
{ {
if (_marks[_curIdx]) { if (_marks[_curIdx]) {
_animation.notifyObservers( _animation.queueNotification(
new FrameReachedEvent( new FrameReachedOp(_animation, tickStamp,
_animation, tickStamp, _sequence[_curIdx], _curIdx)); _sequence[_curIdx], _curIdx));
} }
} }
@@ -213,5 +216,29 @@ public interface AnimationFrameSequencer extends FrameSequencer
/** The animation that we're sequencing for. */ /** The animation that we're sequencing for. */
protected Animation _animation; 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;
}
} }
} }
@@ -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; package com.threerings.media.animation;
@@ -50,45 +50,34 @@ public class AnimationManager extends AbstractMediaManager
{ {
super.tickAllMedia(tickStamp); 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); Animation anim = (Animation)_media.get(ii);
if (anim.isFinished()) { if (!anim.isFinished()) {
// let any animation observers know that we're done continue;
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 + "].");
} }
// 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 /** Used to dispatch {@link AnimationObserver#animationCompleted}. */
protected void dispatchEvent (ObserverList observers, Object event) protected static class AnimCompletedOp implements ObserverList.ObserverOp
{ {
_dispatchOp.init((AnimationEvent)event); public AnimCompletedOp (Animation anim, long when) {
observers.apply(_dispatchOp); _anim = anim;
} _when = when;
/** Used by {@link #dispatchEvent}. */
protected static class DispatchOp implements ObserverList.ObserverOp
{
public void init (AnimationEvent event)
{
_event = event;
} }
public boolean apply (Object observer) public boolean apply (Object observer) {
{ ((AnimationObserver)observer).animationCompleted(_anim, _when);
((AnimationObserver)observer).handleEvent(_event);
return true; return true;
} }
protected AnimationEvent _event; protected Animation _anim;
}; protected long _when;
}
protected DispatchOp _dispatchOp = new DispatchOp();
} }
@@ -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; package com.threerings.media.animation;
@@ -10,10 +10,7 @@ package com.threerings.media.animation;
public interface AnimationObserver public interface AnimationObserver
{ {
/** /**
* This method is called by the {@link AnimationManager} when * Called when the observed animation has completed.
* something interesting involving the animation happens.
*
* @param event the animation event.
*/ */
public void handleEvent (AnimationEvent event); public void animationCompleted (Animation anim, long when);
} }
@@ -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; package com.threerings.media.animation;
@@ -153,8 +153,7 @@ public abstract class AnimationSequencer extends Animation
implements AnimationObserver implements AnimationObserver
{ {
public AnimRecord (Animation anim, long delta, AnimRecord trigger, public AnimRecord (Animation anim, long delta, AnimRecord trigger,
Runnable completionAction) Runnable completionAction) {
{
_anim = anim; _anim = anim;
_delta = delta; _delta = delta;
_trigger = trigger; _trigger = trigger;
@@ -218,11 +217,9 @@ public abstract class AnimationSequencer extends Animation
tick(when); tick(when);
} }
public void handleEvent (AnimationEvent event) public void animationCompleted (Animation anim, long when)
{ {
if (event instanceof AnimationCompletedEvent) { fireCompletion(when);
fireCompletion(event.getWhen());
}
} }
public String toString () public String toString ()
@@ -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; package com.threerings.media.animation;
@@ -34,19 +34,16 @@ public abstract class AnimationWaiter
} }
} }
// documentation inherited // documentation inherited from interface
public void handleEvent (AnimationEvent event) public void animationCompleted (Animation anim, long when)
{ {
if (event instanceof AnimationCompletedEvent) { // note that the animation is finished
// note that the animation is finished animationDidFinish(anim);
Animation anim = event.getAnimation(); _animCount--;
animationDidFinish(anim);
_animCount--;
// let derived classes know when all is done // let derived classes know when all is done
if (_animCount == 0) { if (_animCount == 0) {
allAnimationsFinished(); allAnimationsFinished();
}
} }
} }
@@ -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;
}
@@ -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);
}
@@ -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; package com.threerings.media.animation;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Rectangle; import java.awt.Rectangle;
import com.threerings.media.sprite.PathCompletedEvent;
import com.threerings.media.sprite.Sprite; import com.threerings.media.sprite.Sprite;
import com.threerings.media.sprite.SpriteEvent;
import com.threerings.media.sprite.SpriteManager; import com.threerings.media.sprite.SpriteManager;
import com.threerings.media.sprite.SpriteObserver; import com.threerings.media.sprite.PathObserver;
import com.threerings.media.util.Path; import com.threerings.media.util.Path;
public class SpriteAnimation extends Animation public class SpriteAnimation extends Animation
implements SpriteObserver implements PathObserver
{ {
/** /**
* Constructs a sprite animation for the given sprite. The first time * Constructs a sprite animation for the given sprite. The first time
@@ -54,13 +52,17 @@ public class SpriteAnimation extends Animation
// nothing for now // nothing for now
} }
// documentation inherited // documentation inherited from interface
public void handleEvent (SpriteEvent event) 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 // documentation inherited
protected void didFinish () protected void didFinish ()