| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Interface; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.EventSystems; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | namespace DCL |
| | 11 | | { |
| | 12 | | public class NavmapView : MonoBehaviour |
| | 13 | | { |
| | 14 | | private const float MOUSE_WHEEL_THRESHOLD = 0.04f; |
| | 15 | | private const float MAP_ZOOM_LEVELS = 4; |
| | 16 | |
|
| | 17 | | [Header("References")] |
| | 18 | | [SerializeField] internal Button closeButton; |
| | 19 | | [SerializeField] internal ScrollRect scrollRect; |
| | 20 | | [SerializeField] internal Transform scrollRectContentTransform; |
| | 21 | | [SerializeField] internal TextMeshProUGUI currentSceneNameText; |
| | 22 | | [SerializeField] internal TextMeshProUGUI currentSceneCoordsText; |
| | 23 | | [SerializeField] internal NavmapToastView toastView; |
| | 24 | | [SerializeField] internal InputAction_Measurable mouseWheelAction; |
| | 25 | |
|
| | 26 | | [Header("Zoom")] |
| | 27 | | [SerializeField] internal InputAction_Hold zoomIn; |
| | 28 | | [SerializeField] internal InputAction_Hold zoomOut; |
| | 29 | | [SerializeField] internal Button zoomInButton; |
| | 30 | | [SerializeField] internal Button zoomOutButton; |
| | 31 | | [SerializeField] internal Image zoomInPlus; |
| | 32 | | [SerializeField] internal Image zoomOutMinus; |
| | 33 | | [SerializeField] internal AnimationCurve zoomCurve; |
| | 34 | |
|
| | 35 | | private Vector3 atlasOriginalPosition; |
| | 36 | | private RectTransform containerRectTransform; |
| | 37 | | private int currentZoomLevel; |
| 56 | 38 | | private Color disabledColor = new Color(0f, 0f, 0f, 0.5f); |
| | 39 | | private bool isScaling = false; |
| | 40 | | private MinimapMetadata mapMetadata; |
| | 41 | | private Transform mapRendererMinimapParent; |
| | 42 | | private RectTransform minimapViewport; |
| | 43 | |
|
| 56 | 44 | | private Color normalColor = new Color(0f, 0f, 0f, 1f); |
| | 45 | | Vector3 previousScaleSize; |
| 56 | 46 | | private float scale = 1f; |
| 56 | 47 | | private float scaleDuration = 0.2f; |
| | 48 | |
|
| | 49 | | private InputAction_Trigger.Triggered selectParcelDelegate; |
| | 50 | | private bool waitingForFullscreenHUDOpen = false; |
| | 51 | |
|
| 108 | 52 | | public BaseVariable<bool> navmapVisible => DataStore.i.HUDs.navmapVisible; |
| | 53 | |
|
| 162 | 54 | | private BaseVariable<Transform> configureMapInFullscreenMenu => DataStore.i.exploreV2.configureMapInFullscreenMe |
| | 55 | |
|
| | 56 | | void Start() |
| | 57 | | { |
| 54 | 58 | | mapMetadata = MinimapMetadata.GetMetadata(); |
| 54 | 59 | | containerRectTransform = scrollRectContentTransform.GetComponent<RectTransform>(); |
| | 60 | |
|
| 54 | 61 | | closeButton.onClick.AddListener(() => |
| | 62 | | { |
| 0 | 63 | | navmapVisible.Set(false); |
| 0 | 64 | | }); |
| 54 | 65 | | scrollRect.onValueChanged.AddListener((x) => |
| | 66 | | { |
| 0 | 67 | | if (!navmapVisible.Get()) |
| 0 | 68 | | return; |
| | 69 | |
|
| 0 | 70 | | MapRenderer.i.atlas.UpdateCulling(); |
| 0 | 71 | | CloseToast(); |
| 0 | 72 | | }); |
| | 73 | |
|
| 54 | 74 | | toastView.OnGotoClicked += () => navmapVisible.Set(false); |
| | 75 | |
|
| 54 | 76 | | MapRenderer.OnParcelClicked += TriggerToast; |
| 54 | 77 | | MapRenderer.OnCursorFarFromParcel += CloseToast; |
| 54 | 78 | | CommonScriptableObjects.playerCoords.OnChange += UpdateCurrentSceneData; |
| 54 | 79 | | DataStore.i.exploreV2.isOpen.OnChange += OnExploreChange; |
| 54 | 80 | | navmapVisible.OnChange += OnNavmapVisibleChanged; |
| | 81 | |
|
| 54 | 82 | | configureMapInFullscreenMenu.OnChange += ConfigureMapInFullscreenMenuChanged; |
| 54 | 83 | | ConfigureMapInFullscreenMenuChanged(configureMapInFullscreenMenu.Get(), null); |
| 54 | 84 | | mouseWheelAction.OnValueChanged += OnMouseWheelChangeValue; |
| 54 | 85 | | zoomIn.OnStarted += OnZoomPlusMinus; |
| 54 | 86 | | zoomOut.OnStarted += OnZoomPlusMinus; |
| 54 | 87 | | zoomInButton.onClick.AddListener(() => |
| | 88 | | { |
| 0 | 89 | | OnZoomPlusMinus(DCLAction_Hold.ZoomIn); |
| 0 | 90 | | }); |
| 54 | 91 | | zoomOutButton.onClick.AddListener(() => |
| | 92 | | { |
| 0 | 93 | | OnZoomPlusMinus(DCLAction_Hold.ZoomOut); |
| 0 | 94 | | }); |
| 54 | 95 | | ResetCameraZoom(); |
| 54 | 96 | | Initialize(); |
| 54 | 97 | | } |
| | 98 | |
|
| | 99 | | private void OnDestroy() |
| | 100 | | { |
| 54 | 101 | | MapRenderer.OnParcelClicked -= TriggerToast; |
| 54 | 102 | | MapRenderer.OnCursorFarFromParcel -= CloseToast; |
| 54 | 103 | | CommonScriptableObjects.playerCoords.OnChange -= UpdateCurrentSceneData; |
| 54 | 104 | | navmapVisible.OnChange -= OnNavmapVisibleChanged; |
| 54 | 105 | | configureMapInFullscreenMenu.OnChange -= ConfigureMapInFullscreenMenuChanged; |
| 54 | 106 | | mouseWheelAction.OnValueChanged -= OnMouseWheelChangeValue; |
| 54 | 107 | | zoomIn.OnStarted -= OnZoomPlusMinus; |
| 54 | 108 | | zoomOut.OnStarted -= OnZoomPlusMinus; |
| 54 | 109 | | CommonScriptableObjects.isFullscreenHUDOpen.OnChange -= IsFullscreenHUDOpen_OnChange; |
| 54 | 110 | | DataStore.i.exploreV2.isOpen.OnChange -= OnExploreChange; |
| 54 | 111 | | } |
| | 112 | |
|
| | 113 | | public event Action<bool> OnToggle; |
| | 114 | |
|
| | 115 | | public void Initialize() |
| | 116 | | { |
| 54 | 117 | | toastView.gameObject.SetActive(false); |
| 54 | 118 | | scrollRect.gameObject.SetActive(false); |
| 54 | 119 | | DataStore.i.HUDs.isNavMapInitialized.Set(true); |
| 54 | 120 | | } |
| | 121 | |
|
| | 122 | | public void SetExitButtonActive(bool isActive) => |
| 0 | 123 | | closeButton.gameObject.SetActive(isActive); |
| | 124 | |
|
| | 125 | | public void SetAsFullScreenMenuMode(Transform parentTransform) |
| | 126 | | { |
| 54 | 127 | | if (parentTransform == null) |
| 54 | 128 | | return; |
| | 129 | |
|
| 0 | 130 | | transform.SetParent(parentTransform); |
| 0 | 131 | | transform.localScale = Vector3.one; |
| 0 | 132 | | SetExitButtonActive(false); |
| | 133 | |
|
| 0 | 134 | | RectTransform rectTransform = transform as RectTransform; |
| 0 | 135 | | rectTransform.anchorMin = Vector2.zero; |
| 0 | 136 | | rectTransform.anchorMax = Vector2.one; |
| 0 | 137 | | rectTransform.pivot = new Vector2(0.5f, 0.5f); |
| 0 | 138 | | rectTransform.localPosition = Vector2.zero; |
| 0 | 139 | | rectTransform.offsetMax = Vector2.zero; |
| 0 | 140 | | rectTransform.offsetMin = Vector2.zero; |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | private void ResetCameraZoom() |
| | 144 | | { |
| 54 | 145 | | currentZoomLevel = Mathf.FloorToInt(MAP_ZOOM_LEVELS / 2); |
| 54 | 146 | | scale = zoomCurve.Evaluate(currentZoomLevel); |
| 54 | 147 | | containerRectTransform.localScale = new Vector3(scale, scale, scale); |
| 54 | 148 | | HandleZoomButtonsAspect(); |
| 54 | 149 | | } |
| | 150 | |
|
| | 151 | | private void OnZoomPlusMinus(DCLAction_Hold action) |
| | 152 | | { |
| 0 | 153 | | if (!navmapVisible.Get()) |
| 0 | 154 | | return; |
| | 155 | |
|
| 0 | 156 | | if (action.Equals(DCLAction_Hold.ZoomIn)) |
| | 157 | | { |
| 0 | 158 | | CalculateZoomLevelAndDirection(1); |
| 0 | 159 | | } |
| 0 | 160 | | else if (action.Equals(DCLAction_Hold.ZoomOut)) |
| | 161 | | { |
| 0 | 162 | | CalculateZoomLevelAndDirection(-1); |
| | 163 | | } |
| 0 | 164 | | EventSystem.current.SetSelectedGameObject(null); |
| 0 | 165 | | } |
| | 166 | |
|
| | 167 | | private void OnMouseWheelChangeValue(DCLAction_Measurable action, float value) |
| | 168 | | { |
| 0 | 169 | | if (value > -MOUSE_WHEEL_THRESHOLD && value < MOUSE_WHEEL_THRESHOLD) |
| 0 | 170 | | return; |
| 0 | 171 | | CalculateZoomLevelAndDirection(value); |
| 0 | 172 | | } |
| | 173 | |
|
| | 174 | | private void CalculateZoomLevelAndDirection(float value) |
| | 175 | | { |
| 0 | 176 | | if (!navmapVisible.Get()) |
| 0 | 177 | | return; |
| 0 | 178 | | if (isScaling) |
| 0 | 179 | | return; |
| 0 | 180 | | previousScaleSize = new Vector3(scale, scale, scale); |
| 0 | 181 | | if (value > 0 && currentZoomLevel < MAP_ZOOM_LEVELS) |
| | 182 | | { |
| 0 | 183 | | currentZoomLevel++; |
| 0 | 184 | | StartCoroutine(ScaleOverTime(previousScaleSize)); |
| | 185 | | } |
| 0 | 186 | | if (value < 0 && currentZoomLevel >= 1) |
| | 187 | | { |
| 0 | 188 | | currentZoomLevel--; |
| 0 | 189 | | StartCoroutine(ScaleOverTime(previousScaleSize)); |
| | 190 | | } |
| 0 | 191 | | HandleZoomButtonsAspect(); |
| 0 | 192 | | } |
| | 193 | |
|
| | 194 | | private void HandleZoomButtonsAspect() |
| | 195 | | { |
| 54 | 196 | | if (currentZoomLevel < MAP_ZOOM_LEVELS) |
| | 197 | | { |
| 54 | 198 | | zoomInButton.interactable = true; |
| 54 | 199 | | zoomInPlus.color = normalColor; |
| 54 | 200 | | } |
| | 201 | | else |
| | 202 | | { |
| 0 | 203 | | zoomInButton.interactable = false; |
| 0 | 204 | | zoomInPlus.color = disabledColor; |
| | 205 | | } |
| | 206 | |
|
| 54 | 207 | | if (currentZoomLevel >= 1) |
| | 208 | | { |
| 54 | 209 | | zoomOutButton.interactable = true; |
| 54 | 210 | | zoomOutMinus.color = normalColor; |
| 54 | 211 | | } |
| | 212 | | else |
| | 213 | | { |
| 0 | 214 | | zoomOutButton.interactable = false; |
| 0 | 215 | | zoomOutMinus.color = disabledColor; |
| | 216 | | } |
| 0 | 217 | | } |
| | 218 | |
|
| | 219 | | private IEnumerator ScaleOverTime(Vector3 startScaleSize) |
| | 220 | | { |
| 0 | 221 | | isScaling = true; |
| 0 | 222 | | scale = zoomCurve.Evaluate(currentZoomLevel); |
| 0 | 223 | | MapRenderer.i.scaleFactor = scale; |
| 0 | 224 | | Vector3 targetScale = new Vector3(scale, scale, scale); |
| | 225 | |
|
| 0 | 226 | | float counter = 0; |
| | 227 | |
|
| 0 | 228 | | while (counter < scaleDuration) |
| | 229 | | { |
| 0 | 230 | | counter += Time.deltaTime; |
| 0 | 231 | | containerRectTransform.localScale = Vector3.Lerp(startScaleSize, targetScale, counter / scaleDuration); |
| 0 | 232 | | yield return null; |
| | 233 | | } |
| | 234 | |
|
| 0 | 235 | | isScaling = false; |
| 0 | 236 | | } |
| | 237 | |
|
| | 238 | | private void OnNavmapVisibleChanged(bool current, bool previous) => |
| 0 | 239 | | SetVisible(current); |
| | 240 | |
|
| | 241 | | private void OnExploreChange(bool current, bool previous) |
| | 242 | | { |
| 0 | 243 | | if (current) |
| 0 | 244 | | return; |
| | 245 | |
|
| 0 | 246 | | SetVisible(false); |
| 0 | 247 | | } |
| | 248 | |
|
| | 249 | | internal void SetVisible(bool visible) |
| | 250 | | { |
| 1 | 251 | | if (waitingForFullscreenHUDOpen) |
| 0 | 252 | | return; |
| | 253 | |
|
| 1 | 254 | | if (visible) |
| | 255 | | { |
| 1 | 256 | | if (CommonScriptableObjects.isFullscreenHUDOpen.Get()) |
| | 257 | | { |
| 0 | 258 | | SetVisibility_Internal(true); |
| 0 | 259 | | } |
| | 260 | | else |
| | 261 | | { |
| 1 | 262 | | waitingForFullscreenHUDOpen = true; |
| 1 | 263 | | CommonScriptableObjects.isFullscreenHUDOpen.OnChange -= IsFullscreenHUDOpen_OnChange; |
| 1 | 264 | | CommonScriptableObjects.isFullscreenHUDOpen.OnChange += IsFullscreenHUDOpen_OnChange; |
| | 265 | | } |
| 1 | 266 | | } |
| | 267 | | else |
| | 268 | | { |
| 0 | 269 | | SetVisibility_Internal(false); |
| | 270 | | } |
| 0 | 271 | | } |
| | 272 | |
|
| | 273 | | private void IsFullscreenHUDOpen_OnChange(bool current, bool previous) |
| | 274 | | { |
| 0 | 275 | | if (!current) |
| 0 | 276 | | return; |
| | 277 | |
|
| 0 | 278 | | SetVisibility_Internal(true); |
| 0 | 279 | | waitingForFullscreenHUDOpen = false; |
| 0 | 280 | | } |
| | 281 | |
|
| | 282 | | internal void SetVisibility_Internal(bool visible) |
| | 283 | | { |
| 0 | 284 | | if (MapRenderer.i == null) |
| 0 | 285 | | return; |
| | 286 | |
|
| 0 | 287 | | scrollRect.StopMovement(); |
| | 288 | |
|
| 0 | 289 | | scrollRect.gameObject.SetActive(visible); |
| 0 | 290 | | MapRenderer.i.parcelHighlightEnabled = visible; |
| | 291 | |
|
| 0 | 292 | | if (visible) |
| | 293 | | { |
| 0 | 294 | | if (!DataStore.i.exploreV2.isInitialized.Get()) |
| 0 | 295 | | Utils.UnlockCursor(); |
| | 296 | |
|
| 0 | 297 | | MapRenderer.i.scaleFactor = scale; |
| | 298 | |
|
| 0 | 299 | | if (minimapViewport == null) |
| 0 | 300 | | minimapViewport = MapRenderer.i.atlas.viewport; |
| | 301 | |
|
| 0 | 302 | | if (mapRendererMinimapParent == null) |
| 0 | 303 | | mapRendererMinimapParent = MapRenderer.i.transform.parent; |
| | 304 | |
|
| 0 | 305 | | atlasOriginalPosition = MapRenderer.i.atlas.chunksParent.transform.localPosition; |
| | 306 | |
|
| 0 | 307 | | MapRenderer.i.atlas.viewport = scrollRect.viewport; |
| 0 | 308 | | MapRenderer.i.transform.SetParent(scrollRectContentTransform); |
| 0 | 309 | | MapRenderer.i.atlas.UpdateCulling(); |
| | 310 | |
|
| 0 | 311 | | scrollRect.content = MapRenderer.i.atlas.chunksParent.transform as RectTransform; |
| | 312 | |
|
| | 313 | | // Center map |
| 0 | 314 | | MapRenderer.i.atlas.CenterToTile(Utils.WorldToGridPositionUnclamped(DataStore.i.player.playerWorldPositi |
| | 315 | |
|
| | 316 | | // Set shorter interval of time for populated scenes markers fetch |
| 0 | 317 | | MapRenderer.i.usersPositionMarkerController?.SetUpdateMode(MapGlobalUsersPositionMarkerController.Update |
| 0 | 318 | | } |
| | 319 | | else |
| | 320 | | { |
| 0 | 321 | | if (minimapViewport == null) |
| 0 | 322 | | return; |
| 0 | 323 | | ResetCameraZoom(); |
| 0 | 324 | | CloseToast(); |
| | 325 | |
|
| 0 | 326 | | MapRenderer.i.atlas.viewport = minimapViewport; |
| 0 | 327 | | MapRenderer.i.transform.SetParent(mapRendererMinimapParent); |
| 0 | 328 | | MapRenderer.i.atlas.chunksParent.transform.localPosition = atlasOriginalPosition; |
| 0 | 329 | | MapRenderer.i.atlas.UpdateCulling(); |
| | 330 | |
|
| 0 | 331 | | MapRenderer.i.UpdateRendering(Utils.WorldToGridPositionUnclamped(DataStore.i.player.playerWorldPosition. |
| | 332 | |
|
| | 333 | | // Set longer interval of time for populated scenes markers fetch |
| 0 | 334 | | MapRenderer.i.usersPositionMarkerController?.SetUpdateMode(MapGlobalUsersPositionMarkerController.Update |
| | 335 | | } |
| | 336 | |
|
| 0 | 337 | | OnToggle?.Invoke(visible); |
| 0 | 338 | | } |
| | 339 | |
|
| | 340 | | private void UpdateCurrentSceneData(Vector2Int current, Vector2Int previous) |
| | 341 | | { |
| | 342 | | const string format = "{0},{1}"; |
| 1 | 343 | | currentSceneCoordsText.text = string.Format(format, current.x, current.y); |
| 1 | 344 | | currentSceneNameText.text = MinimapMetadata.GetMetadata().GetSceneInfo(current.x, current.y)?.name ?? "Unnam |
| 1 | 345 | | } |
| | 346 | |
|
| | 347 | | private void TriggerToast(int cursorTileX, int cursorTileY) |
| | 348 | | { |
| 0 | 349 | | if (toastView.isOpen) |
| 0 | 350 | | CloseToast(); |
| 0 | 351 | | var sceneInfo = mapMetadata.GetSceneInfo(cursorTileX, cursorTileY); |
| 0 | 352 | | if (sceneInfo == null) |
| 0 | 353 | | WebInterface.RequestScenesInfoAroundParcel(new Vector2(cursorTileX, cursorTileY), 15); |
| | 354 | |
|
| 0 | 355 | | toastView.Populate(new Vector2Int(cursorTileX, cursorTileY), sceneInfo); |
| 0 | 356 | | } |
| | 357 | |
|
| | 358 | | private void CloseToast() => |
| 0 | 359 | | toastView.OnCloseClick(); |
| | 360 | |
|
| | 361 | | private void ConfigureMapInFullscreenMenuChanged(Transform currentParentTransform, Transform previousParentTrans |
| 54 | 362 | | SetAsFullScreenMenuMode(currentParentTransform); |
| | 363 | | } |
| | 364 | | } |