| | 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 PreviewCameraZoomController : IPreviewCameraZoomController |
| | 11 | | { |
| | 12 | | public event Action<Vector3> OnZoom; |
| | 13 | |
|
| | 14 | | private InputAction_Measurable mouseWheelAction; |
| | 15 | | private float zoomSpeed; |
| | 16 | | private float smoothTime; |
| | 17 | | private ICharacterPreviewInputDetector characterPreviewInputDetector; |
| | 18 | | private Vector3 currentZoomDelta; |
| | 19 | | private Vector3 currentZoomVelocity; |
| 0 | 20 | | private CancellationTokenSource cts = new (); |
| | 21 | |
|
| | 22 | | public void Configure( |
| | 23 | | InputAction_Measurable mouseWheelAction, |
| | 24 | | float zoomSpeed, |
| | 25 | | float smoothTime, |
| | 26 | | ICharacterPreviewInputDetector characterPreviewInputDetector) |
| | 27 | | { |
| 0 | 28 | | this.mouseWheelAction = mouseWheelAction; |
| 0 | 29 | | this.zoomSpeed = zoomSpeed; |
| 0 | 30 | | this.smoothTime = smoothTime; |
| 0 | 31 | | this.characterPreviewInputDetector = characterPreviewInputDetector; |
| | 32 | |
|
| 0 | 33 | | characterPreviewInputDetector.OnPointerFocus += OnPointerEnter; |
| 0 | 34 | | characterPreviewInputDetector.OnPointerUnFocus += OnPointerExit; |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | private void OnPointerEnter(PointerEventData eventData) |
| | 38 | | { |
| 0 | 39 | | cts = cts.SafeRestart(); |
| 0 | 40 | | ZoomAsync(cts.Token).Forget(); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | private void OnPointerExit(PointerEventData eventData) |
| | 44 | | { |
| 0 | 45 | | cts = cts.SafeRestart(); |
| 0 | 46 | | currentZoomDelta = Vector3.zero; |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | private async UniTask ZoomAsync(CancellationToken ct) |
| | 50 | | { |
| 0 | 51 | | while (!ct.IsCancellationRequested) |
| | 52 | | { |
| 0 | 53 | | Vector3 newZoomDelta = Vector3.forward * (-mouseWheelAction.GetValue() * zoomSpeed); |
| | 54 | |
|
| 0 | 55 | | if (currentZoomDelta != newZoomDelta) |
| | 56 | | { |
| 0 | 57 | | currentZoomDelta = Vector3.SmoothDamp(currentZoomDelta, newZoomDelta, ref currentZoomVelocity, smoot |
| 0 | 58 | | OnZoom?.Invoke(currentZoomDelta); |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | await UniTask.NextFrame(ct); |
| | 62 | | } |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public void Dispose() |
| | 66 | | { |
| 0 | 67 | | cts.SafeCancelAndDispose(); |
| 0 | 68 | | characterPreviewInputDetector.OnPointerFocus -= OnPointerEnter; |
| 0 | 69 | | characterPreviewInputDetector.OnPointerUnFocus -= OnPointerExit; |
| 0 | 70 | | } |
| | 71 | | } |
| | 72 | | } |