save_load.gd 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. extends Node
  2. var control_script
  3. var graph_edit
  4. var open_help
  5. var register_movement
  6. var register_input
  7. var link_output
  8. # Called when the node enters the scene tree for the first time.
  9. func _ready() -> void:
  10. pass # Replace with function body.
  11. func init(main_node: Node, graphedit: GraphEdit, openhelp: Callable, registermovement: Callable, registerinput: Callable, linkoutput: Callable) -> void:
  12. control_script = main_node
  13. graph_edit = graphedit
  14. open_help = openhelp
  15. register_movement = registermovement
  16. register_input = registerinput
  17. link_output = linkoutput
  18. func save_graph_edit(path: String):
  19. var file = FileAccess.open(path, FileAccess.WRITE)
  20. if file == null:
  21. print("Failed to open file for saving")
  22. return
  23. var node_data_list = []
  24. var connection_data_list = []
  25. var node_id_map = {} # Map node name to numeric ID
  26. var node_id = 1
  27. # Assign each node a unique numeric ID and gather node data
  28. for node in graph_edit.get_children():
  29. if node is GraphNode:
  30. node_id_map[node.name] = node_id
  31. var offset = node.position_offset
  32. var node_data = {
  33. "id": node_id,
  34. "name": node.name,
  35. "command": node.get_meta("command"),
  36. "offset": { "x": offset.x, "y": offset.y },
  37. "slider_values": {},
  38. "addremoveinlets":{},
  39. "notes": {},
  40. "checkbutton_states": {},
  41. "optionbutton_values": {}
  42. }
  43. # Save slider values and metadata
  44. for child in node.find_children("*", "Slider", true, false):
  45. var relative_path = node.get_path_to(child)
  46. var path_str = str(relative_path)
  47. node_data["slider_values"][path_str] = {
  48. "value": child.value,
  49. "editable": child.editable,
  50. "meta": {}
  51. }
  52. for key in child.get_meta_list():
  53. node_data["slider_values"][path_str]["meta"][str(key)] = child.get_meta(key)
  54. #save add remove inlet meta data
  55. if node.has_node("addremoveinlets"):
  56. if node.get_node("addremoveinlets").has_meta("inlet_count"):
  57. node_data["addremoveinlets"]["inlet_count"] = node.get_node("addremoveinlets").get_meta("inlet_count")
  58. # Save notes from CodeEdit children
  59. for child in node.find_children("*", "CodeEdit", true, false):
  60. node_data["notes"][child.name] = child.text
  61. #save checkbutton states
  62. for child in node.find_children("*", "CheckButton", true, false):
  63. node_data["checkbutton_states"][child.name] = child.button_pressed
  64. #save optionbutton states
  65. for child in node.find_children("*", "OptionButton", true, false):
  66. node_data["optionbutton_values"][child.name] = child.selected
  67. node_data_list.append(node_data)
  68. node_id += 1
  69. # Save connections using node IDs instead of names
  70. for conn in graph_edit.get_connection_list():
  71. # Map from_node and to_node names to IDs
  72. var from_id = node_id_map.get(conn["from_node"], null)
  73. var to_id = node_id_map.get(conn["to_node"], null)
  74. if from_id != null and to_id != null:
  75. connection_data_list.append({
  76. "from_node_id": from_id,
  77. "from_port": conn["from_port"],
  78. "to_node_id": to_id,
  79. "to_port": conn["to_port"]
  80. })
  81. else:
  82. print("Warning: Connection references unknown node(s). Skipping connection.")
  83. var graph_data = {
  84. "nodes": node_data_list,
  85. "connections": connection_data_list
  86. }
  87. var json = JSON.new()
  88. var json_string = json.stringify(graph_data, "\t")
  89. file.store_string(json_string)
  90. file.close()
  91. print("Graph saved.")
  92. control_script.changesmade = false
  93. get_window().title = "SoundThread - " + path.get_file().trim_suffix(".thd")
  94. func load_graph_edit(path: String):
  95. var file = FileAccess.open(path, FileAccess.READ)
  96. if file == null:
  97. print("Failed to open file for loading")
  98. return
  99. var json_text = file.get_as_text()
  100. file.close()
  101. var json = JSON.new()
  102. if json.parse(json_text) != OK:
  103. print("Error parsing JSON")
  104. return
  105. var graph_data = json.get_data()
  106. graph_edit.clear_connections()
  107. # Remove all existing GraphNodes from graph_edit
  108. for node in graph_edit.get_children():
  109. if node is GraphNode:
  110. node.queue_free()
  111. await get_tree().process_frame # Ensure nodes are freed before adding new ones
  112. var id_to_node = {}
  113. # Create nodes
  114. for node_data in graph_data["nodes"]:
  115. var command_name = node_data.get("command", "")
  116. var new_node = graph_edit._make_node(command_name, true)
  117. if new_node == null:
  118. print("Failed to create node for command:", command_name)
  119. continue
  120. new_node.name = node_data["name"]
  121. new_node.position_offset = Vector2(node_data["offset"]["x"], node_data["offset"]["y"])
  122. id_to_node[node_data["id"]] = new_node
  123. # Restore sliders
  124. for slider_path_str in node_data["slider_values"]:
  125. var slider = new_node.get_node_or_null(slider_path_str)
  126. if slider and (slider is HSlider or slider is VSlider):
  127. var slider_info = node_data["slider_values"][slider_path_str]
  128. if typeof(slider_info) == TYPE_DICTIONARY:
  129. slider.value = slider_info.get("value", slider.value)
  130. if slider_info.has("editable"):
  131. slider.editable = slider_info["editable"]
  132. if slider_info.has("meta"):
  133. for key in slider_info["meta"]:
  134. var value = slider_info["meta"][key]
  135. if key == "brk_data" and typeof(value) == TYPE_ARRAY:
  136. var new_array: Array = []
  137. for item in value:
  138. if typeof(item) == TYPE_STRING:
  139. var numbers: PackedStringArray = item.strip_edges().trim_prefix("(").trim_suffix(")").split(",")
  140. if numbers.size() == 2:
  141. var x = float(numbers[0])
  142. var y = float(numbers[1])
  143. new_array.append(Vector2(x, y))
  144. value = new_array
  145. slider.set_meta(key, value)
  146. else:
  147. slider.value = slider_info
  148. # Restore notes
  149. for codeedit_name in node_data["notes"]:
  150. var codeedit = new_node.find_child(codeedit_name, true, false)
  151. if codeedit and (codeedit is CodeEdit):
  152. codeedit.text = node_data["notes"][codeedit_name]
  153. # Restore check buttons if this exists in the file (if statement is to stop crashes when opening old save files)
  154. if node_data.has("checkbutton_states"):
  155. for checkbutton_name in node_data["checkbutton_states"]:
  156. var checkbutton = new_node.find_child(checkbutton_name, true, false)
  157. if checkbutton and (checkbutton is CheckButton):
  158. checkbutton.button_pressed = node_data["checkbutton_states"][checkbutton_name]
  159. # Restore option buttons if this exists in the file (if statement is to stop crashes when opening old save files)
  160. if node_data.has("optionbutton_values"):
  161. for optionbutton_name in node_data["optionbutton_values"]:
  162. var optionbutton = new_node.find_child(optionbutton_name, true, false)
  163. if optionbutton and (optionbutton is OptionButton):
  164. optionbutton.selected = node_data["optionbutton_values"][optionbutton_name]
  165. #restore dynamic inlets
  166. if node_data.has("addremoveinlets") and new_node.has_node("addremoveinlets"):
  167. print("restoring inlets")
  168. var addremoveinlets = new_node.get_node("addremoveinlets")
  169. addremoveinlets.set_meta("inlet_count", node_data["addremoveinlets"]["inlet_count"])
  170. await get_tree().process_frame
  171. addremoveinlets.restore_inlets()
  172. register_input.call(new_node)
  173. # Recreate connections
  174. for conn in graph_data["connections"]:
  175. var from_node = id_to_node.get(conn["from_node_id"], null)
  176. var to_node = id_to_node.get(conn["to_node_id"], null)
  177. if from_node != null and to_node != null:
  178. graph_edit.connect_node(
  179. from_node.name, conn["from_port"],
  180. to_node.name, conn["to_port"]
  181. )
  182. else:
  183. print("Warning: Connection references unknown node ID(s). Skipping connection.")
  184. link_output.call()
  185. print("Graph loaded.")
  186. get_window().title = "SoundThread - " + path.get_file().trim_suffix(".thd")
  187. control_script.changesmade = false