Skip to content

Commit efbdb1f

Browse files
author
David Kline
authored
Merge pull request #2677 from StephenHodgson/vNEXT-SourceStateRefactor
Updated Source State Events to be more generic
2 parents 4d1b8cd + 014b01a commit efbdb1f

File tree

4 files changed

+101
-93
lines changed

4 files changed

+101
-93
lines changed

Assets/MixedRealityToolkit-SDK/Features/Input/Handlers/ControllerPoseSynchronizer.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,26 @@ public virtual void OnSourceLost(SourceStateEventData eventData)
119119
}
120120

121121
/// <inheritdoc />
122-
public virtual void OnSourcePoseChanged(SourcePoseEventData eventData)
122+
public virtual void OnSourcePoseChanged(SourcePoseEventData<TrackingState> eventData)
123+
{
124+
if (eventData.SourceData != TrackingState)
125+
{
126+
IsTracked = eventData.SourceData == TrackingState.Tracked;
127+
TrackingState = eventData.SourceData;
128+
}
129+
}
130+
131+
/// <inheritdoc />
132+
public virtual void OnSourcePoseChanged(SourcePoseEventData<Vector2> eventData) { }
133+
134+
/// <inheritdoc />
135+
public virtual void OnSourcePoseChanged(SourcePoseEventData<Vector3> eventData) { }
136+
137+
/// <inheritdoc />
138+
public virtual void OnSourcePoseChanged(SourcePoseEventData<Quaternion> eventData) { }
139+
140+
/// <inheritdoc />
141+
public virtual void OnSourcePoseChanged(SourcePoseEventData<MixedRealityPose> eventData)
123142
{
124143
if (Controller == null ||
125144
eventData.Controller == null ||
@@ -128,16 +147,10 @@ public virtual void OnSourcePoseChanged(SourcePoseEventData eventData)
128147
return;
129148
}
130149

131-
if (eventData.TrackingState != TrackingState)
132-
{
133-
IsTracked = eventData.TrackingState == TrackingState.Tracked;
134-
TrackingState = eventData.TrackingState;
135-
}
136-
137150
if (UseSourcePoseData && TrackingState == TrackingState.Tracked)
138151
{
139-
transform.localPosition = eventData.MixedRealityPose.Position;
140-
transform.localRotation = eventData.MixedRealityPose.Rotation;
152+
transform.localPosition = eventData.SourceData.Position;
153+
transform.localRotation = eventData.SourceData.Rotation;
141154
}
142155
}
143156

Assets/MixedRealityToolkit-SDK/Features/Input/MixedRealityInputManager.cs

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
using Microsoft.MixedReality.Toolkit.InputSystem.Sources;
54
using Microsoft.MixedReality.Toolkit.Core.Definitions.Devices;
65
using Microsoft.MixedReality.Toolkit.Core.Definitions.InputSystem;
76
using Microsoft.MixedReality.Toolkit.Core.Definitions.Utilities;
@@ -12,6 +11,7 @@
1211
using Microsoft.MixedReality.Toolkit.Core.Interfaces.InputSystem.Handlers;
1312
using Microsoft.MixedReality.Toolkit.Core.Managers;
1413
using Microsoft.MixedReality.Toolkit.Core.Utilities;
14+
using Microsoft.MixedReality.Toolkit.InputSystem.Sources;
1515
using System;
1616
using System.Collections.Generic;
1717
using UnityEngine;
@@ -50,7 +50,11 @@ public class MixedRealityInputManager : MixedRealityEventManager, IMixedRealityI
5050
private int disabledRefCount;
5151

5252
private SourceStateEventData sourceStateEventData;
53-
private SourcePoseEventData sourcePoseEventData;
53+
private SourcePoseEventData<TrackingState> sourceTrackingEventData;
54+
private SourcePoseEventData<Vector2> sourceVector2EventData;
55+
private SourcePoseEventData<Vector3> sourcePositionEventData;
56+
private SourcePoseEventData<Quaternion> sourceRotationEventData;
57+
private SourcePoseEventData<MixedRealityPose> sourcePoseEventData;
5458

