From 074edb1e1f4c225dcb866d672f0526facf337e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Sat, 24 Jun 2023 06:39:50 +0300 Subject: [PATCH] serialize to json/dictionary, instance by type --- classes/deck/deck.gd | 28 +++++++++++++++ classes/deck/deck_node.gd | 19 ++++++++++ classes/deck/node_db.gd | 27 ++++++++++++++ classes/deck/nodes/button.gd | 2 ++ classes/deck/nodes/get_deck_var.gd | 2 ++ classes/deck/nodes/no_type.gd | 1 + classes/deck/nodes/print.gd | 2 ++ classes/deck/nodes/set_deck_var.gd | 2 ++ .../deck_node_renderer_graph_node.gd | 4 +++ .../deck_node_renderer_graph_node.tscn | 2 ++ .../deck_renderer_graph_edit.gd | 35 ++++++++++++++----- .../deck_renderer_graph_edit.tscn | 2 ++ project.godot | 4 +++ 13 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 classes/deck/node_db.gd create mode 100644 classes/deck/nodes/no_type.gd diff --git a/classes/deck/deck.gd b/classes/deck/deck.gd index 2f16d2e..b18df9d 100644 --- a/classes/deck/deck.gd +++ b/classes/deck/deck.gd @@ -39,6 +39,19 @@ func add_node(node: GDScript, meta: Dictionary = {}) -> DeckNode: return node_inst +func add_node_inst(node: DeckNode) -> DeckNode: + var uuid := UUID.v4() + nodes[uuid] = node + node._belonging_to = self + node._id = uuid + +# if !meta.is_empty(): +# for k in meta: +# node.set_meta(k, meta[k]) + + return node + + func get_node(uuid: String) -> DeckNode: return nodes.get(uuid) @@ -61,3 +74,18 @@ func connect_nodes(from_node: DeckNode, to_node: DeckNode, from_port: int, to_po func disconnect_nodes(from_node: DeckNode, to_node: DeckNode, from_port: int, to_port: int) -> void: var hash = {to_node._id: to_port}.hash() from_node.remove_outgoing_connection(from_port, hash) + + +func to_json(with_meta: bool = true) -> String: + var inner := {"nodes": {}} + + for id in nodes.keys(): + inner["nodes"][id] = nodes[id].to_dict(with_meta) + + var d := {"deck": inner} + + if with_meta: + d["meta"] = {} + for meta in get_meta_list(): + d["meta"][meta] = get_meta(meta) + return JSON.stringify(d, "\t", false) diff --git a/classes/deck/deck_node.gd b/classes/deck/deck_node.gd index 4d410a3..79bf106 100644 --- a/classes/deck/deck_node.gd +++ b/classes/deck/deck_node.gd @@ -10,6 +10,9 @@ var ports: Array[Port] var _belonging_to: Deck var _id: String +var node_type: String + +var props_to_serialize: Array[StringName] enum PortType{ INPUT, ## Input port type (slot on the left). @@ -160,3 +163,19 @@ func get_all_ports() -> Array[Port]: func get_node(uuid: String) -> DeckNode: return _belonging_to.get_node(uuid) + + +func to_dict(with_meta: bool = true) -> Dictionary: + var d := { + "_id": _id, + "name": name, + "outgoing_connections": outgoing_connections, + "incoming_connections": incoming_connections, + "props": props_to_serialize, + "node_type": node_type, + } + if with_meta: + d["meta"] = {} + for meta in get_meta_list(): + d["meta"][meta] = get_meta(meta) + return d diff --git a/classes/deck/node_db.gd b/classes/deck/node_db.gd new file mode 100644 index 0000000..294824e --- /dev/null +++ b/classes/deck/node_db.gd @@ -0,0 +1,27 @@ +extends Node + +const BASE_NODE_PATH := "res://classes/deck/nodes/" +var nodes: Dictionary = {} + + +func _init() -> void: + var dir := DirAccess.open(BASE_NODE_PATH) + dir.list_dir_begin() + var current_file := dir.get_next() + 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) + current_file = dir.get_next() + + +func instance_node(type: String) -> DeckNode: + if !nodes.has(type): + return null + + return load(nodes[type]).new() diff --git a/classes/deck/nodes/button.gd b/classes/deck/nodes/button.gd index f983540..2c0da4b 100644 --- a/classes/deck/nodes/button.gd +++ b/classes/deck/nodes/button.gd @@ -1,3 +1,4 @@ +###type=button extends DeckNode @@ -9,3 +10,4 @@ func _init() -> void: ) 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 7e64b0e..9de6736 100644 --- a/classes/deck/nodes/get_deck_var.gd +++ b/classes/deck/nodes/get_deck_var.gd @@ -1,3 +1,4 @@ +###type=get_deck_var extends DeckNode @@ -9,6 +10,7 @@ func _init() -> void: ) name = "Get Deck Var" + node_type = name.to_snake_case() func _value_request(from_port: int) -> Variant: diff --git a/classes/deck/nodes/no_type.gd b/classes/deck/nodes/no_type.gd new file mode 100644 index 0000000..61510e1 --- /dev/null +++ b/classes/deck/nodes/no_type.gd @@ -0,0 +1 @@ +extends Node diff --git a/classes/deck/nodes/print.gd b/classes/deck/nodes/print.gd index 23955ab..a6cf3e9 100644 --- a/classes/deck/nodes/print.gd +++ b/classes/deck/nodes/print.gd @@ -1,3 +1,4 @@ +###type=print extends DeckNode @@ -21,6 +22,7 @@ func _init() -> void: ) name = "Print" + node_type = name.to_snake_case() func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void: diff --git a/classes/deck/nodes/set_deck_var.gd b/classes/deck/nodes/set_deck_var.gd index ac25dfe..b8cc44f 100644 --- a/classes/deck/nodes/set_deck_var.gd +++ b/classes/deck/nodes/set_deck_var.gd @@ -1,3 +1,4 @@ +###type=set_deck_var extends DeckNode @@ -27,6 +28,7 @@ func _init() -> void: ) name = "Set Deck Var" + node_type = name.to_snake_case() func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void: diff --git a/graph_node_renderer/deck_node_renderer_graph_node.gd b/graph_node_renderer/deck_node_renderer_graph_node.gd index 4854c7d..7c8ab1e 100644 --- a/graph_node_renderer/deck_node_renderer_graph_node.gd +++ b/graph_node_renderer/deck_node_renderer_graph_node.gd @@ -43,3 +43,7 @@ func _ready() -> void: 0, Color.WHITE, ) + + +func _on_position_offset_changed() -> void: + node.set_meta("position_offset", position_offset) diff --git a/graph_node_renderer/deck_node_renderer_graph_node.tscn b/graph_node_renderer/deck_node_renderer_graph_node.tscn index d716bd1..2d66ba3 100644 --- a/graph_node_renderer/deck_node_renderer_graph_node.tscn +++ b/graph_node_renderer/deck_node_renderer_graph_node.tscn @@ -7,3 +7,5 @@ offset_right = 280.0 offset_bottom = 217.0 title = "Deck Node" script = ExtResource("1_pos0w") + +[connection signal="position_offset_changed" from="." to="." method="_on_position_offset_changed"] diff --git a/graph_node_renderer/deck_renderer_graph_edit.gd b/graph_node_renderer/deck_renderer_graph_edit.gd index d52900c..d4900bb 100644 --- a/graph_node_renderer/deck_renderer_graph_edit.gd +++ b/graph_node_renderer/deck_renderer_graph_edit.gd @@ -3,10 +3,10 @@ extends GraphEdit const NODE_SCENE := preload("res://graph_node_renderer/deck_node_renderer_graph_node.tscn") 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") +#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() @@ -22,9 +22,21 @@ func _ready() -> void: get_zoom_hbox().add_child(get_var) get_zoom_hbox().add_child(set_var) + var save_btn := Button.new() + save_btn.text = "Save" + get_zoom_hbox().add_child(save_btn) + + save_btn.pressed.connect( + func(): + var t = deck.to_json() + var f := FileAccess.open("user://save_test.json", FileAccess.WRITE) + f.store_string(t) + ) + add_button.pressed.connect( func(): - var node := deck.add_node(button_node) + var node := NodeDB.instance_node("button") + deck.add_node_inst(node) var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate() node_renderer.node = node add_child(node_renderer) @@ -32,7 +44,8 @@ func _ready() -> void: add_print.pressed.connect( func(): - var node := deck.add_node(print_node) + var node := NodeDB.instance_node("print") + deck.add_node_inst(node) var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate() node_renderer.node = node add_child(node_renderer) @@ -40,7 +53,8 @@ func _ready() -> void: get_var.pressed.connect( func(): - var node := deck.add_node(get_var_node) + var node := NodeDB.instance_node("get_deck_var") + deck.add_node_inst(node) var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate() node_renderer.node = node add_child(node_renderer) @@ -48,7 +62,8 @@ func _ready() -> void: set_var.pressed.connect( func(): - var node := deck.add_node(set_var_node) + var node := NodeDB.instance_node("set_deck_varbutton") + deck.add_node_inst(node) var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate() node_renderer.node = node add_child(node_renderer) @@ -79,3 +94,7 @@ func attempt_disconnect(from_node_name: StringName, from_port: int, to_node_name deck.disconnect_nodes(from_node, to_node, from_output, to_input) disconnect_node(from_node_name, from_port, to_node_name, to_port) + + +func _on_scroll_offset_changed(offset: Vector2) -> void: + deck.set_meta("offset", offset) diff --git a/graph_node_renderer/deck_renderer_graph_edit.tscn b/graph_node_renderer/deck_renderer_graph_edit.tscn index 7f6f336..a7720e3 100644 --- a/graph_node_renderer/deck_renderer_graph_edit.tscn +++ b/graph_node_renderer/deck_renderer_graph_edit.tscn @@ -10,3 +10,5 @@ grow_horizontal = 2 grow_vertical = 2 right_disconnects = true script = ExtResource("1_pojfs") + +[connection signal="scroll_offset_changed" from="." to="." method="_on_scroll_offset_changed"] diff --git a/project.godot b/project.godot index a0a95f3..844c23c 100644 --- a/project.godot +++ b/project.godot @@ -16,3 +16,7 @@ run/main_scene="res://graph_node_renderer/deck_renderer_graph_edit.tscn" config/features=PackedStringArray("4.1", "Forward Plus") run/low_processor_mode=true config/icon="res://icon.svg" + +[autoload] + +NodeDB="*res://classes/deck/node_db.gd"