From f15738ca20f28e3cbc145041a140b66598787f51 Mon Sep 17 00:00:00 2001 From: Walter Korman Date: Thu, 23 Aug 2001 00:23:58 +0000 Subject: [PATCH] Modified the animation manager to monitor the ancestor events associated with the animated view's component so that the refresh interval can be registered and unregistered based on component visibility. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@311 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/media/sprite/AnimatedView.java | 11 +++- .../media/sprite/AnimationManager.java | 57 ++++++++++++++----- .../miso/client/SceneViewPanel.java | 9 ++- .../miso/viewer/ViewerSceneViewPanel.java | 10 +--- 4 files changed, 61 insertions(+), 26 deletions(-) diff --git a/src/java/com/threerings/media/sprite/AnimatedView.java b/src/java/com/threerings/media/sprite/AnimatedView.java index f3dd16940..7a787eecb 100644 --- a/src/java/com/threerings/media/sprite/AnimatedView.java +++ b/src/java/com/threerings/media/sprite/AnimatedView.java @@ -1,8 +1,10 @@ // -// $Id: AnimatedView.java,v 1.3 2001/08/22 02:14:57 mdb Exp $ +// $Id: AnimatedView.java,v 1.4 2001/08/23 00:23:58 shaper Exp $ package com.threerings.media.sprite; +import javax.swing.JComponent; + /** * A view that wishes to interact with the animation manager needs to * implement this interface to give the animation manager a means by which @@ -26,4 +28,11 @@ public interface AnimatedView * paint. */ public void paintImmediately (); + + /** + * Return the component associated with the view so that the + * animation manager can restrict its animation to when the + * component is actually visible. + */ + public JComponent getComponent (); } diff --git a/src/java/com/threerings/media/sprite/AnimationManager.java b/src/java/com/threerings/media/sprite/AnimationManager.java index 8711616ed..72798b5be 100644 --- a/src/java/com/threerings/media/sprite/AnimationManager.java +++ b/src/java/com/threerings/media/sprite/AnimationManager.java @@ -1,11 +1,12 @@ // -// $Id: AnimationManager.java,v 1.14 2001/08/22 02:14:57 mdb Exp $ +// $Id: AnimationManager.java,v 1.15 2001/08/23 00:23:58 shaper Exp $ package com.threerings.media.sprite; import java.awt.Graphics; import javax.swing.JComponent; import javax.swing.SwingUtilities; +import javax.swing.event.*; import com.samskivert.util.Interval; import com.samskivert.util.IntervalManager; @@ -19,7 +20,8 @@ import com.threerings.miso.util.PerformanceObserver; * view to allow for animation. It also may someday manage special * scene-wide animations, such as rain, fog, or earthquakes. */ -public class AnimationManager implements Interval, PerformanceObserver +public class AnimationManager + implements Interval, PerformanceObserver, AncestorListener { /** * Construct and initialize the animation manager with a sprite @@ -31,6 +33,19 @@ public class AnimationManager implements Interval, PerformanceObserver _spritemgr = spritemgr; _view = view; + // register to monitor the refresh action + PerformanceMonitor.register(this, "refresh", 1000); + + // register the refresh interval immediately if the component + // is already showing on-screen + JComponent target = _view.getComponent(); + if (target.isShowing()) { + registerInterval(); + } + + // listen to the view's ancestor events + target.addAncestorListener(this); + // create a ticker for queueing up tick requests on the AWT thread _ticker = new Runnable() { public void run () @@ -41,20 +56,12 @@ public class AnimationManager implements Interval, PerformanceObserver } /** - * This method should be called by the component the animation - * manager is animating after it's been fully laid out within its - * container(s); typically, after Component.doLayout() - * has been called. The animation manager will then register - * itself with the IntervalManager to effect its - * periodic repainting of the target component. + * Register the animation manager's refresh interval with the + * interval manager. */ - public void start () + protected void registerInterval () { - // register to monitor the refresh action - PerformanceMonitor.register(this, "refresh", 1000); - - // register ourselves with the interval manager - IntervalManager.register(this, REFRESH_INTERVAL, null, true); + _iid = IntervalManager.register(this, REFRESH_INTERVAL, null, true); } /** @@ -133,6 +140,25 @@ public class AnimationManager implements Interval, PerformanceObserver Log.info(name + " [ticks=" + ticks + "]."); } + /** AncestorListener interface methods. */ + + public void ancestorAdded (AncestorEvent event) + { + if (_iid == -1) { + // register the refresh interval since we're now visible + registerInterval(); + } + } + + public void ancestorRemoved (AncestorEvent event) + { + // un-register the refresh interval since we're now hidden + IntervalManager.remove(_iid); + _iid = -1; + } + + public void ancestorMoved (AncestorEvent event) { } + /** The ticker runnable that we put on the AWT thread periodically. */ protected Runnable _ticker; @@ -145,6 +171,9 @@ public class AnimationManager implements Interval, PerformanceObserver /** The number of outstanding tick requests. */ protected int _ticking = 0; + /** The refresh interval id. */ + protected int _iid = -1; + /** The sprite manager. */ protected SpriteManager _spritemgr; diff --git a/src/java/com/threerings/miso/client/SceneViewPanel.java b/src/java/com/threerings/miso/client/SceneViewPanel.java index eebfc9d5f..ce9050382 100644 --- a/src/java/com/threerings/miso/client/SceneViewPanel.java +++ b/src/java/com/threerings/miso/client/SceneViewPanel.java @@ -1,11 +1,11 @@ // -// $Id: SceneViewPanel.java,v 1.11 2001/08/22 02:14:57 mdb Exp $ +// $Id: SceneViewPanel.java,v 1.12 2001/08/23 00:23:58 shaper Exp $ package com.threerings.miso.scene; import java.awt.*; import java.util.List; -import javax.swing.JPanel; +import javax.swing.*; import com.threerings.media.sprite.*; import com.threerings.media.tile.TileManager; @@ -94,6 +94,11 @@ public class SceneViewPanel } } + public JComponent getComponent () + { + return this; + } + public void paintComponent (Graphics g) { render(g); diff --git a/tests/src/java/com/threerings/miso/viewer/ViewerSceneViewPanel.java b/tests/src/java/com/threerings/miso/viewer/ViewerSceneViewPanel.java index f525c9b80..79cb55167 100644 --- a/tests/src/java/com/threerings/miso/viewer/ViewerSceneViewPanel.java +++ b/tests/src/java/com/threerings/miso/viewer/ViewerSceneViewPanel.java @@ -1,5 +1,5 @@ // -// $Id: ViewerSceneViewPanel.java,v 1.12 2001/08/21 20:02:39 mdb Exp $ +// $Id: ViewerSceneViewPanel.java,v 1.13 2001/08/23 00:23:58 shaper Exp $ package com.threerings.miso.viewer; @@ -78,14 +78,6 @@ public class ViewerSceneViewPanel extends SceneViewPanel Log.info(name + " [ticks=" + ticks + "]."); } - public void doLayout () - { - super.doLayout(); - - // now that we've been fully laid out, start animating - _animmgr.start(); - } - /** MouseListener interface methods */ public void mousePressed (MouseEvent e)