json_editor.gd 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. extends Control
  2. var node_data = {} #stores json file
  3. @onready var parameter_container = $HBoxContainer/VBoxContainer2/ScrollContainer/parameter_container
  4. var json = "res://dev_tools/json_editor/process_help_copy.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. for child in parameter_container.get_children():
  63. child.queue_free()
  64. var count = 1
  65. for param_key in parameters.keys():
  66. var param_box = VBoxContainer.new()
  67. param_box.set_h_size_flags(Control.SIZE_EXPAND_FILL)
  68. parameter_container.add_child(param_box)
  69. var label = Label.new()
  70. label.text = "Parameter " + str(count)
  71. param_box.add_child(label)
  72. var param_data = parameters[param_key]
  73. for field_key in param_data.keys():
  74. var field_value = param_data[field_key]
  75. var hbox = HBoxContainer.new()
  76. var namelabel = RichTextLabel.new()
  77. var namefield
  78. namelabel.text = field_key
  79. namelabel.custom_minimum_size.x = 250
  80. if field_value is bool:
  81. namefield = CheckBox.new()
  82. namefield.button_pressed = field_value
  83. else:
  84. namefield = LineEdit.new()
  85. namefield.text = str(field_value)
  86. namefield.set_h_size_flags(Control.SIZE_EXPAND_FILL)
  87. hbox.add_child(namelabel)
  88. hbox.add_child(namefield)
  89. param_box.add_child(hbox)
  90. var delete_button = Button.new()
  91. delete_button.text = "Delete " + param_data.get("paramname", "")
  92. delete_button.set_h_size_flags(Control.SIZE_EXPAND)
  93. delete_button.connect("pressed", Callable(self, "delete_param").bind(param_box))
  94. param_box.add_child(delete_button)
  95. var margin = MarginContainer.new()
  96. margin.add_theme_constant_override("margin_bottom", 5)
  97. param_box.add_child(margin)
  98. count += 1
  99. func delete_param(container: VBoxContainer):
  100. container.queue_free()
  101. func _on_button_button_down() -> void:
  102. var info = node_data["distort_replace"]
  103. var parameters = info.get("parameters", {})
  104. var parameter = parameters.get("param1", {})
  105. var param_box = VBoxContainer.new()
  106. param_box.set_h_size_flags(Control.SIZE_EXPAND_FILL)
  107. parameter_container.add_child(param_box)
  108. var label = Label.new()
  109. label.text = "New Parameter"
  110. param_box.add_child(label)
  111. for field_key in parameter.keys():
  112. var field_value = parameter[field_key]
  113. var hbox = HBoxContainer.new()
  114. var namelabel = RichTextLabel.new()
  115. var namefield
  116. namelabel.text = field_key
  117. namelabel.custom_minimum_size.x = 250
  118. if field_value is bool:
  119. namefield = CheckBox.new()
  120. else:
  121. namefield = LineEdit.new()
  122. namefield.set_h_size_flags(Control.SIZE_EXPAND_FILL)
  123. hbox.add_child(namelabel)
  124. hbox.add_child(namefield)
  125. param_box.add_child(hbox)
  126. var delete_button = Button.new()
  127. delete_button.text = "Delete"
  128. delete_button.set_h_size_flags(Control.SIZE_EXPAND)
  129. delete_button.connect("pressed", Callable(self, "delete_param").bind(param_box))
  130. param_box.add_child(delete_button)
  131. var margin = MarginContainer.new()
  132. margin.add_theme_constant_override("margin_bottom", 5)
  133. param_box.add_child(margin)
  134. func save_node(is_new: bool) -> void:
  135. var key = $HBoxContainer/VBoxContainer2/HBoxContainer/key.text.strip_edges()
  136. if key == "":
  137. printerr("Key is empty, cannot save")
  138. return
  139. var info = {
  140. "category": $HBoxContainer/VBoxContainer2/HBoxContainer2/category.text,
  141. "subcategory": $HBoxContainer/VBoxContainer2/HBoxContainer3/subcategory.text,
  142. "title": $HBoxContainer/VBoxContainer2/HBoxContainer4/title.text,
  143. "short_description": $HBoxContainer/VBoxContainer2/HBoxContainer5/shortdescription.text,
  144. "description": $HBoxContainer/VBoxContainer2/HBoxContainer7/longdescription.text,
  145. "stereo": $HBoxContainer/VBoxContainer2/HBoxContainer6/stereo.button_pressed,
  146. "parameters": {}
  147. }
  148. for param_box in parameter_container.get_children():
  149. var children = param_box.get_children()
  150. if children.size() < 2:
  151. continue
  152. var param_data = {}
  153. var param_label = children[0] as Label
  154. var param_id = "param" + str(parameter_container.get_children().find(param_box) + 1)
  155. for i in range(1, children.size()):
  156. var node = children[i]
  157. if node is HBoxContainer and node.get_child_count() >= 2:
  158. var field_name = node.get_child(0).text
  159. var input_field = node.get_child(1)
  160. var field_value
  161. if input_field is CheckBox:
  162. field_value = input_field.button_pressed
  163. else:
  164. var raw_text = input_field.text
  165. if raw_text.is_valid_float():
  166. field_value = raw_text.to_float()
  167. elif raw_text.is_valid_int():
  168. field_value = raw_text.to_int()
  169. elif raw_text.to_lower() == "true":
  170. field_value = true
  171. elif raw_text.to_lower() == "false":
  172. field_value = false
  173. else:
  174. field_value = raw_text
  175. param_data[field_name] = field_value
  176. if param_data.size() > 0:
  177. info["parameters"][param_id] = param_data
  178. # Save or update entry
  179. node_data[key] = info
  180. # Write to file
  181. var file = FileAccess.open(json, FileAccess.WRITE)
  182. if file:
  183. file.store_string(JSON.stringify(node_data, "\t")) # pretty print with tab indent
  184. file.close()
  185. fill_search("") # refresh list
  186. $HBoxContainer/VBoxContainer/search/MarginContainer/VBoxContainer/SearchBar.text = ""
  187. func _on_save_changes_button_down() -> void:
  188. save_node(false)
  189. func _on_save_new_button_down() -> void:
  190. save_node(true)
  191. func _on_delete_process_button_down() -> void:
  192. var key = $HBoxContainer/VBoxContainer2/HBoxContainer/key.text.strip_edges()
  193. if key == "":
  194. printerr("No key entered – cannot delete.")
  195. return
  196. if not node_data.has(key):
  197. printerr("Key '%s' not found in JSON." % key)
  198. return
  199. # Remove entry from the dictionary
  200. node_data.erase(key)
  201. # Save updated JSON to file
  202. var file = FileAccess.open(json, FileAccess.WRITE)
  203. if file:
  204. file.store_string(JSON.stringify(node_data, "\t")) # pretty print
  205. file.close()
  206. print("Deleted entry: ", key)
  207. # refresh the list
  208. fill_search("")
  209. $HBoxContainer/VBoxContainer/search/MarginContainer/VBoxContainer/SearchBar.text = ""
  210. _on_new_process_button_down()
  211. func _on_new_process_button_down() -> void:
  212. $HBoxContainer/VBoxContainer2/HBoxContainer/key.text = ""
  213. $HBoxContainer/VBoxContainer2/HBoxContainer2/category.text = ""
  214. $HBoxContainer/VBoxContainer2/HBoxContainer3/subcategory.text = ""
  215. $HBoxContainer/VBoxContainer2/HBoxContainer4/title.text = ""
  216. $HBoxContainer/VBoxContainer2/HBoxContainer5/shortdescription.text = ""
  217. $HBoxContainer/VBoxContainer2/HBoxContainer7/longdescription.text = ""
  218. $HBoxContainer/VBoxContainer2/HBoxContainer6/stereo.button_pressed = false
  219. for child in parameter_container.get_children():
  220. child.queue_free()
  221. func _on_sort_json_button_down() -> void:
  222. var json_to_sort = ProjectSettings.globalize_path(json)
  223. var python_script = ProjectSettings.globalize_path("res://dev_tools/helpers/sort_json.py")
  224. print(json_to_sort)
  225. print(python_script)
  226. # Run the Python script with the JSON path as an argument
  227. var output = []
  228. var exit_code = OS.execute("cmd.exe", ["/c", python_script, json_to_sort], output, true)
  229. # Optionally print the output or check the result
  230. print("Exit code: ", exit_code)
  231. print("Output:\n", output)
  232. fill_search("") # refresh list
  233. $HBoxContainer/VBoxContainer/search/MarginContainer/VBoxContainer/SearchBar.text = ""
  234. load_json()