< Summary

Class:MainScripts.DCL.Controllers.HUD.CharacterPreview.PreviewCameraRotationController
Assembly:CharacterPreviewController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/CharacterPreview/PreviewCameraRotationController.cs
Covered lines:0
Uncovered lines:42
Coverable lines:42
Total lines:97
Line coverage:0% (0 of 42)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:7
Method coverage:0% (0 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PreviewCameraRotationController()0%2100%
Configure(...)0%2100%
OnBeginDrag(...)0%6200%
OnDrag(...)0%20400%
OnEndDrag(...)0%6200%
SlowDownAsync()0%30500%
Dispose()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/CharacterPreview/PreviewCameraRotationController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Tasks;
 3using System;
 4using System.Threading;
 5using UnityEngine;
 6using UnityEngine.EventSystems;
 7
 8namespace 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;
 021        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        {
 033            this.firstClickAction = firstClickAction;
 034            this.rotationFactor = rotationFactor;
 035            this.slowDownTime = slowDownTime;
 036            this.characterPreviewInputDetector = characterPreviewInputDetector;
 037            this.rotateCursorTexture = rotateCursorTexture;
 38
 039            characterPreviewInputDetector.OnDragStarted += OnBeginDrag;
 040            characterPreviewInputDetector.OnDragging += OnDrag;
 041            characterPreviewInputDetector.OnDragFinished += OnEndDrag;
 042        }
 43
 44        private void OnBeginDrag(PointerEventData eventData)
 45        {
 046            if (!firstClickAction.isOn)
 047                return;
 48
 049            cts = cts.SafeRestart();
 050            startDragDateTime = DateTime.Now;
 051            AudioScriptableObjects.buttonClick.Play(true);
 052        }
 53
 54        private void OnDrag(PointerEventData eventData)
 55        {
 056            if (!firstClickAction.isOn)
 057                return;
 58
 059            currentHorizontalRotationVelocity = rotationFactor * eventData.delta.x;
 060            OnHorizontalRotation?.Invoke(currentHorizontalRotationVelocity);
 61
 062            if (rotateCursorTexture != null)
 063                Cursor.SetCursor(rotateCursorTexture, new Vector2(rotateCursorTexture.width / 2f, rotateCursorTexture.he
 064        }
 65
 66        private void OnEndDrag(PointerEventData eventData)
 67        {
 068            timer = slowDownTime;
 069            slowDownVelocity = currentHorizontalRotationVelocity;
 070            SlowDownAsync(cts.Token).Forget();
 071            OnEndDragEvent?.Invoke((DateTime.Now - startDragDateTime).TotalMilliseconds);
 072            AudioScriptableObjects.buttonRelease.Play(true);
 073            Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
 074        }
 75
 76        private async UniTask SlowDownAsync(CancellationToken ct)
 77        {
 078            float inverseTimer = 1f / slowDownTime;
 79
 080            while (timer > 0)
 81            {
 082                timer -= Time.deltaTime;
 083                currentHorizontalRotationVelocity  = Mathf.Lerp(slowDownVelocity, 0, 1 - (timer * inverseTimer));
 084                OnHorizontalRotation?.Invoke(currentHorizontalRotationVelocity);
 085                await UniTask.NextFrame(ct);
 86            }
 087        }
 88
 89        public void Dispose()
 90        {
 091            cts.SafeCancelAndDispose();
 092            characterPreviewInputDetector.OnDragStarted -= OnBeginDrag;
 093            characterPreviewInputDetector.OnDragging -= OnDrag;
 094            characterPreviewInputDetector.OnDragFinished -= OnEndDrag;
 095        }
 96    }
 97}