// return signed angle in degrees between current finger position and ref positions
    static float SignedAngularGap(FingerGestures.Finger finger0, FingerGestures.Finger finger1, Vector2 refPos0, Vector2 refPos1)
    {
        Vector2 curDir = (finger0.Position - finger1.Position).normalized;
        Vector2 refDir = (refPos0 - refPos1).normalized;

        // check if we went past the minimum rotation amount treshold
        return(Mathf.Rad2Deg * FingerGestures.SignedAngle(refDir, curDir));
    }
Ejemplo n.º 2
0
    protected override GestureRecognitionState OnRecognize(SwipeGesture gesture, FingerGestures.IFingerList touches)
    {
        float minDistanceInPixels = ToPixels(MinDistance);
        float maxDistanceInPixels = ToPixels(MaxDistance);

        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 (gesture.Move.magnitude < Mathf.Max(1, minDistanceInPixels))
            {
                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;

        // pixel-adjusted distance
        float distance = gesture.Move.magnitude;

        // moved too far
        if (maxDistanceInPixels > minDistanceInPixels && distance > maxDistanceInPixels)
        {
            //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 < ToPixels(MinVelocity))
        {
            //Debug.LogWarning( "Too slow: " + gesture.Velocity );
            return(GestureRecognitionState.Failed);
        }

        // 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(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);
    }