From 23d170c83e262b0ae7592216d83c662d0e6a0e4b Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 25 Apr 2002 16:23:31 +0000 Subject: [PATCH] Added support for pausing a media panel which pauses all sprites and animations currently operating therein. (It doesn't pause scrolling which I suppose I'll have to fix at some point if we ever need it.) git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1292 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/java/com/threerings/media/MediaPanel.java | 40 ++++++++++++++++++- .../threerings/media/animation/Animation.java | 12 +++++- .../media/animation/AnimationManager.java | 18 ++++++++- .../media/animation/BobbleAnimation.java | 8 +++- .../media/animation/ExplodeAnimation.java | 8 +++- .../media/animation/FadeAnimation.java | 8 +++- .../media/animation/RainAnimation.java | 8 +++- .../com/threerings/media/sprite/Sprite.java | 16 +++++++- .../media/sprite/SpriteManager.java | 18 ++++++++- .../media/util/LineSegmentPath.java | 8 +++- src/java/com/threerings/media/util/Path.java | 10 ++++- 11 files changed, 143 insertions(+), 11 deletions(-) diff --git a/src/java/com/threerings/media/MediaPanel.java b/src/java/com/threerings/media/MediaPanel.java index 99d4c31ee..75d37e869 100644 --- a/src/java/com/threerings/media/MediaPanel.java +++ b/src/java/com/threerings/media/MediaPanel.java @@ -1,5 +1,5 @@ // -// $Id: MediaPanel.java,v 1.2 2002/04/23 03:10:39 mdb Exp $ +// $Id: MediaPanel.java,v 1.3 2002/04/25 16:23:30 mdb Exp $ package com.threerings.media; @@ -131,6 +131,41 @@ public class MediaPanel extends JComponent // ", millis=" + millis + "ms, mspp=" + mspp + "]."); } + /** + * Pauses the sprites and animations that are currently active on this + * media panel. Also stops listening to the frame tick while paused. + */ + public void setPaused (boolean paused) + { + // sanity check + if ((paused && (_pauseTime != 0)) || + (!paused && (_pauseTime == 0))) { + Log.warning("Requested to pause when paused or vice-versa " + + "[paused=" + paused + "]."); + return; + } + + if (paused) { + // stop listening to the frame tick + _framemgr.removeFrameParticipant(this); + // make a note of our pause time + _pauseTime = System.currentTimeMillis(); + + } else { + // start listening to the frame tick once again + _framemgr.registerFrameParticipant(this); + + // let the animation and sprite managers know that we just + // warped into the future + long delta = System.currentTimeMillis() - _pauseTime; + _animmgr.fastForward(delta); + _spritemgr.fastForward(delta); + + // clear out our pause time + _pauseTime = 0; + } + } + /** * Returns a reference to the region manager that is used to * accumulate dirty regions each frame. @@ -483,4 +518,7 @@ public class MediaPanel extends JComponent /** Used to correlate tick()s with paint()s. */ protected boolean _tickPaintPending = false; + + /** Used to track the clock time at which we were paused. */ + protected long _pauseTime; } diff --git a/src/java/com/threerings/media/animation/Animation.java b/src/java/com/threerings/media/animation/Animation.java index c40e58929..71d0b9ac0 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.5 2002/04/23 01:16:28 mdb Exp $ +// $Id: Animation.java,v 1.6 2002/04/25 16:23:30 mdb Exp $ package com.threerings.media.animation; @@ -75,6 +75,16 @@ public abstract class Animation */ public abstract void paint (Graphics2D gfx); + /** + * This is called if the animation manager is paused for some length + * of time and then unpaused. Animations should adjust any time stamps + * they are maintaining internally by the delta so that time maintains + * the illusion of flowing smoothly forward. + */ + public void fastForward (long timeDelta) + { + } + /** * Returns true if the animation has finished all of its business, * false if not. diff --git a/src/java/com/threerings/media/animation/AnimationManager.java b/src/java/com/threerings/media/animation/AnimationManager.java index ed4884909..b758b6494 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.9 2002/04/23 01:16:28 mdb Exp $ +// $Id: AnimationManager.java,v 1.10 2002/04/25 16:23:30 mdb Exp $ package com.threerings.media.animation; @@ -102,6 +102,22 @@ public class AnimationManager } } + /** + * If the animation manager is paused for some length of time, it + * should be fast forwarded by the appropriate number of milliseconds. + * This allows animations to smoothly pick up where they left off + * rather than abruptly jumping into the future, thinking that some + * outrageous amount of time passed since their last tick. + */ + public void fastForward (long timeDelta) + { + // fast forward all of our animations + int size = _anims.size(); + for (int ii = 0; ii < size; ii++) { + ((Animation)_anims.get(ii)).fastForward(timeDelta); + } + } + /** * Renders all registered animations in the given layer that intersect * the supplied clipping rectangle to the given graphics context. diff --git a/src/java/com/threerings/media/animation/BobbleAnimation.java b/src/java/com/threerings/media/animation/BobbleAnimation.java index 76f43605d..a407c59cc 100644 --- a/src/java/com/threerings/media/animation/BobbleAnimation.java +++ b/src/java/com/threerings/media/animation/BobbleAnimation.java @@ -1,5 +1,5 @@ // -// $Id: BobbleAnimation.java,v 1.3 2002/04/15 18:18:20 mdb Exp $ +// $Id: BobbleAnimation.java,v 1.4 2002/04/25 16:23:30 mdb Exp $ package com.threerings.media.animation; @@ -58,6 +58,12 @@ public class BobbleAnimation extends Animation _y = (_sy + dy) * ((RandomUtil.getInt(2) == 0) ? -1 : 1); } + // documentation inherited + public void fastForward (long timeDelta) + { + _end += timeDelta; + } + // documentation inherited public void paint (Graphics2D gfx) { diff --git a/src/java/com/threerings/media/animation/ExplodeAnimation.java b/src/java/com/threerings/media/animation/ExplodeAnimation.java index 74344987c..49f5baea0 100644 --- a/src/java/com/threerings/media/animation/ExplodeAnimation.java +++ b/src/java/com/threerings/media/animation/ExplodeAnimation.java @@ -1,5 +1,5 @@ // -// $Id: ExplodeAnimation.java,v 1.7 2002/04/15 18:18:20 mdb Exp $ +// $Id: ExplodeAnimation.java,v 1.8 2002/04/25 16:23:30 mdb Exp $ package com.threerings.media.animation; @@ -134,6 +134,12 @@ public class ExplodeAnimation extends Animation _start = timestamp; } + // documentation inherited + public void fastForward (long timeDelta) + { + _start += timeDelta; + } + // documentation inherited public void tick (long timestamp) { diff --git a/src/java/com/threerings/media/animation/FadeAnimation.java b/src/java/com/threerings/media/animation/FadeAnimation.java index eb1445da3..26e0e60ed 100644 --- a/src/java/com/threerings/media/animation/FadeAnimation.java +++ b/src/java/com/threerings/media/animation/FadeAnimation.java @@ -1,5 +1,5 @@ // -// $Id: FadeAnimation.java,v 1.3 2002/01/31 17:35:41 shaper Exp $ +// $Id: FadeAnimation.java,v 1.4 2002/04/25 16:23:30 mdb Exp $ package com.threerings.media.animation; @@ -76,6 +76,12 @@ public class FadeAnimation extends Animation invalidate(); } + // documentation inherited + public void fastForward (long timeDelta) + { + _start += timeDelta; + } + // documentation inherited public void paint (Graphics2D gfx) { diff --git a/src/java/com/threerings/media/animation/RainAnimation.java b/src/java/com/threerings/media/animation/RainAnimation.java index a36b6b506..ad7698834 100644 --- a/src/java/com/threerings/media/animation/RainAnimation.java +++ b/src/java/com/threerings/media/animation/RainAnimation.java @@ -1,5 +1,5 @@ // -// $Id: RainAnimation.java,v 1.3 2002/04/23 01:16:28 mdb Exp $ +// $Id: RainAnimation.java,v 1.4 2002/04/25 16:23:30 mdb Exp $ package com.threerings.media.animation; @@ -73,6 +73,12 @@ public class RainAnimation extends Animation invalidate(); } + // documentation inherited + public void fastForward (long timeDelta) + { + _end += timeDelta; + } + // documentation inherited public void paint (Graphics2D gfx) { diff --git a/src/java/com/threerings/media/sprite/Sprite.java b/src/java/com/threerings/media/sprite/Sprite.java index c7f5bfe41..e0818b59a 100644 --- a/src/java/com/threerings/media/sprite/Sprite.java +++ b/src/java/com/threerings/media/sprite/Sprite.java @@ -1,5 +1,5 @@ // -// $Id: Sprite.java,v 1.42 2002/04/23 01:16:28 mdb Exp $ +// $Id: Sprite.java,v 1.43 2002/04/25 16:23:30 mdb Exp $ package com.threerings.media.sprite; @@ -336,6 +336,20 @@ public abstract class Sprite } } + /** + * This is called if the sprite manager is paused for some length of + * time and then unpaused. Sprites should adjust any time stamps they + * are maintaining internally by the delta so that time maintains the + * illusion of flowing smoothly forward. + */ + public void fastForward (long timeDelta) + { + // fast forward any path we're following + if (_path != null) { + _path.fastForward(timeDelta); + } + } + /** * Updates the sprite's render offset which is used to determine * where to place the top-left corner of the render bounds. diff --git a/src/java/com/threerings/media/sprite/SpriteManager.java b/src/java/com/threerings/media/sprite/SpriteManager.java index 5c0601834..512a78eea 100644 --- a/src/java/com/threerings/media/sprite/SpriteManager.java +++ b/src/java/com/threerings/media/sprite/SpriteManager.java @@ -1,5 +1,5 @@ // -// $Id: SpriteManager.java,v 1.29 2002/04/23 01:16:28 mdb Exp $ +// $Id: SpriteManager.java,v 1.30 2002/04/25 16:23:31 mdb Exp $ package com.threerings.media.sprite; @@ -209,6 +209,22 @@ public class SpriteManager handleSpriteEvents(); } + /** + * If the sprite manager is paused for some length of time, it should + * be fast forwarded by the appropriate number of milliseconds. This + * allows sprites to smoothly pick up where they left off rather than + * abruptly jumping into the future, thinking that some outrageous + * amount of time passed since their last tick. + */ + public void fastForward (long timeDelta) + { + int size = _sprites.size(); + for (int ii = 0; ii < size; ii++) { + Sprite sprite = (Sprite)_sprites.get(ii); + sprite.fastForward(timeDelta); + } + } + /** * Call {@link Sprite#tick} on all sprite objects to give them a * chance to move themselves about, change their display image, diff --git a/src/java/com/threerings/media/util/LineSegmentPath.java b/src/java/com/threerings/media/util/LineSegmentPath.java index b7081977c..22d8e2df0 100644 --- a/src/java/com/threerings/media/util/LineSegmentPath.java +++ b/src/java/com/threerings/media/util/LineSegmentPath.java @@ -1,5 +1,5 @@ // -// $Id: LineSegmentPath.java,v 1.20 2002/04/16 20:38:24 mdb Exp $ +// $Id: LineSegmentPath.java,v 1.21 2002/04/25 16:23:30 mdb Exp $ package com.threerings.media.sprite; @@ -212,6 +212,12 @@ public class LineSegmentPath return false; } + // documentation inherited + public void fastForward (long timeDelta) + { + _nodestamp += timeDelta; + } + /** * Place the sprite moving along the path at the end of the * previous path node, face it appropriately for the next node, diff --git a/src/java/com/threerings/media/util/Path.java b/src/java/com/threerings/media/util/Path.java index a25c40ab6..fa194c16b 100644 --- a/src/java/com/threerings/media/util/Path.java +++ b/src/java/com/threerings/media/util/Path.java @@ -1,5 +1,5 @@ // -// $Id: Path.java,v 1.4 2002/04/23 01:16:28 mdb Exp $ +// $Id: Path.java,v 1.5 2002/04/25 16:23:30 mdb Exp $ package com.threerings.media.sprite; @@ -41,6 +41,14 @@ public interface Path */ public boolean updatePosition (Sprite sprite, long tickStamp); + /** + * This is called if the sprite manager is paused for some length of + * time and then unpaused. Paths should adjust any time stamps they + * are maintaining internally by the delta so that time maintains the + * illusion of flowing smoothly forward. + */ + public void fastForward (long timeDelta); + /** * Sets the velocity of this sprite in pixels per millisecond. The * velocity is measured as pixels traversed along the path that the