< Summary

Class:DCL.MouseCatcher
Assembly:MouseCatcher
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/MouseCatcher/MouseCatcher.cs
Covered lines:9
Uncovered lines:27
Coverable lines:36
Total lines:117
Line coverage:25% (9 of 36)
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%220100%
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        void LockCursor();
 18        void UnlockCursor();
 19    }
 20
 21    public class MouseCatcher : MonoBehaviour, IMouseCatcher, IPointerDownHandler, IPointerUpHandler
 22    {
 23        [SerializeField] private InputAction_Trigger unlockInputAction;
 24        [SerializeField] private Canvas canvas;
 25
 026        public bool isLocked => Utils.IsCursorLocked;
 027        bool renderingEnabled => CommonScriptableObjects.rendererState.Get();
 28
 29        public event Action OnMouseUnlock;
 30        public event Action OnMouseLock;
 31        public event Action OnMouseDown;
 32
 33        //Default OnPointerEvent
 6034        public LayerMask OnPointerDownTarget = 1 << 9;
 35
 36        private HUDCanvasCameraModeController hudCanvasCameraModeController;
 37
 38        private void Start()
 39        {
 5840            hudCanvasCameraModeController = new HUDCanvasCameraModeController(canvas, DataStore.i.camera.hudsCamera);
 5841            unlockInputAction.OnTriggered += HandleUnlockInput;
 5842        }
 43
 44        private void OnDestroy()
 45        {
 5846            hudCanvasCameraModeController?.Dispose();
 5847            unlockInputAction.OnTriggered -= HandleUnlockInput;
 5848        }
 49
 50        public void LockCursor()
 51        {
 052            if (!renderingEnabled || DataStore.i.common.isSignUpFlow.Get() || DataStore.i.HUDs.loadingHUD.visible.Get())
 053                return;
 54
 055            Utils.LockCursor();
 56#if !WEB_PLATFORM
 057            OnMouseLock?.Invoke();
 58#endif
 059        }
 60
 61        public void UnlockCursor()
 62        {
 1563            Utils.UnlockCursor();
 64#if !WEB_PLATFORM
 1565            OnMouseUnlock?.Invoke();
 66#endif
 067        }
 68
 69        public void OnPointerDown(PointerEventData eventData)
 70        {
 071            if (eventData.button == PointerEventData.InputButton.Right)
 072                DataStore.i.camera.panning.Set(true);
 073            OnMouseDown?.Invoke();
 074            LockCursor();
 075        }
 76
 77        public void OnPointerUp(PointerEventData eventData)
 78        {
 079            if (eventData.button == PointerEventData.InputButton.Right)
 80            {
 081                if (eventData.button == PointerEventData.InputButton.Right)
 082                    DataStore.i.camera.panning.Set(false);
 083                UnlockCursor();
 84            }
 085        }
 86
 87        #region BROWSER_ONLY
 88
 89        //TODO(Brian): Move all this mechanism to a new MouseLockController and branch
 90        //             behaviour using strategy pattern instead of this.
 91
 92        /// <summary>
 93        /// Externally -ONLY- called by the browser
 94        /// </summary>
 95        /// <param name="val">1 is locked, 0 is unlocked</param>
 96        public void UnlockCursorBrowser(int val)
 97        {
 098            bool lockPointer = val != 0;
 099            Utils.BrowserSetCursorState(lockPointer);
 0100            if (lockPointer)
 101            {
 0102                OnMouseLock?.Invoke();
 0103            }
 104            else
 105            {
 0106                OnMouseUnlock?.Invoke();
 107            }
 0108        }
 109
 110        #endregion
 111
 112        private void HandleUnlockInput(DCLAction_Trigger action)
 113        {
 0114            UnlockCursor();
 0115        }
 116    }
 117}