save_load.gd 6.0 KB

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