| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.EventSystems; |
| | 6 | | using UnityEngine.UI; |
| | 7 | | using static UnityEngine.UI.Toggle; |
| | 8 | |
|
| | 9 | | public 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 | |
|
| | 75 | | public 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() => |
| 449 | 95 | | ConfigureDefaultOnSelectAction(); |
| | 96 | |
|
| | 97 | | private void OnEnable() => |
| 451 | 98 | | StartCoroutine(ForceToRefreshToggleState()); |
| | 99 | |
|
| 76 | 100 | | public GameObject GameObject => gameObject; |
| 0 | 101 | | public RectTransform pivot => transform as RectTransform; |
| 1438 | 102 | | public ToggleEvent onSelect => toggle != null ? toggle.onValueChanged : null; |
| | 103 | |
|
| | 104 | | public SectionToggleModel GetInfo() |
| | 105 | | { |
| 25 | 106 | | 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 | | { |
| 83 | 124 | | if (model == null) |
| 0 | 125 | | return; |
| | 126 | |
|
| 83 | 127 | | if (selectedTitle != null) |
| | 128 | | { |
| 83 | 129 | | selectedTitle.text = model.selectedTitle; |
| 83 | 130 | | selectedTitle.color = model.selectedTextColor; |
| | 131 | | } |
| | 132 | |
|
| 83 | 133 | | if (unselectedTitle != null) |
| | 134 | | { |
| 83 | 135 | | unselectedTitle.text = model.unselectedTitle; |
| 83 | 136 | | unselectedTitle.color = model.unselectedTextColor; |
| | 137 | | } |
| | 138 | |
|
| 83 | 139 | | if (selectedIcon != null) |
| | 140 | | { |
| 83 | 141 | | selectedIcon.sprite = model.selectedIcon; |
| 83 | 142 | | selectedIcon.color = model.selectedImageColor; |
| | 143 | | } |
| | 144 | |
|
| 83 | 145 | | if (unselectedIcon != null) |
| | 146 | | { |
| 83 | 147 | | unselectedIcon.sprite = model.unselectedIcon; |
| 83 | 148 | | unselectedIcon.color = model.unselectedImageColor; |
| | 149 | | } |
| | 150 | |
|
| 83 | 151 | | SetAsNew(model.showNewTag); |
| | 152 | |
|
| 83 | 153 | | backgroundTransitionColorsForSelected = model.backgroundTransitionColorsForSelected; |
| 83 | 154 | | backgroundTransitionColorsForUnselected = model.backgroundTransitionColorsForUnselected; |
| 83 | 155 | | selectedTextColor = model.selectedTextColor; |
| 83 | 156 | | selectedImageColor = model.selectedImageColor; |
| 83 | 157 | | unselectedTextColor = model.unselectedTextColor; |
| 83 | 158 | | unselectedImageColor = model.unselectedImageColor; |
| | 159 | |
|
| 83 | 160 | | onSelect.RemoveAllListeners(); |
| 83 | 161 | | ConfigureDefaultOnSelectAction(); |
| 83 | 162 | | } |
| | 163 | |
|
| | 164 | | public void SelectToggle(bool reselectIfAlreadyOn = false) |
| | 165 | | { |
| 144 | 166 | | if (toggle == null) |
| 0 | 167 | | return; |
| | 168 | |
|
| 144 | 169 | | if (reselectIfAlreadyOn) |
| 43 | 170 | | toggle.isOn = false; |
| | 171 | |
|
| 144 | 172 | | if(!toggle.isOn) |
| 102 | 173 | | toggle.isOn = true; |
| 144 | 174 | | } |
| | 175 | |
|
| | 176 | | public void UnSelectToggle(bool reUnSelectIfAlreadyOff = false) |
| | 177 | | { |
| 0 | 178 | | if (toggle == null) |
| 0 | 179 | | return; |
| | 180 | |
|
| 0 | 181 | | if (reUnSelectIfAlreadyOff) |
| 0 | 182 | | toggle.isOn = true; |
| | 183 | |
|
| 0 | 184 | | if(toggle.isOn) |
| 0 | 185 | | toggle.isOn = false; |
| 0 | 186 | | } |
| | 187 | |
|
| | 188 | | public void SetSelectedVisuals() |
| | 189 | | { |
| 190 | 190 | | if (selectedIcon != null) |
| 190 | 191 | | selectedIcon.gameObject.SetActive(true); |
| | 192 | |
|
| 190 | 193 | | if (unselectedIcon != null) |
| 190 | 194 | | unselectedIcon.gameObject.SetActive(false); |
| | 195 | |
|
| 190 | 196 | | if (selectedTitle != null) |
| 190 | 197 | | selectedTitle.gameObject.SetActive(true); |
| | 198 | |
|
| 190 | 199 | | if (unselectedTitle != null) |
| 190 | 200 | | unselectedTitle.gameObject.SetActive(false); |
| | 201 | |
|
| 190 | 202 | | toggle.colors = backgroundTransitionColorsForSelected; |
| 190 | 203 | | } |
| | 204 | |
|
| | 205 | | public void SetUnselectedVisuals() |
| | 206 | | { |
| 86 | 207 | | if (selectedIcon != null) |
| 86 | 208 | | selectedIcon.gameObject.SetActive(false); |
| | 209 | |
|
| 86 | 210 | | if (unselectedIcon != null) |
| 86 | 211 | | unselectedIcon.gameObject.SetActive(true); |
| | 212 | |
|
| 86 | 213 | | if (selectedTitle != null) |
| 86 | 214 | | selectedTitle.gameObject.SetActive(false); |
| | 215 | |
|
| 86 | 216 | | if (unselectedTitle != null) |
| 86 | 217 | | unselectedTitle.gameObject.SetActive(true); |
| | 218 | |
|
| 86 | 219 | | toggle.colors = backgroundTransitionColorsForUnselected; |
| 86 | 220 | | } |
| | 221 | |
|
| 20 | 222 | | public void SetActive(bool isActive) { gameObject.SetActive(isActive); } |
| | 223 | |
|
| 11 | 224 | | public bool IsActive() { return gameObject.activeSelf; } |
| | 225 | |
|
| | 226 | | public void SetAsNew(bool isNew) |
| | 227 | | { |
| 83 | 228 | | if (newTag == null) |
| 0 | 229 | | return; |
| | 230 | |
|
| 83 | 231 | | newTag.SetActive(isNew); |
| 83 | 232 | | } |
| | 233 | |
|
| | 234 | | internal void ConfigureDefaultOnSelectAction() |
| | 235 | | { |
| 532 | 236 | | onSelect.AddListener((isOn) => |
| | 237 | | { |
| 276 | 238 | | if (isOn) |
| 190 | 239 | | SetSelectedVisuals(); |
| | 240 | | else |
| 86 | 241 | | SetUnselectedVisuals(); |
| 86 | 242 | | }); |
| 532 | 243 | | } |
| | 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. |
| 451 | 249 | | toggle.interactable = false; |
| 451 | 250 | | yield return null; |
| 4 | 251 | | toggle.interactable = true; |
| 4 | 252 | | } |
| | 253 | | } |