commonMain.dsl.Delegates.kt Maven / Gradle / Ivy
package org.openrndr.orsl.shadergenerator.dsl
import kotlin.reflect.KProperty
class OutputProperty(val generator: Generator, val type:String) {
operator fun getValue(any: Any?, property: KProperty<*>): Symbol = symbol(property.name, type)
operator fun setValue(any: Any?, property: KProperty<*>, value: Symbol) {
activeGenerator().emit("${property.name} = ${value.name};")
}
}
class VaryingInProperty(val generator: Generator, val type: String, val forceDefinition: Boolean = false) {
operator fun provideDelegate(any: Any?, property: KProperty<*>) : VaryingInProperty {
if (forceDefinition || !(property.name.startsWith("va_") || property.name.startsWith("vi_"))) {
generator.emitPreamble(property.name, "in $type ${property.name};")
}
return this
}
operator fun getValue(any: Any?, property: KProperty<*>): Symbol = symbol(property.name, type)
}
class VaryingOutProperty(val generator: Generator, val type: String) {
operator fun provideDelegate(any: Any?, property: KProperty<*>) : VaryingOutProperty {
generator.emitPreamble(property.name, "out ${type} ${property.name};")
return this
}
operator fun getValue(any: Any?, property: KProperty<*>): Symbol = symbol(property.name, type)
operator fun setValue(any: Any?, property: KProperty<*>, value: Symbol) {
activeGenerator().emit("${property.name} = ${value.name};")
}
}
class FragmentOutputProperty(val generator: Generator, val type:String) {
operator fun getValue(any: Any?, property: KProperty<*>): Symbol = symbol(property.name, type)
operator fun setValue(any: Any?, property: KProperty<*>, value: Symbol) {
activeGenerator().emit("${property.name} = ${value.name};")
}
}
class ConstantProperty(val type: String) {
operator fun getValue(any: Any?, property: KProperty<*>): Symbol = symbol(property.name, type)
}
class GlobalProperty(val generator: Generator, val type:String) {
operator fun provideDelegate(any: Any?, property: KProperty<*>) : GlobalProperty {
generator.emitPreamble(property.name, "${type} ${property.name};")
return this
}
operator fun getValue(any: Any?, property: KProperty<*>): Symbol = symbol(property.name, type)
operator fun setValue(any: Any?, property: KProperty<*>, value: Symbol) {
activeGenerator().emit("${property.name} = ${value.name};")
}
}
class VariableProperty(val generator: Generator, val type:String,
val initialValue: T?, val initialSymbol: Symbol?) {
operator fun provideDelegate(any: Any?, property: KProperty<*>) : VariableProperty {
if (initialValue == null && initialSymbol == null) {
generator.emit("${type} ${property.name};")
} else if (initialValue != null) {
generator.emit("${type} ${property.name} = ${glsl(initialValue)};")
} else if (initialSymbol != null) {
generator.emit("${type} ${property.name} = ${initialSymbol.name};")
}
return this
}
operator fun getValue(any: Any?, property: KProperty<*>): Symbol = symbol(property.name, type)
operator fun setValue(any: Any?, property: KProperty<*>, value: Symbol) {
activeGenerator().emit("${property.name} = ${value.name};")
}
}
class ArrayVariableProperty(val generator: Generator, val length: Int, val glslType:String) {
private var declarationEmitted = false
operator fun getValue(any: Any?, property: KProperty<*>): ArraySymbol {
if (!declarationEmitted) {
generator.emit("${glslType} ${property.name}[$length];")
declarationEmitted = true
}
return object : ArraySymbol {
override val name: String
get() = property.name
override val length: Int = [email protected]
override val type = glslType
}
}
operator fun setValue(any: Any?, property: KProperty<*>, value: ArraySymbol) {
generator.emit("${property.name} = ${value.name};")
}
operator fun setValue(any: Any?, property: KProperty<*>, value: Symbol) {
if (!declarationEmitted) {
generator.emit("${glslType} ${property.name};")
declarationEmitted = true
}
activeGenerator().emit("${property.name} = ${value.name};")
}
}
class ValueProperty(val type:String) {
operator fun getValue(any: Any?, property: KProperty<*>): Symbol = symbol(property.name, type)
}
class ArrayValueProperty(
val generator: Generator, val length: Int, val type:String) {
operator fun provideDelegate(any: Any?, property: KProperty<*>) : ArrayValueProperty {
generator.emit("$type ${property.name}[$length];")
return this
}
operator fun getValue(any: Any?, property: KProperty<*>): ArraySymbol {
return object : ArraySymbol {
override val name: String
get() = property.name
override val length: Int = [email protected]
override val type = [email protected]
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy