store outgoing connections as a dictionary of dictionaries instead

This commit is contained in:
Lera Elvoé 2023-11-29 14:15:38 +03:00
parent 2c7ccb66db
commit 5eecfc9675
No known key found for this signature in database
3 changed files with 24 additions and 45 deletions

View file

@ -251,17 +251,17 @@ static func from_dict(data: Dictionary, path: String = "") -> Deck:
node._pre_connection()
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 from_port: String in nodes_data[node_id].outgoing_connections:
var connection_data = nodes_data[node_id].outgoing_connections[from_port]
for connection in connection_data:
connection_data[connection] = int(connection_data[connection])
node.incoming_connections[int(connection_id)] = connection_data
node.outgoing_connections[int(from_port)] = connection_data
for to_port: String in nodes_data[node_id].incoming_connections:
var connection_data = nodes_data[node_id].incoming_connections[to_port]
for connection in connection_data:
connection_data[connection] = int(connection_data[connection])
node.incoming_connections[int(to_port)] = connection_data
for i in node.ports.size():
var port_value: Variant

View file

@ -8,7 +8,7 @@ class_name DeckNode
var name: String
## A map of outgoing connections from this node, in the format[br]
## [code]Dictionary[int -> output port, Array[Dictionary[String -> DeckNode#_id, int -> input port]]][/code]
## [code]Dictionary[int -> output port, Dictionary[String -> DeckNode#_id, int -> input port]][/code]
var outgoing_connections: Dictionary
## A map of incoming connections to this node, in the format[br]
## [code]Dictionary[int -> input port, [Dictionary[String -> DeckNode#_id, int -> output port]][/code]
@ -101,12 +101,8 @@ func send(from_output_port: int, data: Variant, extra_data: Array = []) -> void:
if outgoing_connections.get(from_output_port) == null:
return
for connection in outgoing_connections[from_output_port]:
connection = connection as Dictionary
# key is node uuid
# value is input port on destination node
for node in connection:
get_node(node)._receive(connection[node], data, extra_data)
for node: String in outgoing_connections[from_output_port]:
get_node(node)._receive(outgoing_connections[from_output_port][node], data, extra_data)
## Virtual function that's called when this node receives data from another node's [method send] call.
@ -117,11 +113,11 @@ func _receive(to_input_port: int, data: Variant, extra_data: Array = []) -> void
## Add a connection from the output port at [param from_port] to [param to_node]'s input port
## at [param to_port].
func add_outgoing_connection(from_port: int, to_node: String, to_port: int) -> void:
var port_connections: Array = outgoing_connections.get(from_port, [])
port_connections.append({to_node: to_port})
outgoing_connections[from_port] = port_connections
var connection := {to_node: to_port}
var inner: Dictionary = outgoing_connections.get(from_port, {}) as Dictionary
inner.merge(connection)
outgoing_connections[from_port] = inner
get_node(to_node).add_incoming_connection(to_port, _id, from_port)
outgoing_connection_added.emit(from_port)
## Add an incoming connection from [param from_node]'s output port at [param from_port] to this node's
@ -153,24 +149,15 @@ func _value_request(from_port: int) -> Variant:
## Remove an outgoing connection from this node.
## Does [b]not[/b] remove the other node's incoming connection equivalent.
func remove_outgoing_connection(from_port: int, connection_hash: int) -> void:
var port_connections: Array = (outgoing_connections.get(from_port, []) as Array).duplicate(true)
if port_connections.is_empty():
var connections: Dictionary = outgoing_connections.duplicate(true).get(from_port, {}) as Dictionary
if connections.is_empty():
return
var incoming_connection := {}
for to_node: String in connections:
if {to_node: connections[to_node]}.hash() == connection_hash:
(outgoing_connections[from_port] as Dictionary).erase(to_node)
break
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
outgoing_connection_removed.emit(from_port)

View file

@ -128,20 +128,12 @@ func refresh_connections() -> void:
)[0]
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]
for to_node_id: String in node.outgoing_connections[from_port]:
var to_node_port = node.outgoing_connections[from_port][to_node_id]
var to_node: DeckNodeRendererGraphNode = get_children().filter(
func(c: DeckNodeRendererGraphNode):
return c.node._id == to_node_id
)[0]
#print("***")
#print("calling connect_node with:")
#print(from_node.node.name)
#print(from_node.node.get_port_type_idx_from_global(from_port))
#print(to_node.node.name)
#print(to_node.node.get_port_type_idx_from_global(to_node_port))
#print("***")
connect_node(
from_node.name,
from_port,