From 6dd344a8b98ae53b38dd39a898de52c1ace52fbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Mon, 27 Nov 2023 12:10:07 +0300 Subject: [PATCH] FINALLY split up the node renderer function to generate ports --- .../deck_node_renderer_graph_node.gd | 169 ++++++------------ 1 file changed, 56 insertions(+), 113 deletions(-) diff --git a/graph_node_renderer/deck_node_renderer_graph_node.gd b/graph_node_renderer/deck_node_renderer_graph_node.gd index 350d29b..36d0cec 100644 --- a/graph_node_renderer/deck_node_renderer_graph_node.gd +++ b/graph_node_renderer/deck_node_renderer_graph_node.gd @@ -16,44 +16,7 @@ func _ready() -> void: #node.port_removed.connect(_on_node_port_removed) node.ports_updated.connect(_on_node_ports_updated) for port in node.get_all_ports(): - var descriptor_split := port.descriptor.split(":") - match descriptor_split[0]: - "button": - var button := Button.new() - add_child(button) - button.text = port.label - if port.port_type == DeckNode.PortType.OUTPUT: - button.pressed.connect( - func(): - node.send(port.index_of_type, true) - ) - elif port.port_type == DeckNode.PortType.INPUT: - button.pressed.connect( - func(): - node._receive(port.index_of_type, true) - ) - "field": - var line_edit := LineEdit.new() - add_child(line_edit) - if port.value: - line_edit.text = str(port.value) - line_edit.placeholder_text = port.label - port.value_callback = line_edit.get_text - line_edit.text_changed.connect(port.set_value) - _: - var label := Label.new() - add_child(label) - label.text = port.label - - set_slot( - port.index, - port.port_type == DeckNode.PortType.INPUT, - port.type, - Color.WHITE, - port.port_type == DeckNode.PortType.OUTPUT, - port.type, - Color.WHITE, - ) + update_port(port) ## Connected to [signal GraphElement.position_offset_updated] and updates the ## [member node]s properties @@ -72,45 +35,8 @@ func _on_node_position_updated(new_position: Dictionary) -> void: ## using [method GraphNode.set_slot] func _on_node_port_added(port_idx: int) -> void: var port := node.get_all_ports()[port_idx] + update_port(port) - var descriptor_split := port.descriptor.split(":") - match descriptor_split[0]: - "button": - var button := Button.new() - add_child(button) - button.text = port.label - if port.port_type == DeckNode.PortType.OUTPUT: - button.pressed.connect( - func(): - node.send(port.index_of_type, true) - ) - elif port.port_type == DeckNode.PortType.INPUT: - button.pressed.connect( - func(): - node._receive(port.index_of_type, true) - ) - "field": - var line_edit := LineEdit.new() - add_child(line_edit) - if port.value: - line_edit.text = str(port.value) - line_edit.placeholder_text = port.label - port.value_callback = line_edit.get_text - line_edit.text_changed.connect(port.set_value) - _: - var label := Label.new() - add_child(label) - label.text = port.label - - set_slot( - port.index, - port.port_type == DeckNode.PortType.INPUT, - port.type, - Color.WHITE, - port.port_type == DeckNode.PortType.OUTPUT, - port.type, - Color.WHITE, - ) ## Connected to [member node]s [signal port_removed], queue_frees the [Node] ## used for the descriptor, as well as using [method GraphNode.set_slot] to remove the slot. @@ -135,41 +61,58 @@ func _on_node_ports_updated() -> void: c.queue_free() for port in node.get_all_ports(): - var descriptor_split := port.descriptor.split(":") - match descriptor_split[0]: - "button": - var button := Button.new() - add_child(button) - button.text = port.label - if port.port_type == DeckNode.PortType.OUTPUT: - button.pressed.connect( - func(): - node.send(port.index_of_type, true) - ) - elif port.port_type == DeckNode.PortType.INPUT: - button.pressed.connect( - func(): - node._receive(port.index_of_type, true) - ) - "field": - var line_edit := LineEdit.new() - add_child(line_edit) - if port.value: - line_edit.text = str(port.value) - line_edit.placeholder_text = port.label - port.value_callback = line_edit.get_text - line_edit.text_changed.connect(port.set_value) - _: - var label := Label.new() - add_child(label) - label.text = port.label + update_port(port) - set_slot( - port.index, - port.port_type == DeckNode.PortType.INPUT, - port.type, - Color.WHITE, - port.port_type == DeckNode.PortType.OUTPUT, - port.type, - Color.WHITE, - ) + +func update_port(port: Port) -> void: + var descriptor_split := port.descriptor.split(":") + match descriptor_split[0]: + "button": + var button := Button.new() + add_child(button) + button.text = port.label + if port.port_type == DeckNode.PortType.OUTPUT: + button.pressed.connect( + func(): + node.send(port.index_of_type, true) + ) + #elif port.port_type == DeckNode.PortType.INPUT: + else: + button.pressed.connect( + func(): + node._receive(port.index_of_type, true) + ) + "field": + var line_edit := LineEdit.new() + add_child(line_edit) + if port.value: + line_edit.text = str(port.value) + line_edit.placeholder_text = port.label + port.value_callback = line_edit.get_text + line_edit.text_changed.connect(port.set_value) + "singlechoice": + var box := OptionButton.new() + for item in descriptor_split.slice(1): + box.add_item(item) + add_child(box) + port.value_callback = func(): return box.get_item_text(box.get_selected_id()) + if port.type == DeckType.Types.STRING: + box.item_selected.connect( + func(id: int): + port.set_value.call(box.get_item_text(box.get_selected_id())) + ) + + _: + var label := Label.new() + add_child(label) + label.text = port.label + + set_slot( + port.index, + port.port_type == DeckNode.PortType.INPUT, + port.type, + Color.WHITE, + port.port_type == DeckNode.PortType.OUTPUT, + port.type, + Color.WHITE, + )