| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.Events; |
| | 7 | | using UnityEngine.UI; |
| | 8 | |
|
| | 9 | | public interface IWelcomeHUDView |
| | 10 | | { |
| | 11 | | void Initialize(UnityAction<int> OnConfirm, UnityAction OnClose, MessageOfTheDayConfig config); |
| | 12 | | void DisposeSelf(); |
| | 13 | | void SetVisible(bool visible); |
| | 14 | | } |
| | 15 | |
|
| | 16 | | public class WelcomeHUDView : MonoBehaviour, IWelcomeHUDView |
| | 17 | | { |
| | 18 | | private const string PREFAB_PATH = "WelcomeHUD"; |
| | 19 | |
|
| | 20 | | [SerializeField] internal RawImage backgroundImage; |
| | 21 | | [SerializeField] internal TextMeshProUGUI titleText; |
| | 22 | | [SerializeField] internal TextMeshProUGUI timeLeftText; |
| | 23 | | [SerializeField] internal TextMeshProUGUI bodyText; |
| | 24 | | [SerializeField] internal Transform buttonsParent; |
| | 25 | | [SerializeField] internal Button_OnPointerDown closeButton; |
| | 26 | |
|
| | 27 | | [Space] |
| | 28 | | [SerializeField] internal GameObject buttonPrefab; |
| | 29 | |
|
| | 30 | | [Space] |
| | 31 | | [SerializeField] internal bool useImageNativeSize = false; |
| | 32 | |
|
| | 33 | | private AssetPromise_Texture texturePromise; |
| | 34 | | private Coroutine updateTimeCoroutine; |
| | 35 | | private UnityAction OnCloseButtonPressed; |
| | 36 | |
|
| | 37 | | private bool disposed = false; |
| | 38 | |
|
| 7 | 39 | | public static WelcomeHUDView CreateView() => Instantiate(Resources.Load<GameObject>(PREFAB_PATH)).GetComponent<Welco |
| | 40 | |
|
| | 41 | | public void Initialize(UnityAction<int> OnConfirm, UnityAction OnClose, MessageOfTheDayConfig config) |
| | 42 | | { |
| 5 | 43 | | if (config == null) |
| | 44 | | { |
| 1 | 45 | | CleanUp(); |
| 1 | 46 | | return; |
| | 47 | | } |
| | 48 | |
|
| 4 | 49 | | ClearButtons(); |
| 4 | 50 | | titleText.text = config.title; |
| 4 | 51 | | bodyText.text = config.body; |
| | 52 | |
|
| 4 | 53 | | SetupButtons(config.buttons, OnConfirm); |
| 4 | 54 | | closeButton.onPointerDown -= OnCloseButtonPressed; |
| 4 | 55 | | OnCloseButtonPressed = () => OnClose?.Invoke(); |
| 4 | 56 | | closeButton.onPointerDown += OnCloseButtonPressed; |
| | 57 | |
|
| 4 | 58 | | CleanUpPromise(); |
| 4 | 59 | | if (!String.IsNullOrEmpty(config.background_banner)) |
| | 60 | | { |
| 0 | 61 | | texturePromise = new AssetPromise_Texture(config.background_banner); |
| 0 | 62 | | texturePromise.OnSuccessEvent += OnTextureRetrieved; |
| | 63 | | } |
| | 64 | |
|
| 4 | 65 | | AssetPromiseKeeper_Texture.i.Keep(texturePromise); |
| | 66 | |
|
| 4 | 67 | | if (config.endUnixTimestamp > 0) |
| | 68 | | { |
| 0 | 69 | | timeLeftText.gameObject.SetActive(false); |
| 0 | 70 | | if (updateTimeCoroutine == null) |
| 0 | 71 | | updateTimeCoroutine = StartCoroutine(UpdateTimer(DateTimeOffset.FromUnixTimeSeconds(config.endUnixTimest |
| 0 | 72 | | } |
| | 73 | | else |
| | 74 | | { |
| 4 | 75 | | timeLeftText.gameObject.SetActive(false); |
| 4 | 76 | | if (updateTimeCoroutine != null) |
| 0 | 77 | | StopCoroutine(updateTimeCoroutine); |
| | 78 | | } |
| 4 | 79 | | } |
| | 80 | |
|
| | 81 | | private void OnTextureRetrieved(Asset_Texture assetTexture) |
| | 82 | | { |
| 0 | 83 | | backgroundImage.texture = assetTexture.texture; |
| | 84 | |
|
| 0 | 85 | | if (useImageNativeSize) |
| 0 | 86 | | backgroundImage.SetNativeSize(); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | private IEnumerator UpdateTimer(DateTime localEndDate) |
| | 90 | | { |
| 0 | 91 | | while (true) |
| | 92 | | { |
| 0 | 93 | | TimeSpan remainingTime = DateTime.Now > localEndDate ? TimeSpan.Zero : (localEndDate - DateTime.Now); |
| 0 | 94 | | timeLeftText.text = $"Ending in: {remainingTime.Days:00}:{remainingTime.Hours:00}:{remainingTime.Minutes:00} |
| 0 | 95 | | yield return new WaitForSeconds(.5f); |
| | 96 | | } |
| | 97 | | } |
| | 98 | |
|
| | 99 | | private void SetupButtons(MessageOfTheDayConfig.Button[] buttons, UnityAction<int> buttonsCallback) |
| | 100 | | { |
| 4 | 101 | | if (buttons == null) |
| 1 | 102 | | return; |
| | 103 | |
|
| 14 | 104 | | for (var i = 0; i < buttons.Length; i++) |
| | 105 | | { |
| 4 | 106 | | int index = i; |
| 4 | 107 | | GameObject buttonGO = Instantiate(buttonPrefab, buttonsParent); |
| | 108 | |
|
| 4 | 109 | | Button_OnPointerDown button = buttonGO.GetComponentInChildren<Button_OnPointerDown>(); |
| 4 | 110 | | if (button != null) |
| 4 | 111 | | button.onPointerDown += () => buttonsCallback?.Invoke(index); |
| | 112 | |
|
| 4 | 113 | | TextMeshProUGUI caption = buttonGO.GetComponentInChildren<TextMeshProUGUI>(); |
| 4 | 114 | | if (caption != null) |
| 4 | 115 | | caption.text = buttons[i].caption; |
| | 116 | |
|
| 4 | 117 | | Image buttonImage = buttonGO.GetComponentInChildren<Image>(); |
| 4 | 118 | | if (buttonImage != null) |
| 4 | 119 | | buttonImage.color = buttons[i].tint; |
| | 120 | | } |
| 3 | 121 | | } |
| | 122 | |
|
| | 123 | | private void ClearButtons() |
| | 124 | | { |
| 32 | 125 | | for (int i = buttonsParent.childCount - 1; i >= 0; i--) |
| | 126 | | { |
| 4 | 127 | | Destroy(buttonsParent.GetChild(i).gameObject); |
| | 128 | | } |
| 12 | 129 | | } |
| | 130 | |
|
| | 131 | | private void CleanUp() |
| | 132 | | { |
| 8 | 133 | | CleanUpPromise(); |
| 8 | 134 | | ClearButtons(); |
| 8 | 135 | | closeButton.onPointerDown -= OnCloseButtonPressed; |
| 8 | 136 | | } |
| | 137 | |
|
| | 138 | | private void CleanUpPromise() |
| | 139 | | { |
| 12 | 140 | | if (texturePromise == null) |
| 12 | 141 | | return; |
| | 142 | |
|
| 0 | 143 | | AssetPromiseKeeper_Texture.i.Forget(texturePromise); |
| 0 | 144 | | texturePromise.Cleanup(); |
| 0 | 145 | | texturePromise = null; |
| 0 | 146 | | } |
| | 147 | |
|
| | 148 | | public void DisposeSelf() |
| | 149 | | { |
| 4 | 150 | | if (!disposed) |
| | 151 | | { |
| 4 | 152 | | disposed = true; |
| 4 | 153 | | Destroy(gameObject); |
| | 154 | | } |
| 4 | 155 | | } |
| | 156 | |
|
| 16 | 157 | | public void SetVisible(bool visible) { gameObject.SetActive(visible); } |
| | 158 | |
|
| | 159 | | private void OnDestroy() |
| | 160 | | { |
| 7 | 161 | | disposed = true; |
| 7 | 162 | | CleanUp(); |
| 7 | 163 | | } |
| | 164 | | } |