< Summary

Class:SectionToggle
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/SectionSelector/SectionToggle.cs
Covered lines:40
Uncovered lines:4
Coverable lines:44
Total lines:143
Line coverage:90.9% (40 of 44)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
OnEnable()0%110100%
GetInfo()0%110100%
SetInfo(...)0%44093.75%
SelectToggle(...)0%3.043083.33%
SetSelectedVisuals()0%110100%
SetUnselectedVisuals()0%110100%
ConfigureDefaultOnSelectAction()0%110100%
ForceToRefreshToggleState()0%4.123050%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/SectionSelector/SectionToggle.cs

#LineLine coverage
 1using System.Collections;
 2using TMPro;
 3using UnityEngine;
 4using UnityEngine.UI;
 5using static UnityEngine.UI.Toggle;
 6
 7public interface ISectionToggle
 8{
 9    /// <summary>
 10    /// Event that will be triggered when the toggle is selected.
 11    /// </summary>
 12    ToggleEvent onSelect { get; }
 13
 14    /// <summary>
 15    /// Get the toggle info.
 16    /// </summary>
 17    /// <returns>Model with all the toggle info.</returns>
 18    SectionToggleModel GetInfo();
 19
 20    /// <summary>
 21    /// Set the toggle info.
 22    /// </summary>
 23    /// <param name="model">Model with all the toggle info.</param>
 24    void SetInfo(SectionToggleModel model);
 25
 26    /// <summary>
 27    /// Invoke the action of selecting the toggle.
 28    /// </summary>
 29    /// <param name="reselectIfAlreadyOn">True for apply the selection even if the toggle was already off.</param>
 30    void SelectToggle(bool reselectIfAlreadyOn = false);
 31
 32    /// <summary>
 33    /// Set the toggle visuals as selected.
 34    /// </summary>
 35    void SetSelectedVisuals();
 36
 37    /// <summary>
 38    /// Set the toggle visuals as unselected.
 39    /// </summary>
 40    void SetUnselectedVisuals();
 41}
 42
 43public class SectionToggle : MonoBehaviour, ISectionToggle
 44{
 45    [SerializeField] private Toggle toggle;
 46    [SerializeField] private TMP_Text sectionText;
 47    [SerializeField] private Image sectionImage;
 48
 49    [Header("Visual Configuration When Selected")]
 50    [SerializeField] private ColorBlock backgroundTransitionColorsForSelected;
 51    [SerializeField] private Color selectedTextColor;
 52    [SerializeField] private Color selectedImageColor;
 53
 54    [Header("Visual Configuration When Unselected")]
 55    [SerializeField] private ColorBlock backgroundTransitionColorsForUnselected;
 56    [SerializeField] private Color unselectedTextColor;
 57    [SerializeField] private Color unselectedImageColor;
 58
 13959    public ToggleEvent onSelect => toggle?.onValueChanged;
 60
 51061    private void Awake() { ConfigureDefaultOnSelectAction(); }
 62
 51063    private void OnEnable() { StartCoroutine(ForceToRefreshToggleState()); }
 64
 65    public SectionToggleModel GetInfo()
 66    {
 767        return new SectionToggleModel
 68        {
 69            icon = sectionImage.sprite,
 70            title = sectionText.text
 71        };
 72    }
 73
 74    public void SetInfo(SectionToggleModel model)
 75    {
 11576        if (model == null)
 077            return;
 78
 11579        if (sectionText != null)
 11580            sectionText.text = model.title;
 81
 11582        if (sectionImage != null)
 83        {
 11584            sectionImage.enabled = model.icon != null;
 11585            sectionImage.sprite = model.icon;
 86        }
 87
 11588        backgroundTransitionColorsForSelected = model.backgroundTransitionColorsForSelected;
 11589        backgroundTransitionColorsForUnselected = model.backgroundTransitionColorsForUnselected;
 11590        selectedTextColor = model.selectedTextColor;
 11591        selectedImageColor = model.selectedImageColor;
 11592        unselectedTextColor = model.unselectedTextColor;
 11593        unselectedImageColor = model.unselectedImageColor;
 94
 11595        onSelect.RemoveAllListeners();
 11596        ConfigureDefaultOnSelectAction();
 11597    }
 98
 99    public void SelectToggle(bool reselectIfAlreadyOn = false)
 100    {
 32101        if (toggle == null)
 0102            return;
 103
 32104        if (reselectIfAlreadyOn)
 11105            toggle.isOn = false;
 106
 32107        toggle.isOn = true;
 32108    }
 109
 110    public void SetSelectedVisuals()
 111    {
 65112        toggle.colors = backgroundTransitionColorsForSelected;
 65113        sectionText.color = selectedTextColor;
 65114        sectionImage.color = selectedImageColor;
 65115    }
 116
 117    public void SetUnselectedVisuals()
 118    {
 122119        toggle.colors = backgroundTransitionColorsForUnselected;
 122120        sectionText.color = unselectedTextColor;
 122121        sectionImage.color = unselectedImageColor;
 122122    }
 123
 124    internal void ConfigureDefaultOnSelectAction()
 125    {
 370126        onSelect.AddListener((isOn) =>
 127        {
 187128            if (isOn)
 65129                SetSelectedVisuals();
 130            else
 122131                SetUnselectedVisuals();
 122132        });
 370133    }
 134
 135    internal IEnumerator ForceToRefreshToggleState()
 136    {
 137        // After each activation, in order to update the toggle's transition colors correctly, we need to force to chang
 138        // of the component so that Unity notices it is in "dirty" state and it is refreshed.
 255139        toggle.interactable = false;
 255140        yield return null;
 0141        toggle.interactable = true;
 0142    }
 143}