| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.EventSystems; |
| | 5 | |
|
| | 6 | | namespace MainScripts.DCL.Controllers.HUD.CharacterPreview |
| | 7 | | { |
| | 8 | | public class PreviewCameraRotation : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler |
| | 9 | | { |
| | 10 | | public event System.Action<float> OnHorizontalRotation; |
| | 11 | | public event System.Action<double> OnEndDragEvent; |
| | 12 | |
|
| 52 | 13 | | public float rotationFactor = -15f; |
| | 14 | |
|
| 52 | 15 | | public float slowDownTime = 0.5f; |
| | 16 | |
|
| | 17 | | private float currentHorizontalRotationVelocity = 0f; |
| | 18 | |
|
| | 19 | | private float slowDownVelocity; |
| | 20 | |
|
| | 21 | | private Coroutine slowDownCoroutine; |
| | 22 | |
|
| | 23 | | private float timer; |
| | 24 | | private DateTime startDragDateTime; |
| | 25 | |
|
| | 26 | | public void OnBeginDrag(PointerEventData eventData) |
| | 27 | | { |
| 0 | 28 | | startDragDateTime = DateTime.Now; |
| 0 | 29 | | AudioScriptableObjects.buttonClick.Play(true); |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | public void OnDrag(PointerEventData eventData) |
| | 33 | | { |
| 0 | 34 | | if (slowDownCoroutine != null) |
| | 35 | | { |
| 0 | 36 | | StopCoroutine(slowDownCoroutine); |
| 0 | 37 | | slowDownCoroutine = null; |
| | 38 | | } |
| | 39 | |
|
| 0 | 40 | | currentHorizontalRotationVelocity = rotationFactor * eventData.delta.x; |
| 0 | 41 | | OnHorizontalRotation?.Invoke(currentHorizontalRotationVelocity); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public void OnEndDrag(PointerEventData eventData) |
| | 45 | | { |
| 0 | 46 | | timer = slowDownTime; |
| 0 | 47 | | slowDownVelocity = currentHorizontalRotationVelocity; |
| | 48 | |
|
| 0 | 49 | | if (slowDownCoroutine == null) |
| | 50 | | { |
| 0 | 51 | | slowDownCoroutine = StartCoroutine(SlowDown()); |
| | 52 | | } |
| | 53 | |
|
| 0 | 54 | | OnEndDragEvent?.Invoke((DateTime.Now - startDragDateTime).TotalMilliseconds); |
| 0 | 55 | | AudioScriptableObjects.buttonRelease.Play(true); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | private IEnumerator SlowDown() |
| | 59 | | { |
| 0 | 60 | | float inverseTimer = 1f / slowDownTime; |
| | 61 | |
|
| 0 | 62 | | while (timer > 0) |
| | 63 | | { |
| 0 | 64 | | timer -= Time.deltaTime; |
| 0 | 65 | | currentHorizontalRotationVelocity = Mathf.Lerp(slowDownVelocity, 0, 1 - (timer * inverseTimer)); |
| 0 | 66 | | OnHorizontalRotation?.Invoke(currentHorizontalRotationVelocity); |
| | 67 | |
|
| 0 | 68 | | yield return null; |
| | 69 | | } |
| 0 | 70 | | } |
| | 71 | | } |
| | 72 | | } |