< 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
 14119    private List<ColorToggle> colorToggles = new List<ColorToggle>();
 20
 21    public void Populate(IReadOnlyList<Color> colors)
 22    {
 13823        int colorNumber = colors.Count;
 24
 303625        for (int i = 0; i < colorNumber; ++i)
 26        {
 138027            ColorToggle newToggle = Instantiate(colorTogglePrefab, colorContainer);
 138028            newToggle.Initialize(colors[i], false);
 138029            newToggle.OnClicked += ToggleClicked;
 138030            colorToggles.Add(newToggle);
 31        }
 13832    }
 33
 34    public void Select(Color color)
 35    {
 31136        ColorToggle toggle = GetColorToggle(color);
 31137        if (toggle == currentToggle)
 15638            return;
 15539        Select(toggle);
 15540    }
 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    {
 15557        if (currentToggle != null)
 58        {
 2059            currentToggle.selected = false;
 60        }
 61
 15562        currentToggle = colorToggle;
 63
 15564        if (colorToggle)
 65        {
 15566            colorToggle.selected = true;
 67        }
 15568    }
 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    {
 31181        int colorTogglesCount = colorToggles.Count;
 142082        for (int i = 0; i < colorTogglesCount; i++)
 83        {
 71084            Color current = colorToggles[i].color;
 71085            if (color.AproxComparison(current))
 86            {
 31187                return colorToggles[i];
 88            }
 89        }
 90
 091        return null;
 92    }
 93}