Add orx-timer
This commit is contained in:
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++
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user