2023-06-12 17:32:36 +02:00
|
|
|
extends GraphEdit
|
|
|
|
|
|
|
|
const NODE_SCENE := preload("res://graph_node_renderer/deck_node_renderer_graph_node.tscn")
|
|
|
|
|
|
|
|
var deck: Deck = Deck.new()
|
2023-06-24 05:39:50 +02:00
|
|
|
#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")
|
2023-06-12 17:32:36 +02:00
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
var add_button := Button.new()
|
|
|
|
add_button.text = "Button"
|
|
|
|
var add_print := Button.new()
|
|
|
|
add_print.text = "Print"
|
2023-06-13 17:23:10 +02:00
|
|
|
var get_var := Button.new()
|
|
|
|
get_var.text = "Get Var"
|
|
|
|
var set_var := Button.new()
|
|
|
|
set_var.text = "Set Var"
|
2023-06-12 17:32:36 +02:00
|
|
|
get_zoom_hbox().add_child(add_button)
|
|
|
|
get_zoom_hbox().add_child(add_print)
|
2023-06-13 17:23:10 +02:00
|
|
|
get_zoom_hbox().add_child(get_var)
|
|
|
|
get_zoom_hbox().add_child(set_var)
|
2023-06-12 17:32:36 +02:00
|
|
|
|
2023-06-24 05:39:50 +02:00
|
|
|
var save_btn := Button.new()
|
|
|
|
save_btn.text = "Save"
|
|
|
|
get_zoom_hbox().add_child(save_btn)
|
2023-07-21 10:10:24 +02:00
|
|
|
var load_btn := Button.new()
|
|
|
|
load_btn.text = "Load"
|
|
|
|
get_zoom_hbox().add_child(load_btn)
|
2023-06-24 05:39:50 +02:00
|
|
|
|
|
|
|
save_btn.pressed.connect(
|
|
|
|
func():
|
|
|
|
var t = deck.to_json()
|
|
|
|
var f := FileAccess.open("user://save_test.json", FileAccess.WRITE)
|
|
|
|
f.store_string(t)
|
|
|
|
)
|
|
|
|
|
2023-07-21 10:10:24 +02:00
|
|
|
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", ""))
|
2023-07-21 11:12:20 +02:00
|
|
|
|
|
|
|
for node_id in deck.nodes:
|
|
|
|
var node: DeckNode = deck.nodes[node_id]
|
|
|
|
var from_node_name = get_children().filter(
|
|
|
|
func(c: DeckNodeRendererGraphNode):
|
|
|
|
return c.node._id == node_id
|
|
|
|
)[0].name
|
|
|
|
# "outgoing_connections": {
|
|
|
|
# "0": [
|
|
|
|
# {
|
|
|
|
# "c8cc7dce-2a86-461c-a24f-0a4dbd06f379": 1
|
|
|
|
# }
|
|
|
|
# ]
|
|
|
|
# },
|
|
|
|
for from_port in node.outgoing_connections:
|
|
|
|
for connection in node.outgoing_connections[from_port]:
|
|
|
|
var to_node_id = connection.keys()[0]
|
|
|
|
var to_node_port = connection.values()[0]
|
|
|
|
var to_node_name = get_children().filter(
|
|
|
|
func(c: DeckNodeRendererGraphNode):
|
|
|
|
return c.node._id == to_node_id
|
|
|
|
)[0].name
|
|
|
|
prints(from_node_name, to_node_name)
|
|
|
|
connect_node(from_node_name, from_port, to_node_name, to_node_port)
|
2023-07-21 10:10:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-06-12 17:32:36 +02:00
|
|
|
add_button.pressed.connect(
|
|
|
|
func():
|
2023-06-24 05:39:50 +02:00
|
|
|
var node := NodeDB.instance_node("button")
|
|
|
|
deck.add_node_inst(node)
|
2023-06-12 17:32:36 +02:00
|
|
|
var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate()
|
|
|
|
node_renderer.node = node
|
|
|
|
add_child(node_renderer)
|
|
|
|
)
|
|
|
|
|
|
|
|
add_print.pressed.connect(
|
|
|
|
func():
|
2023-06-24 05:39:50 +02:00
|
|
|
var node := NodeDB.instance_node("print")
|
|
|
|
deck.add_node_inst(node)
|
2023-06-12 17:32:36 +02:00
|
|
|
var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate()
|
|
|
|
node_renderer.node = node
|
|
|
|
add_child(node_renderer)
|
|
|
|
)
|
2023-06-12 17:59:30 +02:00
|
|
|
|
2023-06-13 17:23:10 +02:00
|
|
|
get_var.pressed.connect(
|
|
|
|
func():
|
2023-06-24 05:39:50 +02:00
|
|
|
var node := NodeDB.instance_node("get_deck_var")
|
|
|
|
deck.add_node_inst(node)
|
2023-06-13 17:23:10 +02:00
|
|
|
var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate()
|
|
|
|
node_renderer.node = node
|
|
|
|
add_child(node_renderer)
|
|
|
|
)
|
|
|
|
|
|
|
|
set_var.pressed.connect(
|
|
|
|
func():
|
2023-07-21 07:30:12 +02:00
|
|
|
var node := NodeDB.instance_node("set_deck_var")
|
2023-06-24 05:39:50 +02:00
|
|
|
deck.add_node_inst(node)
|
2023-06-13 17:23:10 +02:00
|
|
|
var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate()
|
|
|
|
node_renderer.node = node
|
|
|
|
add_child(node_renderer)
|
|
|
|
)
|
|
|
|
|
2023-06-12 17:59:30 +02:00
|
|
|
connection_request.connect(attempt_connection)
|
2023-06-13 15:06:17 +02:00
|
|
|
disconnection_request.connect(attempt_disconnect)
|
2023-06-12 17:59:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
func attempt_connection(from_node_name: StringName, from_port: int, to_node_name: StringName, to_port: int) -> void:
|
|
|
|
var from_node: DeckNode = get_node(NodePath(from_node_name)).node
|
|
|
|
var to_node: DeckNode = get_node(NodePath(to_node_name)).node
|
|
|
|
|
|
|
|
var from_output := from_node.get_global_port_idx_from_output(from_port)
|
|
|
|
var to_input := to_node.get_global_port_idx_from_input(to_port)
|
|
|
|
|
|
|
|
if deck.connect_nodes(from_node, to_node, from_output, to_input):
|
|
|
|
connect_node(from_node_name, from_port, to_node_name, to_port)
|
2023-06-13 15:06:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
func attempt_disconnect(from_node_name: StringName, from_port: int, to_node_name: StringName, to_port: int) -> void:
|
|
|
|
var from_node: DeckNode = get_node(NodePath(from_node_name)).node
|
|
|
|
var to_node: DeckNode = get_node(NodePath(to_node_name)).node
|
|
|
|
|
|
|
|
var from_output := from_node.get_global_port_idx_from_output(from_port)
|
|
|
|
var to_input := to_node.get_global_port_idx_from_input(to_port)
|
|
|
|
|
|
|
|
deck.disconnect_nodes(from_node, to_node, from_output, to_input)
|
|
|
|
|
|
|
|
disconnect_node(from_node_name, from_port, to_node_name, to_port)
|
2023-06-24 05:39:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _on_scroll_offset_changed(offset: Vector2) -> void:
|
|
|
|
deck.set_meta("offset", offset)
|
2023-07-21 10:10:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
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)
|