| | 1 | | #if UNITY_WEBGL && !UNITY_EDITOR |
| | 2 | | #define WEB_PLATFORM |
| | 3 | | #endif |
| | 4 | |
|
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.EventSystems; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using DCL.Helpers; |
| | 9 | |
|
| | 10 | | namespace 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 | | { |
| 0 | 21 | | public bool isLocked => Utils.isCursorLocked; |
| 0 | 22 | | 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 |
| 204 | 29 | | public LayerMask OnPointerDownTarget = 1 << 9; |
| | 30 | |
|
| | 31 | | void Update() |
| | 32 | | { |
| | 33 | | #if !WEB_PLATFORM |
| | 34 | | //Browser is changing this automatically |
| 19719 | 35 | | if (Input.GetKeyDown(KeyCode.Escape)) |
| | 36 | | { |
| 0 | 37 | | UnlockCursor(); |
| | 38 | | } |
| | 39 | | #endif |
| 19719 | 40 | | } |
| | 41 | |
|
| | 42 | | public void LockCursor() |
| | 43 | | { |
| 0 | 44 | | if (!renderingEnabled || DataStore.i.isSignUpFlow.Get()) |
| 0 | 45 | | return; |
| | 46 | |
|
| 0 | 47 | | Utils.LockCursor(); |
| | 48 | | #if !WEB_PLATFORM |
| 0 | 49 | | OnMouseLock?.Invoke(); |
| | 50 | | #endif |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public void UnlockCursor() |
| | 54 | | { |
| 18 | 55 | | Utils.UnlockCursor(); |
| | 56 | | #if !WEB_PLATFORM |
| 18 | 57 | | OnMouseUnlock?.Invoke(); |
| | 58 | | #endif |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | public void OnPointerDown(PointerEventData eventData) |
| | 62 | | { |
| 0 | 63 | | OnMouseDown?.Invoke(); |
| 0 | 64 | | LockCursor(); |
| 0 | 65 | | } |
| | 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 | | { |
| 0 | 78 | | bool lockPointer = val != 0; |
| 0 | 79 | | Utils.BrowserSetCursorState(lockPointer); |
| 0 | 80 | | if (lockPointer) |
| | 81 | | { |
| 0 | 82 | | OnMouseLock?.Invoke(); |
| 0 | 83 | | } |
| | 84 | | else |
| | 85 | | { |
| 0 | 86 | | OnMouseUnlock?.Invoke(); |
| | 87 | | } |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | #endregion |
| | 91 | | } |
| | 92 | | } |