From 4ed9b37b0fef2e97e346a8ad23caa9eec3f14644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Fri, 21 Jul 2023 12:12:20 +0300 Subject: [PATCH] properly disconnect nodes, removing incoming connections on target node --- classes/deck/deck.gd | 16 +++++++++++++ classes/deck/deck_node.gd | 8 +++++++ .../deck_renderer_graph_edit.gd | 24 +++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/classes/deck/deck.gd b/classes/deck/deck.gd index 153a67c..0da3237 100644 --- a/classes/deck/deck.gd +++ b/classes/deck/deck.gd @@ -79,7 +79,19 @@ func connect_nodes(from_node: DeckNode, to_node: DeckNode, from_port: int, to_po func disconnect_nodes(from_node: DeckNode, to_node: DeckNode, from_port: int, to_port: int) -> void: var hash = {to_node._id: to_port}.hash() +# prints({to_node._id: to_port}, "-", {to_node._id: to_port}.hash()) +# prints(from_node.outgoing_connections[0][0], "-", from_node.outgoing_connections[0][0].hash()) +# +# var a = {to_node._id: to_port} +# var b = from_node.outgoing_connections[0][0] +# +# print(typeof(a.values()[0])) +# print(typeof(b.values()[0])) +# +# prints(a, b) + from_node.remove_outgoing_connection(from_port, hash) + to_node.remove_incoming_connection(to_port) func to_json(with_meta: bool = true) -> String: @@ -117,10 +129,14 @@ static func from_json(json: String) -> Deck: # node.incoming_connections = nodes_data[node_id].incoming_connections for connection_id in nodes_data[node_id].outgoing_connections: var connection_data = nodes_data[node_id].outgoing_connections[connection_id] + for connection in connection_data: + connection[connection.keys()[0]] = int(connection.values()[0]) node.outgoing_connections[int(connection_id)] = connection_data for connection_id in nodes_data[node_id].incoming_connections: var connection_data = nodes_data[node_id].incoming_connections[connection_id] + for connection in connection_data: + connection_data[connection] = int(connection_data[connection]) node.incoming_connections[int(connection_id)] = connection_data for i in nodes_data[node_id].port_values.size(): diff --git a/classes/deck/deck_node.gd b/classes/deck/deck_node.gd index 3597f7b..d63ef30 100644 --- a/classes/deck/deck_node.gd +++ b/classes/deck/deck_node.gd @@ -102,18 +102,26 @@ func remove_outgoing_connection(from_port: int, connection_hash: int) -> void: if port_connections.is_empty(): return + var incoming_connection := {} + var to_remove: int = -1 for i in port_connections.size(): if port_connections[i].hash() == connection_hash: + to_remove = i if to_remove == -1: + print('nothing to remove') return port_connections.remove_at(to_remove) outgoing_connections[from_port] = port_connections +func remove_incoming_connection(to_port: int) -> void: + incoming_connections.erase(to_port) + + func remove_outgoing_connection_from_port(output_port: int, connection_hash: int) -> void: remove_outgoing_connection(get_global_port_idx_from_output(output_port), connection_hash) diff --git a/graph_node_renderer/deck_renderer_graph_edit.gd b/graph_node_renderer/deck_renderer_graph_edit.gd index dfadf4c..25d12be 100644 --- a/graph_node_renderer/deck_renderer_graph_edit.gd +++ b/graph_node_renderer/deck_renderer_graph_edit.gd @@ -50,6 +50,30 @@ func _ready() -> void: node_renderer.node = deck.nodes[node_id] add_child(node_renderer) node_renderer.position_offset = str_to_vector2(deck.nodes[node_id].get_meta("position_offset", "")) + + for node_id in deck.nodes: + var node: DeckNode = deck.nodes[node_id] + var from_node_name = get_children().filter( + func(c: DeckNodeRendererGraphNode): + return c.node._id == node_id + )[0].name +# "outgoing_connections": { +# "0": [ +# { +# "c8cc7dce-2a86-461c-a24f-0a4dbd06f379": 1 +# } +# ] +# }, + for from_port in node.outgoing_connections: + for connection in node.outgoing_connections[from_port]: + var to_node_id = connection.keys()[0] + var to_node_port = connection.values()[0] + var to_node_name = get_children().filter( + func(c: DeckNodeRendererGraphNode): + return c.node._id == to_node_id + )[0].name + prints(from_node_name, to_node_name) + connect_node(from_node_name, from_port, to_node_name, to_node_port) )