< Summary

Class:ColorPickerComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/ColorPicker/ColorPickerComponentView.cs
Covered lines:50
Uncovered lines:41
Coverable lines:91
Total lines:189
Line coverage:54.9% (50 of 91)
Covered branches:0
Total branches:0
Covered methods:12
Total methods:20
Method coverage:60% (12 of 20)

Metrics

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

File(s)

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

#LineLine coverage
 1using DCL.Helpers;
 2using System;
 3using System.Collections;
 4using System.Collections.Generic;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8public 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
 224    public bool IsShowingOnlyPresetColors => model.showOnlyPresetColors;
 225    public List<Color> ColorList => model.colorList;
 226    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    {
 3536        base.Awake();
 37
 3538        colorSelector = colorSelectorObject.GetComponent<IColorSelector>();
 3539        colorSelector.OnColorSelectorChange += UpdateValueFromColorSelector;
 40
 3541        sliderHue.onSliderChange.AddListener(_ => SetColor());
 3542        sliderHue.onIncrement.AddListener(() => ChangeProperty("hue", model.incrementAmount));
 3543        sliderHue.onDecrement.AddListener(() => ChangeProperty("hue", -model.incrementAmount));
 44
 3545        sliderSaturation.onSliderChange.AddListener(_ => SetColor());
 3546        sliderSaturation.onIncrement.AddListener(() => ChangeProperty("sat", model.incrementAmount));
 3547        sliderSaturation.onDecrement.AddListener(() => ChangeProperty("sat", -model.incrementAmount));
 48
 3549        sliderValue.onSliderChange.AddListener(_ => SetColor());
 3550        sliderValue.onIncrement.AddListener(() => ChangeProperty("val", model.incrementAmount));
 3551        sliderValue.onDecrement.AddListener(() => ChangeProperty("val", -model.incrementAmount));
 52
 3553        toggleButton.onClick.AddListener(() => SetActive(!container.activeInHierarchy));
 54
 3555        SetActive(false);
 3556    }
 57
 58    public override void RefreshControl()
 59    {
 060        if (model == null)
 061            return;
 62
 063        SetColorList(model.colorList);
 064        SetIncrementAmount(model.incrementAmount);
 065        SetShowOnlyPresetColors(model.showOnlyPresetColors);
 066    }
 67
 68    public void SetColorSelector(Color newColor)
 69    {
 170        colorSelector.Select(newColor);
 171        CurrentSelectedColor = newColor;
 172    }
 73
 74    public void SetColorList(List<Color> colorList)
 75    {
 276        model.colorList = colorList;
 277        colorSelector ??= colorSelectorObject.GetComponent<IColorSelector>();
 278        colorSelector.Cleanup();
 279        colorSelector.Populate(colorList);
 280    }
 81
 82    public void Configure(ColorPickerComponentModel newModel)
 83    {
 084        model = newModel;
 085        RefreshControl();
 086    }
 87
 88    private void ChangeProperty(string a, float amount)
 89    {
 90        switch (a)
 91        {
 92            case "hue":
 093                sliderHue.AddValueToSlider(amount);
 094                CheckButtonInteractivity(sliderHue);
 095                break;
 96            case "sat":
 097                sliderSaturation.AddValueToSlider(amount);
 098                CheckButtonInteractivity(sliderSaturation);
 099                break;
 100            case "val":
 0101                sliderValue.AddValueToSlider(amount);
 0102                CheckButtonInteractivity(sliderValue);
 103                break;
 104        }
 0105    }
 106
 107    private static void CheckButtonInteractivity(SliderComponentView sliderComponent)
 108    {
 0109        sliderComponent.incrementButton.interactable = sliderComponent.slider.value < sliderComponent.slider.maxValue;
 0110        sliderComponent.decrementButton.interactable = sliderComponent.slider.value > sliderComponent.slider.minValue;
 0111    }
 112
 113    private void UpdateValueFromColorSelector(Color newColor)
 114    {
 0115        UpdateSliderValues(newColor);
 0116        SetColor();
 0117    }
 118
 119    public void UpdateSliderValues(Color currentColor)
 120    {
 1121        Color.RGBToHSV(currentColor, out float h, out float s, out float v);
 1122        colorPreviewImage.color = currentColor;
 1123        sliderHue.slider.SetValueWithoutNotify(h);
 1124        sliderSaturation.slider.SetValueWithoutNotify(s);
 1125        sliderValue.slider.SetValueWithoutNotify(v);
 1126    }
 127
 128    private void SetColor()
 129    {
 0130        Color newColor = Color.HSVToRGB(sliderHue.slider.value, sliderSaturation.slider.value, sliderValue.slider.value)
 0131        colorPreviewImage.color = newColor;
 0132        CheckButtonInteractivity(sliderHue);
 0133        CheckButtonInteractivity(sliderSaturation);
 0134        CheckButtonInteractivity(sliderValue);
 0135        if (!isAudioPlaying)
 0136            StartCoroutine(PlaySound());
 137
 0138        OnColorChanged?.Invoke(newColor);
 0139    }
 140
 141    private IEnumerator PlaySound()
 142    {
 0143        isAudioPlaying = true;
 0144        AudioScriptableObjects.buttonRelease.Play(true);
 0145        yield return new WaitForSeconds(0.05f);
 0146        isAudioPlaying = false;
 0147    }
 148
 149    public void SetActive(bool isActive)
 150    {
 36151        container.SetActive(isActive);
 36152        containerImage.enabled = isActive;
 153
 36154        if (arrowUpMark != null)
 36155            arrowUpMark.SetActive(isActive);
 156
 36157        if (arrowDownMark != null)
 36158            arrowDownMark.SetActive(!isActive);
 159
 36160        if (isActive)
 161        {
 0162            OnColorPickerToggle?.Invoke();
 0163            RebuildLayout();
 164        }
 36165    }
 166
 167    public override void Dispose()
 168    {
 35169        base.Dispose();
 35170        toggleButton.onClick.RemoveAllListeners();
 35171    }
 172
 173    public void SetIncrementAmount(float amount) =>
 0174        model.incrementAmount = amount;
 175
 176    public void SetShowOnlyPresetColors(bool showOnlyPresetColors)
 177    {
 2178        model.showOnlyPresetColors = showOnlyPresetColors;
 179
 2180        sliderHue.gameObject.SetActive(!showOnlyPresetColors);
 2181        sliderSaturation.gameObject.SetActive(!showOnlyPresetColors);
 2182        sliderValue.gameObject.SetActive(!showOnlyPresetColors);
 183
 2184        RebuildLayout();
 2185    }
 186
 187    private void RebuildLayout() =>
 2188        Utils.ForceRebuildLayoutImmediate(transform as RectTransform);
 189}