miggor-StreamGraph/graph_node_renderer/deck_node_renderer_graph_node.gd

176 lines
5 KiB
GDScript3
Raw Normal View History

2023-06-12 17:32:36 +02:00
extends GraphNode
class_name DeckNodeRendererGraphNode
## [GraphNode] based renderer of [DeckNode]
2023-06-12 17:32:36 +02:00
2023-11-25 12:01:17 +01:00
## Stores the data container [DeckNode] that the visuals of this [DeckNodeRendererGraphNode]
## are based off.
var node: DeckNode
2023-06-12 17:32:36 +02:00
2023-11-25 12:01:17 +01:00
## Setups up all the properties based off [member node]. Including looping through
## [method DeckNode.get_all_ports()] and setting up all the descriptors.
2023-06-12 17:32:36 +02:00
func _ready() -> void:
title = node.name
node.position_updated.connect(_on_node_position_updated)
#node.port_added.connect(_on_node_port_added)
#node.port_removed.connect(_on_node_port_removed)
node.ports_updated.connect(_on_node_ports_updated)
2023-06-12 17:32:36 +02:00
for port in node.get_all_ports():
var descriptor_split := port.descriptor.split(":")
match descriptor_split[0]:
"button":
var button := Button.new()
add_child(button)
button.text = port.label
if port.port_type == DeckNode.PortType.OUTPUT:
button.pressed.connect(
func():
node.send(port.index_of_type, true)
2023-06-12 17:59:30 +02:00
)
elif port.port_type == DeckNode.PortType.INPUT:
button.pressed.connect(
func():
node._receive(port.index_of_type, true)
2023-06-12 17:32:36 +02:00
)
"field":
var line_edit := LineEdit.new()
add_child(line_edit)
2023-07-21 10:26:43 +02:00
if port.value:
line_edit.text = str(port.value)
2023-06-12 17:32:36 +02:00
line_edit.placeholder_text = port.label
port.value_callback = line_edit.get_text
2023-07-21 10:26:43 +02:00
line_edit.text_changed.connect(port.set_value)
2023-06-12 17:32:36 +02:00
_:
var label := Label.new()
add_child(label)
label.text = port.label
set_slot(
port.index,
port.port_type == DeckNode.PortType.INPUT,
port.type,
2023-06-12 17:32:36 +02:00
Color.WHITE,
port.port_type == DeckNode.PortType.OUTPUT,
port.type,
2023-06-12 17:32:36 +02:00
Color.WHITE,
)
2023-11-25 12:01:17 +01:00
## Connected to [signal GraphElement.position_offset_updated] and updates the
## [member node]s properties
func _on_position_offset_changed() -> void:
node.position.x = position_offset.x
node.position.y = position_offset.y
2023-11-25 12:01:17 +01:00
## Connected to [member node]s [signal position_updated] to keep parity with the
## data position.
func _on_node_position_updated(new_position: Dictionary) -> void:
position_offset.x = new_position.x
position_offset.y = new_position.y
2023-11-25 12:01:17 +01:00
## Connected to [member node]s [signal port_added] handles setting up the specified
## [member Port.descriptor] with it's required nodes/signals etc. + adding the port
## using [method GraphNode.set_slot]
func _on_node_port_added(port_idx: int) -> void:
var port := node.get_all_ports()[port_idx]
var descriptor_split := port.descriptor.split(":")
match descriptor_split[0]:
"button":
var button := Button.new()
add_child(button)
button.text = port.label
if port.port_type == DeckNode.PortType.OUTPUT:
button.pressed.connect(
func():
node.send(port.index_of_type, true)
)
elif port.port_type == DeckNode.PortType.INPUT:
button.pressed.connect(
func():
node._receive(port.index_of_type, true)
)
"field":
var line_edit := LineEdit.new()
add_child(line_edit)
if port.value:
line_edit.text = str(port.value)
line_edit.placeholder_text = port.label
port.value_callback = line_edit.get_text
line_edit.text_changed.connect(port.set_value)
_:
var label := Label.new()
add_child(label)
label.text = port.label
set_slot(
port.index,
port.port_type == DeckNode.PortType.INPUT,
port.type,
Color.WHITE,
port.port_type == DeckNode.PortType.OUTPUT,
port.type,
Color.WHITE,
)
2023-11-25 12:01:17 +01:00
## Connected to [member node]s [signal port_removed], queue_frees the [Node]
## used for the descriptor, as well as using [method GraphNode.set_slot] to remove the slot.
func _on_node_port_removed(port_idx: int) -> void:
set_slot(
port_idx,
false,
0,
Color.WHITE,
false,
0,
Color.WHITE,
)
get_child(port_idx).queue_free()
2023-11-25 12:01:17 +01:00
## Connected to [member node]s [signal ports_updated]. Remakes all of the ports
## + their descriptors whenever this is received to allow keeping the Renderers ports up to date.
func _on_node_ports_updated() -> void:
clear_all_slots()
for c in get_children():
c.queue_free()
for port in node.get_all_ports():
var descriptor_split := port.descriptor.split(":")
match descriptor_split[0]:
"button":
var button := Button.new()
add_child(button)
button.text = port.label
if port.port_type == DeckNode.PortType.OUTPUT:
button.pressed.connect(
func():
node.send(port.index_of_type, true)
)
elif port.port_type == DeckNode.PortType.INPUT:
button.pressed.connect(
func():
node._receive(port.index_of_type, true)
)
"field":
var line_edit := LineEdit.new()
add_child(line_edit)
if port.value:
line_edit.text = str(port.value)
line_edit.placeholder_text = port.label
port.value_callback = line_edit.get_text
line_edit.text_changed.connect(port.set_value)
_:
var label := Label.new()
add_child(label)
label.text = port.label
set_slot(
port.index,
port.port_type == DeckNode.PortType.INPUT,
port.type,
Color.WHITE,
port.port_type == DeckNode.PortType.OUTPUT,
port.type,
Color.WHITE,
)