From 2c7ccb66dba80188e0d63088d703e4d92efc4b6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Wed, 29 Nov 2023 11:47:47 +0300 Subject: [PATCH] delete nodes, fix grouping --- classes/deck/deck.gd | 49 ++++++++++++++----- .../deck_renderer_graph_edit.gd | 20 +++++++- .../deck_renderer_graph_edit.tscn | 1 + 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/classes/deck/deck.gd b/classes/deck/deck.gd index 2ca6ddc..73933b4 100644 --- a/classes/deck/deck.gd +++ b/classes/deck/deck.gd @@ -71,7 +71,9 @@ func get_node(uuid: String) -> DeckNode: ## Attempt to connect two nodes. Returns [code]true[/code] if the connection succeeded. -func connect_nodes(from_node: DeckNode, to_node: DeckNode, from_output_port: int, to_input_port: int) -> bool: +func connect_nodes(from_node_id: String, to_node_id: String, from_output_port: int, to_input_port: int) -> bool: + var from_node := get_node(from_node_id) + var to_node := get_node(to_node_id) # check that we can do the type conversion var type_a: DeckType.Types = from_node.get_output_ports()[from_output_port].type var type_b: DeckType.Types = to_node.get_input_ports()[to_input_port].type @@ -86,9 +88,10 @@ func connect_nodes(from_node: DeckNode, to_node: DeckNode, from_output_port: int ## Remove a connection from two nodes. -func disconnect_nodes(from_node: DeckNode, to_node: DeckNode, from_output_port: int, to_input_port: int) -> void: - var hash = {to_node._id: to_input_port}.hash() - +func disconnect_nodes(from_node_id: String, to_node_id: String, from_output_port: int, to_input_port: int) -> void: + var hash := {to_node_id: to_input_port}.hash() + var from_node := get_node(from_node_id) + var to_node := get_node(to_node_id) from_node.remove_outgoing_connection(from_output_port, hash) to_node.remove_incoming_connection(to_input_port) @@ -99,8 +102,24 @@ func is_empty() -> bool: ## Remove a node from this deck. -func remove_node(uuid: String) -> void: - var node = nodes.get(uuid) +func remove_node(uuid: String, remove_connections: bool = false) -> void: + var node := get_node(uuid) + if node == null: + return + + if remove_connections: + var outgoing_connections := node.outgoing_connections.duplicate(true) + + for output_port: int in outgoing_connections: + for connection: Dictionary in outgoing_connections[output_port]: + disconnect_nodes(uuid, connection.keys()[0], output_port, connection.values()[0]) + + var incoming_connections := node.incoming_connections.duplicate(true) + + for input_port: int in incoming_connections: + for from_node: String in incoming_connections[input_port]: + disconnect_nodes(from_node, uuid, incoming_connections[input_port][from_node], input_port) + nodes.erase(uuid) node_removed.emit(node) @@ -133,15 +152,19 @@ func group_nodes(nodes_to_group: Array) -> Deck: group.groups[_group_id] = _group _group._belonging_to = group - for from_port: int in node.outgoing_connections: - for connection: Dictionary in node.outgoing_connections[from_port]: - if !(connection.keys()[0] in node_ids_to_keep): - disconnect_nodes(node, get_node(connection.keys()[0]), from_port, connection.values()[0]) + var outgoing_connections := node.outgoing_connections.duplicate(true) - for to_port: int in node.incoming_connections: - for from_node: String in node.incoming_connections[to_port]: + for from_port: int in outgoing_connections: + for connection: Dictionary in outgoing_connections[from_port]: + if !(connection.keys()[0] in node_ids_to_keep): + disconnect_nodes(node._id, connection.keys()[0], from_port, connection.values()[0]) + + var incoming_connections := node.incoming_connections.duplicate(true) + + for to_port: int in incoming_connections: + for from_node: String in incoming_connections[to_port]: if !(from_node in node_ids_to_keep): - disconnect_nodes(get_node(from_node), node, node.incoming_connections[to_port].values()[0], to_port) + disconnect_nodes(from_node, node._id, incoming_connections[to_port][from_node], to_port) midpoint += node.position_as_vector2() remove_node(node._id) diff --git a/graph_node_renderer/deck_renderer_graph_edit.gd b/graph_node_renderer/deck_renderer_graph_edit.gd index 3cdd2e9..e2eb6a9 100644 --- a/graph_node_renderer/deck_renderer_graph_edit.gd +++ b/graph_node_renderer/deck_renderer_graph_edit.gd @@ -55,7 +55,7 @@ func attempt_connection(from_node_name: StringName, from_port: int, to_node_name #var from_output := from_node_renderer.node.get_global_port_idx_from_output(from_port) #var to_input := to_node_renderer.node.get_global_port_idx_from_input(to_port) - if deck.connect_nodes(from_node_renderer.node, to_node_renderer.node, from_port, to_port): + if deck.connect_nodes(from_node_renderer.node._id, to_node_renderer.node._id, from_port, to_port): connect_node( from_node_renderer.name, from_port, @@ -72,7 +72,7 @@ func attempt_disconnect(from_node_name: StringName, from_port: int, to_node_name #var from_output := from_node_renderer.node.get_global_port_idx_from_output(from_port) #var to_input := to_node_renderer.node.get_global_port_idx_from_input(to_port) - deck.disconnect_nodes(from_node_renderer.node, to_node_renderer.node, from_port, to_port) + deck.disconnect_nodes(from_node_renderer.node._id, to_node_renderer.node._id, from_port, to_port) disconnect_node( from_node_renderer.name, @@ -119,6 +119,7 @@ func initialize_from_deck() -> void: ## Loops through all [DeckNode]s in [member Deck.nodes] and calls ## [method GraphEdit.connect_node] for all the connections that exist in each func refresh_connections() -> void: + print(deck.nodes.size()) for node_id in deck.nodes: var node: DeckNode = deck.nodes[node_id] var from_node: DeckNodeRendererGraphNode = get_children().filter( @@ -223,3 +224,18 @@ func _on_add_node_menu_node_selected(type: String) -> void: get_node_renderer(node).position_offset = node_pos search_popup_panel.hide() + + +func _on_delete_nodes_request(nodes: Array[StringName]) -> void: + var node_ids := nodes.map( + func(n: StringName): + return (get_node(NodePath(n)) as DeckNodeRendererGraphNode).node._id + ) + + clear_connections() + + for node_id in node_ids: + deck.remove_node(node_id, true) + + #await get_tree().process_frame + refresh_connections() diff --git a/graph_node_renderer/deck_renderer_graph_edit.tscn b/graph_node_renderer/deck_renderer_graph_edit.tscn index 1d4e361..5f7167c 100644 --- a/graph_node_renderer/deck_renderer_graph_edit.tscn +++ b/graph_node_renderer/deck_renderer_graph_edit.tscn @@ -12,5 +12,6 @@ right_disconnects = true show_arrange_button = false script = ExtResource("1_pojfs") +[connection signal="delete_nodes_request" from="." to="." method="_on_delete_nodes_request"] [connection signal="popup_request" from="." to="." method="_on_popup_request"] [connection signal="scroll_offset_changed" from="." to="." method="_on_scroll_offset_changed"]