json_editor.gd 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. extends Control
  2. var node_data = {} #stores json file
  3. @onready var parameter_container = $HBoxContainer/VBoxContainer2/ScrollContainer/parameter_container
  4. var json = "res://scenes/main/process_help.json"
  5. func _ready() -> void:
  6. Nodes.hide()
  7. load_json()
  8. func load_json():
  9. var file = FileAccess.open(json, FileAccess.READ)
  10. if file:
  11. node_data = JSON.parse_string(file.get_as_text())
  12. fill_search("")
  13. func fill_search(filter: String):
  14. # Remove all existing items from the VBoxContainer
  15. var container = $HBoxContainer/VBoxContainer/search/MarginContainer/VBoxContainer/ScrollContainer/ItemContainer
  16. for child in container.get_children():
  17. child.queue_free()
  18. for key in node_data.keys():
  19. var item = node_data[key]
  20. var title = item.get("title", "")
  21. var category = item.get("category", "")
  22. var subcategory = item.get("subcategory", "")
  23. var short_desc = item.get("short_description", "")
  24. # If filter is not empty, skip non-matches populate all other buttons
  25. if filter != "":
  26. var filter_lc = filter.to_lower()
  27. if not (filter_lc in title.to_lower() or filter_lc in short_desc.to_lower() or filter_lc in category.to_lower() or filter_lc in subcategory.to_lower()):
  28. continue
  29. var hbox = HBoxContainer.new()
  30. var label = RichTextLabel.new()
  31. var editbtn = Button.new()
  32. var margin = MarginContainer.new()
  33. hbox.size.x = container.size.x
  34. label.bbcode_enabled = true
  35. label.text = "[b]%s[/b]\n%s" % [title, short_desc]
  36. label.set_h_size_flags(Control.SIZE_EXPAND_FILL)
  37. label.set_v_size_flags(Control.SIZE_EXPAND_FILL)
  38. label.fit_content = true
  39. editbtn.text = "Edit"
  40. editbtn.custom_minimum_size = Vector2(80, 40)
  41. editbtn.set_h_size_flags(Control.SIZE_SHRINK_CENTER)
  42. editbtn.connect("pressed", Callable(self, "edit_node").bind(key)) #pass key (process name) when button is pressed
  43. margin.add_theme_constant_override("margin_bottom", 3)
  44. container.add_child(hbox)
  45. hbox.add_child(label)
  46. hbox.add_child(editbtn)
  47. container.add_child(margin)
  48. func _on_search_bar_text_changed(new_text: String) -> void:
  49. fill_search(new_text)
  50. func edit_node(key: String):
  51. if node_data.has(key):
  52. #looks up the help data from the json and stores it in info
  53. var info = node_data[key]
  54. var parameters = info.get("parameters", {})
  55. $HBoxContainer/VBoxContainer2/HBoxContainer/key.text = key
  56. $HBoxContainer/VBoxContainer2/HBoxContainer2/category.text = info.get("category", "")
  57. $HBoxContainer/VBoxContainer2/HBoxContainer3/subcategory.text = info.get("subcategory", "")
  58. $HBoxContainer/VBoxContainer2/HBoxContainer4/title.text = info.get("title", "")
  59. $HBoxContainer/VBoxContainer2/HBoxContainer5/shortdescription.text = info.get("short_description", "")
  60. $HBoxContainer/VBoxContainer2/HBoxContainer7/longdescription.text = info.get("description", "")
  61. $HBoxContainer/VBoxContainer2/HBoxContainer6/stereo.button_pressed = bool(info.get("stereo"))
  62. $HBoxContainer/VBoxContainer2/HBoxContainer9/inputtype.text = str(info.get("inputtype", ""))
  63. $HBoxContainer/VBoxContainer2/HBoxContainer11/outputtype.text = str(info.get("outputtype", ""))
  64. for child in parameter_container.get_children():
  65. child.queue_free()
  66. var count = 1
  67. for param_key in parameters.keys():
  68. var param_box = VBoxContainer.new()
  69. param_box.set_h_size_flags(Control.SIZE_EXPAND_FILL)
  70. parameter_container.add_child(param_box)
  71. var label = Label.new()
  72. label.text = "Parameter " + str(count)
  73. param_box.add_child(label)
  74. var param_data = parameters[param_key]
  75. for field_key in param_data.keys():
  76. var field_value = param_data[field_key]
  77. var hbox = HBoxContainer.new()
  78. var namelabel = RichTextLabel.new()
  79. var namefield
  80. namelabel.text = field_key
  81. namelabel.custom_minimum_size.x = 250
  82. if field_value is bool:
  83. namefield = CheckBox.new()
  84. namefield.button_pressed = field_value
  85. else:
  86. namefield = LineEdit.new()
  87. namefield.text = str(field_value)
  88. namefield.set_h_size_flags(Control.SIZE_EXPAND_FILL)
  89. hbox.add_child(namelabel)
  90. hbox.add_child(namefield)
  91. param_box.add_child(hbox)
  92. var delete_button = Button.new()
  93. delete_button.text = "Delete " + param_data.get("paramname", "")
  94. delete_button.set_h_size_flags(Control.SIZE_EXPAND)
  95. delete_button.connect("pressed", Callable(self, "delete_param").bind(param_box))
  96. param_box.add_child(delete_button)
  97. var margin = MarginContainer.new()
  98. margin.add_theme_constant_override("margin_bottom", 5)
  99. param_box.add_child(margin)
  100. count += 1
  101. func delete_param(container: VBoxContainer):
  102. container.queue_free()
  103. func _on_button_button_down() -> void:
  104. var info = node_data["distort_replace"]
  105. var parameters = info.get("parameters", {})
  106. var parameter = parameters.get("param1", {})
  107. var param_box = VBoxContainer.new()
  108. param_box.set_h_size_flags(Control.SIZE_EXPAND_FILL)
  109. parameter_container.add_child(param_box)
  110. var label = Label.new()
  111. label.text = "New Parameter"
  112. param_box.add_child(label)
  113. for field_key in parameter.keys():
  114. var field_value = parameter[field_key]
  115. var hbox = HBoxContainer.new()
  116. var namelabel = RichTextLabel.new()
  117. var namefield
  118. namelabel.text = field_key
  119. namelabel.custom_minimum_size.x = 250
  120. if field_value is bool:
  121. namefield = CheckBox.new()
  122. else:
  123. namefield = LineEdit.new()
  124. namefield.set_h_size_flags(Control.SIZE_EXPAND_FILL)
  125. hbox.add_child(namelabel)
  126. hbox.add_child(namefield)
  127. param_box.add_child(hbox)
  128. var delete_button = Button.new()
  129. delete_button.text = "Delete"
  130. delete_button.set_h_size_flags(Control.SIZE_EXPAND)
  131. delete_button.connect("pressed", Callable(self, "delete_param").bind(param_box))
  132. param_box.add_child(delete_button)
  133. var margin = MarginContainer.new()
  134. margin.add_theme_constant_override("margin_bottom", 5)
  135. param_box.add_child(margin)
  136. func save_node(is_new: bool) -> void:
  137. var key = $HBoxContainer/VBoxContainer2/HBoxContainer/key.text.strip_edges()
  138. if key == "":
  139. printerr("Key is empty, cannot save")
  140. return
  141. var info = {
  142. "category": $HBoxContainer/VBoxContainer2/HBoxContainer2/category.text,
  143. "subcategory": $HBoxContainer/VBoxContainer2/HBoxContainer3/subcategory.text,
  144. "title": $HBoxContainer/VBoxContainer2/HBoxContainer4/title.text,
  145. "short_description": $HBoxContainer/VBoxContainer2/HBoxContainer5/shortdescription.text,
  146. "description": $HBoxContainer/VBoxContainer2/HBoxContainer7/longdescription.text,
  147. "stereo": $HBoxContainer/VBoxContainer2/HBoxContainer6/stereo.button_pressed,
  148. "inputtype": $HBoxContainer/VBoxContainer2/HBoxContainer9/inputtype.text,
  149. "outputtype": $HBoxContainer/VBoxContainer2/HBoxContainer11/outputtype.text,
  150. "parameters": {}
  151. }
  152. for param_box in parameter_container.get_children():
  153. var children = param_box.get_children()
  154. if children.size() < 2:
  155. continue
  156. var param_data = {}
  157. var param_label = children[0] as Label
  158. var param_id = "param" + str(parameter_container.get_children().find(param_box) + 1)
  159. for i in range(1, children.size()):
  160. var node = children[i]
  161. if node is HBoxContainer and node.get_child_count() >= 2:
  162. var field_name = node.get_child(0).text
  163. var input_field = node.get_child(1)
  164. var field_value
  165. if input_field is CheckBox:
  166. field_value = input_field.button_pressed
  167. else:
  168. var raw_text = input_field.text
  169. if raw_text.is_valid_float():
  170. field_value = raw_text.to_float()
  171. elif raw_text.is_valid_int():
  172. field_value = raw_text.to_int()
  173. elif raw_text.to_lower() == "true":
  174. field_value = true
  175. elif raw_text.to_lower() == "false":
  176. field_value = false
  177. else:
  178. field_value = raw_text
  179. param_data[field_name] = field_value
  180. if param_data.size() > 0:
  181. info["parameters"][param_id] = param_data
  182. # Save or update entry
  183. node_data[key] = info
  184. # Write to file
  185. var file = FileAccess.open(json, FileAccess.WRITE)
  186. if file:
  187. file.store_string(JSON.stringify(node_data, "\t")) # pretty print with tab indent
  188. file.close()
  189. fill_search("") # refresh list
  190. $HBoxContainer/VBoxContainer/search/MarginContainer/VBoxContainer/SearchBar.text = ""
  191. func _on_save_changes_button_down() -> void:
  192. save_node(false)
  193. func _on_save_new_button_down() -> void:
  194. save_node(true)
  195. func _on_delete_process_button_down() -> void:
  196. var key = $HBoxContainer/VBoxContainer2/HBoxContainer/key.text.strip_edges()
  197. if key == "":
  198. printerr("No key entered – cannot delete.")
  199. return
  200. if not node_data.has(key):
  201. printerr("Key '%s' not found in JSON." % key)
  202. return
  203. # Remove entry from the dictionary
  204. node_data.erase(key)
  205. # Save updated JSON to file
  206. var file = FileAccess.open(json, FileAccess.WRITE)
  207. if file:
  208. file.store_string(JSON.stringify(node_data, "\t")) # pretty print
  209. file.close()
  210. print("Deleted entry: ", key)
  211. # refresh the list
  212. fill_search("")
  213. $HBoxContainer/VBoxContainer/search/MarginContainer/VBoxContainer/SearchBar.text = ""
  214. _on_new_process_button_down()
  215. func _on_new_process_button_down() -> void:
  216. $HBoxContainer/VBoxContainer2/HBoxContainer/key.text = ""
  217. $HBoxContainer/VBoxContainer2/HBoxContainer2/category.text = ""
  218. $HBoxContainer/VBoxContainer2/HBoxContainer3/subcategory.text = ""
  219. $HBoxContainer/VBoxContainer2/HBoxContainer4/title.text = ""
  220. $HBoxContainer/VBoxContainer2/HBoxContainer5/shortdescription.text = ""
  221. $HBoxContainer/VBoxContainer2/HBoxContainer7/longdescription.text = ""
  222. $HBoxContainer/VBoxContainer2/HBoxContainer6/stereo.button_pressed = false
  223. for child in parameter_container.get_children():
  224. child.queue_free()
  225. func _on_sort_json_button_down() -> void:
  226. var json_to_sort = ProjectSettings.globalize_path(json)
  227. var python_script = ProjectSettings.globalize_path("res://dev_tools/helpers/sort_json.py")
  228. print(json_to_sort)
  229. print(python_script)
  230. # Run the Python script with the JSON path as an argument
  231. var output = []
  232. var exit_code = OS.execute("cmd.exe", ["/c", python_script, json_to_sort], output, true)
  233. # Optionally print the output or check the result
  234. print("Exit code: ", exit_code)
  235. print("Output:\n", output)
  236. fill_search("") # refresh list
  237. $HBoxContainer/VBoxContainer/search/MarginContainer/VBoxContainer/SearchBar.text = ""
  238. load_json()