< Summary

Class:SectionToggle
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/SectionSelector/SectionToggle.cs
Covered lines:65
Uncovered lines:4
Coverable lines:69
Total lines:219
Line coverage:94.2% (65 of 69)
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%4.054085.71%
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    GameObject GameObject { get; }
 11    /// <summary>
 12    /// Pivot of the section object.
 13    /// </summary>
 14    RectTransform pivot { get; }
 15
 16    /// <summary>
 17    /// Event that will be triggered when the toggle is selected.
 18    /// </summary>
 19    ToggleEvent onSelect { get; }
 20
 21    /// <summary>
 22    /// Get the toggle info.
 23    /// </summary>
 24    /// <returns>Model with all the toggle info.</returns>
 25    SectionToggleModel GetInfo();
 26
 27    /// <summary>
 28    /// Set the toggle info.
 29    /// </summary>
 30    /// <param name="model">Model with all the toggle info.</param>
 31    void SetInfo(SectionToggleModel model);
 32
 33    /// <summary>
 34    /// Invoke the action of selecting the toggle.
 35    /// </summary>
 36    /// <param name="reselectIfAlreadyOn">True for apply the selection even if the toggle was already off.</param>
 37    void SelectToggle(bool reselectIfAlreadyOn = false);
 38
 39    /// <summary>
 40    /// Set the toggle visuals as selected.
 41    /// </summary>
 42    void SetSelectedVisuals();
 43
 44    /// <summary>
 45    /// Set the toggle visuals as unselected.
 46    /// </summary>
 47    void SetUnselectedVisuals();
 48
 49    /// <summary>
 50    /// Set the toggle as active or inactive.
 51    /// </summary>
 52    /// <param name="isActive">Tru for activating.</param>
 53    void SetActive(bool isActive);
 54
 55    /// <summary>
 56    /// Check if the toggle is active or not.
 57    /// </summary>
 58    /// <returns>True if it is actived.</returns>
 59    bool IsActive();
 60}
 61
 62public class SectionToggle : MonoBehaviour, ISectionToggle, IPointerDownHandler
 63{
 64    [SerializeField] private Toggle toggle;
 65
 66    [Header("Visual Configuration When Selected")]
 67    [SerializeField] private Image selectedIcon;
 68    [SerializeField] private TMP_Text selectedTitle;
 69    [SerializeField] private ColorBlock backgroundTransitionColorsForSelected;
 70    [SerializeField] private Color selectedTextColor;
 71    [SerializeField] private Color selectedImageColor;
 72
 73    [Header("Visual Configuration When Unselected")]
 74    [SerializeField] private Image unselectedIcon;
 75    [SerializeField] private TMP_Text unselectedTitle;
 76    [SerializeField] private ColorBlock backgroundTransitionColorsForUnselected;
 77    [SerializeField] private Color unselectedTextColor;
 78    [SerializeField] private Color unselectedImageColor;
 79
 80    private void Awake() =>
 48681        ConfigureDefaultOnSelectAction();
 82
 83    private void OnEnable() =>
 58284        StartCoroutine(ForceToRefreshToggleState());
 85
 86    public void OnPointerDown(PointerEventData eventData) =>
 087        SelectToggle();
 88
 4189    public GameObject GameObject => gameObject;
 090    public RectTransform pivot => transform as RectTransform;
 148091    public ToggleEvent onSelect => toggle?.onValueChanged;
 92
 93    public SectionToggleModel GetInfo()
 94    {
 2595        return new SectionToggleModel
 96        {
 97            selectedIcon = selectedIcon.sprite,
 98            selectedTitle = selectedTitle.text,
 99            selectedTextColor = selectedTextColor,
 100            selectedImageColor = selectedImageColor,
 101            unselectedIcon = unselectedIcon.sprite,
 102            unselectedTitle = unselectedTitle.text,
 103            backgroundTransitionColorsForSelected = backgroundTransitionColorsForSelected,
 104            unselectedTextColor = unselectedTextColor,
 105            unselectedImageColor = unselectedImageColor,
 106            backgroundTransitionColorsForUnselected = backgroundTransitionColorsForUnselected
 107        };
 108    }
 109
 110    public void SetInfo(SectionToggleModel model)
 111    {
 85112        if (model == null)
 0113            return;
 114
 85115        if (selectedTitle != null)
 116        {
 85117            selectedTitle.text = model.selectedTitle;
 85118            selectedTitle.color = model.selectedTextColor;
 119        }
 120
 85121        if (unselectedTitle != null)
 122        {
 85123            unselectedTitle.text = model.unselectedTitle;
 85124            unselectedTitle.color = model.unselectedTextColor;
 125        }
 126
 85127        if (selectedIcon != null)
 128        {
 85129            selectedIcon.sprite = model.selectedIcon;
 85130            selectedIcon.color = model.selectedImageColor;
 131        }
 132
 85133        if (unselectedIcon != null)
 134        {
 85135            unselectedIcon.sprite = model.unselectedIcon;
 85136            unselectedIcon.color = model.unselectedImageColor;
 137        }
 138
 85139        backgroundTransitionColorsForSelected = model.backgroundTransitionColorsForSelected;
 85140        backgroundTransitionColorsForUnselected = model.backgroundTransitionColorsForUnselected;
 85141        selectedTextColor = model.selectedTextColor;
 85142        selectedImageColor = model.selectedImageColor;
 85143        unselectedTextColor = model.unselectedTextColor;
 85144        unselectedImageColor = model.unselectedImageColor;
 145
 85146        onSelect.RemoveAllListeners();
 85147        ConfigureDefaultOnSelectAction();
 85148    }
 149
 150    public void SelectToggle(bool reselectIfAlreadyOn = false)
 151    {
 152152        if (toggle == null)
 0153            return;
 154
 152155        if (reselectIfAlreadyOn)
 49156            toggle.isOn = false;
 157
 152158        if(!toggle.isOn)
 105159            toggle.isOn = true;
 152160    }
 161
 162    public void SetSelectedVisuals()
 163    {
 160164        if (selectedIcon != null)
 160165            selectedIcon.gameObject.SetActive(true);
 166
 160167        if (unselectedIcon != null)
 160168            unselectedIcon.gameObject.SetActive(false);
 169
 160170        if (selectedTitle != null)
 160171            selectedTitle.gameObject.SetActive(true);
 172
 160173        if (unselectedTitle != null)
 160174            unselectedTitle.gameObject.SetActive(false);
 175
 160176        toggle.colors = backgroundTransitionColorsForSelected;
 160177    }
 178
 179    public void SetUnselectedVisuals()
 180    {
 86181        if (selectedIcon != null)
 86182            selectedIcon.gameObject.SetActive(false);
 183
 86184        if (unselectedIcon != null)
 86185            unselectedIcon.gameObject.SetActive(true);
 186
 86187        if (selectedTitle != null)
 86188            selectedTitle.gameObject.SetActive(false);
 189
 86190        if (unselectedTitle != null)
 86191            unselectedTitle.gameObject.SetActive(true);
 192
 86193        toggle.colors = backgroundTransitionColorsForUnselected;
 86194    }
 195
 212196    public void SetActive(bool isActive) { gameObject.SetActive(isActive); }
 197
 203198    public bool IsActive() { return gameObject.activeSelf; }
 199
 200    internal void ConfigureDefaultOnSelectAction()
 201    {
 571202        onSelect.AddListener((isOn) =>
 203        {
 246204            if (isOn)
 160205                SetSelectedVisuals();
 206            else
 86207                SetUnselectedVisuals();
 86208        });
 571209    }
 210
 211    internal IEnumerator ForceToRefreshToggleState()
 212    {
 213        // After each activation, in order to update the toggle's transition colors correctly, we need to force to chang
 214        // of the component so that Unity notices it is in "dirty" state and it is refreshed.
 582215        toggle.interactable = false;
 582216        yield return null;
 2217        toggle.interactable = true;
 2218    }
 219}