Skip to content

Commit 0a3186c

Browse files
author
Julia Schwarz
authored
Merge pull request #6987 from microsoft/issue/6986
Fix no OnPointerDragged events from poke pointers
2 parents 78b8d18 + 56bed09 commit 0a3186c

File tree

3 files changed

+156
-35
lines changed

3 files changed

+156
-35
lines changed

Assets/MixedRealityToolkit.SDK/Features/UX/Scripts/Pointers/PokePointer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ private void RaiseTouchUpdated(GameObject targetObject, Vector3 touchPosition)
345345
{
346346
CoreServices.InputSystem?.RaiseOnTouchUpdated(InputSourceParent, Controller, Handedness, touchPosition);
347347
}
348+
else if (closestProximityTouchable.EventsToReceive == TouchableEventType.Pointer)
349+
{
350+
CoreServices.InputSystem?.RaisePointerDragged(this, pointerAction, Handedness, InputSourceParent);
351+
}
348352
}
349353
}
350354

Assets/MixedRealityToolkit.Tests/PlayModeTests/EventCatcherTestUtilities.cs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,51 @@ public void Dispose()
5959
}
6060
}
6161

62+
/// <summary>
63+
/// Utility for counting pointer events.
64+
/// </summary>
65+
internal class PointerEventCatcher : FocusedObjectEventCatcher<PointerEventCatcher>, IMixedRealityPointerHandler
66+
{
67+
public readonly UnityEvent OnPointerDownEvent = new UnityEvent();
68+
public readonly UnityEvent OnPointerUpEvent = new UnityEvent();
69+
public readonly UnityEvent OnPointerDraggedEvent = new UnityEvent();
70+
public readonly UnityEvent OnPointerClickedEvent = new UnityEvent();
71+
72+
public int DragEventCount = 0;
73+
74+
/// <inheritdoc />
75+
public void OnPointerClicked(MixedRealityPointerEventData eventData)
76+
{
77+
OnPointerClickedEvent.Invoke();
78+
}
79+
80+
/// <inheritdoc />
81+
public void OnPointerDown(MixedRealityPointerEventData eventData)
82+
{
83+
++EventsStarted;
84+
OnPointerDownEvent.Invoke();
85+
}
86+
87+
/// <inheritdoc />
88+
public void OnPointerDragged(MixedRealityPointerEventData eventData)
89+
{
90+
++DragEventCount;
91+
OnPointerDraggedEvent.Invoke();
92+
}
93+
94+
/// <inheritdoc />
95+
public void OnPointerUp(MixedRealityPointerEventData eventData)
96+
{
97+
++EventsCompleted;
98+
OnPointerUpEvent.Invoke();
99+
}
100+
101+
102+
}
103+
62104
/// <summary>
63105
/// Utility for counting touch events.
64106
/// </summary>
65-
/// <remarks>
66-
/// Touching an object does not imply getting focus, so use a global event handler to be independent from focus.
67-
/// </remarks>
68107
public class TouchEventCatcher : FocusedObjectEventCatcher<TouchEventCatcher>, IMixedRealityTouchHandler
69108
{
70109
public readonly UnityEvent OnTouchStartedEvent = new UnityEvent();

Assets/MixedRealityToolkit.Tests/PlayModeTests/NearInteractionTouchableTests.cs

Lines changed: 110 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private T CreateTouchable<T>(Vector3 cubeDimensions) where T : BaseNearInteracti
126126
}
127127

128128

