Skip to content

Commit

Permalink
Merge pull request #79 from hai-vr/unlock-cursor-in-vr
Browse files Browse the repository at this point in the history
Ignore all cursor management requests while in VR mode:
  • Loading branch information
dooly123 authored Dec 22, 2024
2 parents d0d5e7a + ad6101d commit 7b00fa9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public string DefaultMode()
/// <summary>
/// checks to see if we are in desktop
/// this being false does not mean its vr.
///
///
/// </summary>
/// <returns></returns>
public static bool IsUserInDesktop()
Expand Down Expand Up @@ -188,6 +188,10 @@ public void SwitchMode(string newMode)
}

CurrentMode = newMode;
if (newMode != "Desktop" && newMode != "Exiting")
{
BasisCursorManagement.UnlockCursorBypassChecks();
}
OnBootModeChanged?.Invoke(CurrentMode);

Debug.Log("Loading " + CurrentMode);
Expand Down Expand Up @@ -475,4 +479,4 @@ public void LoadAndOrSaveDefaultDeviceConfigs(string directoryPath)
UseAbleDeviceConfigs = deviceDictionary.Values.ToList();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using UnityEngine;
using System.Collections.Generic;
using System;
using Basis.Scripts.Device_Management;

public static class BasisCursorManagement
{
// A list to keep track of cursor lock requests
private static List<string> cursorLockRequests = new List<string>();
// Event that gets triggered whenever the cursor state changes
public static event Action<CursorLockMode, bool> OnCursorStateChange;

public static void OverrideableLock(string requestName)
{
Cursor.lockState = CursorLockMode.Locked;
Expand All @@ -22,12 +24,15 @@ public static bool IsVisible()
{
return Cursor.visible;
}

/// <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 (ShouldIgnoreCursorRequests()) return;

Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
Debug.Log("Cursor Locked");
Expand All @@ -39,6 +44,21 @@ public static void LockCursor(string requestName)
/// Removes a request to lock the cursor.
/// </summary>
public static void UnlockCursor(string requestName)
{
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;
Expand All @@ -52,9 +72,20 @@ public static void UnlockCursor(string requestName)
/// </summary>
public static void ConfineCursor(string requestName)
{
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 7b00fa9

Please sign in to comment.