| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Tasks; |
| | 3 | | using System; |
| | 4 | | using System.Threading; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.EventSystems; |
| | 7 | |
|
| | 8 | | namespace MainScripts.DCL.Controllers.HUD.CharacterPreview |
| | 9 | | { |
| | 10 | | public class PreviewCameraRotationController : IPreviewCameraRotationController |
| | 11 | | { |
| | 12 | | public event Action<float> OnHorizontalRotation; |
| | 13 | | public event Action<double> OnEndDragEvent; |
| | 14 | |
|
| | 15 | | private InputAction_Hold firstClickAction; |
| | 16 | | private float rotationFactor; |
| | 17 | | private float slowDownTime; |
| | 18 | | private ICharacterPreviewInputDetector characterPreviewInputDetector; |
| | 19 | | private float currentHorizontalRotationVelocity; |
| | 20 | | private float slowDownVelocity; |
| 0 | 21 | | private CancellationTokenSource cts = new (); |
| | 22 | | private float timer; |
| | 23 | | private DateTime startDragDateTime; |
| | 24 | | private Texture2D rotateCursorTexture; |
| | 25 | |
|
| | 26 | | public void Configure( |
| | 27 | | InputAction_Hold firstClickAction, |
| | 28 | | float rotationFactor, |
| | 29 | | float slowDownTime, |
| | 30 | | ICharacterPreviewInputDetector characterPreviewInputDetector, |
| | 31 | | Texture2D rotateCursorTexture) |
| | 32 | | { |
| 0 | 33 | | this.firstClickAction = firstClickAction; |
| 0 | 34 | | this.rotationFactor = rotationFactor; |
| 0 | 35 | | this.slowDownTime = slowDownTime; |
| 0 | 36 | | this.characterPreviewInputDetector = characterPreviewInputDetector; |
| 0 | 37 | | this.rotateCursorTexture = rotateCursorTexture; |
| | 38 | |
|
| 0 | 39 | | characterPreviewInputDetector.OnDragStarted += OnBeginDrag; |
| 0 | 40 | | characterPreviewInputDetector.OnDragging += OnDrag; |
| 0 | 41 | | characterPreviewInputDetector.OnDragFinished += OnEndDrag; |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | private void OnBeginDrag(PointerEventData eventData) |
| | 45 | | { |
| 0 | 46 | | if (!firstClickAction.isOn) |
| 0 | 47 | | return; |
| | 48 | |
|
| 0 | 49 | | cts = cts.SafeRestart(); |
| 0 | 50 | | startDragDateTime = DateTime.Now; |
| 0 | 51 | | AudioScriptableObjects.buttonClick.Play(true); |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | private void OnDrag(PointerEventData eventData) |
| | 55 | | { |
| 0 | 56 | | if (!firstClickAction.isOn) |
| 0 | 57 | | return; |
| | 58 | |
|
| 0 | 59 | | currentHorizontalRotationVelocity = rotationFactor * eventData.delta.x; |
| 0 | 60 | | OnHorizontalRotation?.Invoke(currentHorizontalRotationVelocity); |
| | 61 | |
|
| 0 | 62 | | if (rotateCursorTexture != null) |
| 0 | 63 | | Cursor.SetCursor(rotateCursorTexture, new Vector2(rotateCursorTexture.width / 2f, rotateCursorTexture.he |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | private void OnEndDrag(PointerEventData eventData) |
| | 67 | | { |
| 0 | 68 | | timer = slowDownTime; |
| 0 | 69 | | slowDownVelocity = currentHorizontalRotationVelocity; |
| 0 | 70 | | SlowDownAsync(cts.Token).Forget(); |
| 0 | 71 | | OnEndDragEvent?.Invoke((DateTime.Now - startDragDateTime).TotalMilliseconds); |
| 0 | 72 | | AudioScriptableObjects.buttonRelease.Play(true); |
| 0 | 73 | | Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | private async UniTask SlowDownAsync(CancellationToken ct) |
| | 77 | | { |
| 0 | 78 | | float inverseTimer = 1f / slowDownTime; |
| | 79 | |
|
| 0 | 80 | | while (timer > 0) |
| | 81 | | { |
| 0 | 82 | | timer -= Time.deltaTime; |
| 0 | 83 | | currentHorizontalRotationVelocity = Mathf.Lerp(slowDownVelocity, 0, 1 - (timer * inverseTimer)); |
| 0 | 84 | | OnHorizontalRotation?.Invoke(currentHorizontalRotationVelocity); |
| 0 | 85 | | await UniTask.NextFrame(ct); |
| | 86 | | } |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | public void Dispose() |
| | 90 | | { |
| 0 | 91 | | cts.SafeCancelAndDispose(); |
| 0 | 92 | | characterPreviewInputDetector.OnDragStarted -= OnBeginDrag; |
| 0 | 93 | | characterPreviewInputDetector.OnDragging -= OnDrag; |
| 0 | 94 | | characterPreviewInputDetector.OnDragFinished -= OnEndDrag; |
| 0 | 95 | | } |
| | 96 | | } |
| | 97 | | } |