diff --git a/orx-timer/README.md b/orx-timer/README.md new file mode 100644 index 00000000..ac375d8a --- /dev/null +++ b/orx-timer/README.md @@ -0,0 +1,39 @@ +# orx-timer + +`orx-timer` adds simple timer functionality to OPENRNDR's Program + +## Prerequisites + +Add `orx-timer` to the `orxFeatures` set in your build.gradle.kts + +## Usage + +`orx-timer` facilitates two extension functions for `Program` + +`fun Program.repeat(intervalInSeconds: Double, count: Int? = null, initialDelayInSeconds: Double = 0.0, action: () -> Unit)` + +`fun Program.timeOut(delayInSeconds: Double, action: () -> Unit)` + +A simple example looks like this: + +```$kotlin +fun main() = application { + program { + repeat(2.0) { + println("hello there ) + } + extend { + + } + } +} +``` + +Note that drawing inside the `repeat` action has no effect. Have a look at the demos listed below for an example of +`repeat` triggered drawing. + +## Demos + + * [Simple `repeat` demonstration](src/demo/kotlin/DemoRepeat01.kt) + * [A `repeat` demonstration with drawing](src/demo/kotlin/DemoRepeat02.kt) + * [Simple `timeOut` demonstration](src/demo/kotlin/DemoTimeOut01.kt) \ No newline at end of file diff --git a/orx-timer/build.gradle b/orx-timer/build.gradle new file mode 100644 index 00000000..49bda9c8 --- /dev/null +++ b/orx-timer/build.gradle @@ -0,0 +1,16 @@ +sourceSets { + demo { + java { + srcDirs = ["src/demo/kotlin"] + compileClasspath += main.getCompileClasspath() + runtimeClasspath += main.getRuntimeClasspath() + } + } +} + +dependencies { + demoImplementation("org.openrndr:openrndr-core:$openrndrVersion") + demoRuntimeOnly("org.openrndr:openrndr-gl3:$openrndrVersion") + demoRuntimeOnly("org.openrndr:openrndr-gl3-natives-$openrndrOS:$openrndrVersion") + demoImplementation(sourceSets.getByName("main").output) +} \ No newline at end of file diff --git a/orx-timer/src/demo/kotlin/DemoRepeat01.kt b/orx-timer/src/demo/kotlin/DemoRepeat01.kt new file mode 100644 index 00000000..f3ed5106 --- /dev/null +++ b/orx-timer/src/demo/kotlin/DemoRepeat01.kt @@ -0,0 +1,13 @@ +import org.openrndr.application +import org.openrndr.extra.timer.repeat + +fun main() = application { + program { + repeat(2.0) { + println("hello there $seconds" ) + } + extend { + + } + } +} \ No newline at end of file diff --git a/orx-timer/src/demo/kotlin/DemoRepeat02.kt b/orx-timer/src/demo/kotlin/DemoRepeat02.kt new file mode 100644 index 00000000..e5e310c1 --- /dev/null +++ b/orx-timer/src/demo/kotlin/DemoRepeat02.kt @@ -0,0 +1,27 @@ +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.events.Event +import org.openrndr.extra.timer.repeat + +/** + * This demonstrates how to combine `repeat {}` with a postponed event to trigger drawing + */ + +fun main() = application { + program { + val event = Event().postpone(true) + event.listen { + drawer.circle(width / 2.0, height / 2.0, 200.0) + } + repeat(2.0) { + // -- we can not draw here, so we relay the repeat signal to the event + event.trigger(null) + } + extend { + drawer.background(ColorRGBa.PINK) + // -- by explicitly calling deliver we know that the drawing code in the listener will be + // -- executed exactly here + event.deliver() + } + } +} \ No newline at end of file diff --git a/orx-timer/src/demo/kotlin/DemoTimeOut01.kt b/orx-timer/src/demo/kotlin/DemoTimeOut01.kt new file mode 100644 index 00000000..3fd0296d --- /dev/null +++ b/orx-timer/src/demo/kotlin/DemoTimeOut01.kt @@ -0,0 +1,10 @@ +import org.openrndr.application +import org.openrndr.extra.timer.timeOut + +fun main() = application { + program { + timeOut(2.0) { + println("hello there $seconds" ) + } + } +} \ No newline at end of file diff --git a/orx-timer/src/main/kotlin/Timer.kt b/orx-timer/src/main/kotlin/Timer.kt new file mode 100644 index 00000000..be6b835f --- /dev/null +++ b/orx-timer/src/main/kotlin/Timer.kt @@ -0,0 +1,23 @@ +package org.openrndr.extra.timer + +import kotlinx.coroutines.yield +import org.openrndr.Program +import org.openrndr.launch + +fun Program.timeOut(delayInSeconds: Double, action: () -> Unit) = repeat(1.0, 1, delayInSeconds, action) + +fun Program.repeat(intervalInSeconds: Double, count: Int? = null, initialDelayInSeconds: Double = 0.0, action: () -> Unit) { + val start = seconds + initialDelayInSeconds + var repetitions = 0 + + launch { + while (count == null || repetitions < count) { + val launchTime = start + repetitions * intervalInSeconds + while (seconds < launchTime) { + yield() + } + action() + repetitions++ + } + } +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 7fbbb9ca..3af9efd2 100644 --- a/settings.gradle +++ b/settings.gradle @@ -33,6 +33,7 @@ include 'orx-camera', 'orx-shapes', 'orx-syphon', 'orx-temporal-blur', + 'orx-timer', 'orx-kinect-common', 'orx-kinect-v1', 'orx-kinect-v1-natives-linux-arm64',