[orx-parameters, orx-property-watchers, orx-file-watcher, orx-gui] Add @PathParameter, file watcher delegates and property delegates

This commit is contained in:
Edwin Jakobs
2023-03-18 20:32:43 +01:00
parent bab525cd92
commit 84e623c3e8
20 changed files with 553 additions and 143 deletions

View File

@@ -0,0 +1,38 @@
import kotlinx.coroutines.yield
import org.openrndr.Program
import org.openrndr.events.Event
import org.openrndr.extra.filewatcher.watchFile
import org.openrndr.launch
import java.io.File
import kotlin.reflect.KProperty
class FileWatcherDelegate<T>(
program: Program,
file: File,
valueChangedEvent: Event<T>? = null,
requestStopEvent: Event<Unit>? = null,
transducer: (File) -> T
) {
val watchValue = watchFile(file, valueChangedEvent, requestStopEvent, transducer)
var value = watchValue()
init {
program.launch {
while (true) {
value = watchValue()
yield()
}
}
}
operator fun getValue(any: Any, property: KProperty<*>): T {
return value
}
}
fun <R> Program.watchingFile(
file: File,
valueChangedEvent: Event<R>? = null,
requestStopEvent: Event<Unit>? = null,
transducer: (File) -> R
) = FileWatcherDelegate(this, file, valueChangedEvent, requestStopEvent, transducer)