Add orx-timer
This commit is contained in:
39
orx-timer/README.md
Normal file
39
orx-timer/README.md
Normal file
@@ -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)
|
||||||
16
orx-timer/build.gradle
Normal file
16
orx-timer/build.gradle
Normal file
@@ -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)
|
||||||
|
}
|
||||||
13
orx-timer/src/demo/kotlin/DemoRepeat01.kt
Normal file
13
orx-timer/src/demo/kotlin/DemoRepeat01.kt
Normal file
@@ -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 {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
27
orx-timer/src/demo/kotlin/DemoRepeat02.kt
Normal file
27
orx-timer/src/demo/kotlin/DemoRepeat02.kt
Normal file
@@ -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<Any?>().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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
10
orx-timer/src/demo/kotlin/DemoTimeOut01.kt
Normal file
10
orx-timer/src/demo/kotlin/DemoTimeOut01.kt
Normal file
@@ -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" )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
23
orx-timer/src/main/kotlin/Timer.kt
Normal file
23
orx-timer/src/main/kotlin/Timer.kt
Normal file
@@ -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++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,6 +33,7 @@ include 'orx-camera',
|
|||||||
'orx-shapes',
|
'orx-shapes',
|
||||||
'orx-syphon',
|
'orx-syphon',
|
||||||
'orx-temporal-blur',
|
'orx-temporal-blur',
|
||||||
|
'orx-timer',
|
||||||
'orx-kinect-common',
|
'orx-kinect-common',
|
||||||
'orx-kinect-v1',
|
'orx-kinect-v1',
|
||||||
'orx-kinect-v1-natives-linux-arm64',
|
'orx-kinect-v1-natives-linux-arm64',
|
||||||
|
|||||||
Reference in New Issue
Block a user