control.gd 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. extends Control
  2. var mainmenu_visible : bool = false #used to test if mainmenu is open
  3. var effect_position = Vector2(40,40) #tracks mouse position for node placement offset
  4. @onready var graph_edit = $GraphEdit
  5. var selected_nodes = {} #used to track which nodes in the GraphEdit are selected
  6. var cdpprogs_location #stores the cdp programs location from user prefs for easy access
  7. var delete_intermediate_outputs # tracks state of delete intermediate outputs toggle
  8. @onready var console_output: RichTextLabel = $Console/ConsoleOutput
  9. var final_output_dir
  10. var copied_nodes_data = [] #stores node data on ctrl+c
  11. var copied_connections = [] #stores all connections on ctrl+c
  12. var undo_redo := UndoRedo.new()
  13. var output_audio_player #tracks the node that is the current output player for linking
  14. var input_audio_player #tracks node that is the current input player for linking
  15. var outfile = "no file" #tracks dir of output file from cdp process
  16. var currentfile = "none" #tracks dir of currently loaded file for saving
  17. var changesmade = false #tracks if user has made changes to the currently loaded save file
  18. var savestate # tracks what the user is trying to do when savechangespopup is called
  19. var helpfile #tracks which help file the user was trying to load when savechangespopup is called
  20. var outfilename #links to the user name for outputfile field
  21. var foldertoggle #links to the reuse folder button
  22. var lastoutputfolder = "none" #tracks last output folder, this can in future be used to replace global.outfile but i cba right now
  23. var process_successful #tracks if the last run process was successful
  24. var help_data := {} #stores help data for each node to display in help popup
  25. var HelpWindowScene = preload("res://scenes/main/help_window.tscn")
  26. var uiscale = 1.0 #tracks scaling for retina screens
  27. # Called when the node enters the scene tree for the first time.
  28. func _ready() -> void:
  29. Nodes.hide()
  30. $mainmenu.hide()
  31. #$"mainmenu/select_effect/Time Domain".show()
  32. #$"mainmenu/select_effect/Time Domain/Distort".show()
  33. #$"mainmenu/select_effect/Frequency Domain/Convert".show()
  34. $NoLocationPopup.hide()
  35. $Console.hide()
  36. $NoInputPopup.hide()
  37. $MultipleConnectionsPopup.hide()
  38. $AudioSettings.hide()
  39. $AudioDevicePopup.hide()
  40. $SearchMenu.hide()
  41. $Settings.hide()
  42. $SaveDialog.access = FileDialog.ACCESS_FILESYSTEM
  43. $SaveDialog.file_mode = FileDialog.FILE_MODE_SAVE_FILE
  44. $SaveDialog.filters = ["*.thd"]
  45. $LoadDialog.access = FileDialog.ACCESS_FILESYSTEM
  46. $LoadDialog.file_mode = FileDialog.FILE_MODE_OPEN_FILE
  47. $LoadDialog.filters = ["*.thd"]
  48. #Goes through all nodes in scene and checks for buttons in the make_node_buttons group
  49. #Associates all buttons with the _on_button_pressed fuction and passes the button as an argument
  50. for child in get_tree().get_nodes_in_group("make_node_buttons"):
  51. if child is Button:
  52. child.pressed.connect(_on_button_pressed.bind(child))
  53. get_node("SearchMenu").make_node.connect(_make_node_from_search_menu)
  54. get_node("mainmenu").make_node.connect(_make_node_from_search_menu)
  55. get_node("mainmenu").open_help.connect(show_help_for_node)
  56. get_node("Settings").open_cdp_location.connect(show_cdp_location)
  57. get_node("Settings").console_on_top.connect(change_console_settings)
  58. check_user_preferences()
  59. get_tree().set_auto_accept_quit(false) #disable closing the app with the x and instead handle it internally
  60. #Check export config for version number and set about menu to current version
  61. #Assumes version of mac + linux builds is the same as windows
  62. #Requires manual update for alpha and beta builds but once the -beta is removed will be fully automatic so long as version is updated on export
  63. var export_config = ConfigFile.new()
  64. export_config.load("res://export_presets.cfg")
  65. $MenuBar/About.set_item_text(0, "SoundThread v" + export_config.get_value("preset.0.options", "application/product_version", "version unknown") + "-alpha")
  66. #checks if display is hidpi and scales ui accordingly hidpi - 144
  67. if DisplayServer.screen_get_dpi(0) >= 144:
  68. uiscale = 2.0
  69. get_window().content_scale_factor = uiscale
  70. #goes through popup_windows group and scales all popups and resizes them
  71. for window in get_tree().get_nodes_in_group("popup_windows"):
  72. window.size = window.size * uiscale
  73. window.content_scale_factor = uiscale
  74. #checks if user has opened a file from the system file menu and loads it
  75. var args = OS.get_cmdline_args()
  76. for arg in args:
  77. var path = arg.strip_edges()
  78. if FileAccess.file_exists(path) and path.get_extension().to_lower() == "thd":
  79. load_graph_edit(path)
  80. break
  81. var file = FileAccess.open("res://scenes/main/process_help.json", FileAccess.READ)
  82. if file:
  83. help_data = JSON.parse_string(file.get_as_text())
  84. new_patch()
  85. check_cdp_location_set()
  86. func new_patch():
  87. #clear old patch
  88. graph_edit.clear_connections()
  89. for node in graph_edit.get_children():
  90. if node is GraphNode:
  91. node.queue_free()
  92. await get_tree().process_frame # Wait for nodes to actually be removed
  93. graph_edit.scroll_offset = Vector2(0, 0)
  94. #Generate input and output nodes
  95. var effect: GraphNode = Nodes.get_node(NodePath("inputfile")).duplicate()
  96. effect.name = "inputfile"
  97. get_node("GraphEdit").add_child(effect, true)
  98. effect.connect("open_help", Callable(self, "show_help_for_node"))
  99. effect.position_offset = Vector2(20,80)
  100. effect = Nodes.get_node(NodePath("outputfile")).duplicate()
  101. effect.name = "outputfile"
  102. get_node("GraphEdit").add_child(effect, true)
  103. effect.connect("open_help", Callable(self, "show_help_for_node"))
  104. effect.position_offset = Vector2((DisplayServer.screen_get_size().x - 480) / uiscale, 80)
  105. _register_node_movement() #link nodes for tracking position changes for changes tracking
  106. changesmade = false #so it stops trying to save unchanged empty files
  107. Global.infile = "no_file" #resets input to stop processes running with old files
  108. get_window().title = "SoundThread"
  109. link_output()
  110. func link_output():
  111. #links various buttons and function in the input nodes - this is called after they are created so that it still works on new and loading files
  112. for control in get_tree().get_nodes_in_group("outputnode"): #check all items in outputnode group
  113. if control.get_meta("outputfunction") == "deleteintermediate": #link delete intermediate files toggle to script
  114. control.toggled.connect(_toggle_delete)
  115. control.button_pressed = true
  116. elif control.get_meta("outputfunction") == "runprocess": #link runprocess button
  117. control.button_down.connect(_run_process)
  118. elif control.get_meta("outputfunction") == "recycle": #link recycle button
  119. control.button_down.connect(_recycle_outfile)
  120. elif control.get_meta("outputfunction") == "audioplayer": #link output audio player
  121. output_audio_player = control
  122. elif control.get_meta("outputfunction") == "filename":
  123. control.text = "outfile"
  124. outfilename = control
  125. elif control.get_meta("outputfunction") == "reusefolder":
  126. foldertoggle = control
  127. foldertoggle.button_pressed = true
  128. elif control.get_meta("outputfunction") == "openfolder":
  129. control.button_down.connect(_open_output_folder)
  130. for control in get_tree().get_nodes_in_group("inputnode"):
  131. if control.get_meta("inputfunction") == "audioplayer": #link input for recycle function
  132. print("input player found")
  133. input_audio_player = control
  134. func check_user_preferences():
  135. var interface_settings = ConfigHandler.load_interface_settings()
  136. var audio_settings = ConfigHandler.load_audio_settings()
  137. var audio_devices = AudioServer.get_output_device_list()
  138. $Console.always_on_top = interface_settings.console_on_top
  139. if audio_devices.has(audio_settings.device):
  140. AudioServer.set_output_device(audio_settings.device)
  141. else:
  142. $AudioDevicePopup.popup_centered()
  143. match interface_settings.theme:
  144. 0:
  145. RenderingServer.set_default_clear_color(Color("#2f4f4e"))
  146. 1:
  147. RenderingServer.set_default_clear_color(Color("#000807"))
  148. 2:
  149. RenderingServer.set_default_clear_color(Color("#98d4d2"))
  150. 3:
  151. RenderingServer.set_default_clear_color(Color(interface_settings.theme_custom_colour))
  152. func show_cdp_location():
  153. $CdpLocationDialog.show()
  154. func check_cdp_location_set():
  155. #checks if the location has been set and prompts user to set it
  156. var cdpprogs_settings = ConfigHandler.load_cdpprogs_settings()
  157. if cdpprogs_settings.location == "no_location":
  158. $NoLocationPopup.popup_centered()
  159. else:
  160. #if location is set, stores it in a variable
  161. cdpprogs_location = str(cdpprogs_settings.location)
  162. print(cdpprogs_location)
  163. func _on_ok_button_button_down() -> void:
  164. #after user has read dialog on where to find cdp progs this loads the file browser
  165. $NoLocationPopup.hide()
  166. if OS.get_name() == "Windows":
  167. $CdpLocationDialog.current_dir = "C:/"
  168. else:
  169. $CdpLocationDialog.current_dir = OS.get_environment("HOME")
  170. $CdpLocationDialog.show()
  171. func _on_cdp_location_dialog_dir_selected(dir: String) -> void:
  172. #saves default location for cdp programs in config file
  173. ConfigHandler.save_cdpprogs_settings(dir)
  174. cdpprogs_location = dir
  175. func _on_cdp_location_dialog_canceled() -> void:
  176. #cycles around the set location prompt if user cancels the file dialog
  177. check_cdp_location_set()
  178. # Called every frame. 'delta' is the elapsed time since the previous frame.
  179. func _process(delta: float) -> void:
  180. #showmenu()
  181. pass
  182. func _input(event):
  183. if event.is_action_pressed("copy_node"):
  184. copy_selected_nodes()
  185. get_viewport().set_input_as_handled()
  186. elif event.is_action_pressed("paste_node"):
  187. simulate_mouse_click() #hacky fix to stop tooltips getting stuck
  188. await get_tree().process_frame
  189. paste_copied_nodes()
  190. get_viewport().set_input_as_handled()
  191. elif event.is_action_pressed("undo"):
  192. undo_redo.undo()
  193. elif event.is_action_pressed("redo"):
  194. undo_redo.redo()
  195. elif event.is_action_pressed("save"):
  196. if currentfile == "none":
  197. savestate = "saveas"
  198. $SaveDialog.popup_centered()
  199. else:
  200. save_graph_edit(currentfile)
  201. elif event.is_action_pressed("open_explore"):
  202. open_explore()
  203. func show_help_for_node(node_name: String, node_title: String):
  204. #check if there is already a help window open for this node and pop it up instead of making a new one
  205. for child in get_tree().current_scene.get_children():
  206. if child is Window and child.title == "Help - " + node_title:
  207. # Found existing window, bring it to front
  208. if child.is_visible():
  209. child.hide()
  210. child.popup()
  211. else:
  212. child.popup()
  213. return
  214. if help_data.has(node_name):
  215. #looks up the help data from the json and stores it in info
  216. var info = help_data[node_name]
  217. #makes an instance of the help_window scene
  218. var help_window = HelpWindowScene.instantiate()
  219. help_window.title = "Help - " + node_title
  220. help_window.get_node("HelpTitle").text = node_title
  221. var output = ""
  222. output += info.get("short_description", "") + "\n\n"
  223. var parameters = info.get("parameters", {})
  224. #checks if there are parameters and if there are places them in a table
  225. if parameters.size() > 0:
  226. output += "[table=3]\n"
  227. output += "[cell][b]Parameter Name[/b][/cell][cell][b]Description[/b][/cell][cell][b]Automatable[/b][/cell]\n"
  228. for key in parameters.keys(): #scans through all parameters
  229. var param = parameters[key]
  230. var name = param.get("paramname", "")
  231. var desc = param.get("paramdescription", "")
  232. var automatable = param.get("automatable", false)
  233. var autom_text = "[center]✓[/center]" if automatable else "[center]𐄂[/center]" #replaces true and false with ticks and crosses
  234. output += "[cell]%s[/cell][cell]%s[/cell][cell]%s[/cell]\n" % [name, desc, autom_text] #places each param detail into cells of the table
  235. output += "[/table]\n\n" #ends the table
  236. output += "[b]Functionality[/b]\n"
  237. var description_text = info.get("description", "")
  238. output += description_text.strip_edges()
  239. #check if this is a cdp process or a utility and display the cdp process if it is one
  240. var category = info.get("category", "")
  241. if category != "utility":
  242. output += "\n\n[b]CDP Process[/b]\nThis node runs the CDP Process: " + node_name.replace("_", " ")
  243. help_window.get_node("HelpText").bbcode_text = output
  244. help_window.get_node("HelpText").scroll_to_line(0) #scrolls to the first line of the help file just incase
  245. # Add to the current scene tree to show it
  246. get_tree().current_scene.add_child(help_window)
  247. if help_window.content_scale_factor < uiscale:
  248. help_window.size = help_window.size * uiscale
  249. help_window.content_scale_factor = uiscale
  250. help_window.popup()
  251. else:
  252. # If no help available, even though there always should be, show a window saying no help found
  253. var help_window = HelpWindowScene.instance()
  254. help_window.title = "Help - " + node_title
  255. help_window.get_node("HelpTitle").text = node_title
  256. help_window.get_node("HelpText").bbcode_text = "No help found."
  257. get_tree().current_scene.add_child(help_window)
  258. help_window.popup()
  259. func simulate_mouse_click():
  260. #simulates clicking the middle mouse button in order to hide any visible tooltips
  261. var click_pos = get_viewport().get_mouse_position()
  262. var down_event := InputEventMouseButton.new()
  263. down_event.button_index = MOUSE_BUTTON_MIDDLE
  264. down_event.pressed = true
  265. down_event.position = click_pos
  266. Input.parse_input_event(down_event)
  267. var up_event := InputEventMouseButton.new()
  268. up_event.button_index = MOUSE_BUTTON_MIDDLE
  269. up_event.pressed = false
  270. up_event.position = click_pos
  271. Input.parse_input_event(up_event)
  272. func _make_node_from_search_menu(command: String):
  273. #close menu
  274. $SearchMenu.hide()
  275. #Find node with matching name to button and create a version of it in the graph edit
  276. #and position it close to the origin right click to open the menu
  277. var effect: GraphNode = Nodes.get_node(NodePath(command)).duplicate()
  278. effect.name = command
  279. get_node("GraphEdit").add_child(effect, true)
  280. effect.connect("open_help", Callable(self, "show_help_for_node"))
  281. effect.set_position_offset((effect_position + graph_edit.scroll_offset) / graph_edit.zoom) #set node to current mouse position in graph edit
  282. _register_inputs_in_node(effect) #link sliders for changes tracking
  283. _register_node_movement() #link nodes for tracking position changes for changes tracking
  284. changesmade = true
  285. # Remove node with UndoRedo
  286. undo_redo.create_action("Add Node")
  287. undo_redo.add_undo_method(Callable(graph_edit, "remove_child").bind(effect))
  288. undo_redo.add_undo_method(Callable(effect, "queue_free"))
  289. undo_redo.add_undo_method(Callable(self, "_track_changes"))
  290. undo_redo.commit_action()
  291. func _on_button_pressed(button: Button):
  292. #close menu
  293. $mainmenu.hide()
  294. mainmenu_visible = false
  295. #Find node with matching name to button and create a version of it in the graph edit
  296. #and position it close to the origin right click to open the menu
  297. var effect: GraphNode = Nodes.get_node(NodePath(button.name)).duplicate()
  298. effect.name = button.name
  299. get_node("GraphEdit").add_child(effect, true)
  300. effect.connect("open_help", Callable(self, "show_help_for_node"))
  301. effect.set_position_offset((effect_position + graph_edit.scroll_offset) / graph_edit.zoom) #set node to current mouse position in graph edit
  302. _register_inputs_in_node(effect) #link sliders for changes tracking
  303. _register_node_movement() #link nodes for tracking position changes for changes tracking
  304. changesmade = true
  305. # Remove node with UndoRedo
  306. undo_redo.create_action("Add Node")
  307. undo_redo.add_undo_method(Callable(graph_edit, "remove_child").bind(effect))
  308. undo_redo.add_undo_method(Callable(effect, "queue_free"))
  309. undo_redo.add_undo_method(Callable(self, "_track_changes"))
  310. undo_redo.commit_action()
  311. func _on_graph_edit_connection_request(from_node: StringName, from_port: int, to_node: StringName, to_port: int) -> void:
  312. #get_node("GraphEdit").connect_node(from_node, from_port, to_node, to_port)
  313. var graph_edit = get_node("GraphEdit")
  314. var to_graph_node = graph_edit.get_node(NodePath(to_node))
  315. # Get the type of the input port using GraphNode's built-in method
  316. var port_type = to_graph_node.get_input_port_type(to_port)
  317. # If port type is 1 and already has a connection, reject the request
  318. if port_type == 1:
  319. var connections = graph_edit.get_connection_list()
  320. var existing_connections = 0
  321. for conn in connections:
  322. if conn.to_node == to_node and conn.to_port == to_port:
  323. existing_connections += 1
  324. if existing_connections >= 1:
  325. var interface_settings = ConfigHandler.load_interface_settings()
  326. if interface_settings.disable_pvoc_warning == false:
  327. $MultipleConnectionsPopup.popup_centered()
  328. return
  329. # If no conflict, allow the connection
  330. graph_edit.connect_node(from_node, from_port, to_node, to_port)
  331. changesmade = true
  332. func _on_graph_edit_disconnection_request(from_node: StringName, from_port: int, to_node: StringName, to_port: int) -> void:
  333. get_node("GraphEdit").disconnect_node(from_node, from_port, to_node, to_port)
  334. changesmade = true
  335. func _on_graph_edit_node_selected(node: Node) -> void:
  336. selected_nodes[node] = true
  337. func _on_graph_edit_node_deselected(node: Node) -> void:
  338. selected_nodes[node] = false
  339. func _unhandled_key_input(event: InputEvent) -> void:
  340. if event is InputEventKey and event.pressed and not event.echo:
  341. if event.keycode == KEY_BACKSPACE:
  342. _on_graph_edit_delete_nodes_request(PackedStringArray(selected_nodes.keys().filter(func(k): return selected_nodes[k])))
  343. pass
  344. func _on_graph_edit_delete_nodes_request(nodes: Array[StringName]) -> void:
  345. var graph_edit = get_node("GraphEdit")
  346. undo_redo.create_action("Delete Nodes (Undo only)")
  347. for node in selected_nodes.keys():
  348. if selected_nodes[node]:
  349. if node.get_meta("command") == "inputfile" or node.get_meta("command") == "outputfile":
  350. print("can't delete input or output")
  351. else:
  352. # Store duplicate and state for undo
  353. var node_data = node.duplicate()
  354. var position = node.position_offset
  355. # Store all connections for undo
  356. var conns = []
  357. for con in graph_edit.get_connection_list():
  358. if con["to_node"] == node.name or con["from_node"] == node.name:
  359. conns.append(con)
  360. # Delete
  361. remove_connections_to_node(node)
  362. node.queue_free()
  363. changesmade = true
  364. # Register undo restore
  365. undo_redo.add_undo_method(Callable(graph_edit, "add_child").bind(node_data, true))
  366. undo_redo.add_undo_method(Callable(node_data, "set_position_offset").bind(position))
  367. for con in conns:
  368. undo_redo.add_undo_method(Callable(graph_edit, "connect_node").bind(
  369. con["from_node"], con["from_port"],
  370. con["to_node"], con["to_port"]
  371. ))
  372. undo_redo.add_undo_method(Callable(self, "set_node_selected").bind(node_data, true))
  373. undo_redo.add_undo_method(Callable(self, "_track_changes"))
  374. undo_redo.add_undo_method(Callable(self, "_register_inputs_in_node").bind(node_data)) #link sliders for changes tracking
  375. undo_redo.add_undo_method(Callable(self, "_register_node_movement")) # link nodes for changes tracking
  376. # Clear selection
  377. selected_nodes = {}
  378. undo_redo.commit_action()
  379. func set_node_selected(node: Node, selected: bool) -> void:
  380. selected_nodes[node] = selected
  381. #
  382. func remove_connections_to_node(node):
  383. for con in get_node("GraphEdit").get_connection_list():
  384. if con["to_node"] == node.name or con["from_node"] == node.name:
  385. get_node("GraphEdit").disconnect_node(con["from_node"], con["from_port"], con["to_node"], con["to_port"])
  386. changesmade = true
  387. #copy and paste nodes with vertical offset on paste
  388. func copy_selected_nodes():
  389. copied_nodes_data.clear()
  390. copied_connections.clear()
  391. var graph_edit = get_node("GraphEdit")
  392. # Store selected nodes and their slider values
  393. for node in graph_edit.get_children():
  394. # Check if the node is selected and not an 'inputfile' or 'outputfile'
  395. if node is GraphNode and selected_nodes.get(node, false):
  396. if node.get_meta("command") == "inputfile" or node.get_meta("command") == "outputfile":
  397. continue # Skip these nodes
  398. var node_data = {
  399. "name": node.name,
  400. "type": node.get_class(),
  401. "offset": node.position_offset,
  402. "slider_values": {}
  403. }
  404. for child in node.get_children():
  405. if child is HSlider or child is VSlider:
  406. node_data["slider_values"][child.name] = child.value
  407. copied_nodes_data.append(node_data)
  408. # Store connections between selected nodes
  409. for conn in graph_edit.get_connection_list():
  410. var from_ref = graph_edit.get_node_or_null(NodePath(conn["from_node"]))
  411. var to_ref = graph_edit.get_node_or_null(NodePath(conn["to_node"]))
  412. var is_from_selected = from_ref != null and selected_nodes.get(from_ref, false)
  413. var is_to_selected = to_ref != null and selected_nodes.get(to_ref, false)
  414. # Skip if any of the connected nodes are 'inputfile' or 'outputfile'
  415. if (from_ref != null and (from_ref.get_meta("command") == "inputfile" or from_ref.get_meta("command") == "outputfile")) or (to_ref != null and (to_ref.get_meta("command") == "inputfile" or to_ref.get_meta("command") == "outputfile")):
  416. continue
  417. if is_from_selected and is_to_selected:
  418. # Store connection as dictionary
  419. var conn_data = {
  420. "from_node": conn["from_node"],
  421. "from_port": conn["from_port"],
  422. "to_node": conn["to_node"],
  423. "to_port": conn["to_port"]
  424. }
  425. copied_connections.append(conn_data)
  426. func paste_copied_nodes():
  427. if copied_nodes_data.is_empty():
  428. return
  429. var graph_edit = get_node("GraphEdit")
  430. var name_map = {}
  431. var pasted_nodes = []
  432. # Step 1: Find topmost and bottommost Y of copied nodes
  433. var min_y = INF
  434. var max_y = -INF
  435. for node_data in copied_nodes_data:
  436. var y = node_data["offset"].y
  437. min_y = min(min_y, y)
  438. max_y = max(max_y, y)
  439. # Step 2: Decide where to paste the group
  440. var base_y_offset = max_y + 350 # Pasting below the lowest node
  441. # Step 3: Paste nodes, preserving vertical layout
  442. for node_data in copied_nodes_data:
  443. var original_node = graph_edit.get_node_or_null(NodePath(node_data["name"]))
  444. if not original_node:
  445. continue
  446. var new_node = original_node.duplicate()
  447. new_node.name = node_data["name"] + "_copy_" + str(randi() % 10000)
  448. var relative_y = node_data["offset"].y - min_y
  449. new_node.position_offset = Vector2(
  450. node_data["offset"].x,
  451. base_y_offset + relative_y
  452. )
  453. # Restore sliders
  454. for child in new_node.get_children():
  455. if child.name in node_data["slider_values"]:
  456. child.value = node_data["slider_values"][child.name]
  457. graph_edit.add_child(new_node, true)
  458. new_node.connect("open_help", Callable(self, "show_help_for_node"))
  459. _register_inputs_in_node(new_node) #link sliders for changes tracking
  460. _register_node_movement() # link nodes for changes tracking
  461. name_map[node_data["name"]] = new_node.name
  462. pasted_nodes.append(new_node)
  463. # Step 4: Reconnect new nodes
  464. for conn_data in copied_connections:
  465. var new_from = name_map.get(conn_data["from_node"], null)
  466. var new_to = name_map.get(conn_data["to_node"], null)
  467. if new_from and new_to:
  468. graph_edit.connect_node(new_from, conn_data["from_port"], new_to, conn_data["to_port"])
  469. # Step 5: Select pasted nodes
  470. for pasted_node in pasted_nodes:
  471. graph_edit.set_selected(pasted_node)
  472. selected_nodes[pasted_node] = true
  473. changesmade = true
  474. # Remove node with UndoRedo
  475. undo_redo.create_action("Paste Nodes")
  476. for pasted_node in pasted_nodes:
  477. undo_redo.add_undo_method(Callable(graph_edit, "remove_child").bind(pasted_node))
  478. undo_redo.add_undo_method(Callable(pasted_node, "queue_free"))
  479. undo_redo.add_undo_method(Callable(self, "remove_connections_to_node").bind(pasted_node))
  480. undo_redo.add_undo_method(Callable(self, "_track_changes"))
  481. undo_redo.commit_action()
  482. #functions for tracking changes for save state detection
  483. func _register_inputs_in_node(node: Node):
  484. #tracks input to nodes sliders and codeedit to track if patch is saved
  485. # Track Sliders
  486. for slider in node.find_children("*", "HSlider", true, false):
  487. # Create a Callable to the correct method
  488. var callable = Callable(self, "_on_any_slider_changed")
  489. # Check if it's already connected, and connect if not
  490. if not slider.is_connected("value_changed", callable):
  491. slider.connect("value_changed", callable)
  492. for slider in node.find_children("*", "VBoxContainer", true, false):
  493. # Also connect to meta_changed if the slider has that signal
  494. if slider.has_signal("meta_changed"):
  495. var meta_callable = Callable(self, "_on_any_slider_meta_changed")
  496. if not slider.is_connected("meta_changed", meta_callable):
  497. slider.connect("meta_changed", meta_callable)
  498. # Track CodeEdits
  499. for editor in node.find_children("*", "CodeEdit", true, false):
  500. var callable = Callable(self, "_on_any_input_changed")
  501. if not editor.is_connected("text_changed", callable):
  502. editor.connect("text_changed", callable)
  503. func _on_any_slider_meta_changed():
  504. changesmade = true
  505. print("Meta changed in slider")
  506. func _register_node_movement():
  507. for graphnode in graph_edit.get_children():
  508. if graphnode is GraphNode:
  509. var callable = Callable(self, "_on_graphnode_moved")
  510. if not graphnode.is_connected("position_offset_changed", callable):
  511. graphnode.connect("position_offset_changed", callable)
  512. func _on_graphnode_moved():
  513. changesmade = true
  514. func _on_any_slider_changed(value: float) -> void:
  515. changesmade = true
  516. func _on_any_input_changed():
  517. changesmade = true
  518. func _track_changes():
  519. changesmade = true
  520. func _run_process() -> void:
  521. if Global.infile == "no_file":
  522. $NoInputPopup.popup_centered()
  523. else:
  524. if foldertoggle.button_pressed == true and lastoutputfolder != "none":
  525. _on_file_dialog_dir_selected(lastoutputfolder)
  526. else:
  527. $FileDialog.show()
  528. func _on_file_dialog_dir_selected(dir: String) -> void:
  529. lastoutputfolder = dir
  530. console_output.clear()
  531. if $Console.is_visible():
  532. $Console.hide()
  533. await get_tree().process_frame # Wait a frame to allow hide to complete
  534. $Console.popup_centered()
  535. else:
  536. $Console.popup_centered()
  537. await get_tree().process_frame
  538. log_console("Generating processing queue", true)
  539. await get_tree().process_frame
  540. #get the current time in hh-mm-ss format as default : causes file name issues
  541. var time_dict = Time.get_time_dict_from_system()
  542. # Pad with zeros to ensure two digits for hour, minute, second
  543. var hour = str(time_dict.hour).pad_zeros(2)
  544. var minute = str(time_dict.minute).pad_zeros(2)
  545. var second = str(time_dict.second).pad_zeros(2)
  546. var time_str = hour + "-" + minute + "-" + second
  547. Global.outfile = dir + "/" + outfilename.text.get_basename() + "_" + Time.get_date_string_from_system() + "_" + time_str
  548. log_console("Output directory and file name(s):" + Global.outfile, true)
  549. await get_tree().process_frame
  550. run_thread_with_branches()
  551. func run_thread_with_branches():
  552. # Detect platform: Determine if the OS is Windows
  553. var is_windows := OS.get_name() == "Windows"
  554. # Choose appropriate commands based on OS
  555. var delete_cmd = "del" if is_windows else "rm"
  556. var rename_cmd = "ren" if is_windows else "mv"
  557. var path_sep := "/" # Always use forward slash for paths
  558. # Get all node connections in the GraphEdit
  559. var connections = graph_edit.get_connection_list()
  560. # Prepare data structures for graph traversal
  561. var graph = {} # forward adjacency list
  562. var reverse_graph = {} # reverse adjacency list (for input lookup)
  563. var indegree = {} # used for topological sort
  564. var all_nodes = {} # map of node name -> GraphNode reference
  565. log_console("Mapping thread.", true)
  566. await get_tree().process_frame # Let UI update
  567. #Step 0: check thread is valid
  568. var is_valid = path_exists_through_all_nodes()
  569. if is_valid == false:
  570. log_console("[color=#9c2828][b]Error: Valid Thread not found[/b][/color]", true)
  571. log_console("Threads must contain at least one processing node and a valid path from the Input File to the Output File.", true)
  572. await get_tree().process_frame # Let UI update
  573. return
  574. else:
  575. log_console("[color=#638382][b]Valid Thread found[/b][/color]", true)
  576. await get_tree().process_frame # Let UI update
  577. # Step 1: Gather nodes from the GraphEdit
  578. for child in graph_edit.get_children():
  579. if child is GraphNode:
  580. var name = str(child.name)
  581. all_nodes[name] = child
  582. if not child.has_meta("utility"):
  583. graph[name] = []
  584. reverse_graph[name] = []
  585. indegree[name] = 0 # Start with zero incoming edges
  586. # Step 2: Build graph relationships from connections
  587. for conn in connections:
  588. var from = str(conn["from_node"])
  589. var to = str(conn["to_node"])
  590. if graph.has(from) and graph.has(to):
  591. graph[from].append(to)
  592. reverse_graph[to].append(from)
  593. indegree[to] += 1 # Count incoming edges
  594. # Step 3: Topological sort to get execution order
  595. var sorted = [] # Sorted list of node names
  596. var queue = [] # Queue of nodes with 0 indegree
  597. for node in graph.keys():
  598. if indegree[node] == 0:
  599. queue.append(node)
  600. while not queue.is_empty():
  601. var current = queue.pop_front()
  602. sorted.append(current)
  603. for neighbor in graph[current]:
  604. indegree[neighbor] -= 1
  605. if indegree[neighbor] == 0:
  606. queue.append(neighbor)
  607. # If not all nodes were processed, there's a cycle
  608. if sorted.size() != graph.size():
  609. log_console("[color=#9c2828][b]Error: Thread not valid[/b][/color]", true)
  610. log_console("Threads cannot contain loops.", true)
  611. return
  612. # Step 4: Start processing audio
  613. var batch_lines = [] # Holds all batch file commands
  614. var intermediate_files = [] # Files to delete later
  615. var breakfiles = [] #breakfiles to delete later
  616. # Dictionary to keep track of each node's output file
  617. var output_files = {}
  618. var process_count = 0
  619. # Start with the original input file
  620. var starting_infile = Global.infile
  621. #If trim is enabled trim input audio
  622. if Global.trim_infile == true:
  623. run_command(cdpprogs_location + "/sfedit", ["cut", "1", starting_infile, "%s_trimmed.wav" % Global.outfile, str(Global.infile_start), str(Global.infile_stop)])
  624. starting_infile = Global.outfile + "_trimmed.wav"
  625. # Mark trimmed file for cleanup if needed
  626. if delete_intermediate_outputs:
  627. intermediate_files.append(Global.outfile + "_trimmed.wav")
  628. var current_infile = starting_infile
  629. # Iterate over the processing nodes in topological order
  630. for node_name in sorted:
  631. var node = all_nodes[node_name]
  632. # Find upstream nodes connected to the current node
  633. var inputs = reverse_graph[node_name]
  634. var input_files = []
  635. for input_node in inputs:
  636. input_files.append(output_files[input_node])
  637. # Merge inputs if this node has more than one input
  638. if input_files.size() > 1:
  639. # Prepare final merge output file name
  640. var runmerge = merge_many_files(process_count, input_files)
  641. var merge_output = runmerge[0]
  642. var converted_files = runmerge[1]
  643. # Track the output and intermediate files
  644. current_infile = merge_output
  645. if delete_intermediate_outputs:
  646. intermediate_files.append(merge_output)
  647. for f in converted_files:
  648. intermediate_files.append(f)
  649. # If only one input, use that
  650. elif input_files.size() == 1:
  651. current_infile = input_files[0]
  652. ## If no input, use the original input file
  653. else:
  654. current_infile = starting_infile
  655. # Build the command for the current node's audio processing
  656. var slider_data = _get_slider_values_ordered(node)
  657. if node.get_slot_type_right(0) == 1: #detect if process outputs pvoc data
  658. if typeof(current_infile) == TYPE_ARRAY:
  659. #check if infile is an array meaning that the last pvoc process was run in dual mono mode
  660. # Process left and right seperately
  661. var pvoc_stereo_files = []
  662. for infile in current_infile:
  663. var makeprocess = make_process(node, process_count, infile, slider_data)
  664. # run the command
  665. run_command(makeprocess[0], makeprocess[3])
  666. await get_tree().process_frame
  667. var output_file = makeprocess[1]
  668. pvoc_stereo_files.append(output_file)
  669. # Mark file for cleanup if needed
  670. if delete_intermediate_outputs:
  671. for file in makeprocess[2]:
  672. breakfiles.append(file)
  673. intermediate_files.append(output_file)
  674. process_count += 1
  675. output_files[node_name] = pvoc_stereo_files
  676. else:
  677. var input_stereo = is_stereo(current_infile)
  678. if input_stereo == true:
  679. #audio file is stereo and needs to be split for pvoc processing
  680. var pvoc_stereo_files = []
  681. ##Split stereo to c1/c2
  682. run_command(cdpprogs_location + "/housekeep",["chans", "2", current_infile])
  683. # Process left and right seperately
  684. for channel in ["c1", "c2"]:
  685. var dual_mono_file = current_infile.get_basename() + "_%s.wav" % channel
  686. var makeprocess = make_process(node, process_count, dual_mono_file, slider_data)
  687. # run the command
  688. run_command(makeprocess[0], makeprocess[3])
  689. await get_tree().process_frame
  690. var output_file = makeprocess[1]
  691. pvoc_stereo_files.append(output_file)
  692. # Mark file for cleanup if needed
  693. if delete_intermediate_outputs:
  694. for file in makeprocess[2]:
  695. breakfiles.append(file)
  696. intermediate_files.append(output_file)
  697. #Delete c1 and c2 because they can be in the wrong folder and if the same infile is used more than once
  698. #with this stereo process CDP will throw errors in the console even though its fine
  699. if is_windows:
  700. dual_mono_file = dual_mono_file.replace("/", "\\")
  701. run_command(delete_cmd, [dual_mono_file])
  702. process_count += 1
  703. # Store output file path for this node
  704. output_files[node_name] = pvoc_stereo_files
  705. else:
  706. #input file is mono run through process
  707. var makeprocess = make_process(node, process_count, current_infile, slider_data)
  708. # run the command
  709. run_command(makeprocess[0], makeprocess[3])
  710. await get_tree().process_frame
  711. var output_file = makeprocess[1]
  712. # Store output file path for this node
  713. output_files[node_name] = output_file
  714. # Mark file for cleanup if needed
  715. if delete_intermediate_outputs:
  716. for file in makeprocess[2]:
  717. breakfiles.append(file)
  718. intermediate_files.append(output_file)
  719. # Increase the process step count
  720. process_count += 1
  721. else:
  722. #Process outputs audio
  723. #check if this is the last pvoc process in a stereo processing chain
  724. if node.get_meta("command") == "pvoc_synth" and typeof(current_infile) == TYPE_ARRAY:
  725. #check if infile is an array meaning that the last pvoc process was run in dual mono mode
  726. # Process left and right seperately
  727. var pvoc_stereo_files = []
  728. for infile in current_infile:
  729. var makeprocess = make_process(node, process_count, infile, slider_data)
  730. # run the command
  731. run_command(makeprocess[0], makeprocess[3])
  732. await get_tree().process_frame
  733. var output_file = makeprocess[1]
  734. pvoc_stereo_files.append(output_file)
  735. # Mark file for cleanup if needed
  736. if delete_intermediate_outputs:
  737. for file in makeprocess[2]:
  738. breakfiles.append(file)
  739. intermediate_files.append(output_file)
  740. process_count += 1
  741. #interleave left and right
  742. var output_file = Global.outfile.get_basename() + str(process_count) + "_interleaved.wav"
  743. run_command(cdpprogs_location + "/submix", ["interleave", pvoc_stereo_files[0], pvoc_stereo_files[1], output_file])
  744. # Store output file path for this node
  745. output_files[node_name] = output_file
  746. # Mark file for cleanup if needed
  747. if delete_intermediate_outputs:
  748. intermediate_files.append(output_file)
  749. else:
  750. #Detect if input file is mono or stereo
  751. var input_stereo = is_stereo(current_infile)
  752. if input_stereo == true:
  753. if node.get_meta("stereo_input") == true: #audio file is stereo and process is stereo, run file through process
  754. var makeprocess = make_process(node, process_count, current_infile, slider_data)
  755. # run the command
  756. run_command(makeprocess[0], makeprocess[3])
  757. await get_tree().process_frame
  758. var output_file = makeprocess[1]
  759. # Store output file path for this node
  760. output_files[node_name] = output_file
  761. # Mark file for cleanup if needed
  762. if delete_intermediate_outputs:
  763. for file in makeprocess[2]:
  764. breakfiles.append(file)
  765. intermediate_files.append(output_file)
  766. else: #audio file is stereo and process is mono, split stereo, process and recombine
  767. ##Split stereo to c1/c2
  768. run_command(cdpprogs_location + "/housekeep",["chans", "2", current_infile])
  769. # Process left and right seperately
  770. var dual_mono_output = []
  771. for channel in ["c1", "c2"]:
  772. var dual_mono_file = current_infile.get_basename() + "_%s.wav" % channel
  773. var makeprocess = make_process(node, process_count, dual_mono_file, slider_data)
  774. # run the command
  775. run_command(makeprocess[0], makeprocess[3])
  776. await get_tree().process_frame
  777. var output_file = makeprocess[1]
  778. dual_mono_output.append(output_file)
  779. # Mark file for cleanup if needed
  780. if delete_intermediate_outputs:
  781. for file in makeprocess[2]:
  782. breakfiles.append(file)
  783. intermediate_files.append(output_file)
  784. #Delete c1 and c2 because they can be in the wrong folder and if the same infile is used more than once
  785. #with this stereo process CDP will throw errors in the console even though its fine
  786. if is_windows:
  787. dual_mono_file = dual_mono_file.replace("/", "\\")
  788. run_command(delete_cmd, [dual_mono_file])
  789. process_count += 1
  790. var output_file = Global.outfile.get_basename() + str(process_count) + "_interleaved.wav"
  791. run_command(cdpprogs_location + "/submix", ["interleave", dual_mono_output[0], dual_mono_output[1], output_file])
  792. # Store output file path for this node
  793. output_files[node_name] = output_file
  794. # Mark file for cleanup if needed
  795. if delete_intermediate_outputs:
  796. intermediate_files.append(output_file)
  797. else: #audio file is mono, run through the process
  798. var makeprocess = make_process(node, process_count, current_infile, slider_data)
  799. # run the command
  800. run_command(makeprocess[0], makeprocess[3])
  801. await get_tree().process_frame
  802. var output_file = makeprocess[1]
  803. # Store output file path for this node
  804. output_files[node_name] = output_file
  805. # Mark file for cleanup if needed
  806. if delete_intermediate_outputs:
  807. for file in makeprocess[2]:
  808. breakfiles.append(file)
  809. intermediate_files.append(output_file)
  810. # Increase the process step count
  811. process_count += 1
  812. # FINAL OUTPUT STAGE
  813. # Collect all nodes that are connected to the outputfile node
  814. var output_inputs := []
  815. for conn in connections:
  816. var to_node = str(conn["to_node"])
  817. if all_nodes.has(to_node) and all_nodes[to_node].get_meta("command") == "outputfile":
  818. output_inputs.append(str(conn["from_node"]))
  819. # List to hold the final output files to be merged (if needed)
  820. var final_outputs := []
  821. for node_name in output_inputs:
  822. if output_files.has(node_name):
  823. final_outputs.append(output_files[node_name])
  824. # If multiple outputs go to the outputfile node, merge them
  825. if final_outputs.size() > 1:
  826. var runmerge = merge_many_files(process_count, final_outputs)
  827. final_output_dir = runmerge[0]
  828. var converted_files = runmerge[1]
  829. if delete_intermediate_outputs:
  830. for f in converted_files:
  831. intermediate_files.append(f)
  832. # Only one output, no merge needed
  833. elif final_outputs.size() == 1:
  834. var single_output = final_outputs[0]
  835. final_output_dir = single_output
  836. intermediate_files.erase(single_output)
  837. # CLEANUP: Delete intermediate files after processing and rename final output
  838. log_console("Cleaning up intermediate files.", true)
  839. for file_path in intermediate_files:
  840. # Adjust file path format for Windows if needed
  841. var fixed_path = file_path
  842. if is_windows:
  843. fixed_path = fixed_path.replace("/", "\\")
  844. run_command(delete_cmd, [fixed_path])
  845. await get_tree().process_frame
  846. #delete break files
  847. for file_path in breakfiles:
  848. # Adjust file path format for Windows if needed
  849. var fixed_path = file_path
  850. if is_windows:
  851. fixed_path = fixed_path.replace("/", "\\")
  852. run_command(delete_cmd, [fixed_path])
  853. await get_tree().process_frame
  854. var final_filename = "%s.wav" % Global.outfile
  855. var final_output_dir_fixed_path = final_output_dir
  856. if is_windows:
  857. final_output_dir_fixed_path = final_output_dir_fixed_path.replace("/", "\\")
  858. run_command(rename_cmd, [final_output_dir_fixed_path, final_filename.get_file()])
  859. else:
  860. run_command(rename_cmd, [final_output_dir_fixed_path, "%s.wav" % Global.outfile])
  861. final_output_dir = Global.outfile + ".wav"
  862. output_audio_player.play_outfile(final_output_dir)
  863. outfile = final_output_dir
  864. var interface_settings = ConfigHandler.load_interface_settings() #checks if close console is enabled and closes console on a success
  865. if interface_settings.auto_close_console and process_successful == true:
  866. $Console.hide()
  867. func is_stereo(file: String) -> bool:
  868. var output = run_command(cdpprogs_location + "/sfprops", ["-c", file])
  869. output = int(output[0].strip_edges()) #convert output from cmd to clean int
  870. if output == 1:
  871. return false
  872. elif output == 2:
  873. return true
  874. elif output == 1026: #ignore pvoc .ana files
  875. return false
  876. else:
  877. log_console("[color=#9c2828]Error: Only mono and stereo files are supported[/color]", true)
  878. return false
  879. func merge_many_files(process_count: int, input_files: Array) -> Array:
  880. var merge_output = "%s_merge_%d.wav" % [Global.outfile.get_basename(), process_count]
  881. var converted_files := [] # Track any mono->stereo converted files
  882. var inputs_to_merge := [] # Files to be used in the final merge
  883. var mono_files := []
  884. var stereo_files := []
  885. # STEP 1: Check each file's channel count
  886. for f in input_files:
  887. var stereo = is_stereo(f)
  888. if stereo == false:
  889. mono_files.append(f)
  890. elif stereo == true:
  891. stereo_files.append(f)
  892. # STEP 2: Convert mono to stereo if there is a mix
  893. if mono_files.size() > 0 and stereo_files.size() > 0:
  894. for mono_file in mono_files:
  895. var stereo_file = "%s_stereo.wav" % mono_file.get_basename()
  896. run_command(cdpprogs_location + "/submix", ["interleave", mono_file, mono_file, stereo_file])
  897. if process_successful == false:
  898. log_console("Failed to interleave mono file: %s" % mono_file, true)
  899. else:
  900. converted_files.append(stereo_file)
  901. inputs_to_merge.append(stereo_file)
  902. # Add existing stereo files
  903. inputs_to_merge += stereo_files
  904. else:
  905. # All mono or all stereo — use input_files directly
  906. inputs_to_merge = input_files.duplicate()
  907. # STEP 3: Merge all input files (converted or original)
  908. var quoted_inputs := []
  909. for f in inputs_to_merge:
  910. quoted_inputs.append(f)
  911. quoted_inputs.insert(0, "mergemany")
  912. quoted_inputs.append(merge_output)
  913. run_command(cdpprogs_location + "/submix", quoted_inputs)
  914. if process_successful == false:
  915. log_console("Failed to to merge files to" + merge_output, true)
  916. return [merge_output, converted_files]
  917. func _get_slider_values_ordered(node: Node) -> Array:
  918. var results := []
  919. for child in node.get_children():
  920. if child is Range:
  921. var flag = child.get_meta("flag") if child.has_meta("flag") else ""
  922. var time
  923. var brk_data = []
  924. var min_slider = child.min_value
  925. var max_slider = child.max_value
  926. if child.has_meta("time"):
  927. time = child.get_meta("time")
  928. else:
  929. time = false
  930. if child.has_meta("brk_data"):
  931. brk_data = child.get_meta("brk_data")
  932. results.append([flag, child.value, time, brk_data, min_slider, max_slider])
  933. elif child.get_child_count() > 0:
  934. var nested := _get_slider_values_ordered(child)
  935. results.append_array(nested)
  936. return results
  937. func make_process(node: Node, process_count: int, current_infile: String, slider_data: Array) -> Array:
  938. # Determine output extension: .wav or .ana based on the node's slot type
  939. var extension = ".wav" if node.get_slot_type_right(0) == 0 else ".ana"
  940. # Construct output filename for this step
  941. var output_file = "%s_%d%s" % [Global.outfile.get_basename(), process_count, extension]
  942. # Get the command name from metadata or default to node name
  943. var command_name = str(node.get_meta("command"))
  944. #command_name = command_name.replace("_", " ")
  945. command_name = command_name.split("_", true, 1)
  946. print(command_name)
  947. var command = "%s/%s" %[cdpprogs_location, command_name[0]]
  948. print(command)
  949. var args = command_name[1].split("_", true, 1)
  950. print(args)
  951. args.append(current_infile)
  952. args.append(output_file)
  953. print(args)
  954. # Start building the command line windows
  955. var line = "%s/%s \"%s\" \"%s\" " % [cdpprogs_location, command_name, current_infile, output_file]
  956. #mac
  957. var cleanup = []
  958. # Append parameter values from the sliders, include flags if present
  959. var slider_count = 0
  960. for entry in slider_data:
  961. var flag = entry[0]
  962. var value = entry[1]
  963. var time = entry[2] #checks if slider is a time percentage slider
  964. var brk_data = entry[3]
  965. var min_slider = entry[4]
  966. var max_slider = entry[5]
  967. if brk_data.size() > 0: #if breakpoint data is present on slider
  968. #Sort all points by time
  969. var sorted_brk_data = []
  970. sorted_brk_data = brk_data.duplicate()
  971. sorted_brk_data.sort_custom(sort_points)
  972. var calculated_brk = []
  973. #get length of input file in seconds
  974. var infile_length = run_command(cdpprogs_location + "/sfprops", ["-d", current_infile])
  975. infile_length = float(infile_length[0].strip_edges())
  976. #scale values from automation window to the right length for file and correct slider values
  977. #need to check how time is handled in all files that accept it, zigzag is x = outfile position, y = infile position
  978. #if time == true:
  979. #for point in sorted_brk_data:
  980. #var new_x = infile_length * (point.x / 700) #time
  981. #var new_y = infile_length * (remap(point.y, 255, 0, min_slider, max_slider) / 100) #slider value scaled as a percentage of infile time
  982. #calculated_brk.append(Vector2(new_x, new_y))
  983. #else:
  984. for i in range(sorted_brk_data.size()):
  985. var point = sorted_brk_data[i]
  986. var new_x = infile_length * (point.x / 700) #time
  987. if i == sorted_brk_data.size() - 1: #check if this is last automation point
  988. new_x = infile_length + 0.1 # force last point's x to infile_length + 100ms to make sure the file is defo over
  989. var new_y = remap(point.y, 255, 0, min_slider, max_slider) #slider value
  990. calculated_brk.append(Vector2(new_x, new_y))
  991. #make text file
  992. var brk_file_path = output_file.get_basename() + "_" + str(slider_count) + ".txt"
  993. write_breakfile(calculated_brk, brk_file_path)
  994. #append text file in place of value
  995. line += ("\"%s\" " % brk_file_path)
  996. args.append(brk_file_path)
  997. cleanup.append(brk_file_path)
  998. else:
  999. if time == true:
  1000. var infile_length = run_command(cdpprogs_location + "/sfprops", ["-d", current_infile])
  1001. infile_length = float(infile_length[0].strip_edges())
  1002. value = infile_length * (value / 100) #calculate percentage time of the input file
  1003. line += ("%s%.2f " % [flag, value]) if flag.begins_with("-") else ("%.2f " % value)
  1004. args.append(("%s%.2f " % [flag, value]) if flag.begins_with("-") else ("%.2f " % value))
  1005. slider_count += 1
  1006. return [command, output_file, cleanup, args]
  1007. #return [line.strip_edges(), output_file, cleanup]
  1008. func sort_points(a, b):
  1009. return a.x < b.x
  1010. func write_breakfile(points: Array, path: String):
  1011. var file = FileAccess.open(path, FileAccess.WRITE)
  1012. if file:
  1013. for point in points:
  1014. var line = str(point.x) + " " + str(point.y) + "\n"
  1015. file.store_string(line)
  1016. file.close()
  1017. else:
  1018. print("Failed to open file for writing.")
  1019. func run_command(command: String, args: Array) -> Array:
  1020. var is_windows = OS.get_name() == "Windows"
  1021. var output: Array = []
  1022. var error: Array = []
  1023. var exit_code := 0
  1024. if is_windows:
  1025. #exit_code = OS.execute("cmd.exe", ["/C", command], output, true, false)
  1026. args.insert(0, command)
  1027. args.insert(0, "/C")
  1028. exit_code = OS.execute("cmd.exe", args, output, true, false)
  1029. else:
  1030. exit_code = OS.execute(command, args, output, true, false)
  1031. var output_str := ""
  1032. for item in output:
  1033. output_str += item + "\n"
  1034. var error_str := ""
  1035. for item in error:
  1036. error_str += item + "\n"
  1037. if is_windows:
  1038. args.remove_at(0)
  1039. console_output.append_text(" ".join(args) + "\n")
  1040. else:
  1041. console_output.append_text(command + " " + " ".join(args) + "\n")
  1042. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1043. if exit_code == 0:
  1044. if output_str.contains("ERROR:"): #checks if CDP reported an error but passed exit code 0 anyway
  1045. console_output.append_text("[color=#9c2828][b]Processes failed[/b][/color]\n")
  1046. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1047. console_output.append_text(output_str + "\n")
  1048. process_successful = false
  1049. else:
  1050. console_output.append_text("[color=#638382]Processes ran successfully[/color]\n")
  1051. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1052. console_output.append_text(output_str + "\n")
  1053. process_successful = true
  1054. else:
  1055. console_output.append_text("[color=#9c2828][b]Processes failed with exit code: %d[/b][/color]\n" % exit_code)
  1056. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1057. console_output.append_text(output_str + "\n")
  1058. console_output.append_text(error_str + "\n")
  1059. if output_str.contains("as an internal or external command"): #check for cdprogs location error on windows
  1060. console_output.append_text("[color=#9c2828][b]Please make sure your cdprogs folder is set to the correct location in the Settings menu. The default location is C:\\CDPR8\\_cdp\\_cdprogs[/b][/color]\n")
  1061. if output_str.contains("command not found"): #check for cdprogs location error on unix systems
  1062. console_output.append_text("[color=#9c2828][b]Please make sure your cdprogs folder is set to the correct location in the Settings menu. The default location is ~/cdpr8/_cdp/_cdprogs[/b][/color]\n")
  1063. process_successful = false
  1064. return output
  1065. func path_exists_through_all_nodes() -> bool:
  1066. var all_nodes = {}
  1067. var graph = {}
  1068. var input_node_name = ""
  1069. var output_node_name = ""
  1070. # Gather all relevant nodes
  1071. for child in graph_edit.get_children():
  1072. if child is GraphNode:
  1073. var name = str(child.name)
  1074. all_nodes[name] = child
  1075. var command = child.get_meta("command")
  1076. if command == "inputfile":
  1077. input_node_name = name
  1078. elif command == "outputfile":
  1079. output_node_name = name
  1080. # Skip utility nodes, include others
  1081. if command in ["inputfile", "outputfile"] or not child.has_meta("utility"):
  1082. graph[name] = []
  1083. # Ensure both input and output were found
  1084. if input_node_name == "" or output_node_name == "":
  1085. print("Input or output node not found!")
  1086. return false
  1087. # Add edges to graph from the connection list
  1088. var connection_list = graph_edit.get_connection_list()
  1089. for conn in connection_list:
  1090. var from = str(conn["from_node"])
  1091. var to = str(conn["to_node"])
  1092. if graph.has(from):
  1093. graph[from].append(to)
  1094. # BFS traversal to check path and depth
  1095. var visited = {}
  1096. var queue = [ { "node": input_node_name, "depth": 0 } ]
  1097. var has_intermediate = false
  1098. while queue.size() > 0:
  1099. var current = queue.pop_front()
  1100. var current_node = current["node"]
  1101. var depth = current["depth"]
  1102. if current_node in visited:
  1103. continue
  1104. visited[current_node] = true
  1105. if current_node == output_node_name and depth >= 2:
  1106. has_intermediate = true
  1107. if graph.has(current_node):
  1108. for neighbor in graph[current_node]:
  1109. queue.append({ "node": neighbor, "depth": depth + 1 })
  1110. return has_intermediate
  1111. func _toggle_delete(toggled_on: bool):
  1112. delete_intermediate_outputs = toggled_on
  1113. print(toggled_on)
  1114. func _on_console_close_requested() -> void:
  1115. $Console.hide()
  1116. func log_console(text: String, update: bool) -> void:
  1117. console_output.append_text(text + "\n \n")
  1118. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1119. if update == true:
  1120. await get_tree().process_frame # Optional: ensure UI updates
  1121. func _on_console_open_folder_button_down() -> void:
  1122. $Console.hide()
  1123. OS.shell_open(Global.outfile.get_base_dir())
  1124. func _on_ok_button_2_button_down() -> void:
  1125. $NoInputPopup.hide()
  1126. func _on_ok_button_3_button_down() -> void:
  1127. $MultipleConnectionsPopup.hide()
  1128. func _on_settings_button_index_pressed(index: int) -> void:
  1129. var interface_settings = ConfigHandler.load_interface_settings()
  1130. match index:
  1131. 0:
  1132. $Settings.popup_centered()
  1133. 1:
  1134. $AudioSettings.popup_centered()
  1135. 2:
  1136. if $Console.is_visible():
  1137. $Console.hide()
  1138. await get_tree().process_frame # Wait a frame to allow hide to complete
  1139. $Console.popup_centered()
  1140. else:
  1141. $Console.popup_centered()
  1142. func _on_file_button_index_pressed(index: int) -> void:
  1143. match index:
  1144. 0:
  1145. if changesmade == true:
  1146. savestate = "newfile"
  1147. $SaveChangesPopup.popup_centered()
  1148. else:
  1149. new_patch()
  1150. currentfile = "none" #reset current file to none for save tracking
  1151. 1:
  1152. if currentfile == "none":
  1153. savestate = "saveas"
  1154. $SaveDialog.popup_centered()
  1155. else:
  1156. save_graph_edit(currentfile)
  1157. 2:
  1158. savestate = "saveas"
  1159. $SaveDialog.popup_centered()
  1160. 3:
  1161. if changesmade == true:
  1162. savestate = "load"
  1163. $SaveChangesPopup.popup_centered()
  1164. else:
  1165. $LoadDialog.popup_centered()
  1166. func save_graph_edit(path: String):
  1167. var file = FileAccess.open(path, FileAccess.WRITE)
  1168. if file == null:
  1169. print("Failed to open file for saving")
  1170. return
  1171. var node_data_list = []
  1172. var connection_data_list = []
  1173. var node_id_map = {} # Map node name to numeric ID
  1174. var node_id = 1
  1175. # Assign each node a unique numeric ID and gather node data
  1176. for node in graph_edit.get_children():
  1177. if node is GraphNode:
  1178. node_id_map[node.name] = node_id
  1179. var offset = node.position_offset
  1180. var node_data = {
  1181. "id": node_id,
  1182. "name": node.name,
  1183. "command": node.get_meta("command"),
  1184. "offset": { "x": offset.x, "y": offset.y },
  1185. "slider_values": {},
  1186. "notes": {}
  1187. }
  1188. # Save slider values and metadata
  1189. for child in node.find_children("*", "Slider", true, false):
  1190. var relative_path = node.get_path_to(child)
  1191. var path_str = str(relative_path)
  1192. node_data["slider_values"][path_str] = {
  1193. "value": child.value,
  1194. "editable": child.editable,
  1195. "meta": {}
  1196. }
  1197. for key in child.get_meta_list():
  1198. node_data["slider_values"][path_str]["meta"][str(key)] = child.get_meta(key)
  1199. # Save notes from CodeEdit children
  1200. for child in node.find_children("*", "CodeEdit", true, false):
  1201. node_data["notes"][child.name] = child.text
  1202. node_data_list.append(node_data)
  1203. node_id += 1
  1204. # Save connections using node IDs instead of names
  1205. for conn in graph_edit.get_connection_list():
  1206. # Map from_node and to_node names to IDs
  1207. var from_id = node_id_map.get(conn["from_node"], null)
  1208. var to_id = node_id_map.get(conn["to_node"], null)
  1209. if from_id != null and to_id != null:
  1210. connection_data_list.append({
  1211. "from_node_id": from_id,
  1212. "from_port": conn["from_port"],
  1213. "to_node_id": to_id,
  1214. "to_port": conn["to_port"]
  1215. })
  1216. else:
  1217. print("Warning: Connection references unknown node(s). Skipping connection.")
  1218. var graph_data = {
  1219. "nodes": node_data_list,
  1220. "connections": connection_data_list
  1221. }
  1222. var json = JSON.new()
  1223. var json_string = json.stringify(graph_data, "\t")
  1224. file.store_string(json_string)
  1225. file.close()
  1226. print("Graph saved.")
  1227. changesmade = false
  1228. get_window().title = "SoundThread - " + path.get_file().trim_suffix(".thd")
  1229. func load_graph_edit(path: String):
  1230. var file = FileAccess.open(path, FileAccess.READ)
  1231. if file == null:
  1232. print("Failed to open file for loading")
  1233. return
  1234. var json_text = file.get_as_text()
  1235. file.close()
  1236. var json = JSON.new()
  1237. if json.parse(json_text) != OK:
  1238. print("Error parsing JSON")
  1239. return
  1240. var graph_data = json.get_data()
  1241. graph_edit.clear_connections()
  1242. # Remove all existing GraphNodes from graph_edit
  1243. for node in graph_edit.get_children():
  1244. if node is GraphNode:
  1245. node.queue_free()
  1246. await get_tree().process_frame # Ensure nodes are freed before adding new ones
  1247. var id_to_node = {} # Map node IDs to new node instances
  1248. # Recreate nodes and store them by ID
  1249. for node_data in graph_data["nodes"]:
  1250. var command_name = node_data.get("command", "")
  1251. var template = Nodes.get_node_or_null(command_name)
  1252. if not template:
  1253. print("Template not found for command:", command_name)
  1254. continue
  1255. var new_node: GraphNode = template.duplicate()
  1256. new_node.name = node_data["name"]
  1257. new_node.position_offset = Vector2(node_data["offset"]["x"], node_data["offset"]["y"])
  1258. new_node.set_meta("command", command_name)
  1259. graph_edit.add_child(new_node)
  1260. new_node.connect("open_help", Callable(self, "show_help_for_node"))
  1261. _register_node_movement() # Track node movement changes
  1262. id_to_node[node_data["id"]] = new_node
  1263. # Restore sliders
  1264. for slider_path_str in node_data["slider_values"]:
  1265. var slider = new_node.get_node_or_null(slider_path_str)
  1266. if slider and (slider is HSlider or slider is VSlider):
  1267. var slider_info = node_data["slider_values"][slider_path_str]
  1268. if typeof(slider_info) == TYPE_DICTIONARY:
  1269. slider.value = slider_info.get("value", slider.value)
  1270. if slider_info.has("editable"):
  1271. slider.editable = slider_info["editable"]
  1272. if slider_info.has("meta"):
  1273. for key in slider_info["meta"]:
  1274. var value = slider_info["meta"][key]
  1275. if key == "brk_data" and typeof(value) == TYPE_ARRAY:
  1276. var new_array: Array = []
  1277. for item in value:
  1278. if typeof(item) == TYPE_STRING:
  1279. var numbers: PackedStringArray = item.strip_edges().trim_prefix("(").trim_suffix(")").split(",")
  1280. if numbers.size() == 2:
  1281. var x = float(numbers[0])
  1282. var y = float(numbers[1])
  1283. new_array.append(Vector2(x, y))
  1284. value = new_array
  1285. slider.set_meta(key, value)
  1286. else:
  1287. slider.value = slider_info
  1288. # Restore notes
  1289. for codeedit_name in node_data["notes"]:
  1290. var codeedit = new_node.find_child(codeedit_name, true, false)
  1291. if codeedit and (codeedit is CodeEdit):
  1292. codeedit.text = node_data["notes"][codeedit_name]
  1293. _register_inputs_in_node(new_node) # Track slider changes
  1294. # Recreate connections by looking up nodes by ID
  1295. for conn in graph_data["connections"]:
  1296. var from_node = id_to_node.get(conn["from_node_id"], null)
  1297. var to_node = id_to_node.get(conn["to_node_id"], null)
  1298. if from_node != null and to_node != null:
  1299. graph_edit.connect_node(
  1300. from_node.name, conn["from_port"],
  1301. to_node.name, conn["to_port"]
  1302. )
  1303. else:
  1304. print("Warning: Connection references unknown node ID(s). Skipping connection.")
  1305. link_output()
  1306. print("Graph loaded.")
  1307. get_window().title = "SoundThread - " + path.get_file().trim_suffix(".thd")
  1308. func _on_save_dialog_file_selected(path: String) -> void:
  1309. save_graph_edit(path) #save file
  1310. #check what the user was trying to do before save and do that action
  1311. if savestate == "newfile":
  1312. new_patch()
  1313. currentfile = "none" #reset current file to none for save tracking
  1314. elif savestate == "load":
  1315. $LoadDialog.popup_centered()
  1316. elif savestate == "helpfile":
  1317. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  1318. load_graph_edit(helpfile)
  1319. elif savestate == "quit":
  1320. await get_tree().create_timer(0.25).timeout #little pause so that it feels like it actually saved even though it did
  1321. get_tree().quit()
  1322. savestate = "none" #reset save state, not really needed but feels good
  1323. func _on_load_dialog_file_selected(path: String) -> void:
  1324. currentfile = path #tracking path here only means "save" only saves patches the user has loaded rather than overwriting help files
  1325. load_graph_edit(path)
  1326. func _on_help_button_index_pressed(index: int) -> void:
  1327. match index:
  1328. 0:
  1329. pass
  1330. 1:
  1331. if changesmade == true:
  1332. savestate = "helpfile"
  1333. helpfile = "res://examples/getting_started.thd"
  1334. $SaveChangesPopup.popup_centered()
  1335. else:
  1336. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  1337. load_graph_edit("res://examples/getting_started.thd")
  1338. 2:
  1339. if changesmade == true:
  1340. savestate = "helpfile"
  1341. helpfile = "res://examples/navigating.thd"
  1342. $SaveChangesPopup.popup_centered()
  1343. else:
  1344. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  1345. load_graph_edit("res://examples/navigating.thd")
  1346. 3:
  1347. if changesmade == true:
  1348. savestate = "helpfile"
  1349. helpfile = "res://examples/building_a_thread.thd"
  1350. $SaveChangesPopup.popup_centered()
  1351. else:
  1352. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  1353. load_graph_edit("res://examples/building_a_thread.thd")
  1354. 4:
  1355. if changesmade == true:
  1356. savestate = "helpfile"
  1357. helpfile = "res://examples/frequency_domain.thd"
  1358. $SaveChangesPopup.popup_centered()
  1359. else:
  1360. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  1361. load_graph_edit("res://examples/frequency_domain.thd")
  1362. 5:
  1363. if changesmade == true:
  1364. savestate = "helpfile"
  1365. helpfile = "res://examples/automation.thd"
  1366. $SaveChangesPopup.popup_centered()
  1367. else:
  1368. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  1369. load_graph_edit("res://examples/automation.thd")
  1370. 6:
  1371. if changesmade == true:
  1372. savestate = "helpfile"
  1373. helpfile = "res://examples/trimming.thd"
  1374. $SaveChangesPopup.popup_centered()
  1375. else:
  1376. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  1377. load_graph_edit("res://examples/trimming.thd")
  1378. 7:
  1379. pass
  1380. 8:
  1381. if changesmade == true:
  1382. savestate = "helpfile"
  1383. helpfile = "res://examples/wetdry.thd"
  1384. $SaveChangesPopup.popup_centered()
  1385. else:
  1386. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  1387. load_graph_edit("res://examples/wetdry.thd")
  1388. 9:
  1389. if changesmade == true:
  1390. savestate = "helpfile"
  1391. helpfile = "res://examples/resonant_filters.thd"
  1392. $SaveChangesPopup.popup_centered()
  1393. else:
  1394. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  1395. load_graph_edit("res://examples/resonant_filters.thd")
  1396. 10:
  1397. pass
  1398. 11:
  1399. OS.shell_open("https://www.composersdesktop.com/docs/html/ccdpndex.htm")
  1400. 12:
  1401. OS.shell_open("https://github.com/j-p-higgins/SoundThread/issues")
  1402. func _recycle_outfile():
  1403. if outfile != "no file":
  1404. input_audio_player.recycle_outfile(outfile)
  1405. func _on_save_changes_button_down() -> void:
  1406. $SaveChangesPopup.hide()
  1407. if currentfile == "none":
  1408. $SaveDialog.show()
  1409. else:
  1410. save_graph_edit(currentfile)
  1411. if savestate == "newfile":
  1412. new_patch()
  1413. currentfile = "none" #reset current file to none for save tracking
  1414. elif savestate == "load":
  1415. $LoadDialog.popup_centered()
  1416. elif savestate == "helpfile":
  1417. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  1418. load_graph_edit(helpfile)
  1419. elif savestate == "quit":
  1420. await get_tree().create_timer(0.25).timeout #little pause so that it feels like it actually saved even though it did
  1421. get_tree().quit()
  1422. savestate = "none"
  1423. func _on_dont_save_changes_button_down() -> void:
  1424. $SaveChangesPopup.hide()
  1425. if savestate == "newfile":
  1426. new_patch()
  1427. currentfile = "none" #reset current file to none for save tracking
  1428. elif savestate == "load":
  1429. $LoadDialog.popup_centered()
  1430. elif savestate == "helpfile":
  1431. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  1432. load_graph_edit(helpfile)
  1433. elif savestate == "quit":
  1434. get_tree().quit()
  1435. savestate = "none"
  1436. func _notification(what):
  1437. if what == NOTIFICATION_WM_CLOSE_REQUEST:
  1438. $Console.hide()
  1439. if changesmade == true:
  1440. savestate = "quit"
  1441. $SaveChangesPopup.popup_centered()
  1442. #$HelpWindow.hide()
  1443. else:
  1444. get_tree().quit() # default behavior
  1445. func _open_output_folder():
  1446. if lastoutputfolder != "none":
  1447. OS.shell_open(lastoutputfolder)
  1448. func _on_rich_text_label_meta_clicked(meta: Variant) -> void:
  1449. print(str(meta))
  1450. OS.shell_open(str(meta))
  1451. func _on_graph_edit_popup_request(at_position: Vector2) -> void:
  1452. effect_position = graph_edit.get_local_mouse_position()
  1453. #get the mouse position in screen coordinates
  1454. var mouse_screen_pos = DisplayServer.mouse_get_position()
  1455. #get the window position in screen coordinates
  1456. var window_screen_pos = get_window().position
  1457. #get the window size relative to its scaling for retina displays
  1458. var window_size = get_window().size * DisplayServer.screen_get_scale()
  1459. #calculate the xy position of the mouse clamped to the size of the window and menu so it doesn't go off the screen
  1460. var clamped_x = clamp(mouse_screen_pos.x, window_screen_pos.x, window_screen_pos.x + window_size.x - $SearchMenu.size.x)
  1461. var clamped_y = clamp(mouse_screen_pos.y, window_screen_pos.y, window_screen_pos.y + window_size.y - (420 * DisplayServer.screen_get_scale()))
  1462. #position and show the menu
  1463. $SearchMenu.position = Vector2(clamped_x, clamped_y)
  1464. $SearchMenu.popup()
  1465. func _on_audio_settings_close_requested() -> void:
  1466. $AudioSettings.hide()
  1467. func _on_open_audio_settings_button_down() -> void:
  1468. $AudioDevicePopup.hide()
  1469. $AudioSettings.popup_centered()
  1470. func _on_audio_device_popup_close_requested() -> void:
  1471. $AudioDevicePopup.hide()
  1472. func _on_mainmenu_close_requested() -> void:
  1473. #closes menu if click is anywhere other than the menu as it is a window with popup set to true
  1474. $mainmenu.hide()
  1475. func open_explore():
  1476. effect_position = graph_edit.get_local_mouse_position()
  1477. #get the mouse position in screen coordinates
  1478. var mouse_screen_pos = DisplayServer.mouse_get_position()
  1479. #get the window position in screen coordinates
  1480. var window_screen_pos = get_window().position
  1481. #get the window size relative to its scaling for retina displays
  1482. var window_size = get_window().size * DisplayServer.screen_get_scale()
  1483. #get the size of the popup menu
  1484. var popup_size = $mainmenu.size
  1485. #calculate the xy position of the mouse clamped to the size of the window and menu so it doesn't go off the screen
  1486. var clamped_x = clamp(mouse_screen_pos.x, window_screen_pos.x, window_screen_pos.x + window_size.x - popup_size.x)
  1487. var clamped_y = clamp(mouse_screen_pos.y, window_screen_pos.y, window_screen_pos.y + window_size.y - popup_size.y)
  1488. #position and show the menu
  1489. $mainmenu.position = Vector2(clamped_x, clamped_y)
  1490. $mainmenu.popup()
  1491. func change_console_settings(toggled: bool):
  1492. $Console.always_on_top = toggled