save_load.gd 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. "notes": {},
  39. "checkbutton_states": {},
  40. "optionbutton_values": {}
  41. }
  42. # Save slider values and metadata
  43. for child in node.find_children("*", "Slider", true, false):
  44. var relative_path = node.get_path_to(child)
  45. var path_str = str(relative_path)
  46. node_data["slider_values"][path_str] = {
  47. "value": child.value,
  48. "editable": child.editable,
  49. "meta": {}
  50. }
  51. for key in child.get_meta_list():
  52. node_data["slider_values"][path_str]["meta"][str(key)] = child.get_meta(key)
  53. # Save notes from CodeEdit children
  54. for child in node.find_children("*", "CodeEdit", true, false):
  55. node_data["notes"][child.name] = child.text
  56. #save checkbutton states
  57. for child in node.find_children("*", "CheckButton", true, false):
  58. node_data["checkbutton_states"][child.name] = child.button_pressed
  59. #save optionbutton states
  60. for child in node.find_children("*", "OptionButton", true, false):
  61. node_data["optionbutton_values"][child.name] = child.selected
  62. node_data_list.append(node_data)
  63. node_id += 1
  64. # Save connections using node IDs instead of names
  65. for conn in graph_edit.get_connection_list():
  66. # Map from_node and to_node names to IDs
  67. var from_id = node_id_map.get(conn["from_node"], null)
  68. var to_id = node_id_map.get(conn["to_node"], null)
  69. if from_id != null and to_id != null:
  70. connection_data_list.append({
  71. "from_node_id": from_id,
  72. "from_port": conn["from_port"],
  73. "to_node_id": to_id,
  74. "to_port": conn["to_port"]
  75. })
  76. else:
  77. print("Warning: Connection references unknown node(s). Skipping connection.")
  78. var graph_data = {
  79. "nodes": node_data_list,
  80. "connections": connection_data_list
  81. }
  82. var json = JSON.new()
  83. var json_string = json.stringify(graph_data, "\t")
  84. file.store_string(json_string)
  85. file.close()
  86. print("Graph saved.")
  87. control_script.changesmade = false
  88. get_window().title = "SoundThread - " + path.get_file().trim_suffix(".thd")
  89. func load_graph_edit(path: String):
  90. var file = FileAccess.open(path, FileAccess.READ)
  91. if file == null:
  92. print("Failed to open file for loading")
  93. return
  94. var json_text = file.get_as_text()
  95. file.close()
  96. var json = JSON.new()
  97. if json.parse(json_text) != OK:
  98. print("Error parsing JSON")
  99. return
  100. var graph_data = json.get_data()
  101. graph_edit.clear_connections()
  102. # Remove all existing GraphNodes from graph_edit
  103. for node in graph_edit.get_children():
  104. if node is GraphNode:
  105. node.queue_free()
  106. await get_tree().process_frame # Ensure nodes are freed before adding new ones
  107. var id_to_node = {}
  108. # Create nodes
  109. for node_data in graph_data["nodes"]:
  110. var command_name = node_data.get("command", "")
  111. var new_node = graph_edit._make_node(command_name, true)
  112. if new_node == null:
  113. print("Failed to create node for command:", command_name)
  114. continue
  115. new_node.name = node_data["name"]
  116. new_node.position_offset = Vector2(node_data["offset"]["x"], node_data["offset"]["y"])
  117. id_to_node[node_data["id"]] = new_node
  118. # Restore sliders
  119. for slider_path_str in node_data["slider_values"]:
  120. var slider = new_node.get_node_or_null(slider_path_str)
  121. if slider and (slider is HSlider or slider is VSlider):
  122. var slider_info = node_data["slider_values"][slider_path_str]
  123. if typeof(slider_info) == TYPE_DICTIONARY:
  124. slider.value = slider_info.get("value", slider.value)
  125. if slider_info.has("editable"):
  126. slider.editable = slider_info["editable"]
  127. if slider_info.has("meta"):
  128. for key in slider_info["meta"]:
  129. var value = slider_info["meta"][key]
  130. if key == "brk_data" and typeof(value) == TYPE_ARRAY:
  131. var new_array: Array = []
  132. for item in value:
  133. if typeof(item) == TYPE_STRING:
  134. var numbers: PackedStringArray = item.strip_edges().trim_prefix("(").trim_suffix(")").split(",")
  135. if numbers.size() == 2:
  136. var x = float(numbers[0])
  137. var y = float(numbers[1])
  138. new_array.append(Vector2(x, y))
  139. value = new_array
  140. slider.set_meta(key, value)
  141. else:
  142. slider.value = slider_info
  143. # Restore notes
  144. for codeedit_name in node_data["notes"]:
  145. var codeedit = new_node.find_child(codeedit_name, true, false)
  146. if codeedit and (codeedit is CodeEdit):
  147. codeedit.text = node_data["notes"][codeedit_name]
  148. # Restore check buttons if this exists in the file (if statement is to stop crashes when opening old save files)
  149. if node_data.has("checkbutton_states"):
  150. for checkbutton_name in node_data["checkbutton_states"]:
  151. var checkbutton = new_node.find_child(checkbutton_name, true, false)
  152. if checkbutton and (checkbutton is CheckButton):
  153. checkbutton.button_pressed = node_data["checkbutton_states"][checkbutton_name]
  154. # Restore option buttons if this exists in the file (if statement is to stop crashes when opening old save files)
  155. if node_data.has("optionbutton_values"):
  156. for optionbutton_name in node_data["optionbutton_values"]:
  157. var optionbutton = new_node.find_child(optionbutton_name, true, false)
  158. if optionbutton and (optionbutton is OptionButton):
  159. optionbutton.selected = node_data["optionbutton_values"][optionbutton_name]
  160. register_input.call(new_node)
  161. # Recreate connections
  162. for conn in graph_data["connections"]:
  163. var from_node = id_to_node.get(conn["from_node_id"], null)
  164. var to_node = id_to_node.get(conn["to_node_id"], null)
  165. if from_node != null and to_node != null:
  166. graph_edit.connect_node(
  167. from_node.name, conn["from_port"],
  168. to_node.name, conn["to_port"]
  169. )
  170. else:
  171. print("Warning: Connection references unknown node ID(s). Skipping connection.")
  172. link_output.call()
  173. print("Graph loaded.")
  174. get_window().title = "SoundThread - " + path.get_file().trim_suffix(".thd")
  175. control_script.changesmade = false