< 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() =>
 52980        ConfigureDefaultOnSelectAction();
 81
 82    private void OnEnable() =>
 62583        StartCoroutine(ForceToRefreshToggleState());
 84
 85    public void OnPointerDown(PointerEventData eventData) =>
 086        SelectToggle();
 87
 088    public RectTransform pivot => transform as RectTransform;
 84989    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    {
 94110        if (model == null)
 0111            return;
 112
 94113        if (selectedTitle != null)
 114        {
 94115            selectedTitle.text = model.selectedTitle;
 94116            selectedTitle.color = model.selectedTextColor;
 117        }
 118
 94119        if (unselectedTitle != null)
 120        {
 94121            unselectedTitle.text = model.unselectedTitle;
 94122            unselectedTitle.color = model.unselectedTextColor;
 123        }
 124
 94125        if (selectedIcon != null)
 126        {
 94127            selectedIcon.sprite = model.selectedIcon;
 94128            selectedIcon.color = model.selectedImageColor;
 129        }
 130
 94131        if (unselectedIcon != null)
 132        {
 94133            unselectedIcon.sprite = model.unselectedIcon;
 94134            unselectedIcon.color = model.unselectedImageColor;
 135        }
 136
 94137        backgroundTransitionColorsForSelected = model.backgroundTransitionColorsForSelected;
 94138        backgroundTransitionColorsForUnselected = model.backgroundTransitionColorsForUnselected;
 94139        selectedTextColor = model.selectedTextColor;
 94140        selectedImageColor = model.selectedImageColor;
 94141        unselectedTextColor = model.unselectedTextColor;
 94142        unselectedImageColor = model.unselectedImageColor;
 143
 94144        onSelect.RemoveAllListeners();
 94145        ConfigureDefaultOnSelectAction();
 94146    }
 147
 148    public void SelectToggle(bool reselectIfAlreadyOn = false)
 149    {
 168150        if (toggle == null)
 0151            return;
 152
 168153        if (reselectIfAlreadyOn)
 54154            toggle.isOn = false;
 155
 168156        toggle.isOn = true;
 168157    }
 158
 159    public void SetSelectedVisuals()
 160    {
 177161        if (selectedIcon != null)
 177162            selectedIcon.gameObject.SetActive(true);
 163
 177164        if (unselectedIcon != null)
 177165            unselectedIcon.gameObject.SetActive(false);
 166
 177167        if (selectedTitle != null)
 177168            selectedTitle.gameObject.SetActive(true);
 169
 177170        if (unselectedTitle != null)
 177171            unselectedTitle.gameObject.SetActive(false);
 172
 177173        toggle.colors = backgroundTransitionColorsForSelected;
 177174    }
 175
 176    public void SetUnselectedVisuals()
 177    {
 95178        if (selectedIcon != null)
 95179            selectedIcon.gameObject.SetActive(false);
 180
 95181        if (unselectedIcon != null)
 95182            unselectedIcon.gameObject.SetActive(true);
 183
 95184        if (selectedTitle != null)
 95185            selectedTitle.gameObject.SetActive(false);
 186
 95187        if (unselectedTitle != null)
 95188            unselectedTitle.gameObject.SetActive(true);
 189
 95190        toggle.colors = backgroundTransitionColorsForUnselected;
 95191    }
 192
 216193    public void SetActive(bool isActive) { gameObject.SetActive(isActive); }
 194
 205195    public bool IsActive() { return gameObject.activeSelf; }
 196
 197    internal void ConfigureDefaultOnSelectAction()
 198    {
 623199        onSelect.AddListener((isOn) =>
 200        {
 272201            if (isOn)
 177202                SetSelectedVisuals();
 203            else
 95204                SetUnselectedVisuals();
 95205        });
 623206    }
 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.
 625212        toggle.interactable = false;
 625213        yield return null;
 2214        toggle.interactable = true;
 2215    }
 216}