From f9069db8be33efea110665d20ce13a3a0783d8bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Fri, 21 Jul 2023 08:30:12 +0300 Subject: [PATCH] new way to instance nodes --- classes/deck/deck_node.gd | 3 ++ classes/deck/node_db.gd | 34 +++++++++++++++---- classes/deck/nodes/button.gd | 8 ++--- classes/deck/nodes/get_deck_var.gd | 8 ++--- classes/deck/nodes/no_type.gd | 1 - classes/deck/nodes/print.gd | 8 ++--- classes/deck/nodes/set_deck_var.gd | 8 ++--- .../deck_renderer_graph_edit.gd | 2 +- 8 files changed, 47 insertions(+), 25 deletions(-) delete mode 100644 classes/deck/nodes/no_type.gd diff --git a/classes/deck/deck_node.gd b/classes/deck/deck_node.gd index 79bf106..4f3e4e5 100644 --- a/classes/deck/deck_node.gd +++ b/classes/deck/deck_node.gd @@ -12,6 +12,9 @@ var _belonging_to: Deck var _id: String var node_type: String +var description: String +var aliases: Array[String] + var props_to_serialize: Array[StringName] enum PortType{ diff --git a/classes/deck/node_db.gd b/classes/deck/node_db.gd index 294824e..2758602 100644 --- a/classes/deck/node_db.gd +++ b/classes/deck/node_db.gd @@ -1,6 +1,8 @@ extends Node const BASE_NODE_PATH := "res://classes/deck/nodes/" + +# Dictionary[node_type, NodeDescriptor] var nodes: Dictionary = {} @@ -11,12 +13,11 @@ func _init() -> void: while current_file != "": print(current_file) if current_file.ends_with(".gd"): - var f := FileAccess.open(BASE_NODE_PATH.path_join(current_file), FileAccess.READ) - var line := f.get_line() - if !line.begins_with("###type="): - current_file = dir.get_next() - continue - nodes[line.trim_prefix("###type=")] = BASE_NODE_PATH.path_join(current_file) +# var f := FileAccess.open(BASE_NODE_PATH.path_join(current_file), FileAccess.READ) + var script_path := BASE_NODE_PATH.path_join(current_file) + var node: DeckNode = load(script_path).new() as DeckNode + var descriptor := NodeDescriptor.new(script_path, node.name, node.description, node.aliases) + nodes[node.node_type] = descriptor current_file = dir.get_next() @@ -24,4 +25,23 @@ func instance_node(type: String) -> DeckNode: if !nodes.has(type): return null - return load(nodes[type]).new() + return load(nodes[type]["script_path"]).new() + + +class NodeDescriptor: + var name: String + var description: String + var aliases: String + + var script_path: String + + + func _init(p_script_path: String, p_name: String, p_description: String, p_aliases: Array[String]) -> void: + script_path = p_script_path + + name = p_name + description = p_description + aliases = p_aliases.reduce( + func(accum, el) -> void: + accum += el + , "") diff --git a/classes/deck/nodes/button.gd b/classes/deck/nodes/button.gd index 2c0da4b..3bb3b15 100644 --- a/classes/deck/nodes/button.gd +++ b/classes/deck/nodes/button.gd @@ -1,13 +1,13 @@ -###type=button extends DeckNode func _init() -> void: + name = "Button" + node_type = "button" + description = "a button" + add_output_port( Deck.Types.BOOL, "Press me", "button" ) - - name = "Button" - node_type = name.to_snake_case() diff --git a/classes/deck/nodes/get_deck_var.gd b/classes/deck/nodes/get_deck_var.gd index 9de6736..b37a4ab 100644 --- a/classes/deck/nodes/get_deck_var.gd +++ b/classes/deck/nodes/get_deck_var.gd @@ -1,17 +1,17 @@ -###type=get_deck_var extends DeckNode func _init() -> void: + name = "Get Deck Var" + node_type = name.to_snake_case() + description = "retrieve a deck variable" + add_output_port( Deck.Types.STRING, "Variable", "field" ) - name = "Get Deck Var" - node_type = name.to_snake_case() - func _value_request(from_port: int) -> Variant: var key = ports[0].value_callback.call() diff --git a/classes/deck/nodes/no_type.gd b/classes/deck/nodes/no_type.gd deleted file mode 100644 index 61510e1..0000000 --- a/classes/deck/nodes/no_type.gd +++ /dev/null @@ -1 +0,0 @@ -extends Node diff --git a/classes/deck/nodes/print.gd b/classes/deck/nodes/print.gd index a6cf3e9..327217d 100644 --- a/classes/deck/nodes/print.gd +++ b/classes/deck/nodes/print.gd @@ -1,8 +1,11 @@ -###type=print extends DeckNode func _init() -> void: + name = "Print" + node_type = name.to_snake_case() + description = "print a value" + add_input_port( Deck.Types.STRING, "Text to print", @@ -21,9 +24,6 @@ func _init() -> void: "label" ) - name = "Print" - node_type = name.to_snake_case() - func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void: if to_port != 1: diff --git a/classes/deck/nodes/set_deck_var.gd b/classes/deck/nodes/set_deck_var.gd index b8cc44f..400aabd 100644 --- a/classes/deck/nodes/set_deck_var.gd +++ b/classes/deck/nodes/set_deck_var.gd @@ -1,8 +1,11 @@ -###type=set_deck_var extends DeckNode func _init() -> void: + name = "Set Deck Var" + node_type = name.to_snake_case() + description = "set deck variable" + add_input_port( Deck.Types.STRING, "Variable Name", @@ -27,9 +30,6 @@ func _init() -> void: "label" ) - name = "Set Deck Var" - node_type = name.to_snake_case() - func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void: if to_port != 2: diff --git a/graph_node_renderer/deck_renderer_graph_edit.gd b/graph_node_renderer/deck_renderer_graph_edit.gd index d4900bb..a83fcdb 100644 --- a/graph_node_renderer/deck_renderer_graph_edit.gd +++ b/graph_node_renderer/deck_renderer_graph_edit.gd @@ -62,7 +62,7 @@ func _ready() -> void: set_var.pressed.connect( func(): - var node := NodeDB.instance_node("set_deck_varbutton") + var node := NodeDB.instance_node("set_deck_var") deck.add_node_inst(node) var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate() node_renderer.node = node