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