diff --git a/classes/deck/deck.gd b/classes/deck/deck.gd index b18df9d..88d1f03 100644 --- a/classes/deck/deck.gd +++ b/classes/deck/deck.gd @@ -39,11 +39,17 @@ 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 +func add_node_inst(node: DeckNode, assign_id: String = "", assign_to_self: bool = true) -> DeckNode: + if assign_to_self: + node._belonging_to = self + + if assign_id == "": + var uuid := UUID.v4() + nodes[uuid] = node + node._id = uuid + else: + nodes[assign_id] = node + # if !meta.is_empty(): # for k in meta: @@ -77,7 +83,7 @@ func disconnect_nodes(from_node: DeckNode, to_node: DeckNode, from_port: int, to func to_json(with_meta: bool = true) -> String: - var inner := {"nodes": {}} + var inner := {"nodes": {}, "variable_stack": variable_stack} for id in nodes.keys(): inner["nodes"][id] = nodes[id].to_dict(with_meta) @@ -89,3 +95,38 @@ func to_json(with_meta: bool = true) -> String: for meta in get_meta_list(): d["meta"][meta] = get_meta(meta) return JSON.stringify(d, "\t", false) + + +static func from_json(json: String) -> Deck: + var data: Dictionary = JSON.parse_string(json) as Dictionary + var deck := Deck.new() + + deck.variable_stack = data.deck.variable_stack + + for key in data.meta: + deck.set_meta(key, data.meta[key]) + + var nodes_data: Dictionary = data.deck.nodes as Dictionary + + for node_id in nodes_data: + var node := deck.add_node_inst(NodeDB.instance_node(nodes_data[node_id].node_type), node_id, false) + node._id = node_id + node.name = nodes_data[node_id].name + node._belonging_to = deck +# node.outgoing_connections = nodes_data[node_id].outgoing_connections +# node.incoming_connections = nodes_data[node_id].incoming_connections + for connection_id in nodes_data[node_id].outgoing_connections: + var connection_data = nodes_data[node_id].outgoing_connections[connection_id] + node.outgoing_connections[int(connection_id)] = connection_data + + for connection_id in nodes_data[node_id].incoming_connections: + var connection_data = nodes_data[node_id].incoming_connections[connection_id] + node.incoming_connections[int(connection_id)] = connection_data + + for key in nodes_data[node_id].meta: + node.set_meta(key, nodes_data[node_id].meta[key]) + + for prop in nodes_data[node_id].props: + node.set(prop, nodes_data[node_id].props[prop]) + + return deck diff --git a/classes/deck/deck_node.gd b/classes/deck/deck_node.gd index 4f3e4e5..a40af53 100644 --- a/classes/deck/deck_node.gd +++ b/classes/deck/deck_node.gd @@ -174,9 +174,13 @@ func to_dict(with_meta: bool = true) -> Dictionary: "name": name, "outgoing_connections": outgoing_connections, "incoming_connections": incoming_connections, - "props": props_to_serialize, + "props": {}, "node_type": node_type, } + + for prop in props_to_serialize: + d.props[prop] = get(prop) + if with_meta: d["meta"] = {} for meta in get_meta_list(): diff --git a/classes/deck/nodes/print.gd b/classes/deck/nodes/print.gd index 327217d..0296121 100644 --- a/classes/deck/nodes/print.gd +++ b/classes/deck/nodes/print.gd @@ -1,11 +1,15 @@ extends DeckNode +var times_activated := 0 + func _init() -> void: name = "Print" node_type = name.to_snake_case() description = "print a value" + props_to_serialize = [&"times_activated"] + add_input_port( Deck.Types.STRING, "Text to print", @@ -37,6 +41,8 @@ func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void: else: data_to_print = data.get_value() + times_activated += 1 + # var data_to_print = input_ports[0].value_callback.call() print(data_to_print) print("extra data: ", extra_data) diff --git a/graph_node_renderer/deck_renderer_graph_edit.gd b/graph_node_renderer/deck_renderer_graph_edit.gd index a83fcdb..dfadf4c 100644 --- a/graph_node_renderer/deck_renderer_graph_edit.gd +++ b/graph_node_renderer/deck_renderer_graph_edit.gd @@ -25,6 +25,9 @@ func _ready() -> void: var save_btn := Button.new() save_btn.text = "Save" get_zoom_hbox().add_child(save_btn) + var load_btn := Button.new() + load_btn.text = "Load" + get_zoom_hbox().add_child(load_btn) save_btn.pressed.connect( func(): @@ -33,6 +36,23 @@ func _ready() -> void: f.store_string(t) ) + load_btn.pressed.connect( + func(): + var f := FileAccess.open("user://save_test.json", FileAccess.READ) + for i in get_children(): + i.queue_free() + + deck = Deck.from_json(f.get_as_text()) + scroll_offset = str_to_vector2(deck.get_meta("offset", "")) + + for node_id in deck.nodes: + var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate() + node_renderer.node = deck.nodes[node_id] + add_child(node_renderer) + node_renderer.position_offset = str_to_vector2(deck.nodes[node_id].get_meta("position_offset", "")) + ) + + add_button.pressed.connect( func(): var node := NodeDB.instance_node("button") @@ -98,3 +118,13 @@ func attempt_disconnect(from_node_name: StringName, from_port: int, to_node_name func _on_scroll_offset_changed(offset: Vector2) -> void: deck.set_meta("offset", offset) + + +func str_to_vector2(s: String) -> Vector2: + if s.is_empty(): + return Vector2() + + var san := s.trim_prefix("(").trim_suffix(")").split(",") + var x := float(san[0]) + var y := float(san[1]) + return Vector2(x, y) diff --git a/test.gd b/test.gd deleted file mode 100644 index fbe3c1c..0000000 --- a/test.gd +++ /dev/null @@ -1,49 +0,0 @@ -extends Control - -var node_renderer_scene := preload("res://test_node_renderer.tscn") -@onready var nodes_container: HBoxContainer = $NodesContainer - -@onready var add_button_button: Button = $AddButtonButton -@onready var add_print_button: Button = $AddPrintButton -@onready var connect_them_button: Button = $ConnectThemButton -@onready var disconnect_them_button: Button = $DisconnectThemButton - -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") - - -func _ready() -> void: - add_button_button.pressed.connect( - func(): - var node := deck.add_node(button_node) - var node_renderer = node_renderer_scene.instantiate() - node_renderer.node = node - nodes_container.add_child(node_renderer) - add_button_button.disabled = true - ) - - add_print_button.pressed.connect( - func(): - var node := deck.add_node(print_node) - var node_renderer = node_renderer_scene.instantiate() - node_renderer.node = node - nodes_container.add_child(node_renderer) - add_print_button.disabled = true - ) - - connect_them_button.pressed.connect( - func(): - var node_a: DeckNode = nodes_container.get_child(0).node - var node_b: DeckNode = nodes_container.get_child(1).node - - deck.connect_nodes(node_a, node_b, 0, 1) - ) - - disconnect_them_button.pressed.connect( - func(): - var node_a: DeckNode = nodes_container.get_child(0).node - var node_b: DeckNode = nodes_container.get_child(1).node - - deck.disconnect_nodes(node_a, node_b, 0, 1) - ) diff --git a/test.tscn b/test.tscn deleted file mode 100644 index c849215..0000000 --- a/test.tscn +++ /dev/null @@ -1,54 +0,0 @@ -[gd_scene load_steps=2 format=3 uid="uid://bhpd6rfiuimw5"] - -[ext_resource type="Script" path="res://test.gd" id="1_in4g7"] - -[node name="Test" type="Control"] -layout_mode = 3 -anchors_preset = 15 -anchor_right = 1.0 -anchor_bottom = 1.0 -grow_horizontal = 2 -grow_vertical = 2 -script = ExtResource("1_in4g7") - -[node name="AddButtonButton" type="Button" parent="."] -layout_mode = 0 -offset_left = 26.0 -offset_top = 576.0 -offset_right = 119.0 -offset_bottom = 607.0 -text = "Add Button" - -[node name="AddPrintButton" type="Button" parent="."] -layout_mode = 0 -offset_left = 152.0 -offset_top = 576.0 -offset_right = 248.0 -offset_bottom = 607.0 -text = "Add Print" - -[node name="ConnectThemButton" type="Button" parent="."] -layout_mode = 0 -offset_left = 283.0 -offset_top = 576.0 -offset_right = 379.0 -offset_bottom = 607.0 -text = "Connect them" - -[node name="DisconnectThemButton" type="Button" parent="."] -layout_mode = 0 -offset_left = 421.0 -offset_top = 576.0 -offset_right = 538.0 -offset_bottom = 607.0 -text = "Disconnect them" - -[node name="NodesContainer" type="HBoxContainer" parent="."] -layout_mode = 1 -anchors_preset = -1 -anchor_right = 0.999646 -anchor_bottom = 0.245654 -offset_right = 0.40799 -offset_bottom = -0.184006 -theme_override_constants/separation = 20 -metadata/_edit_use_anchors_ = true