| | 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 | | CommonScriptableObjects.tutorialActive.OnChange -= TutorialActive_OnChange; |
| 4 | 82 | | CommonScriptableObjects.emailPromptActive.OnChange -= EmailPromptActive_OnChange; |
| | 83 | |
|
| 4 | 84 | | view?.DisposeSelf(); |
| 4 | 85 | | } |
| | 86 | |
|
| | 87 | | public void SetVisibility(bool visible) |
| | 88 | | { |
| 4 | 89 | | view.SetVisible(visible); |
| 4 | 90 | | if (visible) |
| | 91 | | { |
| 1 | 92 | | Utils.UnlockCursor(); |
| 1 | 93 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 1 | 94 | | } |
| | 95 | | else |
| | 96 | | { |
| 3 | 97 | | AudioScriptableObjects.dialogClose.Play(true); |
| | 98 | | } |
| | 99 | |
|
| 4 | 100 | | CommonScriptableObjects.motdActive.Set(visible); |
| 4 | 101 | | } |
| | 102 | |
|
| | 103 | | internal virtual void SendAction(string action) |
| | 104 | | { |
| 1 | 105 | | if (string.IsNullOrEmpty(action)) |
| 0 | 106 | | return; |
| | 107 | |
|
| 1 | 108 | | WebInterface.SendChatMessage(new ChatMessage |
| | 109 | | { |
| | 110 | | messageType = ChatMessage.Type.NONE, |
| | 111 | | recipient = string.Empty, |
| | 112 | | body = action, |
| | 113 | | }); |
| 1 | 114 | | } |
| | 115 | |
|
| 6 | 116 | | void StartPopupRoutine() { showPopupDelayedRoutine = CoroutineStarter.Start(ShowPopupDelayed(POPUP_DELAY)); } |
| | 117 | |
|
| | 118 | | void StopPopupRoutine() |
| | 119 | | { |
| 0 | 120 | | if (showPopupDelayedRoutine != null) |
| | 121 | | { |
| 0 | 122 | | CoroutineStarter.Stop(showPopupDelayedRoutine); |
| 0 | 123 | | showPopupDelayedRoutine = null; |
| | 124 | | } |
| 0 | 125 | | isPopupRoutineRunning = false; |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | private IEnumerator ShowPopupDelayed(float seconds) |
| | 129 | | { |
| 3 | 130 | | isPopupRoutineRunning = true; |
| 3 | 131 | | view.Initialize(OnConfirmPressed, OnClosePressed, config); |
| | 132 | |
|
| 6 | 133 | | yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get()); |
| 6 | 134 | | yield return new WaitUntil(() => !CommonScriptableObjects.tutorialActive.Get()); |
| 6 | 135 | | yield return new WaitUntil(() => !CommonScriptableObjects.emailPromptActive.Get()); |
| 3 | 136 | | yield return WaitForSecondsCache.Get(seconds); |
| 0 | 137 | | yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get()); |
| | 138 | |
|
| 0 | 139 | | SetVisibility(true); |
| 0 | 140 | | isPopupRoutineRunning = false; |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | void ResetPopupDelayed() |
| | 144 | | { |
| 0 | 145 | | if (isPopupRoutineRunning) |
| | 146 | | { |
| 0 | 147 | | StopPopupRoutine(); |
| 0 | 148 | | StartPopupRoutine(); |
| | 149 | | } |
| 0 | 150 | | } |
| | 151 | |
|
| | 152 | | private void TutorialActive_OnChange(bool current, bool previous) |
| | 153 | | { |
| 0 | 154 | | if (current) |
| 0 | 155 | | ResetPopupDelayed(); |
| 0 | 156 | | } |
| | 157 | |
|
| | 158 | | private void EmailPromptActive_OnChange(bool current, bool previous) |
| | 159 | | { |
| 0 | 160 | | if (current) |
| 0 | 161 | | ResetPopupDelayed(); |
| 0 | 162 | | } |
| | 163 | | } |