miggor-StreamGraph/graph_node_renderer/deck_renderer_graph_edit.gd
2023-06-24 06:39:50 +03:00

100 lines
3.2 KiB
GDScript

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")
func _ready() -> void:
var add_button := Button.new()
add_button.text = "Button"
var add_print := Button.new()
add_print.text = "Print"
var get_var := Button.new()
get_var.text = "Get Var"
var set_var := Button.new()
set_var.text = "Set Var"
get_zoom_hbox().add_child(add_button)
get_zoom_hbox().add_child(add_print)
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 := NodeDB.instance_node("button")
deck.add_node_inst(node)
var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate()
node_renderer.node = node
add_child(node_renderer)
)
add_print.pressed.connect(
func():
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)
)
get_var.pressed.connect(
func():
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)
)
set_var.pressed.connect(
func():
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)
)
connection_request.connect(attempt_connection)
disconnection_request.connect(attempt_disconnect)
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)
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)
func _on_scroll_offset_changed(offset: Vector2) -> void:
deck.set_meta("offset", offset)