| | 1 | | using System; |
| | 2 | | using DCL.SettingsCommon.SettingsControllers.BaseControllers; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | namespace DCL.SettingsPanelHUD.Controls |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// MonoBehaviour that represents the view of a SLIDER type CONTROL. |
| | 11 | | /// </summary> |
| | 12 | | public class SliderSettingsControlView : SettingsControlView |
| | 13 | | { |
| | 14 | | [SerializeField] private Slider slider; |
| | 15 | | [SerializeField] private TextMeshProUGUI indicatorLabel; |
| | 16 | |
|
| 0 | 17 | | public Slider sliderControl => slider; |
| | 18 | |
|
| | 19 | | private SliderControlModel sliderControlConfig; |
| | 20 | | private SliderSettingsControlController sliderController; |
| | 21 | |
|
| | 22 | | public override void Initialize(SettingsControlModel controlConfig, SettingsControlController settingsControlCon |
| | 23 | | { |
| 17 | 24 | | sliderController = (SliderSettingsControlController)settingsControlController; |
| 17 | 25 | | sliderController.OnIndicatorLabelChange += OverrideIndicatorLabel; |
| | 26 | |
|
| 17 | 27 | | this.sliderControlConfig = (SliderControlModel)controlConfig; |
| 17 | 28 | | slider.maxValue = this.sliderControlConfig.sliderMaxValue; |
| 17 | 29 | | slider.minValue = this.sliderControlConfig.sliderMinValue; |
| 17 | 30 | | slider.wholeNumbers = this.sliderControlConfig.wholeNumbers; |
| | 31 | |
|
| 17 | 32 | | base.Initialize(controlConfig, sliderController); |
| 17 | 33 | | OverrideIndicatorLabel(slider.value.ToString()); |
| 17 | 34 | | sliderController.UpdateSetting(this.sliderControlConfig.storeValueAsNormalized ? RemapSliderValueTo01(slider |
| | 35 | |
|
| 17 | 36 | | slider.onValueChanged.AddListener(sliderValue => |
| | 37 | | { |
| | 38 | | // https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings |
| | 39 | | // FX = floating point with X precision digits |
| 0 | 40 | | string stringFormat = sliderControlConfig.wholeNumbers ? "F0" : "F1"; |
| | 41 | |
|
| 0 | 42 | | OverrideIndicatorLabel( sliderValue.ToString(stringFormat) ); |
| 0 | 43 | | ApplySetting(this.sliderControlConfig.storeValueAsNormalized ? RemapSliderValueTo01(sliderValue) : slide |
| 0 | 44 | | }); |
| 17 | 45 | | } |
| | 46 | |
|
| | 47 | | protected override void OnDestroy() |
| | 48 | | { |
| 18 | 49 | | base.OnDestroy(); |
| | 50 | |
|
| 18 | 51 | | if (sliderController != null) |
| 17 | 52 | | sliderController.OnIndicatorLabelChange -= OverrideIndicatorLabel; |
| 18 | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Overrides the text of the label associated to the slider. |
| | 57 | | /// </summary> |
| | 58 | | /// <param name="text">New label text.</param> |
| 46 | 59 | | public void OverrideIndicatorLabel(string text) { indicatorLabel.text = text; } |
| | 60 | |
|
| | 61 | | public override void RefreshControl() |
| | 62 | | { |
| 17 | 63 | | base.RefreshControl(); |
| | 64 | |
|
| 17 | 65 | | float storedValue = Convert.ToSingle( sliderController.GetStoredValue() ); |
| | 66 | |
|
| 17 | 67 | | float newValue = sliderControlConfig.storeValueAsNormalized ? RemapNormalizedValueToSlider(storedValue) : st |
| | 68 | |
|
| 17 | 69 | | if (slider.value != newValue) |
| 16 | 70 | | slider.value = newValue; |
| 17 | 71 | | } |
| | 72 | |
|
| | 73 | | private float RemapSliderValueTo01(float value) |
| | 74 | | { |
| 1 | 75 | | return (value - slider.minValue) |
| | 76 | | / (slider.maxValue - slider.minValue) |
| | 77 | | * (1 - 0) + 0; //(value - from1) / (to1 - from1) * (to2 - from2) + from2 |
| | 78 | | } |
| | 79 | |
|
| 1 | 80 | | private float RemapNormalizedValueToSlider(float value) { return Mathf.Lerp(slider.minValue, slider.maxValue, va |
| | 81 | | } |
| | 82 | | } |