/// <summary> /// Called if touches are removed. /// </summary> /// <param name="touches">The touches.</param> protected virtual void touchesEnded(IList <ITouch> touches) { if (combineTouches) { foreach (var touch in touches) { touchSequence.Add(touch); } if (activeTouches.Count == 0) { // Checking which points were removed in clusterExistenceTime seconds to set their centroid as cached screen position var cluster = touchSequence.FindElementsLaterThan(Time.time - combineTouchesInterval, shouldCacheTouchPosition); cachedScreenPosition = ClusterUtils.Get2DCenterPosition(cluster); cachedPreviousScreenPosition = ClusterUtils.GetPrevious2DCenterPosition(cluster); } } else { if (activeTouches.Count == 0) { var lastPoint = touches[touches.Count - 1]; if (shouldCacheTouchPosition(lastPoint)) { cachedScreenPosition = lastPoint.Position; cachedPreviousScreenPosition = lastPoint.PreviousPosition; } else { cachedScreenPosition = TouchManager.INVALID_POSITION; cachedPreviousScreenPosition = TouchManager.INVALID_POSITION; } } } }
/// <inheritdoc /> protected override void pointersReleased(IList <Pointer> pointers) { #if UNITY_5_6_OR_NEWER gestureSampler.Begin(); #endif base.pointersReleased(pointers); if (combinePointers) { var count = pointers.Count; for (var i = 0; i < count; i++) { pointerSequence.Add(pointers[i]); } if (NumPointers == 0) { // Checking which points were removed in clusterExistenceTime seconds to set their centroid as cached screen position var cluster = pointerSequence.FindElementsLaterThan(Time.unscaledTime - combinePointersInterval, shouldCachePointerPosition); cachedScreenPosition = ClusterUtils.Get2DCenterPosition(cluster); cachedPreviousScreenPosition = ClusterUtils.GetPrevious2DCenterPosition(cluster); } } else { if (NumPointers == 0) { if (!isActive) { setState(GestureState.Failed); #if UNITY_5_6_OR_NEWER gestureSampler.End(); #endif return; } // pointers outside of gesture target are ignored in shouldCachePointerPosition() // if all pointers are outside ScreenPosition will be invalid if (TouchManager.IsInvalidPosition(ScreenPosition)) { setState(GestureState.Failed); } else { tapsDone++; isActive = false; if (tapsDone >= numberOfTapsRequired) { setState(GestureState.Recognized); } } } } #if UNITY_5_6_OR_NEWER gestureSampler.End(); #endif }
/// <inheritdoc /> protected override Vector2 getPointScreenPosition(int index) { if (!clusters.HasClusters) { return(ClusterUtils.Get2DCenterPosition(activeTouches)); } return(clusters.GetCenterPosition(index)); }
/// <summary> /// Calculates the center position of one of the clusters. /// </summary> /// <param name="id"> Cluster id. Either <see cref="CLUSTER1"/> or <see cref="CLUSTER2"/>. </param> /// <returns> Cluster's centroid position or <see cref="TouchManager.INVALID_POSITION"/> if cluster contains no pointers. </returns> public Vector2 GetCenterPosition(int id) { if (!HasClusters) { return(TouchManager.INVALID_POSITION); } Vector2 result; switch (id) { case CLUSTER1: result = ClusterUtils.Get2DCenterPosition(cluster1); break; case CLUSTER2: result = ClusterUtils.Get2DCenterPosition(cluster2); break; default: return(TouchManager.INVALID_POSITION); } return(result); }
internal void INTERNAL_TouchEnded(TouchPoint touch) { var total = numTouches - 1; touchesNumState = TouchesNumState.InRange; if (minTouches <= 0) { // have no touches if (total == 0) { touchesNumState = TouchesNumState.PassedMinThreshold; } } else { if (numTouches >= minTouches) { // had >= minTouches, got < minTouches if (total < minTouches) { touchesNumState = TouchesNumState.PassedMinThreshold; } } // last event we already were under minTouches else { touchesNumState = TouchesNumState.TooFew; } } if (maxTouches > 0) { if (numTouches > maxTouches) { if (total <= maxTouches) { // this event we crossed both minTouches and maxTouches if (touchesNumState == TouchesNumState.PassedMinThreshold) { touchesNumState = TouchesNumState.PassedMinMaxThreshold; } // this event we crossed maxTouches else { touchesNumState = TouchesNumState.PassedMaxThreshold; } } // last event we already were over maxTouches else { touchesNumState = TouchesNumState.TooMany; } } } activeTouches.Remove(touch); numTouches = total; if (combineTouches) { touchSequence.Add(touch); if (NumTouches == 0) { // Checking which points were removed in clusterExistenceTime seconds to set their centroid as cached screen position var cluster = touchSequence.FindElementsLaterThan(Time.time - combineTouchesInterval, shouldCacheTouchPosition); cachedScreenPosition = ClusterUtils.Get2DCenterPosition(cluster); cachedPreviousScreenPosition = ClusterUtils.GetPrevious2DCenterPosition(cluster); } } else { if (NumTouches == 0) { if (shouldCacheTouchPosition(touch)) { cachedScreenPosition = touch.Position; cachedPreviousScreenPosition = touch.PreviousPosition; } else { cachedScreenPosition = TouchManager.INVALID_POSITION; cachedPreviousScreenPosition = TouchManager.INVALID_POSITION; } } } touchEnded(touch); }
/// <inheritdoc /> protected override Vector2 getPointScreenPosition() { return(ClusterUtils.Get2DCenterPosition(activeTouches)); }
private void distributePoints() { cluster1.Clear(); cluster2.Clear(); hasClusters = checkClusters(); if (!hasClusters) { return; } cluster1.Add(points[0]); cluster2.Add(points[1]); var total = points.Count; if (total == 2) { return; } var oldHash1 = ""; var oldHash2 = ""; var hash1 = "#"; var hash2 = "#"; while (oldHash1 != hash1 || oldHash2 != hash2) { var center1 = ClusterUtils.Get2DCenterPosition(cluster1); var center2 = ClusterUtils.Get2DCenterPosition(cluster2); Pointer obj1 = null; Pointer obj2 = null; // Take most distant points from cluster1 and cluster2 var maxDist1 = -float.MaxValue; var maxDist2 = -float.MaxValue; for (var i = 0; i < total; i++) { var obj = points[i]; var dist = (center1 - obj.Position).sqrMagnitude; if (dist > maxDist2) { maxDist2 = dist; obj2 = obj; } dist = (center2 - obj.Position).sqrMagnitude; if (dist > maxDist1) { maxDist1 = dist; obj1 = obj; } } // If it is the same point it means that this point is too far away from both clusters and has to be in a separate cluster if (obj1 == obj2) { center1 = (center1 + center2) * .5f; center2 = obj2.Position; } else { center1 = obj1.Position; center2 = obj2.Position; } cluster1.Clear(); cluster2.Clear(); for (var i = 0; i < total; i++) { var obj = points[i]; if ((center1 - obj.Position).sqrMagnitude < (center2 - obj.Position).sqrMagnitude) { cluster1.Add(obj); } else { cluster2.Add(obj); } } oldHash1 = hash1; oldHash2 = hash2; hash1 = ClusterUtils.GetPointsHash(cluster1); hash2 = ClusterUtils.GetPointsHash(cluster2); } markClean(); }