serialize to json/dictionary, instance by type

This commit is contained in:
Lera Elvoé 2023-06-24 06:39:50 +03:00
parent 46169147a4
commit 074edb1e1f
No known key found for this signature in database
13 changed files with 122 additions and 8 deletions

View file

@ -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)

View file

@ -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

27
classes/deck/node_db.gd Normal file
View file

@ -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()

View file

@ -1,3 +1,4 @@
###type=button
extends DeckNode
@ -9,3 +10,4 @@ func _init() -> void:
)
name = "Button"
node_type = name.to_snake_case()

View file

@ -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:

View file

@ -0,0 +1 @@
extends Node

View file

@ -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:

View file

@ -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:

View file

@ -43,3 +43,7 @@ func _ready() -> void:
0,
Color.WHITE,
)
func _on_position_offset_changed() -> void:
node.set_meta("position_offset", position_offset)

View file

@ -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"]

View file

@ -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)

View file

@ -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"]

View file

@ -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"