< Summary

Class:DCL.NavmapView
Assembly:Navmap
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NavMap/NavmapView.cs
Covered lines:67
Uncovered lines:121
Coverable lines:188
Total lines:364
Line coverage:35.6% (67 of 188)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
NavmapView()0%110100%
Start()0%110100%
OnDestroy()0%110100%
Initialize()0%110100%
SetExitButtonActive(...)0%2100%
SetAsFullScreenMenuMode(...)0%4.422015.38%
ResetCameraZoom()0%110100%
OnZoomPlusMinus(...)0%20400%
OnMouseWheelChangeValue(...)0%12300%
CalculateZoomLevelAndDirection(...)0%56700%
HandleZoomButtonsAspect()0%3.513061.54%
ScaleOverTime()0%20400%
OnNavmapVisibleChanged(...)0%2100%
OnExploreChange(...)0%6200%
SetVisible(...)0%5.164058.33%
IsFullscreenHUDOpen_OnChange(...)0%6200%
SetVisibility_Internal(...)0%1101000%
UpdateCurrentSceneData(...)0%440100%
TriggerToast(...)0%12300%
CloseToast()0%2100%
ConfigureMapInFullscreenMenuChanged(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NavMap/NavmapView.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using DCL.Helpers;
 4using DCL.Interface;
 5using TMPro;
 6using UnityEngine;
 7using UnityEngine.EventSystems;
 8using UnityEngine.UI;
 9
 10namespace 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;
 5638        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
 5644        private Color normalColor = new Color(0f, 0f, 0f, 1f);
 45        Vector3 previousScaleSize;
 5646        private float scale = 1f;
 5647        private float scaleDuration = 0.2f;
 48
 49        private InputAction_Trigger.Triggered selectParcelDelegate;
 50        private bool waitingForFullscreenHUDOpen = false;
 51
 10852        public BaseVariable<bool> navmapVisible => DataStore.i.HUDs.navmapVisible;
 53
 16254        private BaseVariable<Transform> configureMapInFullscreenMenu => DataStore.i.exploreV2.configureMapInFullscreenMe
 55
 56        void Start()
 57        {
 5458            mapMetadata = MinimapMetadata.GetMetadata();
 5459            containerRectTransform = scrollRectContentTransform.GetComponent<RectTransform>();
 60
 5461            closeButton.onClick.AddListener(() =>
 62            {
 063                navmapVisible.Set(false);
 064            });
 5465            scrollRect.onValueChanged.AddListener((x) =>
 66            {
 067                if (!navmapVisible.Get())
 068                    return;
 69
 070                MapRenderer.i.atlas.UpdateCulling();
 071                CloseToast();
 072            });
 73
 5474            toastView.OnGotoClicked += () => navmapVisible.Set(false);
 75
 5476            MapRenderer.OnParcelClicked += TriggerToast;
 5477            MapRenderer.OnCursorFarFromParcel += CloseToast;
 5478            CommonScriptableObjects.playerCoords.OnChange += UpdateCurrentSceneData;
 5479            DataStore.i.exploreV2.isOpen.OnChange += OnExploreChange;
 5480            navmapVisible.OnChange += OnNavmapVisibleChanged;
 81
 5482            configureMapInFullscreenMenu.OnChange += ConfigureMapInFullscreenMenuChanged;
 5483            ConfigureMapInFullscreenMenuChanged(configureMapInFullscreenMenu.Get(), null);
 5484            mouseWheelAction.OnValueChanged += OnMouseWheelChangeValue;
 5485            zoomIn.OnStarted += OnZoomPlusMinus;
 5486            zoomOut.OnStarted += OnZoomPlusMinus;
 5487            zoomInButton.onClick.AddListener(() =>
 88            {
 089                OnZoomPlusMinus(DCLAction_Hold.ZoomIn);
 090            });
 5491            zoomOutButton.onClick.AddListener(() =>
 92            {
 093                OnZoomPlusMinus(DCLAction_Hold.ZoomOut);
 094            });
 5495            ResetCameraZoom();
 5496            Initialize();
 5497        }
 98
 99        private void OnDestroy()
 100        {
 54101            MapRenderer.OnParcelClicked -= TriggerToast;
 54102            MapRenderer.OnCursorFarFromParcel -= CloseToast;
 54103            CommonScriptableObjects.playerCoords.OnChange -= UpdateCurrentSceneData;
 54104            navmapVisible.OnChange -= OnNavmapVisibleChanged;
 54105            configureMapInFullscreenMenu.OnChange -= ConfigureMapInFullscreenMenuChanged;
 54106            mouseWheelAction.OnValueChanged -= OnMouseWheelChangeValue;
 54107            zoomIn.OnStarted -= OnZoomPlusMinus;
 54108            zoomOut.OnStarted -= OnZoomPlusMinus;
 54109            CommonScriptableObjects.isFullscreenHUDOpen.OnChange -= IsFullscreenHUDOpen_OnChange;
 54110            DataStore.i.exploreV2.isOpen.OnChange -= OnExploreChange;
 54111        }
 112
 113        public event Action<bool> OnToggle;
 114
 115        public void Initialize()
 116        {
 54117            toastView.gameObject.SetActive(false);
 54118            scrollRect.gameObject.SetActive(false);
 54119            DataStore.i.HUDs.isNavMapInitialized.Set(true);
 54120        }
 121
 122        public void SetExitButtonActive(bool isActive) =>
 0123            closeButton.gameObject.SetActive(isActive);
 124
 125        public void SetAsFullScreenMenuMode(Transform parentTransform)
 126        {
 54127            if (parentTransform == null)
 54128                return;
 129
 0130            transform.SetParent(parentTransform);
 0131            transform.localScale = Vector3.one;
 0132            SetExitButtonActive(false);
 133
 0134            RectTransform rectTransform = transform as RectTransform;
 0135            rectTransform.anchorMin = Vector2.zero;
 0136            rectTransform.anchorMax = Vector2.one;
 0137            rectTransform.pivot = new Vector2(0.5f, 0.5f);
 0138            rectTransform.localPosition = Vector2.zero;
 0139            rectTransform.offsetMax = Vector2.zero;
 0140            rectTransform.offsetMin = Vector2.zero;
 0141        }
 142
 143        private void ResetCameraZoom()
 144        {
 54145            currentZoomLevel = Mathf.FloorToInt(MAP_ZOOM_LEVELS / 2);
 54146            scale = zoomCurve.Evaluate(currentZoomLevel);
 54147            containerRectTransform.localScale = new Vector3(scale, scale, scale);
 54148            HandleZoomButtonsAspect();
 54149        }
 150
 151        private void OnZoomPlusMinus(DCLAction_Hold action)
 152        {
 0153            if (!navmapVisible.Get())
 0154                return;
 155
 0156            if (action.Equals(DCLAction_Hold.ZoomIn))
 157            {
 0158                CalculateZoomLevelAndDirection(1);
 0159            }
 0160            else if (action.Equals(DCLAction_Hold.ZoomOut))
 161            {
 0162                CalculateZoomLevelAndDirection(-1);
 163            }
 0164            EventSystem.current.SetSelectedGameObject(null);
 0165        }
 166
 167        private void OnMouseWheelChangeValue(DCLAction_Measurable action, float value)
 168        {
 0169            if (value > -MOUSE_WHEEL_THRESHOLD && value < MOUSE_WHEEL_THRESHOLD)
 0170                return;
 0171            CalculateZoomLevelAndDirection(value);
 0172        }
 173
 174        private void CalculateZoomLevelAndDirection(float value)
 175        {
 0176            if (!navmapVisible.Get())
 0177                return;
 0178            if (isScaling)
 0179                return;
 0180            previousScaleSize = new Vector3(scale, scale, scale);
 0181            if (value > 0 && currentZoomLevel < MAP_ZOOM_LEVELS)
 182            {
 0183                currentZoomLevel++;
 0184                StartCoroutine(ScaleOverTime(previousScaleSize));
 185            }
 0186            if (value < 0 && currentZoomLevel >= 1)
 187            {
 0188                currentZoomLevel--;
 0189                StartCoroutine(ScaleOverTime(previousScaleSize));
 190            }
 0191            HandleZoomButtonsAspect();
 0192        }
 193
 194        private void HandleZoomButtonsAspect()
 195        {
 54196            if (currentZoomLevel < MAP_ZOOM_LEVELS)
 197            {
 54198                zoomInButton.interactable = true;
 54199                zoomInPlus.color = normalColor;
 54200            }
 201            else
 202            {
 0203                zoomInButton.interactable = false;
 0204                zoomInPlus.color = disabledColor;
 205            }
 206
 54207            if (currentZoomLevel >= 1)
 208            {
 54209                zoomOutButton.interactable = true;
 54210                zoomOutMinus.color = normalColor;
 54211            }
 212            else
 213            {
 0214                zoomOutButton.interactable = false;
 0215                zoomOutMinus.color = disabledColor;
 216            }
 0217        }
 218
 219        private IEnumerator ScaleOverTime(Vector3 startScaleSize)
 220        {
 0221            isScaling = true;
 0222            scale = zoomCurve.Evaluate(currentZoomLevel);
 0223            MapRenderer.i.scaleFactor = scale;
 0224            Vector3 targetScale = new Vector3(scale, scale, scale);
 225
 0226            float counter = 0;
 227
 0228            while (counter < scaleDuration)
 229            {
 0230                counter += Time.deltaTime;
 0231                containerRectTransform.localScale = Vector3.Lerp(startScaleSize, targetScale, counter / scaleDuration);
 0232                yield return null;
 233            }
 234
 0235            isScaling = false;
 0236        }
 237
 238        private void OnNavmapVisibleChanged(bool current, bool previous) =>
 0239            SetVisible(current);
 240
 241        private void OnExploreChange(bool current, bool previous)
 242        {
 0243            if (current)
 0244                return;
 245
 0246            SetVisible(false);
 0247        }
 248
 249        internal void SetVisible(bool visible)
 250        {
 1251            if (waitingForFullscreenHUDOpen)
 0252                return;
 253
 1254            if (visible)
 255            {
 1256                if (CommonScriptableObjects.isFullscreenHUDOpen.Get())
 257                {
 0258                    SetVisibility_Internal(true);
 0259                }
 260                else
 261                {
 1262                    waitingForFullscreenHUDOpen = true;
 1263                    CommonScriptableObjects.isFullscreenHUDOpen.OnChange -= IsFullscreenHUDOpen_OnChange;
 1264                    CommonScriptableObjects.isFullscreenHUDOpen.OnChange += IsFullscreenHUDOpen_OnChange;
 265                }
 1266            }
 267            else
 268            {
 0269                SetVisibility_Internal(false);
 270            }
 0271        }
 272
 273        private void IsFullscreenHUDOpen_OnChange(bool current, bool previous)
 274        {
 0275            if (!current)
 0276                return;
 277
 0278            SetVisibility_Internal(true);
 0279            waitingForFullscreenHUDOpen = false;
 0280        }
 281
 282        internal void SetVisibility_Internal(bool visible)
 283        {
 0284            if (MapRenderer.i == null)
 0285                return;
 286
 0287            scrollRect.StopMovement();
 288
 0289            scrollRect.gameObject.SetActive(visible);
 0290            MapRenderer.i.parcelHighlightEnabled = visible;
 291
 0292            if (visible)
 293            {
 0294                if (!DataStore.i.exploreV2.isInitialized.Get())
 0295                    Utils.UnlockCursor();
 296
 0297                MapRenderer.i.scaleFactor = scale;
 298
 0299                if (minimapViewport == null)
 0300                    minimapViewport = MapRenderer.i.atlas.viewport;
 301
 0302                if (mapRendererMinimapParent == null)
 0303                    mapRendererMinimapParent = MapRenderer.i.transform.parent;
 304
 0305                atlasOriginalPosition = MapRenderer.i.atlas.chunksParent.transform.localPosition;
 306
 0307                MapRenderer.i.atlas.viewport = scrollRect.viewport;
 0308                MapRenderer.i.transform.SetParent(scrollRectContentTransform);
 0309                MapRenderer.i.atlas.UpdateCulling();
 310
 0311                scrollRect.content = MapRenderer.i.atlas.chunksParent.transform as RectTransform;
 312
 313                // Center map
 0314                MapRenderer.i.atlas.CenterToTile(Utils.WorldToGridPositionUnclamped(DataStore.i.player.playerWorldPositi
 315
 316                // Set shorter interval of time for populated scenes markers fetch
 0317                MapRenderer.i.usersPositionMarkerController?.SetUpdateMode(MapGlobalUsersPositionMarkerController.Update
 0318            }
 319            else
 320            {
 0321                if (minimapViewport == null)
 0322                    return;
 0323                ResetCameraZoom();
 0324                CloseToast();
 325
 0326                MapRenderer.i.atlas.viewport = minimapViewport;
 0327                MapRenderer.i.transform.SetParent(mapRendererMinimapParent);
 0328                MapRenderer.i.atlas.chunksParent.transform.localPosition = atlasOriginalPosition;
 0329                MapRenderer.i.atlas.UpdateCulling();
 330
 0331                MapRenderer.i.UpdateRendering(Utils.WorldToGridPositionUnclamped(DataStore.i.player.playerWorldPosition.
 332
 333                // Set longer interval of time for populated scenes markers fetch
 0334                MapRenderer.i.usersPositionMarkerController?.SetUpdateMode(MapGlobalUsersPositionMarkerController.Update
 335            }
 336
 0337            OnToggle?.Invoke(visible);
 0338        }
 339
 340        private void UpdateCurrentSceneData(Vector2Int current, Vector2Int previous)
 341        {
 342            const string format = "{0},{1}";
 1343            currentSceneCoordsText.text = string.Format(format, current.x, current.y);
 1344            currentSceneNameText.text = MinimapMetadata.GetMetadata().GetSceneInfo(current.x, current.y)?.name ?? "Unnam
 1345        }
 346
 347        private void TriggerToast(int cursorTileX, int cursorTileY)
 348        {
 0349            if (toastView.isOpen)
 0350                CloseToast();
 0351            var sceneInfo = mapMetadata.GetSceneInfo(cursorTileX, cursorTileY);
 0352            if (sceneInfo == null)
 0353                WebInterface.RequestScenesInfoAroundParcel(new Vector2(cursorTileX, cursorTileY), 15);
 354
 0355            toastView.Populate(new Vector2Int(cursorTileX, cursorTileY), sceneInfo);
 0356        }
 357
 358        private void CloseToast() =>
 0359            toastView.OnCloseClick();
 360
 361        private void ConfigureMapInFullscreenMenuChanged(Transform currentParentTransform, Transform previousParentTrans
 54362            SetAsFullScreenMenuMode(currentParentTransform);
 363    }
 364}