Add useSignal
Some checks failed
Lint / lint (push) Failing after 9s

This commit is contained in:
Julien Valverdé
2026-01-01 23:25:05 +01:00
parent d37c5e1405
commit fae11b2024
8 changed files with 73 additions and 21 deletions

View File

@@ -0,0 +1,5 @@
declare module "godot" {
interface SceneNodes {
"src/TestUi2.tscn": {};
}
}

View File

@@ -0,0 +1,6 @@
import TestUi2 from "../../../src/TestUi2";
declare module "godot" {
interface ResourceTypes {
"res://src/TestUi2.tscn": PackedScene<TestUi2>;
}
}

View File

@@ -8,4 +8,8 @@ export default class TestUi1 extends Control {
_ready(): void {
Renderer.renderComponent(this, TestUi1Component)
}
_exit_tree(): void {
console.log("exit tree")
}
}

View File

@@ -1,5 +1,5 @@
import Godot, { Vector2 } from "godot"
import { useEffect, useState } from "react"
import { useState } from "react"
import { Component } from "react-godot-renderer"
@@ -11,26 +11,16 @@ const Button = Component.make(Godot.Button)
export function TestUi1Component() {
const [text, setText] = useState("Default text")
const textEditRef = TextEdit.useUnsafeRef()
const buttonRef = Button.useUnsafeRef()
const textEditRef = TextEdit.useRef()
Component.useSignal(textEditRef, "text_changed", function(this) {
setText(this.text)
})
useEffect(() => {
const onTextChanged = Godot.Callable.create(() => {
setText(textEditRef.current.text)
})
const buttonRef = Button.useRef()
Component.useSignal(buttonRef, "pressed", () => {
console.log("Pressed!")
})
textEditRef.current.text_changed.connect(onTextChanged)
return () => { textEditRef.current.text_changed.disconnect(onTextChanged) }
}, [textEditRef.current])
useEffect(() => {
const onPressed = Godot.Callable.create(() => {
console.log("Pressed!")
})
buttonRef.current.pressed.connect(onPressed)
return () => { buttonRef.current.pressed.disconnect(onPressed) }
}, [buttonRef.current])
return (
<HFlowContainer

View File

@@ -0,0 +1,9 @@
import { Control } from "godot"
export default class TestUi2 extends Control {
// Called when the node enters the scene tree for the first time.
_ready(): void {
}
}

View File

@@ -0,0 +1 @@
uid://bm1qao8uj6b1f

View File

@@ -0,0 +1,12 @@
[gd_scene load_steps=2 format=3 uid="uid://b17syqj60kwx1"]
[ext_resource type="Script" uid="uid://bm1qao8uj6b1f" path="res://src/TestUi2.ts" id="1_1lkkw"]
[node name="TestUi2" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_1lkkw")