< Summary

Class:ColorSelector
Assembly:AvatarEditorHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/AvatarEditorHUD/Scripts/ColorSelector.cs
Covered lines:24
Uncovered lines:14
Coverable lines:38
Total lines:93
Line coverage:63.1% (24 of 38)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ColorSelector()0%110100%
Populate(...)0%220100%
Select(...)0%220100%
SelectRandom()0%20400%
Select(...)0%330100%
ToggleClicked(...)0%12300%
GetColorToggle(...)0%3.023087.5%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/AvatarEditorHUD/Scripts/ColorSelector.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using DCL.Helpers;
 3using UnityEngine;
 4using UnityEngine.UI;
 5
 6[RequireComponent(typeof(ToggleGroup))]
 7public class ColorSelector : MonoBehaviour
 8{
 9    [SerializeField]
 10    private ColorToggle colorTogglePrefab;
 11
 12    [SerializeField]
 13    private RectTransform colorContainer;
 14
 15    public event System.Action<Color> OnColorChanged;
 16
 17    private ColorToggle currentToggle;
 18
 26419    private List<ColorToggle> colorToggles = new List<ColorToggle>();
 20
 21    public void Populate(IReadOnlyList<Color> colors)
 22    {
 13223        int colorNumber = colors.Count;
 24
 290425        for (int i = 0; i < colorNumber; ++i)
 26        {
 132027            ColorToggle newToggle = Instantiate(colorTogglePrefab, colorContainer);
 132028            newToggle.Initialize(colors[i], false);
 132029            newToggle.OnClicked += ToggleClicked;
 132030            colorToggles.Add(newToggle);
 31        }
 13232    }
 33
 34    public void Select(Color color)
 35    {
 29936        ColorToggle toggle = GetColorToggle(color);
 29937        if (toggle == currentToggle)
 15038            return;
 14939        Select(toggle);
 14940    }
 41
 42    public void SelectRandom()
 43    {
 044        if (colorToggles.Count == 0)
 045            return;
 46
 047        ColorToggle toggle = colorToggles[Random.Range(0, colorToggles.Count)];
 048        if (toggle == currentToggle)
 049            return;
 50
 051        Select(toggle);
 052        OnColorChanged?.Invoke(currentToggle.color);
 053    }
 54
 55    private void Select(ColorToggle colorToggle)
 56    {
 14957        if (currentToggle != null)
 58        {
 2059            currentToggle.selected = false;
 60        }
 61
 14962        currentToggle = colorToggle;
 63
 14964        if (colorToggle)
 65        {
 14966            colorToggle.selected = true;
 67        }
 14968    }
 69
 70    private void ToggleClicked(ColorToggle toggle)
 71    {
 072        if (toggle == currentToggle)
 073            return;
 74
 075        Select(toggle);
 076        OnColorChanged?.Invoke(currentToggle.color);
 077    }
 78
 79    private ColorToggle GetColorToggle(Color color)
 80    {
 29981        int colorTogglesCount = colorToggles.Count;
 134682        for (int i = 0; i < colorTogglesCount; i++)
 83        {
 67384            Color current = colorToggles[i].color;
 67385            if (color.AproxComparison(current))
 86            {
 29987                return colorToggles[i];
 88            }
 89        }
 90
 091        return null;
 92    }
 93}