Add option to change the script file (such that orx-olive can be combined with file drag/drop)
This commit is contained in:
@@ -4,11 +4,12 @@ import org.openrndr.Extension
|
||||
import org.openrndr.Program
|
||||
import org.openrndr.draw.Session
|
||||
import org.openrndr.events.Event
|
||||
import org.operndr.extras.filewatcher.stop
|
||||
import org.operndr.extras.filewatcher.watchFile
|
||||
import java.io.File
|
||||
|
||||
|
||||
fun <T> Event<T>.saveListeners(store: MutableMap<Event<*>, List<(Any) -> Unit>>) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
store[this] = listeners.map { it } as List<(Any) -> Unit>
|
||||
}
|
||||
|
||||
@@ -20,12 +21,24 @@ class Olive<P : Program> : Extension {
|
||||
override var enabled: Boolean = true
|
||||
var session: Session? = null
|
||||
|
||||
|
||||
|
||||
var running = false
|
||||
|
||||
internal var scriptChange: (String)->Unit = {}
|
||||
|
||||
var script = "src/main/kotlin/live.kts"
|
||||
set(value) {
|
||||
scriptChange(value)
|
||||
field = value
|
||||
}
|
||||
|
||||
override fun setup(program: Program) {
|
||||
System.setProperty("idea.io.use.fallback", "true")
|
||||
System.setProperty("org.openrndr.ignoreShadeStyleErrors", "true")
|
||||
|
||||
var watcher: (() -> Unit)? = null
|
||||
|
||||
val store = mutableMapOf<Event<*>, List<(Any) -> Unit>>()
|
||||
|
||||
val originalExtensions = program.extensions.map { it }
|
||||
@@ -48,8 +61,9 @@ class Olive<P : Program> : Extension {
|
||||
|
||||
trackedListeners.forEach { it.saveListeners(store) }
|
||||
|
||||
val f = File(script)
|
||||
|
||||
fun setupScript(scriptFile:String) {
|
||||
watcher?.stop()
|
||||
val f = File(scriptFile)
|
||||
if (!f.exists()) {
|
||||
f.parentFile.mkdirs()
|
||||
var className = program.javaClass.name
|
||||
@@ -71,25 +85,28 @@ class Olive<P : Program> : Extension {
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
program.watchFile(File(script)) {
|
||||
watcher = program.watchFile(File(script)) {
|
||||
try {
|
||||
|
||||
val script = it.readText()
|
||||
val func = KtsObjectLoader().load<P.() -> Unit>(script)
|
||||
|
||||
program.extensions.clear()
|
||||
program.extensions.addAll(originalExtensions)
|
||||
|
||||
trackedListeners.forEach { it.restoreListeners(store) }
|
||||
trackedListeners.forEach { l -> l.restoreListeners(store) }
|
||||
session?.end()
|
||||
|
||||
session = Session()
|
||||
session?.start()
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
func(program as P)
|
||||
} catch (e: Throwable) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
setupScript(script)
|
||||
scriptChange = ::setupScript
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user