| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | namespace DCL.Builder |
| | 9 | | { |
| | 10 | | public interface IPublishProgressView |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// This represent the current state of the publishing view |
| | 14 | | /// </summary> |
| | 15 | | enum PublishStatus |
| | 16 | | { |
| | 17 | | IDLE = 0, |
| | 18 | | CONFIRM = 1, |
| | 19 | | PUBLISHING = 2, |
| | 20 | | ERROR = 3, |
| | 21 | | SUCCESS = 4 |
| | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// This action will be called when the confirm button is pressed |
| | 26 | | /// </summary> |
| | 27 | | event Action OnPublishConfirmButtonPressed; |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// This action will be called when the view is closed |
| | 31 | | /// </summary> |
| | 32 | | event Action OnViewClosed; |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Whe should go back to the previous view |
| | 36 | | /// </summary> |
| | 37 | | event Action OnBackPressed; |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Show the confirm publish pop up |
| | 41 | | /// </summary> |
| | 42 | | void ConfirmDeployment(); |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Call this function when the publish start |
| | 46 | | /// </summary> |
| | 47 | | void PublishStarted(); |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Hide the view |
| | 51 | | /// </summary> |
| | 52 | | void Hide(); |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Shows the errors passes in a pop up |
| | 56 | | /// </summary> |
| | 57 | | /// <param name="message"></param> |
| | 58 | | void PublishError(string message); |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Shows that the project has been successfully published |
| | 62 | | /// </summary> |
| | 63 | | void ProjectPublished(); |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Sets the project to display |
| | 67 | | /// </summary> |
| | 68 | | /// <param name="publishedProject"></param> |
| | 69 | | void SetPublishInfo(IBuilderScene publishedProject, PublishInfo info); |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Dispose the view |
| | 73 | | /// </summary> |
| | 74 | | void Dispose(); |
| | 75 | | } |
| | 76 | |
|
| | 77 | | public class PublishProgressView : BaseComponentView, IPublishProgressView |
| | 78 | | { |
| | 79 | | // Main titles |
| | 80 | | private const string CONFIRM_MAIN_TEXT = "Are you sure you want to publish your project?"; |
| | 81 | | private const string ERROR_MAIN_TEXT = "The publication of your project has failed!"; |
| | 82 | | private const string SUCCESS_MAIN_TEXT = "Project successfully published!"; |
| | 83 | | private const string PUBLISHING_MAIN_TEXT = "Publishing"; |
| | 84 | |
|
| | 85 | | // Sub titles |
| | 86 | | private const string CONFIRM_SUB_TITLE_TEXT = @"{0} will be deployed in {1},{2}"; |
| | 87 | | private const string PUBLISHING_SUB_TITLE_TEXT = @"{0} is being deployed in {1},{2}"; |
| | 88 | | private const string SUCCESS_SUB_TITLE_TEXT = @"{0} is now a live place in [landName]{1},{2}"; |
| | 89 | |
|
| | 90 | | public event Action OnPublishConfirmButtonPressed; |
| | 91 | | public event Action OnViewClosed; |
| | 92 | | public event Action OnBackPressed; |
| | 93 | |
|
| | 94 | | [Header("General references")] |
| | 95 | | [SerializeField] internal TMP_Text mainTitleTextView; |
| | 96 | | [SerializeField] internal TMP_Text subTitleTextView; |
| | 97 | | [SerializeField] internal RawImage screenshotImage; |
| | 98 | | [SerializeField] internal ModalComponentView modal; |
| | 99 | |
|
| | 100 | | [Header("Confirm Popup references")] |
| | 101 | | [SerializeField] internal GameObject confirmGameObject; |
| | 102 | | [SerializeField] internal Button noButton; |
| | 103 | | [SerializeField] internal Button yesButton; |
| | 104 | |
|
| | 105 | | [Header("Publish Progress references")] |
| | 106 | | [SerializeField] internal GameObject progressGameObject; |
| | 107 | | [SerializeField] internal LoadingBar loadingBar; |
| | 108 | | [Header("Error references")] |
| | 109 | | [SerializeField] internal TMP_Text errorTextView; |
| | 110 | | [SerializeField] internal GameObject errorGameObject; |
| | 111 | | [SerializeField] internal Button cancelButton; |
| | 112 | | [SerializeField] internal Button retryButton; |
| | 113 | |
|
| | 114 | | [Header("Success references")] |
| | 115 | | [SerializeField] internal Button okButton; |
| | 116 | | [SerializeField] internal GameObject successGameObject; |
| | 117 | | private IPublishProgressView.PublishStatus currentStatus; |
| | 118 | | private float currentProgress = 0; |
| | 119 | | private Coroutine fakeProgressCoroutine; |
| | 120 | | private IBuilderScene currentScene; |
| | 121 | | private PublishInfo currentInfo; |
| | 122 | |
|
| 0 | 123 | | public override void RefreshControl() { } |
| | 124 | |
|
| | 125 | | public override void Awake() |
| | 126 | | { |
| 0 | 127 | | base.Awake(); |
| 0 | 128 | | cancelButton.onClick.AddListener(Close); |
| 0 | 129 | | noButton.onClick.AddListener(Back); |
| 0 | 130 | | okButton.onClick.AddListener(Close); |
| | 131 | |
|
| 0 | 132 | | yesButton.onClick.AddListener(ConfirmPublish); |
| 0 | 133 | | retryButton.onClick.AddListener(ConfirmPublish); |
| | 134 | |
|
| 0 | 135 | | gameObject.SetActive(false); |
| 0 | 136 | | modal.OnCloseAction += Close; |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | public override void Dispose() |
| | 140 | | { |
| 0 | 141 | | base.Dispose(); |
| 0 | 142 | | cancelButton.onClick.RemoveAllListeners(); |
| 0 | 143 | | noButton.onClick.RemoveAllListeners(); |
| 0 | 144 | | yesButton.onClick.RemoveAllListeners(); |
| 0 | 145 | | okButton.onClick.RemoveAllListeners(); |
| 0 | 146 | | retryButton.onClick.RemoveAllListeners(); |
| | 147 | |
|
| 0 | 148 | | if (fakeProgressCoroutine != null) |
| 0 | 149 | | StopCoroutine(fakeProgressCoroutine); |
| | 150 | |
|
| 0 | 151 | | modal.OnCloseAction -= Close; |
| 0 | 152 | | } |
| | 153 | |
|
| | 154 | | private void ShowCurrentStatus() |
| | 155 | | { |
| 0 | 156 | | successGameObject.SetActive(false); |
| 0 | 157 | | errorGameObject.SetActive(false); |
| 0 | 158 | | confirmGameObject.SetActive(false); |
| 0 | 159 | | progressGameObject.SetActive(false); |
| 0 | 160 | | modal.CanBeCancelled(true); |
| | 161 | |
|
| 0 | 162 | | switch (currentStatus) |
| | 163 | | { |
| | 164 | | case IPublishProgressView.PublishStatus.CONFIRM: |
| 0 | 165 | | ShowConfirmPopUp(); |
| 0 | 166 | | break; |
| | 167 | | case IPublishProgressView.PublishStatus.PUBLISHING: |
| 0 | 168 | | ShowProgressView(); |
| 0 | 169 | | break; |
| | 170 | | case IPublishProgressView.PublishStatus.ERROR: |
| 0 | 171 | | ShowPublishError(); |
| 0 | 172 | | break; |
| | 173 | | case IPublishProgressView.PublishStatus.SUCCESS: |
| 0 | 174 | | ShowProjectPublishSucces(); |
| | 175 | | break; |
| | 176 | | } |
| 0 | 177 | | } |
| | 178 | |
|
| | 179 | | public void SetPublishInfo(IBuilderScene publishedProject, PublishInfo info) |
| | 180 | | { |
| 0 | 181 | | screenshotImage.texture = publishedProject.sceneScreenshotTexture; |
| 0 | 182 | | currentScene = publishedProject; |
| 0 | 183 | | currentInfo = info; |
| 0 | 184 | | } |
| | 185 | |
|
| 0 | 186 | | public void ProjectPublished() { ChangeStatus(IPublishProgressView.PublishStatus.SUCCESS); } |
| | 187 | |
|
| | 188 | | internal string GetConfirmSubtitleText(string baseText) |
| | 189 | | { |
| 0 | 190 | | string title = currentScene.manifest.project.title; |
| 0 | 191 | | string posX = currentInfo.coordsToPublish.x.ToString(); |
| 0 | 192 | | string posY = currentInfo.coordsToPublish.y.ToString(); |
| | 193 | |
|
| 0 | 194 | | return string.Format(baseText, title, posX, posY); |
| | 195 | | } |
| | 196 | |
|
| | 197 | | public void ShowConfirmPopUp() |
| | 198 | | { |
| 0 | 199 | | confirmGameObject.SetActive(true); |
| 0 | 200 | | mainTitleTextView.text = CONFIRM_MAIN_TEXT; |
| 0 | 201 | | subTitleTextView.text = GetConfirmSubtitleText(CONFIRM_SUB_TITLE_TEXT); |
| 0 | 202 | | gameObject.SetActive(true); |
| 0 | 203 | | modal.Show(); |
| 0 | 204 | | } |
| | 205 | |
|
| 0 | 206 | | public void ConfirmDeployment() { ChangeStatus(IPublishProgressView.PublishStatus.CONFIRM); } |
| | 207 | |
|
| | 208 | | public void ShowProgressView() |
| | 209 | | { |
| 0 | 210 | | mainTitleTextView.text = PUBLISHING_MAIN_TEXT; |
| 0 | 211 | | subTitleTextView.text = GetConfirmSubtitleText(PUBLISHING_SUB_TITLE_TEXT); |
| | 212 | |
|
| 0 | 213 | | currentProgress = 0; |
| 0 | 214 | | modal.CanBeCancelled(false); |
| 0 | 215 | | progressGameObject.SetActive(true); |
| 0 | 216 | | if (fakeProgressCoroutine != null) |
| 0 | 217 | | StopCoroutine(fakeProgressCoroutine); |
| 0 | 218 | | fakeProgressCoroutine = StartCoroutine(FakePublishProgress()); |
| 0 | 219 | | AudioScriptableObjects.enable.Play(); |
| 0 | 220 | | } |
| | 221 | |
|
| | 222 | | public void ShowProjectPublishSucces() |
| | 223 | | { |
| 0 | 224 | | mainTitleTextView.text = SUCCESS_MAIN_TEXT; |
| 0 | 225 | | string text = GetConfirmSubtitleText(SUCCESS_SUB_TITLE_TEXT); |
| | 226 | |
|
| 0 | 227 | | foreach (var land in DataStore.i.builderInWorld.landsWithAccess.Get()) |
| | 228 | | { |
| 0 | 229 | | if (land.baseCoords == currentInfo.coordsToPublish && !string.IsNullOrEmpty(land.name)) |
| 0 | 230 | | text = text.Replace("[landName]", land.name + " "); |
| | 231 | | } |
| | 232 | |
|
| 0 | 233 | | text = text.Replace("[landName]", ""); |
| 0 | 234 | | subTitleTextView.text = text; |
| | 235 | |
|
| 0 | 236 | | successGameObject.SetActive(true); |
| 0 | 237 | | } |
| | 238 | |
|
| | 239 | | public void ShowPublishError() |
| | 240 | | { |
| 0 | 241 | | mainTitleTextView.text = ERROR_MAIN_TEXT; |
| 0 | 242 | | errorGameObject.SetActive(true); |
| 0 | 243 | | } |
| | 244 | |
|
| | 245 | | private void ChangeStatus(IPublishProgressView.PublishStatus status) |
| | 246 | | { |
| 0 | 247 | | currentStatus = status; |
| 0 | 248 | | ShowCurrentStatus(); |
| 0 | 249 | | } |
| | 250 | |
|
| | 251 | | public void Back() |
| | 252 | | { |
| 0 | 253 | | modal.Hide(); |
| 0 | 254 | | OnBackPressed?.Invoke(); |
| 0 | 255 | | } |
| | 256 | |
|
| | 257 | | public void Close() |
| | 258 | | { |
| 0 | 259 | | modal.Hide(); |
| 0 | 260 | | OnViewClosed?.Invoke(); |
| 0 | 261 | | } |
| | 262 | |
|
| | 263 | | [ContextMenu("Start Deployment")] |
| 0 | 264 | | public void ConfirmPublish() { OnPublishConfirmButtonPressed?.Invoke(); } |
| | 265 | |
|
| 0 | 266 | | public void PublishStarted() { ChangeStatus(IPublishProgressView.PublishStatus.PUBLISHING); } |
| | 267 | |
|
| | 268 | | public void Hide() |
| | 269 | | { |
| 0 | 270 | | modal.Hide(); |
| 0 | 271 | | if (fakeProgressCoroutine != null) |
| 0 | 272 | | StopCoroutine(fakeProgressCoroutine); |
| 0 | 273 | | } |
| | 274 | |
|
| | 275 | | public void PublishError(string message) |
| | 276 | | { |
| 0 | 277 | | errorTextView.text = message; |
| 0 | 278 | | ChangeStatus(IPublishProgressView.PublishStatus.ERROR); |
| 0 | 279 | | } |
| | 280 | |
|
| 0 | 281 | | public void SetPercentage(float newValue) { loadingBar.SetPercentage(newValue); } |
| | 282 | |
|
| | 283 | | private IEnumerator FakePublishProgress() |
| | 284 | | { |
| 0 | 285 | | while (true) |
| | 286 | | { |
| 0 | 287 | | float newPercentage = UnityEngine.Random.Range(1f, 15f); |
| 0 | 288 | | currentProgress += newPercentage; |
| | 289 | |
|
| 0 | 290 | | currentProgress = Mathf.Clamp( |
| | 291 | | currentProgress, |
| | 292 | | currentProgress - newPercentage, |
| | 293 | | 99f); |
| | 294 | |
|
| 0 | 295 | | SetPercentage(currentProgress); |
| | 296 | |
|
| 0 | 297 | | yield return new WaitForSeconds(UnityEngine.Random.Range(0.15f, 0.65f)); |
| | 298 | | } |
| | 299 | | } |
| | 300 | | } |
| | 301 | | } |