Exemplo n.º 1
0
    // estimate the next state and completeness of the gesture
    public static void CheckForGesture(ref ExtraGestureData gestureData, float timestamp, LeapManager leapManager)
    {
        if(gestureData.complete || !leapManager)
            return;

        switch(gestureData.gesture)
        {
            // check for Fist
            case ExtraGestures.Fist:
                switch(gestureData.state)
                {
                    case 0:  // gesture detection - phase 1
                        if(leapManager.IsHandValid() && leapManager.GetFingersCount() > 1)
                        {
                            SetGestureJoint(ref gestureData, timestamp, true, leapManager.GetHandID(), leapManager.GetHandPos());
                            gestureData.tagFloat = leapManager.GetThumbID();
                            gestureData.screenPos = leapManager.GetCursorNormalizedPos();
                            gestureData.progress = 0.3f;
                        }
                        break;

                    case 1:  // gesture phase 2 - complete
                        if(leapManager.IsHandValid())
                        {
                            // check for stay-in-place
                            Vector3 jointPos = leapManager.GetHandPos();
                            bool bHandMatch = (leapManager.GetHandID() == gestureData.jointId);
                            bool bThumbMatch = leapManager.GetFingersCount() > 0 ? (leapManager.GetThumbID() == (int)gestureData.tagFloat) : true;

                            if(bHandMatch && bThumbMatch)
                            {
                                if(leapManager.GetFingersCount() <= 1)
                                {
                                    gestureData.screenPos = leapManager.GetCursorNormalizedPos();
                                    CheckPoseComplete(ref gestureData, timestamp, jointPos, true, Constants.PoseCompleteDuration);
                                }
                            }
                            else
                            {
                                // hand or thumb don´t match
                                SetGestureCancelled(ref gestureData);
                            }

        //							Debug.Log(gestureData.complete.ToString() + " GestID: " + gestureData.jointId +
        //									" Fingers: " + leapManager.GetFingersCount() +
        //									" ThumbID: " + leapManager.GetThumbID().ToString());

                        }
                        break;
                }
                break;

            // check for Click
            case ExtraGestures.Click:
                switch(gestureData.state)
                {
                    case 0:  // gesture detection - phase 1
                        if(leapManager.IsHandValid())
                        {
                            SetGestureJoint(ref gestureData, timestamp, true, leapManager.GetHandID(), leapManager.GetHandPos());
                            gestureData.screenPos = leapManager.GetCursorNormalizedPos();
                            gestureData.progress = 0.3f;
                        }
                        break;

                    case 1:  // gesture phase 2 - complete
                        if(leapManager.IsHandValid())
                        {
                            // check for stay-in-place
                            Vector3 jointPos = leapManager.GetHandPos();
                            Vector3 distVector = jointPos - gestureData.jointPos;
                            bool isInPose = leapManager.GetHandID() == gestureData.jointId &&
                                            distVector.magnitude < 0.05f;

                            CheckPoseComplete(ref gestureData, timestamp, jointPos, isInPose, Constants.ClickStayDuration);
                        }
                        break;
                }
                break;

            // check for HandSwipe
            case ExtraGestures.HandSwipe:
                switch(gestureData.state)
                {
                    case 0:  // gesture detection - phase 1
                        if(leapManager.IsHandValid())
                        {
                            SetGestureJoint(ref gestureData, timestamp, true, leapManager.GetHandID(), leapManager.GetHandPos());
                            gestureData.screenPos = leapManager.GetCursorNormalizedPos();
                            gestureData.progress = 0.5f;
                        }
                        break;

                    case 1:  // gesture phase 2 - complete
                        if((timestamp - gestureData.timestamp) < 1.5f)
                        {
                            if(leapManager.IsHandValid())
                            {
                                Vector3 jointPos = leapManager.GetHandPos();
                                Vector3 distVector = jointPos - gestureData.jointPos;
                                bool isInPose = leapManager.GetHandID() == gestureData.jointId &&
                                                distVector.magnitude > 0.15f;

                                if(isInPose)
                                {
                                    CheckPoseComplete(ref gestureData, timestamp, jointPos, isInPose, 0f);
                                }
                            }
                        }
                        else
                        {
                            // cancel the gesture
                            SetGestureCancelled(ref gestureData);
                        }
                        break;
                }
                break;
        }

        // here come more gesture-cases
    }