Improve orx-olive

This commit is contained in:
Edwin Jakobs
2019-05-21 00:15:05 +02:00
parent b5db7998b7
commit ee4f424d9a
6 changed files with 91 additions and 34 deletions

View File

@@ -1,15 +0,0 @@
package org.openrndr.extra.olive
import org.openrndr.Program
import org.openrndr.application
class StupidProgram:Program() {
val thisIsStupid = 5
}
fun main() = application{
program(StupidProgram()) {
extend(Olive<StupidProgram>())
}
}

View File

@@ -4,9 +4,9 @@ import java.io.InputStream
import java.io.Reader
import javax.script.ScriptEngineManager
class LoadException(message: String? = null, cause: Throwable? = null) : RuntimeException(message, cause)
internal class LoadException(message: String? = null, cause: Throwable? = null) : RuntimeException(message, cause)
class KtsObjectLoader(classLoader: ClassLoader? = Thread.currentThread().contextClassLoader) {
internal class KtsObjectLoader(classLoader: ClassLoader? = Thread.currentThread().contextClassLoader) {
val engine = ScriptEngineManager(classLoader).getEngineByExtension("kts")
@@ -17,10 +17,8 @@ class KtsObjectLoader(classLoader: ClassLoader? = Thread.currentThread().context
}
inline fun <R> safeEval(noinline evaluation: () -> R?) = try {
println(evaluation)
evaluation()
} catch (e: Exception) {
e.printStackTrace()
throw LoadException("Cannot load script", e)
}

View File

@@ -6,25 +6,29 @@ import org.openrndr.draw.Session
import org.operndr.extras.filewatcher.watchFile
import java.io.File
class Olive<P:Program>():Extension {
class Olive<P : Program> : Extension {
override var enabled: Boolean = true
var session: Session? = null
var script = "src/main/kotlin/live.kts"
override fun setup(program: Program) {
System.setProperty("idea.io.use.fallback", "true")
val f = File(script)
if (!f.exists()) {
f.parentFile.mkdirs()
var className = program.javaClass.name
if (className.contains("$"))
className = "Program"
f.writeText("""
@file:Suppress("UNUSED_LAMBDA_EXPRESSION")
import org.openrndr.Program
import org.openrndr.draw.*
{ program: ${program.javaClass.name} ->
{ program: $className ->
program.apply {
extend {
@@ -37,9 +41,7 @@ class Olive<P:Program>():Extension {
program.watchFile(File(script)) {
try {
val script =it.readText()
println(script)
val script = it.readText()
val func = KtsObjectLoader().load<P.() -> Unit>(script)
program.extensions.clear()