| | 1 | | using System; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | public interface IToggleComponentView |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Event that will be triggered when the toggle changes. |
| | 10 | | /// </summary> |
| | 11 | | event Action<bool, string, string> OnSelectedChanged; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Id associated to the toogle. |
| | 15 | | /// </summary> |
| | 16 | | string id { get; } |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// On/Off state of the toggle. |
| | 20 | | /// </summary> |
| | 21 | | bool isOn { get; set; } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Text (if exists) associated to the toggle. |
| | 25 | | /// </summary> |
| | 26 | | string title { get; } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Set the toggle text. |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="newText">New text.</param> |
| | 32 | | void SetText(string newText); |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Set toggle text active |
| | 36 | | /// </summary> |
| | 37 | | /// <param name="isActive">Is active</param> |
| | 38 | | void SetTextActive(bool isActive); |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Set the toggle clickable or not. |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="isInteractable">Clickable or not</param> |
| | 44 | | void SetInteractable(bool isInteractable); |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Return if the toggle is Interactable or not |
| | 48 | | /// </summary> |
| | 49 | | bool IsInteractable(); |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Set the state of the toggle as On/Off without notify event. |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="isOn"></param> |
| | 55 | | void SetIsOnWithoutNotify(bool isOn); |
| | 56 | |
|
| | 57 | | void SetChangeTextColorOnSelect(bool changeTextColorOnSelect); |
| | 58 | | } |
| | 59 | |
|
| | 60 | | public class ToggleComponentView : BaseComponentView, IToggleComponentView, IComponentModelConfig<ToggleComponentModel> |
| | 61 | | { |
| | 62 | | [Header("Prefab References")] |
| | 63 | | [SerializeField] internal Toggle toggle; |
| | 64 | | [SerializeField] internal TMP_Text text; |
| | 65 | | [SerializeField] GameObject activeOn = null; |
| | 66 | | [SerializeField] GameObject activeOff = null; |
| | 67 | | [SerializeField] Color textColorOnUnselected; |
| | 68 | | [SerializeField] Color textColorOnSelected; |
| | 69 | |
|
| | 70 | | [Header("Configuration")] |
| | 71 | | [SerializeField] internal ToggleComponentModel model; |
| | 72 | |
|
| | 73 | | public event Action<bool, string, string> OnSelectedChanged; |
| | 74 | |
|
| | 75 | | public string id |
| | 76 | | { |
| 91 | 77 | | get => model.id; |
| 0 | 78 | | set => model.id = value; |
| | 79 | | } |
| | 80 | |
|
| | 81 | | public bool isOn |
| | 82 | | { |
| 27 | 83 | | get => toggle.isOn; |
| | 84 | | set |
| | 85 | | { |
| 915 | 86 | | model.isOn = value; |
| | 87 | |
|
| 915 | 88 | | if (toggle == null) |
| 1 | 89 | | return; |
| | 90 | |
|
| 914 | 91 | | toggle.isOn = value; |
| 914 | 92 | | RefreshActiveStatus(); |
| 914 | 93 | | } |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | public string title => text.text; |
| | 97 | |
|
| | 98 | | public override void Awake() |
| | 99 | | { |
| 552 | 100 | | base.Awake(); |
| | 101 | |
|
| 552 | 102 | | toggle.onValueChanged.AddListener(ToggleChanged); |
| 552 | 103 | | RefreshActiveStatus(); |
| 552 | 104 | | } |
| | 105 | |
|
| | 106 | | private void RefreshActiveStatus() |
| | 107 | | { |
| 1468 | 108 | | if (activeOn) |
| 155 | 109 | | activeOn.gameObject.SetActive(isOn); |
| | 110 | |
|
| 1468 | 111 | | if (activeOff) |
| 155 | 112 | | activeOff.gameObject.SetActive(!isOn); |
| | 113 | |
|
| 1468 | 114 | | if (text != null && model.changeTextColorOnSelect) |
| 953 | 115 | | text.color = isOn ? textColorOnSelected : textColorOnUnselected; |
| 1468 | 116 | | } |
| | 117 | |
|
| | 118 | | private void ToggleChanged(bool isOn) |
| | 119 | | { |
| 373 | 120 | | this.isOn = isOn; |
| 373 | 121 | | OnSelectedChanged?.Invoke(isOn, model.id, model.text); |
| 17 | 122 | | } |
| | 123 | |
|
| | 124 | | public void Configure(ToggleComponentModel newModel) |
| | 125 | | { |
| 464 | 126 | | model = newModel; |
| 464 | 127 | | RefreshControl(); |
| 464 | 128 | | } |
| | 129 | |
|
| | 130 | | public override void RefreshControl() |
| | 131 | | { |
| 466 | 132 | | if (model == null) |
| 0 | 133 | | return; |
| | 134 | |
|
| 466 | 135 | | id = model.id; |
| 466 | 136 | | isOn = model.isOn; |
| 466 | 137 | | SetText(model.text); |
| 466 | 138 | | SetTextActive(model.isTextActive); |
| 466 | 139 | | SetChangeTextColorOnSelect(model.changeTextColorOnSelect); |
| 466 | 140 | | } |
| | 141 | |
|
| | 142 | | public void SetText(string newText) |
| | 143 | | { |
| 467 | 144 | | model.text = newText; |
| | 145 | |
|
| 467 | 146 | | if (text == null) |
| 0 | 147 | | return; |
| | 148 | |
|
| 467 | 149 | | text.text = newText; |
| 467 | 150 | | } |
| | 151 | |
|
| | 152 | | public override void Dispose() |
| | 153 | | { |
| 565 | 154 | | base.Dispose(); |
| | 155 | |
|
| 565 | 156 | | toggle.onValueChanged.RemoveAllListeners(); |
| 565 | 157 | | } |
| | 158 | |
|
| 0 | 159 | | public bool IsInteractable() { return toggle.interactable; } |
| | 160 | |
|
| 4 | 161 | | public void SetInteractable(bool isActive) { toggle.interactable = isActive; } |
| | 162 | |
|
| | 163 | | public void SetTextActive(bool isActive) |
| | 164 | | { |
| 468 | 165 | | model.isTextActive = isActive; |
| 468 | 166 | | text.gameObject.SetActive(isActive); |
| 468 | 167 | | } |
| | 168 | |
|
| | 169 | | public void SetIsOnWithoutNotify(bool isOn) |
| | 170 | | { |
| 2 | 171 | | model.isOn = isOn; |
| | 172 | |
|
| 2 | 173 | | if (toggle == null) |
| 0 | 174 | | return; |
| | 175 | |
|
| 2 | 176 | | toggle.SetIsOnWithoutNotify(isOn); |
| 2 | 177 | | RefreshActiveStatus(); |
| 2 | 178 | | } |
| | 179 | |
|
| 0 | 180 | | public void SetChangeTextColorOnSelect(bool changeTextColorOnSelect) { model.changeTextColorOnSelect = changeTextCol |
| | 181 | | } |