| | 1 | | using System.Collections; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.EventSystems; |
| | 5 | | using UnityEngine.UI; |
| | 6 | | using static UnityEngine.UI.Toggle; |
| | 7 | |
|
| | 8 | | public 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 | |
|
| | 61 | | public 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 | |
|
| 0 | 79 | | public RectTransform pivot => transform as RectTransform; |
| 738 | 80 | | public ToggleEvent onSelect => toggle?.onValueChanged; |
| | 81 | |
|
| 740 | 82 | | private void Awake() { ConfigureDefaultOnSelectAction(); } |
| | 83 | |
|
| 746 | 84 | | private void OnEnable() { StartCoroutine(ForceToRefreshToggleState()); } |
| | 85 | |
|
| | 86 | | public SectionToggleModel GetInfo() |
| | 87 | | { |
| 25 | 88 | | 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 | | { |
| 94 | 105 | | if (model == null) |
| 0 | 106 | | return; |
| | 107 | |
|
| 94 | 108 | | if (selectedTitle != null) |
| | 109 | | { |
| 94 | 110 | | selectedTitle.text = model.selectedTitle; |
| 94 | 111 | | selectedTitle.color = model.selectedTextColor; |
| | 112 | | } |
| | 113 | |
|
| 94 | 114 | | if (unselectedTitle != null) |
| | 115 | | { |
| 94 | 116 | | unselectedTitle.text = model.unselectedTitle; |
| 94 | 117 | | unselectedTitle.color = model.unselectedTextColor; |
| | 118 | | } |
| | 119 | |
|
| 94 | 120 | | if (selectedIcon != null) |
| | 121 | | { |
| 94 | 122 | | selectedIcon.sprite = model.selectedIcon; |
| 94 | 123 | | selectedIcon.color = model.selectedImageColor; |
| | 124 | | } |
| | 125 | |
|
| 94 | 126 | | if (unselectedIcon != null) |
| | 127 | | { |
| 94 | 128 | | unselectedIcon.sprite = model.unselectedIcon; |
| 94 | 129 | | unselectedIcon.color = model.unselectedImageColor; |
| | 130 | | } |
| | 131 | |
|
| 94 | 132 | | backgroundTransitionColorsForSelected = model.backgroundTransitionColorsForSelected; |
| 94 | 133 | | backgroundTransitionColorsForUnselected = model.backgroundTransitionColorsForUnselected; |
| 94 | 134 | | selectedTextColor = model.selectedTextColor; |
| 94 | 135 | | selectedImageColor = model.selectedImageColor; |
| 94 | 136 | | unselectedTextColor = model.unselectedTextColor; |
| 94 | 137 | | unselectedImageColor = model.unselectedImageColor; |
| | 138 | |
|
| 94 | 139 | | onSelect.RemoveAllListeners(); |
| 94 | 140 | | ConfigureDefaultOnSelectAction(); |
| 94 | 141 | | } |
| | 142 | |
|
| | 143 | | public void OnPointerDown(PointerEventData eventData) |
| | 144 | | { |
| 0 | 145 | | SelectToggle(); |
| 0 | 146 | | } |
| | 147 | |
|
| | 148 | | public void SelectToggle(bool reselectIfAlreadyOn = false) |
| | 149 | | { |
| 142 | 150 | | if (toggle == null) |
| 0 | 151 | | return; |
| | 152 | |
|
| 142 | 153 | | if (reselectIfAlreadyOn) |
| 28 | 154 | | toggle.isOn = false; |
| | 155 | |
|
| 142 | 156 | | toggle.isOn = true; |
| 142 | 157 | | } |
| | 158 | |
|
| | 159 | | public void SetSelectedVisuals() |
| | 160 | | { |
| 151 | 161 | | if (selectedIcon != null) |
| 151 | 162 | | selectedIcon.gameObject.SetActive(true); |
| | 163 | |
|
| 151 | 164 | | if (unselectedIcon != null) |
| 151 | 165 | | unselectedIcon.gameObject.SetActive(false); |
| | 166 | |
|
| 151 | 167 | | if (selectedTitle != null) |
| 151 | 168 | | selectedTitle.gameObject.SetActive(true); |
| | 169 | |
|
| 151 | 170 | | if (unselectedTitle != null) |
| 151 | 171 | | unselectedTitle.gameObject.SetActive(false); |
| | 172 | |
|
| 151 | 173 | | toggle.colors = backgroundTransitionColorsForSelected; |
| 151 | 174 | | } |
| | 175 | |
|
| | 176 | | public void SetUnselectedVisuals() |
| | 177 | | { |
| 100 | 178 | | if (selectedIcon != null) |
| 100 | 179 | | selectedIcon.gameObject.SetActive(false); |
| | 180 | |
|
| 100 | 181 | | if (unselectedIcon != null) |
| 100 | 182 | | unselectedIcon.gameObject.SetActive(true); |
| | 183 | |
|
| 100 | 184 | | if (selectedTitle != null) |
| 100 | 185 | | selectedTitle.gameObject.SetActive(false); |
| | 186 | |
|
| 100 | 187 | | if (unselectedTitle != null) |
| 100 | 188 | | unselectedTitle.gameObject.SetActive(true); |
| | 189 | |
|
| 100 | 190 | | toggle.colors = backgroundTransitionColorsForUnselected; |
| 100 | 191 | | } |
| | 192 | |
|
| 24 | 193 | | public void SetActive(bool isActive) { gameObject.SetActive(isActive); } |
| | 194 | |
|
| 13 | 195 | | public bool IsActive() { return gameObject.activeSelf; } |
| | 196 | |
|
| | 197 | | internal void ConfigureDefaultOnSelectAction() |
| | 198 | | { |
| 464 | 199 | | onSelect.AddListener((isOn) => |
| | 200 | | { |
| 251 | 201 | | if (isOn) |
| 151 | 202 | | SetSelectedVisuals(); |
| | 203 | | else |
| 100 | 204 | | SetUnselectedVisuals(); |
| 100 | 205 | | }); |
| 464 | 206 | | } |
| | 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. |
| 373 | 212 | | toggle.interactable = false; |
| 373 | 213 | | yield return null; |
| 0 | 214 | | toggle.interactable = true; |
| 0 | 215 | | } |
| | 216 | | } |