< Summary

Class:SectionToggle
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/SectionSelector/SectionToggle.cs
Covered lines:61
Uncovered lines:7
Coverable lines:68
Total lines:216
Line coverage:89.7% (61 of 68)
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%66095.65%
OnPointerDown(...)0%2100%
SelectToggle(...)0%3.043083.33%
SetSelectedVisuals()0%550100%
SetUnselectedVisuals()0%550100%
SetActive(...)0%110100%
IsActive()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.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
 079    public RectTransform pivot => transform as RectTransform;
 73880    public ToggleEvent onSelect => toggle?.onValueChanged;
 81
 74082    private void Awake() { ConfigureDefaultOnSelectAction(); }
 83
 74684    private void OnEnable() { StartCoroutine(ForceToRefreshToggleState()); }
 85
 86    public SectionToggleModel GetInfo()
 87    {
 2588        return new SectionToggleModel
 89        {
 90            selectedIcon = selectedIcon.sprite,
 91            selectedTitle = selectedTitle.text,
 92            selectedTextColor = selectedTextColor,
 93            selectedImageColor = selectedImageColor,
 94            unselectedIcon = unselectedIcon.sprite,
 95            unselectedTitle = unselectedTitle.text,
 96            backgroundTransitionColorsForSelected = backgroundTransitionColorsForSelected,
 97            unselectedTextColor = unselectedTextColor,
 98            unselectedImageColor = unselectedImageColor,
 99            backgroundTransitionColorsForUnselected = backgroundTransitionColorsForUnselected
 100        };
 101    }
 102
 103    public void SetInfo(SectionToggleModel model)
 104    {
 94105        if (model == null)
 0106            return;
 107
 94108        if (selectedTitle != null)
 109        {
 94110            selectedTitle.text = model.selectedTitle;
 94111            selectedTitle.color = model.selectedTextColor;
 112        }
 113
 94114        if (unselectedTitle != null)
 115        {
 94116            unselectedTitle.text = model.unselectedTitle;
 94117            unselectedTitle.color = model.unselectedTextColor;
 118        }
 119
 94120        if (selectedIcon != null)
 121        {
 94122            selectedIcon.sprite = model.selectedIcon;
 94123            selectedIcon.color = model.selectedImageColor;
 124        }
 125
 94126        if (unselectedIcon != null)
 127        {
 94128            unselectedIcon.sprite = model.unselectedIcon;
 94129            unselectedIcon.color = model.unselectedImageColor;
 130        }
 131
 94132        backgroundTransitionColorsForSelected = model.backgroundTransitionColorsForSelected;
 94133        backgroundTransitionColorsForUnselected = model.backgroundTransitionColorsForUnselected;
 94134        selectedTextColor = model.selectedTextColor;
 94135        selectedImageColor = model.selectedImageColor;
 94136        unselectedTextColor = model.unselectedTextColor;
 94137        unselectedImageColor = model.unselectedImageColor;
 138
 94139        onSelect.RemoveAllListeners();
 94140        ConfigureDefaultOnSelectAction();
 94141    }
 142
 143    public void OnPointerDown(PointerEventData eventData)
 144    {
 0145        SelectToggle();
 0146    }
 147
 148    public void SelectToggle(bool reselectIfAlreadyOn = false)
 149    {
 142150        if (toggle == null)
 0151            return;
 152
 142153        if (reselectIfAlreadyOn)
 28154            toggle.isOn = false;
 155
 142156        toggle.isOn = true;
 142157    }
 158
 159    public void SetSelectedVisuals()
 160    {
 151161        if (selectedIcon != null)
 151162            selectedIcon.gameObject.SetActive(true);
 163
 151164        if (unselectedIcon != null)
 151165            unselectedIcon.gameObject.SetActive(false);
 166
 151167        if (selectedTitle != null)
 151168            selectedTitle.gameObject.SetActive(true);
 169
 151170        if (unselectedTitle != null)
 151171            unselectedTitle.gameObject.SetActive(false);
 172
 151173        toggle.colors = backgroundTransitionColorsForSelected;
 151174    }
 175
 176    public void SetUnselectedVisuals()
 177    {
 100178        if (selectedIcon != null)
 100179            selectedIcon.gameObject.SetActive(false);
 180
 100181        if (unselectedIcon != null)
 100182            unselectedIcon.gameObject.SetActive(true);
 183
 100184        if (selectedTitle != null)
 100185            selectedTitle.gameObject.SetActive(false);
 186
 100187        if (unselectedTitle != null)
 100188            unselectedTitle.gameObject.SetActive(true);
 189
 100190        toggle.colors = backgroundTransitionColorsForUnselected;
 100191    }
 192
 24193    public void SetActive(bool isActive) { gameObject.SetActive(isActive); }
 194
 13195    public bool IsActive() { return gameObject.activeSelf; }
 196
 197    internal void ConfigureDefaultOnSelectAction()
 198    {
 464199        onSelect.AddListener((isOn) =>
 200        {
 251201            if (isOn)
 151202                SetSelectedVisuals();
 203            else
 100204                SetUnselectedVisuals();
 100205        });
 464206    }
 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.
 373212        toggle.interactable = false;
 373213        yield return null;
 0214        toggle.interactable = true;
 0215    }
 216}