control.gd 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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 cdpprogs_location #stores the cdp programs location from user prefs for easy access
  6. var delete_intermediate_outputs # tracks state of delete intermediate outputs toggle
  7. @onready var console_output: RichTextLabel = $Console/ConsoleOutput
  8. var undo_redo := UndoRedo.new()
  9. var output_audio_player #tracks the node that is the current output player for linking
  10. var input_audio_player #potentially unused, remove? tracks node that is the current input player for linking
  11. var currentfile = "none" #tracks dir of currently loaded file for saving
  12. var changesmade = false #tracks if user has made changes to the currently loaded save file
  13. var savestate # tracks what the user is trying to do when savechangespopup is called
  14. var helpfile #tracks which help file the user was trying to load when savechangespopup is called
  15. var outfilename #links to the user name for outputfile field
  16. var foldertoggle #links to the reuse folder button
  17. #var lastoutputfolder = "none" #tracks last output folder, this can in future be used to replace global.outfile but i cba right now
  18. var uiscale = 0.0 # tracks the overal ui scale after hidpi adjustment and user offset
  19. var retina_scaling = 1.0 #tracks scaling for retina screens
  20. var use_anyway #used to store the folder selected for cdprogs when it appears the wrong folder is selected but the user wants to use it anyway
  21. var main_theme = preload("res://theme/main_theme.tres") #load the theme
  22. var default_input_node #stores a reference to the input node created on launch to allow auto loading a wav file
  23. var output_folder_label
  24. @onready var fft_size_option_button = $FFTSize
  25. @onready var fft_overlap_option_button = $FFTOverlap
  26. #scripts
  27. var open_help
  28. var run_thread
  29. var save_load
  30. # Called when the node enters the scene tree for the first time.
  31. func _ready() -> void:
  32. $mainmenu.hide()
  33. $NoLocationPopup.hide()
  34. $Console.hide()
  35. $NoInputPopup.hide()
  36. $MultipleConnectionsPopup.hide()
  37. $AudioSettings.hide()
  38. $AudioDevicePopup.hide()
  39. $SearchMenu.hide()
  40. $Settings.hide()
  41. $ProgressWindow.hide()
  42. $WrongFolderPopup.hide()
  43. $SaveChangesPopup.hide()
  44. $SaveDialog.access = FileDialog.ACCESS_FILESYSTEM
  45. $SaveDialog.file_mode = FileDialog.FILE_MODE_SAVE_FILE
  46. $SaveDialog.filters = ["*.thd"]
  47. $LoadDialog.access = FileDialog.ACCESS_FILESYSTEM
  48. $LoadDialog.file_mode = FileDialog.FILE_MODE_OPEN_FILE
  49. $LoadDialog.filters = ["*.thd"]
  50. get_tree().set_auto_accept_quit(false) #disable closing the app with the x and instead handle it internally
  51. load_scripts()
  52. make_signal_connections()
  53. hidpi_adjustment()
  54. check_user_preferences()
  55. new_patch()
  56. await get_tree().process_frame
  57. load_from_filesystem()
  58. check_cdp_location_set()
  59. func load_scripts():
  60. #load and initialise scripts
  61. open_help = preload("res://scenes/main/scripts/open_help.gd").new()
  62. open_help.init(self)
  63. add_child(open_help)
  64. run_thread = preload("res://scenes/main/scripts/run_thread.gd").new()
  65. run_thread.init(self, $ProgressWindow, $ProgressWindow/ProgressLabel, $ProgressWindow/ProgressBar, $GraphEdit, $Console, $Console/ConsoleOutput)
  66. add_child(run_thread)
  67. graph_edit.init(self, $GraphEdit, Callable(open_help, "show_help_for_node"), $MultipleConnectionsPopup)
  68. save_load = preload("res://scenes/main/scripts/save_load.gd").new()
  69. save_load.init(self, $GraphEdit, Callable(open_help, "show_help_for_node"), Callable(graph_edit, "_register_node_movement"), Callable(graph_edit, "_register_inputs_in_node"), Callable(self, "link_output"))
  70. add_child(save_load)
  71. func make_signal_connections():
  72. get_node("SearchMenu").make_node.connect(graph_edit._make_node)
  73. get_node("SearchMenu").swap_node.connect(graph_edit._swap_node)
  74. get_node("SearchMenu").connect_to_clicked_node.connect(graph_edit._connect_to_clicked_node)
  75. get_node("mainmenu").make_node.connect(graph_edit._make_node)
  76. get_node("mainmenu").open_help.connect(open_help.show_help_for_node)
  77. get_node("Settings").open_cdp_location.connect(show_cdp_location)
  78. get_node("Settings").console_on_top.connect(change_console_settings)
  79. get_node("Settings").invert_ui.connect(invert_theme_toggled)
  80. get_node("Settings").swap_zoom_and_move.connect(swap_zoom_and_move)
  81. get_node("Settings").ui_scale_multiplier_changed.connect(scale_ui)
  82. get_window().files_dropped.connect(on_files_dropped)
  83. func hidpi_adjustment():
  84. #checks if display is hidpi and scales ui accordingly hidpi - 144
  85. if DisplayServer.screen_get_dpi(0) >= 144:
  86. retina_scaling = 2.0
  87. else:
  88. retina_scaling = 1.0
  89. func scale_ui(scale_multiplier: float):
  90. var old_uiscale = uiscale
  91. uiscale = retina_scaling * scale_multiplier
  92. get_window().content_scale_factor = uiscale
  93. #goes through popup_windows group and scales all popups and resizes them
  94. for window in get_tree().get_nodes_in_group("popup_windows"):
  95. if old_uiscale != 0: #if ui scale = 0 this is the first time this is being adjusted so no need to revert values back to default first
  96. window.size = (window.size / old_uiscale) * uiscale
  97. else:
  98. window.size = window.size * uiscale
  99. window.content_scale_factor = uiscale
  100. func load_from_filesystem():
  101. #checks if user has opened a file from the system file menu and loads it
  102. var args = OS.get_cmdline_args()
  103. for arg in args:
  104. var path = arg.strip_edges()
  105. if FileAccess.file_exists(path) and path.get_extension().to_lower() == "thd":
  106. save_load.load_graph_edit(path)
  107. break
  108. if FileAccess.file_exists(path) and path.get_extension().to_lower() == "wav":
  109. default_input_node.get_node("AudioPlayer")._on_file_selected(path)
  110. break
  111. func new_patch():
  112. #clear old patch
  113. graph_edit.clear_connections()
  114. for node in graph_edit.get_children():
  115. if node is GraphNode:
  116. node.queue_free()
  117. await get_tree().process_frame # Wait for nodes to actually be removed
  118. graph_edit.scroll_offset = Vector2(0, 0)
  119. #Generate input and output nodes
  120. var effect = Utilities.nodes["inputfile"].instantiate()
  121. effect.name = "inputfile"
  122. get_node("GraphEdit").add_child(effect, true)
  123. effect.connect("open_help", Callable(open_help, "show_help_for_node"))
  124. if effect.has_signal("node_moved"):
  125. effect.node_moved.connect(graph_edit._auto_link_nodes)
  126. effect.position_offset = Vector2(20,80)
  127. default_input_node = effect #store a reference to this node to allow for loading into it directly if software launched with a wav file argument
  128. effect = Utilities.nodes["outputfile"].instantiate()
  129. effect.name = "outputfile"
  130. get_node("GraphEdit").add_child(effect, true)
  131. effect.init() #initialise ui from user prefs
  132. effect.connect("open_help", Callable(open_help, "show_help_for_node"))
  133. if effect.has_signal("node_moved"):
  134. effect.node_moved.connect(graph_edit._auto_link_nodes)
  135. effect.position_offset = Vector2((DisplayServer.screen_get_size().x - 480) / uiscale, 80)
  136. graph_edit._register_node_movement() #link nodes for tracking position changes for changes tracking
  137. #set label for last output folder
  138. var interface_settings = ConfigHandler.load_interface_settings()
  139. output_folder_label = effect.get_node("OutputFolderMargin/OutputFolderLabel")
  140. if output_folder_label != null and interface_settings.last_used_output_folder != "no_file":
  141. output_folder_label.text = interface_settings.last_used_output_folder
  142. output_folder_label.get_parent().tooltip_text = interface_settings.last_used_output_folder
  143. changesmade = false #so it stops trying to save unchanged empty files
  144. get_window().title = "SoundThread"
  145. link_output()
  146. #set fft size and overlap to default
  147. $FFTSize.select(9)
  148. _on_fft_size_item_selected(9)
  149. $FFTOverlap.select(2)
  150. _on_fft_overlap_item_selected(2)
  151. func link_output():
  152. #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
  153. for control in get_tree().get_nodes_in_group("outputnode"): #check all items in outputnode group
  154. if control.has_meta("outputfunction"):
  155. if control.get_meta("outputfunction") == "deleteintermediate": #link delete intermediate files toggle to script
  156. control.toggled.connect(_toggle_delete)
  157. _toggle_delete(control.button_pressed)
  158. #control.button_pressed = interface_settings.get("delete_intermediate", true)
  159. elif control.get_meta("outputfunction") == "runprocess": #link runprocess button
  160. control.button_down.connect(_run_process)
  161. elif control.get_meta("outputfunction") == "audioplayer": #link output audio player
  162. output_audio_player = control
  163. elif control.get_meta("outputfunction") == "filename":
  164. control.text = "outfile"
  165. outfilename = control
  166. elif control.get_meta("outputfunction") == "reusefolder":
  167. foldertoggle = control
  168. #foldertoggle.button_pressed = interface_settings.get("reuse_output_folder", true)
  169. elif control.get_meta("outputfunction") == "openfolder":
  170. control.button_down.connect(_open_output_folder)
  171. #for control in get_tree().get_nodes_in_group("inputnode"):
  172. #if control.get_meta("inputfunction") == "audioplayer": #link input for recycle function
  173. #print("input player found")
  174. #input_audio_player = control
  175. func check_user_preferences():
  176. var interface_settings = ConfigHandler.load_interface_settings()
  177. var audio_settings = ConfigHandler.load_audio_settings()
  178. var audio_devices = AudioServer.get_output_device_list()
  179. $Console.always_on_top = interface_settings.console_on_top
  180. if audio_devices.has(audio_settings.device):
  181. AudioServer.set_output_device(audio_settings.device)
  182. else:
  183. $AudioDevicePopup.popup_centered()
  184. match interface_settings.theme:
  185. 0:
  186. RenderingServer.set_default_clear_color(Color("#2f4f4e"))
  187. 1:
  188. RenderingServer.set_default_clear_color(Color("#000807"))
  189. 2:
  190. RenderingServer.set_default_clear_color(Color("#98d4d2"))
  191. 3:
  192. RenderingServer.set_default_clear_color(Color(interface_settings.theme_custom_colour))
  193. #set the theme to either the main theme or inverted theme depending on user preferences
  194. invert_theme_toggled(interface_settings.invert_theme)
  195. swap_zoom_and_move(interface_settings.swap_zoom_and_move)
  196. #scale ui
  197. scale_ui(interface_settings.ui_scale_multiplier)
  198. func show_cdp_location():
  199. $CdpLocationDialog.show()
  200. func check_cdp_location_set():
  201. #checks if the location has been set and prompts user to set it
  202. var cdpprogs_settings = ConfigHandler.load_cdpprogs_settings()
  203. if cdpprogs_settings.location == "no_location":
  204. $NoLocationPopup.popup_centered()
  205. else:
  206. #if location is set, stores it in a variable
  207. cdpprogs_location = str(cdpprogs_settings.location)
  208. print(cdpprogs_location)
  209. func _on_ok_button_button_down() -> void:
  210. #after user has read dialog on where to find cdp progs this loads the file browser
  211. $NoLocationPopup.hide()
  212. if OS.get_name() == "Windows":
  213. $CdpLocationDialog.current_dir = "C:/"
  214. else:
  215. $CdpLocationDialog.current_dir = OS.get_environment("HOME")
  216. $CdpLocationDialog.show()
  217. func _on_cdp_location_dialog_dir_selected(dir: String) -> void:
  218. var is_windows = OS.get_name() == "Windows"
  219. var cdprogs_correct
  220. #check if the selected folder contains the hilite program as it has a reasonably unique name and will indicate that the CDP processes do exist in that folder
  221. if is_windows:
  222. cdprogs_correct = FileAccess.file_exists(dir + "/distort.exe")
  223. else:
  224. cdprogs_correct = FileAccess.file_exists(dir + "/distort")
  225. if cdprogs_correct:
  226. #if this location does seem to contain cdp programs
  227. #saves default location for cdp programs in config file
  228. ConfigHandler.save_cdpprogs_settings(dir)
  229. cdpprogs_location = dir
  230. else:
  231. #if it doesn't seem to contain the programs then try and extrapolate the correct folder from the one selected
  232. var selected_folder = dir.get_slice("/", (dir.get_slice_count("/") - 1))
  233. print(selected_folder)
  234. if selected_folder.to_lower() == "cdpr8":
  235. dir = dir + "/_cdp/_cdprogs"
  236. #run this function recursively to check if the programs do exist
  237. _on_cdp_location_dialog_dir_selected(dir)
  238. elif selected_folder.to_lower() == "_cdp":
  239. dir = dir + "/_cdprogs"
  240. #run this function recursively to check if the programs do exist
  241. _on_cdp_location_dialog_dir_selected(dir)
  242. else:
  243. #can't find them
  244. use_anyway = dir
  245. $WrongFolderPopup.popup_centered()
  246. func _on_cdp_location_dialog_canceled() -> void:
  247. #cycles around the set location prompt if user cancels the file dialog
  248. check_cdp_location_set()
  249. func _on_select_folder_button_button_down() -> void:
  250. $WrongFolderPopup.hide()
  251. _on_ok_button_button_down()
  252. func _on_use_anyway_button_button_down() -> void:
  253. $WrongFolderPopup.hide()
  254. ConfigHandler.save_cdpprogs_settings(use_anyway)
  255. cdpprogs_location = use_anyway
  256. func _input(event):
  257. if event.is_action_pressed("undo"):
  258. simulate_mouse_click()
  259. await get_tree().process_frame
  260. undo_redo.undo()
  261. #elif event.is_action_pressed("redo"):
  262. #undo_redo.redo()
  263. elif event.is_action_pressed("save"):
  264. if currentfile == "none":
  265. savestate = "saveas"
  266. $SaveDialog.popup_centered()
  267. else:
  268. save_load.save_graph_edit(currentfile)
  269. elif event.is_action_pressed("open_explore"):
  270. open_explore()
  271. elif event.is_action_pressed("search"):
  272. var pos = graph_edit.get_local_mouse_position()
  273. _on_graph_edit_popup_request(pos)
  274. elif event.is_action_pressed("run_thread"):
  275. _run_process()
  276. elif event.is_action_pressed("new"):
  277. if changesmade == true:
  278. savestate = "newfile"
  279. $SaveChangesPopup.popup_centered()
  280. else:
  281. new_patch()
  282. currentfile = "none" #reset current file to none for save tracking
  283. elif event.is_action_pressed("save_as"):
  284. savestate = "saveas"
  285. $SaveDialog.popup_centered()
  286. func simulate_mouse_click():
  287. #simulates clicking the middle mouse button in order to hide any visible tooltips
  288. var click_pos = get_viewport().get_mouse_position()
  289. var down_event := InputEventMouseButton.new()
  290. down_event.button_index = MOUSE_BUTTON_MIDDLE
  291. down_event.pressed = true
  292. down_event.position = click_pos
  293. Input.parse_input_event(down_event)
  294. var up_event := InputEventMouseButton.new()
  295. up_event.button_index = MOUSE_BUTTON_MIDDLE
  296. up_event.pressed = false
  297. up_event.position = click_pos
  298. Input.parse_input_event(up_event)
  299. func _run_process() -> void:
  300. #check if any of the inputfile nodes don't have files loaded
  301. var interface_settings = ConfigHandler.load_interface_settings()
  302. for node in graph_edit.get_children():
  303. if node.get_meta("command") == "inputfile" and node.get_node("AudioPlayer").has_meta("inputfile") == false:
  304. $NoInputPopup.popup_centered()
  305. return
  306. #check if the reuse folder toggle is set and a folder has been previously chosen
  307. var output_folder = interface_settings.last_used_output_folder
  308. if foldertoggle.button_pressed == true and output_folder != "no_file" and DirAccess.open(output_folder) != null:
  309. _on_file_dialog_dir_selected(output_folder)
  310. else:
  311. $FileDialog.show()
  312. func _on_file_dialog_dir_selected(dir: String) -> void:
  313. ConfigHandler.save_interface_settings("last_used_output_folder", dir)
  314. if output_folder_label != null:
  315. output_folder_label.text = dir
  316. output_folder_label.tooltip_text = dir
  317. console_output.clear()
  318. var interface_settings = ConfigHandler.load_interface_settings()
  319. if interface_settings.disable_progress_bar == false:
  320. $ProgressWindow.show()
  321. else:
  322. if $Console.is_visible():
  323. $Console.hide()
  324. await get_tree().process_frame # Wait a frame to allow hide to complete
  325. $Console.popup_centered()
  326. else:
  327. $Console.popup_centered()
  328. await get_tree().process_frame
  329. run_thread.log_console("Generating processing queue", true)
  330. await get_tree().process_frame
  331. #get the current time in hh-mm-ss format as default : causes file name issues
  332. var time_dict = Time.get_time_dict_from_system()
  333. # Pad with zeros to ensure two digits for hour, minute, second
  334. var hour = str(time_dict.hour).pad_zeros(2)
  335. var minute = str(time_dict.minute).pad_zeros(2)
  336. var second = str(time_dict.second).pad_zeros(2)
  337. var time_str = hour + "-" + minute + "-" + second
  338. Global.outfile = dir + "/" + outfilename.text.get_basename() + "_" + Time.get_date_string_from_system() + "_" + time_str
  339. #check path and file name do not contain special characters
  340. var check_file_name = Global.check_for_invalid_chars(Global.outfile)
  341. if check_file_name["contains_invalid_characters"] == false:
  342. run_thread.log_console("Output directory and file name(s):" + Global.outfile, true)
  343. await get_tree().process_frame
  344. run_thread.run_thread_with_branches()
  345. else:
  346. run_thread.log_console("[color=#9c2828][b]Error:[/b][/color] Chosen file name or folder path " + Global.outfile.get_basename() + " contains invalid characters.", true)
  347. run_thread.log_console("File names and paths can only contain A-Z a-z 0-9 - _ + and space.", true)
  348. run_thread.log_console("Chosen file name/path contains the following invalid characters: " + " ".join(check_file_name["invalid_characters_found"]), true)
  349. if $ProgressWindow.visible:
  350. $ProgressWindow.hide()
  351. if !$Console.visible:
  352. $Console.popup_centered()
  353. func _toggle_delete(toggled_on: bool):
  354. delete_intermediate_outputs = toggled_on
  355. print(toggled_on)
  356. func _on_console_close_requested() -> void:
  357. $Console.hide()
  358. func _on_console_open_folder_button_down() -> void:
  359. $Console.hide()
  360. var interface_settings = ConfigHandler.load_interface_settings()
  361. var output_folder = interface_settings.last_used_output_folder
  362. if output_folder != "no_file" and DirAccess.open(output_folder) != null:
  363. OS.shell_open(output_folder)
  364. func _on_ok_button_2_button_down() -> void:
  365. $NoInputPopup.hide()
  366. func _on_ok_button_3_button_down() -> void:
  367. $MultipleConnectionsPopup.hide()
  368. func _on_settings_button_index_pressed(index: int) -> void:
  369. match index:
  370. 0:
  371. $Settings.cdpprogs_location = cdpprogs_location
  372. $Settings.popup_centered()
  373. 1:
  374. $AudioSettings.popup_centered()
  375. 2:
  376. if $Console.is_visible():
  377. $Console.hide()
  378. await get_tree().process_frame # Wait a frame to allow hide to complete
  379. $Console.popup_centered()
  380. else:
  381. $Console.popup_centered()
  382. func _on_file_button_index_pressed(index: int) -> void:
  383. match index:
  384. 0:
  385. if changesmade == true:
  386. savestate = "newfile"
  387. $SaveChangesPopup.popup_centered()
  388. else:
  389. new_patch()
  390. currentfile = "none" #reset current file to none for save tracking
  391. 1:
  392. if currentfile == "none":
  393. savestate = "saveas"
  394. $SaveDialog.popup_centered()
  395. else:
  396. save_load.save_graph_edit(currentfile)
  397. print("save pressed, changes made =")
  398. print(changesmade)
  399. print("current file =")
  400. print(currentfile)
  401. 2:
  402. savestate = "saveas"
  403. $SaveDialog.popup_centered()
  404. 3:
  405. if changesmade == true:
  406. savestate = "load"
  407. $SaveChangesPopup.popup_centered()
  408. else:
  409. $LoadDialog.popup_centered()
  410. func _on_save_dialog_file_selected(path: String) -> void:
  411. save_load.save_graph_edit(path) #save file
  412. #check what the user was trying to do before save and do that action
  413. if savestate == "newfile":
  414. new_patch()
  415. currentfile = "none" #reset current file to none for save tracking
  416. elif savestate == "load":
  417. $LoadDialog.popup_centered()
  418. elif savestate == "helpfile":
  419. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  420. save_load.load_graph_edit(helpfile)
  421. elif savestate == "quit":
  422. undo_redo.free()
  423. await get_tree().create_timer(0.25).timeout #little pause so that it feels like it actually saved even though it did
  424. get_tree().quit()
  425. elif savestate == "saveas":
  426. currentfile = path
  427. savestate = "none" #reset save state, not really needed but feels good
  428. func _on_load_dialog_file_selected(path: String) -> void:
  429. currentfile = path #tracking path here only means "save" only saves patches the user has loaded rather than overwriting help files
  430. save_load.load_graph_edit(path)
  431. func _on_help_button_index_pressed(index: int) -> void:
  432. match index:
  433. 0:
  434. pass
  435. 1:
  436. if changesmade == true:
  437. savestate = "helpfile"
  438. helpfile = "res://examples/getting_started.thd"
  439. $SaveChangesPopup.popup_centered()
  440. else:
  441. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  442. save_load.load_graph_edit("res://examples/getting_started.thd")
  443. 2:
  444. if changesmade == true:
  445. savestate = "helpfile"
  446. helpfile = "res://examples/navigating.thd"
  447. $SaveChangesPopup.popup_centered()
  448. else:
  449. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  450. save_load.load_graph_edit("res://examples/navigating.thd")
  451. 3:
  452. if changesmade == true:
  453. savestate = "helpfile"
  454. helpfile = "res://examples/building_a_thread.thd"
  455. $SaveChangesPopup.popup_centered()
  456. else:
  457. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  458. save_load.load_graph_edit("res://examples/building_a_thread.thd")
  459. 4:
  460. if changesmade == true:
  461. savestate = "helpfile"
  462. helpfile = "res://examples/frequency_domain.thd"
  463. $SaveChangesPopup.popup_centered()
  464. else:
  465. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  466. save_load.load_graph_edit("res://examples/frequency_domain.thd")
  467. 5:
  468. if changesmade == true:
  469. savestate = "helpfile"
  470. helpfile = "res://examples/automation.thd"
  471. $SaveChangesPopup.popup_centered()
  472. else:
  473. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  474. save_load.load_graph_edit("res://examples/automation.thd")
  475. 6:
  476. if changesmade == true:
  477. savestate = "helpfile"
  478. helpfile = "res://examples/trimming.thd"
  479. $SaveChangesPopup.popup_centered()
  480. else:
  481. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  482. save_load.load_graph_edit("res://examples/trimming.thd")
  483. 7:
  484. if changesmade == true:
  485. savestate = "helpfile"
  486. helpfile = "res://examples/multiple_inputs.thd"
  487. $SaveChangesPopup.popup_centered()
  488. else:
  489. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  490. save_load.load_graph_edit("res://examples/multiple_inputs.thd")
  491. 8:
  492. if changesmade == true:
  493. savestate = "helpfile"
  494. helpfile = "res://examples/preview_nodes.thd"
  495. $SaveChangesPopup.popup_centered()
  496. else:
  497. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  498. save_load.load_graph_edit("res://examples/preview_nodes.thd")
  499. 9:
  500. pass
  501. 10:
  502. if changesmade == true:
  503. savestate = "helpfile"
  504. helpfile = "res://examples/wetdry.thd"
  505. $SaveChangesPopup.popup_centered()
  506. else:
  507. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  508. save_load.load_graph_edit("res://examples/wetdry.thd")
  509. 11:
  510. if changesmade == true:
  511. savestate = "helpfile"
  512. helpfile = "res://examples/resonant_filters.thd"
  513. $SaveChangesPopup.popup_centered()
  514. else:
  515. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  516. save_load.load_graph_edit("res://examples/resonant_filters.thd")
  517. 12:
  518. pass
  519. 13:
  520. OS.shell_open("https://www.composersdesktop.com/docs/html/ccdpndex.htm")
  521. 14:
  522. OS.shell_open("https://github.com/j-p-higgins/SoundThread/issues")
  523. #func _recycle_outfile():
  524. #if outfile != "no file":
  525. #input_audio_player.recycle_outfile(outfile)
  526. func _on_save_changes_button_down() -> void:
  527. $SaveChangesPopup.hide()
  528. if currentfile == "none":
  529. $SaveDialog.show()
  530. else:
  531. save_load.save_graph_edit(currentfile)
  532. if savestate == "newfile":
  533. new_patch()
  534. currentfile = "none" #reset current file to none for save tracking
  535. elif savestate == "load":
  536. $LoadDialog.popup_centered()
  537. elif savestate == "helpfile":
  538. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  539. save_load.load_graph_edit(helpfile)
  540. elif savestate == "quit":
  541. undo_redo.free()
  542. await get_tree().create_timer(0.25).timeout #little pause so that it feels like it actually saved even though it did
  543. get_tree().quit()
  544. savestate = "none"
  545. func _on_dont_save_changes_button_down() -> void:
  546. $SaveChangesPopup.hide()
  547. if savestate == "newfile":
  548. new_patch()
  549. currentfile = "none" #reset current file to none for save tracking
  550. elif savestate == "load":
  551. $LoadDialog.popup_centered()
  552. elif savestate == "helpfile":
  553. currentfile = "none" #reset current file to none for save tracking so user cant save over help file
  554. save_load.load_graph_edit(helpfile)
  555. elif savestate == "quit":
  556. undo_redo.free()
  557. get_tree().quit()
  558. savestate = "none"
  559. func _on_cancel_changes_button_down() -> void:
  560. $SaveChangesPopup.hide()
  561. savestate = "none"
  562. func _notification(what):
  563. if what == NOTIFICATION_WM_CLOSE_REQUEST:
  564. run_thread._on_kill_process_button_down()
  565. $Console.hide()
  566. if changesmade == true:
  567. savestate = "quit"
  568. $SaveChangesPopup.popup_centered()
  569. #$HelpWindow.hide()
  570. else:
  571. undo_redo.free()
  572. get_tree().quit() # default behavior
  573. func _open_output_folder():
  574. var interface_settings = ConfigHandler.load_interface_settings()
  575. var output_folder = interface_settings.last_used_output_folder
  576. if output_folder != "no_file" and DirAccess.open(output_folder) != null:
  577. OS.shell_open(output_folder)
  578. func _on_rich_text_label_meta_clicked(meta: Variant) -> void:
  579. print(str(meta))
  580. OS.shell_open(str(meta))
  581. func _on_graph_edit_popup_request(at_position: Vector2) -> void:
  582. effect_position = graph_edit.get_local_mouse_position()
  583. #give the search menu the ui scale
  584. $SearchMenu.uiscale = uiscale
  585. #get the mouse position in screen coordinates
  586. var mouse_screen_pos = DisplayServer.mouse_get_position()
  587. #get the window position in screen coordinates
  588. var window_screen_pos = get_window().position
  589. #get the window size relative to its scaling for retina displays
  590. var window_size = get_window().size * DisplayServer.screen_get_scale()
  591. #see if it was empty space or a node that was right clicked
  592. var clicked_node
  593. for child in graph_edit.get_children():
  594. if child is GraphNode:
  595. if Rect2(child.position, child.size).has_point(effect_position):
  596. clicked_node = child
  597. break
  598. if clicked_node and clicked_node.get_meta("command") != "outputfile":
  599. var title = clicked_node.title
  600. if Input.is_action_pressed("auto_link_nodes"):
  601. $SearchMenu/VBoxContainer/ReplaceLabel.text = "Connect to " + title
  602. $SearchMenu/VBoxContainer/ReplaceLabel.show()
  603. $SearchMenu.replace_node = false
  604. $SearchMenu.connect_to_node = true
  605. $SearchMenu.node_to_connect_to = clicked_node
  606. else:
  607. $SearchMenu/VBoxContainer/ReplaceLabel.text = "Replace " + title
  608. $SearchMenu/VBoxContainer/ReplaceLabel.show()
  609. $SearchMenu.replace_node = true
  610. $SearchMenu.connect_to_node = false
  611. $SearchMenu.node_to_replace = clicked_node
  612. else:
  613. var interface_settings = ConfigHandler.load_interface_settings()
  614. if interface_settings.right_click_opens_explore:
  615. open_explore()
  616. return
  617. else:
  618. $SearchMenu/VBoxContainer/ReplaceLabel.hide()
  619. $SearchMenu.replace_node = false
  620. $SearchMenu.connect_to_node = false
  621. #calculate the xy position of the mouse clamped to the size of the window and menu so it doesn't go off the screen
  622. var clamped_x = clamp(mouse_screen_pos.x, window_screen_pos.x, window_screen_pos.x + window_size.x - $SearchMenu.size.x)
  623. var clamped_y = clamp(mouse_screen_pos.y, window_screen_pos.y, window_screen_pos.y + window_size.y - (420 * DisplayServer.screen_get_scale()))
  624. #position and show the menu
  625. $SearchMenu.position = Vector2(clamped_x, clamped_y)
  626. $SearchMenu.popup()
  627. func _on_audio_settings_close_requested() -> void:
  628. $AudioSettings.hide()
  629. func _on_open_audio_settings_button_down() -> void:
  630. $AudioDevicePopup.hide()
  631. $AudioSettings.popup_centered()
  632. func _on_audio_device_popup_close_requested() -> void:
  633. $AudioDevicePopup.hide()
  634. func _on_mainmenu_close_requested() -> void:
  635. #closes menu if click is anywhere other than the menu as it is a window with popup set to true
  636. $mainmenu.hide()
  637. func open_explore():
  638. effect_position = graph_edit.get_local_mouse_position()
  639. #get the mouse position in screen coordinates
  640. var mouse_screen_pos = DisplayServer.mouse_get_position()
  641. #get the window position in screen coordinates
  642. var window_screen_pos = get_window().position
  643. #get the window size relative to its scaling for retina displays
  644. var window_size = get_window().size * DisplayServer.screen_get_scale()
  645. #get the size of the popup menu
  646. var popup_size = $mainmenu.size
  647. #calculate the xy position of the mouse clamped to the size of the window and menu so it doesn't go off the screen
  648. var clamped_x = clamp(mouse_screen_pos.x, window_screen_pos.x, window_screen_pos.x + window_size.x - popup_size.x)
  649. var clamped_y = clamp(mouse_screen_pos.y, window_screen_pos.y, window_screen_pos.y + window_size.y - popup_size.y)
  650. #position and show the menu
  651. $mainmenu.position = Vector2(clamped_x, clamped_y)
  652. $mainmenu.popup()
  653. func change_console_settings(toggled: bool):
  654. $Console.always_on_top = toggled
  655. func _on_kill_process_button_down() -> void:
  656. run_thread._on_kill_process_button_down()
  657. func invert_theme_toggled(toggled: bool):
  658. if toggled:
  659. var inverted = invert_theme(main_theme)
  660. get_tree().root.theme = inverted # force refresh
  661. $MenuBarBackground.color = Color(0.934, 0.934, 0.934)
  662. for color_rect in get_tree().get_nodes_in_group("invertable_background"):
  663. if color_rect is ColorRect:
  664. color_rect.color = Color(0.898, 0.898, 0.898, 0.6)
  665. else:
  666. get_tree().root.theme = main_theme # force refresheme = main_theme
  667. $MenuBarBackground.color = Color(0.065, 0.065, 0.065)
  668. for color_rect in get_tree().get_nodes_in_group("invertable_background"):
  669. if color_rect is ColorRect:
  670. color_rect.color = Color(0.102, 0.102, 0.102, 0.6)
  671. func invert_theme(theme: Theme) -> Theme:
  672. var inverted_theme = theme.duplicate(true) # deep copy
  673. # Check all types and color names in the theme
  674. var types = inverted_theme.get_type_list()
  675. for type in types:
  676. var color_names = inverted_theme.get_color_list(type)
  677. for cname in color_names:
  678. var col = inverted_theme.get_color(cname, type)
  679. var inverted = Color(1.0 - col.r, 1.0 - col.g, 1.0 - col.b, col.a)
  680. inverted_theme.set_color(cname, type, inverted)
  681. var style_names = inverted_theme.get_stylebox_list(type)
  682. for sname in style_names:
  683. if type == "GraphEdit" and sname == "panel":
  684. continue
  685. var sb = inverted_theme.get_stylebox(sname, type)
  686. var new_sb = sb.duplicate()
  687. if new_sb is StyleBoxFlat:
  688. var col = new_sb.bg_color
  689. new_sb.bg_color = Color(1.0 - col.r, 1.0 - col.g, 1.0 - col.b, col.a)
  690. inverted_theme.set_stylebox(sname, type, new_sb)
  691. return inverted_theme
  692. func swap_zoom_and_move(toggled: bool):
  693. if toggled:
  694. graph_edit.set_panning_scheme(1)
  695. else:
  696. graph_edit.set_panning_scheme(0)
  697. func on_files_dropped(files):
  698. var mouse_pos = graph_edit.get_local_mouse_position()
  699. #see if files were dropped on an input node
  700. var dropped_node
  701. for child in graph_edit.get_children():
  702. if child is GraphNode:
  703. if Rect2(child.position, child.size).has_point(mouse_pos):
  704. dropped_node = child
  705. break
  706. #if they were dropped on a node and the first file in the array is a wav file replace the file in the input node
  707. if dropped_node and dropped_node.has_meta("command") and dropped_node.get_meta("command") == "inputfile":
  708. if files[0].get_extension().to_lower() == "wav":
  709. dropped_node.get_node("AudioPlayer")._on_file_selected(files[0])
  710. else:
  711. #else make a new input node at the mouse position and load it in
  712. if files[0].get_extension().to_lower() == "wav":
  713. var new_input_node = graph_edit._make_node("inputfile")
  714. new_input_node.position_offset = mouse_pos
  715. new_input_node.get_node("AudioPlayer")._on_file_selected(files[0])
  716. #remove first element from the array
  717. files.remove_at(0)
  718. #check if there are any other files
  719. if files.size() > 0:
  720. var position_plus_offset = Vector2(mouse_pos.x, mouse_pos.y + 250) #apply a vertical offset from the mouse position so nodes dont overlap
  721. for file in files:
  722. if file.get_extension().to_lower() == "wav":
  723. var new_input_node = graph_edit._make_node("inputfile")
  724. new_input_node.position_offset = position_plus_offset
  725. new_input_node.get_node("AudioPlayer")._on_file_selected(file)
  726. position_plus_offset.y = position_plus_offset.y + 250
  727. func _on_fft_size_item_selected(index: int) -> void:
  728. var fft_size
  729. if index == 13:
  730. fft_size = 16380
  731. else:
  732. fft_size = 1 << (index + 1)
  733. run_thread.fft_size = fft_size
  734. func _on_fft_overlap_item_selected(index: int) -> void:
  735. run_thread.fft_overlap = index + 1