From 17db4abdaf1e525d898152ba6ec997e32be88d4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=C3=AF=7E?= Date: Sun, 22 Dec 2024 09:44:02 +0100 Subject: [PATCH 1/2] Ignore all cursor management requests while in VR mode: - When entering VR mode, unlock cursor and ignore all cursor management requests. - This new behaviour prevents the cursor from being stolen from other VR desktop overlay applications. --- .../BasisDeviceManagement.cs | 6 ++-- .../Devices/BasisCursorManagement.cs | 28 ++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Basis/Packages/Basis Framework/Device Management/BasisDeviceManagement.cs b/Basis/Packages/Basis Framework/Device Management/BasisDeviceManagement.cs index f2569c709..7b918b174 100644 --- a/Basis/Packages/Basis Framework/Device Management/BasisDeviceManagement.cs +++ b/Basis/Packages/Basis Framework/Device Management/BasisDeviceManagement.cs @@ -41,7 +41,7 @@ public string DefaultMode() /// /// checks to see if we are in desktop /// this being false does not mean its vr. - /// + /// /// /// public static bool IsUserInDesktop() @@ -188,6 +188,8 @@ public void SwitchMode(string newMode) } CurrentMode = newMode; + if (newMode == "Desktop") BasisCursorManagement.SwitchToDesktopMode(); + else if (newMode != "Exiting") BasisCursorManagement.SwitchToVRMode(); OnBootModeChanged?.Invoke(CurrentMode); Debug.Log("Loading " + CurrentMode); @@ -475,4 +477,4 @@ public void LoadAndOrSaveDefaultDeviceConfigs(string directoryPath) UseAbleDeviceConfigs = deviceDictionary.Values.ToList(); } } -} \ No newline at end of file +} diff --git a/Basis/Packages/Basis Framework/Device Management/Devices/BasisCursorManagement.cs b/Basis/Packages/Basis Framework/Device Management/Devices/BasisCursorManagement.cs index 915face9e..069e20745 100644 --- a/Basis/Packages/Basis Framework/Device Management/Devices/BasisCursorManagement.cs +++ b/Basis/Packages/Basis Framework/Device Management/Devices/BasisCursorManagement.cs @@ -8,6 +8,10 @@ public static class BasisCursorManagement private static List cursorLockRequests = new List(); // Event that gets triggered whenever the cursor state changes public static event Action OnCursorStateChange; + + // When in VR mode, all cursor lock requests are ignored. + private static bool isVRMode; + public static void OverrideableLock(string requestName) { Cursor.lockState = CursorLockMode.Locked; @@ -22,12 +26,30 @@ public static bool IsVisible() { return Cursor.visible; } + + public static void SwitchToVRMode() + { + isVRMode = true; + + Cursor.lockState = CursorLockMode.None; + Cursor.visible = true; + Debug.Log("Cursor Unlocked due to entering VR mode"); + OnCursorStateChange?.Invoke(CursorLockMode.None, true); + } + + public static void SwitchToDesktopMode() + { + isVRMode = false; + } + /// /// Locks the cursor to the center of the screen and hides it. /// Adds a request to lock the cursor. /// public static void LockCursor(string requestName) { + if (isVRMode) return; + Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; Debug.Log("Cursor Locked"); @@ -40,6 +62,8 @@ public static void LockCursor(string requestName) /// public static void UnlockCursor(string requestName) { + if (isVRMode) return; + Cursor.lockState = CursorLockMode.None; Cursor.visible = true; Debug.Log("Cursor Unlocked"); @@ -52,9 +76,11 @@ public static void UnlockCursor(string requestName) /// public static void ConfineCursor(string requestName) { + if (isVRMode) return; + Cursor.lockState = CursorLockMode.Confined; Cursor.visible = true; Debug.Log("Cursor Confined"); OnCursorStateChange?.Invoke(CursorLockMode.Confined, true); } -} \ No newline at end of file +} From ad6101d85a74dc2b8f5041214e26acb294de14f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=C3=AF=7E?= Date: Sun, 22 Dec 2024 18:53:08 +0100 Subject: [PATCH 2/2] In cursor management request checks, use BasisDeviceManagement.IsUserInDesktop(): - Switch to BasisDeviceManagement.IsUserInDesktop() for checking cursor management requests. - Bypass checks to unlock the cursor when switching from Desktop to VR. --- .../BasisDeviceManagement.cs | 6 ++- .../Devices/BasisCursorManagement.cs | 47 ++++++++++--------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/Basis/Packages/Basis Framework/Device Management/BasisDeviceManagement.cs b/Basis/Packages/Basis Framework/Device Management/BasisDeviceManagement.cs index 7b918b174..b2f976883 100644 --- a/Basis/Packages/Basis Framework/Device Management/BasisDeviceManagement.cs +++ b/Basis/Packages/Basis Framework/Device Management/BasisDeviceManagement.cs @@ -188,8 +188,10 @@ public void SwitchMode(string newMode) } CurrentMode = newMode; - if (newMode == "Desktop") BasisCursorManagement.SwitchToDesktopMode(); - else if (newMode != "Exiting") BasisCursorManagement.SwitchToVRMode(); + if (newMode != "Desktop" && newMode != "Exiting") + { + BasisCursorManagement.UnlockCursorBypassChecks(); + } OnBootModeChanged?.Invoke(CurrentMode); Debug.Log("Loading " + CurrentMode); diff --git a/Basis/Packages/Basis Framework/Device Management/Devices/BasisCursorManagement.cs b/Basis/Packages/Basis Framework/Device Management/Devices/BasisCursorManagement.cs index 069e20745..1151fdedf 100644 --- a/Basis/Packages/Basis Framework/Device Management/Devices/BasisCursorManagement.cs +++ b/Basis/Packages/Basis Framework/Device Management/Devices/BasisCursorManagement.cs @@ -1,6 +1,7 @@ using UnityEngine; using System.Collections.Generic; using System; +using Basis.Scripts.Device_Management; public static class BasisCursorManagement { @@ -9,9 +10,6 @@ public static class BasisCursorManagement // Event that gets triggered whenever the cursor state changes public static event Action OnCursorStateChange; - // When in VR mode, all cursor lock requests are ignored. - private static bool isVRMode; - public static void OverrideableLock(string requestName) { Cursor.lockState = CursorLockMode.Locked; @@ -27,28 +25,13 @@ public static bool IsVisible() return Cursor.visible; } - public static void SwitchToVRMode() - { - isVRMode = true; - - Cursor.lockState = CursorLockMode.None; - Cursor.visible = true; - Debug.Log("Cursor Unlocked due to entering VR mode"); - OnCursorStateChange?.Invoke(CursorLockMode.None, true); - } - - public static void SwitchToDesktopMode() - { - isVRMode = false; - } - /// /// Locks the cursor to the center of the screen and hides it. /// Adds a request to lock the cursor. /// public static void LockCursor(string requestName) { - if (isVRMode) return; + if (ShouldIgnoreCursorRequests()) return; Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; @@ -62,8 +45,21 @@ public static void LockCursor(string requestName) /// public static void UnlockCursor(string requestName) { - if (isVRMode) return; + if (ShouldIgnoreCursorRequests()) return; + InternalUnlockCursor(); + } + + /// + /// Unlocks the cursor and makes it visible. Bypasses checks that would have prevented it from being unlocked. + /// + public static void UnlockCursorBypassChecks() + { + InternalUnlockCursor(); + } + + private static void InternalUnlockCursor() + { Cursor.lockState = CursorLockMode.None; Cursor.visible = true; Debug.Log("Cursor Unlocked"); @@ -76,11 +72,20 @@ public static void UnlockCursor(string requestName) /// public static void ConfineCursor(string requestName) { - if (isVRMode) return; + if (ShouldIgnoreCursorRequests()) return; Cursor.lockState = CursorLockMode.Confined; Cursor.visible = true; Debug.Log("Cursor Confined"); OnCursorStateChange?.Invoke(CursorLockMode.Confined, true); } + + private static bool ShouldIgnoreCursorRequests() + { + var isUserInVR = !BasisDeviceManagement.IsUserInDesktop(); + + // When in VR mode, all cursor lock requests are must be ignored, + // so that cursor control is not taken away from other external desktop overlay applications. + return isUserInVR; + } }