< Summary

Class:SectionToggle
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/SectionSelector/SectionToggle.cs
Covered lines:69
Uncovered lines:11
Coverable lines:80
Total lines:253
Line coverage:86.2% (69 of 80)
Covered branches:0
Total branches:0
Covered methods:14
Total methods:16
Method coverage:87.5% (14 of 16)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
OnEnable()0%110100%
GetInfo()0%220100%
SetInfo(...)0%66095.83%
SelectToggle(...)0%4.054085.71%
UnSelectToggle(...)0%20400%
SetSelectedVisuals()0%550100%
SetUnselectedVisuals()0%550100%
SetActive(...)0%110100%
IsActive()0%110100%
SetAsNew(...)0%2.062075%
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;
 2using System.Collections;
 3using TMPro;
 4using UnityEngine;
 5using UnityEngine.EventSystems;
 6using UnityEngine.UI;
 7using static UnityEngine.UI.Toggle;
 8
 9public interface ISectionToggle
 10{
 11    GameObject GameObject { get; }
 12    /// <summary>
 13    /// Pivot of the section object.
 14    /// </summary>
 15    RectTransform pivot { get; }
 16
 17    /// <summary>
 18    /// Event that will be triggered when the toggle is selected.
 19    /// </summary>
 20    ToggleEvent onSelect { get; }
 21
 22    /// <summary>
 23    /// Get the toggle info.
 24    /// </summary>
 25    /// <returns>Model with all the toggle info.</returns>
 26    SectionToggleModel GetInfo();
 27
 28    /// <summary>
 29    /// Set the toggle info.
 30    /// </summary>
 31    /// <param name="model">Model with all the toggle info.</param>
 32    void SetInfo(SectionToggleModel model);
 33
 34    /// <summary>
 35    /// Invoke the action of selecting the toggle.
 36    /// </summary>
 37    /// <param name="reselectIfAlreadyOn">True for apply the selection even if the toggle was already off.</param>
 38    void SelectToggle(bool reselectIfAlreadyOn = false);
 39
 40    /// <summary>
 41    /// Invoke the action of un-selecting the toggle.
 42    /// </summary>
 43    /// <param name="reUnSelectIfAlreadyOff">True for apply the un-selection even if the toggle was already off.</param>
 44    void UnSelectToggle(bool reUnSelectIfAlreadyOff = false);
 45
 46    /// <summary>
 47    /// Set the toggle visuals as selected.
 48    /// </summary>
 49    void SetSelectedVisuals();
 50
 51    /// <summary>
 52    /// Set the toggle visuals as unselected.
 53    /// </summary>
 54    void SetUnselectedVisuals();
 55
 56    /// <summary>
 57    /// Set the toggle as active or inactive.
 58    /// </summary>
 59    /// <param name="isActive">Tru for activating.</param>
 60    void SetActive(bool isActive);
 61
 62    /// <summary>
 63    /// Check if the toggle is active or not.
 64    /// </summary>
 65    /// <returns>True if it is actived.</returns>
 66    bool IsActive();
 67
 68    /// <summary>
 69    /// Show/Hide the NEW tag.
 70    /// </summary>
 71    /// <param name="isNew">True for showing the tag.</param>
 72    void SetAsNew(bool isNew);
 73}
 74
 75public class SectionToggle : MonoBehaviour, ISectionToggle
 76{
 77    [SerializeField] private Toggle toggle;
 78    [SerializeField] private GameObject newTag;
 79
 80    [Header("Visual Configuration When Selected")]
 81    [SerializeField] private Image selectedIcon;
 82    [SerializeField] private TMP_Text selectedTitle;
 83    [SerializeField] private ColorBlock backgroundTransitionColorsForSelected;
 84    [SerializeField] private Color selectedTextColor;
 85    [SerializeField] private Color selectedImageColor;
 86
 87    [Header("Visual Configuration When Unselected")]
 88    [SerializeField] private Image unselectedIcon;
 89    [SerializeField] private TMP_Text unselectedTitle;
 90    [SerializeField] private ColorBlock backgroundTransitionColorsForUnselected;
 91    [SerializeField] private Color unselectedTextColor;
 92    [SerializeField] private Color unselectedImageColor;
 93
 94    private void Awake() =>
 44995        ConfigureDefaultOnSelectAction();
 96
 97    private void OnEnable() =>
 45198        StartCoroutine(ForceToRefreshToggleState());
 99
 76100    public GameObject GameObject => gameObject;
 0101    public RectTransform pivot => transform as RectTransform;
 1438102    public ToggleEvent onSelect => toggle != null ? toggle.onValueChanged : null;
 103
 104    public SectionToggleModel GetInfo()
 105    {
 25106        return new SectionToggleModel
 107        {
 108            selectedIcon = selectedIcon.sprite,
 109            selectedTitle = selectedTitle.text,
 110            selectedTextColor = selectedTextColor,
 111            selectedImageColor = selectedImageColor,
 112            unselectedIcon = unselectedIcon.sprite,
 113            unselectedTitle = unselectedTitle.text,
 114            backgroundTransitionColorsForSelected = backgroundTransitionColorsForSelected,
 115            unselectedTextColor = unselectedTextColor,
 116            unselectedImageColor = unselectedImageColor,
 117            backgroundTransitionColorsForUnselected = backgroundTransitionColorsForUnselected,
 118            showNewTag = newTag != null && newTag.activeSelf,
 119        };
 120    }
 121
 122    public void SetInfo(SectionToggleModel model)
 123    {
 83124        if (model == null)
 0125            return;
 126
 83127        if (selectedTitle != null)
 128        {
 83129            selectedTitle.text = model.selectedTitle;
 83130            selectedTitle.color = model.selectedTextColor;
 131        }
 132
 83133        if (unselectedTitle != null)
 134        {
 83135            unselectedTitle.text = model.unselectedTitle;
 83136            unselectedTitle.color = model.unselectedTextColor;
 137        }
 138
 83139        if (selectedIcon != null)
 140        {
 83141            selectedIcon.sprite = model.selectedIcon;
 83142            selectedIcon.color = model.selectedImageColor;
 143        }
 144
 83145        if (unselectedIcon != null)
 146        {
 83147            unselectedIcon.sprite = model.unselectedIcon;
 83148            unselectedIcon.color = model.unselectedImageColor;
 149        }
 150
 83151        SetAsNew(model.showNewTag);
 152
 83153        backgroundTransitionColorsForSelected = model.backgroundTransitionColorsForSelected;
 83154        backgroundTransitionColorsForUnselected = model.backgroundTransitionColorsForUnselected;
 83155        selectedTextColor = model.selectedTextColor;
 83156        selectedImageColor = model.selectedImageColor;
 83157        unselectedTextColor = model.unselectedTextColor;
 83158        unselectedImageColor = model.unselectedImageColor;
 159
 83160        onSelect.RemoveAllListeners();
 83161        ConfigureDefaultOnSelectAction();
 83162    }
 163
 164    public void SelectToggle(bool reselectIfAlreadyOn = false)
 165    {
 144166        if (toggle == null)
 0167            return;
 168
 144169        if (reselectIfAlreadyOn)
 43170            toggle.isOn = false;
 171
 144172        if(!toggle.isOn)
 102173            toggle.isOn = true;
 144174    }
 175
 176    public void UnSelectToggle(bool reUnSelectIfAlreadyOff = false)
 177    {
 0178        if (toggle == null)
 0179            return;
 180
 0181        if (reUnSelectIfAlreadyOff)
 0182            toggle.isOn = true;
 183
 0184        if(toggle.isOn)
 0185            toggle.isOn = false;
 0186    }
 187
 188    public void SetSelectedVisuals()
 189    {
 190190        if (selectedIcon != null)
 190191            selectedIcon.gameObject.SetActive(true);
 192
 190193        if (unselectedIcon != null)
 190194            unselectedIcon.gameObject.SetActive(false);
 195
 190196        if (selectedTitle != null)
 190197            selectedTitle.gameObject.SetActive(true);
 198
 190199        if (unselectedTitle != null)
 190200            unselectedTitle.gameObject.SetActive(false);
 201
 190202        toggle.colors = backgroundTransitionColorsForSelected;
 190203    }
 204
 205    public void SetUnselectedVisuals()
 206    {
 86207        if (selectedIcon != null)
 86208            selectedIcon.gameObject.SetActive(false);
 209
 86210        if (unselectedIcon != null)
 86211            unselectedIcon.gameObject.SetActive(true);
 212
 86213        if (selectedTitle != null)
 86214            selectedTitle.gameObject.SetActive(false);
 215
 86216        if (unselectedTitle != null)
 86217            unselectedTitle.gameObject.SetActive(true);
 218
 86219        toggle.colors = backgroundTransitionColorsForUnselected;
 86220    }
 221
 20222    public void SetActive(bool isActive) { gameObject.SetActive(isActive); }
 223
 11224    public bool IsActive() { return gameObject.activeSelf; }
 225
 226    public void SetAsNew(bool isNew)
 227    {
 83228        if (newTag == null)
 0229            return;
 230
 83231        newTag.SetActive(isNew);
 83232    }
 233
 234    internal void ConfigureDefaultOnSelectAction()
 235    {
 532236        onSelect.AddListener((isOn) =>
 237        {
 276238            if (isOn)
 190239                SetSelectedVisuals();
 240            else
 86241                SetUnselectedVisuals();
 86242        });
 532243    }
 244
 245    internal IEnumerator ForceToRefreshToggleState()
 246    {
 247        // After each activation, in order to update the toggle's transition colors correctly, we need to force to chang
 248        // of the component so that Unity notices it is in "dirty" state and it is refreshed.
 451249        toggle.interactable = false;
 451250        yield return null;
 4251        toggle.interactable = true;
 4252    }
 253}