godot.core.Properties.kt Maven / Gradle / Ivy
Show all versions of godot-library-debug Show documentation
package godot.core
import godot.core.memory.TransferContext
import kotlin.reflect.KMutableProperty1
data class KtPropertyInfo(
val _type: VariantType,
val name: String,
val className: String,
val _hint: PropertyHint,
val hintString: String,
val visibleInEditor: Boolean,
) {
val type: Int
get() = _type.ordinal
val hint: Int
get() = _hint.ordinal
}
open class KtProperty(
val ktPropertyInfo: KtPropertyInfo,
protected val kProperty: KMutableProperty1,
protected val variantType: VariantType,
val isRef: Boolean
) {
open fun callGet(instance: T) {
TransferContext.writeReturnValue(kProperty.get(instance), variantType)
}
open fun callSet(instance: T) {
val arg = extractSetterArgument()
kProperty.set(instance, arg)
}
protected fun
extractSetterArgument(): P {
//TODO: manage nullable argument of enum setter (only for objects)
val arg = TransferContext.readSingleArgument(variantType)
@Suppress("UNCHECKED_CAST")
return arg as P
}
}
class KtEnumProperty(
ktPropertyInfo: KtPropertyInfo,
kProperty: KMutableProperty1,
val getValueConverter: (P?) -> Int,
val setValueConverter: (Int) -> P
) : KtProperty(
ktPropertyInfo,
kProperty,
VariantType.JVM_INT,
false
) {
override fun callGet(instance: T) {
TransferContext.writeReturnValue(getValueConverter(kProperty.get(instance)), VariantType.JVM_INT)
}
override fun callSet(instance: T) {
val arg = extractSetterArgument()
kProperty.set(instance, setValueConverter(arg))
}
}
class KtEnumListProperty, L : Collection>(
ktPropertyInfo: KtPropertyInfo,
kProperty: KMutableProperty1,
val getValueConverter: (L?) -> VariantArray,
val setValueConverter: (VariantArray) -> L
) : KtProperty(
ktPropertyInfo,
kProperty,
VariantType.ARRAY,
false
) {
override fun callGet(instance: T) {
TransferContext.writeReturnValue(getValueConverter(kProperty.get(instance)), VariantType.ARRAY)
}
override fun callSet(instance: T) {
val arg = extractSetterArgument>()
kProperty.set(instance, setValueConverter(arg))
}
}