< Summary

Class:DCLServices.MapRendererV2.MapCameraController.MapCameraController
Assembly:MapRendererV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/MapRendererV2/MapCameraController/MapCameraController.cs
Covered lines:0
Uncovered lines:120
Coverable lines:120
Total lines:271
Line coverage:0% (0 of 120)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:28
Method coverage:0% (0 of 28)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MapCameraController(...)0%2100%
Initialize(...)0%2100%
ResizeTexture(...)0%12300%
ClampTextureResolution(...)0%2100%
GetVerticalSizeInLocalUnits()0%2100%
GetRenderTexture()0%6200%
GetInteractivityController()0%2100%
SetZoom(...)0%2100%
SetPosition(...)0%2100%
SetLocalPosition(...)0%2100%
SetLocalPositionClamped(...)0%2100%
SetPositionAndZoom(...)0%2100%
TranslateTo(...)0%2100%
SetCameraSize(...)0%12300%
ClampLocalPosition(...)0%2100%
CalculateCameraPositionBounds()0%12300%
SuspendRendering()0%2100%
ResumeRendering()0%2100%
SetActive(...)0%2100%
GetCameraRect()0%2100%
Release(...)0%12300%
Dispose()0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/MapRendererV2/MapCameraController/MapCameraController.cs

#LineLine coverage
 1using DCL;
 2using DCLServices.MapRendererV2.CommonBehavior;
 3using DCLServices.MapRendererV2.CoordsUtils;
 4using DCLServices.MapRendererV2.Culling;
 5using DCLServices.MapRendererV2.MapLayers;
 6using DG.Tweening;
 7using System;
 8using UnityEngine;
 9using Utils = DCL.Helpers.Utils;
 10
 11namespace DCLServices.MapRendererV2.MapCameraController
 12{
 13    internal partial class MapCameraController : IMapCameraControllerInternal
 14    {
 15        private const float CAMERA_HEIGHT = 0;
 16
 17        private const int MAX_TEXTURE_SIZE = 4096;
 18
 19        public event Action<IMapActivityOwner, IMapCameraControllerInternal> OnReleasing;
 20        public event Action<float, float> ZoomChanged;
 21
 022        public MapLayer EnabledLayers { get; private set; }
 23
 024        public Camera Camera => mapCameraObject.mapCamera;
 25
 026        public float Zoom => Mathf.InverseLerp(zoomValues.y, zoomValues.x, mapCameraObject.mapCamera.orthographicSize);
 27
 028        public Vector2 LocalPosition => mapCameraObject.mapCamera.transform.localPosition;
 29
 030        public Vector2 CoordsPosition => coordsUtils.PositionToCoordsUnclamped(LocalPosition);
 31
 32        private readonly IMapInteractivityControllerInternal interactivityBehavior;
 33        private readonly ICoordsUtils coordsUtils;
 34        private readonly IMapCullingController cullingController;
 35        private readonly MapCameraObject mapCameraObject;
 36
 37        private RenderTexture renderTexture;
 38
 39        // Zoom Thresholds in Parcels
 40        private Vector2Int zoomValues;
 41
 42        private Rect cameraPositionBounds;
 43        private Sequence translationSequence;
 44
 045        public MapCameraController(
 46            IMapInteractivityControllerInternal interactivityBehavior,
 47            MapCameraObject mapCameraObject,
 48            ICoordsUtils coordsUtils,
 49            IMapCullingController cullingController
 50        )
 51        {
 052            this.interactivityBehavior = interactivityBehavior;
 053            this.coordsUtils = coordsUtils;
 054            this.mapCameraObject = mapCameraObject;
 055            this.cullingController = cullingController;
 56
 057            mapCameraObject.transform.localPosition = Vector3.up * CAMERA_HEIGHT;
 058            mapCameraObject.mapCamera.orthographic = true;
 059        }
 60
 61        void IMapCameraControllerInternal.Initialize(Vector2Int textureResolution, Vector2Int zoomValues, MapLayer layer
 62        {
 063            textureResolution = ClampTextureResolution(textureResolution);
 064            renderTexture = new RenderTexture(textureResolution.x, textureResolution.y, 0, RenderTextureFormat.Default, 
 65            // Bilinear and Trilinear make texture blurry
 066            renderTexture.filterMode = FilterMode.Point;
 067            renderTexture.autoGenerateMips = false;
 068            renderTexture.useMipMap = false;
 69
 070            this.zoomValues = zoomValues * coordsUtils.ParcelSize;
 71
 072            EnabledLayers = layers;
 73
 074            mapCameraObject.mapCamera.targetTexture = renderTexture;
 75
 076            cullingController.OnCameraAdded(this);
 77
 078            interactivityBehavior.Initialize(layers);
 079        }
 80
 81        public void ResizeTexture(Vector2Int textureResolution)
 82        {
 083            if (!Camera) return;
 84
 085            if (renderTexture.IsCreated())
 086                renderTexture.Release();
 87
 088            textureResolution = ClampTextureResolution(textureResolution);
 089            renderTexture.width = textureResolution.x;
 090            renderTexture.height = textureResolution.y;
 091            renderTexture.Create();
 92
 093            Camera.ResetAspect();
 94
 095            SetLocalPosition(mapCameraObject.transform.localPosition);
 096        }
 97
 98        private Vector2Int ClampTextureResolution(Vector2Int desiredRes)
 99        {
 0100            float factor = Mathf.Min(1, MAX_TEXTURE_SIZE / (float) Mathf.Max(desiredRes.x, desiredRes.y));
 0101            return Vector2Int.FloorToInt((Vector2) desiredRes * factor);
 102        }
 103
 104        public float GetVerticalSizeInLocalUnits() =>
 0105            mapCameraObject.mapCamera.orthographicSize * 2;
 106
 107        public RenderTexture GetRenderTexture()
 108        {
 0109            if (renderTexture == null)
 0110                throw new Exception("Trying to get RenderTexture from a not initialized MapCameraController");
 111
 0112            return renderTexture;
 113        }
 114
 115        public IMapInteractivityController GetInteractivityController() =>
 0116            interactivityBehavior;
 117
 118        public void SetZoom(float value)
 119        {
 0120            SetCameraSize(value);
 121            // Clamp local position as boundaries are dependent on zoom
 0122            SetLocalPositionClamped(mapCameraObject.transform.localPosition);
 0123            cullingController.SetCameraDirty(this);
 0124        }
 125
 126        public void SetPosition(Vector2 coordinates)
 127        {
 0128            translationSequence.Kill();
 0129            translationSequence = null;
 130
 0131            Vector3 position = coordsUtils.CoordsToPositionUnclamped(coordinates);
 0132            mapCameraObject.transform.localPosition = ClampLocalPosition(new Vector3(position.x, position.y, CAMERA_HEIG
 0133            cullingController.SetCameraDirty(this);
 0134        }
 135
 136        public void SetLocalPosition(Vector2 localCameraPosition)
 137        {
 0138            SetLocalPositionClamped(localCameraPosition);
 0139            cullingController.SetCameraDirty(this);
 0140        }
 141
 142        private void SetLocalPositionClamped(Vector2 localCameraPosition)
 143        {
 0144            translationSequence.Kill();
 0145            translationSequence = null;
 146
 0147            mapCameraObject.transform.localPosition = ClampLocalPosition(localCameraPosition);
 0148        }
 149
 150        public void SetPositionAndZoom(Vector2 coordinates, float zoom)
 151        {
 0152            translationSequence.Kill();
 0153            translationSequence = null;
 154
 0155            SetCameraSize(zoom);
 156
 0157            Vector3 position = coordsUtils.CoordsToPositionUnclamped(coordinates);
 0158            mapCameraObject.transform.localPosition = ClampLocalPosition(new Vector3(position.x, position.y, CAMERA_HEIG
 0159            cullingController.SetCameraDirty(this);
 0160        }
 161
 162        public void TranslateTo(Vector2 coordinates, float duration, Action onComplete = null)
 163        {
 0164            translationSequence = DOTween.Sequence();
 165
 0166            Vector3 position = coordsUtils.CoordsToPositionUnclamped(coordinates);
 0167            Vector3 targetPosition = ClampLocalPosition(new Vector3(position.x, position.y, CAMERA_HEIGHT));
 168
 0169            translationSequence.Join(mapCameraObject.transform.DOLocalMove(targetPosition, duration).SetEase(Ease.OutQua
 170                               .OnComplete(() =>
 171                                {
 0172                                    CalculateCameraPositionBounds();
 0173                                    cullingController.SetCameraDirty(this);
 0174                                    onComplete?.Invoke();
 0175                                });
 0176        }
 177
 178        private void SetCameraSize(float zoom)
 179        {
 0180            zoom = Mathf.Clamp01(zoom);
 0181            mapCameraObject.mapCamera.orthographicSize = Mathf.Lerp(zoomValues.y, zoomValues.x, zoom);
 182
 0183            if (DataStore.i.featureFlags.flags.Get().IsFeatureEnabled("map_focus_home_or_user"))
 184            {
 0185                interactivityBehavior.ApplyCameraZoom(zoomValues.x, mapCameraObject.mapCamera.orthographicSize);
 0186                ZoomChanged?.Invoke(zoomValues.x, mapCameraObject.mapCamera.orthographicSize);
 187            }
 188
 0189            CalculateCameraPositionBounds();
 0190        }
 191
 192        private Vector3 ClampLocalPosition(Vector3 localPos)
 193        {
 0194            localPos.x = Mathf.Clamp(localPos.x, cameraPositionBounds.xMin, cameraPositionBounds.xMax);
 0195            localPos.y = Mathf.Clamp(localPos.y, cameraPositionBounds.yMin, cameraPositionBounds.yMax);
 196
 0197            return localPos;
 198        }
 199
 200        private void CalculateCameraPositionBounds()
 201        {
 0202            var worldBounds = coordsUtils.VisibleWorldBounds;
 203
 0204            var cameraYSize = mapCameraObject.mapCamera.orthographicSize;
 0205            var cameraXSize = cameraYSize * mapCameraObject.mapCamera.aspect;
 206
 0207            float xMin = worldBounds.xMin + cameraXSize;
 0208            float xMax = worldBounds.xMax - cameraXSize;
 209
 0210            float yMin = worldBounds.yMin + cameraYSize;
 0211            float yMax = worldBounds.yMax - cameraYSize;
 212
 213            // If the map's width is smaller than the camera's width, disable X-drag
 0214            if (worldBounds.xMax - worldBounds.xMin < 2 * cameraXSize)
 0215                xMin = xMax = 0;
 216
 217            // If the map's height is smaller than the camera's height, disable Y-drag
 0218            if (worldBounds.yMax - worldBounds.yMin < 2 * cameraYSize)
 0219                yMin = yMax = 0;
 220
 0221            cameraPositionBounds = Rect.MinMaxRect(xMin, yMin, xMax, yMax);
 0222        }
 223
 224        public void SuspendRendering()
 225        {
 0226            mapCameraObject.mapCamera.enabled = false;
 0227        }
 228
 229        public void ResumeRendering()
 230        {
 0231            mapCameraObject.mapCamera.enabled = true;
 0232        }
 233
 234        public void SetActive(bool active)
 235        {
 0236            mapCameraObject.gameObject.SetActive(active);
 0237        }
 238
 239        public Rect GetCameraRect()
 240        {
 0241            var cameraYSize = mapCameraObject.mapCamera.orthographicSize;
 0242            var cameraXSize = cameraYSize * mapCameraObject.mapCamera.aspect;
 243
 0244            var size = new Vector2(cameraXSize * 2f, cameraYSize * 2f);
 245
 0246            return new Rect((Vector2) mapCameraObject.transform.localPosition - new Vector2(cameraXSize, cameraYSize), s
 247        }
 248
 249        public void Release(IMapActivityOwner owner)
 250        {
 0251            cullingController.OnCameraRemoved(this);
 0252            renderTexture?.Release();
 0253            interactivityBehavior.Release();
 0254            OnReleasing?.Invoke(owner, this);
 0255        }
 256
 257        public void Dispose()
 258        {
 0259            translationSequence.Kill();
 0260            translationSequence = null;
 261
 0262            if (mapCameraObject != null)
 0263                Utils.SafeDestroy(mapCameraObject.gameObject);
 264
 0265            interactivityBehavior.Dispose();
 266
 0267            renderTexture?.Release();
 0268            renderTexture = null;
 0269        }
 270    }
 271}