SliderConfigOptionUI.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using KairoEngine.Core;
  6. using KairoEngine.Core.ConfigOptions;
  7. using KairoEngine.UI.Tooltips;
  8. using Sirenix.OdinInspector;
  9. using TMPro;
  10. namespace KairoEngine.UI.ConfigOptions
  11. {
  12. [HideMonoScript]
  13. public class SliderConfigOptionUI : MonoBehaviour
  14. {
  15. public TextMeshProUGUI titleText;
  16. public TooltipTrigger tooltipTrigger;
  17. public Slider slider;
  18. private Coroutine delayedSave;
  19. [ReadOnly] public ConfigOptionBase configOption;
  20. public void OnEnable()
  21. {
  22. slider.onValueChanged.AddListener(delegate {OnChange(); });
  23. }
  24. void OnDisable()
  25. {
  26. slider.onValueChanged.RemoveListener(delegate {OnChange(); });
  27. }
  28. public void Setup(ConfigOptionBase configOption)
  29. {
  30. this.configOption = configOption;
  31. var uiData = configOption.GetUiData();
  32. if(uiData == null)
  33. {
  34. Debug.LogError("Missing configOption", this.gameObject);
  35. return;
  36. }
  37. titleText.text = uiData.name;
  38. //tooltipTrigger.header = uiData.name;
  39. tooltipTrigger.content = uiData.description;
  40. if(uiData.sliderData == null)
  41. {
  42. Debug.LogError($"Missing sliderData in configOption for {uiData.name}", this.gameObject);
  43. return;
  44. }
  45. slider.minValue = uiData.sliderData.minValue;
  46. slider.maxValue = uiData.sliderData.maxValue;
  47. slider.value = uiData.sliderData.currentValue;
  48. }
  49. public void OnChange()
  50. {
  51. ApplyConfig();
  52. if(this.gameObject.activeInHierarchy == false) return;
  53. if(delayedSave != null) StopCoroutine(delayedSave);
  54. delayedSave = StartCoroutine(Timer.StartRealtime(2f, SaveConfig));
  55. }
  56. private void ApplyConfig()
  57. {
  58. if(configOption == null) return;
  59. ConfigOptionData data = new ConfigOptionData("", "", 0, slider.value);
  60. configOption.SetValue(data, false);
  61. }
  62. private void SaveConfig()
  63. {
  64. if(configOption == null) return;
  65. ConfigOptionData data = new ConfigOptionData("", "", 0, slider.value);
  66. configOption.SetValue(data, true);
  67. }
  68. }
  69. }