| | 1 | | #if UNITY_WEBGL && !UNITY_EDITOR |
| | 2 | | #define WEB_PLATFORM |
| | 3 | | #endif |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.EventSystems; |
| | 9 | |
|
| | 10 | | namespace 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 | |
|
| 0 | 23 | | public bool isLocked => Utils.IsCursorLocked; |
| 0 | 24 | | 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 |
| 58 | 31 | | public LayerMask OnPointerDownTarget = 1 << 9; |
| | 32 | |
|
| | 33 | | private void Start() |
| | 34 | | { |
| 57 | 35 | | unlockInputAction.OnTriggered += HandleUnlockInput; |
| 57 | 36 | | } |
| | 37 | |
|
| | 38 | | private void OnDestroy() |
| | 39 | | { |
| 57 | 40 | | unlockInputAction.OnTriggered -= HandleUnlockInput; |
| 57 | 41 | | } |
| | 42 | |
|
| | 43 | | public void LockCursor() |
| | 44 | | { |
| 0 | 45 | | if (!renderingEnabled || DataStore.i.common.isSignUpFlow.Get() || DataStore.i.HUDs.loadingHUD.visible.Get()) |
| 0 | 46 | | return; |
| | 47 | |
|
| 0 | 48 | | Utils.LockCursor(); |
| | 49 | | #if !WEB_PLATFORM |
| 0 | 50 | | OnMouseLock?.Invoke(); |
| | 51 | | #endif |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | public void UnlockCursor() |
| | 55 | | { |
| 28 | 56 | | Utils.UnlockCursor(); |
| | 57 | | #if !WEB_PLATFORM |
| 28 | 58 | | OnMouseUnlock?.Invoke(); |
| | 59 | | #endif |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | public void OnPointerDown(PointerEventData eventData) |
| | 63 | | { |
| 0 | 64 | | if (eventData.button == PointerEventData.InputButton.Right) |
| 0 | 65 | | DataStore.i.camera.panning.Set(true); |
| 0 | 66 | | OnMouseDown?.Invoke(); |
| 0 | 67 | | LockCursor(); |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | public void OnPointerUp(PointerEventData eventData) |
| | 71 | | { |
| 0 | 72 | | if (eventData.button == PointerEventData.InputButton.Right) |
| | 73 | | { |
| 0 | 74 | | if (eventData.button == PointerEventData.InputButton.Right) |
| 0 | 75 | | DataStore.i.camera.panning.Set(false); |
| 0 | 76 | | UnlockCursor(); |
| | 77 | | } |
| 0 | 78 | | } |
| | 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 | | { |
| 0 | 91 | | bool lockPointer = val != 0; |
| 0 | 92 | | Utils.BrowserSetCursorState(lockPointer); |
| 0 | 93 | | if (lockPointer) |
| | 94 | | { |
| 0 | 95 | | OnMouseLock?.Invoke(); |
| 0 | 96 | | } |
| | 97 | | else |
| | 98 | | { |
| 0 | 99 | | OnMouseUnlock?.Invoke(); |
| | 100 | | } |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | #endregion |
| | 104 | |
|
| | 105 | | private void HandleUnlockInput(DCLAction_Trigger action) |
| | 106 | | { |
| 0 | 107 | | UnlockCursor(); |
| 0 | 108 | | } |
| | 109 | | } |
| | 110 | | } |