@@ -1,13 +1,37 @@
|
|||||||
import Godot from "godot"
|
import Godot, { Vector2 } from "godot"
|
||||||
|
import { useEffect } from "react"
|
||||||
import { Component } from "react-godot-renderer"
|
import { Component } from "react-godot-renderer"
|
||||||
|
|
||||||
|
|
||||||
const Control = Component.make(Godot.Control)
|
const HFlowContainer = Component.make(Godot.HFlowContainer)
|
||||||
const Label = Component.make(Godot.Label)
|
const Label = Component.make(Godot.Label)
|
||||||
|
const TextEdit = Component.make(Godot.TextEdit)
|
||||||
|
const Button = Component.make(Godot.Button)
|
||||||
|
|
||||||
export function TestUi1Component() {
|
export function TestUi1Component() {
|
||||||
|
const textEditRef = TextEdit.useUnsafeRef()
|
||||||
|
const buttonRef = Button.useUnsafeRef()
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const onTextChanged = Godot.Callable.create(() => {
|
||||||
|
console.log(textEditRef.current.text)
|
||||||
|
})
|
||||||
|
|
||||||
|
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 (
|
return (
|
||||||
<Control
|
<HFlowContainer
|
||||||
name="Root"
|
name="Root"
|
||||||
anchor_left={0}
|
anchor_left={0}
|
||||||
anchor_top={0}
|
anchor_top={0}
|
||||||
@@ -19,7 +43,18 @@ export function TestUi1Component() {
|
|||||||
text="This is a label"
|
text="This is a label"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Control name="Control" />
|
<TextEdit
|
||||||
</Control>
|
ref={textEditRef}
|
||||||
|
name="TextEdit"
|
||||||
|
text=""
|
||||||
|
custom_minimum_size={new Vector2(200, 35)}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
ref={buttonRef}
|
||||||
|
name="Button"
|
||||||
|
text="Ok"
|
||||||
|
/>
|
||||||
|
</HFlowContainer>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,21 +2,37 @@ import type * as Godot from "godot"
|
|||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
|
|
||||||
|
|
||||||
export type Component<T extends Godot.Node<Godot.NodePathMap>> = React.FunctionComponent<Props<T>>
|
export interface Component<T extends Godot.Node<Godot.NodePathMap>>
|
||||||
|
extends React.FunctionComponent<Props<T>>, Prototype<T> {}
|
||||||
|
|
||||||
export type Props<T extends Godot.Node<Godot.NodePathMap>> = {
|
export type Props<T extends Godot.Node<Godot.NodePathMap>> = {
|
||||||
|
readonly ref?: React.RefObject<T | null>
|
||||||
|
} & {
|
||||||
// biome-ignore lint/complexity/noBannedTypes: using Function here is completely fine
|
// biome-ignore lint/complexity/noBannedTypes: using Function here is completely fine
|
||||||
[K in keyof T as T[K] extends Function ? never : K]?: T[K]
|
[K in keyof T as T[K] extends Function ? never : K]?: T[K]
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface InstrinsicAttributes {
|
export interface Prototype<T extends Godot.Node<Godot.NodePathMap>> {
|
||||||
|
useRef(): React.RefObject<T | null>
|
||||||
|
useUnsafeRef(): React.RefObject<T>
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface InstrinsicAttributes extends React.Attributes {
|
||||||
readonly children?: React.ReactNode
|
readonly children?: React.ReactNode
|
||||||
readonly name?: string
|
readonly name?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const Prototype: Prototype<any> = {
|
||||||
|
useRef() { return React.useRef(null) },
|
||||||
|
useUnsafeRef() { return React.useRef(null) },
|
||||||
|
}
|
||||||
|
|
||||||
export const make = <T extends Godot.Node<Godot.NodePathMap>>(
|
export const make = <T extends Godot.Node<Godot.NodePathMap>>(
|
||||||
class_: new (...args: any[]) => T
|
class_: new (...args: any[]) => T
|
||||||
): Component<T> => {
|
): Component<T> => Object.setPrototypeOf(
|
||||||
const f = (props: Props<T>) => React.createElement("element", { ...props, class: class_ })
|
Object.assign(
|
||||||
f.displayName = class_.name
|
(props: Props<T>) => React.createElement("element", { ...props, class: class_ }),
|
||||||
return f
|
{ displayName: class_.name },
|
||||||
}
|
),
|
||||||
|
Prototype,
|
||||||
|
)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export const renderComponent: {
|
|||||||
} = (
|
} = (
|
||||||
container: Node<NodePathMap>,
|
container: Node<NodePathMap>,
|
||||||
component: React.FC<{}>,
|
component: React.FC<{}>,
|
||||||
props?: React.Attributes,
|
props?: Record<string, unknown>,
|
||||||
): void => {
|
): void => {
|
||||||
const reconciler = Reconciler.make()
|
const reconciler = Reconciler.make()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user