< 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:117
Coverable lines:184
Total lines:349
Line coverage:36.4% (67 of 184)
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%
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%
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
 16237        BaseVariable<Transform> configureMapInFullscreenMenu => DataStore.i.exploreV2.configureMapInFullscreenMenu;
 38
 10839        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;
 5645        private float scale = 1f;
 46
 47        private bool isScaling = false;
 5648        private float scaleDuration = 0.2f;
 5649        private Color normalColor = new Color(0f,0f,0f,1f);
 5650        private Color disabledColor = new Color(0f,0f,0f,0.5f);
 51
 52        void Start()
 53        {
 5454            mapMetadata = MinimapMetadata.GetMetadata();
 5455            containerRectTransform = scrollRectContentTransform.GetComponent<RectTransform>();
 56
 5457            closeButton.onClick.AddListener(() =>
 58            {
 059                navmapVisible.Set(false);
 060            });
 5461            scrollRect.onValueChanged.AddListener((x) =>
 62            {
 063                if (!navmapVisible.Get())
 064                    return;
 65
 066                MapRenderer.i.atlas.UpdateCulling();
 067                CloseToast();
 068            });
 69
 5470            toastView.OnGotoClicked += () => navmapVisible.Set(false);
 71
 5472            MapRenderer.OnParcelClicked += TriggerToast;
 5473            MapRenderer.OnCursorFarFromParcel += CloseToast;
 5474            CommonScriptableObjects.playerCoords.OnChange += UpdateCurrentSceneData;
 5475            DataStore.i.exploreV2.isOpen.OnChange += OnExploreChange;
 5476            navmapVisible.OnChange += OnNavmapVisibleChanged;
 77
 5478            configureMapInFullscreenMenu.OnChange += ConfigureMapInFullscreenMenuChanged;
 5479            ConfigureMapInFullscreenMenuChanged(configureMapInFullscreenMenu.Get(), null);
 5480            mouseWheelAction.OnValueChanged += OnMouseWheelChangeValue;
 5481            zoomIn.OnStarted += OnZoomPlusMinus;
 5482            zoomOut.OnStarted += OnZoomPlusMinus;
 5483            zoomInButton.onClick.AddListener(() => {
 084                OnZoomPlusMinus(DCLAction_Hold.ZoomIn);
 085            });
 5486            zoomOutButton.onClick.AddListener(() => {
 087                OnZoomPlusMinus(DCLAction_Hold.ZoomOut);
 088            });
 5489            ResetCameraZoom();
 5490            Initialize();
 5491        }
 92
 93        private void ResetCameraZoom()
 94        {
 5495            currentZoomLevel = Mathf.FloorToInt(MAP_ZOOM_LEVELS / 2);
 5496            scale = zoomCurve.Evaluate(currentZoomLevel);
 5497            containerRectTransform.localScale = new Vector3(scale, scale, scale);
 5498            HandleZoomButtonsAspect();
 5499        }
 100
 101        private void OnZoomPlusMinus(DCLAction_Hold action)
 102        {
 0103            if (!navmapVisible.Get()) return;
 104
 0105            if (action.Equals(DCLAction_Hold.ZoomIn))
 106            {
 0107                CalculateZoomLevelAndDirection(1);
 0108            }
 0109            else if (action.Equals(DCLAction_Hold.ZoomOut))
 110            {
 0111                CalculateZoomLevelAndDirection(-1);
 112            }
 0113            EventSystem.current.SetSelectedGameObject(null);
 0114        }
 115
 116        private void OnMouseWheelChangeValue(DCLAction_Measurable action, float value)
 117        {
 0118            if (value > -MOUSE_WHEEL_THRESHOLD && value < MOUSE_WHEEL_THRESHOLD) return;
 0119            CalculateZoomLevelAndDirection(value);
 0120        }
 121
 122        Vector3 previousScaleSize;
 123
 124        private void CalculateZoomLevelAndDirection(float value)
 125        {
 0126            if (!navmapVisible.Get()) return;
 0127            if (isScaling) return;
 0128            previousScaleSize = new Vector3(scale, scale, scale);
 0129            if (value > 0 && currentZoomLevel < MAP_ZOOM_LEVELS)
 130            {
 0131                currentZoomLevel++;
 0132                StartCoroutine(ScaleOverTime(previousScaleSize));
 133            }
 0134            if (value < 0 && currentZoomLevel >= 1)
 135            {
 0136                currentZoomLevel--;
 0137                StartCoroutine(ScaleOverTime(previousScaleSize));
 138            }
 0139            HandleZoomButtonsAspect();
 0140        }
 141
 142        private void HandleZoomButtonsAspect() {
 54143            if (currentZoomLevel < MAP_ZOOM_LEVELS)
 144            {
 54145                zoomInButton.interactable = true;
 54146                zoomInPlus.color = normalColor;
 54147            }
 148            else
 149            {
 0150                zoomInButton.interactable = false;
 0151                zoomInPlus.color = disabledColor;
 152            }
 153
 54154            if (currentZoomLevel >= 1)
 155            {
 54156                zoomOutButton.interactable = true;
 54157                zoomOutMinus.color = normalColor;
 54158            }
 159            else
 160            {
 0161                zoomOutButton.interactable = false;
 0162                zoomOutMinus.color = disabledColor;
 163            }
 0164        }
 165
 166        private IEnumerator ScaleOverTime(Vector3 startScaleSize)
 167        {
 0168            isScaling = true;
 0169            scale = zoomCurve.Evaluate(currentZoomLevel);
 0170            MapRenderer.i.scaleFactor = scale;
 0171            Vector3 targetScale = new Vector3(scale, scale, scale);
 172
 0173            float counter = 0;
 174
 0175            while (counter < scaleDuration)
 176            {
 0177                counter += Time.deltaTime;
 0178                containerRectTransform.localScale = Vector3.Lerp(startScaleSize, targetScale, counter / scaleDuration);
 0179                yield return null;
 180            }
 181
 0182            isScaling = false;
 0183        }
 184
 0185        private void OnNavmapVisibleChanged(bool current, bool previous) { SetVisible(current); }
 186
 187        public void Initialize()
 188        {
 54189            toastView.gameObject.SetActive(false);
 54190            scrollRect.gameObject.SetActive(false);
 54191            DataStore.i.HUDs.isNavMapInitialized.Set(true);
 54192        }
 193
 194        private void OnDestroy()
 195        {
 54196            MapRenderer.OnParcelClicked -= TriggerToast;
 54197            MapRenderer.OnCursorFarFromParcel -= CloseToast;
 54198            CommonScriptableObjects.playerCoords.OnChange -= UpdateCurrentSceneData;
 54199            navmapVisible.OnChange -= OnNavmapVisibleChanged;
 54200            configureMapInFullscreenMenu.OnChange -= ConfigureMapInFullscreenMenuChanged;
 54201            mouseWheelAction.OnValueChanged -= OnMouseWheelChangeValue;
 54202            zoomIn.OnStarted -= OnZoomPlusMinus;
 54203            zoomOut.OnStarted -= OnZoomPlusMinus;
 54204            CommonScriptableObjects.isFullscreenHUDOpen.OnChange -= IsFullscreenHUDOpen_OnChange;
 54205            DataStore.i.exploreV2.isOpen.OnChange -= OnExploreChange;
 54206        }
 207
 208        private void OnExploreChange(bool current, bool previous)
 209        {
 0210            if (current)
 0211                return;
 212
 0213            SetVisible(false);
 0214        }
 215
 216        internal void SetVisible(bool visible)
 217        {
 1218            if (waitingForFullscreenHUDOpen)
 0219                return;
 220
 1221            if (visible)
 222            {
 1223                if (CommonScriptableObjects.isFullscreenHUDOpen.Get())
 224                {
 0225                    SetVisibility_Internal(true);
 0226                }
 227                else
 228                {
 1229                    waitingForFullscreenHUDOpen = true;
 1230                    CommonScriptableObjects.isFullscreenHUDOpen.OnChange -= IsFullscreenHUDOpen_OnChange;
 1231                    CommonScriptableObjects.isFullscreenHUDOpen.OnChange += IsFullscreenHUDOpen_OnChange;
 232                }
 1233            }
 234            else
 235            {
 0236                SetVisibility_Internal(false);
 237            }
 0238        }
 239
 240        private void IsFullscreenHUDOpen_OnChange(bool current, bool previous)
 241        {
 0242            if (!current)
 0243                return;
 244
 0245            SetVisibility_Internal(true);
 0246            waitingForFullscreenHUDOpen = false;
 0247        }
 248
 249        internal void SetVisibility_Internal(bool visible)
 250        {
 0251            if (MapRenderer.i == null)
 0252                return;
 253
 0254            scrollRect.StopMovement();
 255
 0256            scrollRect.gameObject.SetActive(visible);
 0257            MapRenderer.i.parcelHighlightEnabled = visible;
 258
 0259            if (visible)
 260            {
 0261                if (!DataStore.i.exploreV2.isInitialized.Get())
 0262                    Utils.UnlockCursor();
 263
 0264                MapRenderer.i.scaleFactor = scale;
 265
 0266                if(minimapViewport == null)
 0267                    minimapViewport = MapRenderer.i.atlas.viewport;
 268
 0269                if (mapRendererMinimapParent == null)
 0270                    mapRendererMinimapParent = MapRenderer.i.transform.parent;
 271
 0272                atlasOriginalPosition = MapRenderer.i.atlas.chunksParent.transform.localPosition;
 273
 0274                MapRenderer.i.atlas.viewport = scrollRect.viewport;
 0275                MapRenderer.i.transform.SetParent(scrollRectContentTransform);
 0276                MapRenderer.i.atlas.UpdateCulling();
 277
 0278                scrollRect.content = MapRenderer.i.atlas.chunksParent.transform as RectTransform;
 279
 280                // Center map
 0281                MapRenderer.i.atlas.CenterToTile(Utils.WorldToGridPositionUnclamped(DataStore.i.player.playerWorldPositi
 282
 283                // Set shorter interval of time for populated scenes markers fetch
 0284                MapRenderer.i.usersPositionMarkerController?.SetUpdateMode(MapGlobalUsersPositionMarkerController.Update
 0285            }
 286            else
 287            {
 0288                if (minimapViewport == null)
 0289                    return;
 0290                ResetCameraZoom();
 0291                CloseToast();
 292
 0293                MapRenderer.i.atlas.viewport = minimapViewport;
 0294                MapRenderer.i.transform.SetParent(mapRendererMinimapParent);
 0295                MapRenderer.i.atlas.chunksParent.transform.localPosition = atlasOriginalPosition;
 0296                MapRenderer.i.atlas.UpdateCulling();
 297
 0298                MapRenderer.i.UpdateRendering(Utils.WorldToGridPositionUnclamped(DataStore.i.player.playerWorldPosition.
 299
 300                // Set longer interval of time for populated scenes markers fetch
 0301                MapRenderer.i.usersPositionMarkerController?.SetUpdateMode(MapGlobalUsersPositionMarkerController.Update
 302            }
 303
 0304            OnToggle?.Invoke(visible);
 0305        }
 306
 307        void UpdateCurrentSceneData(Vector2Int current, Vector2Int previous)
 308        {
 309            const string format = "{0},{1}";
 1310            currentSceneCoordsText.text = string.Format(format, current.x, current.y);
 1311            currentSceneNameText.text = MinimapMetadata.GetMetadata().GetSceneInfo(current.x, current.y)?.name ?? "Unnam
 1312        }
 313
 314        void TriggerToast(int cursorTileX, int cursorTileY)
 315        {
 0316            if(toastView.isOpen)
 0317                CloseToast();
 0318            var sceneInfo = mapMetadata.GetSceneInfo(cursorTileX, cursorTileY);
 0319            if (sceneInfo == null)
 0320                WebInterface.RequestScenesInfoAroundParcel(new Vector2(cursorTileX, cursorTileY), 15);
 321
 0322            toastView.Populate(new Vector2Int(cursorTileX, cursorTileY), sceneInfo);
 0323        }
 324
 0325        private void CloseToast() { toastView.OnCloseClick(); }
 326
 0327        public void SetExitButtonActive(bool isActive) { closeButton.gameObject.SetActive(isActive); }
 328
 329        public void SetAsFullScreenMenuMode(Transform parentTransform)
 330        {
 54331            if (parentTransform == null)
 54332                return;
 333
 0334            transform.SetParent(parentTransform);
 0335            transform.localScale = Vector3.one;
 0336            SetExitButtonActive(false);
 337
 0338            RectTransform rectTransform = transform as RectTransform;
 0339            rectTransform.anchorMin = Vector2.zero;
 0340            rectTransform.anchorMax = Vector2.one;
 0341            rectTransform.pivot = new Vector2(0.5f, 0.5f);
 0342            rectTransform.localPosition = Vector2.zero;
 0343            rectTransform.offsetMax = Vector2.zero;
 0344            rectTransform.offsetMin = Vector2.zero;
 0345        }
 346
 108347        private void ConfigureMapInFullscreenMenuChanged(Transform currentParentTransform, Transform previousParentTrans
 348    }
 349}