properly disconnect nodes, removing incoming connections on target node

This commit is contained in:
Lera Elvoé 2023-07-21 12:12:20 +03:00
parent 2f92233ca0
commit 4ed9b37b0f
No known key found for this signature in database
3 changed files with 48 additions and 0 deletions

View file

@ -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: func disconnect_nodes(from_node: DeckNode, to_node: DeckNode, from_port: int, to_port: int) -> void:
var hash = {to_node._id: to_port}.hash() 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) from_node.remove_outgoing_connection(from_port, hash)
to_node.remove_incoming_connection(to_port)
func to_json(with_meta: bool = true) -> String: 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 # node.incoming_connections = nodes_data[node_id].incoming_connections
for connection_id in nodes_data[node_id].outgoing_connections: for connection_id in nodes_data[node_id].outgoing_connections:
var connection_data = nodes_data[node_id].outgoing_connections[connection_id] 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 node.outgoing_connections[int(connection_id)] = connection_data
for connection_id in nodes_data[node_id].incoming_connections: for connection_id in nodes_data[node_id].incoming_connections:
var connection_data = nodes_data[node_id].incoming_connections[connection_id] 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 node.incoming_connections[int(connection_id)] = connection_data
for i in nodes_data[node_id].port_values.size(): for i in nodes_data[node_id].port_values.size():

View file

@ -102,18 +102,26 @@ func remove_outgoing_connection(from_port: int, connection_hash: int) -> void:
if port_connections.is_empty(): if port_connections.is_empty():
return return
var incoming_connection := {}
var to_remove: int = -1 var to_remove: int = -1
for i in port_connections.size(): for i in port_connections.size():
if port_connections[i].hash() == connection_hash: if port_connections[i].hash() == connection_hash:
to_remove = i to_remove = i
if to_remove == -1: if to_remove == -1:
print('nothing to remove')
return return
port_connections.remove_at(to_remove) port_connections.remove_at(to_remove)
outgoing_connections[from_port] = port_connections 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: 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) remove_outgoing_connection(get_global_port_idx_from_output(output_port), connection_hash)

View file

@ -50,6 +50,30 @@ func _ready() -> void:
node_renderer.node = deck.nodes[node_id] node_renderer.node = deck.nodes[node_id]
add_child(node_renderer) add_child(node_renderer)
node_renderer.position_offset = str_to_vector2(deck.nodes[node_id].get_meta("position_offset", "")) 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)
) )