[gd_scene load_steps=2 format=2]

[sub_resource type="GDScript" id=1]
script/source = "extends Control

var _gdpd

func _load_patch(pd_patch) :
	#separate file name from directory
	var patch_name = pd_patch.split(\"/\")[-1]
	var patch_dir = pd_patch.trim_suffix(patch_name)
	
	#load patch
	_gdpd.openPatch(patch_name, patch_dir)

func _ready() :
	_gdpd = load(\"res://addons/gdpd/bin/gdpd.gdns\").new()

func _process(delta) :
	while _gdpd.has_message() :
		var msg = _gdpd.get_next()
		print(msg)
		if msg[0] == \"random\" :
#			print(\"r\")
			pass

func _on_Start_pressed() :
	var inps = _gdpd.get_available_input_devices()
	var outs = _gdpd.get_available_output_devices()
#	_gdpd.init(0, 2, 48000, 64)
	_gdpd.init_devices(inps[0], outs[0])
	
	#the patch path should be the absolute one
	_load_patch(ProjectSettings.globalize_path(\"res://patch.pd\"))
	_load_patch(ProjectSettings.globalize_path(\"res://patch.pd\"))
	
	_gdpd.subscribe(\"toGodot\")

func _on_Stop_pressed() :
	
	_gdpd.closeAllPatches()
	_gdpd.stop()

func _on_Send_pressed() :
	_gdpd.start_message(1)
	_gdpd.add_float(300)
	_gdpd.finish_list(\"fromGodot\")

"

[node name="Control" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
script = SubResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Stop" type="Button" parent="."]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -50.0
margin_top = 75.0
margin_right = 50.0
margin_bottom = 125.0
text = "Stop"

[node name="Start" type="Button" parent="."]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -50.0
margin_top = -125.0
margin_right = 50.0
margin_bottom = -75.0
text = "Start"

[node name="Send" type="Button" parent="."]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -50.0
margin_top = -25.0
margin_right = 50.0
margin_bottom = 25.0
text = "send"

[connection signal="pressed" from="Stop" to="." method="_on_Stop_pressed"]
[connection signal="pressed" from="Start" to="." method="_on_Start_pressed"]
[connection signal="pressed" from="Send" to="." method="_on_Send_pressed"]
