From 364b4a207046445ba1d8ee4b9a1999fa7c433ca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Thu, 30 Nov 2023 12:16:48 +0300 Subject: [PATCH] refactor copy/paste to use dicts --- classes/deck/deck.gd | 45 +++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/classes/deck/deck.gd b/classes/deck/deck.gd index 5a51433..07bf520 100644 --- a/classes/deck/deck.gd +++ b/classes/deck/deck.gd @@ -198,7 +198,7 @@ func get_group(uuid: String) -> Deck: return groups.get(uuid) -func copy_nodes_json(nodes_to_copy: Array[String]) -> String: +func copy_nodes(nodes_to_copy: Array[String]) -> Dictionary: var d := {"nodes": {}} for node_id: String in nodes_to_copy: @@ -226,7 +226,11 @@ func copy_nodes_json(nodes_to_copy: Array[String]) -> String: d.nodes.values()[0].position.x = 0 d.nodes.values()[0].position.y = 0 - return JSON.stringify(d) + return d + + +func copy_nodes_json(nodes_to_copy: Array[String]) -> String: + return JSON.stringify(copy_nodes(nodes_to_copy)) func allocate_ids(count: int) -> Array[String]: @@ -236,48 +240,47 @@ func allocate_ids(count: int) -> Array[String]: return res -func paste_nodes_from_json(json: String, position: Vector2 = Vector2()) -> void: - var d: Dictionary = JSON.parse_string(json) as Dictionary - if !d.get("nodes"): +func paste_nodes_from_dict(nodes: Dictionary, position: Vector2 = Vector2()) -> void: + if !nodes.get("nodes"): return - var new_ids := allocate_ids(d.nodes.size()) + var new_ids := allocate_ids(nodes.nodes.size()) var ids_map := {} - for i: int in d.nodes.keys().size(): - var node_id: String = d.nodes.keys()[i] + for i: int in nodes.nodes.keys().size(): + var node_id: String = nodes.nodes.keys()[i] ids_map[node_id] = new_ids[i] - var new_json := json - for old_id: String in ids_map: - new_json = new_json.replace(old_id, ids_map[old_id]) + for node_id: String in nodes.nodes: + nodes.nodes[node_id]._id = ids_map[node_id] - for node_id: String in d.nodes: - d.nodes[node_id]._id = ids_map[node_id] + nodes.nodes[node_id].position.x += position.x + nodes.nodes[node_id].position.y += position.y - d.nodes[node_id].position.x += position.x - d.nodes[node_id].position.y += position.y - - var outgoing_connections: Dictionary = d.nodes[node_id].outgoing_connections as Dictionary + var outgoing_connections: Dictionary = nodes.nodes[node_id].outgoing_connections as Dictionary var outgoing_connections_res := {} for from_port: String in outgoing_connections: outgoing_connections_res[from_port] = {} for to_node_id: String in outgoing_connections[from_port]: outgoing_connections_res[from_port][ids_map[to_node_id]] = outgoing_connections[from_port][to_node_id] - var incoming_connections: Dictionary = d.nodes[node_id].incoming_connections as Dictionary + var incoming_connections: Dictionary = nodes.nodes[node_id].incoming_connections as Dictionary var incoming_connections_res := {} for to_port: String in incoming_connections: incoming_connections_res[to_port] = {} for from_node_id: String in incoming_connections[to_port]: incoming_connections_res[to_port][ids_map[from_node_id]] = incoming_connections[to_port][from_node_id] - d.nodes[node_id].outgoing_connections = outgoing_connections_res - d.nodes[node_id].incoming_connections = incoming_connections_res + nodes.nodes[node_id].outgoing_connections = outgoing_connections_res + nodes.nodes[node_id].incoming_connections = incoming_connections_res - var node := DeckNode.from_dict(d.nodes[node_id]) + var node := DeckNode.from_dict(nodes.nodes[node_id]) add_node_inst(node, ids_map[node_id]) +func paste_nodes_from_json(json: String, position: Vector2 = Vector2()) -> void: + paste_nodes_from_dict(JSON.parse_string(json), position) + + ## Returns a [Dictionary] representation of this deck. func to_dict(with_meta: bool = true) -> Dictionary: var inner := {