Add support for resolving shader phrases from delegated properties
This commit is contained in:
23
orx-shader-phrases/src/main/kotlin/PhraseResource.kt
Normal file
23
orx-shader-phrases/src/main/kotlin/PhraseResource.kt
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package org.openrndr.extra.shaderphrases
|
||||||
|
|
||||||
|
import org.openrndr.resourceUrl
|
||||||
|
import java.net.URL
|
||||||
|
import kotlin.properties.ReadOnlyProperty
|
||||||
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PhraseResource can be used as a delegate
|
||||||
|
*/
|
||||||
|
class PhraseResource<R>(private val resourceUrl: String) : ReadOnlyProperty<R, String> {
|
||||||
|
override fun getValue(thisRef: R, property: KProperty<*>): String {
|
||||||
|
return URL(resourceUrl).readText()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PhraseResource delegate builder function
|
||||||
|
*/
|
||||||
|
fun phraseResource(resource: String) : PhraseResource<Any?> {
|
||||||
|
return PhraseResource(resourceUrl(resource))
|
||||||
|
}
|
||||||
@@ -21,13 +21,20 @@ fun preprocessShader(source: String): String {
|
|||||||
val packageClass = packageClassTokens.joinToString(".")
|
val packageClass = packageClassTokens.joinToString(".")
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
/* Note that JVM-style reflection is used here because of short-comings in the Kotlin reflection
|
||||||
|
library (as of 1.3.61), most notably reflection support for file facades is missing. */
|
||||||
val c = Class.forName(packageClass)
|
val c = Class.forName(packageClass)
|
||||||
if (c.annotations.any { it.annotationClass == ShaderPhrases::class }) {
|
if (c.annotations.any { it.annotationClass == ShaderPhrases::class }) {
|
||||||
if (fieldName == "*") {
|
if (fieldName == "*") {
|
||||||
|
c.declaredMethods.filter { it.returnType.name == "java.lang.String" }.map {
|
||||||
|
"/* imported from $packageClass.$it */\n${it.invoke(null)}\n"
|
||||||
|
}.joinToString("\n") +
|
||||||
|
|
||||||
c.declaredFields.filter { it.type.name == "java.lang.String" }.map {
|
c.declaredFields.filter { it.type.name == "java.lang.String" }.map {
|
||||||
"/* imported from $packageClass.$it */\n ${it.get(null)}"
|
"/* imported from $packageClass.$it */\n${it.get(null)}\n"
|
||||||
}.joinToString("\n")
|
}.joinToString("\n")
|
||||||
} else {
|
} else {
|
||||||
|
// TODO add method based phrase resolver like in the wildcard case above.
|
||||||
try {
|
try {
|
||||||
c.getDeclaredField(fieldName).get(null)
|
c.getDeclaredField(fieldName).get(null)
|
||||||
} catch (e: NoSuchFieldException) {
|
} catch (e: NoSuchFieldException) {
|
||||||
|
|||||||
Reference in New Issue
Block a user