protected override GestureState OnActive(FingerGestures.IFingerList touches)
    {
        if (touches.Count != RequiredFingerCount)
        {
            // fingers were lifted off
            if (touches.Count < RequiredFingerCount)
            {
                if (direction != FingerGestures.SwipeDirection.None)
                {
                    if (OnSwipe != null)
                    {
                        OnSwipe(this);
                    }

                    return(GestureState.Recognized);
                }
            }

            return(GestureState.Failed);
        }

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

        float distance = Move.magnitude;

        // didnt move far enough
        if (distance < MinDistance)
        {
            return(GestureState.InProgress);
        }

        float elapsedTime = Time.time - startTime;

        if (elapsedTime > 0)
        {
            velocity = distance / elapsedTime;
        }
        else
        {
            velocity = 0;
        }

        // we're going too slow
        if (velocity < MinVelocity)
        {
            return(GestureState.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(GestureState.Failed);
        }

        direction = newDirection;
        return(GestureState.InProgress);
    }
Beispiel #2
0
    void OnDrag(DragGesture gesture)
    {
        if (!isPlaying)
        {
            return;
        }

        // update the ninja gesture cut trail
        UpdateTrail(gesture);

        // Figure out the direction of the trail
        Vector2 dir = gesture.TotalMove.normalized;         // you could also use Gesture.DeltaMove to get the movement change since last frame

        FingerGestures.SwipeDirection swipeDir = FingerGestures.GetSwipeDirection(dir);

        if (swipeDir != lastDirection &&
            swipeDir != FingerGestures.SwipeDirection.UpperLeftDiagonal &&
            swipeDir != FingerGestures.SwipeDirection.UpperRightDiagonal &&
            swipeDir != FingerGestures.SwipeDirection.LowerLeftDiagonal &&
            swipeDir != FingerGestures.SwipeDirection.LowerRightDiagonal)
        {
            AudioManager.Instance.PlayClip("ninjaWhoosh", variations: 3);
        }
        lastDirection = swipeDir;

        switch (gesture.Phase)
        {
        case ContinuousGesturePhase.Updated:
            GameObject go = gesture.Selection;
            if (go)
            {
                //Debug.Log("Touching " + go.name);
                NinjaTrigger trigger = go.GetComponent <NinjaTrigger>();

                // if the trigger is null, check the parent...a little hacky, but sue me!
                if (trigger == null)
                {
                    trigger = go.transform.parent.gameObject.GetComponent <NinjaTrigger>();
                }
                if (trigger)
                {
                    trigger.OnCut(gesture.Position);
                }
            }
            break;
        }
    }
Beispiel #3
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);
    }