| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Tasks; |
| | 3 | | using System; |
| | 4 | | using System.Threading; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.EventSystems; |
| | 7 | | using Vector3 = UnityEngine.Vector3; |
| | 8 | |
|
| | 9 | | namespace MainScripts.DCL.Controllers.HUD.CharacterPreview |
| | 10 | | { |
| | 11 | | public class PreviewCameraPanningController : IPreviewCameraPanningController |
| | 12 | | { |
| | 13 | | public event Action<Vector3> OnPanning; |
| | 14 | |
|
| | 15 | | private InputAction_Hold secondClickAction; |
| | 16 | | private InputAction_Hold middleClickAction; |
| | 17 | | private float panSpeed; |
| | 18 | | private bool allowVerticalPanning; |
| | 19 | | private bool allowHorizontalPanning; |
| | 20 | | private float inertiaDuration; |
| | 21 | | private ICharacterPreviewInputDetector characterPreviewInputDetector; |
| | 22 | | private Vector3 lastMousePosition; |
| | 23 | | private Vector3 lastPanningDeltaBeforeEndDrag; |
| 0 | 24 | | private CancellationTokenSource cts = new (); |
| | 25 | | private Texture2D panningCursorTexture; |
| | 26 | |
|
| | 27 | | public void Configure( |
| | 28 | | InputAction_Hold secondClickAction, |
| | 29 | | InputAction_Hold middleClickAction, |
| | 30 | | float panSpeed, |
| | 31 | | bool allowVerticalPanning, |
| | 32 | | bool allowHorizontalPanning, |
| | 33 | | float inertiaDuration, |
| | 34 | | ICharacterPreviewInputDetector characterPreviewInputDetector, |
| | 35 | | Texture2D panningCursorTexture) |
| | 36 | | { |
| 0 | 37 | | this.secondClickAction = secondClickAction; |
| 0 | 38 | | this.middleClickAction = middleClickAction; |
| 0 | 39 | | this.panSpeed = panSpeed; |
| 0 | 40 | | this.allowVerticalPanning = allowVerticalPanning; |
| 0 | 41 | | this.allowHorizontalPanning = allowHorizontalPanning; |
| 0 | 42 | | this.inertiaDuration = inertiaDuration; |
| 0 | 43 | | this.characterPreviewInputDetector = characterPreviewInputDetector; |
| 0 | 44 | | this.panningCursorTexture = panningCursorTexture; |
| | 45 | |
|
| 0 | 46 | | characterPreviewInputDetector.OnDragStarted += OnBeginDrag; |
| 0 | 47 | | characterPreviewInputDetector.OnDragging += OnDrag; |
| 0 | 48 | | characterPreviewInputDetector.OnDragFinished += OnEndDrag; |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | private void OnBeginDrag(PointerEventData eventData) |
| | 52 | | { |
| 0 | 53 | | if (!middleClickAction.isOn && !secondClickAction.isOn) |
| 0 | 54 | | return; |
| | 55 | |
|
| 0 | 56 | | cts = cts.SafeRestart(); |
| 0 | 57 | | lastMousePosition = Input.mousePosition; |
| 0 | 58 | | AudioScriptableObjects.buttonClick.Play(true); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | private void OnDrag(PointerEventData eventData) |
| | 62 | | { |
| 0 | 63 | | if (!middleClickAction.isOn && !secondClickAction.isOn) |
| 0 | 64 | | return; |
| | 65 | |
|
| 0 | 66 | | var panningDelta = Input.mousePosition - lastMousePosition; |
| 0 | 67 | | panningDelta.y *= -1; |
| | 68 | |
|
| 0 | 69 | | if (!allowVerticalPanning) |
| 0 | 70 | | panningDelta.y = 0f; |
| | 71 | |
|
| 0 | 72 | | if (!allowHorizontalPanning) |
| 0 | 73 | | panningDelta.x = 0f; |
| | 74 | |
|
| 0 | 75 | | panningDelta *= panSpeed * Time.deltaTime; |
| 0 | 76 | | lastPanningDeltaBeforeEndDrag = panningDelta; |
| 0 | 77 | | lastMousePosition = Input.mousePosition; |
| | 78 | |
|
| 0 | 79 | | OnPanning?.Invoke(panningDelta); |
| | 80 | |
|
| 0 | 81 | | if (panningCursorTexture != null) |
| 0 | 82 | | Cursor.SetCursor(panningCursorTexture, new Vector2(panningCursorTexture.width / 2f, panningCursorTexture |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | private void OnEndDrag(PointerEventData eventData) |
| | 86 | | { |
| 0 | 87 | | if (lastPanningDeltaBeforeEndDrag.magnitude >= 0.01f) |
| 0 | 88 | | InertiaAsync(cts.Token).Forget(); |
| | 89 | |
|
| 0 | 90 | | AudioScriptableObjects.buttonRelease.Play(true); |
| 0 | 91 | | Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | private async UniTask InertiaAsync(CancellationToken ct) |
| | 95 | | { |
| 0 | 96 | | float inverseTimer = 1f / inertiaDuration; |
| 0 | 97 | | float timeLeft = inertiaDuration; |
| | 98 | |
|
| 0 | 99 | | while (timeLeft > 0) |
| | 100 | | { |
| 0 | 101 | | timeLeft -= Time.deltaTime; |
| 0 | 102 | | OnPanning?.Invoke(Vector3.Lerp(lastPanningDeltaBeforeEndDrag, Vector3.zero, 1 - (timeLeft * inverseTimer |
| 0 | 103 | | await UniTask.NextFrame(ct); |
| | 104 | | } |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | public void Dispose() |
| | 108 | | { |
| 0 | 109 | | cts.SafeCancelAndDispose(); |
| 0 | 110 | | characterPreviewInputDetector.OnDragStarted -= OnBeginDrag; |
| 0 | 111 | | characterPreviewInputDetector.OnDragging -= OnDrag; |
| 0 | 112 | | characterPreviewInputDetector.OnDragFinished -= OnEndDrag; |
| 0 | 113 | | } |
| | 114 | | } |
| | 115 | | } |