ResourcePackConfigMenu.gd 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. extends Control
  2. var config_json := {}
  3. const RESOURCE_PACK_CONFIG_OPTION_NODE = preload("uid://c5ea03ob6ncq7")
  4. signal closed
  5. var selected_index := 0
  6. var active := false
  7. var json_path := ""
  8. func open() -> void:
  9. if active: return
  10. clear_options()
  11. spawn_options()
  12. show()
  13. await get_tree().process_frame
  14. %Options.active = true
  15. active = true
  16. func clear_options() -> void:
  17. for i in %Options.options:
  18. i.queue_free()
  19. %Options.options.clear()
  20. func _process(_delta: float) -> void:
  21. if Input.is_action_just_pressed("ui_back") and active:
  22. close()
  23. func spawn_options() -> void:
  24. for i in config_json.options:
  25. var node = RESOURCE_PACK_CONFIG_OPTION_NODE.instantiate()
  26. node.config_name = i
  27. if config_json.options[i] is bool:
  28. node.values = ["SETTING_OFF", "SETTING_ON"]
  29. node.selected_index = int(config_json.options[i])
  30. node.is_bool = true
  31. else:
  32. node.values = config_json.value_keys[i]
  33. node.selected_index = config_json.value_keys[i].find(config_json.options[i])
  34. %Options.add_child(node)
  35. node.value_changed.connect(value_changed)
  36. %Options.options.append(node)
  37. func value_changed(option: PackConfigOption) -> void:
  38. if option.is_bool:
  39. config_json.options[option.config_name] = bool(option.selected_index)
  40. else:
  41. config_json.options[option.config_name] = option.values[option.selected_index]
  42. update_json()
  43. func update_json() -> void:
  44. var file = FileAccess.open(json_path, FileAccess.WRITE)
  45. file.store_string(JSON.stringify(config_json, "\t", false))
  46. file.close()
  47. func close() -> void:
  48. ResourceSetter.cache.clear()
  49. ResourceSetterNew.cache.clear()
  50. Global.level_theme_changed.emit()
  51. closed.emit()
  52. clear_options()
  53. hide()
  54. %Options.active = false
  55. active = false