diff --git a/build.gradle b/build.gradle index a86db5b2..16ddc1c9 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { allprojects { group 'org.openrndr.extra' - version '0.0.26' + version '0.0.27' } repositories { @@ -13,7 +13,7 @@ repositories { } ext { - openrndrVersion = "0.3.33" + openrndrVersion = "0.4.0-SNAPSHOT" } subprojects { @@ -49,6 +49,13 @@ subprojects { classifier = 'sources' from sourceSets.main.kotlin } + + compileKotlin { + kotlinOptions.jvmTarget = "1.8" + } + compileTestKotlin { + kotlinOptions.jvmTarget = "1.8" + } } dependencies { diff --git a/orx-olive/build.gradle b/orx-olive/build.gradle new file mode 100644 index 00000000..0a97e97a --- /dev/null +++ b/orx-olive/build.gradle @@ -0,0 +1,10 @@ +dependencies { + compile project(":orx-file-watcher") + runtime "org.openrndr:openrndr-gl3:0.4.0-SNAPSHOT" + runtime "org.openrndr:openrndr-gl3-natives-linux-x64:0.4.0-SNAPSHOT" + compile "org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable" + compile "org.jetbrains.kotlin:kotlin-compiler-embeddable" + compile "org.jetbrains.kotlin:kotlin-script-runtime" + compile "org.jetbrains.kotlin:kotlin-script-util" + runtime "org.jetbrains.kotlin:kotlin-scripting-compiler" +} \ No newline at end of file diff --git a/orx-olive/src/main/kotlin/Example.kt b/orx-olive/src/main/kotlin/Example.kt new file mode 100644 index 00000000..4b44a266 --- /dev/null +++ b/orx-olive/src/main/kotlin/Example.kt @@ -0,0 +1,15 @@ +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()) + + } +} \ No newline at end of file diff --git a/orx-olive/src/main/kotlin/KtsObjectLoader.kt b/orx-olive/src/main/kotlin/KtsObjectLoader.kt new file mode 100644 index 00000000..8db53729 --- /dev/null +++ b/orx-olive/src/main/kotlin/KtsObjectLoader.kt @@ -0,0 +1,37 @@ +package org.openrndr.extra.olive + +import java.io.InputStream +import java.io.Reader +import javax.script.ScriptEngineManager + +class LoadException(message: String? = null, cause: Throwable? = null) : RuntimeException(message, cause) + +class KtsObjectLoader(classLoader: ClassLoader? = Thread.currentThread().contextClassLoader) { + + val engine = ScriptEngineManager(classLoader).getEngineByExtension("kts") + + init { + if (engine == null) { + throw RuntimeException("could not create scripting engine") + } + } + + inline fun safeEval(noinline evaluation: () -> R?) = try { + println(evaluation) + evaluation() + } catch (e: Exception) { + e.printStackTrace() + throw LoadException("Cannot load script", e) + } + + inline fun Any?.castOrError() = takeIf { it is T }?.let { it as T } + ?: throw IllegalArgumentException("Cannot cast $this to expected type ${T::class}") + + inline fun load(script: String): T = safeEval { engine.eval(script) }.castOrError() + + inline fun load(reader: Reader): T = safeEval { engine.eval(reader) }.castOrError() + + inline fun load(inputStream: InputStream): T = load(inputStream.reader()) + + inline fun loadAll(vararg inputStream: InputStream): List = inputStream.map(::load) +} \ No newline at end of file diff --git a/orx-olive/src/main/kotlin/Olive.kt b/orx-olive/src/main/kotlin/Olive.kt new file mode 100644 index 00000000..4c2874ac --- /dev/null +++ b/orx-olive/src/main/kotlin/Olive.kt @@ -0,0 +1,73 @@ +package org.openrndr.extra.olive + +import org.openrndr.Extension +import org.openrndr.Program +import org.openrndr.draw.Session +import org.operndr.extras.filewatcher.watchFile +import java.io.File + +class Olive():Extension { + override var enabled: Boolean = true + var session: Session? = null + + var script = "src/main/kotlin/live.kts" + + override fun setup(program: Program) { + val f = File(script) + + + + if (!f.exists()) { + f.parentFile.mkdirs() + f.writeText(""" + @file:Suppress("UNUSED_LAMBDA_EXPRESSION") + import org.openrndr.Program + import org.openrndr.draw.* + + { program: ${program.javaClass.name} -> + program.apply { + extend { + + } + } + } + """.trimIndent()) + } + + program.watchFile(File(script)) { + try { + + val script =it.readText() + + println(script) + val func = KtsObjectLoader().load Unit>(script) + + program.extensions.clear() + + program.keyboard.keyDown.listeners.clear() + program.keyboard.keyUp.listeners.clear() + program.keyboard.character.listeners.clear() + program.keyboard.keyRepeat.listeners.clear() + program.mouse.clicked.listeners.clear() + program.mouse.buttonDown.listeners.clear() + program.mouse.dragged.listeners.clear() + program.mouse.buttonUp.listeners.clear() + program.mouse.moved.listeners.clear() + program.window.drop.listeners.clear() + program.window.focused.listeners.clear() + program.window.minimized.listeners.clear() + program.window.unfocused.listeners.clear() + program.window.restored.listeners.clear() + program.window.sized.listeners.clear() + session?.end() + + session = Session() + session?.start() + + func(program as P) + } catch (e: Throwable) { + e.printStackTrace() + } + } + } +} \ No newline at end of file diff --git a/orx-olive/src/main/resources/META-INF/services/javax.script.ScriptEngineFactory b/orx-olive/src/main/resources/META-INF/services/javax.script.ScriptEngineFactory new file mode 100644 index 00000000..f8f59003 --- /dev/null +++ b/orx-olive/src/main/resources/META-INF/services/javax.script.ScriptEngineFactory @@ -0,0 +1 @@ +org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 872af521..e9def1be 100644 --- a/settings.gradle +++ b/settings.gradle @@ -13,4 +13,5 @@ include 'orx-camera', 'orx-midi', 'orx-no-clear', 'orx-noise', - 'orx-obj-loader' \ No newline at end of file + 'orx-obj-loader', + 'orx-olive' \ No newline at end of file