< 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:63
Uncovered lines:114
Coverable lines:177
Total lines:339
Line coverage:35.5% (63 of 177)
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%12300%
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.54054.55%
IsFullscreenHUDOpen_OnChange(...)0%6200%
SetVisibility_Internal(...)0%72800%
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 (action.Equals(DCLAction_Hold.ZoomIn))
 103            {
 0104                CalculateZoomLevelAndDirection(1);
 0105            }
 0106            else if (action.Equals(DCLAction_Hold.ZoomOut))
 107            {
 0108                CalculateZoomLevelAndDirection(-1);
 109            }
 0110            EventSystem.current.SetSelectedGameObject(null);
 0111        }
 112
 113        private void OnMouseWheelChangeValue(DCLAction_Measurable action, float value)
 114        {
 0115            if (value > -MOUSE_WHEEL_THRESHOLD && value < MOUSE_WHEEL_THRESHOLD) return;
 0116            CalculateZoomLevelAndDirection(value);
 0117        }
 118
 119        Vector3 previousScaleSize;
 120
 121        private void CalculateZoomLevelAndDirection(float value)
 122        {
 0123            if (!navmapVisible.Get()) return;
 0124            if (isScaling) return;
 0125            previousScaleSize = new Vector3(scale, scale, scale);
 0126            if (value > 0 && currentZoomLevel < MAP_ZOOM_LEVELS)
 127            {
 0128                currentZoomLevel++;
 0129                StartCoroutine(ScaleOverTime(previousScaleSize));
 130            }
 0131            if (value < 0 && currentZoomLevel >= 1)
 132            {
 0133                currentZoomLevel--;
 0134                StartCoroutine(ScaleOverTime(previousScaleSize));
 135            }
 0136            HandleZoomButtonsAspect();
 0137        }
 138
 139        private void HandleZoomButtonsAspect() {
 53140            if (currentZoomLevel < MAP_ZOOM_LEVELS)
 141            {
 53142                zoomInButton.interactable = true;
 53143                zoomInPlus.color = normalColor;
 53144            }
 145            else
 146            {
 0147                zoomInButton.interactable = false;
 0148                zoomInPlus.color = disabledColor;
 149            }
 150
 53151            if (currentZoomLevel >= 1)
 152            {
 53153                zoomOutButton.interactable = true;
 53154                zoomOutMinus.color = normalColor;
 53155            }
 156            else
 157            {
 0158                zoomOutButton.interactable = false;
 0159                zoomOutMinus.color = disabledColor;
 160            }
 0161        }
 162
 163        private IEnumerator ScaleOverTime(Vector3 startScaleSize)
 164        {
 0165            isScaling = true;
 0166            scale = zoomCurve.Evaluate(currentZoomLevel);
 0167            MapRenderer.i.scaleFactor = scale;
 0168            Vector3 targetScale = new Vector3(scale, scale, scale);
 169
 0170            float counter = 0;
 171
 0172            while (counter < scaleDuration)
 173            {
 0174                counter += Time.deltaTime;
 0175                containerRectTransform.localScale = Vector3.Lerp(startScaleSize, targetScale, counter / scaleDuration);
 0176                yield return null;
 177            }
 178
 0179            isScaling = false;
 0180        }
 181
 0182        private void OnNavmapVisibleChanged(bool current, bool previous) { SetVisible(current); }
 183
 184        public void Initialize()
 185        {
 53186            toastView.gameObject.SetActive(false);
 53187            scrollRect.gameObject.SetActive(false);
 53188            DataStore.i.HUDs.isNavMapInitialized.Set(true);
 53189        }
 190
 191        private void OnDestroy()
 192        {
 53193            MapRenderer.OnParcelClicked -= TriggerToast;
 53194            MapRenderer.OnCursorFarFromParcel -= CloseToast;
 53195            CommonScriptableObjects.playerCoords.OnChange -= UpdateCurrentSceneData;
 53196            navmapVisible.OnChange -= OnNavmapVisibleChanged;
 53197            configureMapInFullscreenMenu.OnChange -= ConfigureMapInFullscreenMenuChanged;
 53198            mouseWheelAction.OnValueChanged -= OnMouseWheelChangeValue;
 53199            zoomIn.OnStarted -= OnZoomPlusMinus;
 53200            zoomOut.OnStarted -= OnZoomPlusMinus;
 53201        }
 202
 203        internal void SetVisible(bool visible)
 204        {
 1205            if (waitingForFullscreenHUDOpen)
 0206                return;
 207
 1208            if (visible)
 209            {
 1210                if (CommonScriptableObjects.isFullscreenHUDOpen.Get())
 211                {
 0212                    SetVisibility_Internal(true);
 0213                }
 214                else
 215                {
 1216                    waitingForFullscreenHUDOpen = true;
 1217                    CommonScriptableObjects.isFullscreenHUDOpen.OnChange += IsFullscreenHUDOpen_OnChange;
 218                }
 1219            }
 220            else
 221            {
 0222                SetVisibility_Internal(false);
 223            }
 0224        }
 225
 226        private void IsFullscreenHUDOpen_OnChange(bool current, bool previous)
 227        {
 0228            if (!current)
 0229                return;
 230
 0231            CommonScriptableObjects.isFullscreenHUDOpen.OnChange -= IsFullscreenHUDOpen_OnChange;
 0232            SetVisibility_Internal(true);
 0233            waitingForFullscreenHUDOpen = false;
 0234        }
 235
 236        internal void SetVisibility_Internal(bool visible)
 237        {
 0238            if (MapRenderer.i == null)
 0239                return;
 240
 0241            scrollRect.StopMovement();
 242
 0243            scrollRect.gameObject.SetActive(visible);
 0244            MapRenderer.i.parcelHighlightEnabled = visible;
 245
 0246            if (visible)
 247            {
 0248                if (!DataStore.i.exploreV2.isInitialized.Get())
 0249                    Utils.UnlockCursor();
 250
 0251                MapRenderer.i.scaleFactor = scale;
 0252                minimapViewport = MapRenderer.i.atlas.viewport;
 0253                mapRendererMinimapParent = MapRenderer.i.transform.parent;
 0254                atlasOriginalPosition = MapRenderer.i.atlas.chunksParent.transform.localPosition;
 255
 0256                MapRenderer.i.atlas.viewport = scrollRect.viewport;
 0257                MapRenderer.i.transform.SetParent(scrollRectContentTransform);
 0258                MapRenderer.i.atlas.UpdateCulling();
 259
 0260                scrollRect.content = MapRenderer.i.atlas.chunksParent.transform as RectTransform;
 261
 262                // Reparent the player icon parent to scroll everything together
 0263                MapRenderer.i.atlas.overlayLayerGameobject.transform.SetParent(scrollRect.content);
 264
 265                // Center map
 0266                MapRenderer.i.atlas.CenterToTile(Utils.WorldToGridPositionUnclamped(CommonScriptableObjects.playerWorldP
 267
 268                // Set shorter interval of time for populated scenes markers fetch
 0269                MapRenderer.i.usersPositionMarkerController?.SetUpdateMode(MapGlobalUsersPositionMarkerController.Update
 0270            }
 271            else
 272            {
 0273                if (minimapViewport == null)
 0274                    return;
 275
 0276                ResetCameraZoom();
 0277                CloseToast();
 278
 0279                MapRenderer.i.atlas.viewport = minimapViewport;
 0280                MapRenderer.i.transform.SetParent(mapRendererMinimapParent);
 0281                MapRenderer.i.atlas.chunksParent.transform.localPosition = atlasOriginalPosition;
 0282                MapRenderer.i.atlas.UpdateCulling();
 283
 284                // Restore the player icon to its original parent
 0285                MapRenderer.i.atlas.overlayLayerGameobject.transform.SetParent(MapRenderer.i.atlas.chunksParent.transfor
 0286                (MapRenderer.i.atlas.overlayLayerGameobject.transform as RectTransform).anchoredPosition = Vector2.zero;
 287
 0288                MapRenderer.i.UpdateRendering(Utils.WorldToGridPositionUnclamped(CommonScriptableObjects.playerWorldPosi
 289
 290                // Set longer interval of time for populated scenes markers fetch
 0291                MapRenderer.i.usersPositionMarkerController?.SetUpdateMode(MapGlobalUsersPositionMarkerController.Update
 292            }
 293
 0294            OnToggle?.Invoke(visible);
 0295        }
 296
 297        void UpdateCurrentSceneData(Vector2Int current, Vector2Int previous)
 298        {
 299            const string format = "{0},{1}";
 1300            currentSceneCoordsText.text = string.Format(format, current.x, current.y);
 1301            currentSceneNameText.text = MinimapMetadata.GetMetadata().GetSceneInfo(current.x, current.y)?.name ?? "Unnam
 1302        }
 303
 304        void TriggerToast(int cursorTileX, int cursorTileY)
 305        {
 0306            if(toastView.isOpen)
 0307                CloseToast();
 0308            var sceneInfo = mapMetadata.GetSceneInfo(cursorTileX, cursorTileY);
 0309            if (sceneInfo == null)
 0310                WebInterface.RequestScenesInfoAroundParcel(new Vector2(cursorTileX, cursorTileY), 15);
 311
 0312            toastView.Populate(new Vector2Int(cursorTileX, cursorTileY), sceneInfo);
 0313        }
 314
 0315        private void CloseToast() { toastView.OnCloseClick(); }
 316
 0317        public void SetExitButtonActive(bool isActive) { closeButton.gameObject.SetActive(isActive); }
 318
 319        public void SetAsFullScreenMenuMode(Transform parentTransform)
 320        {
 53321            if (parentTransform == null)
 53322                return;
 323
 0324            transform.SetParent(parentTransform);
 0325            transform.localScale = Vector3.one;
 0326            SetExitButtonActive(false);
 327
 0328            RectTransform rectTransform = transform as RectTransform;
 0329            rectTransform.anchorMin = Vector2.zero;
 0330            rectTransform.anchorMax = Vector2.one;
 0331            rectTransform.pivot = new Vector2(0.5f, 0.5f);
 0332            rectTransform.localPosition = Vector2.zero;
 0333            rectTransform.offsetMax = Vector2.zero;
 0334            rectTransform.offsetMin = Vector2.zero;
 0335        }
 336
 106337        private void ConfigureMapInFullscreenMenuChanged(Transform currentParentTransform, Transform previousParentTrans
 338    }
 339}