| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Events; |
| | 8 | | using UnityEngine.UI; |
| | 9 | | using UnityEngine.UIElements; |
| | 10 | | using Button = UnityEngine.UI.Button; |
| | 11 | |
|
| | 12 | | namespace DCL.Builder |
| | 13 | | { |
| | 14 | | public interface IPublishProjectDetailView |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// If the publish is canceled this action will be called |
| | 18 | | /// </summary> |
| | 19 | | event Action OnCancel; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// If the publish button is pressed this action will be called |
| | 23 | | /// </summary> |
| | 24 | | event Action<PublishInfo> OnPublishButtonPressed; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// If the rotation of the project is changed, this event will be fired |
| | 28 | | /// </summary> |
| | 29 | | event Action<PublishInfo.ProjectRotation> OnProjectRotateChange; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Update the size of the project |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="parcels"></param> |
| | 35 | | void UpdateProjectSize(Vector2Int[] parcels); |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Set the project to publish |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="scene"></param> |
| | 41 | | void SetProjectToPublish(IBuilderScene scene); |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// This will reset the view to the first state |
| | 45 | | /// </summary> |
| | 46 | | void ResetView(); |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// This will show the detail modal |
| | 50 | | /// </summary> |
| | 51 | | void Show(); |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// This will hide the detail modal |
| | 55 | | /// </summary> |
| | 56 | | void Hide(); |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Dispose the view |
| | 60 | | /// </summary> |
| | 61 | | void Dispose(); |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public class PublishProjectDetailView : BaseComponentView, IPublishProjectDetailView |
| | 65 | | { |
| | 66 | | private const float SECONDS_TO_HIDE_TOP_TOAST = 4.5f; |
| | 67 | | public event Action<PublishInfo.ProjectRotation> OnProjectRotateChange; |
| | 68 | | public event Action OnCancel; |
| | 69 | | public event Action<PublishInfo> OnPublishButtonPressed; |
| | 70 | |
|
| | 71 | | [SerializeField] internal ModalComponentView modal; |
| | 72 | |
|
| | 73 | | [Header("First step")] |
| | 74 | | [SerializeField] internal GameObject firstStep; |
| | 75 | |
|
| | 76 | | [SerializeField] internal Button backButton; |
| | 77 | | [SerializeField] internal Button nextButton; |
| | 78 | | [SerializeField] internal Button rotateLeftButton; |
| | 79 | | [SerializeField] internal Button rotateRightButton; |
| | 80 | |
|
| | 81 | | [SerializeField] internal GameObject aerialScreenshotGameObject; |
| | 82 | | [SerializeField] internal RawImage sceneAerialScreenshotImage; |
| | 83 | |
|
| | 84 | | [SerializeField] internal LimitInputField nameInputField; |
| | 85 | | [SerializeField] internal LimitInputField descriptionInputField; |
| | 86 | |
|
| | 87 | | [Header("Second step")] |
| | 88 | | [SerializeField] internal GameObject secondStep; |
| | 89 | | [SerializeField] internal Button backSecondButton; |
| | 90 | | [SerializeField] internal Button publishButton; |
| | 91 | | [SerializeField] internal PublishLandListView landListView; |
| | 92 | | [SerializeField] internal ProjectPublishToast toastView; |
| | 93 | | [SerializeField] internal PublishMapView mapView; |
| | 94 | | [SerializeField] internal SearchLandView searchView; |
| | 95 | | [SerializeField] internal Button miniSearchView; |
| | 96 | | [SerializeField] internal TextMeshProUGUI topToastText; |
| | 97 | | [SerializeField] internal GameObject topToastGameObject; |
| | 98 | |
|
| | 99 | | [SerializeField] internal RawImage sceneScreenshotImage; |
| | 100 | |
|
| | 101 | | internal IBuilderScene scene; |
| | 102 | | private PublishInfo.ProjectRotation projectRotation = PublishInfo.ProjectRotation.NORTH; |
| | 103 | | internal int currentStep = 0; |
| 0 | 104 | | internal List<Vector2Int> availableLandsToPublish = new List<Vector2Int>(); |
| | 105 | |
|
| | 106 | | internal Vector2Int selectedCoords; |
| | 107 | | internal bool areCoordsSelected = false; |
| | 108 | | private Coroutine toastTopHideCoroutine; |
| | 109 | |
|
| | 110 | |
|
| | 111 | | public override void RefreshControl() |
| | 112 | | { |
| 0 | 113 | | if (scene == null) |
| 0 | 114 | | return; |
| | 115 | |
|
| 0 | 116 | | SetProjectToPublish(scene); |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | public override void Awake() |
| | 120 | | { |
| 0 | 121 | | base.Awake(); |
| 0 | 122 | | modal.OnCloseAction += CancelPublish; |
| | 123 | |
|
| 0 | 124 | | landListView.OnLandSelected += LandSelected; |
| 0 | 125 | | mapView.OnParcelHover += ParcelHovered; |
| 0 | 126 | | mapView.OnParcelClicked += ParcelClicked; |
| 0 | 127 | | searchView.OnValueSearch += SearchLand; |
| 0 | 128 | | searchView.OnSearchCanceled += DeactivateSearch; |
| | 129 | |
|
| 0 | 130 | | backButton.onClick.AddListener(Back); |
| 0 | 131 | | nextButton.onClick.AddListener(Next); |
| | 132 | |
|
| 0 | 133 | | backSecondButton.onClick.AddListener(Back); |
| 0 | 134 | | publishButton.onClick.AddListener(PublishButtonPressed); |
| | 135 | |
|
| 0 | 136 | | rotateLeftButton.onClick.AddListener( RotateLeft); |
| 0 | 137 | | rotateRightButton.onClick.AddListener( RotateRight); |
| 0 | 138 | | miniSearchView.onClick.AddListener(ShowSearchBar); |
| | 139 | |
|
| 0 | 140 | | nameInputField.OnEmptyValue += DisableNextButton; |
| 0 | 141 | | nameInputField.OnLimitReached += DisableNextButton; |
| 0 | 142 | | nameInputField.OnInputAvailable += EnableNextButton; |
| | 143 | |
|
| 0 | 144 | | descriptionInputField.OnLimitReached += DisableNextButton; |
| 0 | 145 | | descriptionInputField.OnInputAvailable += EnableNextButton; |
| | 146 | |
|
| 0 | 147 | | gameObject.SetActive(false); |
| 0 | 148 | | searchView.gameObject.SetActive(false); |
| 0 | 149 | | } |
| | 150 | |
|
| | 151 | | public override void Dispose() |
| | 152 | | { |
| 0 | 153 | | base.Dispose(); |
| | 154 | |
|
| 0 | 155 | | modal.OnCloseAction -= CancelPublish; |
| | 156 | |
|
| 0 | 157 | | mapView.OnParcelHover -= ParcelHovered; |
| 0 | 158 | | mapView.OnParcelClicked -= ParcelClicked; |
| 0 | 159 | | landListView.OnLandSelected -= LandSelected; |
| 0 | 160 | | searchView.OnValueSearch -= SearchLand; |
| 0 | 161 | | searchView.OnSearchCanceled -= DeactivateSearch; |
| | 162 | |
|
| 0 | 163 | | backButton.onClick.RemoveAllListeners(); |
| 0 | 164 | | nextButton.onClick.RemoveAllListeners(); |
| | 165 | |
|
| 0 | 166 | | backSecondButton.onClick.RemoveAllListeners(); |
| 0 | 167 | | publishButton.onClick.RemoveAllListeners(); |
| | 168 | |
|
| 0 | 169 | | rotateLeftButton.onClick.RemoveAllListeners(); |
| 0 | 170 | | rotateRightButton.onClick.RemoveAllListeners(); |
| 0 | 171 | | miniSearchView.onClick.RemoveAllListeners(); |
| | 172 | |
|
| 0 | 173 | | nameInputField.OnLimitReached -= DisableNextButton; |
| 0 | 174 | | nameInputField.OnInputAvailable -= EnableNextButton; |
| 0 | 175 | | nameInputField.OnEmptyValue -= DisableNextButton; |
| | 176 | |
|
| 0 | 177 | | descriptionInputField.OnLimitReached -= DisableNextButton; |
| 0 | 178 | | descriptionInputField.OnInputAvailable -= EnableNextButton; |
| 0 | 179 | | } |
| | 180 | |
|
| | 181 | | private void DeactivateSearch() |
| | 182 | | { |
| 0 | 183 | | landListView.SetActive(false); |
| 0 | 184 | | } |
| | 185 | |
|
| | 186 | | private void Back() |
| | 187 | | { |
| 0 | 188 | | if (currentStep <= 0) |
| | 189 | | { |
| 0 | 190 | | Hide(); |
| 0 | 191 | | return; |
| | 192 | | } |
| | 193 | |
|
| 0 | 194 | | currentStep--; |
| 0 | 195 | | ShowCurrentStep(); |
| 0 | 196 | | } |
| | 197 | |
|
| | 198 | | private void Next() |
| | 199 | | { |
| 0 | 200 | | currentStep++; |
| 0 | 201 | | ShowCurrentStep(); |
| 0 | 202 | | } |
| | 203 | |
|
| | 204 | | private void SearchLand(string searchInput) |
| | 205 | | { |
| 0 | 206 | | List<LandWithAccess> filteredLands = new List<LandWithAccess>(); |
| 0 | 207 | | foreach (LandWithAccess land in DataStore.i.builderInWorld.landsWithAccess.Get()) |
| | 208 | | { |
| 0 | 209 | | bool shouldBeAdded = land.name.ToLower().Contains(searchInput.ToLower()); |
| | 210 | |
|
| 0 | 211 | | foreach (Vector2Int landParcel in land.parcels) |
| | 212 | | { |
| 0 | 213 | | if(BIWUtils.Vector2INTToString(landParcel).Contains(searchInput)) |
| 0 | 214 | | shouldBeAdded = true; |
| | 215 | | } |
| | 216 | |
|
| 0 | 217 | | if(shouldBeAdded && !filteredLands.Contains(land)) |
| 0 | 218 | | filteredLands.Add(land); |
| | 219 | | } |
| | 220 | |
|
| | 221 | | // We fill the list with the lands |
| 0 | 222 | | landListView.SetContent(scene.manifest.project.cols, scene.manifest.project.rows, filteredLands); |
| 0 | 223 | | landListView.SetActive(true); |
| 0 | 224 | | } |
| | 225 | |
|
| | 226 | | private void ShowTopToast(string text) |
| | 227 | | { |
| 0 | 228 | | topToastText.text = text; |
| 0 | 229 | | topToastGameObject.SetActive(true); |
| 0 | 230 | | HideSearchBar(); |
| | 231 | |
|
| 0 | 232 | | toastTopHideCoroutine = StartCoroutine(WaitAndHideTopToast()); |
| 0 | 233 | | } |
| | 234 | |
|
| | 235 | | private void ShowSearchBar() |
| | 236 | | { |
| 0 | 237 | | searchView.gameObject.SetActive(true); |
| 0 | 238 | | miniSearchView.gameObject.SetActive(false); |
| 0 | 239 | | HideTopToast(); |
| 0 | 240 | | } |
| | 241 | |
|
| | 242 | | private void HideSearchBar() |
| | 243 | | { |
| 0 | 244 | | searchView.gameObject.SetActive(false); |
| 0 | 245 | | miniSearchView.gameObject.SetActive(true); |
| 0 | 246 | | landListView.HideEmptyContent(); |
| 0 | 247 | | landListView.SetActive(false); |
| 0 | 248 | | } |
| | 249 | |
|
| | 250 | | private void HideTopToast() |
| | 251 | | { |
| 0 | 252 | | if (toastTopHideCoroutine != null) |
| 0 | 253 | | StopCoroutine(toastTopHideCoroutine); |
| 0 | 254 | | topToastGameObject.SetActive(false); |
| 0 | 255 | | } |
| | 256 | |
|
| | 257 | | private void ParcelClicked(Vector2Int parcel) |
| | 258 | | { |
| 0 | 259 | | if (!availableLandsToPublish.Contains(parcel)) |
| | 260 | | { |
| 0 | 261 | | ShowTopToast("Projects can't be placed in a Land you don't own."); |
| 0 | 262 | | return; |
| | 263 | | } |
| | 264 | |
|
| 0 | 265 | | LandSelected(parcel); |
| 0 | 266 | | } |
| | 267 | |
|
| | 268 | | public void LandSelected(Vector2Int parcel) |
| | 269 | | { |
| 0 | 270 | | foreach (LandWithAccess land in DataStore.i.builderInWorld.landsWithAccess.Get()) |
| | 271 | | { |
| 0 | 272 | | foreach (Vector2Int landParcel in land.parcels) |
| | 273 | | { |
| 0 | 274 | | if (landParcel == parcel) |
| | 275 | | { |
| 0 | 276 | | toastView.SetLandInfo(land.name, BIWUtils.Vector2INTToString(landParcel)); |
| 0 | 277 | | bool isEmpty = !(land.scenes.Count == 0 || land.scenes[0].isEmpty); |
| 0 | 278 | | toastView.SetSubtitleActive(isEmpty); |
| 0 | 279 | | toastView.Show(); |
| 0 | 280 | | break; |
| | 281 | | } |
| | 282 | | } |
| | 283 | |
|
| | 284 | | } |
| | 285 | |
|
| 0 | 286 | | mapView.SelectLandInMap(parcel); |
| 0 | 287 | | areCoordsSelected = true; |
| 0 | 288 | | selectedCoords = parcel; |
| 0 | 289 | | publishButton.interactable = true; |
| 0 | 290 | | } |
| | 291 | |
|
| | 292 | | private void ParcelHovered(Vector2Int parcel) |
| | 293 | | { |
| 0 | 294 | | bool isAvailable = availableLandsToPublish.Contains(parcel); |
| 0 | 295 | | mapView.SetAvailabilityToPublish(isAvailable); |
| 0 | 296 | | } |
| | 297 | |
|
| | 298 | | private void ShowCurrentStep() |
| | 299 | | { |
| 0 | 300 | | firstStep.SetActive(false); |
| 0 | 301 | | secondStep.SetActive(false); |
| 0 | 302 | | switch (currentStep) |
| | 303 | | { |
| | 304 | | case 0: // Choose name, desc and rotation |
| 0 | 305 | | firstStep.SetActive(true); |
| 0 | 306 | | break; |
| | 307 | | case 1: // Choose land to deploy |
| 0 | 308 | | searchView.ClearSearch(); |
| 0 | 309 | | HideSearchBar(); |
| 0 | 310 | | secondStep.SetActive(true); |
| 0 | 311 | | if (availableLandsToPublish.Count > 0) |
| 0 | 312 | | GoToCoords(availableLandsToPublish[0]); |
| | 313 | |
|
| 0 | 314 | | if (areCoordsSelected) |
| | 315 | | { |
| 0 | 316 | | LandSelected(selectedCoords); |
| 0 | 317 | | GoToCoords(selectedCoords); |
| 0 | 318 | | toastView.Show(true); |
| | 319 | | } |
| | 320 | | break; |
| | 321 | | } |
| 0 | 322 | | } |
| | 323 | |
|
| | 324 | | private void RotateLeft() |
| | 325 | | { |
| 0 | 326 | | projectRotation--; |
| 0 | 327 | | if (projectRotation < 0) |
| 0 | 328 | | projectRotation = PublishInfo.ProjectRotation.WEST; |
| 0 | 329 | | SetRotation(projectRotation); |
| 0 | 330 | | } |
| | 331 | |
|
| | 332 | | private void RotateRight() |
| | 333 | | { |
| 0 | 334 | | projectRotation++; |
| 0 | 335 | | if (projectRotation > PublishInfo.ProjectRotation.WEST) |
| 0 | 336 | | projectRotation = PublishInfo.ProjectRotation.NORTH; |
| 0 | 337 | | SetRotation(projectRotation); |
| 0 | 338 | | } |
| | 339 | |
|
| | 340 | | private void SetRotation(PublishInfo.ProjectRotation rotation) |
| | 341 | | { |
| 0 | 342 | | float zRotation = 0; |
| | 343 | | switch (rotation) |
| | 344 | | { |
| | 345 | | case PublishInfo.ProjectRotation.NORTH: |
| 0 | 346 | | zRotation = 0; |
| 0 | 347 | | break; |
| | 348 | | case PublishInfo.ProjectRotation.EAST: |
| 0 | 349 | | zRotation = 270; |
| 0 | 350 | | break; |
| | 351 | | case PublishInfo.ProjectRotation.SOUTH: |
| 0 | 352 | | zRotation = 180; |
| 0 | 353 | | break; |
| | 354 | | case PublishInfo.ProjectRotation.WEST: |
| 0 | 355 | | zRotation = 90; |
| | 356 | | break; |
| | 357 | | } |
| 0 | 358 | | sceneAerialScreenshotImage.rectTransform.rotation = Quaternion.Euler(0, 0, zRotation); |
| 0 | 359 | | OnProjectRotateChange?.Invoke(rotation); |
| 0 | 360 | | } |
| | 361 | |
|
| | 362 | | public void SetProjectToPublish(IBuilderScene scene) |
| | 363 | | { |
| 0 | 364 | | this.scene = scene; |
| | 365 | |
|
| | 366 | | // Reset common views |
| 0 | 367 | | toastView.Hide(true); |
| 0 | 368 | | HideSearchBar(); |
| | 369 | |
|
| | 370 | | // We reset the selected coords and disable the publish button until the coords are selected |
| 0 | 371 | | areCoordsSelected = false; |
| 0 | 372 | | publishButton.interactable = false; |
| | 373 | |
|
| | 374 | | // We set the screenshot |
| 0 | 375 | | if (scene.aerialScreenshotTexture != null && BIWUtils.IsParcelSceneSquare(scene.scene.sceneData.parcels)) |
| | 376 | | { |
| 0 | 377 | | aerialScreenshotGameObject.SetActive(true); |
| 0 | 378 | | sceneAerialScreenshotImage.texture = scene.aerialScreenshotTexture; |
| 0 | 379 | | } |
| | 380 | | else |
| | 381 | | { |
| 0 | 382 | | aerialScreenshotGameObject.SetActive(false); |
| | 383 | | } |
| | 384 | |
|
| 0 | 385 | | if (sceneScreenshotImage != null) |
| 0 | 386 | | sceneScreenshotImage.texture = scene.sceneScreenshotTexture; |
| | 387 | |
|
| | 388 | | // We set the scene info |
| 0 | 389 | | nameInputField.SetText(scene.manifest.project.title); |
| 0 | 390 | | descriptionInputField.SetText(scene.manifest.project.description); |
| | 391 | |
|
| | 392 | | // We filter the available lands |
| 0 | 393 | | CheckAvailableLandsToPublish(scene); |
| | 394 | |
|
| | 395 | | // We set the size of the project in the builder |
| 0 | 396 | | UpdateProjectSize(scene.scene.sceneData.parcels); |
| 0 | 397 | | } |
| | 398 | |
|
| | 399 | | public void UpdateProjectSize(Vector2Int[] parcels) |
| | 400 | | { |
| | 401 | | // We set the size of the project in the builder |
| 0 | 402 | | mapView.SetProjectSize(parcels); |
| 0 | 403 | | } |
| | 404 | |
|
| | 405 | | private void CheckAvailableLandsToPublish(IBuilderScene sceneToPublish) |
| | 406 | | { |
| 0 | 407 | | availableLandsToPublish.Clear(); |
| 0 | 408 | | availableLandsToPublish = BIWUtils.GetLandsToPublishProject(DataStore.i.builderInWorld.landsWithAccess.Get() |
| 0 | 409 | | } |
| | 410 | |
|
| | 411 | | private void LandSelected(LandWithAccess land) |
| | 412 | | { |
| 0 | 413 | | landListView.SetActive(false); |
| 0 | 414 | | searchView.ClearSearch(); |
| 0 | 415 | | HideSearchBar(); |
| | 416 | |
|
| | 417 | | // We select the land |
| 0 | 418 | | LandSelected(land.baseCoords); |
| | 419 | |
|
| | 420 | | // We set the map to the main land |
| 0 | 421 | | GoToCoords(land.baseCoords); |
| 0 | 422 | | } |
| | 423 | |
|
| | 424 | | private void GoToCoords(Vector2Int coord) |
| | 425 | | { |
| | 426 | | // We set the map to the main land |
| 0 | 427 | | CoroutineStarter.Start(WaitFrameToPositionMap(coord)); |
| 0 | 428 | | } |
| | 429 | |
|
| | 430 | | public void Show() |
| | 431 | | { |
| 0 | 432 | | gameObject.SetActive(true); |
| 0 | 433 | | mapView.SetVisible(true); |
| 0 | 434 | | modal.Show(); |
| 0 | 435 | | if (areCoordsSelected) |
| | 436 | | { |
| 0 | 437 | | LandSelected(selectedCoords); |
| 0 | 438 | | GoToCoords(selectedCoords); |
| 0 | 439 | | toastView.Show(true); |
| | 440 | | } |
| 0 | 441 | | } |
| | 442 | |
|
| | 443 | | public void ResetView() |
| | 444 | | { |
| 0 | 445 | | currentStep = 0; |
| 0 | 446 | | ShowCurrentStep(); |
| 0 | 447 | | } |
| | 448 | |
|
| | 449 | | public void Hide() |
| | 450 | | { |
| 0 | 451 | | if(!modal.isVisible) |
| 0 | 452 | | return; |
| | 453 | |
|
| 0 | 454 | | modal.Hide(); |
| 0 | 455 | | mapView.SetVisible(false); |
| 0 | 456 | | CancelPublish(); |
| 0 | 457 | | } |
| | 458 | |
|
| | 459 | | internal void EnableNextButton() |
| | 460 | | { |
| 0 | 461 | | if(nameInputField.IsInputAvailable() && descriptionInputField.IsInputAvailable()) |
| 0 | 462 | | nextButton.interactable = true; |
| 0 | 463 | | } |
| | 464 | |
|
| 0 | 465 | | internal void DisableNextButton() { nextButton.interactable = false; } |
| | 466 | |
|
| | 467 | | private void PublishButtonPressed() |
| | 468 | | { |
| 0 | 469 | | Hide(); |
| 0 | 470 | | PublishInfo publishInfo = new PublishInfo(); |
| 0 | 471 | | scene.manifest.project.title = nameInputField.GetValue(); |
| 0 | 472 | | scene.manifest.project.description = descriptionInputField.GetValue(); |
| 0 | 473 | | publishInfo.coordsToPublish = selectedCoords; |
| | 474 | |
|
| 0 | 475 | | OnPublishButtonPressed?.Invoke(publishInfo); |
| 0 | 476 | | } |
| | 477 | |
|
| 0 | 478 | | private void CancelPublish() { OnCancel?.Invoke(); } |
| | 479 | |
|
| | 480 | | private void CancelButtonPressed() |
| | 481 | | { |
| 0 | 482 | | Hide(); |
| 0 | 483 | | CancelPublish(); |
| 0 | 484 | | } |
| | 485 | |
|
| | 486 | | IEnumerator WaitFrameToPositionMap(Vector2Int coords) |
| | 487 | | { |
| 0 | 488 | | yield return null; |
| 0 | 489 | | mapView.GoToCoords(coords); |
| 0 | 490 | | } |
| | 491 | |
|
| | 492 | | IEnumerator WaitAndHideTopToast() |
| | 493 | | { |
| 0 | 494 | | yield return new WaitForSeconds(SECONDS_TO_HIDE_TOP_TOAST); |
| 0 | 495 | | HideTopToast(); |
| 0 | 496 | | } |
| | 497 | | } |
| | 498 | | } |