12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using KairoEngine.Core;
- using KairoEngine.Core.ConfigOptions;
- using KairoEngine.UI.Tooltips;
- using Sirenix.OdinInspector;
- using TMPro;
- namespace KairoEngine.UI.ConfigOptions
- {
- [HideMonoScript]
- public class SliderConfigOptionUI : MonoBehaviour
- {
- public TextMeshProUGUI titleText;
- public TooltipTrigger tooltipTrigger;
- public Slider slider;
- private Coroutine delayedSave;
- [ReadOnly] public ConfigOptionBase configOption;
- public void OnEnable()
- {
- slider.onValueChanged.AddListener(delegate {OnChange(); });
- }
- void OnDisable()
- {
- slider.onValueChanged.RemoveListener(delegate {OnChange(); });
- }
- public void Setup(ConfigOptionBase configOption)
- {
- this.configOption = configOption;
- var uiData = configOption.GetUiData();
- if(uiData == null)
- {
- Debug.LogError("Missing configOption", this.gameObject);
- return;
- }
- titleText.text = uiData.name;
- //tooltipTrigger.header = uiData.name;
- tooltipTrigger.content = uiData.description;
- if(uiData.sliderData == null)
- {
- Debug.LogError($"Missing sliderData in configOption for {uiData.name}", this.gameObject);
- return;
- }
- slider.minValue = uiData.sliderData.minValue;
- slider.maxValue = uiData.sliderData.maxValue;
- slider.value = uiData.sliderData.currentValue;
- }
- public void OnChange()
- {
- ApplyConfig();
- if(this.gameObject.activeInHierarchy == false) return;
- if(delayedSave != null) StopCoroutine(delayedSave);
- delayedSave = StartCoroutine(Timer.StartRealtime(2f, SaveConfig));
- }
- private void ApplyConfig()
- {
- if(configOption == null) return;
- ConfigOptionData data = new ConfigOptionData("", "", 0, slider.value);
- configOption.SetValue(data, false);
- }
- private void SaveConfig()
- {
- if(configOption == null) return;
- ConfigOptionData data = new ConfigOptionData("", "", 0, slider.value);
- configOption.SetValue(data, true);
- }
- }
- }
|