From 46169147a446562f52450ea76945c31424c4ac7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Tue, 13 Jun 2023 18:23:10 +0300 Subject: [PATCH] add call time resolution of port values --- classes/deck/deck.gd | 2 + classes/deck/deck_node.gd | 23 +++++++- classes/deck/nodes/get_deck_var.gd | 16 ++++++ classes/deck/nodes/print.gd | 9 +++- classes/deck/nodes/set_deck_var.gd | 52 +++++++++++++++++++ .../deck_renderer_graph_edit.gd | 25 ++++++++- 6 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 classes/deck/nodes/get_deck_var.gd create mode 100644 classes/deck/nodes/set_deck_var.gd diff --git a/classes/deck/deck.gd b/classes/deck/deck.gd index c466be3..2f16d2e 100644 --- a/classes/deck/deck.gd +++ b/classes/deck/deck.gd @@ -21,6 +21,8 @@ static var type_assoc: Dictionary = { Types.DICTIONARY: DeckType.DeckTypeDictionary, } +var variable_stack: Dictionary = {} + func add_node(node: GDScript, meta: Dictionary = {}) -> DeckNode: # TODO: accept instances of DeckNode instead of instancing here? diff --git a/classes/deck/deck_node.gd b/classes/deck/deck_node.gd index 63b4525..4d410a3 100644 --- a/classes/deck/deck_node.gd +++ b/classes/deck/deck_node.gd @@ -4,6 +4,7 @@ var name: String var input_ports: Array[Port] var output_ports: Array[Port] var outgoing_connections: Dictionary +var incoming_connections: Dictionary var ports: Array[Port] @@ -49,7 +50,7 @@ func send(from_port: int, data: DeckType, extra_data: Array = []) -> void: # key is node uuid # value is input port on destination node for node in connection: - _belonging_to.get_node(node)._receive(connection[node], data, extra_data) + get_node(node)._receive(connection[node], data, extra_data) func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void: @@ -68,6 +69,26 @@ func add_outgoing_connection(from_port: int, to_node: String, to_port: int) -> v var port_connections: Array = outgoing_connections.get(from_port, []) port_connections.append({to_node: to_port}) outgoing_connections[from_port] = port_connections + get_node(to_node).add_incoming_connection(to_port, _id, from_port) + + +func add_incoming_connection(to_port: int, from_node: String, from_port: int) -> void: + var connection := {from_node: from_port} + incoming_connections[to_port] = connection + + +func request_value(on_port: int) -> Variant: + if !incoming_connections.has(on_port): + return null + + var connection: Dictionary = incoming_connections[on_port] + var node := get_node(connection.keys()[0]) + return node._value_request(connection.values()[0]) + + +# override this +func _value_request(from_port: int) -> Variant: + return null func remove_outgoing_connection(from_port: int, connection_hash: int) -> void: diff --git a/classes/deck/nodes/get_deck_var.gd b/classes/deck/nodes/get_deck_var.gd new file mode 100644 index 0000000..7e64b0e --- /dev/null +++ b/classes/deck/nodes/get_deck_var.gd @@ -0,0 +1,16 @@ +extends DeckNode + + +func _init() -> void: + add_output_port( + Deck.Types.STRING, + "Variable", + "field" + ) + + name = "Get Deck Var" + + +func _value_request(from_port: int) -> Variant: + var key = ports[0].value_callback.call() + return _belonging_to.variable_stack[key] diff --git a/classes/deck/nodes/print.gd b/classes/deck/nodes/print.gd index e042156..23955ab 100644 --- a/classes/deck/nodes/print.gd +++ b/classes/deck/nodes/print.gd @@ -27,7 +27,14 @@ func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void: if to_port != 1: return - var data_to_print = ports[0].value_callback.call() if ports[0].value_callback.call() != "" else data.get_value() + var data_to_print + if request_value(0) != null: + data_to_print = request_value(0) + elif ports[0].value_callback.call() != "": + data_to_print = ports[0].value_callback.call() + else: + data_to_print = data.get_value() + # var data_to_print = input_ports[0].value_callback.call() print(data_to_print) print("extra data: ", extra_data) diff --git a/classes/deck/nodes/set_deck_var.gd b/classes/deck/nodes/set_deck_var.gd new file mode 100644 index 0000000..ac25dfe --- /dev/null +++ b/classes/deck/nodes/set_deck_var.gd @@ -0,0 +1,52 @@ +extends DeckNode + + +func _init() -> void: + add_input_port( + Deck.Types.STRING, + "Variable Name", + "field" + ) + + add_input_port( + Deck.Types.STRING, + "Value", + "field" + ) + + add_input_port( + Deck.Types.BOOL, + "Set", + "button" + ) + + add_output_port( + Deck.Types.STRING, + "Value", + "label" + ) + + name = "Set Deck Var" + + +func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void: + if to_port != 2: + return + + var var_name: String = get_value_for_port(0, data) + # String for now, until i figure out an Any type + var var_value: String = get_value_for_port(1, data) + + _belonging_to.variable_stack[var_name] = var_value + + send(3, DeckType.DeckTypeString.new(var_value)) + +# this can probably go into DeckNode with a different name that makes it clear +# that it prioritizes call-time resolution +func get_value_for_port(port: int, data: DeckType) -> Variant: + if request_value(port) != null: + return request_value(port) + elif ports[port].value_callback.call() != "": + return ports[port].value_callback.call() + else: + return data.get_value() diff --git a/graph_node_renderer/deck_renderer_graph_edit.gd b/graph_node_renderer/deck_renderer_graph_edit.gd index aeff7e6..d52900c 100644 --- a/graph_node_renderer/deck_renderer_graph_edit.gd +++ b/graph_node_renderer/deck_renderer_graph_edit.gd @@ -5,15 +5,22 @@ const NODE_SCENE := preload("res://graph_node_renderer/deck_node_renderer_graph_ var deck: Deck = Deck.new() var button_node = preload("res://classes/deck/nodes/button.gd") var print_node = preload("res://classes/deck/nodes/print.gd") - +var get_var_node = preload("res://classes/deck/nodes/get_deck_var.gd") +var set_var_node = preload("res://classes/deck/nodes/set_deck_var.gd") func _ready() -> void: var add_button := Button.new() add_button.text = "Button" var add_print := Button.new() add_print.text = "Print" + var get_var := Button.new() + get_var.text = "Get Var" + var set_var := Button.new() + set_var.text = "Set Var" get_zoom_hbox().add_child(add_button) get_zoom_hbox().add_child(add_print) + get_zoom_hbox().add_child(get_var) + get_zoom_hbox().add_child(set_var) add_button.pressed.connect( func(): @@ -31,6 +38,22 @@ func _ready() -> void: add_child(node_renderer) ) + get_var.pressed.connect( + func(): + var node := deck.add_node(get_var_node) + var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate() + node_renderer.node = node + add_child(node_renderer) + ) + + set_var.pressed.connect( + func(): + var node := deck.add_node(set_var_node) + var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate() + node_renderer.node = node + add_child(node_renderer) + ) + connection_request.connect(attempt_connection) disconnection_request.connect(attempt_disconnect)