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