Added orx-olive test
This commit is contained in:
11
build.gradle
11
build.gradle
@@ -4,7 +4,7 @@ plugins {
|
|||||||
|
|
||||||
allprojects {
|
allprojects {
|
||||||
group 'org.openrndr.extra'
|
group 'org.openrndr.extra'
|
||||||
version '0.0.26'
|
version '0.0.27'
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
@@ -13,7 +13,7 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ext {
|
ext {
|
||||||
openrndrVersion = "0.3.33"
|
openrndrVersion = "0.4.0-SNAPSHOT"
|
||||||
}
|
}
|
||||||
|
|
||||||
subprojects {
|
subprojects {
|
||||||
@@ -49,6 +49,13 @@ subprojects {
|
|||||||
classifier = 'sources'
|
classifier = 'sources'
|
||||||
from sourceSets.main.kotlin
|
from sourceSets.main.kotlin
|
||||||
}
|
}
|
||||||
|
|
||||||
|
compileKotlin {
|
||||||
|
kotlinOptions.jvmTarget = "1.8"
|
||||||
|
}
|
||||||
|
compileTestKotlin {
|
||||||
|
kotlinOptions.jvmTarget = "1.8"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
10
orx-olive/build.gradle
Normal file
10
orx-olive/build.gradle
Normal file
@@ -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"
|
||||||
|
}
|
||||||
15
orx-olive/src/main/kotlin/Example.kt
Normal file
15
orx-olive/src/main/kotlin/Example.kt
Normal file
@@ -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<StupidProgram>())
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
37
orx-olive/src/main/kotlin/KtsObjectLoader.kt
Normal file
37
orx-olive/src/main/kotlin/KtsObjectLoader.kt
Normal file
@@ -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 <R> safeEval(noinline evaluation: () -> R?) = try {
|
||||||
|
println(evaluation)
|
||||||
|
evaluation()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
throw LoadException("Cannot load script", e)
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <reified T> Any?.castOrError() = takeIf { it is T }?.let { it as T }
|
||||||
|
?: throw IllegalArgumentException("Cannot cast $this to expected type ${T::class}")
|
||||||
|
|
||||||
|
inline fun <reified T> load(script: String): T = safeEval { engine.eval(script) }.castOrError()
|
||||||
|
|
||||||
|
inline fun <reified T> load(reader: Reader): T = safeEval { engine.eval(reader) }.castOrError()
|
||||||
|
|
||||||
|
inline fun <reified T> load(inputStream: InputStream): T = load(inputStream.reader())
|
||||||
|
|
||||||
|
inline fun <reified T> loadAll(vararg inputStream: InputStream): List<T> = inputStream.map(::load)
|
||||||
|
}
|
||||||
73
orx-olive/src/main/kotlin/Olive.kt
Normal file
73
orx-olive/src/main/kotlin/Olive.kt
Normal file
@@ -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<P:Program>():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<P.() -> 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory
|
||||||
@@ -13,4 +13,5 @@ include 'orx-camera',
|
|||||||
'orx-midi',
|
'orx-midi',
|
||||||
'orx-no-clear',
|
'orx-no-clear',
|
||||||
'orx-noise',
|
'orx-noise',
|
||||||
'orx-obj-loader'
|
'orx-obj-loader',
|
||||||
|
'orx-olive'
|
||||||
Reference in New Issue
Block a user