| | 1 | | using System; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | | using static DCL.MapGlobalUsersPositionMarkerController; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class NavmapVisibilityBehaviour : IDisposable |
| | 10 | | { |
| | 11 | | private Vector3 atlasOriginalPosition; |
| | 12 | |
|
| | 13 | | private Transform mapRendererMinimapParent; |
| | 14 | | private RectTransform minimapViewport; |
| | 15 | |
|
| | 16 | | private bool waitingForFullscreenHUDOpen; |
| | 17 | |
|
| | 18 | | private readonly BaseVariable<bool> navmapVisible; |
| | 19 | |
|
| | 20 | | private readonly ScrollRect scrollRect; |
| | 21 | | private readonly Transform scrollRectContentTransform; |
| | 22 | |
|
| | 23 | | private readonly NavmapZoom zoom; |
| | 24 | | private readonly NavmapToastView toastView; |
| | 25 | |
|
| 54 | 26 | | public NavmapVisibilityBehaviour(BaseVariable<bool> navmapVisible, ScrollRect scrollRect, Transform scrollRectCo |
| | 27 | | { |
| 54 | 28 | | this.navmapVisible = navmapVisible; |
| | 29 | |
|
| 54 | 30 | | this.scrollRect = scrollRect; |
| 54 | 31 | | this.scrollRectContentTransform = scrollRectContentTransform; |
| | 32 | |
|
| 54 | 33 | | this.zoom = zoom; |
| 54 | 34 | | this.toastView = toastView; |
| | 35 | |
|
| 54 | 36 | | DataStore.i.exploreV2.isOpen.OnChange += OnExploreOpenChanged; |
| 54 | 37 | | navmapVisible.OnChange += OnNavmapVisibilityChanged; |
| 54 | 38 | | } |
| | 39 | |
|
| | 40 | | public void Dispose() |
| | 41 | | { |
| 54 | 42 | | DataStore.i.exploreV2.isOpen.OnChange -= OnExploreOpenChanged; |
| 54 | 43 | | navmapVisible.OnChange -= OnNavmapVisibilityChanged; |
| | 44 | |
|
| 54 | 45 | | if (waitingForFullscreenHUDOpen == false) |
| 53 | 46 | | CommonScriptableObjects.isFullscreenHUDOpen.OnChange -= OnFullScreenOpened; |
| 54 | 47 | | } |
| | 48 | |
|
| | 49 | | private void OnNavmapVisibilityChanged(bool isVisible, bool _) => |
| 0 | 50 | | SetVisible(isVisible); |
| | 51 | |
|
| | 52 | | private void OnExploreOpenChanged(bool isOpen, bool _) |
| | 53 | | { |
| 0 | 54 | | if (!isOpen) |
| 0 | 55 | | SetVisible(false); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | internal void SetVisible(bool visible) |
| | 59 | | { |
| 1 | 60 | | if (waitingForFullscreenHUDOpen) |
| 0 | 61 | | return; |
| | 62 | |
|
| 1 | 63 | | if (visible) |
| | 64 | | { |
| 1 | 65 | | if (CommonScriptableObjects.isFullscreenHUDOpen.Get()) |
| | 66 | | { |
| 0 | 67 | | SetVisibility_Internal(true); |
| 0 | 68 | | } |
| | 69 | | else |
| | 70 | | { |
| 1 | 71 | | waitingForFullscreenHUDOpen = true; |
| 1 | 72 | | CommonScriptableObjects.isFullscreenHUDOpen.OnChange += OnFullScreenOpened; |
| | 73 | | } |
| 1 | 74 | | } |
| | 75 | | else |
| | 76 | | { |
| 0 | 77 | | SetVisibility_Internal(false); |
| | 78 | | } |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | private void OnFullScreenOpened(bool isFullScreen, bool _) |
| | 82 | | { |
| 0 | 83 | | CommonScriptableObjects.isFullscreenHUDOpen.OnChange -= OnFullScreenOpened; |
| | 84 | |
|
| 0 | 85 | | if (!isFullScreen) |
| 0 | 86 | | return; |
| | 87 | |
|
| 0 | 88 | | SetVisibility_Internal(true); |
| 0 | 89 | | waitingForFullscreenHUDOpen = false; |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | private void SetVisibility_Internal(bool visible) |
| | 93 | | { |
| 0 | 94 | | if (MapRenderer.i == null) |
| 0 | 95 | | return; |
| | 96 | |
|
| 0 | 97 | | scrollRect.StopMovement(); |
| | 98 | |
|
| 0 | 99 | | scrollRect.gameObject.SetActive(visible); |
| 0 | 100 | | MapRenderer.i.parcelHighlightEnabled = visible; |
| | 101 | |
|
| 0 | 102 | | if (visible) |
| | 103 | | { |
| 0 | 104 | | if (!DataStore.i.exploreV2.isInitialized.Get()) |
| 0 | 105 | | Utils.UnlockCursor(); |
| | 106 | |
|
| 0 | 107 | | MapRenderer.i.scaleFactor = zoom.Scale; |
| | 108 | |
|
| 0 | 109 | | if (minimapViewport == null || mapRendererMinimapParent == null) |
| | 110 | | { |
| 0 | 111 | | minimapViewport = MapRenderer.i.atlas.viewport; |
| 0 | 112 | | mapRendererMinimapParent = MapRenderer.i.transform.parent; |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | atlasOriginalPosition = MapRenderer.i.atlas.chunksParent.transform.localPosition; |
| | 116 | |
|
| 0 | 117 | | MapRenderer.i.atlas.viewport = scrollRect.viewport; |
| 0 | 118 | | MapRenderer.i.transform.SetParent(scrollRectContentTransform); |
| 0 | 119 | | MapRenderer.i.atlas.UpdateCulling(); |
| | 120 | |
|
| 0 | 121 | | scrollRect.content = MapRenderer.i.atlas.chunksParent.transform as RectTransform; |
| | 122 | |
|
| | 123 | | // Center map |
| 0 | 124 | | MapRenderer.i.atlas.CenterToTile(Utils.WorldToGridPositionUnclamped(DataStore.i.player.playerWorldPositi |
| | 125 | |
|
| | 126 | | // Set shorter interval of time for populated scenes markers fetch |
| 0 | 127 | | MapRenderer.i.usersPositionMarkerController?.SetUpdateMode(UpdateMode.FOREGROUND); |
| 0 | 128 | | } |
| 0 | 129 | | else if (minimapViewport != null) |
| | 130 | | { |
| 0 | 131 | | zoom.ResetToDefault(); |
| 0 | 132 | | toastView.Close(); |
| | 133 | |
|
| 0 | 134 | | MapRenderer.i.atlas.viewport = minimapViewport; |
| 0 | 135 | | MapRenderer.i.transform.SetParent(mapRendererMinimapParent); |
| 0 | 136 | | MapRenderer.i.atlas.chunksParent.transform.localPosition = atlasOriginalPosition; |
| 0 | 137 | | MapRenderer.i.atlas.UpdateCulling(); |
| | 138 | |
|
| 0 | 139 | | MapRenderer.i.UpdateRendering(Utils.WorldToGridPositionUnclamped(DataStore.i.player.playerWorldPosition. |
| | 140 | |
|
| | 141 | | // Set longer interval of time for populated scenes markers fetch |
| 0 | 142 | | MapRenderer.i.usersPositionMarkerController?.SetUpdateMode(UpdateMode.BACKGROUND); |
| | 143 | | } |
| 0 | 144 | | } |
| | 145 | | } |
| | 146 | | } |