diff --git a/Runtime/Interaction/Logic/HPUIGestureLogicUnified.cs b/Runtime/Interaction/Logic/HPUIGestureLogicUnified.cs index 506db81..fc03973 100644 --- a/Runtime/Interaction/Logic/HPUIGestureLogicUnified.cs +++ b/Runtime/Interaction/Logic/HPUIGestureLogicUnified.cs @@ -49,8 +49,6 @@ public void OnSelectEntering(IHPUIInteractable interactable) return; } - activeInteractablesCount += 1; - if (interactorGestureState == HPUIGesture.None) { interactorGestureState = HPUIGesture.Tap; @@ -68,6 +66,7 @@ public void OnSelectEntering(IHPUIInteractable interactable) HPUIInteractionState state = new HPUIInteractionState(Time.time, interactable.ComputeInteractorPostion(interactor), inValidWindow); activeInteractables.Add(interactable, state); + activeInteractablesCount += 1; // If a new higher priority targets is encountered within tap time window, we hand over control to that. if (interactable.zOrder < lowestTargetZIndex && inValidWindow) @@ -86,9 +85,10 @@ public void OnSelectEntering(IHPUIInteractable interactable) private void ComputeCurrectTrackingInteractable() { - // Any target that is active should be ok for this. + // Any target that is active should be ok for this? IHPUIInteractable interactableToTrack = activeInteractables .Where(kvp => kvp.Value.active) + .OrderBy(kvp => kvp.Value.startTime) .First().Key; if (interactableToTrack != currentTrackingInteractable) @@ -124,9 +124,7 @@ public void OnSelectExiting(IHPUIInteractable interactable) return; } - activeInteractablesCount -= 1; - - if (activeInteractablesCount == 0) + if (activeInteractablesCount == 1) { HPUIInteractionState state; if (activePriorityInteractable != null) @@ -169,6 +167,7 @@ public void OnSelectExiting(IHPUIInteractable interactable) if (activeInteractables.ContainsKey(interactable)) { activeInteractables[interactable].active = false; + activeInteractablesCount -= 1; ComputeCurrectTrackingInteractable(); } } @@ -259,6 +258,7 @@ public void Update() throw new NotImplementedException(); } + previousPosition = currentPosition; } /// diff --git a/Tests/HPUIGestureLogicUnifiedTest.cs b/Tests/HPUIGestureLogicUnifiedTest.cs index da81ded..4f147e4 100644 --- a/Tests/HPUIGestureLogicUnifiedTest.cs +++ b/Tests/HPUIGestureLogicUnifiedTest.cs @@ -91,6 +91,58 @@ public IEnumerator HPUIGestureLogicUnifiedTest_SimpleGesture() Assert.Greater(gesturesCount, 0); } + [UnityTest] + public IEnumerator HPUIGestureLogicUnifiedTest_TapThenGesture() + { + Reset(); + TestHPUIInteractable i1 = new TestHPUIInteractable(0, true, true, OnTapCallback, OnGestureCallback); + IHPUIGestureLogic logic = new HPUIGestureLogicUnified(new HPUIInteractor(), TapTimeThreshold, TapDistanceThreshold); + // First tap + logic.OnSelectEntering(i1); + logic.Update(); + yield return new WaitForSeconds(TapTimeThreshold /2); + logic.Update(); + logic.OnSelectExiting(i1); + Assert.AreEqual(tapsCount, 1); + Assert.AreEqual(gesturesCount, 0); + + // Gesture + Reset(); + logic.OnSelectEntering(i1); + logic.Update(); + yield return new WaitForSeconds(TapTimeThreshold * 2); + logic.Update(); + logic.OnSelectExiting(i1); + Assert.AreEqual(tapsCount, 0); + Assert.Greater(gesturesCount, 0); + } + + [UnityTest] + public IEnumerator HPUIGestureLogicUnifiedTest_GestureThenTap() + { + Reset(); + TestHPUIInteractable i1 = new TestHPUIInteractable(0, true, true, OnTapCallback, OnGestureCallback); + IHPUIGestureLogic logic = new HPUIGestureLogicUnified(new HPUIInteractor(), TapTimeThreshold, TapDistanceThreshold); + // Gesture + logic.OnSelectEntering(i1); + logic.Update(); + yield return new WaitForSeconds(TapTimeThreshold * 2); + logic.Update(); + logic.OnSelectExiting(i1); + Assert.AreEqual(tapsCount, 0); + Assert.Greater(gesturesCount, 0); + + // tap + Reset(); + logic.OnSelectEntering(i1); + logic.Update(); + yield return new WaitForSeconds(TapTimeThreshold / 2); + logic.Update(); + logic.OnSelectExiting(i1); + Assert.AreEqual(tapsCount, 1); + Assert.AreEqual(gesturesCount, 0); + } + [Test] public void HPUIGestureLogicUnifiedTest_TwoItem_tap_time() {