Skip to content

Commit

Permalink
In cursor management request checks, use BasisDeviceManagement.IsUser…
Browse files Browse the repository at this point in the history
…InDesktop():

- Switch to BasisDeviceManagement.IsUserInDesktop() for checking cursor management requests.
- Bypass checks to unlock the cursor when switching from Desktop to VR.
  • Loading branch information
hai-vr committed Dec 22, 2024
1 parent 17db4ab commit ad6101d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEngine;
using System.Collections.Generic;
using System;
using Basis.Scripts.Device_Management;

public static class BasisCursorManagement
{
Expand All @@ -9,9 +10,6 @@ public static class BasisCursorManagement
// Event that gets triggered whenever the cursor state changes
public static event Action<CursorLockMode, bool> 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;
Expand All @@ -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;
}

/// <summary>
/// Locks the cursor to the center of the screen and hides it.
/// Adds a request to lock the cursor.
/// </summary>
public static void LockCursor(string requestName)
{
if (isVRMode) return;
if (ShouldIgnoreCursorRequests()) return;

Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
Expand All @@ -62,8 +45,21 @@ public static void LockCursor(string requestName)
/// </summary>
public static void UnlockCursor(string requestName)
{
if (isVRMode) return;
if (ShouldIgnoreCursorRequests()) return;

InternalUnlockCursor();
}

/// <summary>
/// Unlocks the cursor and makes it visible. Bypasses checks that would have prevented it from being unlocked.
/// </summary>
public static void UnlockCursorBypassChecks()
{
InternalUnlockCursor();
}

private static void InternalUnlockCursor()
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
Debug.Log("Cursor Unlocked");
Expand All @@ -76,11 +72,20 @@ public static void UnlockCursor(string requestName)
/// </summary>
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;
}
}

0 comments on commit ad6101d

Please sign in to comment.