unify ports into one container array

This commit is contained in:
Lera Elvoé 2023-06-12 18:32:16 +03:00
parent 0a309cf530
commit f0f3b2e685
No known key found for this signature in database
3 changed files with 107 additions and 6 deletions

View file

@ -5,18 +5,39 @@ var input_ports: Array[Port]
var output_ports: Array[Port] var output_ports: Array[Port]
var outgoing_connections: Dictionary var outgoing_connections: Dictionary
var ports: Array[Port]
var _belonging_to: Deck var _belonging_to: Deck
var _id: String var _id: String
enum PortType{
INPUT, ## Input port type (slot on the left).
OUTPUT, ## Output port type (slot on the right).
VIRTUAL, ## Virtual port type (no slot on left [i]or[/i] right).
}
func add_input_port(type: Deck.Types, label: String, descriptor: String = "") -> void: func add_input_port(type: Deck.Types, label: String, descriptor: String = "") -> void:
var port := Port.new(type, label, descriptor) var port := Port.new(type, label, ports.size(), PortType.INPUT, get_input_ports().size(), descriptor)
input_ports.append(port) ports.append(port)
func add_output_port(type: Deck.Types, label: String, descriptor: String = "") -> void: func add_output_port(type: Deck.Types, label: String, descriptor: String = "") -> void:
var port := Port.new(type, label, descriptor) var port := Port.new(type, label, ports.size(), PortType.OUTPUT, get_output_ports().size(), descriptor)
output_ports.append(port) ports.append(port)
func add_virtual_port(type: Deck.Types, label: String, descriptor: String = "") -> void:
var port := Port.new(type, label, ports.size(), PortType.VIRTUAL, get_virtual_ports().size(), descriptor)
ports.append(port)
func send_from_output_port(output_port: int, data: DeckType, extra_data: Array = []) -> void:
var global_port := get_global_port_idx_from_output(output_port)
if global_port == -1:
return
send(global_port, data, extra_data)
func send(from_port: int, data: DeckType, extra_data: Array = []) -> void: func send(from_port: int, data: DeckType, extra_data: Array = []) -> void:
@ -35,6 +56,14 @@ func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void:
pass pass
func add_outgoing_connection_to_port(output_port: int, to_node: String, to_input_port: int) -> void:
add_outgoing_connection(
get_global_port_idx_from_output(output_port),
to_node,
get_node(to_node).get_global_port_idx_from_input(to_input_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 port_connections: Array = outgoing_connections.get(from_port, []) var port_connections: Array = outgoing_connections.get(from_port, [])
port_connections.append({to_node: to_port}) port_connections.append({to_node: to_port})
@ -56,3 +85,57 @@ func remove_outgoing_connection(from_port: int, connection_hash: int) -> void:
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_outgoing_connection_from_port(output_port: int, connection_hash: int) -> void:
remove_outgoing_connection(get_global_port_idx_from_output(output_port), connection_hash)
func get_input_ports() -> Array[Port]:
return ports.filter(
func(port: Port) -> bool:
return port.port_type == PortType.INPUT
)
func get_output_ports() -> Array[Port]:
return ports.filter(
func(port: Port) -> bool:
return port.port_type == PortType.OUTPUT
)
func get_virtual_ports() -> Array[Port]:
return ports.filter(
func(port: Port) -> bool:
return port.port_type == PortType.VIRTUAL
)
func get_global_port_idx_from_input(idx: int) -> int:
if get_input_ports().size() > idx:
return get_input_ports()[idx].index
else:
return -1
func get_global_port_idx_from_output(idx: int) -> int:
if get_output_ports().size() > idx:
return get_output_ports()[idx].index
else:
return -1
func get_global_port_idx_from_virtual(idx: int) -> int:
if get_virtual_ports().size() > idx:
return get_virtual_ports()[idx].index
else:
return -1
func get_all_ports() -> Array[Port]:
return ports
func get_node(uuid: String) -> DeckNode:
return _belonging_to.get_node(uuid)

View file

@ -14,6 +14,12 @@ func _init() -> void:
"button" "button"
) )
add_output_port(
Deck.Types.BOOL,
"On Trigger",
"label"
)
name = "Print" name = "Print"

View file

@ -5,13 +5,25 @@ var label: String
var descriptor: String var descriptor: String
var value_callback: Callable var value_callback: Callable
var port_type: DeckNode.PortType
var index_of_type: int
var index: int
func _init( func _init(
p_type: Deck.Types, p_type: Deck.Types,
p_label: String, p_label: String,
p_index: int,
p_port_type: DeckNode.PortType,
p_index_of_type: int,
p_descriptor: String = "", p_descriptor: String = "",
p_value_callback: Callable = Callable()) -> void: # p_value_callback: Callable = Callable(),
) -> void:
type = p_type type = p_type
label = p_label label = p_label
descriptor = p_descriptor descriptor = p_descriptor
value_callback = p_value_callback # value_callback = p_value_callback
port_type = p_port_type
index_of_type = p_index_of_type
index = p_index