< Summary

Class:ColorPickerComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/ColorPicker/ColorPickerComponentView.cs
Covered lines:14
Uncovered lines:59
Coverable lines:73
Total lines:177
Line coverage:19.1% (14 of 73)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%2100%
RefreshControl()0%6200%
SetColorSelector(...)0%110100%
SetColorList(...)0%220100%
Configure(...)0%2100%
ChangeProperty(...)0%20400%
CheckButtonInteractability(...)0%2100%
UpdateValueFromColorSelector(...)0%2100%
UpdateSliderValues(...)0%110100%
SetColor()0%6200%
PlaySound()0%12300%
SetActive(...)0%2100%
Dispose()0%2100%
SetIncrementAmount(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/ColorPicker/ColorPickerComponentView.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using DCL;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8public 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
 27public 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    {
 048        base.Awake();
 49
 050        colorSelector = colorSelectorObject.GetComponent<IColorSelector>();
 51
 052        colorSelector.OnColorSelectorChange += UpdateValueFromColorSelector;
 53
 054        sliderHue.onSliderChange.AddListener(hue => SetColor());
 055        sliderHue.onIncrement.AddListener(() => ChangeProperty("hue", model.incrementAmount));
 056        sliderHue.onDecrement.AddListener(() => ChangeProperty("hue", -model.incrementAmount));
 57
 058        sliderSaturation.onSliderChange.AddListener(saturation => SetColor());
 059        sliderSaturation.onIncrement.AddListener(() => ChangeProperty("sat", model.incrementAmount));
 060        sliderSaturation.onDecrement.AddListener(() => ChangeProperty("sat", -model.incrementAmount));
 61
 062        sliderValue.onSliderChange.AddListener(value => SetColor());
 063        sliderValue.onIncrement.AddListener(() => ChangeProperty("val", model.incrementAmount));
 064        sliderValue.onDecrement.AddListener(() => ChangeProperty("val", -model.incrementAmount));
 65
 066        toggleButton.onClick.AddListener(() => SetActive(!container.activeInHierarchy));
 67
 068        SetActive(false);
 069    }
 70
 71    public override void RefreshControl()
 72    {
 073        if (model == null)
 074            return;
 75
 076        SetColorList(model.colorList);
 077        SetIncrementAmount(model.incrementAmount);
 078    }
 79
 80    public void SetColorSelector(Color newColor)
 81    {
 34482        colorSelector.Select(newColor);
 34483    }
 84
 85    public void SetColorList(List<Color> colorList)
 86    {
 19287        if(colorSelector == null)
 19288            colorSelector = colorSelectorObject.GetComponent<IColorSelector>();
 89
 19290        model.colorList = colorList;
 19291        colorSelector.Cleanup();
 19292        colorSelector.Populate(colorList);
 19293    }
 94
 95    public void Configure(ColorPickerComponentModel newModel)
 96    {
 097        model = newModel;
 098        RefreshControl();
 099    }
 100
 101    private void ChangeProperty(string a, float amount)
 102    {
 103        switch (a)
 104        {
 105            case "hue":
 0106                sliderHue.AddValueToSlider(amount);
 0107                CheckButtonInteractability(sliderHue);
 0108                break;
 109            case "sat":
 0110                sliderSaturation.AddValueToSlider(amount);
 0111                CheckButtonInteractability(sliderSaturation);
 0112                break;
 113            case "val":
 0114                sliderValue.AddValueToSlider(amount);
 0115                CheckButtonInteractability(sliderValue);
 116                break;
 117            default:
 118                break;
 119        }
 0120    }
 121
 122    private void CheckButtonInteractability(SliderComponentView sliderComponent)
 123    {
 0124        sliderComponent.incrementButton.interactable = (sliderComponent.slider.value < sliderComponent.slider.maxValue);
 0125        sliderComponent.decrementButton.interactable = (sliderComponent.slider.value > sliderComponent.slider.minValue);
 0126    }
 127
 128    public void UpdateValueFromColorSelector(Color newColor)
 129    {
 0130        UpdateSliderValues(newColor);
 0131        SetColor();
 0132    }
 133
 134    public void UpdateSliderValues(Color currentColor)
 135    {
 344136        Color.RGBToHSV(currentColor, out float h, out float s, out float v);
 344137        colorPreviewImage.color = currentColor;
 344138        sliderHue.slider.SetValueWithoutNotify(h);
 344139        sliderSaturation.slider.SetValueWithoutNotify(s);
 344140        sliderValue.slider.SetValueWithoutNotify(v);
 344141    }
 142
 143    private void SetColor()
 144    {
 0145        Color newColor = Color.HSVToRGB(sliderHue.slider.value, sliderSaturation.slider.value, sliderValue.slider.value)
 0146        colorPreviewImage.color = newColor;
 0147        CheckButtonInteractability(sliderHue);
 0148        CheckButtonInteractability(sliderSaturation);
 0149        CheckButtonInteractability(sliderValue);
 0150        if (!isAudioPlaying)
 0151            StartCoroutine(PlaySound());
 152
 0153        OnColorChanged.Invoke(newColor);
 0154    }
 155
 156    private IEnumerator PlaySound()
 157    {
 0158        isAudioPlaying = true;
 0159        AudioScriptableObjects.buttonRelease.Play(true);
 0160        yield return new WaitForSeconds(0.05f);
 0161        isAudioPlaying = false;
 0162    }
 163
 164    public void SetActive(bool isActive)
 165    {
 0166        container.SetActive(isActive);
 0167        containerImage.enabled = isActive;
 0168    }
 169
 170    override public void Dispose()
 171    {
 0172        base.Dispose();
 0173        toggleButton.onClick.RemoveAllListeners();
 0174    }
 175
 0176    public void SetIncrementAmount(float amount) { model.incrementAmount = amount; }
 177}