Skip to content

Commit ba82911

Browse files
Updated Source State Events to be more generic
1 parent aa6c97c commit ba82911

File tree

4 files changed

+93
-86
lines changed

4 files changed

+93
-86
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,19 @@ 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+
/// <inheritdoc />
125+
public virtual void OnSourcePoseChanged(SourcePoseEventData<Vector2> eventData) { }
126+
127+
/// <inheritdoc />
128+
public virtual void OnSourcePoseChanged(SourcePoseEventData<Vector3> eventData) { }
129+
130+
/// <inheritdoc />
131+
public virtual void OnSourcePoseChanged(SourcePoseEventData<Quaternion> eventData) { }
132+
133+
/// <inheritdoc />
134+
public virtual void OnSourcePoseChanged(SourcePoseEventData<MixedRealityPose> eventData)
123135
{
124136
if (Controller == null ||
125137
eventData.Controller == null ||
@@ -136,8 +148,8 @@ public virtual void OnSourcePoseChanged(SourcePoseEventData eventData)
136148

137149
if (UseSourcePoseData && TrackingState == TrackingState.Tracked)
138150
{
139-
transform.localPosition = eventData.MixedRealityPose.Position;
140-
transform.localRotation = eventData.MixedRealityPose.Rotation;
151+
transform.localPosition = eventData.SourceData.Position;
152+
transform.localRotation = eventData.SourceData.Rotation;
141153
}
142154
}
143155

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

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -184,7 +188,12 @@ private void InitializeInternal()
184188
}
185189

186190
sourceStateEventData = new SourceStateEventData(EventSystem.current);
187-
sourcePoseEventData = new SourcePoseEventData(EventSystem.current);
191+
192+
sourceTrackingEventData = new SourcePoseEventData<TrackingState>(EventSystem.current);
193+
sourceVector2EventData = new SourcePoseEventData<Vector2>(EventSystem.current);
194+
sourcePositionEventData = new SourcePoseEventData<Vector3>(EventSystem.current);
195+
sourceRotationEventData = new SourcePoseEventData<Quaternion>(EventSystem.current);
196+
sourcePoseEventData = new SourcePoseEventData<MixedRealityPose>(EventSystem.current);
188197

189198
focusEventData = new FocusEventData(EventSystem.current);
190199

@@ -578,42 +587,70 @@ public void RaiseSourceLost(IMixedRealityInputSource source, IMixedRealityContro
578587
public void RaiseSourceTrackingStateChanged(IMixedRealityInputSource source, IMixedRealityController controller, TrackingState state)
579588
{
580589
// Create input event
581-
sourcePoseEventData.Initialize(source, controller, state);
590+
sourceTrackingEventData.Initialize(source, controller, state);
582591

583592
// Pass handler through HandleEvent to perform modal/fallback logic
584-
HandleEvent(sourcePoseEventData, OnSourcePoseChangedEventHandler);
593+
HandleEvent(sourceTrackingEventData, OnSourceTrackingChangedEventHandler);
585594
}
586595

596+
private static readonly ExecuteEvents.EventFunction<IMixedRealitySourcePoseHandler> OnSourceTrackingChangedEventHandler =
597+
delegate (IMixedRealitySourcePoseHandler handler, BaseEventData eventData)
598+
{
599+
var casted = ExecuteEvents.ValidateEventData<SourcePoseEventData<TrackingState>>(eventData);
600+
handler.OnSourcePoseChanged(casted);
601+
};
602+
587603
/// <inheritdoc />
588604
public void RaiseSourcePositionChanged(IMixedRealityInputSource source, IMixedRealityController controller, Vector2 position)
589605
{
590606
// Create input event
591-
sourcePoseEventData.Initialize(source, controller, position);
607+
sourceVector2EventData.Initialize(source, controller, position);
592608

593609
// Pass handler through HandleEvent to perform modal/fallback logic
594-
HandleEvent(sourcePoseEventData, OnSourcePoseChangedEventHandler);
610+
HandleEvent(sourceVector2EventData, OnSourcePoseVector2ChangedEventHandler);
595611
}
596612

613+
private static readonly ExecuteEvents.EventFunction<IMixedRealitySourcePoseHandler> OnSourcePoseVector2ChangedEventHandler =
614+
delegate (IMixedRealitySourcePoseHandler handler, BaseEventData eventData)
615+
{
616+
var casted = ExecuteEvents.ValidateEventData<SourcePoseEventData<Vector2>>(eventData);
617+
handler.OnSourcePoseChanged(casted);
618+
};
619+
597620
/// <inheritdoc />
598621
public void RaiseSourcePositionChanged(IMixedRealityInputSource source, IMixedRealityController controller, Vector3 position)
599622
{
600623
// Create input event
601-
sourcePoseEventData.Initialize(source, controller, position);
624+
sourcePositionEventData.Initialize(source, controller, position);
602625

603626
// Pass handler through HandleEvent to perform modal/fallback logic
604-
HandleEvent(sourcePoseEventData, OnSourcePoseChangedEventHandler);
627+
HandleEvent(sourcePositionEventData, OnSourcePositionChangedEventHandler);
605628
}
606629

630+
private static readonly ExecuteEvents.EventFunction<IMixedRealitySourcePoseHandler> OnSourcePositionChangedEventHandler =
631+
delegate (IMixedRealitySourcePoseHandler handler, BaseEventData eventData)
632+
{
633+
var casted = ExecuteEvents.ValidateEventData<SourcePoseEventData<Vector3>>(eventData);
634+
handler.OnSourcePoseChanged(casted);
635+
};
636+
607637
/// <inheritdoc />
608638
public void RaiseSourceRotationChanged(IMixedRealityInputSource source, IMixedRealityController controller, Quaternion rotation)
609639
{
610640
// Create input event
611-
sourcePoseEventData.Initialize(source, controller, rotation);
641+
sourceRotationEventData.Initialize(source, controller, rotation);
612642

613643
// Pass handler through HandleEvent to perform modal/fallback logic
614-
HandleEvent(sourcePoseEventData, OnSourcePoseChangedEventHandler);
644+
HandleEvent(sourceRotationEventData, OnSourceRotationChangedEventHandler);
615645
}
616646

647+
private static readonly ExecuteEvents.EventFunction<IMixedRealitySourcePoseHandler> OnSourceRotationChangedEventHandler =
648+
delegate (IMixedRealitySourcePoseHandler handler, BaseEventData eventData)
649+
{
650+
var casted = ExecuteEvents.ValidateEventData<SourcePoseEventData<Quaternion>>(eventData);
651+
handler.OnSourcePoseChanged(casted);
652+
};
653+
617654
/// <inheritdoc />
618655
public void RaiseSourcePoseChanged(IMixedRealityInputSource source, IMixedRealityController controller, MixedRealityPose position)
619656
{
@@ -627,7 +664,7 @@ public void RaiseSourcePoseChanged(IMixedRealityInputSource source, IMixedRealit
627664
private static readonly ExecuteEvents.EventFunction<IMixedRealitySourcePoseHandler> OnSourcePoseChangedEventHandler =
628665
delegate (IMixedRealitySourcePoseHandler handler, BaseEventData eventData)
629666
{
630-
var casted = ExecuteEvents.ValidateEventData<SourcePoseEventData>(eventData);
667+
var casted = ExecuteEvents.ValidateEventData<SourcePoseEventData<MixedRealityPose>>(eventData);
631668
handler.OnSourcePoseChanged(casted);
632669
};
633670

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)