< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
OnEnable()0%110100%
OnPointerDown(...)0%2100%
GetInfo()0%110100%
SetInfo(...)0%66095.65%
SelectToggle(...)0%3.043083.33%
SetSelectedVisuals()0%550100%
SetUnselectedVisuals()0%550100%
SetActive(...)0%110100%
IsActive()0%110100%
ConfigureDefaultOnSelectAction()0%110100%
ForceToRefreshToggleState()0%330100%

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.EventSystems;
 5using UnityEngine.UI;
 6using static UnityEngine.UI.Toggle;
 7
 8public interface ISectionToggle
 9{
 10    /// <summary>
 11    /// Pivot of the section object.
 12    /// </summary>
 13    RectTransform pivot { get; }
 14
 15    /// <summary>
 16    /// Event that will be triggered when the toggle is selected.
 17    /// </summary>
 18    ToggleEvent onSelect { get; }
 19
 20    /// <summary>
 21    /// Get the toggle info.
 22    /// </summary>
 23    /// <returns>Model with all the toggle info.</returns>
 24    SectionToggleModel GetInfo();
 25
 26    /// <summary>
 27    /// Set the toggle info.
 28    /// </summary>
 29    /// <param name="model">Model with all the toggle info.</param>
 30    void SetInfo(SectionToggleModel model);
 31
 32    /// <summary>
 33    /// Invoke the action of selecting the toggle.
 34    /// </summary>
 35    /// <param name="reselectIfAlreadyOn">True for apply the selection even if the toggle was already off.</param>
 36    void SelectToggle(bool reselectIfAlreadyOn = false);
 37
 38    /// <summary>
 39    /// Set the toggle visuals as selected.
 40    /// </summary>
 41    void SetSelectedVisuals();
 42
 43    /// <summary>
 44    /// Set the toggle visuals as unselected.
 45    /// </summary>
 46    void SetUnselectedVisuals();
 47
 48    /// <summary>
 49    /// Set the toggle as active or inactive.
 50    /// </summary>
 51    /// <param name="isActive">Tru for activating.</param>
 52    void SetActive(bool isActive);
 53
 54    /// <summary>
 55    /// Check if the toggle is active or not.
 56    /// </summary>
 57    /// <returns>True if it is actived.</returns>
 58    bool IsActive();
 59}
 60
 61public class SectionToggle : MonoBehaviour, ISectionToggle, IPointerDownHandler
 62{
 63    [SerializeField] private Toggle toggle;
 64
 65    [Header("Visual Configuration When Selected")]
 66    [SerializeField] private Image selectedIcon;
 67    [SerializeField] private TMP_Text selectedTitle;
 68    [SerializeField] private ColorBlock backgroundTransitionColorsForSelected;
 69    [SerializeField] private Color selectedTextColor;
 70    [SerializeField] private Color selectedImageColor;
 71
 72    [Header("Visual Configuration When Unselected")]
 73    [SerializeField] private Image unselectedIcon;
 74    [SerializeField] private TMP_Text unselectedTitle;
 75    [SerializeField] private ColorBlock backgroundTransitionColorsForUnselected;
 76    [SerializeField] private Color unselectedTextColor;
 77    [SerializeField] private Color unselectedImageColor;
 78
 79    private void Awake() =>
 44380        ConfigureDefaultOnSelectAction();
 81
 82    private void OnEnable() =>
 53983        StartCoroutine(ForceToRefreshToggleState());
 84
 85    public void OnPointerDown(PointerEventData eventData) =>
 086        SelectToggle();
 87
 088    public RectTransform pivot => transform as RectTransform;
 133289    public ToggleEvent onSelect => toggle?.onValueChanged;
 90
 91    public SectionToggleModel GetInfo()
 92    {
 2593        return new SectionToggleModel
 94        {
 95            selectedIcon = selectedIcon.sprite,
 96            selectedTitle = selectedTitle.text,
 97            selectedTextColor = selectedTextColor,
 98            selectedImageColor = selectedImageColor,
 99            unselectedIcon = unselectedIcon.sprite,
 100            unselectedTitle = unselectedTitle.text,
 101            backgroundTransitionColorsForSelected = backgroundTransitionColorsForSelected,
 102            unselectedTextColor = unselectedTextColor,
 103            unselectedImageColor = unselectedImageColor,
 104            backgroundTransitionColorsForUnselected = backgroundTransitionColorsForUnselected
 105        };
 106    }
 107
 108    public void SetInfo(SectionToggleModel model)
 109    {
 82110        if (model == null)
 0111            return;
 112
 82113        if (selectedTitle != null)
 114        {
 82115            selectedTitle.text = model.selectedTitle;
 82116            selectedTitle.color = model.selectedTextColor;
 117        }
 118
 82119        if (unselectedTitle != null)
 120        {
 82121            unselectedTitle.text = model.unselectedTitle;
 82122            unselectedTitle.color = model.unselectedTextColor;
 123        }
 124
 82125        if (selectedIcon != null)
 126        {
 82127            selectedIcon.sprite = model.selectedIcon;
 82128            selectedIcon.color = model.selectedImageColor;
 129        }
 130
 82131        if (unselectedIcon != null)
 132        {
 82133            unselectedIcon.sprite = model.unselectedIcon;
 82134            unselectedIcon.color = model.unselectedImageColor;
 135        }
 136
 82137        backgroundTransitionColorsForSelected = model.backgroundTransitionColorsForSelected;
 82138        backgroundTransitionColorsForUnselected = model.backgroundTransitionColorsForUnselected;
 82139        selectedTextColor = model.selectedTextColor;
 82140        selectedImageColor = model.selectedImageColor;
 82141        unselectedTextColor = model.unselectedTextColor;
 82142        unselectedImageColor = model.unselectedImageColor;
 143
 82144        onSelect.RemoveAllListeners();
 82145        ConfigureDefaultOnSelectAction();
 82146    }
 147
 148    public void SelectToggle(bool reselectIfAlreadyOn = false)
 149    {
 149150        if (toggle == null)
 0151            return;
 152
 149153        if (reselectIfAlreadyOn)
 49154            toggle.isOn = false;
 155
 149156        toggle.isOn = true;
 149157    }
 158
 159    public void SetSelectedVisuals()
 160    {
 157161        if (selectedIcon != null)
 157162            selectedIcon.gameObject.SetActive(true);
 163
 157164        if (unselectedIcon != null)
 157165            unselectedIcon.gameObject.SetActive(false);
 166
 157167        if (selectedTitle != null)
 157168            selectedTitle.gameObject.SetActive(true);
 169
 157170        if (unselectedTitle != null)
 157171            unselectedTitle.gameObject.SetActive(false);
 172
 157173        toggle.colors = backgroundTransitionColorsForSelected;
 157174    }
 175
 176    public void SetUnselectedVisuals()
 177    {
 83178        if (selectedIcon != null)
 83179            selectedIcon.gameObject.SetActive(false);
 180
 83181        if (unselectedIcon != null)
 83182            unselectedIcon.gameObject.SetActive(true);
 183
 83184        if (selectedTitle != null)
 83185            selectedTitle.gameObject.SetActive(false);
 186
 83187        if (unselectedTitle != null)
 83188            unselectedTitle.gameObject.SetActive(true);
 189
 83190        toggle.colors = backgroundTransitionColorsForUnselected;
 83191    }
 192
 212193    public void SetActive(bool isActive) { gameObject.SetActive(isActive); }
 194
 203195    public bool IsActive() { return gameObject.activeSelf; }
 196
 197    internal void ConfigureDefaultOnSelectAction()
 198    {
 525199        onSelect.AddListener((isOn) =>
 200        {
 240201            if (isOn)
 157202                SetSelectedVisuals();
 203            else
 83204                SetUnselectedVisuals();
 83205        });
 525206    }
 207
 208    internal IEnumerator ForceToRefreshToggleState()
 209    {
 210        // After each activation, in order to update the toggle's transition colors correctly, we need to force to chang
 211        // of the component so that Unity notices it is in "dirty" state and it is refreshed.
 539212        toggle.interactable = false;
 539213        yield return null;
 2214        toggle.interactable = true;
 2215    }
 216}