22// Licensed under the MIT License. See LICENSE in the project root for license information.
33
44using Microsoft . MixedReality . Toolkit . Internal . Definitions . Devices ;
5+ using Microsoft . MixedReality . Toolkit . Internal . Definitions . InputSystem ;
56using Microsoft . MixedReality . Toolkit . Internal . Definitions . Utilities ;
67using Microsoft . MixedReality . Toolkit . Internal . EventDatum . Input ;
7- using Microsoft . MixedReality . Toolkit . Internal . Extensions ;
8- using Microsoft . MixedReality . Toolkit . Internal . Interfaces ;
8+ using Microsoft . MixedReality . Toolkit . Internal . Interfaces . Devices ;
99using Microsoft . MixedReality . Toolkit . Internal . Interfaces . InputSystem . Handlers ;
1010using UnityEngine ;
1111
@@ -14,37 +14,22 @@ namespace Microsoft.MixedReality.Toolkit.SDK.Input.Handlers
1414 /// <summary>
1515 /// Waits for a controller to be initialized, then synchronizes its transform position to a specified handedness.
1616 /// </summary>
17- public class ControllerPoseSynchronizer : InputSystemGlobalListener , IMixedRealitySourcePoseHandler
17+ public class ControllerPoseSynchronizer : InputSystemGlobalListener , IMixedRealitySourcePoseHandler , IMixedRealityControllerPoseSynchronizer
1818 {
19+ #region IMixedRealityControllerPoseSynchronizer Implementation
20+
1921 [ SerializeField ]
2022 [ Tooltip ( "The handedness this controller should synchronize with." ) ]
2123 private Handedness handedness = Handedness . Left ;
2224
23- /// <summary>
24- /// The handedness this controller should synchronize with.
25- /// </summary>
25+ /// <inheritdoc />
2626 public Handedness Handedness => handedness ;
2727
2828 [ SerializeField ]
29- [ Tooltip ( "Disables child GameObjects when the controller source is lost." ) ]
30- private bool disableChildren = true ;
31-
32- /// <summary>
33- ///Disables child <see cref="GameObject"/>s the controller source is lost.
34- /// </summary>
35- public bool DisableChildren
36- {
37- get { return disableChildren ; }
38- set { disableChildren = value ; }
39- }
40-
41- [ SerializeField ]
42- [ Tooltip ( "Should this GameObject clean itself up after source is lost?" ) ]
29+ [ Tooltip ( "Should this GameObject clean itself up when it's controller is lost?" ) ]
4330 private bool destroyOnSourceLost = true ;
4431
45- /// <summary>
46- /// Should this GameObject clean itself up after source is lost?
47- /// </summary>
32+ /// <inheritdoc />
4833 public bool DestroyOnSourceLost
4934 {
5035 get { return destroyOnSourceLost ; }
@@ -61,44 +46,49 @@ public bool DestroyOnSourceLost
6146 /// </summary>
6247 protected TrackingState TrackingState = TrackingState . NotTracked ;
6348
64- /// <summary>
65- /// The currently assigned Controller.
66- /// </summary >
49+ private IMixedRealityController controller ;
50+
51+ /// <inheritdoc / >
6752 public virtual IMixedRealityController Controller
6853 {
6954 get { return controller ; }
7055 set
7156 {
7257 handedness = value . ControllerHandedness ;
7358 controller = value ;
59+ gameObject . name = $ "{ handedness } _{ gameObject . name } ";
7460 }
7561 }
7662
77- private IMixedRealityController controller ;
78-
79- #region IMixedRealitySourcePoseHandler Implementation
63+ [ SerializeField ]
64+ [ Tooltip ( "Should the Transform's position be driven from the source pose or from input handler?" ) ]
65+ private bool useSourcePoseData = true ;
8066
8167 /// <inheritdoc />
82- public virtual void OnSourceDetected ( SourceStateEventData eventData )
68+ public bool UseSourcePoseData
8369 {
84- if ( Controller == null ||
85- eventData . Controller == null ||
86- eventData . Controller . InputSource . SourceId != Controller . InputSource . SourceId )
87- {
88- return ;
89- }
70+ get { return useSourcePoseData ; }
71+ set { useSourcePoseData = value ; }
72+ }
9073
91- if ( eventData . Controller . ControllerHandedness == Handedness &&
92- eventData . Controller . InputSource . SourceId == Controller . InputSource . SourceId )
93- {
74+ [ SerializeField ]
75+ [ Tooltip ( "The input action that will drive the Transform's pose, position, or rotation." ) ]
76+ private MixedRealityInputAction poseAction = MixedRealityInputAction . None ;
9477
95- if ( disableChildren )
96- {
97- gameObject . SetChildrenActive ( true ) ;
98- }
99- }
78+ /// <inheritdoc />
79+ public MixedRealityInputAction PoseAction
80+ {
81+ get { return poseAction ; }
82+ set { poseAction = value ; }
10083 }
10184
85+ #endregion IMixedRealityControllerPoseSynchronizer Implementation
86+
87+ #region IMixedRealitySourcePoseHandler Implementation
88+
89+ /// <inheritdoc />
90+ public virtual void OnSourceDetected ( SourceStateEventData eventData ) { }
91+
10292 /// <inheritdoc />
10393 public virtual void OnSourceLost ( SourceStateEventData eventData )
10494 {
@@ -114,11 +104,6 @@ public virtual void OnSourceLost(SourceStateEventData eventData)
114104 IsTracked = false ;
115105 TrackingState = TrackingState . NotTracked ;
116106
117- if ( disableChildren )
118- {
119- gameObject . SetChildrenActive ( false ) ;
120- }
121-
122107 if ( destroyOnSourceLost )
123108 {
124109 if ( Application . isEditor )
@@ -149,13 +134,75 @@ public virtual void OnSourcePoseChanged(SourcePoseEventData eventData)
149134 TrackingState = eventData . TrackingState ;
150135 }
151136
152- if ( TrackingState == TrackingState . Tracked )
137+ if ( UseSourcePoseData && TrackingState == TrackingState . Tracked )
153138 {
154139 transform . localPosition = eventData . MixedRealityPose . Position ;
155140 transform . localRotation = eventData . MixedRealityPose . Rotation ;
156141 }
157142 }
158143
159144 #endregion IMixedRealitySourcePoseHandler Implementation
145+
146+ #region IMixedRealityInputHandler Implementation
147+
148+ public virtual void OnInputUp ( InputEventData eventData ) { }
149+
150+ public virtual void OnInputDown ( InputEventData eventData ) { }
151+
152+ public virtual void OnInputPressed ( InputEventData < float > eventData ) { }
153+
154+ public virtual void OnPositionInputChanged ( InputEventData < Vector2 > eventData ) { }
155+
156+ #endregion IMixedRealityInputHandler Implementation
157+
158+ #region IMixedRealitySpatialInputHandler Implementation
159+
160+ /// <inheritdoc />
161+ public virtual void OnPositionChanged ( InputEventData < Vector3 > eventData )
162+ {
163+ if ( eventData . SourceId == Controller ? . InputSource . SourceId )
164+ {
165+ if ( ! UseSourcePoseData &&
166+ PoseAction == eventData . MixedRealityInputAction )
167+ {
168+ IsTracked = true ;
169+ TrackingState = TrackingState . Tracked ;
170+ transform . localPosition = eventData . InputData ;
171+ }
172+ }
173+ }
174+
175+ /// <inheritdoc />
176+ public virtual void OnRotationChanged ( InputEventData < Quaternion > eventData )
177+ {
178+ if ( eventData . SourceId == Controller ? . InputSource . SourceId )
179+ {
180+ if ( ! UseSourcePoseData &&
181+ PoseAction == eventData . MixedRealityInputAction )
182+ {
183+ IsTracked = true ;
184+ TrackingState = TrackingState . Tracked ;
185+ transform . localRotation = eventData . InputData ;
186+ }
187+ }
188+ }
189+
190+ /// <inheritdoc />
191+ public virtual void OnPoseInputChanged ( InputEventData < MixedRealityPose > eventData )
192+ {
193+ if ( eventData . SourceId == Controller ? . InputSource . SourceId )
194+ {
195+ if ( ! UseSourcePoseData &&
196+ PoseAction == eventData . MixedRealityInputAction )
197+ {
198+ IsTracked = true ;
199+ TrackingState = TrackingState . Tracked ;
200+ transform . localPosition = eventData . InputData . Position ;
201+ transform . localRotation = eventData . InputData . Rotation ;
202+ }
203+ }
204+ }
205+
206+ #endregion IMixedRealitySpatialInputHandler Implementation
160207 }
161208}
0 commit comments