From b2d96a471e271a0016d7af1a08da7b6a6fc5d6a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Fri, 8 Dec 2023 09:33:37 +0300 Subject: [PATCH] sync port values in group instances --- classes/deck/deck.gd | 10 +++++++++- classes/deck/deck_holder.gd | 14 ++++++++++++++ classes/deck/deck_node.gd | 7 +++++++ classes/deck/port.gd | 9 ++++++--- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/classes/deck/deck.gd b/classes/deck/deck.gd index ca3d553..5f19b37 100644 --- a/classes/deck/deck.gd +++ b/classes/deck/deck.gd @@ -39,11 +39,13 @@ signal node_added(node: DeckNode) ## Emitted when a node has been removed from this deck. signal node_removed(node: DeckNode) +#region group signals signal node_added_to_group(node: DeckNode, assign_id: String, assign_to_self: bool, deck: Deck) signal node_removed_from_group(node_id: String, remove_connections: bool, deck: Deck) signal nodes_connected_in_group(from_node_id: String, to_node_id: String, from_output_port: int, to_input_port: int, deck: Deck) signal nodes_disconnected_in_group(from_node_id: String, to_node_id: String, from_output_port: int, to_input_port: int, deck: Deck) - +signal node_port_value_updated(node_id: String, port_idx: int, new_value: Variant, deck: Deck) +#endregion ## Instantiate a node by its' [member DeckNode.node_type] and add it to this deck.[br] ## See [method add_node_inst] for parameter descriptions. @@ -74,6 +76,12 @@ func add_node_inst(node: DeckNode, assign_id: String = "", assign_to_self: bool if is_group && emit_group_signals: node_added_to_group.emit(node, assign_id, assign_to_self, self) + node.port_value_updated.connect( + func(port_idx: int, new_value: Variant): + if is_group && emit_group_signals: + node_port_value_updated.emit(node._id, port_idx, new_value, self) + ) + return node diff --git a/classes/deck/deck_holder.gd b/classes/deck/deck_holder.gd index b9636d3..b5ef9fb 100644 --- a/classes/deck/deck_holder.gd +++ b/classes/deck/deck_holder.gd @@ -45,6 +45,7 @@ static func add_group_from_dict(data: Dictionary, deck_id: String, instance_id: group.node_removed_from_group.connect(DeckHolder._on_node_removed_from_group) group.nodes_connected_in_group.connect(DeckHolder._on_nodes_connected_in_group) group.nodes_disconnected_in_group.connect(DeckHolder._on_nodes_disconnected_in_group) + group.node_port_value_updated.connect(DeckHolder._on_node_port_value_updated) return group @@ -64,6 +65,7 @@ static func add_empty_group() -> Deck: group.node_removed_from_group.connect(DeckHolder._on_node_removed_from_group) group.nodes_connected_in_group.connect(DeckHolder._on_nodes_connected_in_group) group.nodes_disconnected_in_group.connect(DeckHolder._on_nodes_disconnected_in_group) + group.node_port_value_updated.connect(DeckHolder._on_node_port_value_updated) return group @@ -158,4 +160,16 @@ static func _on_nodes_disconnected_in_group(from_node_id: String, to_node_id: St instance.disconnect_nodes(from_node_id, to_node_id, from_output_port, to_input_port) instance.emit_group_signals = true + +static func _on_node_port_value_updated(node_id: String, port_idx: int, new_value: Variant, deck: Deck) -> void: + var group_id := deck.id + for instance_id: String in decks[group_id]: + if instance_id == deck.instance_id: + continue + + var instance: Deck = get_group_instance(group_id, instance_id) + instance.emit_group_signals = false + instance.get_node(node_id).get_all_ports()[port_idx].set_value_no_signal(new_value) + instance.emit_group_signals = true + #endregion diff --git a/classes/deck/deck_node.gd b/classes/deck/deck_node.gd index 4f60b17..74ae61b 100644 --- a/classes/deck/deck_node.gd +++ b/classes/deck/deck_node.gd @@ -64,6 +64,8 @@ signal incoming_connection_added(from_port: int) ## Emitted when a connection to this node has been removed. signal incoming_connection_removed(from_port: int) +signal port_value_updated(port_idx: int, new_value: Variant) + ## Add an input port to this node. Usually only used at initialization. func add_input_port(type: DeckType.Types, label: String, descriptor: String = "") -> void: @@ -85,10 +87,15 @@ func add_port(type: DeckType.Types, label: String, port_type: PortType, index_of var port := Port.new(type, label, ports.size(), port_type, index_of_type, descriptor) ports.append(port) port_added.emit(ports.size() - 1) + port.value_updated.connect( + func(new_value: Variant) -> void: + port_value_updated.emit(port.index, new_value) + ) ports_updated.emit() ## Remove a port from this node. +## @deprecated func remove_port(port_idx: int) -> void: outgoing_connections.erase(port_idx) incoming_connections.erase(port_idx) diff --git a/classes/deck/port.gd b/classes/deck/port.gd index 000c822..4387780 100644 --- a/classes/deck/port.gd +++ b/classes/deck/port.gd @@ -25,7 +25,7 @@ var index_of_type: int var index: int ## The value of this port. -var value: Variant: set = set_value +var value: Variant signal value_updated(new_value: Variant) @@ -50,10 +50,13 @@ func _init( func set_value(v: Variant) -> void: + set_value_no_signal(v) + value_updated.emit(value) + + +func set_value_no_signal(v: Variant) -> void: if v is Callable: value = v.call() - value_updated.emit(value) return value = v - value_updated.emit(value)