Skip to content

Commit

Permalink
Add a way to write notifications for Java.
Browse files Browse the repository at this point in the history
  • Loading branch information
CedNaru committed Sep 7, 2024
1 parent 93f3dfb commit 1f0fc90
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
29 changes: 20 additions & 9 deletions harness/tests/src/main/java/godot/tests/JavaTestClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import godot.Button;
import godot.Node;
import godot.annotation.*;
import godot.core.Dictionary;
import godot.core.NativeCallable;
import godot.core.StringNameUtils;
import godot.core.VariantArray;
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 @@ -68,11 +68,11 @@ public String greeting() {
@RegisterProperty
public boolean signalEmitted = false;

@RegisterConstructor
public JavaTestClass() {
VariantArray<Integer> arr = new VariantArray<>(Integer.class);
Dictionary<Float, String> dict = new Dictionary<>(Float.class, String.class);
}
@RegisterProperty
public VariantArray<Integer> variantArray = new VariantArray<>(Integer.class);

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

@RegisterFunction
public void connectAndTriggerSignal() {
Expand All @@ -84,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

0 comments on commit 1f0fc90

Please sign in to comment.