@@ -17,34 +17,42 @@ namespace Microsoft.MixedReality.Toolkit.SDK.UX.Pointers
1717 /// </summary>
1818 public class MousePointer : BaseControllerPointer , IMixedRealityMousePointer
1919 {
20+ private float timeoutTimer = 0.0f ;
21+
22+ private bool isInteractionEnabled = false ;
23+
24+ private bool cursorWasDisabledOnDown = false ;
25+
26+ private bool isDisabled = true ;
27+
28+ #region IMixedRealityMousePointer Implementaiton
29+
2030 [ SerializeField ]
2131 [ Tooltip ( "Should the mouse cursor be hidden when no active input is received?" ) ]
2232 private bool hideCursorWhenInactive = true ;
2333
2434 /// <inheritdoc />
25- public bool HideCursorWhenInactive => hideCursorWhenInactive ;
35+ bool IMixedRealityMousePointer . HideCursorWhenInactive => hideCursorWhenInactive ;
2636
2737 [ SerializeField ]
2838 [ Range ( 0.01f , 1f ) ]
2939 [ Tooltip ( "What is the movement threshold to reach before un-hiding mouse cursor?" ) ]
3040 private float movementThresholdToUnHide = 0.1f ;
3141
3242 /// <inheritdoc />
33- public float MovementThresholdToUnHide => movementThresholdToUnHide ;
43+ float IMixedRealityMousePointer . MovementThresholdToUnHide => movementThresholdToUnHide ;
3444
3545 [ SerializeField ]
3646 [ Range ( 0f , 10f ) ]
3747 [ Tooltip ( "How long should it take before the mouse cursor is hidden?" ) ]
3848 private float hideTimeout = 3.0f ;
3949
4050 /// <inheritdoc />
41- public float HideTimeout => hideTimeout ;
51+ float IMixedRealityMousePointer . HideTimeout => hideTimeout ;
4252
43- private float timeoutTimer ;
53+ #endregion IMixedRealityMousePointer Implementation
4454
45- private bool isInteractionEnabled = false ;
46-
47- private bool cursorWasDisabledOnDown = false ;
55+ #region IMixedRealityPointer Implementaiton
4856
4957 /// <inheritdoc />
5058 public override bool IsInteractionEnabled => isInteractionEnabled ;
@@ -82,61 +90,57 @@ public override void OnPreRaycast()
8290 }
8391 }
8492
85- public override void OnSourcePoseChanged ( SourcePoseEventData < Vector2 > eventData )
93+ #endregion IMixedRealityPointer Implementaiton
94+
95+ #region IMixedRealitySourcePoseHandler Implementaiton
96+
97+ /// <inheritdoc />
98+ public override void OnSourceDetected ( SourceStateEventData eventData )
8699 {
87- if ( Controller == null ||
88- eventData . Controller == null ||
89- eventData . Controller . InputSource . SourceId != Controller . InputSource . SourceId )
100+ if ( RayStabilizer != null )
90101 {
91- return ;
102+ RayStabilizer = null ;
92103 }
93104
94- if ( UseSourcePoseData )
95- {
96- if ( BaseCursor != null &&
97- ! BaseCursor . IsVisible &&
98- ( eventData . SourceData . x >= movementThresholdToUnHide ||
99- eventData . SourceData . y >= MovementThresholdToUnHide ) )
100- {
101- BaseCursor . SetVisibility ( true ) ;
102- transform . rotation = CameraCache . Main . transform . rotation ;
103- }
105+ base . OnSourceDetected ( eventData ) ;
104106
105- var newRotation = Vector3 . zero ;
106- newRotation . x += eventData . SourceData . y ;
107- newRotation . y += eventData . SourceData . x ;
108- transform . Rotate ( newRotation , Space . World ) ;
107+ if ( eventData . SourceId == Controller ? . InputSource . SourceId )
108+ {
109+ isInteractionEnabled = true ;
109110 }
110111 }
111112
112113 /// <inheritdoc />
113- public override void OnPositionInputChanged ( InputEventData < Vector2 > eventData )
114+ public override void OnSourceLost ( SourceStateEventData eventData )
114115 {
116+ base . OnSourceLost ( eventData ) ;
117+
115118 if ( eventData . SourceId == Controller ? . InputSource . SourceId )
116119 {
117- if ( ! UseSourcePoseData &&
118- PoseAction == eventData . MixedRealityInputAction )
119- {
120- if ( BaseCursor != null &&
121- ! BaseCursor . IsVisible &&
122- ( eventData . InputData . x >= movementThresholdToUnHide ||
123- eventData . InputData . y >= MovementThresholdToUnHide ) )
124- {
125- BaseCursor . SetVisibility ( true ) ;
126- transform . rotation = CameraCache . Main . transform . rotation ;
127- }
128-
129- IsTracked = true ;
130- TrackingState = TrackingState . Tracked ;
131-
132- var newRotation = Vector3 . zero ;
133- newRotation . x += eventData . InputData . x ;
134- newRotation . y += eventData . InputData . y ;
135- transform . Rotate ( newRotation , Space . World ) ;
136- }
120+ isInteractionEnabled = false ;
137121 }
138122 }
139123
124+ /// <inheritdoc />
125+ public override void OnSourcePoseChanged ( SourcePoseEventData < Vector2 > eventData )
126+ {
127+ if ( Controller == null ||
128+ eventData . Controller == null ||
129+ eventData . Controller . InputSource . SourceId != Controller . InputSource . SourceId )
130+ {
131+ return ;
132+ }
133+
134+ if ( UseSourcePoseData )
135+ {
136+ UpdateMousePosition ( eventData . SourceData . x , eventData . SourceData . y ) ;
137+ }
138+ }
139+
140+ #endregion IMixedRealitySourcePoseHandler Implementaiton
141+
142+ #region IMixedRealityInputHandler Implementaiton
143+
140144 /// <inheritdoc />
141145 public override void OnInputDown ( InputEventData eventData )
142146 {
@@ -162,8 +166,27 @@ public override void OnInputUp(InputEventData eventData)
162166 }
163167 }
164168
169+ /// <inheritdoc />
170+ public override void OnPositionInputChanged ( InputEventData < Vector2 > eventData )
171+ {
172+ if ( eventData . SourceId == Controller ? . InputSource . SourceId )
173+ {
174+ if ( ! UseSourcePoseData &&
175+ PoseAction == eventData . MixedRealityInputAction )
176+ {
177+ UpdateMousePosition ( eventData . InputData . x , eventData . InputData . y ) ;
178+ }
179+ }
180+ }
181+
182+ #endregion IMixedRealityInputHandler Implementaiton
183+
184+ #region Monobehaviour Implementaiton
185+
165186 protected override void Start ( )
166187 {
188+ isDisabled = DisableCursorOnStart ;
189+
167190 base . Start ( ) ;
168191
169192 if ( RayStabilizer != null )
@@ -183,42 +206,42 @@ protected override void Start()
183206
184207 private void Update ( )
185208 {
186- if ( ! isInteractionEnabled ) { return ; }
209+ if ( ! hideCursorWhenInactive || isDisabled ) { return ; }
187210
188211 timeoutTimer += Time . unscaledDeltaTime ;
189212
190213 if ( timeoutTimer >= hideTimeout )
191214 {
192215 timeoutTimer = 0.0f ;
193216 BaseCursor ? . SetVisibility ( false ) ;
217+ isDisabled = true ;
194218 }
195219 }
196220
197- /// <inheritdoc />
198- public override void OnSourceDetected ( SourceStateEventData eventData )
221+ #endregion Monobehaviour Implementaiton
222+
223+ private void UpdateMousePosition ( float mouseX , float mouseY )
199224 {
200- if ( RayStabilizer != null )
225+ if ( Mathf . Abs ( mouseX ) >= movementThresholdToUnHide ||
226+ Mathf . Abs ( mouseY ) >= movementThresholdToUnHide )
201227 {
202- RayStabilizer = null ;
203- }
204-
205- base . OnSourceDetected ( eventData ) ;
228+ if ( isDisabled )
229+ {
230+ BaseCursor ? . SetVisibility ( true ) ;
231+ transform . rotation = CameraCache . Main . transform . rotation ;
232+ }
233+ else
234+ {
235+ timeoutTimer = 0.0f ;
236+ }
206237
207- if ( eventData . SourceId == Controller ? . InputSource . SourceId )
208- {
209- isInteractionEnabled = true ;
238+ isDisabled = false ;
210239 }
211- }
212-
213- /// <inheritdoc />
214- public override void OnSourceLost ( SourceStateEventData eventData )
215- {
216- base . OnSourceLost ( eventData ) ;
217240
218- if ( eventData . SourceId == Controller ? . InputSource . SourceId )
219- {
220- isInteractionEnabled = false ;
221- }
241+ var newRotation = Vector3 . zero ;
242+ newRotation . x += mouseX ;
243+ newRotation . y += mouseY ;
244+ transform . Rotate ( newRotation , Space . World ) ;
222245 }
223246 }
224247}
0 commit comments