5559
private FocusEventData focusEventData;
5660

@@ -180,7 +184,12 @@ private void InitializeInternal()
180184
}
181185

182186
sourceStateEventData = new SourceStateEventData(EventSystem.current);
183-
sourcePoseEventData = new SourcePoseEventData(EventSystem.current);
187+
188+
sourceTrackingEventData = new SourcePoseEventData<TrackingState>(EventSystem.current);
189+
sourceVector2EventData = new SourcePoseEventData<Vector2>(EventSystem.current);
190+
sourcePositionEventData = new SourcePoseEventData<Vector3>(EventSystem.current);
191+
sourceRotationEventData = new SourcePoseEventData<Quaternion>(EventSystem.current);
192+
sourcePoseEventData = new SourcePoseEventData<MixedRealityPose>(EventSystem.current);
184193

185194
focusEventData = new FocusEventData(EventSystem.current);
186195

@@ -570,42 +579,70 @@ public void RaiseSourceLost(IMixedRealityInputSource source, IMixedRealityContro
570579
public void RaiseSourceTrackingStateChanged(IMixedRealityInputSource source, IMixedRealityController controller, TrackingState state)
571580
{
572581
// Create input event
573-
sourcePoseEventData.Initialize(source, controller, state);
582+
sourceTrackingEventData.Initialize(source, controller, state);
574583

575584
// Pass handler through HandleEvent to perform modal/fallback logic
576-
HandleEvent(sourcePoseEventData, OnSourcePoseChangedEventHandler);
585+
HandleEvent(sourceTrackingEventData, OnSourceTrackingChangedEventHandler);
577586
}
578587

588+
private static readonly ExecuteEvents.EventFunction<IMixedRealitySourcePoseHandler> OnSourceTrackingChangedEventHandler =
589+
delegate (IMixedRealitySourcePoseHandler handler, BaseEventData eventData)
590+
{
591+
var casted = ExecuteEvents.ValidateEventData<SourcePoseEventData<TrackingState>>(eventData);
592+
handler.OnSourcePoseChanged(casted);
593+
};
594+
579595
/// <inheritdoc />
580596
public void RaiseSourcePositionChanged(IMixedRealityInputSource source, IMixedRealityController controller, Vector2 position)
581597
{
582598
// Create input event
583-
sourcePoseEventData.Initialize(source, controller, position);
599+
sourceVector2EventData.Initialize(source, controller, position);
584600

585601
// Pass handler through HandleEvent to perform modal/fallback logic
586-
HandleEvent(sourcePoseEventData, OnSourcePoseChangedEventHandler);
602+
HandleEvent(sourceVector2EventData, OnSourcePoseVector2ChangedEventHandler);
587603
}
588604

605+
private static readonly ExecuteEvents.EventFunction<IMixedRealitySourcePoseHandler> OnSourcePoseVector2ChangedEventHandler =
606+
delegate (IMixedRealitySourcePoseHandler handler, BaseEventData eventData)
607+
{
608+
var casted = ExecuteEvents.ValidateEventData<SourcePoseEventData<Vector2>>(eventData);
609+
handler.OnSourcePoseChanged(casted);
610+
};
611+
589612
/// <inheritdoc />
590613
public void RaiseSourcePositionChanged(IMixedRealityInputSource source, IMixedRealityController controller, Vector3 position)
591614
{
592615
// Create input event
593-
sourcePoseEventData.Initialize(source, controller, position);
616+
sourcePositionEventData.Initialize(source, controller, position);
594617

595618
// Pass handler through HandleEvent to perform modal/fallback logic
596-
HandleEvent(sourcePoseEventData, OnSourcePoseChangedEventHandler);
619+
HandleEvent(sourcePositionEventData, OnSourcePositionChangedEventHandler);
597620
}
598621

622+
private static readonly ExecuteEvents.EventFunction<IMixedRealitySourcePoseHandler> OnSourcePositionChangedEventHandler =
623+
delegate (IMixedRealitySourcePoseHandler handler, BaseEventData eventData)
624+
{
625+
var casted = ExecuteEvents.ValidateEventData<SourcePoseEventData<Vector3>>(eventData);
626+
handler.OnSourcePoseChanged(casted);
627+
};
628+
599629
/// <inheritdoc />
600630
public void RaiseSourceRotationChanged(IMixedRealityInputSource source, IMixedRealityController controller, Quaternion rotation)
601631
{
602632
// Create input event
603-
sourcePoseEventData.Initialize(source, controller, rotation);
633+
sourceRotationEventData.Initialize(source, controller, rotation);
604634

605635
// Pass handler through HandleEvent to perform modal/fallback logic
606-
HandleEvent(sourcePoseEventData, OnSourcePoseChangedEventHandler);
636+
HandleEvent(sourceRotationEventData, OnSourceRotationChangedEventHandler);
607637
}
608638

639+
private static readonly ExecuteEvents.EventFunction<IMixedRealitySourcePoseHandler> OnSourceRotationChangedEventHandler =
640+
delegate (IMixedRealitySourcePoseHandler handler, BaseEventData eventData)
641+
{
642+
var casted = ExecuteEvents.ValidateEventData<SourcePoseEventData<Quaternion>>(eventData);
643+
handler.OnSourcePoseChanged(casted);
644+
};
645+
609646
/// <inheritdoc />
610647
public void RaiseSourcePoseChanged(IMixedRealityInputSource source, IMixedRealityController controller, MixedRealityPose position)
611648
{
@@ -619,7 +656,7 @@ public void RaiseSourcePoseChanged(IMixedRealityInputSource source, IMixedRealit
619656
private static readonly ExecuteEvents.EventFunction<IMixedRealitySourcePoseHandler> OnSourcePoseChangedEventHandler =
620657
delegate (IMixedRealitySourcePoseHandler handler, BaseEventData eventData)
621658
{
622-
var casted = ExecuteEvents.ValidateEventData<SourcePoseEventData>(eventData);
659+
var casted = ExecuteEvents.ValidateEventData<SourcePoseEventData<MixedRealityPose>>(eventData);
623660
handler.OnSourcePoseChanged(casted);
624661
};
625662

Lines changed: 5 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
using Microsoft.MixedReality.Toolkit.Core.Definitions.Devices;
5-
using Microsoft.MixedReality.Toolkit.Core.Definitions.Utilities;
64
using Microsoft.MixedReality.Toolkit.Core.Interfaces.Devices;
75
using Microsoft.MixedReality.Toolkit.Core.Interfaces.InputSystem;
86
using UnityEngine;
@@ -14,90 +12,26 @@ namespace Microsoft.MixedReality.Toolkit.Core.EventDatum.Input
1412
/// Describes a source change event.
1513
/// <remarks>Source State events do not have an associated <see cref="Definitions.InputSystem.MixedRealityInputAction"/>.</remarks>
1614
/// </summary>
17-
public class SourcePoseEventData : SourceStateEventData
15+
public class SourcePoseEventData<T> : SourceStateEventData
1816
{
19-
/// <summary>
20-
/// The new tracking state of the input source.
21-
/// </summary>
22-
public TrackingState TrackingState { get; private set; } = TrackingState.NotTracked;
23-
2417
/// <summary>
2518
/// The new position of the input source.
2619
/// </summary>
27-
public Vector2 TwoDofPosition { get; private set; } = Vector2.zero;
28-
29-
/// <summary>
30-
/// The new position of the input source.
31-
/// </summary>
32-
public Vector3 ThreeDofPosition { get; private set; } = Vector3.zero;
33-
34-
/// <summary>
35-
/// The new rotation of the input source.
36-
/// </summary>
37-
public Quaternion ThreeDofRotation { get; private set; } = Quaternion.identity;
38-
39-
/// <summary>
40-
/// The new position and rotation of the input source.
41-
/// </summary>
42-
public MixedRealityPose MixedRealityPose { get; private set; } = MixedRealityPose.ZeroIdentity;
20+
public T SourceData { get; private set; }
4321

4422
/// <inheritdoc />
4523
public SourcePoseEventData(EventSystem eventSystem) : base(eventSystem) { }
4624

47-
public void Initialize(IMixedRealityInputSource inputSource, IMixedRealityController controller, TrackingState trackingState)
48-
{
49-
Initialize(inputSource, controller);
50-
TrackingState = trackingState;
51-
}
52-
53-
/// <summary>
54-
/// Populates the event with data.
55-
/// </summary>
56-
/// <param name="inputSource"></param>
57-
/// <param name="controller"></param>
58-
/// <param name="position"></param>
59-
public void Initialize(IMixedRealityInputSource inputSource, IMixedRealityController controller, Vector2 position)
60-
{
61-
Initialize(inputSource, controller);
62-
TwoDofPosition = position;
63-
}
64-
65-
/// <summary>
66-
/// Populates the event with data.
67-
/// </summary>
68-
/// <param name="inputSource"></param>
69-
/// <param name="controller"></param>
70-
/// <param name="position"></param>
71-
public void Initialize(IMixedRealityInputSource inputSource, IMixedRealityController controller, Vector3 position)
72-
{
73-
Initialize(inputSource, controller);
74-
ThreeDofPosition = position;
75-
}
76-
77-
/// <summary>
78-
/// Populates the event with data.
79-
/// </summary>
80-
/// <param name="inputSource"></param>
81-
/// <param name="controller"></param>
82-
/// <param name="rotation"></param>
83-
public void Initialize(IMixedRealityInputSource inputSource, IMixedRealityController controller, Quaternion rotation)
84-
{
85-
Initialize(inputSource, controller);
86-
ThreeDofRotation = rotation;
87-
}
88-
8925
/// <summary>
9026
/// Populates the event with data.
9127
/// </summary>
9228
/// <param name="inputSource"></param>
9329
/// <param name="controller"></param>
94-
/// <param name="pose"></param>
95-
public void Initialize(IMixedRealityInputSource inputSource, IMixedRealityController controller, MixedRealityPose pose)
30+
/// <param name="data"></param>
31+
public void Initialize(IMixedRealityInputSource inputSource, IMixedRealityController controller, T data)
9632
{
9733
Initialize(inputSource, controller);
98-
ThreeDofPosition = pose.Position;
99-
ThreeDofRotation = pose.Rotation;
100-
MixedRealityPose = pose;
34+
SourceData = data;
10135
}
10236
}
10337
}
Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4+
using Microsoft.MixedReality.Toolkit.Core.Definitions.Devices;
5+
using Microsoft.MixedReality.Toolkit.Core.Definitions.Utilities;
46
using Microsoft.MixedReality.Toolkit.Core.EventDatum.Input;
7+
using UnityEngine;
58

69
namespace Microsoft.MixedReality.Toolkit.Core.Interfaces.InputSystem.Handlers
710
{
@@ -10,9 +13,30 @@ namespace Microsoft.MixedReality.Toolkit.Core.Interfaces.InputSystem.Handlers
1013
/// </summary>
1114
public interface IMixedRealitySourcePoseHandler : IMixedRealitySourceStateHandler
1215
{
16+
/// <summary>
17+
/// Raised when the source pose tracking state is changed.
18+
/// </summary>
19+
/// <param name="eventData"></param>
20+
void OnSourcePoseChanged(SourcePoseEventData<TrackingState> eventData);
21+
22+
/// <summary>
23+
/// Raised when the source position is changed.
24+
/// </summary>
25+
void OnSourcePoseChanged(SourcePoseEventData<Vector2> eventData);
26+
1327
/// <summary>
1428
/// Raised when the source position is changed.
1529
/// </summary>
16-
void OnSourcePoseChanged(SourcePoseEventData eventData);
30+
void OnSourcePoseChanged(SourcePoseEventData<Vector3> eventData);
31+
32+
/// <summary>
33+
/// Raised when the source rotation is changed.
34+
/// </summary>
35+
void OnSourcePoseChanged(SourcePoseEventData<Quaternion> eventData);
36+
37+
/// <summary>
38+
/// Raised when the source pose is changed.
39+
/// </summary>
40+
void OnSourcePoseChanged(SourcePoseEventData<MixedRealityPose> eventData);
1741
}
1842
}

0 commit comments

Comments
 (0)