| | 1 | | using System.Collections; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | | using static UnityEngine.UI.Toggle; |
| | 6 | |
|
| | 7 | | public interface ISectionToggle |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Event that will be triggered when the toggle is selected. |
| | 11 | | /// </summary> |
| | 12 | | ToggleEvent onSelect { get; } |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Get the toggle info. |
| | 16 | | /// </summary> |
| | 17 | | /// <returns>Model with all the toggle info.</returns> |
| | 18 | | SectionToggleModel GetInfo(); |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Set the toggle info. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="model">Model with all the toggle info.</param> |
| | 24 | | void SetInfo(SectionToggleModel model); |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Invoke the action of selecting the toggle. |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="reselectIfAlreadyOn">True for apply the selection even if the toggle was already off.</param> |
| | 30 | | void SelectToggle(bool reselectIfAlreadyOn = false); |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Set the toggle visuals as selected. |
| | 34 | | /// </summary> |
| | 35 | | void SetSelectedVisuals(); |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Set the toggle visuals as unselected. |
| | 39 | | /// </summary> |
| | 40 | | void SetUnselectedVisuals(); |
| | 41 | | } |
| | 42 | |
|
| | 43 | | public class SectionToggle : MonoBehaviour, ISectionToggle |
| | 44 | | { |
| | 45 | | [SerializeField] private Toggle toggle; |
| | 46 | | [SerializeField] private TMP_Text sectionText; |
| | 47 | | [SerializeField] private Image sectionImage; |
| | 48 | |
|
| | 49 | | [Header("Visual Configuration When Selected")] |
| | 50 | | [SerializeField] private ColorBlock backgroundTransitionColorsForSelected; |
| | 51 | | [SerializeField] private Color selectedTextColor; |
| | 52 | | [SerializeField] private Color selectedImageColor; |
| | 53 | |
|
| | 54 | | [Header("Visual Configuration When Unselected")] |
| | 55 | | [SerializeField] private ColorBlock backgroundTransitionColorsForUnselected; |
| | 56 | | [SerializeField] private Color unselectedTextColor; |
| | 57 | | [SerializeField] private Color unselectedImageColor; |
| | 58 | |
|
| 139 | 59 | | public ToggleEvent onSelect => toggle?.onValueChanged; |
| | 60 | |
|
| 510 | 61 | | private void Awake() { ConfigureDefaultOnSelectAction(); } |
| | 62 | |
|
| 510 | 63 | | private void OnEnable() { StartCoroutine(ForceToRefreshToggleState()); } |
| | 64 | |
|
| | 65 | | public SectionToggleModel GetInfo() |
| | 66 | | { |
| 7 | 67 | | return new SectionToggleModel |
| | 68 | | { |
| | 69 | | icon = sectionImage.sprite, |
| | 70 | | title = sectionText.text |
| | 71 | | }; |
| | 72 | | } |
| | 73 | |
|
| | 74 | | public void SetInfo(SectionToggleModel model) |
| | 75 | | { |
| 115 | 76 | | if (model == null) |
| 0 | 77 | | return; |
| | 78 | |
|
| 115 | 79 | | if (sectionText != null) |
| 115 | 80 | | sectionText.text = model.title; |
| | 81 | |
|
| 115 | 82 | | if (sectionImage != null) |
| | 83 | | { |
| 115 | 84 | | sectionImage.enabled = model.icon != null; |
| 115 | 85 | | sectionImage.sprite = model.icon; |
| | 86 | | } |
| | 87 | |
|
| 115 | 88 | | backgroundTransitionColorsForSelected = model.backgroundTransitionColorsForSelected; |
| 115 | 89 | | backgroundTransitionColorsForUnselected = model.backgroundTransitionColorsForUnselected; |
| 115 | 90 | | selectedTextColor = model.selectedTextColor; |
| 115 | 91 | | selectedImageColor = model.selectedImageColor; |
| 115 | 92 | | unselectedTextColor = model.unselectedTextColor; |
| 115 | 93 | | unselectedImageColor = model.unselectedImageColor; |
| | 94 | |
|
| 115 | 95 | | onSelect.RemoveAllListeners(); |
| 115 | 96 | | ConfigureDefaultOnSelectAction(); |
| 115 | 97 | | } |
| | 98 | |
|
| | 99 | | public void SelectToggle(bool reselectIfAlreadyOn = false) |
| | 100 | | { |
| 32 | 101 | | if (toggle == null) |
| 0 | 102 | | return; |
| | 103 | |
|
| 32 | 104 | | if (reselectIfAlreadyOn) |
| 11 | 105 | | toggle.isOn = false; |
| | 106 | |
|
| 32 | 107 | | toggle.isOn = true; |
| 32 | 108 | | } |
| | 109 | |
|
| | 110 | | public void SetSelectedVisuals() |
| | 111 | | { |
| 65 | 112 | | toggle.colors = backgroundTransitionColorsForSelected; |
| 65 | 113 | | sectionText.color = selectedTextColor; |
| 65 | 114 | | sectionImage.color = selectedImageColor; |
| 65 | 115 | | } |
| | 116 | |
|
| | 117 | | public void SetUnselectedVisuals() |
| | 118 | | { |
| 122 | 119 | | toggle.colors = backgroundTransitionColorsForUnselected; |
| 122 | 120 | | sectionText.color = unselectedTextColor; |
| 122 | 121 | | sectionImage.color = unselectedImageColor; |
| 122 | 122 | | } |
| | 123 | |
|
| | 124 | | internal void ConfigureDefaultOnSelectAction() |
| | 125 | | { |
| 370 | 126 | | onSelect.AddListener((isOn) => |
| | 127 | | { |
| 187 | 128 | | if (isOn) |
| 65 | 129 | | SetSelectedVisuals(); |
| | 130 | | else |
| 122 | 131 | | SetUnselectedVisuals(); |
| 122 | 132 | | }); |
| 370 | 133 | | } |
| | 134 | |
|
| | 135 | | internal IEnumerator ForceToRefreshToggleState() |
| | 136 | | { |
| | 137 | | // After each activation, in order to update the toggle's transition colors correctly, we need to force to chang |
| | 138 | | // of the component so that Unity notices it is in "dirty" state and it is refreshed. |
| 255 | 139 | | toggle.interactable = false; |
| 255 | 140 | | yield return null; |
| 0 | 141 | | toggle.interactable = true; |
| 0 | 142 | | } |
| | 143 | | } |