followup to 5eecfc9: store input ports in outgoing connection in an array

This commit is contained in:
Lera Elvoé 2023-11-30 09:06:06 +03:00
parent 5eecfc9675
commit d3bf258ba9
No known key found for this signature in database
3 changed files with 30 additions and 24 deletions

View file

@ -89,10 +89,9 @@ func connect_nodes(from_node_id: String, to_node_id: String, from_output_port: i
## Remove a connection from two nodes. ## Remove a connection from two nodes.
func disconnect_nodes(from_node_id: String, to_node_id: String, from_output_port: int, to_input_port: int) -> void: func disconnect_nodes(from_node_id: String, to_node_id: String, from_output_port: int, to_input_port: int) -> void:
var hash := {to_node_id: to_input_port}.hash()
var from_node := get_node(from_node_id) var from_node := get_node(from_node_id)
var to_node := get_node(to_node_id) var to_node := get_node(to_node_id)
from_node.remove_outgoing_connection(from_output_port, hash) from_node.remove_outgoing_connection(from_output_port, to_node_id, to_input_port)
to_node.remove_incoming_connection(to_input_port) to_node.remove_incoming_connection(to_input_port)
@ -252,10 +251,13 @@ static func from_dict(data: Dictionary, path: String = "") -> Deck:
node._pre_connection() node._pre_connection()
for from_port: String in nodes_data[node_id].outgoing_connections: for from_port: String in nodes_data[node_id].outgoing_connections:
var connection_data = nodes_data[node_id].outgoing_connections[from_port] var connection_data: Dictionary = nodes_data[node_id].outgoing_connections[from_port]
for connection in connection_data: node.outgoing_connections[int(from_port)] = {}
connection_data[connection] = int(connection_data[connection]) for to_node: String in connection_data:
node.outgoing_connections[int(from_port)] = connection_data var input_ports: Array = connection_data[to_node]
node.outgoing_connections[int(from_port)][to_node] = []
for to_input_port in input_ports:
node.outgoing_connections[int(from_port)][to_node].append(int(to_input_port))
for to_port: String in nodes_data[node_id].incoming_connections: for to_port: String in nodes_data[node_id].incoming_connections:
var connection_data = nodes_data[node_id].incoming_connections[to_port] var connection_data = nodes_data[node_id].incoming_connections[to_port]

View file

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

View file

@ -119,7 +119,6 @@ func initialize_from_deck() -> void:
## Loops through all [DeckNode]s in [member Deck.nodes] and calls ## Loops through all [DeckNode]s in [member Deck.nodes] and calls
## [method GraphEdit.connect_node] for all the connections that exist in each ## [method GraphEdit.connect_node] for all the connections that exist in each
func refresh_connections() -> void: func refresh_connections() -> void:
print(deck.nodes.size())
for node_id in deck.nodes: for node_id in deck.nodes:
var node: DeckNode = deck.nodes[node_id] var node: DeckNode = deck.nodes[node_id]
var from_node: DeckNodeRendererGraphNode = get_children().filter( var from_node: DeckNodeRendererGraphNode = get_children().filter(
@ -129,11 +128,12 @@ func refresh_connections() -> void:
for from_port in node.outgoing_connections: for from_port in node.outgoing_connections:
for to_node_id: String in node.outgoing_connections[from_port]: 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_ports = node.outgoing_connections[from_port][to_node_id]
var to_node: DeckNodeRendererGraphNode = get_children().filter( var to_node: DeckNodeRendererGraphNode = get_children().filter(
func(c: DeckNodeRendererGraphNode): func(c: DeckNodeRendererGraphNode):
return c.node._id == to_node_id return c.node._id == to_node_id
)[0] )[0]
for to_node_port: int in to_node_ports:
connect_node( connect_node(
from_node.name, from_node.name,
from_port, from_port,