< Summary

Class:DCL.Builder.PublishMapView
Assembly:BuilderPublisher
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Publisher/ProjectPublishHUD/Scripts/Projects/PublishMapView.cs
Covered lines:0
Uncovered lines:107
Coverable lines:107
Total lines:228
Line coverage:0% (0 of 107)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PublishMapView()0%2100%
Start()0%2100%
OnDestroy()0%2100%
BeginDrag()0%2100%
EndDrag()0%6200%
MoreZoom()0%2100%
LessZoom()0%2100%
ApplyCurrentZoom()0%2100%
UpdateOwnedLands()0%72800%
SetProjectSize(...)0%2100%
GoToCoords(...)0%2100%
SetVisible(...)0%20400%
SetAvailabilityToPublish(...)0%12300%
ParcelHovered(...)0%12300%
SetMapRendererInContainer()0%2100%
RemoveMapRendererFromContainer()0%2100%
SelectLandInMap(...)0%2100%
ParcelSelect(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Publisher/ProjectPublishHUD/Scripts/Projects/PublishMapView.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using DCL.Helpers;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8namespace DCL.Builder
 9{
 10    public class PublishMapView : MonoBehaviour
 11    {
 12        private const float MAX_ZOOM = 4f;
 13        private const float MIN_ZOOM = 1.0f;
 14        private const float SIZE_PER_ZOOM = 0.5f;
 15
 16        public event Action<Vector2Int> OnParcelClicked;
 17        public event Action<Vector2Int> OnParcelHover;
 18
 19        [Header("References")]
 20        [SerializeField] internal ScrollRect scrollRect;
 21        [SerializeField] RectTransform scrollRectContentTransform;
 22        [SerializeField] internal Button lessZoomBtn;
 23        [SerializeField] internal Button moreZoomBtn;
 24
 25        private RectTransform minimapViewport;
 26        private RectTransform viewRectTransform;
 27        private Transform mapRendererMinimapParent;
 28        private Vector3 atlasOriginalPosition;
 29        private Vector2 initialContentPosition;
 30        private Vector2Int sceneSize;
 31
 32        private bool isVisible = false;
 33        private bool isDragging = false;
 34        private float lastScale = 0;
 035        private float currentZoomScale = 2f;
 36
 37        private void Start()
 38        {
 039            lessZoomBtn.onClick.AddListener(LessZoom);
 040            moreZoomBtn.onClick.AddListener(MoreZoom);
 41
 042            viewRectTransform = GetComponent<RectTransform>();
 043            scrollRect.onValueChanged.AddListener((x) =>
 44            {
 045                if (isVisible)
 046                    MapRenderer.i.atlas.UpdateCulling();
 047            });
 48
 049            MapRenderer.OnParcelClicked += ParcelSelect;
 050        }
 51
 52        private void OnDestroy()
 53        {
 054            moreZoomBtn.onClick.RemoveAllListeners();
 055            lessZoomBtn.onClick.RemoveAllListeners();
 056            MapRenderer.OnParcelClicked -= ParcelSelect;
 057        }
 58
 59        // Note: this event is handled by an event trigger in the same gameobject as the scrollrect
 60        public void BeginDrag()
 61        {
 062            isDragging = true;
 063            MapRenderer.i.SetParcelHighlightActive(false);
 064        }
 65
 66        // Note: this event is handled by an event trigger in the same gameobject as the scrollrect
 67        public void EndDrag()
 68        {
 069            isDragging = false;
 070            if(RectTransformUtility.RectangleContainsScreenPoint(viewRectTransform, Input.mousePosition))
 071                MapRenderer.i.SetParcelHighlightActive(true);
 072        }
 73
 74        internal void MoreZoom()
 75        {
 076            currentZoomScale = Mathf.Clamp(currentZoomScale + SIZE_PER_ZOOM, MIN_ZOOM, MAX_ZOOM);
 077            ApplyCurrentZoom();
 078        }
 79
 80        internal void LessZoom()
 81        {
 082            currentZoomScale = Mathf.Clamp(currentZoomScale - SIZE_PER_ZOOM, MIN_ZOOM, MAX_ZOOM);
 083            ApplyCurrentZoom();
 084        }
 85
 86        internal void ApplyCurrentZoom()
 87        {
 088            MapRenderer.i.transform.localScale = Vector3.one * currentZoomScale;
 089        }
 90
 91        internal void UpdateOwnedLands()
 92        {
 093            List<Vector2Int> landsToHighlight = new List<Vector2Int>();
 094            List<Vector2Int> landsToHighlightWithContent = new List<Vector2Int>();
 095            foreach (var land in DataStore.i.builderInWorld.landsWithAccess.Get())
 96            {
 097                foreach (Vector2Int landParcel in land.parcels)
 98                {
 099                    bool found = false;
 0100                    foreach (Scene scene in land.scenes)
 101                    {
 0102                        if(scene.isEmpty)
 103                            continue;
 104
 0105                        foreach (Vector2Int sceneParcel in scene.parcels)
 106                        {
 0107                            if (sceneParcel == landParcel)
 108                            {
 0109                                found = true;
 0110                                break;
 111                            }
 112                        }
 113                    }
 0114                    if(found)
 0115                        landsToHighlightWithContent.Add(landParcel);
 116                    else
 0117                        landsToHighlight.Add(landParcel);
 118                }
 119            }
 0120            MapRenderer.i.HighlightLands(landsToHighlight, landsToHighlightWithContent);
 0121        }
 122
 123        public void SetProjectSize(Vector2Int[] parcels)
 124        {
 0125            sceneSize = BIWUtils.GetSceneSize(parcels);
 0126            MapRenderer.i.SetHighlighSize(sceneSize);
 0127        }
 128
 129        public void GoToCoords(Vector2Int coords)
 130        {
 131            //Reset scroll
 0132            scrollRect.content.anchoredPosition = initialContentPosition;
 0133            MapRenderer.i.atlas.CenterToTile(coords);
 0134        }
 135
 136        internal void SetVisible(bool visible)
 137        {
 0138            if (MapRenderer.i == null || isVisible == visible)
 0139                return;
 140
 0141            isVisible = visible;
 142
 0143            scrollRect.StopMovement();
 0144            scrollRect.gameObject.SetActive(visible);
 0145            MapRenderer.i.parcelHighlightEnabled = visible;
 146
 0147            if (visible)
 0148                SetMapRendererInContainer();
 149            else
 0150                RemoveMapRendererFromContainer();
 0151        }
 152
 153        public void SetAvailabilityToPublish(bool isAvailable)
 154        {
 0155            var style = isAvailable ? MapParcelHighlight.HighlighStyle.BUILDER_ENABLE : MapParcelHighlight.HighlighStyle
 0156            MapRenderer.i.SetHighlightStyle(style);
 0157        }
 158
 159        private void ParcelHovered(float x, float y)
 160        {
 0161            if(!isDragging)
 0162                MapRenderer.i.SetParcelHighlightActive(true);
 0163            OnParcelHover?.Invoke( new Vector2Int(Mathf.RoundToInt(x), Mathf.RoundToInt(y)));
 0164        }
 165
 166        private void SetMapRendererInContainer()
 167        {
 0168            minimapViewport = MapRenderer.i.atlas.viewport;
 0169            mapRendererMinimapParent = MapRenderer.i.transform.parent;
 0170            atlasOriginalPosition = MapRenderer.i.atlas.chunksParent.transform.localPosition;
 171
 0172            lastScale = MapRenderer.i.transform.localScale.x;
 173
 0174            MapRenderer.i.SetHighlightStyle(MapParcelHighlight.HighlighStyle.BUILDER_DISABLE);
 0175            MapRenderer.i.atlas.viewport = scrollRect.viewport;
 0176            MapRenderer.i.transform.SetParent(scrollRectContentTransform);
 0177            MapRenderer.i.atlas.UpdateCulling();
 0178            MapRenderer.i.OnMovedParcelCursor += ParcelHovered;
 0179            MapRenderer.i.SetPointOfInterestActive(false);
 0180            MapRenderer.i.SetPlayerIconActive(false);
 0181            MapRenderer.i.SetOtherPlayersIconActive(false);
 182
 0183            currentZoomScale = 2f;
 0184            ApplyCurrentZoom();
 185
 0186            scrollRect.content = MapRenderer.i.atlas.chunksParent.transform as RectTransform;
 0187            initialContentPosition = scrollRect.content.anchoredPosition;
 188
 189            // Reparent the player icon parent to scroll everything together
 0190            MapRenderer.i.atlas.overlayLayerGameobject.transform.SetParent(scrollRect.content);
 191
 0192            UpdateOwnedLands();
 0193        }
 194
 195        private void RemoveMapRendererFromContainer()
 196        {
 0197            MapRenderer.i.CleanLandsHighlights();
 0198            MapRenderer.i.ClearLandHighlightsInfo();
 0199            MapRenderer.i.SetHighlightStyle(MapParcelHighlight.HighlighStyle.DEFAULT);
 0200            MapRenderer.i.atlas.viewport = minimapViewport;
 0201            MapRenderer.i.transform.SetParent(mapRendererMinimapParent);
 0202            MapRenderer.i.OnMovedParcelCursor -= ParcelHovered;
 0203            MapRenderer.i.atlas.chunksParent.transform.localPosition = atlasOriginalPosition;
 0204            MapRenderer.i.atlas.UpdateCulling();
 0205            MapRenderer.i.SetPointOfInterestActive(true);
 0206            MapRenderer.i.SetPlayerIconActive(true);
 0207            MapRenderer.i.SetOtherPlayersIconActive(true);
 0208            MapRenderer.i.transform.localScale = Vector3.one * lastScale;
 209
 210            // Restore the player icon to its original parent
 0211            MapRenderer.i.atlas.overlayLayerGameobject.transform.SetParent(MapRenderer.i.atlas.chunksParent.transform.pa
 0212            (MapRenderer.i.atlas.overlayLayerGameobject.transform as RectTransform).anchoredPosition = Vector2.zero;
 213
 0214            MapRenderer.i.UpdateRendering(Utils.WorldToGridPositionUnclamped(DataStore.i.player.playerWorldPosition.Get(
 215
 0216        }
 217
 218        public void SelectLandInMap(Vector2Int coord)
 219        {
 0220            MapRenderer.i.SelectLand(coord, sceneSize);
 0221        }
 222
 223        private void ParcelSelect(int cursorTileX, int cursorTileY)
 224        {
 0225            OnParcelClicked?.Invoke(new Vector2Int(cursorTileX, cursorTileY));
 0226        }
 227    }
 228}