Skip to content

Commit 6b32cbe

Browse files
committed
Also expose current focus state for HandsProvider
1 parent 2f1d7a8 commit 6b32cbe

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

org.mixedrealitytoolkit.input/MRTK.Input.asmdef

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
"name": "com.unity.xr.openxr",
5151
"expression": "",
5252
"define": "UNITY_OPENXR_PRESENT"
53+
},
54+
{
55+
"name": "com.qualcomm.snapdragon.spaces",
56+
"expression": "",
57+
"define": "SNAPDRAGON_SPACES_PRESENT"
5358
}
5459
],
5560
"noEngineReferences": false

org.mixedrealitytoolkit.input/Subsystems/Hands/HandsProvider.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,11 @@ public override bool TryGetEntireHand(XRNode handNode, out IReadOnlyList<HandJoi
6161
{
6262
Debug.Assert(handNode == XRNode.LeftHand || handNode == XRNode.RightHand, "Non-hand XRNode used in TryGetEntireHand query.");
6363

64-
#if !UNITY_EDITOR
65-
if (!Application.isFocused)
64+
if (!MRTKInputFocusManager.HasFocus)
6665
{
6766
jointPoses = Array.Empty<HandJointPose>();
6867
return false;
6968
}
70-
#endif // !UNITY_EDITOR
7169

7270
return hands[handNode].TryGetEntireHand(out jointPoses);
7371
}
@@ -77,13 +75,11 @@ public override bool TryGetJoint(TrackedHandJoint joint, XRNode handNode, out Ha
7775
{
7876
Debug.Assert(handNode == XRNode.LeftHand || handNode == XRNode.RightHand, "Non-hand XRNode used in TryGetJoint query.");
7977

80-
#if !UNITY_EDITOR
81-
if (!Application.isFocused)
78+
if (!MRTKInputFocusManager.HasFocus)
8279
{
8380
jointPose = default;
8481
return false;
8582
}
86-
#endif // !UNITY_EDITOR
8783

8884
return hands[handNode].TryGetJoint(joint, out jointPose);
8985
}

org.mixedrealitytoolkit.input/Utilities/MRTKInputFocusManager.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Mixed Reality Toolkit Contributors
22
// Licensed under the BSD 3-Clause
33

4+
using System.Collections.Generic;
45
using UnityEngine;
56
using UnityEngine.Events;
67
using UnityEngine.InputSystem;
@@ -20,6 +21,18 @@ public sealed class MRTKInputFocusManager : MonoBehaviour
2021
/// </summary>
2122
public static UnityEvent<bool> OnXrSessionFocus { get; } = new UnityEvent<bool>();
2223

24+
/// <summary>
25+
/// Whether the current XrSession has focus or not.
26+
/// </summary>
27+
public static bool HasFocus =>
28+
#if UNITY_EDITOR
29+
true;
30+
#elif SNAPDRAGON_SPACES_PRESENT
31+
lastSessionState == 5;
32+
#else
33+
Application.isFocused;
34+
#endif
35+
2336
/// <summary>
2437
/// We want to ensure we're focused for input, as some runtimes continue reporting "tracked" while pose updates are paused.
2538
/// This is allowed, per-spec, as a "should": "Runtimes should make input actions inactive while the application is unfocused,
@@ -43,15 +56,25 @@ private void OnFocusChange(bool focus)
4356
}
4457
}
4558

46-
#if USING_SNAPDRAGON_SPACES_SDK
59+
#if SNAPDRAGON_SPACES_PRESENT
60+
private static readonly List<Qualcomm.Snapdragon.Spaces.SpacesOpenXRFeature> featureList = new();
61+
private static int lastSessionState = -1;
4762
private Qualcomm.Snapdragon.Spaces.SpacesOpenXRFeature spacesOpenXRFeature = null;
48-
private int lastSessionState = -1;
4963

5064
private void Update()
5165
{
5266
if (spacesOpenXRFeature == null)
5367
{
54-
spacesOpenXRFeature = UnityEngine.XR.OpenXR.OpenXRSettings.Instance.GetFeature<Qualcomm.Snapdragon.Spaces.SpacesOpenXRFeature>();
68+
int count = UnityEngine.XR.OpenXR.OpenXRSettings.Instance.GetFeatures(featureList);
69+
for (int i = 0; i < count; i++)
70+
{
71+
Qualcomm.Snapdragon.Spaces.SpacesOpenXRFeature feature = featureList[i];
72+
if (feature != null && feature.enabled)
73+
{
74+
spacesOpenXRFeature = feature;
75+
break;
76+
}
77+
}
5578
}
5679

5780
// XrSessionState maps better to this behavior than OnApplicationFocus but isn't

0 commit comments

Comments
 (0)