129-
private TouchEventCatcher CreateEventCatcher(BaseNearInteractionTouchable touchable)
129+
private TouchEventCatcher CreateTouchEventCatcher(BaseNearInteractionTouchable touchable)
130130
{
131131
var catcher = TouchEventCatcher.Create(touchable.gameObject);
132132

@@ -142,6 +142,81 @@ private TouchEventCatcher CreateEventCatcher(BaseNearInteractionTouchable toucha
142142
return catcher;
143143
}
144144

145+
146+
private PointerEventCatcher CreatePointerEventCatcher(BaseNearInteractionTouchable touchable)
147+
{
148+
var catcher = PointerEventCatcher.Create(touchable.gameObject);
149+
catcher.OnPointerDownEvent.AddListener(() =>
150+
{
151+
touchable.GetComponent<Renderer>().material = pokeMaterial;
152+
});
153+
catcher.OnPointerUpEvent.AddListener(() =>
154+
{
155+
touchable.GetComponent<Renderer>().material = idleMaterial;
156+
});
157+
158+
return catcher;
159+
}
160+
161+
/// <summary>
162+
/// Test that NearInteractionTouchable can raise pointer events
163+
/// </summary>
164+
[UnityTest]
165+
public IEnumerator NearInteractionTouchablePointerEvents()
166+
{
167+
var touchable = CreateTouchable<NearInteractionTouchable>(objectScale);
168+
touchable.SetLocalForward(touchNormal);
169+
touchable.SetBounds(new Vector2(0.5f, 0.5f));
170+
touchable.EventsToReceive = TouchableEventType.Pointer;
171+
172+
yield return new WaitForFixedUpdate();
173+
yield return null;
174+
175+
TestHand testHand = new TestHand(Handedness.Right);
176+
yield return testHand.Show(initialHandPosition);
177+
178+
using (var catcher = CreatePointerEventCatcher(touchable))
179+
{
180+
yield return testHand.MoveTo(objectPosition);
181+
Assert.AreEqual(1, catcher.EventsStarted);
182+
Assert.AreEqual(0, catcher.EventsCompleted);
183+
yield return testHand.MoveTo(rightPosition);
184+
Assert.AreEqual(1, catcher.EventsStarted);
185+
Assert.AreEqual(1, catcher.EventsCompleted);
186+
Assert.Greater(catcher.DragEventCount, 0);
187+
188+
// Touch started and completed when entering and exiting behind the plane
189+
yield return testHand.MoveTo(initialHandPosition);
190+
yield return testHand.MoveTo(objectPosition);
191+
Assert.AreEqual(2, catcher.EventsStarted);
192+
Assert.AreEqual(1, catcher.EventsCompleted);
193+
yield return testHand.MoveTo(backPosition);
194+
Assert.AreEqual(2, catcher.EventsStarted);
195+
Assert.AreEqual(2, catcher.EventsCompleted);
196+
Assert.Greater(catcher.DragEventCount, 1);
197+
int dragEventCount = catcher.DragEventCount;
198+
199+
// No touch when moving at behind the plane
200+
yield return testHand.MoveTo(rightPosition);
201+
Assert.AreEqual(2, catcher.EventsStarted);
202+
Assert.AreEqual(2, catcher.EventsCompleted);
203+
Assert.AreEqual(dragEventCount, catcher.DragEventCount, "No drag events should fire when poke pointer moving behind plane");
204+
205+
206+
// No touch when moving outside the bounds
207+
yield return testHand.MoveTo(initialHandPosition + outOfBoundsOffset);
208+
yield return testHand.MoveTo(objectPosition + outOfBoundsOffset);
209+
yield return testHand.MoveTo(rightPosition);
210+
Assert.AreEqual(2, catcher.EventsStarted);
211+
Assert.AreEqual(2, catcher.EventsCompleted);
212+
Assert.AreEqual(dragEventCount, catcher.DragEventCount, "No drag events should fire when poke pointer moving outside bounds");
213+
}
214+
215+
yield return testHand.Hide();
216+
217+
UnityEngine.Object.Destroy(touchable.gameObject);
218+
}
219+
145220
/// <summary>
146221
/// Test creates an object with NearInteractionTouchable
147222
/// </summary>
@@ -155,39 +230,41 @@ public IEnumerator NearInteractionTouchableVariant()
155230
yield return new WaitForFixedUpdate();
156231
yield return null;
157232

158-
yield return PlayModeTestUtilities.ShowHand(Handedness.Right, inputSim);
233+
TestHand testHand = new TestHand(Handedness.Right);
234+
yield return testHand.Show(initialHandPosition);
159235

160-
using (var catcher = CreateEventCatcher(touchable))
236+
using (var catcher = CreateTouchEventCatcher(touchable))
161237
{
162-
// Touch started and completed when entering and exiting
163-
yield return PlayModeTestUtilities.MoveHand(initialHandPosition, objectPosition, ArticulatedHandPose.GestureId.Open, Handedness.Right, inputSim);
238+
yield return testHand.MoveTo(objectPosition);
164239
Assert.AreEqual(1, catcher.EventsStarted);
165240
Assert.AreEqual(0, catcher.EventsCompleted);
166-
yield return PlayModeTestUtilities.MoveHand(objectPosition, rightPosition, ArticulatedHandPose.GestureId.Pinch, Handedness.Right, inputSim);
241+
yield return testHand.MoveTo(rightPosition);
167242
Assert.AreEqual(1, catcher.EventsStarted);
168243
Assert.AreEqual(1, catcher.EventsCompleted);
169244

170245
// Touch started and completed when entering and exiting behind the plane
171-
yield return PlayModeTestUtilities.MoveHand(initialHandPosition, objectPosition, ArticulatedHandPose.GestureId.Open, Handedness.Right, inputSim);
246+
yield return testHand.MoveTo(initialHandPosition);
247+
yield return testHand.MoveTo(objectPosition);
172248
Assert.AreEqual(2, catcher.EventsStarted);
173249
Assert.AreEqual(1, catcher.EventsCompleted);
174-
yield return PlayModeTestUtilities.MoveHand(objectPosition, backPosition, ArticulatedHandPose.GestureId.Pinch, Handedness.Right, inputSim);
250+
yield return testHand.MoveTo(backPosition);
175251
Assert.AreEqual(2, catcher.EventsStarted);
176252
Assert.AreEqual(2, catcher.EventsCompleted);
177253

178254
// No touch when moving at behind the plane
179-
yield return PlayModeTestUtilities.MoveHand(backPosition, rightPosition, ArticulatedHandPose.GestureId.Pinch, Handedness.Right, inputSim);
255+
yield return testHand.MoveTo(rightPosition);
180256
Assert.AreEqual(2, catcher.EventsStarted);
181257
Assert.AreEqual(2, catcher.EventsCompleted);
182258

183259
// No touch when moving outside the bounds
184-
yield return PlayModeTestUtilities.MoveHand(initialHandPosition + outOfBoundsOffset, objectPosition + outOfBoundsOffset, ArticulatedHandPose.GestureId.Open, Handedness.Right, inputSim);
185-
yield return PlayModeTestUtilities.MoveHand(objectPosition + outOfBoundsOffset, rightPosition, ArticulatedHandPose.GestureId.Open, Handedness.Right, inputSim);
260+
yield return testHand.MoveTo(initialHandPosition + outOfBoundsOffset);
261+
yield return testHand.MoveTo(objectPosition + outOfBoundsOffset);
262+
yield return testHand.MoveTo(rightPosition);
186263
Assert.AreEqual(2, catcher.EventsStarted);
187264
Assert.AreEqual(2, catcher.EventsCompleted);
188265
}
189266

190-
yield return PlayModeTestUtilities.HideHand(Handedness.Right, inputSim);
267+
yield return testHand.Hide();
191268

192269
UnityEngine.Object.Destroy(touchable.gameObject);
193270
}
@@ -199,50 +276,51 @@ public IEnumerator NearInteractionTouchableVariant()
199276
public IEnumerator NearInteractionTouchableVolumeVariant()
200277
{
201278
var touchable = CreateTouchable<NearInteractionTouchableVolume>(Vector3.one);
202-
279+
203280
yield return new WaitForFixedUpdate();
204281
yield return null;
205282

206-
yield return PlayModeTestUtilities.ShowHand(Handedness.Right, inputSim);
207-
208-
using (var catcher = CreateEventCatcher(touchable))
283+
TestHand testHand = new TestHand(Handedness.Left);
284+
yield return testHand.Show(Vector3.zero);
285+
yield return testHand.MoveTo(initialHandPosition);
286+
using (var catcher = CreateTouchEventCatcher(touchable))
209287
{
210288
// Touch started when entering collider
211-
yield return PlayModeTestUtilities.MoveHand(initialHandPosition, objectPosition, ArticulatedHandPose.GestureId.Open, Handedness.Right, inputSim);
289+
yield return testHand.MoveTo(objectPosition);
212290
Assert.AreEqual(1, catcher.EventsStarted);
213291
Assert.AreEqual(0, catcher.EventsCompleted);
214292

215293
// Ensure no touch up event fires while moving hand/pokepointer through collider to each corner of volume
216294
Vector3[] cornerPositions = new Vector3[8];
217295
touchable.GetComponent<BoxCollider>().bounds.GetCornerPositions(ref cornerPositions);
218-
var currentPos = objectPosition;
219296
for (int i = 0; i < cornerPositions.Length; i++)
220297
{
221-
yield return PlayModeTestUtilities.MoveHand(currentPos, cornerPositions[i], ArticulatedHandPose.GestureId.Open, Handedness.Right, inputSim);
222-
currentPos = cornerPositions[i];
223-
Assert.AreEqual(1, catcher.EventsStarted, "Received extra touch down when moving through volume to position " + currentPos);
224-
Assert.AreEqual(0, catcher.EventsCompleted, "Received extra touch up when moving through volume to position " + currentPos);
298+
yield return testHand.MoveTo(cornerPositions[i]);
299+
Assert.AreEqual(1, catcher.EventsStarted, "Received extra touch down when moving through volume to position " + cornerPositions[i]);
300+
Assert.AreEqual(0, catcher.EventsCompleted, "Received extra touch up when moving through volume to position " + cornerPositions[i]);
225301
}
226302

227303
// Touch up when exit collider
228-
yield return PlayModeTestUtilities.MoveHand(currentPos, rightPosition, ArticulatedHandPose.GestureId.Pinch, Handedness.Right, inputSim);
304+
yield return testHand.MoveTo(rightPosition);
229305
Assert.AreEqual(1, catcher.EventsStarted);
230306
Assert.AreEqual(1, catcher.EventsCompleted);
231307

232308
// No touch when moving outside the collider
233-
yield return PlayModeTestUtilities.MoveHand(backPosition, rightPosition, ArticulatedHandPose.GestureId.Pinch, Handedness.Right, inputSim);
309+
yield return testHand.Hide();
310+
yield return testHand.Show(backPosition);
311+
yield return testHand.MoveTo(backPosition + new Vector3(2f, 0f, 0f));
234312
Assert.AreEqual(1, catcher.EventsStarted);
235313
Assert.AreEqual(1, catcher.EventsCompleted);
236314

237315
// Touch when moving off-center
238-
yield return PlayModeTestUtilities.MoveHand(initialHandPosition + outOfBoundsOffset, objectPosition + outOfBoundsOffset, ArticulatedHandPose.GestureId.Open, Handedness.Right, inputSim);
239-
yield return PlayModeTestUtilities.MoveHand(objectPosition + outOfBoundsOffset, rightPosition, ArticulatedHandPose.GestureId.Open, Handedness.Right, inputSim);
316+
yield return testHand.Hide();
317+
yield return testHand.Show(initialHandPosition + outOfBoundsOffset);
318+
yield return testHand.MoveTo(objectPosition + outOfBoundsOffset);
319+
yield return testHand.MoveTo(rightPosition);
240320
Assert.AreEqual(2, catcher.EventsStarted);
241321
Assert.AreEqual(2, catcher.EventsCompleted);
242322
}
243323

244-
yield return PlayModeTestUtilities.HideHand(Handedness.Right, inputSim);
245-
246324
UnityEngine.Object.Destroy(touchable.gameObject);
247325
}
248326

