| | 1 | | using System.Collections; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | public class Notification : MonoBehaviour |
| | 7 | | { |
| | 8 | | public class Model |
| | 9 | | { |
| | 10 | | public NotificationFactory.Type type; |
| | 11 | | public string message; |
| | 12 | | public string buttonMessage; |
| | 13 | | public float timer; |
| | 14 | | public string scene; |
| | 15 | | public System.Action callback; |
| | 16 | | public string externalCallbackID; |
| | 17 | |
|
| | 18 | | public string groupID; |
| | 19 | | public bool destroyOnFinish = false; |
| | 20 | | } |
| | 21 | |
|
| | 22 | | [SerializeField] internal TextMeshProUGUI messageLabel; |
| | 23 | |
|
| | 24 | | [SerializeField] private Button actionButton; |
| | 25 | |
|
| | 26 | | [SerializeField] private TextMeshProUGUI actionButtonLabel; |
| | 27 | |
|
| 212 | 28 | | public Notification.Model model { get; private set; } = new Model(); |
| | 29 | |
|
| | 30 | | public event System.Action<Notification> OnNotificationDismissed; |
| | 31 | |
|
| | 32 | | Coroutine timerCoroutine; |
| | 33 | |
|
| | 34 | | ShowHideAnimator showHideAnimator; |
| | 35 | |
|
| | 36 | | private void OnEnable() |
| | 37 | | { |
| 4 | 38 | | if (actionButton != null) |
| 4 | 39 | | actionButton.onClick.AddListener(Dismiss); |
| | 40 | |
|
| 4 | 41 | | AudioScriptableObjects.notification.Play(true); |
| 4 | 42 | | } |
| | 43 | |
|
| | 44 | | private void OnDisable() |
| | 45 | | { |
| 4 | 46 | | if (actionButton != null) |
| 4 | 47 | | actionButton.onClick.RemoveAllListeners(); |
| | 48 | |
|
| 4 | 49 | | StopTimer(); |
| | 50 | |
|
| 4 | 51 | | if (showHideAnimator != null) |
| 4 | 52 | | showHideAnimator.OnWillFinishHide -= DismissInternal; |
| 4 | 53 | | } |
| | 54 | |
|
| | 55 | | private void OnDestroy() |
| | 56 | | { |
| 4 | 57 | | if (showHideAnimator != null) |
| 4 | 58 | | showHideAnimator.OnWillFinishHide -= DismissInternal; |
| | 59 | |
|
| 4 | 60 | | StopTimer(); |
| 4 | 61 | | } |
| | 62 | |
|
| | 63 | | public void Show(Notification.Model model) |
| | 64 | | { |
| 6 | 65 | | gameObject.SetActive(true); |
| | 66 | |
|
| 6 | 67 | | if (showHideAnimator == null) |
| 6 | 68 | | showHideAnimator = GetComponent<ShowHideAnimator>(); |
| | 69 | |
|
| 6 | 70 | | if (showHideAnimator != null) |
| | 71 | | { |
| 6 | 72 | | showHideAnimator.OnWillFinishHide -= DismissInternal; |
| 6 | 73 | | showHideAnimator.Show(); |
| | 74 | | } |
| | 75 | |
|
| 6 | 76 | | this.model = model; |
| | 77 | |
|
| 6 | 78 | | if (!string.IsNullOrEmpty(this.model.message)) |
| | 79 | | { |
| 5 | 80 | | messageLabel.text = this.model.message; |
| | 81 | | } |
| | 82 | |
|
| 6 | 83 | | if (!string.IsNullOrEmpty(this.model.buttonMessage)) |
| | 84 | | { |
| 0 | 85 | | actionButtonLabel.text = this.model.buttonMessage; |
| | 86 | | } |
| | 87 | |
|
| 6 | 88 | | if (this.model.timer > 0) |
| | 89 | | { |
| 3 | 90 | | StopTimer(); |
| 3 | 91 | | timerCoroutine = CoroutineStarter.Start(TimerCoroutine(this.model.timer)); |
| | 92 | | } |
| | 93 | |
|
| 6 | 94 | | if (!string.IsNullOrEmpty(this.model.scene)) |
| | 95 | | { |
| 0 | 96 | | string sceneID = CommonScriptableObjects.sceneID ?? string.Empty; |
| 0 | 97 | | CurrentSceneUpdated(sceneID, string.Empty); |
| | 98 | | } |
| | 99 | |
|
| 6 | 100 | | if (actionButton != null) |
| | 101 | | { |
| 4 | 102 | | if (this.model.callback != null) |
| | 103 | | { |
| 0 | 104 | | actionButton.onClick.AddListener(this.model.callback.Invoke); |
| | 105 | | } |
| | 106 | |
|
| 4 | 107 | | if (!string.IsNullOrEmpty(this.model.externalCallbackID)) |
| | 108 | | { |
| 0 | 109 | | actionButton.onClick.AddListener(() => |
| | 110 | | { |
| | 111 | | // TODO: send message to kernel with callbackID |
| 0 | 112 | | }); |
| | 113 | | } |
| | 114 | | } |
| 6 | 115 | | } |
| | 116 | |
|
| | 117 | | private IEnumerator TimerCoroutine(float timer) |
| | 118 | | { |
| 3 | 119 | | yield return WaitForSecondsCache.Get(timer); |
| 1 | 120 | | Dismiss(); |
| 1 | 121 | | } |
| | 122 | |
|
| | 123 | | private void CurrentSceneUpdated(string current, string previous) |
| | 124 | | { |
| 0 | 125 | | if (string.CompareOrdinal(current, model.scene) != 0) |
| | 126 | | { |
| 0 | 127 | | Dismiss(); |
| | 128 | | } |
| 0 | 129 | | } |
| | 130 | |
|
| 2 | 131 | | protected virtual void Dismiss() { Dismiss(false); } |
| | 132 | |
|
| | 133 | | public void Dismiss(bool instant) |
| | 134 | | { |
| 1 | 135 | | StopTimer(); |
| | 136 | |
|
| 1 | 137 | | if (!instant && showHideAnimator != null) |
| | 138 | | { |
| 1 | 139 | | showHideAnimator.OnWillFinishHide -= DismissInternal; |
| 1 | 140 | | showHideAnimator.OnWillFinishHide += DismissInternal; |
| 1 | 141 | | showHideAnimator.Hide(); |
| 1 | 142 | | } |
| | 143 | | else |
| | 144 | | { |
| 0 | 145 | | DismissInternal(); |
| | 146 | | } |
| | 147 | |
|
| 1 | 148 | | if (this != null) |
| | 149 | | { |
| 1 | 150 | | OnNotificationDismissed?.Invoke(this); |
| | 151 | | } |
| 1 | 152 | | } |
| | 153 | |
|
| | 154 | | private void DismissInternal(ShowHideAnimator animator = null) |
| | 155 | | { |
| 1 | 156 | | if (this == null) |
| 0 | 157 | | return; |
| | 158 | |
|
| 1 | 159 | | if (showHideAnimator != null) |
| 1 | 160 | | showHideAnimator.OnWillFinishHide -= DismissInternal; |
| | 161 | |
|
| 1 | 162 | | if (model.destroyOnFinish) |
| 1 | 163 | | Destroy(gameObject); |
| | 164 | | else |
| 0 | 165 | | gameObject.SetActive(false); |
| 0 | 166 | | } |
| | 167 | |
|
| | 168 | | private void StopTimer() |
| | 169 | | { |
| 12 | 170 | | if (timerCoroutine != null) |
| | 171 | | { |
| 1 | 172 | | CoroutineStarter.Stop(timerCoroutine); |
| 1 | 173 | | timerCoroutine = null; |
| | 174 | | } |
| 12 | 175 | | } |
| | 176 | | } |