json_editor.gd 11 KB

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