Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make VariantArray and Dictionary constructors visible to Java #685

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions harness/tests/src/main/java/godot/tests/JavaTestClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import godot.Button;
import godot.Node;
import godot.annotation.*;
import godot.core.NativeCallable;
import godot.core.StringNameUtils;
import godot.core.*;
import godot.signals.Signal;
import godot.signals.Signal2;
import godot.signals.SignalProvider;
import kotlin.Unit;
import kotlin.jvm.functions.Function2;
import org.jetbrains.annotations.NotNull;

@RegisterClass
public class JavaTestClass extends Node {
Expand Down Expand Up @@ -66,6 +68,12 @@ public String greeting() {
@RegisterProperty
public boolean signalEmitted = false;

@RegisterProperty
public VariantArray<Integer> variantArray = new VariantArray<>(Integer.class);

@RegisterProperty
public Dictionary<Float, String> dictionary = new Dictionary<>(Float.class, String.class);

@RegisterFunction
public void connectAndTriggerSignal() {
connect(
Expand All @@ -76,6 +84,17 @@ public void connectAndTriggerSignal() {
emitSignal(StringNameUtils.asStringName("test_signal"));
}

@NotNull
@Override
public GodotNotification _notification() {
return godotNotification(
this, (myself, notification) -> {
System.out.println(notification);
return null;
}
);
}

@RegisterFunction
public void signalCallback() {
signalEmitted = true;
Expand Down
7 changes: 5 additions & 2 deletions kt/godot-library/src/main/kotlin/godot/core/KtObject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import godot.util.nullObjectID
import godot.util.nullptr
import kotlincompile.definitions.GodotJvmBuildConfig

@JvmInline
value class GodotNotification internal constructor(val block: Any.(Int) -> Unit)
class GodotNotification internal constructor(val block: Any.(Int) -> Unit)

@Suppress("LeakingThis")
abstract class KtObject {
Expand Down Expand Up @@ -92,8 +91,12 @@ abstract class KtObject {
open fun _notification(): GodotNotification = godotNotification {}

@Suppress("UNCHECKED_CAST")
@JvmName("kotlinNotification")
protected fun <T : KtObject> T.godotNotification(block: T.(Int) -> Unit): GodotNotification = GodotNotification(block as Any.(Int) -> Unit)

@JvmName("godotNotification")
protected fun <T : KtObject> javaGodotNotification(obj: T, block: T.(Int) -> Unit) = obj.godotNotification(block)

@Suppress("FunctionName")
/**
* Called automatically when the Object is destroyed. Note that this method is not available for RefCounted or any of its child class.
Expand Down
29 changes: 17 additions & 12 deletions kt/godot-library/src/main/kotlin/godot/core/bridge/Dictionary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import godot.core.memory.MemoryManager
import godot.core.memory.TransferContext
import godot.util.MapIterator
import godot.util.VoidPtr
import kotlin.jvm.internal.Reflection
import kotlin.reflect.KClass

class Dictionary<K, V> : NativeCoreType, MutableMap<K, V> {

Expand All @@ -21,8 +23,20 @@ class Dictionary<K, V> : NativeCoreType, MutableMap<K, V> {
MemoryManager.registerNativeCoreType(this, VariantType.DICTIONARY)
}

constructor(keyClass: Class<*>, valueClass: Class<*>) : this(Reflection.getOrCreateKotlinClass(keyClass), Reflection.getOrCreateKotlinClass(valueClass))

@PublishedApi
internal constructor(keyVariantType: VariantType, valueVariantType: VariantType) {
internal constructor(keyClass: KClass<*>, valueClass: KClass<*>) {

val keyVariantType = variantMapper[keyClass]
checkNotNull(keyVariantType) {
"Can't create a Dictionary with generic key ${keyClass}."
}
val valueVariantType = variantMapper[valueClass]
checkNotNull(valueVariantType) {
"Can't create a Dictionary with generic value ${valueClass}."
}

this.keyVariantType = keyVariantType
this.valueVariantType = valueVariantType
_handle = Bridge.engine_call_constructor()
Expand Down Expand Up @@ -361,17 +375,8 @@ class Dictionary<K, V> : NativeCoreType, MutableMap<K, V> {


companion object {
inline operator fun <reified K, reified V> invoke(): Dictionary<K, V> {
val keyVariantType = variantMapper[K::class]
checkNotNull(keyVariantType) {
"Can't create a Dictionary with generic key ${K::class}."
}
val valVariantType = variantMapper[V::class]
checkNotNull(valVariantType) {
"Can't create a Dictionary with generic value ${V::class}."
}
return Dictionary(keyVariantType, valVariantType)
}
inline operator fun <reified K, reified V> invoke() = Dictionary<K, V>(K::class, V::class)

}
}

Expand Down
25 changes: 12 additions & 13 deletions kt/godot-library/src/main/kotlin/godot/core/bridge/VariantArray.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import godot.core.memory.MemoryManager
import godot.core.memory.TransferContext
import godot.util.IndexedIterator
import godot.util.VoidPtr
import kotlin.jvm.internal.Reflection
import kotlin.reflect.KClass


Expand All @@ -22,8 +23,15 @@ class VariantArray<T> : NativeCoreType, MutableCollection<T> {
MemoryManager.registerNativeCoreType(this, VariantType.ARRAY)
}

constructor(parameterClazz: Class<*>) : this(Reflection.getOrCreateKotlinClass(parameterClazz))

@PublishedApi
internal constructor(variantType: VariantType, parameterClazz: KClass<*>) {
internal constructor(parameterClazz: KClass<*>) {
val variantType = variantMapper[parameterClazz]
checkNotNull(variantType) {
"Can't create a VariantArray with generic ${parameterClazz}."
}

this.variantType = variantType
_handle = if (variantType != VariantType.ANY) {
TransferContext.writeArguments(
Expand Down Expand Up @@ -339,7 +347,7 @@ class VariantArray<T> : NativeCoreType, MutableCollection<T> {
*
* See also [any], [all], [map] and [reduce].
*/
fun filter(callable: Callable) : VariantArray<T> {
fun filter(callable: Callable): VariantArray<T> {
TransferContext.writeArguments(VariantType.CALLABLE to callable)
Bridge.engine_call_filter(_handle)
return (TransferContext.readReturnValue(VariantType.ARRAY) as VariantArray<T>).also {
Expand Down Expand Up @@ -629,17 +637,8 @@ class VariantArray<T> : NativeCoreType, MutableCollection<T> {
external fun engine_call_operator_get(_handle: VoidPtr)
}

companion object{
inline operator fun <reified T> invoke(): VariantArray<T> {
val variantType = variantMapper[T::class]
checkNotNull(variantType) {
"Can't create a VariantArray with generic ${T::class}."
}
return VariantArray(
variantType,
T::class
)
}
companion object {
inline operator fun <reified T> invoke() = VariantArray<T>(T::class)
}
}

Expand Down