| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Interface; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Model with the configuration for a Message of the Day |
| | 9 | | /// </summary> |
| | 10 | | [Serializable] |
| | 11 | | public class MessageOfTheDayConfig |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Model with the configuration for each button in the Message of the Day |
| | 15 | | /// </summary> |
| | 16 | | [Serializable] |
| | 17 | | public class Button |
| | 18 | | { |
| | 19 | | public string caption; |
| | 20 | | public string action; //global chat action to perform |
| | 21 | | public Color tint; |
| | 22 | | } |
| | 23 | |
|
| | 24 | | public string background_banner; |
| | 25 | | public int endUnixTimestamp; |
| | 26 | | public string title; |
| | 27 | | public string body; |
| | 28 | | public Button[] buttons; |
| | 29 | | } |
| | 30 | |
|
| | 31 | | public class WelcomeHUDController : IHUD |
| | 32 | | { |
| | 33 | | const float POPUP_DELAY = 2; |
| | 34 | |
|
| | 35 | | internal IWelcomeHUDView view; |
| | 36 | | private MessageOfTheDayConfig config = null; |
| | 37 | | private Coroutine showPopupDelayedRoutine; |
| | 38 | | private bool isPopupRoutineRunning = false; |
| | 39 | |
|
| 4 | 40 | | internal virtual IWelcomeHUDView CreateView() => WelcomeHUDView.CreateView(); |
| | 41 | |
|
| 4 | 42 | | public WelcomeHUDController() |
| | 43 | | { |
| 4 | 44 | | view = CreateView(); |
| 4 | 45 | | view.SetVisible(false); |
| | 46 | |
|
| 4 | 47 | | CommonScriptableObjects.tutorialActive.OnChange -= TutorialActive_OnChange; |
| 4 | 48 | | CommonScriptableObjects.tutorialActive.OnChange += TutorialActive_OnChange; |
| | 49 | |
|
| 4 | 50 | | CommonScriptableObjects.emailPromptActive.OnChange -= EmailPromptActive_OnChange; |
| 4 | 51 | | CommonScriptableObjects.emailPromptActive.OnChange += EmailPromptActive_OnChange; |
| 4 | 52 | | } |
| | 53 | |
|
| | 54 | | public void Initialize(MessageOfTheDayConfig config) |
| | 55 | | { |
| 3 | 56 | | this.config = config; |
| | 57 | |
|
| 3 | 58 | | if (!isPopupRoutineRunning) |
| 3 | 59 | | StartPopupRoutine(); |
| | 60 | | else |
| 0 | 61 | | ResetPopupDelayed(); |
| 0 | 62 | | } |
| | 63 | |
|
| 6 | 64 | | internal void Close() { SetVisibility(false); } |
| | 65 | |
|
| | 66 | | internal virtual void OnConfirmPressed(int buttonIndex) |
| | 67 | | { |
| 3 | 68 | | Close(); |
| | 69 | |
|
| 3 | 70 | | if (config?.buttons != null && buttonIndex >= 0 && buttonIndex < config?.buttons.Length) |
| | 71 | | { |
| 1 | 72 | | string action = config.buttons[buttonIndex].action; |
| 1 | 73 | | SendAction(action); |
| | 74 | | } |
| 3 | 75 | | } |
| | 76 | |
|
| 0 | 77 | | void OnClosePressed() { Close(); } |
| | 78 | |
|
| | 79 | | public void Dispose() |
| | 80 | | { |
| 4 | 81 | | StopPopupRoutine(); |
| | 82 | |
|
| 4 | 83 | | CommonScriptableObjects.tutorialActive.OnChange -= TutorialActive_OnChange; |
| 4 | 84 | | CommonScriptableObjects.emailPromptActive.OnChange -= EmailPromptActive_OnChange; |
| | 85 | |
|
| 4 | 86 | | view?.DisposeSelf(); |
| 4 | 87 | | } |
| | 88 | |
|
| | 89 | | public void SetVisibility(bool visible) |
| | 90 | | { |
| 4 | 91 | | view.SetVisible(visible); |
| 4 | 92 | | if (visible) |
| | 93 | | { |
| 1 | 94 | | Utils.UnlockCursor(); |
| 1 | 95 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 1 | 96 | | } |
| | 97 | | else |
| | 98 | | { |
| 3 | 99 | | AudioScriptableObjects.dialogClose.Play(true); |
| | 100 | | } |
| | 101 | |
|
| 4 | 102 | | CommonScriptableObjects.motdActive.Set(visible); |
| 4 | 103 | | } |
| | 104 | |
|
| | 105 | | internal virtual void SendAction(string action) |
| | 106 | | { |
| 1 | 107 | | if (string.IsNullOrEmpty(action)) |
| 0 | 108 | | return; |
| | 109 | |
|
| 1 | 110 | | WebInterface.SendChatMessage(new ChatMessage |
| | 111 | | { |
| | 112 | | messageType = ChatMessage.Type.NONE, |
| | 113 | | recipient = string.Empty, |
| | 114 | | body = action, |
| | 115 | | }); |
| 1 | 116 | | } |
| | 117 | |
|
| 6 | 118 | | void StartPopupRoutine() { showPopupDelayedRoutine = CoroutineStarter.Start(ShowPopupDelayed(POPUP_DELAY)); } |
| | 119 | |
|
| | 120 | | void StopPopupRoutine() |
| | 121 | | { |
| 4 | 122 | | if (showPopupDelayedRoutine != null) |
| | 123 | | { |
| 3 | 124 | | CoroutineStarter.Stop(showPopupDelayedRoutine); |
| 3 | 125 | | showPopupDelayedRoutine = null; |
| | 126 | | } |
| | 127 | |
|
| 4 | 128 | | isPopupRoutineRunning = false; |
| 4 | 129 | | } |
| | 130 | |
|
| | 131 | | private IEnumerator ShowPopupDelayed(float seconds) |
| | 132 | | { |
| 3 | 133 | | isPopupRoutineRunning = true; |
| 3 | 134 | | view.Initialize(OnConfirmPressed, OnClosePressed, config); |
| | 135 | |
|
| 6 | 136 | | yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get()); |
| 2 | 137 | | yield return new WaitUntil(() => !CommonScriptableObjects.tutorialActive.Get()); |
| 0 | 138 | | yield return new WaitUntil(() => !CommonScriptableObjects.emailPromptActive.Get()); |
| 0 | 139 | | yield return WaitForSecondsCache.Get(seconds); |
| 0 | 140 | | yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get()); |
| | 141 | |
|
| 0 | 142 | | SetVisibility(true); |
| 0 | 143 | | isPopupRoutineRunning = false; |
| 0 | 144 | | } |
| | 145 | |
|
| | 146 | | void ResetPopupDelayed() |
| | 147 | | { |
| 0 | 148 | | if (isPopupRoutineRunning) |
| | 149 | | { |
| 0 | 150 | | StopPopupRoutine(); |
| 0 | 151 | | StartPopupRoutine(); |
| | 152 | | } |
| 0 | 153 | | } |
| | 154 | |
|
| | 155 | | private void TutorialActive_OnChange(bool current, bool previous) |
| | 156 | | { |
| 0 | 157 | | if (current) |
| 0 | 158 | | ResetPopupDelayed(); |
| 0 | 159 | | } |
| | 160 | |
|
| | 161 | | private void EmailPromptActive_OnChange(bool current, bool previous) |
| | 162 | | { |
| 0 | 163 | | if (current) |
| 0 | 164 | | ResetPopupDelayed(); |
| 0 | 165 | | } |
| | 166 | | } |