@@ -365,7 +443,7 @@ public IEnumerator NearInteractionTouchableOverlapQuerySaturation()
365443
touchables[i].SetBounds(new Vector2(0.5f, 0.5f));
366444
touchables[i].transform.position = objectPosition + r * radiusStart;
367445

368-
catchers[i] = CreateEventCatcher(touchables[i]);
446+
catchers[i] = CreateTouchEventCatcher(touchables[i]);
369447
}
370448

371449
yield return new WaitForFixedUpdate();
@@ -491,10 +569,10 @@ public IEnumerator NearInteractionTouchableDistance()
491569
var touchableRect = CreateTouchable<NearInteractionTouchable>(0.15f);
492570
touchableRect.SetLocalForward(touchNormal);
493571
touchableRect.SetBounds(new Vector2(0.5f, 0.5f));
494-
var catcherRect = CreateEventCatcher(touchableRect);
572+
var catcherRect = CreateTouchEventCatcher(touchableRect);
495573

496574
var touchableVolume = CreateTouchable<NearInteractionTouchableVolume>(0.15f);
497-
var catcherVolume = CreateEventCatcher(touchableVolume);
575+
var catcherVolume = CreateTouchEventCatcher(touchableVolume);
498576

499577
var canvas = UnityUiUtilities.CreateCanvas(0.002f);
500578
var touchableUI = canvas.GetComponent<NearInteractionTouchableUnityUI>();
@@ -634,7 +712,7 @@ public IEnumerator NearInteractionTouchableSetTouchableCollider()
634712
BoxCollider newBoxCollider = cube2.GetComponent<BoxCollider>();
635713
newBoxCollider.size = new Vector3(4, 2, 1.2f);
636714

637-
using (var catcher = CreateEventCatcher(nearIT))
715+
using (var catcher = CreateTouchEventCatcher(nearIT))
638716
{
639717
// Touch started and completed when entering and exiting the collider
640718
yield return rightHand.Move(new Vector3(0, 0, 0.4f));

0 commit comments

Comments
 (0)