< Summary

Class:DCL.MouseCatcher
Assembly:MouseCatcher
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/MouseCatcher/MouseCatcher.cs
Covered lines:7
Uncovered lines:27
Coverable lines:34
Total lines:110
Line coverage:20.5% (7 of 34)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MouseCatcher()0%110100%
Start()0%110100%
OnDestroy()0%110100%
LockCursor()0%30500%
UnlockCursor()0%2.152066.67%
OnPointerDown(...)0%12300%
OnPointerUp(...)0%12300%
UnlockCursorBrowser(...)0%20400%
HandleUnlockInput(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/MouseCatcher/MouseCatcher.cs

#LineLine coverage
 1#if UNITY_WEBGL && !UNITY_EDITOR
 2#define WEB_PLATFORM
 3#endif
 4
 5using System;
 6using DCL.Helpers;
 7using UnityEngine;
 8using UnityEngine.EventSystems;
 9
 10namespace DCL
 11{
 12    public interface IMouseCatcher
 13    {
 14        event Action OnMouseUnlock;
 15        event Action OnMouseLock;
 16        bool isLocked { get; }
 17    }
 18
 19    public class MouseCatcher : MonoBehaviour, IMouseCatcher, IPointerDownHandler, IPointerUpHandler
 20    {
 21        [SerializeField] private InputAction_Trigger unlockInputAction;
 22
 023        public bool isLocked => Utils.IsCursorLocked;
 024        bool renderingEnabled => CommonScriptableObjects.rendererState.Get();
 25
 26        public event Action OnMouseUnlock;
 27        public event Action OnMouseLock;
 28        public event Action OnMouseDown;
 29
 30        //Default OnPointerEvent
 5831        public LayerMask OnPointerDownTarget = 1 << 9;
 32
 33        private void Start()
 34        {
 5735            unlockInputAction.OnTriggered += HandleUnlockInput;
 5736        }
 37
 38        private void OnDestroy()
 39        {
 5740            unlockInputAction.OnTriggered -= HandleUnlockInput;
 5741        }
 42
 43        public void LockCursor()
 44        {
 045            if (!renderingEnabled || DataStore.i.common.isSignUpFlow.Get() || DataStore.i.HUDs.loadingHUD.visible.Get())
 046                return;
 47
 048            Utils.LockCursor();
 49#if !WEB_PLATFORM
 050            OnMouseLock?.Invoke();
 51#endif
 052        }
 53
 54        public void UnlockCursor()
 55        {
 2856            Utils.UnlockCursor();
 57#if !WEB_PLATFORM
 2858            OnMouseUnlock?.Invoke();
 59#endif
 060        }
 61
 62        public void OnPointerDown(PointerEventData eventData)
 63        {
 064            if (eventData.button == PointerEventData.InputButton.Right)
 065                DataStore.i.camera.panning.Set(true);
 066            OnMouseDown?.Invoke();
 067            LockCursor();
 068        }
 69
 70        public void OnPointerUp(PointerEventData eventData)
 71        {
 072            if (eventData.button == PointerEventData.InputButton.Right)
 73            {
 074                if (eventData.button == PointerEventData.InputButton.Right)
 075                    DataStore.i.camera.panning.Set(false);
 076                UnlockCursor();
 77            }
 078        }
 79
 80        #region BROWSER_ONLY
 81
 82        //TODO(Brian): Move all this mechanism to a new MouseLockController and branch
 83        //             behaviour using strategy pattern instead of this.
 84
 85        /// <summary>
 86        /// Externally -ONLY- called by the browser
 87        /// </summary>
 88        /// <param name="val">1 is locked, 0 is unlocked</param>
 89        public void UnlockCursorBrowser(int val)
 90        {
 091            bool lockPointer = val != 0;
 092            Utils.BrowserSetCursorState(lockPointer);
 093            if (lockPointer)
 94            {
 095                OnMouseLock?.Invoke();
 096            }
 97            else
 98            {
 099                OnMouseUnlock?.Invoke();
 100            }
 0101        }
 102
 103        #endregion
 104
 105        private void HandleUnlockInput(DCLAction_Trigger action)
 106        {
 0107            UnlockCursor();
 0108        }
 109    }
 110}