| | 1 | | using DCL.Helpers; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | public class ColorPickerComponentView : BaseComponentView, IComponentModelConfig<ColorPickerComponentModel> |
| | 9 | | { |
| | 10 | | [SerializeField] private SliderComponentView sliderHue; |
| | 11 | | [SerializeField] private SliderComponentView sliderSaturation; |
| | 12 | | [SerializeField] private SliderComponentView sliderValue; |
| | 13 | | [SerializeField] private Button toggleButton; |
| | 14 | | [SerializeField] private GameObject container; |
| | 15 | | [SerializeField] private Image containerImage; |
| | 16 | | [SerializeField] private GameObject colorSelectorObject; |
| | 17 | | [SerializeField] private Image colorPreviewImage; |
| | 18 | | [SerializeField] private GameObject arrowUpMark; |
| | 19 | | [SerializeField] private GameObject arrowDownMark; |
| | 20 | |
|
| | 21 | | [Header("Configuration")] |
| | 22 | | [SerializeField] internal ColorPickerComponentModel model; |
| | 23 | |
|
| 2 | 24 | | public bool IsShowingOnlyPresetColors => model.showOnlyPresetColors; |
| 2 | 25 | | public List<Color> ColorList => model.colorList; |
| 2 | 26 | | public Color CurrentSelectedColor { get; private set; } |
| | 27 | |
|
| | 28 | | public event Action<Color> OnColorChanged; |
| | 29 | | public event Action OnColorPickerToggle; |
| | 30 | |
|
| | 31 | | private IColorSelector colorSelector; |
| | 32 | | private bool isAudioPlaying; |
| | 33 | |
|
| | 34 | | public override void Awake() |
| | 35 | | { |
| 35 | 36 | | base.Awake(); |
| | 37 | |
|
| 35 | 38 | | colorSelector = colorSelectorObject.GetComponent<IColorSelector>(); |
| 35 | 39 | | colorSelector.OnColorSelectorChange += UpdateValueFromColorSelector; |
| | 40 | |
|
| 35 | 41 | | sliderHue.onSliderChange.AddListener(_ => SetColor()); |
| 35 | 42 | | sliderHue.onIncrement.AddListener(() => ChangeProperty("hue", model.incrementAmount)); |
| 35 | 43 | | sliderHue.onDecrement.AddListener(() => ChangeProperty("hue", -model.incrementAmount)); |
| | 44 | |
|
| 35 | 45 | | sliderSaturation.onSliderChange.AddListener(_ => SetColor()); |
| 35 | 46 | | sliderSaturation.onIncrement.AddListener(() => ChangeProperty("sat", model.incrementAmount)); |
| 35 | 47 | | sliderSaturation.onDecrement.AddListener(() => ChangeProperty("sat", -model.incrementAmount)); |
| | 48 | |
|
| 35 | 49 | | sliderValue.onSliderChange.AddListener(_ => SetColor()); |
| 35 | 50 | | sliderValue.onIncrement.AddListener(() => ChangeProperty("val", model.incrementAmount)); |
| 35 | 51 | | sliderValue.onDecrement.AddListener(() => ChangeProperty("val", -model.incrementAmount)); |
| | 52 | |
|
| 35 | 53 | | toggleButton.onClick.AddListener(() => SetActive(!container.activeInHierarchy)); |
| | 54 | |
|
| 35 | 55 | | SetActive(false); |
| 35 | 56 | | } |
| | 57 | |
|
| | 58 | | public override void RefreshControl() |
| | 59 | | { |
| 0 | 60 | | if (model == null) |
| 0 | 61 | | return; |
| | 62 | |
|
| 0 | 63 | | SetColorList(model.colorList); |
| 0 | 64 | | SetIncrementAmount(model.incrementAmount); |
| 0 | 65 | | SetShowOnlyPresetColors(model.showOnlyPresetColors); |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | public void SetColorSelector(Color newColor) |
| | 69 | | { |
| 1 | 70 | | colorSelector.Select(newColor); |
| 1 | 71 | | CurrentSelectedColor = newColor; |
| 1 | 72 | | } |
| | 73 | |
|
| | 74 | | public void SetColorList(List<Color> colorList) |
| | 75 | | { |
| 2 | 76 | | model.colorList = colorList; |
| 2 | 77 | | colorSelector ??= colorSelectorObject.GetComponent<IColorSelector>(); |
| 2 | 78 | | colorSelector.Cleanup(); |
| 2 | 79 | | colorSelector.Populate(colorList); |
| 2 | 80 | | } |
| | 81 | |
|
| | 82 | | public void Configure(ColorPickerComponentModel newModel) |
| | 83 | | { |
| 0 | 84 | | model = newModel; |
| 0 | 85 | | RefreshControl(); |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | private void ChangeProperty(string a, float amount) |
| | 89 | | { |
| | 90 | | switch (a) |
| | 91 | | { |
| | 92 | | case "hue": |
| 0 | 93 | | sliderHue.AddValueToSlider(amount); |
| 0 | 94 | | CheckButtonInteractivity(sliderHue); |
| 0 | 95 | | break; |
| | 96 | | case "sat": |
| 0 | 97 | | sliderSaturation.AddValueToSlider(amount); |
| 0 | 98 | | CheckButtonInteractivity(sliderSaturation); |
| 0 | 99 | | break; |
| | 100 | | case "val": |
| 0 | 101 | | sliderValue.AddValueToSlider(amount); |
| 0 | 102 | | CheckButtonInteractivity(sliderValue); |
| | 103 | | break; |
| | 104 | | } |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | private static void CheckButtonInteractivity(SliderComponentView sliderComponent) |
| | 108 | | { |
| 0 | 109 | | sliderComponent.incrementButton.interactable = sliderComponent.slider.value < sliderComponent.slider.maxValue; |
| 0 | 110 | | sliderComponent.decrementButton.interactable = sliderComponent.slider.value > sliderComponent.slider.minValue; |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | private void UpdateValueFromColorSelector(Color newColor) |
| | 114 | | { |
| 0 | 115 | | UpdateSliderValues(newColor); |
| 0 | 116 | | SetColor(); |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | public void UpdateSliderValues(Color currentColor) |
| | 120 | | { |
| 1 | 121 | | Color.RGBToHSV(currentColor, out float h, out float s, out float v); |
| 1 | 122 | | colorPreviewImage.color = currentColor; |
| 1 | 123 | | sliderHue.slider.SetValueWithoutNotify(h); |
| 1 | 124 | | sliderSaturation.slider.SetValueWithoutNotify(s); |
| 1 | 125 | | sliderValue.slider.SetValueWithoutNotify(v); |
| 1 | 126 | | } |
| | 127 | |
|
| | 128 | | private void SetColor() |
| | 129 | | { |
| 0 | 130 | | Color newColor = Color.HSVToRGB(sliderHue.slider.value, sliderSaturation.slider.value, sliderValue.slider.value) |
| 0 | 131 | | colorPreviewImage.color = newColor; |
| 0 | 132 | | CheckButtonInteractivity(sliderHue); |
| 0 | 133 | | CheckButtonInteractivity(sliderSaturation); |
| 0 | 134 | | CheckButtonInteractivity(sliderValue); |
| 0 | 135 | | if (!isAudioPlaying) |
| 0 | 136 | | StartCoroutine(PlaySound()); |
| | 137 | |
|
| 0 | 138 | | OnColorChanged?.Invoke(newColor); |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | private IEnumerator PlaySound() |
| | 142 | | { |
| 0 | 143 | | isAudioPlaying = true; |
| 0 | 144 | | AudioScriptableObjects.buttonRelease.Play(true); |
| 0 | 145 | | yield return new WaitForSeconds(0.05f); |
| 0 | 146 | | isAudioPlaying = false; |
| 0 | 147 | | } |
| | 148 | |
|
| | 149 | | public void SetActive(bool isActive) |
| | 150 | | { |
| 36 | 151 | | container.SetActive(isActive); |
| 36 | 152 | | containerImage.enabled = isActive; |
| | 153 | |
|
| 36 | 154 | | if (arrowUpMark != null) |
| 36 | 155 | | arrowUpMark.SetActive(isActive); |
| | 156 | |
|
| 36 | 157 | | if (arrowDownMark != null) |
| 36 | 158 | | arrowDownMark.SetActive(!isActive); |
| | 159 | |
|
| 36 | 160 | | if (isActive) |
| | 161 | | { |
| 0 | 162 | | OnColorPickerToggle?.Invoke(); |
| 0 | 163 | | RebuildLayout(); |
| | 164 | | } |
| 36 | 165 | | } |
| | 166 | |
|
| | 167 | | public override void Dispose() |
| | 168 | | { |
| 35 | 169 | | base.Dispose(); |
| 35 | 170 | | toggleButton.onClick.RemoveAllListeners(); |
| 35 | 171 | | } |
| | 172 | |
|
| | 173 | | public void SetIncrementAmount(float amount) => |
| 0 | 174 | | model.incrementAmount = amount; |
| | 175 | |
|
| | 176 | | public void SetShowOnlyPresetColors(bool showOnlyPresetColors) |
| | 177 | | { |
| 2 | 178 | | model.showOnlyPresetColors = showOnlyPresetColors; |
| | 179 | |
|
| 2 | 180 | | sliderHue.gameObject.SetActive(!showOnlyPresetColors); |
| 2 | 181 | | sliderSaturation.gameObject.SetActive(!showOnlyPresetColors); |
| 2 | 182 | | sliderValue.gameObject.SetActive(!showOnlyPresetColors); |
| | 183 | |
|
| 2 | 184 | | RebuildLayout(); |
| 2 | 185 | | } |
| | 186 | |
|
| | 187 | | private void RebuildLayout() => |
| 2 | 188 | | Utils.ForceRebuildLayoutImmediate(transform as RectTransform); |
| | 189 | | } |