From 6b43e807d6b41ba903a1ae6e37b41fde0b95303d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Sat, 10 Jun 2023 21:23:57 +0300 Subject: [PATCH] receive callbacks --- classes/deck/nodes/print.gd | 17 +++++++++++++---- classes/deck/port.gd | 8 +++++++- classes/types/deck_type.gd | 24 ++++++++++++++++++++++++ port_drawer.gd | 3 ++- test_node_renderer.gd | 24 ++++++++++++++++++------ 5 files changed, 64 insertions(+), 12 deletions(-) diff --git a/classes/deck/nodes/print.gd b/classes/deck/nodes/print.gd index eaf3aae..208b4a0 100644 --- a/classes/deck/nodes/print.gd +++ b/classes/deck/nodes/print.gd @@ -4,15 +4,24 @@ extends DeckNode func _init() -> void: add_input_port( Deck.Types.STRING, - "Input", + "Text to print", "field" ) + add_input_port( + Deck.Types.BOOL, + "Trigger", + "button" + ) + name = "Print" -func _receive(_to_port: int, data: DeckType, extra_data: Array = []) -> void: - # we only have one port, so we can skip checking which port we received on - var data_to_print = data.get_value() +func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void: + if to_port != 1: + return + +# var data_to_print = data.get_value() + var data_to_print = input_ports[0].value_callback.call() print(data_to_print) print("extra data: ", extra_data) diff --git a/classes/deck/port.gd b/classes/deck/port.gd index 258ae10..055cf04 100644 --- a/classes/deck/port.gd +++ b/classes/deck/port.gd @@ -3,9 +3,15 @@ class_name Port var type: Deck.Types var label: String var descriptor: String +var value_callback: Callable -func _init(p_type: Deck.Types, p_label: String, p_descriptor: String = "") -> void: +func _init( + p_type: Deck.Types, + p_label: String, + p_descriptor: String = "", + p_value_callback: Callable = Callable()) -> void: type = p_type label = p_label descriptor = p_descriptor + value_callback = p_value_callback diff --git a/classes/types/deck_type.gd b/classes/types/deck_type.gd index da7e437..040e138 100644 --- a/classes/types/deck_type.gd +++ b/classes/types/deck_type.gd @@ -12,6 +12,10 @@ func get_value() -> Variant: return _value +func set_value(new_value: Variant) -> void: + _value = new_value + + static func from(other: DeckType): return null @@ -32,6 +36,10 @@ class DeckTypeError extends DeckType: class DeckTypeNumeric extends DeckType: + func _init(value: float = 0.0) -> void: + _value = value + + static func from(other: DeckType): if other is DeckTypeNumeric: return other @@ -58,6 +66,10 @@ class DeckTypeNumeric extends DeckType: class DeckTypeString extends DeckType: + func _init(value: String = "") -> void: + _value = value + + static func from(other: DeckType): if other is DeckTypeString: return other @@ -67,6 +79,10 @@ class DeckTypeString extends DeckType: class DeckTypeBool extends DeckType: + func _init(value: bool = false) -> void: + _value = value + + static func from(other: DeckType): if other is DeckTypeBool: return other @@ -83,6 +99,10 @@ class DeckTypeBool extends DeckType: class DeckTypeArray extends DeckType: + func _init(value: Array = []) -> void: + _value = value + + static func from(other: DeckType): if other is DeckTypeString: var inst := DeckTypeArray.new() @@ -95,6 +115,10 @@ class DeckTypeArray extends DeckType: class DeckTypeDictionary extends DeckType: + func _init(value: Dictionary = {}) -> void: + _value = value + + static func from(other: DeckType): if other is DeckTypeString: var inst := DeckTypeDictionary.new() diff --git a/port_drawer.gd b/port_drawer.gd index 7e07c20..7c500d3 100644 --- a/port_drawer.gd +++ b/port_drawer.gd @@ -24,11 +24,12 @@ func add_label(text: String) -> void: l.size_flags_horizontal = Control.SIZE_EXPAND_FILL -func add_field() -> void: +func add_field(placeholder: String = "") -> void: var le := LineEdit.new() add_child(le) move_child(le, 1) le.size_flags_horizontal = Control.SIZE_EXPAND_FILL + le.placeholder_text = placeholder le.text_changed.connect( func(new_text: String): diff --git a/test_node_renderer.gd b/test_node_renderer.gd index 33ca14d..55ad263 100644 --- a/test_node_renderer.gd +++ b/test_node_renderer.gd @@ -13,15 +13,20 @@ var port_drawer_scene := preload("res://port_drawer.tscn") func _ready() -> void: name_label.text = node.name - for input_port in node.input_ports: + for i in node.input_ports.size(): + var input_port := node.input_ports[i] var port_drawer: PortDrawer = port_drawer_scene.instantiate() elements_container.add_child(port_drawer) port_drawer.set_input_enabled(true) match input_port.descriptor: "field": - port_drawer.add_field() -# "button": -# port_drawer.add_button(input_port.label) + port_drawer.add_field(input_port.label) + input_port.value_callback = port_drawer.get_text + "button": + port_drawer.add_button(input_port.label) + port_drawer.button_pressed.connect(func(): + node._receive(i, DeckType.DeckTypeString.new("memes")) + ) _: port_drawer.add_label(input_port.label) @@ -32,7 +37,14 @@ func _ready() -> void: var port_drawer: PortDrawer = elements_container.get_child(i) port_drawer.set_output_enabled(true) var output_port := node.output_ports[i] - if output_port.descriptor == "button": - port_drawer.add_button(output_port.label) + match output_port.descriptor: + "field": + port_drawer.add_field(output_port.label) + output_port.value_callback = port_drawer.get_text + "button": + port_drawer.add_button(output_port.label) + _: + port_drawer.add_label(output_port.label) +