| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.Tasks; |
| | 3 | | using DCLServices.MapRendererV2.ConsumerUtils; |
| | 4 | | using DCLServices.MapRendererV2.MapCameraController; |
| | 5 | | using ExploreV2Analytics; |
| | 6 | | using System; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.EventSystems; |
| | 10 | |
|
| | 11 | | namespace DCL |
| | 12 | | { |
| | 13 | | public class NavMapLocationControlsController : IDisposable |
| | 14 | | { |
| | 15 | | public const float TRANSLATION_DURATION = 0.5f; |
| | 16 | |
|
| | 17 | | internal readonly BaseVariable<Vector2Int> homePoint; |
| | 18 | | internal readonly BaseVariable<Vector3> playerPlayerWorldPosition; |
| | 19 | |
|
| | 20 | | private readonly INavMapLocationControlsView view; |
| | 21 | | private readonly IExploreV2Analytics exploreV2Analytics; |
| | 22 | | private readonly INavmapZoomViewController navmapZoomViewController; |
| | 23 | | private readonly INavmapToastViewController toastViewController; |
| | 24 | |
|
| | 25 | | private IMapCameraController mapCamera; |
| | 26 | |
|
| | 27 | | private bool active; |
| | 28 | | private CancellationTokenSource cts; |
| | 29 | |
|
| 0 | 30 | | private MapRenderImage.ParcelClickData homeParcel => new() |
| | 31 | | { |
| | 32 | | Parcel = homePoint.Get(), |
| | 33 | | WorldPosition = new Vector2(Screen.width / 2f, Screen.height / 2f), |
| | 34 | | }; |
| | 35 | |
|
| 7 | 36 | | public NavMapLocationControlsController(INavMapLocationControlsView view, IExploreV2Analytics exploreV2Analytics |
| | 37 | | INavmapToastViewController toastViewController, BaseVariable<Vector2Int> homePoint, |
| | 38 | | BaseVariable<Vector3> playerPlayerWorldPosition) |
| | 39 | | { |
| 7 | 40 | | this.view = view; |
| 7 | 41 | | this.exploreV2Analytics = exploreV2Analytics; |
| 7 | 42 | | this.navmapZoomViewController = navmapZoomViewController; |
| 7 | 43 | | this.toastViewController = toastViewController; |
| 7 | 44 | | this.homePoint = homePoint; |
| 7 | 45 | | this.playerPlayerWorldPosition = playerPlayerWorldPosition; |
| 7 | 46 | | } |
| | 47 | |
|
| | 48 | | public void Dispose() |
| | 49 | | { |
| 6 | 50 | | cts?.SafeCancelAndDispose(); |
| 6 | 51 | | cts = null; |
| 6 | 52 | | } |
| | 53 | |
|
| | 54 | | public void Activate(IMapCameraController mapCameraController) |
| | 55 | | { |
| 4 | 56 | | if (active && mapCamera == mapCameraController) |
| 0 | 57 | | return; |
| | 58 | |
|
| 4 | 59 | | cts ??= new CancellationTokenSource(); |
| | 60 | |
|
| 4 | 61 | | mapCamera = mapCameraController; |
| | 62 | |
|
| 4 | 63 | | view.HomeButtonClicked += FocusOnHomeLocation; |
| 4 | 64 | | view.CenterToPlayerButtonClicked += CenterToPlayerLocation; |
| | 65 | |
|
| 4 | 66 | | active = true; |
| 4 | 67 | | } |
| | 68 | |
|
| | 69 | | public void Deactivate() |
| | 70 | | { |
| 3 | 71 | | if (!active) return; |
| | 72 | |
|
| 1 | 73 | | cts?.SafeRestart(); |
| | 74 | |
|
| 1 | 75 | | view.HomeButtonClicked -= FocusOnHomeLocation; |
| 1 | 76 | | view.CenterToPlayerButtonClicked -= CenterToPlayerLocation; |
| | 77 | |
|
| 1 | 78 | | active = false; |
| 1 | 79 | | } |
| | 80 | |
|
| | 81 | | public void Hide() |
| | 82 | | { |
| 1 | 83 | | Deactivate(); |
| 1 | 84 | | view.Hide(); |
| 1 | 85 | | } |
| | 86 | |
|
| | 87 | | private void FocusOnHomeLocation() |
| | 88 | | { |
| 2 | 89 | | EventSystem.current?.SetSelectedGameObject(null); |
| 2 | 90 | | toastViewController.CloseCurrentToast(); |
| | 91 | |
|
| 2 | 92 | | mapCamera.TranslateTo( |
| | 93 | | coordinates: homePoint.Get(), |
| | 94 | | duration: TRANSLATION_DURATION, |
| 0 | 95 | | onComplete: () => toastViewController.ShowPlaceToast(homeParcel, showUntilClick: true)); |
| | 96 | |
|
| 2 | 97 | | exploreV2Analytics.SendCenterMapToHome(); |
| 2 | 98 | | } |
| | 99 | |
|
| | 100 | | private void CenterToPlayerLocation() |
| | 101 | | { |
| 2 | 102 | | EventSystem.current?.SetSelectedGameObject(null); |
| 2 | 103 | | toastViewController.CloseCurrentToast(); |
| | 104 | |
|
| 2 | 105 | | mapCamera.TranslateTo( |
| | 106 | | coordinates: Utils.WorldToGridPosition(playerPlayerWorldPosition.Get()), |
| | 107 | | duration: TRANSLATION_DURATION); |
| | 108 | |
|
| 2 | 109 | | exploreV2Analytics.SendCenterMapToPlayer(); |
| 2 | 110 | | } |
| | 111 | | } |
| | 112 | | } |