Skip to content

Commit ca4b32f

Browse files
Added mouse controller intelligent switching.
1 parent c0f4243 commit ca4b32f

File tree

7 files changed

+145
-1
lines changed

7 files changed

+145
-1
lines changed

Assets/MixedRealityToolkit-SDK/Features/UX/Prefabs/Pointers/MousePointer.prefab

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ MonoBehaviour:
6060
axisConstraint: 0
6161
cursorPrefab: {fileID: 1151083198953756, guid: 667821d88830305449757690d22f71e0,
6262
type: 2}
63-
disableCursorOnStart: 0
63+
disableCursorOnStart: 1
6464
setCursorVisibilityOnSourceDetected: 0
6565
raycastOrigin: {fileID: 0}
6666
activeHoldAction:
@@ -75,3 +75,6 @@ MonoBehaviour:
7575
overrideGlobalPointerExtent: 0
7676
pointerExtent: 10
7777
pointerOrientation: 0
78+
hideCursorWhenInactive: 1
79+
movementThresholdToUnHide: 0.1
80+
hideTimeout: 3

Assets/MixedRealityToolkit-SDK/Features/UX/Scripts/Cursors/BaseCursor.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ public virtual void SetVisibility(bool visible)
110110
}
111111
}
112112

113+
/// <inheritdoc />
114+
public bool IsVisible => PrimaryCursorVisual != null ? PrimaryCursorVisual.gameObject.activeInHierarchy : gameObject.activeInHierarchy;
115+
113116
/// <inheritdoc />
114117
public bool SetVisibilityOnSourceDetected { get; set; } = false;
115118

Assets/MixedRealityToolkit-SDK/Features/UX/Scripts/Pointers/MousePointer.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,35 @@ namespace Microsoft.MixedReality.Toolkit.SDK.UX.Pointers
1717
/// </summary>
1818
public class MousePointer : BaseControllerPointer, IMixedRealityMousePointer
1919
{
20+
[SerializeField]
21+
[Tooltip("Should the mouse cursor be hidden when no active input is received?")]
22+
private bool hideCursorWhenInactive = true;
23+
24+
/// <inheritdoc />
25+
public bool HideCursorWhenInactive => hideCursorWhenInactive;
26+
27+
[SerializeField]
28+
[Range(0.01f, 1f)]
29+
[Tooltip("What is the movement threshold to reach before un-hiding mouse cursor?")]
30+
private float movementThresholdToUnHide = 0.1f;
31+
32+
/// <inheritdoc />
33+
public float MovementThresholdToUnHide => movementThresholdToUnHide;
34+
35+
[SerializeField]
36+
[Range(0f, 10f)]
37+
[Tooltip("How long should it take before the mouse cursor is hidden?")]
38+
private float hideTimeout = 3.0f;
39+
40+
/// <inheritdoc />
41+
public float HideTimeout => hideTimeout;
42+
43+
private float timeoutTimer;
44+
2045
private bool isInteractionEnabled = false;
2146

47+
private bool cursorWasDisabledOnDown = false;
48+
2249
/// <inheritdoc />
2350
public override bool IsInteractionEnabled => isInteractionEnabled;
2451

@@ -66,6 +93,14 @@ public override void OnSourcePoseChanged(SourcePoseEventData<Vector2> eventData)
6693

6794
if (UseSourcePoseData)
6895
{
96+
if (!BaseCursor.IsVisible &&
97+
(eventData.SourceData.x >= movementThresholdToUnHide ||
98+
eventData.SourceData.y >= MovementThresholdToUnHide))
99+
{
100+
BaseCursor?.SetVisibility(true);
101+
transform.rotation = CameraCache.Main.transform.rotation;
102+
}
103+
69104
var newRotation = Vector3.zero;
70105
newRotation.x += eventData.SourceData.y;
71106
newRotation.y += eventData.SourceData.x;
@@ -81,8 +116,17 @@ public override void OnPositionInputChanged(InputEventData<Vector2> eventData)
81116
if (!UseSourcePoseData &&
82117
PoseAction == eventData.MixedRealityInputAction)
83118
{
119+
if (!BaseCursor.IsVisible &&
120+
(eventData.InputData.x >= movementThresholdToUnHide ||
121+
eventData.InputData.y >= MovementThresholdToUnHide))
122+
{
123+
BaseCursor?.SetVisibility(true);
124+
transform.rotation = CameraCache.Main.transform.rotation;
125+
}
126+
84127
IsTracked = true;
85128
TrackingState = TrackingState.Tracked;
129+
86130
var newRotation = Vector3.zero;
87131
newRotation.x += eventData.InputData.x;
88132
newRotation.y += eventData.InputData.y;
@@ -91,6 +135,31 @@ public override void OnPositionInputChanged(InputEventData<Vector2> eventData)
91135
}
92136
}
93137

138+
/// <inheritdoc />
139+
public override void OnInputDown(InputEventData eventData)
140+
{
141+
cursorWasDisabledOnDown = !BaseCursor.IsVisible;
142+
143+
if (cursorWasDisabledOnDown)
144+
{
145+
BaseCursor?.SetVisibility(true);
146+
transform.rotation = CameraCache.Main.transform.rotation;
147+
}
148+
else
149+
{
150+
base.OnInputDown(eventData);
151+
}
152+
}
153+
154+
/// <inheritdoc />
155+
public override void OnInputUp(InputEventData eventData)
156+
{
157+
if (BaseCursor.IsVisible && !cursorWasDisabledOnDown)
158+
{
159+
base.OnInputUp(eventData);
160+
}
161+
}
162+
94163
protected override void Start()
95164
{
96165
base.Start();
@@ -110,6 +179,19 @@ protected override void Start()
110179
}
111180
}
112181

