| | 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 | | 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 | |
|
| 0 | 26 | | public bool isLocked => Utils.IsCursorLocked; |
| 0 | 27 | | 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 |
| 60 | 34 | | public LayerMask OnPointerDownTarget = 1 << 9; |
| | 35 | |
|
| | 36 | | private HUDCanvasCameraModeController hudCanvasCameraModeController; |
| | 37 | |
|
| | 38 | | private void Start() |
| | 39 | | { |
| 58 | 40 | | hudCanvasCameraModeController = new HUDCanvasCameraModeController(canvas, DataStore.i.camera.hudsCamera); |
| 58 | 41 | | unlockInputAction.OnTriggered += HandleUnlockInput; |
| 58 | 42 | | } |
| | 43 | |
|
| | 44 | | private void OnDestroy() |
| | 45 | | { |
| 58 | 46 | | hudCanvasCameraModeController?.Dispose(); |
| 58 | 47 | | unlockInputAction.OnTriggered -= HandleUnlockInput; |
| 58 | 48 | | } |
| | 49 | |
|
| | 50 | | public void LockCursor() |
| | 51 | | { |
| 0 | 52 | | if (!renderingEnabled || DataStore.i.common.isSignUpFlow.Get() || DataStore.i.HUDs.loadingHUD.visible.Get()) |
| 0 | 53 | | return; |
| | 54 | |
|
| 0 | 55 | | Utils.LockCursor(); |
| | 56 | | #if !WEB_PLATFORM |
| 0 | 57 | | OnMouseLock?.Invoke(); |
| | 58 | | #endif |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | public void UnlockCursor() |
| | 62 | | { |
| 15 | 63 | | Utils.UnlockCursor(); |
| | 64 | | #if !WEB_PLATFORM |
| 15 | 65 | | OnMouseUnlock?.Invoke(); |
| | 66 | | #endif |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | public void OnPointerDown(PointerEventData eventData) |
| | 70 | | { |
| 0 | 71 | | if (eventData.button == PointerEventData.InputButton.Right) |
| 0 | 72 | | DataStore.i.camera.panning.Set(true); |
| 0 | 73 | | OnMouseDown?.Invoke(); |
| 0 | 74 | | LockCursor(); |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | public void OnPointerUp(PointerEventData eventData) |
| | 78 | | { |
| 0 | 79 | | if (eventData.button == PointerEventData.InputButton.Right) |
| | 80 | | { |
| 0 | 81 | | if (eventData.button == PointerEventData.InputButton.Right) |
| 0 | 82 | | DataStore.i.camera.panning.Set(false); |
| 0 | 83 | | UnlockCursor(); |
| | 84 | | } |
| 0 | 85 | | } |
| | 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 | | { |
| 0 | 98 | | bool lockPointer = val != 0; |
| 0 | 99 | | Utils.BrowserSetCursorState(lockPointer); |
| 0 | 100 | | if (lockPointer) |
| | 101 | | { |
| 0 | 102 | | OnMouseLock?.Invoke(); |
| 0 | 103 | | } |
| | 104 | | else |
| | 105 | | { |
| 0 | 106 | | OnMouseUnlock?.Invoke(); |
| | 107 | | } |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | #endregion |
| | 111 | |
|
| | 112 | | private void HandleUnlockInput(DCLAction_Trigger action) |
| | 113 | | { |
| 0 | 114 | | UnlockCursor(); |
| 0 | 115 | | } |
| | 116 | | } |
| | 117 | | } |