SliderConfigOptionUI.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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(delayedSave != null) StopCoroutine(delayedSave);
  53. delayedSave = StartCoroutine(Timer.StartRealtime(2f, SaveConfig));
  54. }
  55. private void ApplyConfig()
  56. {
  57. if(configOption == null) return;
  58. ConfigOptionData data = new ConfigOptionData("", "", 0, slider.value);
  59. configOption.SetValue(data, false);
  60. }
  61. private void SaveConfig()
  62. {
  63. if(configOption == null) return;
  64. ConfigOptionData data = new ConfigOptionData("", "", 0, slider.value);
  65. configOption.SetValue(data, true);
  66. }
  67. }
  68. }