| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCLServices.MapRendererV2.MapCameraController; |
| | 3 | | using System; |
| | 4 | | using System.Threading; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.EventSystems; |
| | 7 | |
|
| | 8 | | namespace DCLServices.MapRendererV2.ConsumerUtils |
| | 9 | | { |
| | 10 | | public class MapCameraDragBehavior : IDisposable |
| | 11 | | { |
| | 12 | | [Serializable] |
| | 13 | | public class MapCameraDragBehaviorData |
| | 14 | | { |
| | 15 | | [SerializeField] internal bool inertia; |
| | 16 | | [SerializeField] internal float decelerationRate; |
| | 17 | | } |
| | 18 | |
|
| 1 | 19 | | private static Vector3[] worldCorners = new Vector3[4]; |
| | 20 | |
|
| 0 | 21 | | internal bool dragging { get; private set; } |
| | 22 | |
|
| | 23 | | private readonly RectTransform rectTransform; |
| | 24 | | private readonly MapCameraDragBehaviorData data; |
| | 25 | |
|
| | 26 | | private IMapCameraController mapCameraController; |
| | 27 | |
|
| | 28 | | private Vector2 pointerPositionOnDragBegin; |
| | 29 | | private Vector2 cameraPositionOnDragBegin; |
| | 30 | |
|
| | 31 | | private float screenSpaceToLocalCameraPositionRatio; |
| | 32 | |
|
| | 33 | | private CancellationTokenSource inertiaLoopCTS; |
| | 34 | |
|
| 1 | 35 | | public MapCameraDragBehavior(RectTransform rectTransform, MapCameraDragBehaviorData data) |
| | 36 | | { |
| 1 | 37 | | this.rectTransform = rectTransform; |
| 1 | 38 | | this.data = data; |
| 1 | 39 | | } |
| | 40 | |
|
| | 41 | | public void Activate(IMapCameraController cameraController) |
| | 42 | | { |
| 0 | 43 | | mapCameraController = cameraController; |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | public void Deactivate() |
| | 47 | | { |
| 0 | 48 | | mapCameraController = null; |
| 0 | 49 | | dragging = false; |
| | 50 | |
|
| 0 | 51 | | StopInertia(); |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | public void OnBeginDrag(PointerEventData eventData) |
| | 55 | | { |
| 0 | 56 | | if (eventData.button != PointerEventData.InputButton.Left) |
| 0 | 57 | | return; |
| | 58 | |
|
| 0 | 59 | | rectTransform.GetWorldCorners(worldCorners); |
| | 60 | |
|
| 0 | 61 | | Vector2 bottomLeft = RectTransformUtility.WorldToScreenPoint(eventData.pressEventCamera, worldCorners[0]); |
| 0 | 62 | | Vector2 topRight = RectTransformUtility.WorldToScreenPoint(eventData.pressEventCamera, worldCorners[2]); |
| | 63 | |
|
| 0 | 64 | | screenSpaceToLocalCameraPositionRatio = mapCameraController.GetVerticalSizeInLocalUnits() / (topRight - bott |
| | 65 | |
|
| 0 | 66 | | pointerPositionOnDragBegin = eventData.position; |
| 0 | 67 | | cameraPositionOnDragBegin = mapCameraController.LocalPosition; |
| | 68 | |
|
| 0 | 69 | | dragging = true; |
| | 70 | |
|
| 0 | 71 | | if (data.inertia) |
| | 72 | | { |
| 0 | 73 | | StopInertia(); |
| 0 | 74 | | inertiaLoopCTS = new CancellationTokenSource(); |
| 0 | 75 | | InertiaLoop(inertiaLoopCTS.Token).Forget(); |
| | 76 | | } |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | public void OnDrag(PointerEventData eventData) |
| | 80 | | { |
| 0 | 81 | | if (eventData.button != PointerEventData.InputButton.Left) |
| 0 | 82 | | return; |
| | 83 | |
|
| 0 | 84 | | var pointerPosition = eventData.position; |
| | 85 | |
|
| 0 | 86 | | var pointerDelta = pointerPosition - pointerPositionOnDragBegin; |
| 0 | 87 | | var targetCameraLocalPosition = cameraPositionOnDragBegin - (pointerDelta * screenSpaceToLocalCameraPosition |
| | 88 | |
|
| 0 | 89 | | mapCameraController.SetLocalPosition(targetCameraLocalPosition); |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | public void OnEndDrag(PointerEventData eventData) |
| | 93 | | { |
| 0 | 94 | | if (eventData.button != PointerEventData.InputButton.Left) |
| 0 | 95 | | return; |
| | 96 | |
|
| 0 | 97 | | dragging = false; |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | private void StopInertia() |
| | 101 | | { |
| 1 | 102 | | inertiaLoopCTS?.Cancel(); |
| 1 | 103 | | inertiaLoopCTS?.Dispose(); |
| 1 | 104 | | inertiaLoopCTS = null; |
| 1 | 105 | | } |
| | 106 | |
|
| | 107 | | private async UniTaskVoid InertiaLoop(CancellationToken ct) |
| | 108 | | { |
| 0 | 109 | | var prevPosition = mapCameraController.LocalPosition; |
| 0 | 110 | | var inertialVelocity = Vector2.zero; |
| | 111 | |
|
| 0 | 112 | | while (true) |
| | 113 | | { |
| 0 | 114 | | await UniTask.NextFrame(PlayerLoopTiming.PostLateUpdate); |
| 0 | 115 | | if (ct.IsCancellationRequested) break; |
| 0 | 116 | | float unscaledDeltaTime = Time.unscaledDeltaTime; |
| 0 | 117 | | var cameraLocalPos = mapCameraController.LocalPosition; |
| | 118 | |
|
| 0 | 119 | | if (dragging) |
| | 120 | | { |
| 0 | 121 | | var newVelocity = (cameraLocalPos - prevPosition) / unscaledDeltaTime; |
| 0 | 122 | | inertialVelocity = Vector2.Lerp(inertialVelocity, newVelocity, unscaledDeltaTime * 10); |
| | 123 | | } |
| | 124 | | else |
| | 125 | | { |
| 0 | 126 | | for (var axis = 0; axis < 2; axis++) |
| | 127 | | { |
| 0 | 128 | | inertialVelocity[axis] *= Mathf.Pow(this.data.decelerationRate, unscaledDeltaTime); |
| | 129 | |
|
| 0 | 130 | | if (Mathf.Abs(inertialVelocity[axis]) < 1) |
| 0 | 131 | | inertialVelocity[axis] = 0; |
| | 132 | | } |
| | 133 | |
|
| 0 | 134 | | mapCameraController.SetLocalPosition(cameraLocalPos + (inertialVelocity * unscaledDeltaTime)); |
| | 135 | |
|
| 0 | 136 | | if (Mathf.Approximately(inertialVelocity.x, 0) && Mathf.Approximately(inertialVelocity.y, 0)) |
| | 137 | | break; |
| | 138 | | } |
| | 139 | |
|
| 0 | 140 | | prevPosition = cameraLocalPos; |
| | 141 | | } |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | | public void Dispose() |
| | 145 | | { |
| 1 | 146 | | StopInertia(); |
| 1 | 147 | | } |
| | 148 | | } |
| | 149 | | } |