| | 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 | | 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 | |
|
| | 62 | | public 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() => |
| 486 | 81 | | ConfigureDefaultOnSelectAction(); |
| | 82 | |
|
| | 83 | | private void OnEnable() => |
| 582 | 84 | | StartCoroutine(ForceToRefreshToggleState()); |
| | 85 | |
|
| | 86 | | public void OnPointerDown(PointerEventData eventData) => |
| 0 | 87 | | SelectToggle(); |
| | 88 | |
|
| 41 | 89 | | public GameObject GameObject => gameObject; |
| 0 | 90 | | public RectTransform pivot => transform as RectTransform; |
| 1480 | 91 | | public ToggleEvent onSelect => toggle?.onValueChanged; |
| | 92 | |
|
| | 93 | | public SectionToggleModel GetInfo() |
| | 94 | | { |
| 25 | 95 | | 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 | | { |
| 85 | 112 | | if (model == null) |
| 0 | 113 | | return; |
| | 114 | |
|
| 85 | 115 | | if (selectedTitle != null) |
| | 116 | | { |
| 85 | 117 | | selectedTitle.text = model.selectedTitle; |
| 85 | 118 | | selectedTitle.color = model.selectedTextColor; |
| | 119 | | } |
| | 120 | |
|
| 85 | 121 | | if (unselectedTitle != null) |
| | 122 | | { |
| 85 | 123 | | unselectedTitle.text = model.unselectedTitle; |
| 85 | 124 | | unselectedTitle.color = model.unselectedTextColor; |
| | 125 | | } |
| | 126 | |
|
| 85 | 127 | | if (selectedIcon != null) |
| | 128 | | { |
| 85 | 129 | | selectedIcon.sprite = model.selectedIcon; |
| 85 | 130 | | selectedIcon.color = model.selectedImageColor; |
| | 131 | | } |
| | 132 | |
|
| 85 | 133 | | if (unselectedIcon != null) |
| | 134 | | { |
| 85 | 135 | | unselectedIcon.sprite = model.unselectedIcon; |
| 85 | 136 | | unselectedIcon.color = model.unselectedImageColor; |
| | 137 | | } |
| | 138 | |
|
| 85 | 139 | | backgroundTransitionColorsForSelected = model.backgroundTransitionColorsForSelected; |
| 85 | 140 | | backgroundTransitionColorsForUnselected = model.backgroundTransitionColorsForUnselected; |
| 85 | 141 | | selectedTextColor = model.selectedTextColor; |
| 85 | 142 | | selectedImageColor = model.selectedImageColor; |
| 85 | 143 | | unselectedTextColor = model.unselectedTextColor; |
| 85 | 144 | | unselectedImageColor = model.unselectedImageColor; |
| | 145 | |
|
| 85 | 146 | | onSelect.RemoveAllListeners(); |
| 85 | 147 | | ConfigureDefaultOnSelectAction(); |
| 85 | 148 | | } |
| | 149 | |
|
| | 150 | | public void SelectToggle(bool reselectIfAlreadyOn = false) |
| | 151 | | { |
| 152 | 152 | | if (toggle == null) |
| 0 | 153 | | return; |
| | 154 | |
|
| 152 | 155 | | if (reselectIfAlreadyOn) |
| 49 | 156 | | toggle.isOn = false; |
| | 157 | |
|
| 152 | 158 | | if(!toggle.isOn) |
| 105 | 159 | | toggle.isOn = true; |
| 152 | 160 | | } |
| | 161 | |
|
| | 162 | | public void SetSelectedVisuals() |
| | 163 | | { |
| 160 | 164 | | if (selectedIcon != null) |
| 160 | 165 | | selectedIcon.gameObject.SetActive(true); |
| | 166 | |
|
| 160 | 167 | | if (unselectedIcon != null) |
| 160 | 168 | | unselectedIcon.gameObject.SetActive(false); |
| | 169 | |
|
| 160 | 170 | | if (selectedTitle != null) |
| 160 | 171 | | selectedTitle.gameObject.SetActive(true); |
| | 172 | |
|
| 160 | 173 | | if (unselectedTitle != null) |
| 160 | 174 | | unselectedTitle.gameObject.SetActive(false); |
| | 175 | |
|
| 160 | 176 | | toggle.colors = backgroundTransitionColorsForSelected; |
| 160 | 177 | | } |
| | 178 | |
|
| | 179 | | public void SetUnselectedVisuals() |
| | 180 | | { |
| 86 | 181 | | if (selectedIcon != null) |
| 86 | 182 | | selectedIcon.gameObject.SetActive(false); |
| | 183 | |
|
| 86 | 184 | | if (unselectedIcon != null) |
| 86 | 185 | | unselectedIcon.gameObject.SetActive(true); |
| | 186 | |
|
| 86 | 187 | | if (selectedTitle != null) |
| 86 | 188 | | selectedTitle.gameObject.SetActive(false); |
| | 189 | |
|
| 86 | 190 | | if (unselectedTitle != null) |
| 86 | 191 | | unselectedTitle.gameObject.SetActive(true); |
| | 192 | |
|
| 86 | 193 | | toggle.colors = backgroundTransitionColorsForUnselected; |
| 86 | 194 | | } |
| | 195 | |
|
| 212 | 196 | | public void SetActive(bool isActive) { gameObject.SetActive(isActive); } |
| | 197 | |
|
| 203 | 198 | | public bool IsActive() { return gameObject.activeSelf; } |
| | 199 | |
|
| | 200 | | internal void ConfigureDefaultOnSelectAction() |
| | 201 | | { |
| 571 | 202 | | onSelect.AddListener((isOn) => |
| | 203 | | { |
| 246 | 204 | | if (isOn) |
| 160 | 205 | | SetSelectedVisuals(); |
| | 206 | | else |
| 86 | 207 | | SetUnselectedVisuals(); |
| 86 | 208 | | }); |
| 571 | 209 | | } |
| | 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. |
| 582 | 215 | | toggle.interactable = false; |
| 582 | 216 | | yield return null; |
| 2 | 217 | | toggle.interactable = true; |
| 2 | 218 | | } |
| | 219 | | } |