Esempio n. 1
0
    protected override bool CanBegin(PinchGesture gesture, FingerGestures.IFingerList touches)
    {
        if (!base.CanBegin(gesture, touches))
        {
            return(false);
        }

        FingerGestures.Finger finger0 = touches[0];
        FingerGestures.Finger finger1 = touches[1];

        if (!FingerGestures.AllFingersMoving(finger0, finger1))
        {
            return(false);
        }

        if (!FingersMovedInOppositeDirections(finger0, finger1))
        {
            return(false);
        }

        float startGap = Vector2.SqrMagnitude(finger0.StartPosition - finger1.StartPosition);
        float curGap   = Vector2.SqrMagnitude(finger0.Position - finger1.Position);

        if (FingerGestures.GetAdjustedPixelDistance(Mathf.Abs(startGap - curGap)) < (MinDistance * MinDistance))
        {
            return(false);
        }

        return(true);
    }
Esempio n. 2
0
    protected override GestureRecognitionState OnRecognize(PointCloudGesture gesture, FingerGestures.IFingerList touches)
    {
        if (touches.Count != RequiredFingerCount)
        {
            // fingers lifted off?
            if (touches.Count < RequiredFingerCount)
            {
                // recognize
                if (RecognizePointCloud(gesture))
                {
                    return(GestureRecognitionState.Recognized);
                }

                return(GestureRecognitionState.Failed);
            }

            return(GestureRecognitionState.Failed);
        }

        // update current gesture position
        gesture.Position = touches.GetAveragePosition();

        float   minSampleDelta = FingerGestures.GetAdjustedPixelDistance(MinDistanceBetweenSamples);
        Vector2 lastSamplePos  = gesture.RawPoints[gesture.RawPoints.Count - 1].Position;

        // check if we should take a new sample
        if (Vector2.SqrMagnitude(gesture.Position - lastSamplePos) > minSampleDelta * minSampleDelta)
        {
            int strokeId = 0;   //TODO increment this after each finger up>down
            gesture.RawPoints.Add(new Point(strokeId, gesture.Position));
        }

        return(GestureRecognitionState.InProgress);
    }
Esempio n. 3
0
    // Find closest cluster within radius
    Cluster FindExistingCluster(FingerGestures.Finger finger)
    {
        Cluster best        = null;
        float   bestSqrDist = float.MaxValue;

        // account for higher pixel density touch screens
        float adjustedClusterRadius = FingerGestures.GetAdjustedPixelDistance(ClusterRadius);

        foreach (Cluster cluster in clusters)
        {
            float elapsedTime = finger.StarTime - cluster.StartTime;

            // temporal grouping criteria
            if (elapsedTime > TimeTolerance)
            {
                continue;
            }

            Vector2 centroid = cluster.Fingers.GetAveragePosition();
            float   sqrDist  = Vector2.SqrMagnitude(finger.Position - centroid);

            if (sqrDist < bestSqrDist && sqrDist < (adjustedClusterRadius * adjustedClusterRadius))
            {
                best        = cluster;
                bestSqrDist = sqrDist;
            }
        }

        return(best);
    }
