save_load.gd 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 = {}
  102. # Create nodes
  103. for node_data in graph_data["nodes"]:
  104. var command_name = node_data.get("command", "")
  105. var new_node = graph_edit._make_node(command_name, true)
  106. if new_node == null:
  107. print("Failed to create node for command:", command_name)
  108. continue
  109. new_node.name = node_data["name"]
  110. new_node.position_offset = Vector2(node_data["offset"]["x"], node_data["offset"]["y"])
  111. id_to_node[node_data["id"]] = new_node
  112. # Restore sliders
  113. for slider_path_str in node_data["slider_values"]:
  114. var slider = new_node.get_node_or_null(slider_path_str)
  115. if slider and (slider is HSlider or slider is VSlider):
  116. var slider_info = node_data["slider_values"][slider_path_str]
  117. if typeof(slider_info) == TYPE_DICTIONARY:
  118. slider.value = slider_info.get("value", slider.value)
  119. if slider_info.has("editable"):
  120. slider.editable = slider_info["editable"]
  121. if slider_info.has("meta"):
  122. for key in slider_info["meta"]:
  123. var value = slider_info["meta"][key]
  124. if key == "brk_data" and typeof(value) == TYPE_ARRAY:
  125. var new_array: Array = []
  126. for item in value:
  127. if typeof(item) == TYPE_STRING:
  128. var numbers: PackedStringArray = item.strip_edges().trim_prefix("(").trim_suffix(")").split(",")
  129. if numbers.size() == 2:
  130. var x = float(numbers[0])
  131. var y = float(numbers[1])
  132. new_array.append(Vector2(x, y))
  133. value = new_array
  134. slider.set_meta(key, value)
  135. else:
  136. slider.value = slider_info
  137. # Restore notes
  138. for codeedit_name in node_data["notes"]:
  139. var codeedit = new_node.find_child(codeedit_name, true, false)
  140. if codeedit and (codeedit is CodeEdit):
  141. codeedit.text = node_data["notes"][codeedit_name]
  142. register_input.call(new_node)
  143. # Recreate connections
  144. for conn in graph_data["connections"]:
  145. var from_node = id_to_node.get(conn["from_node_id"], null)
  146. var to_node = id_to_node.get(conn["to_node_id"], null)
  147. if from_node != null and to_node != null:
  148. graph_edit.connect_node(
  149. from_node.name, conn["from_port"],
  150. to_node.name, conn["to_port"]
  151. )
  152. else:
  153. print("Warning: Connection references unknown node ID(s). Skipping connection.")
  154. link_output.call()
  155. print("Graph loaded.")
  156. get_window().title = "SoundThread - " + path.get_file().trim_suffix(".thd")