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
This commit is contained in:
Walter Korman
2001-08-23 00:23:58 +00:00
parent 0e61a42004
commit f15738ca20
4 changed files with 61 additions and 26 deletions
@@ -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; package com.threerings.media.sprite;
import javax.swing.JComponent;
/** /**
* A view that wishes to interact with the animation manager needs to * A view that wishes to interact with the animation manager needs to
* implement this interface to give the animation manager a means by which * implement this interface to give the animation manager a means by which
@@ -26,4 +28,11 @@ public interface AnimatedView
* paint. * paint.
*/ */
public void paintImmediately (); 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 ();
} }
@@ -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; package com.threerings.media.sprite;
import java.awt.Graphics; import java.awt.Graphics;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.event.*;
import com.samskivert.util.Interval; import com.samskivert.util.Interval;
import com.samskivert.util.IntervalManager; 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 * view to allow for animation. It also may someday manage special
* scene-wide animations, such as rain, fog, or earthquakes. * 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 * Construct and initialize the animation manager with a sprite
@@ -31,6 +33,19 @@ public class AnimationManager implements Interval, PerformanceObserver
_spritemgr = spritemgr; _spritemgr = spritemgr;
_view = view; _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 // create a ticker for queueing up tick requests on the AWT thread
_ticker = new Runnable() { _ticker = new Runnable() {
public void run () public void run ()
@@ -41,20 +56,12 @@ public class AnimationManager implements Interval, PerformanceObserver
} }
/** /**
* This method should be called by the component the animation * Register the animation manager's refresh interval with the
* manager is animating after it's been fully laid out within its * interval manager.
* container(s); typically, after <code>Component.doLayout()</code>
* has been called. The animation manager will then register
* itself with the <code>IntervalManager</code> to effect its
* periodic repainting of the target component.
*/ */
public void start () protected void registerInterval ()
{ {
// register to monitor the refresh action _iid = IntervalManager.register(this, REFRESH_INTERVAL, null, true);
PerformanceMonitor.register(this, "refresh", 1000);
// register ourselves with the interval manager
IntervalManager.register(this, REFRESH_INTERVAL, null, true);
} }
/** /**
@@ -133,6 +140,25 @@ public class AnimationManager implements Interval, PerformanceObserver
Log.info(name + " [ticks=" + ticks + "]."); 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. */ /** The ticker runnable that we put on the AWT thread periodically. */
protected Runnable _ticker; protected Runnable _ticker;
@@ -145,6 +171,9 @@ public class AnimationManager implements Interval, PerformanceObserver
/** The number of outstanding tick requests. */ /** The number of outstanding tick requests. */
protected int _ticking = 0; protected int _ticking = 0;
/** The refresh interval id. */
protected int _iid = -1;
/** The sprite manager. */ /** The sprite manager. */
protected SpriteManager _spritemgr; protected SpriteManager _spritemgr;
@@ -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; package com.threerings.miso.scene;
import java.awt.*; import java.awt.*;
import java.util.List; import java.util.List;
import javax.swing.JPanel; import javax.swing.*;
import com.threerings.media.sprite.*; import com.threerings.media.sprite.*;
import com.threerings.media.tile.TileManager; import com.threerings.media.tile.TileManager;
@@ -94,6 +94,11 @@ public class SceneViewPanel
} }
} }
public JComponent getComponent ()
{
return this;
}
public void paintComponent (Graphics g) public void paintComponent (Graphics g)
{ {
render(g); render(g);
@@ -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; package com.threerings.miso.viewer;
@@ -78,14 +78,6 @@ public class ViewerSceneViewPanel extends SceneViewPanel
Log.info(name + " [ticks=" + ticks + "]."); 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 */ /** MouseListener interface methods */
public void mousePressed (MouseEvent e) public void mousePressed (MouseEvent e)