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