Browse Source

finished up json editor

Jonathan Higgins 6 months ago
parent
commit
b523a6c003

+ 51 - 14
dev_tools/helpers/sort_json.py

@@ -1,30 +1,67 @@
 import json
+import sys
+from collections import OrderedDict
 
-# Load the JSON
-with open("process_help.json", "r", encoding="utf-8") as f:
-    data = json.load(f)
+# --- Custom category order ---
+CATEGORY_ORDER = ["time", "pvoc", "utility"]
 
-# Convert dict to list of items with their keys
-items = [
-    {"key": k, **v}
-    for k, v in data.items()
+# --- Desired param key order ---
+PARAM_ORDER = [
+    "paramname",
+    "paramdescription",
+    "automatable",
+    "time",
+    "min",
+    "max",
+    "flag",
+    "minrange",
+    "maxrange",
+    "step",
+    "value",
+    "exponential",
+    "uitype"
 ]
 
-# Sort by subcategory first, then by title
+# --- Load JSON path from args ---
+if len(sys.argv) < 2:
+    print("Usage: python sort_json.py path_to_json")
+    sys.exit(1)
+
+json_path = sys.argv[1]
+
+with open(json_path, "r", encoding="utf-8") as f:
+    data = json.load(f)
+
+# --- Reorder parameter fields ---
+def reorder_param_fields(param):
+    return OrderedDict((k, param[k]) for k in PARAM_ORDER if k in param)
+
+# --- Convert to sortable items ---
+items = [{"key": k, **v} for k, v in data.items()]
+
+# --- Sort by category, subcategory, title ---
 items_sorted = sorted(
     items,
     key=lambda item: (
-	item.get("category", "").lower(),
+        CATEGORY_ORDER.index(item.get("category", "")) if item.get("category", "") in CATEGORY_ORDER else 999,
         item.get("subcategory", "").lower(),
         item.get("title", "").lower()
     )
 )
 
-# Convert back to dict, using the original keys (still sorted)
-sorted_data = {item["key"]: {k: item[k] for k in item if k != "key"} for item in items_sorted}
+# --- Rebuild with sorted parameter fields ---
+sorted_data = {}
+for item in items_sorted:
+    key = item.pop("key")
+    if "parameters" in item:
+        item["parameters"] = {
+            param_key: reorder_param_fields(param_val)
+            for param_key, param_val in item["parameters"].items()
+        }
+    sorted_data[key] = item
 
-# Write the sorted JSON back out
-with open("process_help_sorted.json", "w", encoding="utf-8") as f:
+# --- Overwrite the original file ---
+with open(json_path, "w", encoding="utf-8") as f:
     json.dump(sorted_data, f, indent=2, ensure_ascii=False)
 
-print("Sorted JSON saved to process_help_sorted.json")
+print(f"Sorted JSON saved to {json_path}")

+ 142 - 5
dev_tools/json_editor/json_editor.gd

@@ -2,17 +2,20 @@ extends Control
 
 var node_data = {} #stores json file
 @onready var parameter_container = $HBoxContainer/VBoxContainer2/ScrollContainer/parameter_container
+var json = "res://dev_tools/json_editor/process_help_copy.json"
 
 func _ready() -> void:
 	Nodes.hide()
 		
 	load_json()
-	fill_search("")
+	
 	
 func load_json():
-	var file = FileAccess.open("res://scenes/main/process_help.json", FileAccess.READ)
+	var file = FileAccess.open(json, FileAccess.READ)
 	if file:
 		node_data = JSON.parse_string(file.get_as_text())
+		
+	fill_search("")
 
 
 func fill_search(filter: String):
@@ -132,14 +135,13 @@ func delete_param(container: VBoxContainer):
 	container.queue_free()
 
 
+
+
 func _on_button_button_down() -> void:
 	var info = node_data["distort_replace"]
 	var parameters = info.get("parameters", {})
 	var parameter = parameters.get("param1", {})
 	
-	print(info)
-	print(parameter)
-	
 	var param_box = VBoxContainer.new()
 	param_box.set_h_size_flags(Control.SIZE_EXPAND_FILL)
 	parameter_container.add_child(param_box)
@@ -180,6 +182,141 @@ func _on_button_button_down() -> void:
 	margin.add_theme_constant_override("margin_bottom", 5)
 	param_box.add_child(margin)
 	
+
+func save_node(is_new: bool) -> void:
+	var key = $HBoxContainer/VBoxContainer2/HBoxContainer/key.text.strip_edges()
+	if key == "":
+		printerr("Key is empty, cannot save")
+		return
+
+	var info = {
+		"category": $HBoxContainer/VBoxContainer2/HBoxContainer2/category.text,
+		"subcategory": $HBoxContainer/VBoxContainer2/HBoxContainer3/subcategory.text,
+		"title": $HBoxContainer/VBoxContainer2/HBoxContainer4/title.text,
+		"short_description": $HBoxContainer/VBoxContainer2/HBoxContainer5/shortdescription.text,
+		"description": $HBoxContainer/VBoxContainer2/HBoxContainer7/longdescription.text,
+		"stereo": $HBoxContainer/VBoxContainer2/HBoxContainer6/stereo.button_pressed,
+		"parameters": {}
+	}
+
+	for param_box in parameter_container.get_children():
+		var children = param_box.get_children()
+		if children.size() < 2:
+			continue
+
+		var param_data = {}
+		var param_label = children[0] as Label
+		var param_id = "param" + str(parameter_container.get_children().find(param_box) + 1)
+
+		for i in range(1, children.size()):
+			var node = children[i]
+			if node is HBoxContainer and node.get_child_count() >= 2:
+				var field_name = node.get_child(0).text
+				var input_field = node.get_child(1)
+				var field_value
+
+				if input_field is CheckBox:
+					field_value = input_field.button_pressed
+				else:
+					var raw_text = input_field.text
+					if raw_text.is_valid_float():
+						field_value = raw_text.to_float()
+					elif raw_text.is_valid_int():
+						field_value = raw_text.to_int()
+					elif raw_text.to_lower() == "true":
+						field_value = true
+					elif raw_text.to_lower() == "false":
+						field_value = false
+					else:
+						field_value = raw_text
+
+				param_data[field_name] = field_value
+
+		if param_data.size() > 0:
+			info["parameters"][param_id] = param_data
+
+	# Save or update entry
+	node_data[key] = info
+
+	# Write to file
+	var file = FileAccess.open(json, FileAccess.WRITE)
+	if file:
+		file.store_string(JSON.stringify(node_data, "\t"))  # pretty print with tab indent
+		file.close()
+
+	fill_search("")  # refresh list
+	$HBoxContainer/VBoxContainer/search/MarginContainer/VBoxContainer/SearchBar.text = ""
 	
 	
+
+
+
+func _on_save_changes_button_down() -> void:
+	save_node(false)
+
+
+func _on_save_new_button_down() -> void:
+	save_node(true)
+
+
+func _on_delete_process_button_down() -> void:
+	var key = $HBoxContainer/VBoxContainer2/HBoxContainer/key.text.strip_edges()
+	if key == "":
+		printerr("No key entered – cannot delete.")
+		return
 	
+	if not node_data.has(key):
+		printerr("Key '%s' not found in JSON." % key)
+		return
+
+	# Remove entry from the dictionary
+	node_data.erase(key)
+
+	# Save updated JSON to file
+	var file = FileAccess.open(json, FileAccess.WRITE)
+	if file:
+		file.store_string(JSON.stringify(node_data, "\t"))  # pretty print
+		file.close()
+
+	print("Deleted entry: ", key)
+
+	# refresh the list
+	fill_search("")
+	$HBoxContainer/VBoxContainer/search/MarginContainer/VBoxContainer/SearchBar.text = ""
+	_on_new_process_button_down()
+
+
+
+func _on_new_process_button_down() -> void:
+	$HBoxContainer/VBoxContainer2/HBoxContainer/key.text = ""
+	$HBoxContainer/VBoxContainer2/HBoxContainer2/category.text = ""
+	$HBoxContainer/VBoxContainer2/HBoxContainer3/subcategory.text = ""
+	$HBoxContainer/VBoxContainer2/HBoxContainer4/title.text = ""
+	$HBoxContainer/VBoxContainer2/HBoxContainer5/shortdescription.text = ""
+	$HBoxContainer/VBoxContainer2/HBoxContainer7/longdescription.text = ""
+	$HBoxContainer/VBoxContainer2/HBoxContainer6/stereo.button_pressed = false
+	
+	for child in parameter_container.get_children():
+			child.queue_free()
+	
+
+
+func _on_sort_json_button_down() -> void:
+	var json_to_sort = ProjectSettings.globalize_path(json)
+	var python_script = ProjectSettings.globalize_path("res://dev_tools/helpers/sort_json.py")
+	
+	print(json_to_sort)
+	print(python_script)
+
+	# Run the Python script with the JSON path as an argument
+	var output = []
+	var exit_code = OS.execute("cmd.exe", ["/c", python_script, json_to_sort], output, true)
+
+	# Optionally print the output or check the result
+	print("Exit code: ", exit_code)
+	print("Output:\n", output)
+	
+	fill_search("")  # refresh list
+	$HBoxContainer/VBoxContainer/search/MarginContainer/VBoxContainer/SearchBar.text = ""
+	
+	load_json()

+ 9 - 0
dev_tools/json_editor/json_editor.tscn

@@ -79,6 +79,10 @@ text = "Save Changes"
 layout_mode = 2
 text = "Save New"
 
+[node name="DeleteProcess" type="Button" parent="HBoxContainer/VBoxContainer"]
+layout_mode = 2
+text = "Delete Process"
+
 [node name="SortJSON" type="Button" parent="HBoxContainer/VBoxContainer"]
 layout_mode = 2
 text = "Sort JSON"
@@ -192,4 +196,9 @@ layout_mode = 2
 text = "Add a parameter"
 
 [connection signal="text_changed" from="HBoxContainer/VBoxContainer/search/MarginContainer/VBoxContainer/SearchBar" to="." method="_on_search_bar_text_changed"]
+[connection signal="button_down" from="HBoxContainer/VBoxContainer/NewProcess" to="." method="_on_new_process_button_down"]
+[connection signal="button_down" from="HBoxContainer/VBoxContainer/SaveChanges" to="." method="_on_save_changes_button_down"]
+[connection signal="button_down" from="HBoxContainer/VBoxContainer/SaveNew" to="." method="_on_save_new_button_down"]
+[connection signal="button_down" from="HBoxContainer/VBoxContainer/DeleteProcess" to="." method="_on_delete_process_button_down"]
+[connection signal="button_down" from="HBoxContainer/VBoxContainer/SortJSON" to="." method="_on_sort_json_button_down"]
 [connection signal="button_down" from="HBoxContainer/VBoxContainer2/Button" to="." method="_on_button_button_down"]

File diff suppressed because it is too large
+ 219 - 303
dev_tools/json_editor/process_help_copy.json


Some files were not shown because too many files changed in this diff