diff options
author | Hans-Christoph Steiner <eighthave@users.sourceforge.net> | 2008-03-22 02:15:12 +0000 |
---|---|---|
committer | Hans-Christoph Steiner <eighthave@users.sourceforge.net> | 2008-03-22 02:15:12 +0000 |
commit | a764e59e1d3a8e330f0d484fdb26b35ca3f0b2e4 (patch) | |
tree | c4ecadccdecf2809b99c0da0545f255a6ad25bb5 /src/java/com/cycling74/max/MaxClock.java |
bringing pdj-0.8.3 into the main branchsvn2git-root
svn path=/trunk/externals/loaders/pdj/; revision=9621
Diffstat (limited to 'src/java/com/cycling74/max/MaxClock.java')
-rw-r--r-- | src/java/com/cycling74/max/MaxClock.java | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/java/com/cycling74/max/MaxClock.java b/src/java/com/cycling74/max/MaxClock.java new file mode 100644 index 0000000..5c5acbb --- /dev/null +++ b/src/java/com/cycling74/max/MaxClock.java @@ -0,0 +1,113 @@ +package com.cycling74.max; + +/** + * Used to delay the execution of a block of code. Java implementation + * of a pdclock. + * <p><blockquote><pre> + * + * import com.cycling74.max.*; + * + * class clocktest extends MaxObject implements Executable { + * MaxClock clock; + * float value; + * + * public clocktest() { + * clock = new MaxClock(this); + * } + * + * public void inlet(float f) { + * value = f; + * // ask to call execute after 250ms + * clock.delay(250); + * } + * + * // this is called after 250ms + * public void execute() { + * outlet(0, value) + * } + * } + * </pre></blockquote></p> + */ +public class MaxClock { + private Executable exec; + private long _clock_ptr; + + /** + * Creates a pdclock without an executable. + */ + public MaxClock() { + create_clock(); + } + + /** + * Creates a pdclock with an executable <code>e</code>. + * @param e the executable to execute when the clock will be triggerd. + */ + public MaxClock(Executable e) { + create_clock(); + exec = e; + } + + /** + * Creates a pdclock with a specific method on a object. + * @param o the object that holds the method + * @param methodName the name of the method to execute when the clock + * will be triggerd. + */ + public MaxClock(Object o, String methodName) { + create_clock(); + exec = new Callback(o, methodName); + } + + /** + * Returns the Executable for this clock. + * @return the Executable for this clock + */ + public Executable getExecutable() { + return exec; + } + + /** + * Set the Executable for this clock. + * @param e the Executable to call for this clock + */ + public void setExecutable(Executable e) { + exec = e; + } + + /** + * The method to override if no Executable is provided. + */ + public void tick() { + exec.execute(); + } + + protected void finalize() throws Throwable { + release(); + super.finalize(); + } + + /** + * Returns pure-data time in milliseconds. + * @return pure-data time in milliseconds + */ + public static native double getTime(); + + /** + * Time to wait until next tick. + * @param time in miliseconds + */ + public native void delay(double time); + + /** + * Release the clock from pure-data. The clock becomes unless afterwards. + */ + public native void release(); + + /** + * Cancels the last delay call. + */ + public native void unset(); + + private native void create_clock(); +} |