Pulled starting and stopping code out into start() and stop().
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@797 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: AnimationManager.java,v 1.19 2001/12/16 05:43:00 shaper Exp $
|
// $Id: AnimationManager.java,v 1.20 2001/12/16 08:05:06 mdb Exp $
|
||||||
|
|
||||||
package com.threerings.media.sprite;
|
package com.threerings.media.sprite;
|
||||||
|
|
||||||
@@ -37,43 +37,54 @@ public class AnimationManager
|
|||||||
// register to monitor the refresh action
|
// register to monitor the refresh action
|
||||||
PerformanceMonitor.register(this, "refresh", 1000);
|
PerformanceMonitor.register(this, "refresh", 1000);
|
||||||
|
|
||||||
// register the refresh interval immediately if the component
|
// register the refresh interval immediately if the component is
|
||||||
// is already showing on-screen
|
// already showing on-screen
|
||||||
JComponent target = _view.getComponent();
|
JComponent target = _view.getComponent();
|
||||||
if (target.isShowing()) {
|
if (target.isShowing()) {
|
||||||
registerInterval();
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
// listen to the view's ancestor events
|
// listen to the view's ancestor events
|
||||||
target.addAncestorListener(new AncestorAdapter() {
|
target.addAncestorListener(new AncestorAdapter() {
|
||||||
public void ancestorAdded (AncestorEvent event) {
|
public void ancestorAdded (AncestorEvent event) {
|
||||||
if (_iid == -1) {
|
// start ourselves up when our animated view is added
|
||||||
// register the refresh interval since we're now visible
|
start();
|
||||||
registerInterval();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
public void ancestorRemoved (AncestorEvent event) {
|
public void ancestorRemoved (AncestorEvent event) {
|
||||||
// un-register the refresh interval since we're now hidden
|
// automatically shutdown if our animated view is hidden
|
||||||
IntervalManager.remove(_iid);
|
stop();
|
||||||
_iid = -1;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// create a ticker for queueing up tick requests on the AWT thread
|
|
||||||
_ticker = new Runnable() {
|
|
||||||
public void run () {
|
|
||||||
tick();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the animation manager's refresh interval with the
|
* Starts the animation manager to doing its business.
|
||||||
* interval manager.
|
|
||||||
*/
|
*/
|
||||||
protected void registerInterval ()
|
public synchronized void start ()
|
||||||
{
|
{
|
||||||
_iid = IntervalManager.register(this, REFRESH_INTERVAL, null, true);
|
if (_ticker == null) {
|
||||||
|
// create ticker for queueing up tick requests on AWT thread
|
||||||
|
_ticker = new Runnable() {
|
||||||
|
public void run () {
|
||||||
|
tick();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// register the refresh interval
|
||||||
|
_iid = IntervalManager.register(this, REFRESH_INTERVAL, null, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instructs the animation manager to stop doing its business.
|
||||||
|
*/
|
||||||
|
public synchronized void stop ()
|
||||||
|
{
|
||||||
|
if (_ticker != null) {
|
||||||
|
_ticker = null;
|
||||||
|
// un-register the refresh interval since we're now hidden
|
||||||
|
IntervalManager.remove(_iid);
|
||||||
|
_iid = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -83,8 +94,7 @@ public class AnimationManager
|
|||||||
*/
|
*/
|
||||||
protected synchronized boolean requestTick ()
|
protected synchronized boolean requestTick ()
|
||||||
{
|
{
|
||||||
if (_ticking++ > 0) return false;
|
return !(_ticking++ > 0);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -112,7 +122,7 @@ public class AnimationManager
|
|||||||
{
|
{
|
||||||
if (requestTick()) {
|
if (requestTick()) {
|
||||||
// throw the tick task on the AWT thread task queue
|
// throw the tick task on the AWT thread task queue
|
||||||
SwingUtilities.invokeLater(_ticker);
|
queueTick();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,6 +132,13 @@ public class AnimationManager
|
|||||||
*/
|
*/
|
||||||
protected void tick ()
|
protected void tick ()
|
||||||
{
|
{
|
||||||
|
synchronized (this) {
|
||||||
|
// see if we were shutdown since we were last queued up
|
||||||
|
if (_ticker == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// every tick should have a timestamp associated with it
|
// every tick should have a timestamp associated with it
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
|
|
||||||
@@ -146,6 +163,17 @@ public class AnimationManager
|
|||||||
// request for at least one more tick since we started
|
// request for at least one more tick since we started
|
||||||
// this tick, so we want to queue up another tick
|
// this tick, so we want to queue up another tick
|
||||||
// immediately
|
// immediately
|
||||||
|
queueTick();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Queues up a tick on the AWT event handler thread, iff we are still
|
||||||
|
* operating.
|
||||||
|
*/
|
||||||
|
protected synchronized void queueTick ()
|
||||||
|
{
|
||||||
|
if (_ticker != null) {
|
||||||
SwingUtilities.invokeLater(_ticker);
|
SwingUtilities.invokeLater(_ticker);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -156,12 +184,6 @@ public class AnimationManager
|
|||||||
Log.info(name + " [ticks=" + ticks + "].");
|
Log.info(name + " [ticks=" + ticks + "].");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The desired number of refresh operations per second. */
|
|
||||||
protected static final int FRAME_RATE = 70;
|
|
||||||
|
|
||||||
/** The milliseconds to sleep to obtain desired frame rate. */
|
|
||||||
protected static final long REFRESH_INTERVAL = 1000 / FRAME_RATE;
|
|
||||||
|
|
||||||
/** 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;
|
||||||
|
|
||||||
@@ -176,4 +198,10 @@ public class AnimationManager
|
|||||||
|
|
||||||
/** The view on which we are animating. */
|
/** The view on which we are animating. */
|
||||||
protected AnimatedView _view;
|
protected AnimatedView _view;
|
||||||
|
|
||||||
|
/** The desired number of refresh operations per second. */
|
||||||
|
protected static final int FRAME_RATE = 70;
|
||||||
|
|
||||||
|
/** The milliseconds to sleep to obtain desired frame rate. */
|
||||||
|
protected static final long REFRESH_INTERVAL = 1000 / FRAME_RATE;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user