From 5a57753d844f695305dd46955fcb1d0537358af0 Mon Sep 17 00:00:00 2001 From: Mike Thomas Date: Wed, 21 Dec 2005 01:48:15 +0000 Subject: [PATCH] New client-side version of the Interval class which runs as a FrameParticipant, using the FrameManager's timing mechanism. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3791 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/media/FrameInterval.java | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/java/com/threerings/media/FrameInterval.java diff --git a/src/java/com/threerings/media/FrameInterval.java b/src/java/com/threerings/media/FrameInterval.java new file mode 100644 index 000000000..e5dc43f3a --- /dev/null +++ b/src/java/com/threerings/media/FrameInterval.java @@ -0,0 +1,110 @@ +package com.threerings.media; + +import java.awt.Component; + +import com.threerings.media.FrameManager; +import com.threerings.media.FrameParticipant; + +public abstract class FrameInterval + implements FrameParticipant +{ + /** + * Constructor - registers the interval as a frame participant + */ + public FrameInterval (FrameManager mgr) + { + _mgr = mgr; + } + + // documentation inhertied from FrameParticipant + public Component getComponent () + { + return null; + } + + // documentation inherited from FrameParticipant + public boolean needsPaint () + { + return false; + } + + // documentation inherited from FrameParticipant + public void tick (long tickStamp) + { + if (_nextTime == -1) { + // First time through + _nextTime = tickStamp + _initDelay; + } else if (tickStamp >= _nextTime) { + + // If we're repeating, set the next time to run, otherwise, reset + if (_repeatDelay != 0L) { + _nextTime += _repeatDelay; + } else { + _nextTime = -1; + } + expired(); + } + } + + /** + * + * The main method where your interval should do its work. + * + */ + public abstract void expired (); + + /** + * Schedule the interval to execute once, after the specified delay. + * Supersedes any previous schedule that this Interval may have had. + */ + public final void schedule (long delay) + { + schedule(delay, 0L); + } + + /** + * Schedule the interval to execute repeatedly, with the same delay. + * Supersedes any previous schedule that this Interval may have had. + */ + public final void schedule (long delay, boolean repeat) + { + schedule(delay, repeat ? delay : 0L); + } + + /** + * Schedule the interval to execute repeatedly with the specified + * initial delay and repeat delay. + * Supersedes any previous schedule that this Interval may have had. + */ + public final void schedule (long initialDelay, long repeatDelay) + { + if (!_mgr.isRegisteredFrameParticipant(this)) { + _mgr.registerFrameParticipant(this); + } + + _repeatDelay = repeatDelay; + _initDelay = initialDelay; + _nextTime = -1; + } + + /** + * Cancel the current schedule, and ensure that any expirations that + * are queued up but have not yet run do not run. + */ + public final void cancel () + { + _mgr.removeFrameParticipant(this); + } + + /** Time of the next expiration. */ + protected long _nextTime; + + /** Time between expirations. */ + protected long _repeatDelay; + + /** Time between expirations. */ + protected long _initDelay; + + /** The context whose FrameManager we are using. */ + protected FrameManager _mgr; +}