refactor copy/paste to use dicts

This commit is contained in:
Lera Elvoé 2023-11-30 12:16:48 +03:00
parent 8520f240d0
commit 364b4a2070
No known key found for this signature in database

View file

@ -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 := {