All Downloads are FREE. Search and download functionalities are using the official Maven repository.

godot.core.Properties.kt Maven / Gradle / Ivy

The newest version!
package godot.core

import godot.PropertyHint
import godot.PropertyUsageFlags
import godot.core.memory.TransferContext
import godot.util.GodotLogging
import kotlin.reflect.KMutableProperty1

data class KtPropertyInfo(
    val _type: VariantConverter,
    val name: String,
    val className: String,
    val _hint: PropertyHint,
    val hintString: String,
    val usage: Long = if (_type === VariantCaster.ANY) {
        PropertyUsageFlags.PROPERTY_USAGE_NIL_IS_VARIANT.flag
    } else {
        PropertyUsageFlags.PROPERTY_USAGE_NONE.flag
    }
) {
    val type: Int
        get() = _type.id

    val hint: Int
        get() = _hint.id.toInt()
}

open class KtProperty(
    val ktPropertyInfo: KtPropertyInfo,
    protected val kProperty: KMutableProperty1,
    protected val variantConverter: VariantConverter,
) {
    open fun callGet(instance: T) {
        try {
            TransferContext.writeReturnValue(kProperty.get(instance), variantConverter)
        } catch (t: Throwable) {
            GodotLogging.error("Error calling JVM getter ${kProperty.name} of script $instance from Godot\n:" + t.stackTraceToString())
            TransferContext.writeReturnValue(null, VariantParser.NIL)
        }
    }

    open fun callSet(instance: T) {
        val arg = extractSetterArgument

() try { kProperty.set(instance, arg) } catch (t: Throwable) { GodotLogging.error("Error calling JVM setter ${kProperty.name} of script $instance from Godot:\n" + t.stackTraceToString()) } } protected fun

extractSetterArgument(): P { //TODO: manage nullable argument of enum setter (only for objects) val arg = TransferContext.readSingleArgument(variantConverter) @Suppress("UNCHECKED_CAST") return arg as P } } class KtEnumProperty( ktPropertyInfo: KtPropertyInfo, kProperty: KMutableProperty1, val getValueConverter: (P?) -> Int, val setValueConverter: (Int) -> P ) : KtProperty( ktPropertyInfo, kProperty, VariantCaster.INT ) { override fun callGet(instance: T) { TransferContext.writeReturnValue(getValueConverter(kProperty.get(instance)), VariantCaster.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, VariantParser.ARRAY ) { override fun callGet(instance: T) { TransferContext.writeReturnValue(getValueConverter(kProperty.get(instance)), VariantParser.ARRAY) } override fun callSet(instance: T) { val arg = extractSetterArgument>() kProperty.set(instance, setValueConverter(arg)) } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy