< 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:65
Uncovered lines:116
Coverable lines:181
Total lines:347
Line coverage:35.9% (65 of 181)
Covered branches:0
Total branches:0

Metrics

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

File(s)

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

#LineLine coverage
 1using UnityEngine;
 2using System.Collections;
 3using UnityEngine.UI;
 4using DCL.Interface;
 5using DCL.Helpers;
 6using TMPro;
 7using System;
 8using UnityEngine.EventSystems;
 9
 10namespace 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
 15937        BaseVariable<Transform> configureMapInFullscreenMenu => DataStore.i.exploreV2.configureMapInFullscreenMenu;
 38
 10639        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;
 5445        private float scale = 1f;
 46
 47        private bool isScaling = false;
 5448        private float scaleDuration = 0.2f;
 5449        private Color normalColor = new Color(0f,0f,0f,1f);
 5450        private Color disabledColor = new Color(0f,0f,0f,0.5f);
 51
 52        void Start()
 53        {
 5354            mapMetadata = MinimapMetadata.GetMetadata();
 5355            containerRectTransform = scrollRectContentTransform.GetComponent<RectTransform>();
 56
 5357            closeButton.onClick.AddListener(() =>
 58            {
 059                navmapVisible.Set(false);
 060            });
 5361            scrollRect.onValueChanged.AddListener((x) =>
 62            {
 063                if (!navmapVisible.Get())
 064                    return;
 65
 066                MapRenderer.i.atlas.UpdateCulling();
 067                CloseToast();
 068            });
 69
 5370            toastView.OnGotoClicked += () => navmapVisible.Set(false);
 71
 5372            MapRenderer.OnParcelClicked += TriggerToast;
 5373            MapRenderer.OnCursorFarFromParcel += CloseToast;
 5374            CommonScriptableObjects.playerCoords.OnChange += UpdateCurrentSceneData;
 5375            navmapVisible.OnChange += OnNavmapVisibleChanged;
 76
 5377            configureMapInFullscreenMenu.OnChange += ConfigureMapInFullscreenMenuChanged;
 5378            ConfigureMapInFullscreenMenuChanged(configureMapInFullscreenMenu.Get(), null);
 5379            mouseWheelAction.OnValueChanged += OnMouseWheelChangeValue;
 5380            zoomIn.OnStarted += OnZoomPlusMinus;
 5381            zoomOut.OnStarted += OnZoomPlusMinus;
 5382            zoomInButton.onClick.AddListener(() => {
 083                OnZoomPlusMinus(DCLAction_Hold.ZoomIn);
 084            });
 5385            zoomOutButton.onClick.AddListener(() => {
 086                OnZoomPlusMinus(DCLAction_Hold.ZoomOut);
 087            });
 5388            ResetCameraZoom();
 5389            Initialize();
 5390        }
 91
 92        private void ResetCameraZoom()
 93        {
 5394            currentZoomLevel = Mathf.FloorToInt(MAP_ZOOM_LEVELS / 2);
 5395            scale = zoomCurve.Evaluate(currentZoomLevel);
 5396            containerRectTransform.localScale = new Vector3(scale, scale, scale);
 5397            HandleZoomButtonsAspect();
 5398        }
 99
 100        private void OnZoomPlusMinus(DCLAction_Hold action)
 101        {
 0102            if (!navmapVisible.Get()) return;
 103
 0104            if (action.Equals(DCLAction_Hold.ZoomIn))
 105            {
 0106                CalculateZoomLevelAndDirection(1);
 0107            }
 0108            else if (action.Equals(DCLAction_Hold.ZoomOut))
 109            {
 0110                CalculateZoomLevelAndDirection(-1);
 111            }
 0112            EventSystem.current.SetSelectedGameObject(null);
 0113        }
 114
 115        private void OnMouseWheelChangeValue(DCLAction_Measurable action, float value)
 116        {
 0117            if (value > -MOUSE_WHEEL_THRESHOLD && value < MOUSE_WHEEL_THRESHOLD) return;
 0118            CalculateZoomLevelAndDirection(value);
 0119        }
 120
 121        Vector3 previousScaleSize;
 122
 123        private void CalculateZoomLevelAndDirection(float value)
 124        {
 0125            if (!navmapVisible.Get()) return;
 0126            if (isScaling) return;
 0127            previousScaleSize = new Vector3(scale, scale, scale);
 0128            if (value > 0 && currentZoomLevel < MAP_ZOOM_LEVELS)
 129            {
 0130                currentZoomLevel++;
 0131                StartCoroutine(ScaleOverTime(previousScaleSize));
 132            }
 0133            if (value < 0 && currentZoomLevel >= 1)
 134            {
 0135                currentZoomLevel--;
 0136                StartCoroutine(ScaleOverTime(previousScaleSize));
 137            }
 0138            HandleZoomButtonsAspect();
 0139        }
 140
 141        private void HandleZoomButtonsAspect() {
 53142            if (currentZoomLevel < MAP_ZOOM_LEVELS)
 143            {
 53144                zoomInButton.interactable = true;
 53145                zoomInPlus.color = normalColor;
 53146            }
 147            else
 148            {
 0149                zoomInButton.interactable = false;
 0150                zoomInPlus.color = disabledColor;
 151            }
 152
 53153            if (currentZoomLevel >= 1)
 154            {
 53155                zoomOutButton.interactable = true;
 53156                zoomOutMinus.color = normalColor;
 53157            }
 158            else
 159            {
 0160                zoomOutButton.interactable = false;
 0161                zoomOutMinus.color = disabledColor;
 162            }
 0163        }
 164
 165        private IEnumerator ScaleOverTime(Vector3 startScaleSize)
 166        {
 0167            isScaling = true;
 0168            scale = zoomCurve.Evaluate(currentZoomLevel);
 0169            MapRenderer.i.scaleFactor = scale;
 0170            Vector3 targetScale = new Vector3(scale, scale, scale);
 171
 0172            float counter = 0;
 173
 0174            while (counter < scaleDuration)
 175            {
 0176                counter += Time.deltaTime;
 0177                containerRectTransform.localScale = Vector3.Lerp(startScaleSize, targetScale, counter / scaleDuration);
 0178                yield return null;
 179            }
 180
 0181            isScaling = false;
 0182        }
 183
 0184        private void OnNavmapVisibleChanged(bool current, bool previous) { SetVisible(current); }
 185
 186        public void Initialize()
 187        {
 53188            toastView.gameObject.SetActive(false);
 53189            scrollRect.gameObject.SetActive(false);
 53190            DataStore.i.HUDs.isNavMapInitialized.Set(true);
 53191        }
 192
 193        private void OnDestroy()
 194        {
 53195            MapRenderer.OnParcelClicked -= TriggerToast;
 53196            MapRenderer.OnCursorFarFromParcel -= CloseToast;
 53197            CommonScriptableObjects.playerCoords.OnChange -= UpdateCurrentSceneData;
 53198            navmapVisible.OnChange -= OnNavmapVisibleChanged;
 53199            configureMapInFullscreenMenu.OnChange -= ConfigureMapInFullscreenMenuChanged;
 53200            mouseWheelAction.OnValueChanged -= OnMouseWheelChangeValue;
 53201            zoomIn.OnStarted -= OnZoomPlusMinus;
 53202            zoomOut.OnStarted -= OnZoomPlusMinus;
 53203            CommonScriptableObjects.isFullscreenHUDOpen.OnChange -= IsFullscreenHUDOpen_OnChange;
 53204        }
 205
 206        internal void SetVisible(bool visible)
 207        {
 1208            if (waitingForFullscreenHUDOpen)
 0209                return;
 210
 1211            if (visible)
 212            {
 1213                if (CommonScriptableObjects.isFullscreenHUDOpen.Get())
 214                {
 0215                    SetVisibility_Internal(true);
 0216                }
 217                else
 218                {
 1219                    waitingForFullscreenHUDOpen = true;
 1220                    CommonScriptableObjects.isFullscreenHUDOpen.OnChange -= IsFullscreenHUDOpen_OnChange;
 1221                    CommonScriptableObjects.isFullscreenHUDOpen.OnChange += IsFullscreenHUDOpen_OnChange;
 222                }
 1223            }
 224            else
 225            {
 0226                SetVisibility_Internal(false);
 227            }
 0228        }
 229
 230        private void IsFullscreenHUDOpen_OnChange(bool current, bool previous)
 231        {
 232
 0233            if (!current)
 0234                return;
 235
 0236            SetVisibility_Internal(true);
 0237            waitingForFullscreenHUDOpen = false;
 0238        }
 239
 240        internal void SetVisibility_Internal(bool visible)
 241        {
 0242            if (MapRenderer.i == null)
 0243                return;
 244
 0245            scrollRect.StopMovement();
 246
 0247            scrollRect.gameObject.SetActive(visible);
 0248            MapRenderer.i.parcelHighlightEnabled = visible;
 249
 0250            if (visible)
 251            {
 0252                if (!DataStore.i.exploreV2.isInitialized.Get())
 0253                    Utils.UnlockCursor();
 254
 0255                MapRenderer.i.scaleFactor = scale;
 256
 0257                if(minimapViewport == null)
 0258                    minimapViewport = MapRenderer.i.atlas.viewport;
 259
 0260                if (mapRendererMinimapParent == null)
 0261                    mapRendererMinimapParent = MapRenderer.i.transform.parent;
 262
 0263                atlasOriginalPosition = MapRenderer.i.atlas.chunksParent.transform.localPosition;
 264
 0265                MapRenderer.i.atlas.viewport = scrollRect.viewport;
 0266                MapRenderer.i.transform.SetParent(scrollRectContentTransform);
 0267                MapRenderer.i.atlas.UpdateCulling();
 268
 0269                scrollRect.content = MapRenderer.i.atlas.chunksParent.transform as RectTransform;
 270
 271                // Reparent the player icon parent to scroll everything together
 0272                MapRenderer.i.atlas.overlayLayerGameobject.transform.SetParent(scrollRect.content);
 273
 274                // Center map
 0275                MapRenderer.i.atlas.CenterToTile(Utils.WorldToGridPositionUnclamped(CommonScriptableObjects.playerWorldP
 276
 277                // Set shorter interval of time for populated scenes markers fetch
 0278                MapRenderer.i.usersPositionMarkerController?.SetUpdateMode(MapGlobalUsersPositionMarkerController.Update
 0279            }
 280            else
 281            {
 0282                if (minimapViewport == null)
 0283                    return;
 0284                ResetCameraZoom();
 0285                CloseToast();
 286
 0287                MapRenderer.i.atlas.viewport = minimapViewport;
 0288                MapRenderer.i.transform.SetParent(mapRendererMinimapParent);
 0289                MapRenderer.i.atlas.chunksParent.transform.localPosition = atlasOriginalPosition;
 0290                MapRenderer.i.atlas.UpdateCulling();
 291
 292                // Restore the player icon to its original parent
 0293                MapRenderer.i.atlas.overlayLayerGameobject.transform.SetParent(MapRenderer.i.atlas.chunksParent.transfor
 0294                (MapRenderer.i.atlas.overlayLayerGameobject.transform as RectTransform).anchoredPosition = Vector2.zero;
 295
 0296                MapRenderer.i.UpdateRendering(Utils.WorldToGridPositionUnclamped(CommonScriptableObjects.playerWorldPosi
 297
 298                // Set longer interval of time for populated scenes markers fetch
 0299                MapRenderer.i.usersPositionMarkerController?.SetUpdateMode(MapGlobalUsersPositionMarkerController.Update
 300            }
 301
 0302            OnToggle?.Invoke(visible);
 0303        }
 304
 305        void UpdateCurrentSceneData(Vector2Int current, Vector2Int previous)
 306        {
 307            const string format = "{0},{1}";
 1308            currentSceneCoordsText.text = string.Format(format, current.x, current.y);
 1309            currentSceneNameText.text = MinimapMetadata.GetMetadata().GetSceneInfo(current.x, current.y)?.name ?? "Unnam
 1310        }
 311
 312        void TriggerToast(int cursorTileX, int cursorTileY)
 313        {
 0314            if(toastView.isOpen)
 0315                CloseToast();
 0316            var sceneInfo = mapMetadata.GetSceneInfo(cursorTileX, cursorTileY);
 0317            if (sceneInfo == null)
 0318                WebInterface.RequestScenesInfoAroundParcel(new Vector2(cursorTileX, cursorTileY), 15);
 319
 0320            toastView.Populate(new Vector2Int(cursorTileX, cursorTileY), sceneInfo);
 0321        }
 322
 0323        private void CloseToast() { toastView.OnCloseClick(); }
 324
 0325        public void SetExitButtonActive(bool isActive) { closeButton.gameObject.SetActive(isActive); }
 326
 327        public void SetAsFullScreenMenuMode(Transform parentTransform)
 328        {
 53329            if (parentTransform == null)
 53330                return;
 331
 0332            transform.SetParent(parentTransform);
 0333            transform.localScale = Vector3.one;
 0334            SetExitButtonActive(false);
 335
 0336            RectTransform rectTransform = transform as RectTransform;
 0337            rectTransform.anchorMin = Vector2.zero;
 0338            rectTransform.anchorMax = Vector2.one;
 0339            rectTransform.pivot = new Vector2(0.5f, 0.5f);
 0340            rectTransform.localPosition = Vector2.zero;
 0341            rectTransform.offsetMax = Vector2.zero;
 0342            rectTransform.offsetMin = Vector2.zero;
 0343        }
 344
 106345        private void ConfigureMapInFullscreenMenuChanged(Transform currentParentTransform, Transform previousParentTrans
 346    }
 347}