sync some node events on group instances

This commit is contained in:
Lera Elvoé 2023-12-08 08:41:10 +03:00
parent 0d6f3169f1
commit 58842437b6
No known key found for this signature in database
2 changed files with 77 additions and 1 deletions

View file

@ -32,11 +32,18 @@ var group_output_node: String
## @experimental ## @experimental
#var group_node: String #var group_node: String
var emit_group_signals: bool = true
## Emitted when a node has been added to this deck. ## Emitted when a node has been added to this deck.
signal node_added(node: DeckNode) signal node_added(node: DeckNode)
## Emitted when a node has been removed from this deck. ## Emitted when a node has been removed from this deck.
signal node_removed(node: DeckNode) signal node_removed(node: DeckNode)
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)
## Instantiate a node by its' [member DeckNode.node_type] and add it to this deck.[br] ## Instantiate a node by its' [member DeckNode.node_type] and add it to this deck.[br]
## See [method add_node_inst] for parameter descriptions. ## See [method add_node_inst] for parameter descriptions.
@ -64,6 +71,8 @@ func add_node_inst(node: DeckNode, assign_id: String = "", assign_to_self: bool
node._id = assign_id node._id = assign_id
node_added.emit(node) node_added.emit(node)
if is_group && emit_group_signals:
node_added_to_group.emit(node, assign_id, assign_to_self, self)
return node return node
@ -86,6 +95,9 @@ func connect_nodes(from_node_id: String, to_node_id: String, from_output_port: i
# TODO: prevent duplicate connections # TODO: prevent duplicate connections
if is_group && emit_group_signals:
nodes_connected_in_group.emit(from_node_id, to_node_id, from_output_port, to_input_port, self)
from_node.add_outgoing_connection(from_output_port, to_node._id, to_input_port) from_node.add_outgoing_connection(from_output_port, to_node._id, to_input_port)
return true return true
@ -96,6 +108,8 @@ func disconnect_nodes(from_node_id: String, to_node_id: String, from_output_port
var to_node := get_node(to_node_id) var to_node := get_node(to_node_id)
from_node.remove_outgoing_connection(from_output_port, to_node_id, to_input_port) from_node.remove_outgoing_connection(from_output_port, to_node_id, to_input_port)
to_node.remove_incoming_connection(to_input_port) to_node.remove_incoming_connection(to_input_port)
if is_group && emit_group_signals:
nodes_disconnected_in_group.emit(from_node_id, to_node_id, from_output_port, to_input_port, self)
## Returns true if this deck has no nodes and no variables. ## Returns true if this deck has no nodes and no variables.
@ -130,6 +144,9 @@ func remove_node(uuid: String, remove_connections: bool = false) -> void:
node_removed.emit(node) node_removed.emit(node)
if is_group && emit_group_signals:
node_removed_from_group.emit(uuid, remove_connections, self)
## Group the [param nodes_to_group] into a new deck and return it. ## Group the [param nodes_to_group] into a new deck and return it.
## Returns [code]null[/code] on failure.[br] ## Returns [code]null[/code] on failure.[br]

View file

@ -41,6 +41,10 @@ static func add_group_from_dict(data: Dictionary, deck_id: String, instance_id:
var instances: Dictionary = decks.get(deck_id, {}) var instances: Dictionary = decks.get(deck_id, {})
instances[instance_id] = group instances[instance_id] = group
decks[deck_id] = instances decks[deck_id] = instances
group.node_added_to_group.connect(DeckHolder._on_node_added_to_group)
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)
return group return group
@ -56,7 +60,10 @@ static func add_empty_group() -> Deck:
group.id = UUID.v4() group.id = UUID.v4()
group.instance_id = UUID.v4() group.instance_id = UUID.v4()
decks[group.id] = {group.instance_id: group} decks[group.id] = {group.instance_id: group}
group.node_added_to_group.connect(DeckHolder._on_node_added_to_group)
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)
return group return group
@ -100,3 +107,55 @@ static func send_event(event_name: StringName, event_data: Dictionary = {}) -> v
else: else:
for deck_instance_id: String in decks[deck_id]: for deck_instance_id: String in decks[deck_id]:
(decks[deck_id][deck_instance_id] as Deck).send_event(event_name, event_data) (decks[deck_id][deck_instance_id] as Deck).send_event(event_name, event_data)
#region group signal callbacks
static func _on_node_added_to_group(node: DeckNode, assign_id: String, assign_to_self: bool, 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
var node_duplicate := DeckNode.from_dict(node.to_dict())
instance.add_node_inst(node_duplicate, assign_id, assign_to_self)
instance.emit_group_signals = true
static func _on_node_removed_from_group(node_id: String, remove_connections: bool, 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.remove_node(node_id, remove_connections)
instance.emit_group_signals = true
static func _on_nodes_connected_in_group(from_node_id: String, to_node_id: String, from_output_port: int, to_input_port: int, 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.connect_nodes(from_node_id, to_node_id, from_output_port, to_input_port)
instance.emit_group_signals = true
static func _on_nodes_disconnected_in_group(from_node_id: String, to_node_id: String, from_output_port: int, to_input_port: int, 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.disconnect_nodes(from_node_id, to_node_id, from_output_port, to_input_port)
instance.emit_group_signals = true
#endregion