| | 1 | | using DCLServices.MapRendererV2.MapCameraController; |
| | 2 | | using System; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.EventSystems; |
| | 5 | | using UnityEngine.Profiling; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | namespace 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 | |
|
| 1 | 22 | | private static readonly string DRAG_SAMPLE_NAME = $"{nameof(MapRenderImage)}.{nameof(OnDrag)}"; |
| 1 | 23 | | private static readonly string POINTER_MOVE_SAMPLE_NAME = $"{nameof(MapRenderImage)}.{nameof(OnPointerMove)}"; |
| 1 | 24 | | 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 | | { |
| 1 | 45 | | dragBehavior = new MapCameraDragBehavior(rectTransform, data); |
| 1 | 46 | | } |
| | 47 | |
|
| | 48 | | public void Activate(Camera hudCamera, RenderTexture renderTexture, IMapCameraController mapCameraController) |
| | 49 | | { |
| 0 | 50 | | interactivityController = mapCameraController.GetInteractivityController(); |
| 0 | 51 | | this.highlightEnabled = interactivityController.HighlightEnabled; |
| 0 | 52 | | this.hudCamera = hudCamera; |
| | 53 | |
|
| 0 | 54 | | texture = renderTexture; |
| | 55 | |
|
| 0 | 56 | | dragBehavior?.Activate(mapCameraController); |
| | 57 | |
|
| 0 | 58 | | isActive = true; |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | public void Deactivate() |
| | 62 | | { |
| 0 | 63 | | dragBehavior?.Deactivate(); |
| | 64 | |
|
| 0 | 65 | | hudCamera = null; |
| 0 | 66 | | interactivityController = null; |
| 0 | 67 | | texture = null; |
| | 68 | |
|
| 0 | 69 | | isActive = false; |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | public void OnPointerMove(PointerEventData eventData) |
| | 73 | | { |
| 0 | 74 | | if (!isActive) |
| 0 | 75 | | return; |
| | 76 | |
|
| 0 | 77 | | if (dragging) |
| 0 | 78 | | return; |
| | 79 | |
|
| 0 | 80 | | Profiler.BeginSample(POINTER_MOVE_SAMPLE_NAME); |
| | 81 | |
|
| 0 | 82 | | ProcessHover(eventData); |
| | 83 | |
|
| 0 | 84 | | Profiler.EndSample(); |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | public void OnPointerExit(PointerEventData eventData) |
| | 88 | | { |
| 0 | 89 | | if (!isActive) |
| 0 | 90 | | return; |
| | 91 | |
|
| 0 | 92 | | if (!highlightEnabled) |
| 0 | 93 | | return; |
| | 94 | |
|
| 0 | 95 | | interactivityController.RemoveHighlight(); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | public void OnPointerClick(PointerEventData eventData) |
| | 99 | | { |
| 0 | 100 | | Profiler.BeginSample(POINTER_CLICK_SAMPLE_NAME); |
| | 101 | |
|
| 0 | 102 | | if (isActive && !dragging && TryGetParcelUnderPointer(eventData, out var parcel, out _, out _)) |
| | 103 | | { |
| 0 | 104 | | ParcelClicked?.Invoke(new ParcelClickData |
| | 105 | | { |
| | 106 | | Parcel = parcel, |
| | 107 | | WorldPosition = GetParcelWorldPosition(parcel), |
| | 108 | | }); |
| | 109 | | } |
| | 110 | |
|
| 0 | 111 | | Profiler.EndSample(); |
| 0 | 112 | | } |
| | 113 | |
|
| 0 | 114 | | private bool dragging => dragBehavior is { dragging: true }; |
| | 115 | |
|
| | 116 | | private Vector2 GetParcelWorldPosition(Vector2Int parcel) |
| | 117 | | { |
| 0 | 118 | | var normalizedDiscretePosition = interactivityController.GetNormalizedPosition(parcel); |
| 0 | 119 | | return rectTransform.TransformPoint(rectTransform.rect.size * (normalizedDiscretePosition - rectTransform.pi |
| | 120 | | } |
| | 121 | |
|
| | 122 | | private void ProcessHover(PointerEventData eventData) |
| | 123 | | { |
| 0 | 124 | | if (TryGetParcelUnderPointer(eventData, out var parcel, out _, out var worldPosition)) |
| | 125 | | { |
| 0 | 126 | | if (highlightEnabled) |
| 0 | 127 | | interactivityController.HighlightParcel(parcel); |
| | 128 | |
|
| 0 | 129 | | Hovered?.Invoke(worldPosition); |
| | 130 | | } |
| 0 | 131 | | else if (highlightEnabled) |
| 0 | 132 | | interactivityController.RemoveHighlight(); |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | private bool TryGetParcelUnderPointer(PointerEventData eventData, out Vector2Int parcel, out Vector2 localPositi |
| | 136 | | { |
| 0 | 137 | | var screenPoint = eventData.position; |
| 0 | 138 | | if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, screenPoint, hudCamera, out world |
| | 139 | | { |
| 0 | 140 | | var rectSize = rectTransform.rect.size; |
| 0 | 141 | | localPosition = (Vector2) rectTransform.InverseTransformPoint(worldPosition); |
| 0 | 142 | | var leftCornerRelativeLocalPosition = localPosition + (rectTransform.pivot * rectSize); |
| 0 | 143 | | return interactivityController.TryGetParcel(leftCornerRelativeLocalPosition / rectSize, out parcel); |
| | 144 | | } |
| 0 | 145 | | parcel = Vector2Int.zero; |
| 0 | 146 | | localPosition = Vector2.zero; |
| 0 | 147 | | return false; |
| | 148 | | } |
| | 149 | |
|
| | 150 | | public void OnDrag(PointerEventData eventData) |
| | 151 | | { |
| 0 | 152 | | if (!isActive) return; |
| | 153 | |
|
| 0 | 154 | | Profiler.BeginSample(DRAG_SAMPLE_NAME); |
| | 155 | |
|
| 0 | 156 | | dragBehavior?.OnDrag(eventData); |
| | 157 | |
|
| 0 | 158 | | Profiler.EndSample(); |
| 0 | 159 | | } |
| | 160 | |
|
| | 161 | | public void OnBeginDrag(PointerEventData eventData) |
| | 162 | | { |
| 0 | 163 | | if (!isActive) return; |
| | 164 | |
|
| 0 | 165 | | DragStarted?.Invoke(); |
| 0 | 166 | | dragBehavior?.OnBeginDrag(eventData); |
| 0 | 167 | | } |
| | 168 | |
|
| | 169 | | public void OnEndDrag(PointerEventData eventData) |
| | 170 | | { |
| 0 | 171 | | if (!isActive) return; |
| | 172 | |
|
| 0 | 173 | | dragBehavior?.OnEndDrag(eventData); |
| | 174 | |
|
| 0 | 175 | | ProcessHover(eventData); |
| 0 | 176 | | } |
| | 177 | |
|
| | 178 | | protected override void OnDestroy() |
| | 179 | | { |
| 1 | 180 | | base.OnDestroy(); |
| | 181 | |
|
| 1 | 182 | | dragBehavior?.Dispose(); |
| 1 | 183 | | } |
| | 184 | | } |
| | 185 | | } |