Esempio n. 4
0
    protected override void OnBegin(PinchGesture gesture, FingerGestures.IFingerList touches)
    {
        FingerGestures.Finger finger0 = touches[0];
        FingerGestures.Finger finger1 = touches[1];

        gesture.StartPosition = 0.5f * (finger0.StartPosition + finger1.StartPosition);
        gesture.Position      = 0.5f * (finger0.Position + finger1.Position);

        gesture.Gap = Vector2.Distance(finger0.StartPosition, finger1.StartPosition);
        float curGap = Vector2.Distance(finger0.Position, finger1.Position);

        gesture.Delta = FingerGestures.GetAdjustedPixelDistance(DeltaScale * (curGap - gesture.Gap));
        gesture.Gap   = curGap;
    }
    protected override GestureRecognitionState OnRecognize(PinchGesture gesture, FingerGestures.IFingerList touches)
    {
        if (touches.Count != RequiredFingerCount)
        {
            gesture.Delta = 0;

            // fingers were lifted?
            if (touches.Count < RequiredFingerCount)
            {
                return(GestureRecognitionState.Recognized);
            }

            // more fingers added, gesture failed
            return(GestureRecognitionState.Failed);
        }

        FingerGestures.Finger finger0 = touches[0];
        FingerGestures.Finger finger1 = touches[1];

        gesture.Position = 0.5f * (finger0.Position + finger1.Position);

        float curGap = Vector2.Distance(finger0.Position, finger1.Position);

        // dont do anything if both fingers arent moving
        if (!FingerGestures.AllFingersMoving(finger0, finger1))
        {
            return(GestureRecognitionState.InProgress);
        }


        float newDelta = FingerGestures.GetAdjustedPixelDistance(DeltaScale * (curGap - gesture.Gap));

        gesture.Gap = curGap;

        if (Mathf.Abs(newDelta) > 0.001f)
        {
            if (!FingersMovedInOppositeDirections(finger0, finger1))
            {
                // skip without firing event
                return(GestureRecognitionState.Failed);// GestureRecognitionState.InProgress; //TODO: might want to make this configurable, so the recognizer can fail if fingers move in same direction
            }

            gesture.Delta = newDelta;
            RaiseEvent(gesture);
        }

        return(GestureRecognitionState.InProgress);
    }
    protected override GestureRecognitionState OnRecognize(SwipeGesture gesture, FingerGestures.IFingerList touches)
    {
        if (touches.Count != RequiredFingerCount)
        {
            // more fingers were added - fail right away
            if (touches.Count > RequiredFingerCount)
            {
                return(GestureRecognitionState.Failed);
            }

            //
            // fingers were lifted-off
            //

            // didn't swipe far enough
            if (FingerGestures.GetAdjustedPixelDistance(gesture.Move.magnitude) < Mathf.Max(1, MinDistance))
            {
                return(GestureRecognitionState.Failed);
            }

            // get approx swipe direction
            gesture.Direction = FingerGestures.GetSwipeDirection(gesture.Move);
            return(GestureRecognitionState.Recognized);
        }

        Vector2 previousMotion = gesture.Move;

        gesture.Position = touches.GetAveragePosition();
        gesture.Move     = gesture.Position - gesture.StartPosition;

        float distance = FingerGestures.GetAdjustedPixelDistance(gesture.Move.magnitude);

        // moved too far
        if (MaxDistance > MinDistance && distance > MaxDistance)
        {
            //Debug.LogWarning( "Too far: " + distance );
            return(GestureRecognitionState.Failed);
        }

        if (gesture.ElapsedTime > 0)
        {
            gesture.Velocity = distance / gesture.ElapsedTime;
        }
        else
        {
            gesture.Velocity = 0;
        }

        // we're going too slow
        if (gesture.MoveCounter > 2 && gesture.Velocity < MinVelocity)
        {
            //Debug.LogWarning( "Too slow: " + gesture.Velocity );
            return(GestureRecognitionState.Failed);
        }

        /*
         * FingerGestures.SwipeDirection newDirection = FingerGestures.GetSwipeDirection( Move.normalized, DirectionTolerance );
         *
         * // we went in a bad direction
         * if( !IsValidDirection( newDirection ) || ( direction != FingerGestures.SwipeDirection.None && newDirection != direction ) )
         *  return GestureRecognitionState.Failed;
         *
         * direction = newDirection;
         */

        // check if we have deviated too much from our initial direction
        if (distance > 50.0f && gesture.MoveCounter > 2)
        {
            // accumulate delta angle
            gesture.Deviation += Mathf.Rad2Deg * FingerGestures.SignedAngle(previousMotion, gesture.Move);

            if (Mathf.Abs(gesture.Deviation) > MaxDeviation)
            {
                //Debug.LogWarning( "Swipe: deviated too much from initial direction (" + gesture.Deviation + ")" );
                return(GestureRecognitionState.Failed);
            }
        }

        ++gesture.MoveCounter;
        return(GestureRecognitionState.InProgress);
    }
    protected override GestureRecognitionState OnRecognize(PinchRotateGesture gesture, FingerGestures.IFingerList touches)
    {
        if (touches.Count != RequiredFingerCount)
        {
            gesture.Delta = 0;

            // fingers were lifted?
            if (touches.Count < RequiredFingerCount)
            {
                return(GestureRecognitionState.Recognized);
            }

            // more fingers added, gesture failed
            return(GestureRecognitionState.Failed);
        }

        FingerGestures.Finger finger0 = touches[0];
        FingerGestures.Finger finger1 = touches[1];

        gesture.Position = 0.5f * (finger0.Position + finger1.Position);

        float curGap = Vector2.Distance(finger0.Position, finger1.Position);

        // dont do anything if both fingers arent moving
        if (!FingerGestures.AllFingersMoving(finger0, finger1))   // && curGap >= UnityEngine.Screen.width * 0.25f
        {
            return(GestureRecognitionState.InProgress);
        }


        float newDelta = FingerGestures.GetAdjustedPixelDistance(DeltaScale * (curGap - gesture.Gap));

        gesture.Gap = curGap;

        // if( Mathf.Abs( newDelta ) > 0.001f )
        {
            if (touches.AllMoving() && touches.MovingInSameDirection(0.35f))//&& curGap < UnityEngine.Screen.width*0.25f
            {
                EventMessageName = OnTwoFingerDrag;
                //Debug.Log("OnTwoFingerDrag rec");
                // skip without firing event
                // return GestureRecognitionState.InProgress;// GestureRecognitionState.InProgress; //TODO: might want to make this configurable, so the recognizer can fail if fingers move in same direction
            }
            else if (touches.AllMoving() && FingersMovedInOppositeDirections(finger0, finger1))
            {
                EventMessageName = EventOnPinch;
                //   Debug.Log("EventOnPinch rec");
            }
            else
            {
                //  Debug.Log("pinch rotate inProgress");
                return(GestureRecognitionState.InProgress);
            }
            gesture.LastDelta = gesture.DeltaMove;
            gesture.Delta     = newDelta;
            gesture.DeltaMove = gesture.Position - gesture.LastPos;

            // if we are currently moving, or we were still moving last frame (allows listeners to detect when the finger is stationary when MoveDelta = 0)...
            if (gesture.DeltaMove.sqrMagnitude > 0 || gesture.LastDelta.sqrMagnitude > 0)
            {
                gesture.LastPos = gesture.Position;
            }
            RaiseEvent(gesture);
        }

        return(GestureRecognitionState.InProgress);
    }