182+
private void Update()
183+
{
184+
if (!isInteractionEnabled) { return; }
185+
186+
timeoutTimer += Time.unscaledDeltaTime;
187+
188+
if (timeoutTimer >= hideTimeout)
189+
{
190+
timeoutTimer = 0.0f;
191+
BaseCursor?.SetVisibility(false);
192+
}
193+
}
194+
113195
/// <inheritdoc />
114196
public override void OnSourceDetected(SourceStateEventData eventData)
115197
{

Assets/MixedRealityToolkit-SDK/Inspectors/UX/Pointers/BaseControllerPointerInspector.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public override void OnInspectorGUI()
5151

5252
if (basePointerFoldout)
5353
{
54+
EditorGUI.indentLevel++;
55+
5456
EditorGUILayout.PropertyField(cursorPrefab);
5557
EditorGUILayout.PropertyField(disableCursorOnStart);
5658
EditorGUILayout.PropertyField(setCursorVisibilityOnSourceDetected);
@@ -69,7 +71,9 @@ public override void OnInspectorGUI()
6971
}
7072
}
7173

74+
EditorGUI.indentLevel--;
7275
}
76+
7377
serializedObject.ApplyModifiedProperties();
7478
}
7579
}

Assets/MixedRealityToolkit-SDK/Inspectors/UX/Pointers/MousePointerInspector.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,43 @@ namespace Microsoft.MixedReality.Toolkit.SDK.Inspectors.UX.Pointers
99
[CustomEditor(typeof(MousePointer))]
1010
public class MousePointerInspector : BaseControllerPointerInspector
1111
{
12+
private SerializedProperty hideCursorWhenInactive;
13+
private SerializedProperty movementThresholdToUnHide;
14+
private SerializedProperty hideTimeout;
15+
private bool mousePointerFoldout = true;
16+
1217
protected override void OnEnable()
1318
{
1419
DrawBasePointerActions = false;
1520
base.OnEnable();
21+
22+
hideCursorWhenInactive = serializedObject.FindProperty("hideCursorWhenInactive");
23+
movementThresholdToUnHide = serializedObject.FindProperty("movementThresholdToUnHide");
24+
hideTimeout = serializedObject.FindProperty("hideTimeout");
25+
}
26+
27+
public override void OnInspectorGUI()
28+
{
29+
base.OnInspectorGUI();
30+
serializedObject.Update();
31+
32+
mousePointerFoldout = EditorGUILayout.Foldout(mousePointerFoldout, "Mouse Pointer Settings", true);
33+
34+
if (mousePointerFoldout)
35+
{
36+
EditorGUI.indentLevel++;
37+
EditorGUILayout.PropertyField(hideCursorWhenInactive);
38+
39+
if (hideCursorWhenInactive.boolValue)
40+
{
41+
EditorGUILayout.PropertyField(movementThresholdToUnHide);
42+
EditorGUILayout.PropertyField(hideTimeout);
43+
}
44+
45+
EditorGUI.indentLevel--;
46+
}
47+
48+
serializedObject.ApplyModifiedProperties();
1649
}
1750
}
1851
}

Assets/MixedRealityToolkit/_Core/Interfaces/InputSystem/IMixedRealityCursor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public interface IMixedRealityCursor : IMixedRealityFocusChangedHandler, IMixedR
4242
/// <param name="visible">True if cursor should be visible, false if not.</param>
4343
void SetVisibility(bool visible);
4444

45+
/// <summary>
46+
/// Is the cursor currently visible?
47+
/// </summary>
48+
bool IsVisible { get; }
49+
4550
/// <summary>
4651
/// Sets the visibility of the <see cref="IMixedRealityCursor"/> when the source is detected.
4752
/// </summary>

Assets/MixedRealityToolkit/_Core/Interfaces/InputSystem/IMixedRealityMousePointer.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,19 @@ namespace Microsoft.MixedReality.Toolkit.Core.Interfaces.InputSystem
88
/// </summary>
99
public interface IMixedRealityMousePointer : IMixedRealityPointer
1010
{
11+
/// <summary>
12+
/// Should the mouse cursor be hidden when no active input is received?
13+
/// </summary>
14+
bool HideCursorWhenInactive { get; }
15+
16+
/// <summary>
17+
/// What is the movement threshold to reach before un-hiding mouse cursor?
18+
/// </summary>
19+
float MovementThresholdToUnHide { get; }
20+
21+
/// <summary>
22+
/// How long should it take before the mouse cursor is hidden?
23+
/// </summary>
24+
float HideTimeout { get; }
1125
}
1226
}

0 commit comments

Comments
 (0)