< Summary

Class:DCL.MouseCatcher
Assembly:MouseCatcher
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/MouseCatcher/MouseCatcher.cs
Covered lines:5
Uncovered lines:19
Coverable lines:24
Total lines:92
Line coverage:20.8% (5 of 24)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MouseCatcher()0%110100%
Update()0%2.152066.67%
LockCursor()0%20400%
UnlockCursor()0%2.152066.67%
OnPointerDown(...)0%6200%
UnlockCursorBrowser(...)0%20400%

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 UnityEngine;
 6using UnityEngine.EventSystems;
 7using System.Collections.Generic;
 8using DCL.Helpers;
 9
 10namespace DCL
 11{
 12    public interface IMouseCatcher
 13    {
 14        event System.Action OnMouseUnlock;
 15        event System.Action OnMouseLock;
 16        bool isLocked { get; }
 17    }
 18
 19    public class MouseCatcher : MonoBehaviour, IMouseCatcher, IPointerDownHandler
 20    {
 021        public bool isLocked => Utils.isCursorLocked;
 022        bool renderingEnabled => CommonScriptableObjects.rendererState.Get();
 23
 24        public event System.Action OnMouseUnlock;
 25        public event System.Action OnMouseLock;
 26        public event System.Action OnMouseDown;
 27
 28        //Default OnPointerEvent
 20429        public LayerMask OnPointerDownTarget = 1 << 9;
 30
 31        void Update()
 32        {
 33#if !WEB_PLATFORM
 34            //Browser is changing this automatically
 1971935            if (Input.GetKeyDown(KeyCode.Escape))
 36            {
 037                UnlockCursor();
 38            }
 39#endif
 1971940        }
 41
 42        public void LockCursor()
 43        {
 044            if (!renderingEnabled || DataStore.i.isSignUpFlow.Get())
 045                return;
 46
 047            Utils.LockCursor();
 48#if !WEB_PLATFORM
 049            OnMouseLock?.Invoke();
 50#endif
 051        }
 52
 53        public void UnlockCursor()
 54        {
 1855            Utils.UnlockCursor();
 56#if !WEB_PLATFORM
 1857            OnMouseUnlock?.Invoke();
 58#endif
 059        }
 60
 61        public void OnPointerDown(PointerEventData eventData)
 62        {
 063            OnMouseDown?.Invoke();
 064            LockCursor();
 065        }
 66
 67        #region BROWSER_ONLY
 68
 69        //TODO(Brian): Move all this mechanism to a new MouseLockController and branch
 70        //             behaviour using strategy pattern instead of this.
 71
 72        /// <summary>
 73        /// Externally -ONLY- called by the browser
 74        /// </summary>
 75        /// <param name="val">1 is locked, 0 is unlocked</param>
 76        public void UnlockCursorBrowser(int val)
 77        {
 078            bool lockPointer = val != 0;
 079            Utils.BrowserSetCursorState(lockPointer);
 080            if (lockPointer)
 81            {
 082                OnMouseLock?.Invoke();
 083            }
 84            else
 85            {
 086                OnMouseUnlock?.Invoke();
 87            }
 088        }
 89
 90        #endregion
 91    }
 92}