< Summary

Class:DCLServices.MapRendererV2.ConsumerUtils.MapRenderImage
Assembly:MapRendererConsumerUtils
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/MapRendererV2/ConsumerUtils/MapRenderImage.cs
Covered lines:8
Uncovered lines:64
Coverable lines:72
Total lines:185
Line coverage:11.1% (8 of 72)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:15
Method coverage:20% (3 of 15)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MapRenderImage()0%110100%
EmbedMapCameraDragBehavior(...)0%110100%
Activate(...)0%6200%
Deactivate()0%6200%
OnPointerMove(...)0%12300%
OnPointerExit(...)0%12300%
OnPointerClick(...)0%30500%
GetParcelWorldPosition(...)0%2100%
ProcessHover(...)0%30500%
TryGetParcelUnderPointer(...)0%6200%
OnDrag(...)0%12300%
OnBeginDrag(...)0%20400%
OnEndDrag(...)0%12300%
OnDestroy()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/MapRendererV2/ConsumerUtils/MapRenderImage.cs

#LineLine coverage
 1using DCLServices.MapRendererV2.MapCameraController;
 2using System;
 3using UnityEngine;
 4using UnityEngine.EventSystems;
 5using UnityEngine.Profiling;
 6using UnityEngine.UI;
 7
 8namespace DCLServices.MapRendererV2.ConsumerUtils
 9{
 10    /// <summary>
 11    /// Extends <see cref="RawImage"/> to provide interactivity functionality
 12    /// </summary>
 13    public class MapRenderImage : RawImage, IPointerMoveHandler, IPointerExitHandler, IPointerClickHandler,
 14        IDragHandler, IBeginDragHandler, IEndDragHandler
 15    {
 16        public struct ParcelClickData
 17        {
 18            public Vector2Int Parcel;
 19            public Vector2 WorldPosition;
 20        }
 21
 122        private static readonly string DRAG_SAMPLE_NAME = $"{nameof(MapRenderImage)}.{nameof(OnDrag)}";
 123        private static readonly string POINTER_MOVE_SAMPLE_NAME = $"{nameof(MapRenderImage)}.{nameof(OnPointerMove)}";
 124        private static readonly string POINTER_CLICK_SAMPLE_NAME = $"{nameof(MapRenderImage)}.{nameof(OnPointerClick)}";
 25
 26        public event Action<ParcelClickData> ParcelClicked;
 27
 28        /// <summary>
 29        /// Notifies with the world position
 30        /// </summary>
 31        public event Action<Vector2> Hovered;
 32
 33        public event Action DragStarted;
 34
 35        private MapCameraDragBehavior dragBehavior;
 36
 37        private bool highlightEnabled;
 38        private IMapInteractivityController interactivityController;
 39        private Camera hudCamera;
 40
 41        private bool isActive;
 42
 43        public void EmbedMapCameraDragBehavior(MapCameraDragBehavior.MapCameraDragBehaviorData data)
 44        {
 145            dragBehavior = new MapCameraDragBehavior(rectTransform, data);
 146        }
 47
 48        public void Activate(Camera hudCamera, RenderTexture renderTexture, IMapCameraController mapCameraController)
 49        {
 050            interactivityController = mapCameraController.GetInteractivityController();
 051            this.highlightEnabled = interactivityController.HighlightEnabled;
 052            this.hudCamera = hudCamera;
 53
 054            texture = renderTexture;
 55
 056            dragBehavior?.Activate(mapCameraController);
 57
 058            isActive = true;
 059        }
 60
 61        public void Deactivate()
 62        {
 063            dragBehavior?.Deactivate();
 64
 065            hudCamera = null;
 066            interactivityController = null;
 067            texture = null;
 68
 069            isActive = false;
 070        }
 71
 72        public void OnPointerMove(PointerEventData eventData)
 73        {
 074            if (!isActive)
 075                return;
 76
 077            if (dragging)
 078                return;
 79
 080            Profiler.BeginSample(POINTER_MOVE_SAMPLE_NAME);
 81
 082            ProcessHover(eventData);
 83
 084            Profiler.EndSample();
 085        }
 86
 87        public void OnPointerExit(PointerEventData eventData)
 88        {
 089            if (!isActive)
 090                return;
 91
 092            if (!highlightEnabled)
 093                return;
 94
 095            interactivityController.RemoveHighlight();
 096        }
 97
 98        public void OnPointerClick(PointerEventData eventData)
 99        {
 0100            Profiler.BeginSample(POINTER_CLICK_SAMPLE_NAME);
 101
 0102            if (isActive && !dragging && TryGetParcelUnderPointer(eventData, out var parcel, out _, out _))
 103            {
 0104                ParcelClicked?.Invoke(new ParcelClickData
 105                {
 106                    Parcel = parcel,
 107                    WorldPosition = GetParcelWorldPosition(parcel),
 108                });
 109            }
 110
 0111            Profiler.EndSample();
 0112        }
 113
 0114        private bool dragging => dragBehavior is { dragging: true };
 115
 116        private Vector2 GetParcelWorldPosition(Vector2Int parcel)
 117        {
 0118            var normalizedDiscretePosition = interactivityController.GetNormalizedPosition(parcel);
 0119            return rectTransform.TransformPoint(rectTransform.rect.size * (normalizedDiscretePosition - rectTransform.pi
 120        }
 121
 122        private void ProcessHover(PointerEventData eventData)
 123        {
 0124            if (TryGetParcelUnderPointer(eventData, out var parcel, out _, out var worldPosition))
 125            {
 0126                if (highlightEnabled)
 0127                    interactivityController.HighlightParcel(parcel);
 128
 0129                Hovered?.Invoke(worldPosition);
 130            }
 0131            else if (highlightEnabled)
 0132                interactivityController.RemoveHighlight();
 0133        }
 134
 135        private bool TryGetParcelUnderPointer(PointerEventData eventData, out Vector2Int parcel, out Vector2 localPositi
 136        {
 0137            var screenPoint = eventData.position;
 0138            if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, screenPoint, hudCamera, out world
 139            {
 0140                var rectSize = rectTransform.rect.size;
 0141                localPosition = (Vector2) rectTransform.InverseTransformPoint(worldPosition);
 0142                var leftCornerRelativeLocalPosition = localPosition + (rectTransform.pivot * rectSize);
 0143                return interactivityController.TryGetParcel(leftCornerRelativeLocalPosition / rectSize, out parcel);
 144            }
 0145            parcel = Vector2Int.zero;
 0146            localPosition = Vector2.zero;
 0147            return false;
 148        }
 149
 150        public void OnDrag(PointerEventData eventData)
 151        {
 0152            if (!isActive) return;
 153
 0154            Profiler.BeginSample(DRAG_SAMPLE_NAME);
 155
 0156            dragBehavior?.OnDrag(eventData);
 157
 0158            Profiler.EndSample();
 0159        }
 160
 161        public void OnBeginDrag(PointerEventData eventData)
 162        {
 0163            if (!isActive) return;
 164
 0165            DragStarted?.Invoke();
 0166            dragBehavior?.OnBeginDrag(eventData);
 0167        }
 168
 169        public void OnEndDrag(PointerEventData eventData)
 170        {
 0171            if (!isActive) return;
 172
 0173            dragBehavior?.OnEndDrag(eventData);
 174
 0175            ProcessHover(eventData);
 0176        }
 177
 178        protected override void OnDestroy()
 179        {
 1180            base.OnDestroy();
 181
 1182            dragBehavior?.Dispose();
 1183        }
 184    }
 185}