示例#1
0
    void Update()
    {
        // start Kinect interaction as needed
        if (!interactionInited)
        {
            StartInteraction();

            if (!interactionInited)
            {
                Application.Quit();
                return;
            }
        }

        // update Kinect interaction
        if (InteractionWrapper.UpdateKinectInteraction() == 0)
        {
            //int lastSkeletonTrackingID = skeletonTrackingID;
            skeletonTrackingID = InteractionWrapper.GetSkeletonTrackingID();

            if (skeletonTrackingID != 0)
            {
                InteractionWrapper.InteractionHandEventType handEvent = InteractionWrapper.InteractionHandEventType.None;
                Vector4 handPos = Vector4.zero;
                //Vector4 shoulderPos = Vector4.zero;
                //Vector3 screenPos = Vector3.zero;

                // process left hand
                leftHandState   = InteractionWrapper.GetLeftHandState();
                handEvent       = (InteractionWrapper.InteractionHandEventType)InteractionWrapper.GetLeftHandEvent();
                isLeftHandPress = InteractionWrapper.GetLeftHandPressed();

                InteractionWrapper.GetLeftCursorPos(ref handPos);
                leftHandScreenPos.x = Mathf.Clamp01(handPos.x);
                leftHandScreenPos.y = 1.0f - Mathf.Clamp01(handPos.y);
                //leftHandScreenPos.z = Mathf.Clamp01(handPos.z);
                leftHandScreenPos.z = handPos.z;

                bool bLeftHandPrimaryNow  = (leftHandState & (uint)InteractionWrapper.NuiHandpointerState.PrimaryForUser) != 0;
                bool bRightHandPrimaryNow = (rightHandState & (uint)InteractionWrapper.NuiHandpointerState.PrimaryForUser) != 0;

                if ((bLeftHandPrimaryNow != isLeftHandPrimary) || (bRightHandPrimaryNow != isRightHandPrimary))
                {
                    if (controlMouseCursor && dragInProgress)
                    {
                        MouseControl.MouseRelease();
                        dragInProgress = false;
                    }

                    lastLeftHandEvent  = InteractionWrapper.InteractionHandEventType.Release;
                    lastRightHandEvent = InteractionWrapper.InteractionHandEventType.Release;
                }

                if (controlMouseCursor && (handEvent != lastLeftHandEvent))
                {
                    if (controlMouseDrag && !dragInProgress && (handEvent == InteractionWrapper.InteractionHandEventType.Grip))
                    {
                        dragInProgress = true;
                        MouseControl.MouseDrag();
                    }
                    else if (dragInProgress && (handEvent == InteractionWrapper.InteractionHandEventType.Release))
                    {
                        MouseControl.MouseRelease();
                        dragInProgress = false;
                    }
                }

                leftHandEvent = handEvent;
                if (handEvent != InteractionWrapper.InteractionHandEventType.None)
                {
                    lastLeftHandEvent = handEvent;
                }

                // if the hand is primary, set the cursor position
                if (bLeftHandPrimaryNow)
                {
                    isLeftHandPrimary = true;

                    // check for left hand click
                    float fClickDist = (leftHandScreenPos - lastLeftHandPos).magnitude;
                    if (!dragInProgress && fClickDist < InteractionWrapper.Constants.ClickMaxDistance)
                    {
                        if ((Time.realtimeSinceStartup - lastLeftHandTime) >= InteractionWrapper.Constants.ClickStayDuration)
                        {
                            if (!isLeftHandClick)
                            {
                                isLeftHandClick       = true;
                                leftHandClickProgress = 1f;

                                if (controlMouseCursor)
                                {
                                    MouseControl.MouseClick();

                                    isLeftHandClick       = false;
                                    leftHandClickProgress = 0f;
                                    lastLeftHandPos       = Vector3.zero;
                                    lastLeftHandTime      = Time.realtimeSinceStartup;
                                }
                            }
                        }
                        else
                        {
                            leftHandClickProgress = (Time.realtimeSinceStartup - lastLeftHandTime) / InteractionWrapper.Constants.ClickStayDuration;
                        }
                    }
                    else
                    {
                        isLeftHandClick       = false;
                        leftHandClickProgress = 0f;
                        lastLeftHandPos       = leftHandScreenPos;
                        lastLeftHandTime      = Time.realtimeSinceStartup;
                    }

                    if ((leftHandClickProgress < 0.7f) /**&& !isLeftHandPress*/)
                    {
                        cursorScreenPos = Vector3.Lerp(cursorScreenPos, leftHandScreenPos, smoothFactor * Time.deltaTime);
                    }
                    else
                    {
                        leftHandScreenPos = cursorScreenPos;
                    }

                    if (controlMouseCursor)
                    {
                        MouseControl.MouseMove(cursorScreenPos, debugText);
                    }
                }
                else
                {
                    isLeftHandPrimary = false;
                }

//				// check for left hand Pull-gesture
//				if(lastLeftHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
//				{
//					if(!bPullLeftHandStarted)
//					{
//						if(leftHandScreenPos.z > pushWhenPressMoreThan)
//						{
//							bPullLeftHandStarted = true;
//							bPullLeftHandFinished = false;
//							pullLeftHandStartedAt = Time.realtimeSinceStartup;
//						}
//					}
//				}

                // check for left hand Push-gesture
                if (lastLeftHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
                {
                    if (!bPushLeftHandStarted)
                    {
                        if (!isLeftHandPress)
                        {
                            bPushLeftHandStarted  = true;
                            bPushLeftHandFinished = false;
                            pushLeftHandStartedAt = Time.realtimeSinceStartup;
                        }
                    }
                }

//				if(lastLeftHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
//				{
//					if(bPullLeftHandStarted)
//					{
//						if((Time.realtimeSinceStartup - pullLeftHandStartedAt) <= 1.5f)
//						{
//							if(leftHandScreenPos.z <= pullWhenPressLessThan)
//							{
//								bPullLeftHandFinished = true;
//								bPullLeftHandStarted = false;
//							}
//						}
//						else
//						{
//							bPullLeftHandStarted = false;
//						}
//					}
//				}
//				else
//				{
//					// no more hand grip
//					bPullLeftHandFinished = false;
//					bPullLeftHandStarted = false;
//				}

                if (lastLeftHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
                {
                    if (bPushLeftHandStarted)
                    {
                        if ((Time.realtimeSinceStartup - pushLeftHandStartedAt) <= 1.5f)
                        {
                            if (isLeftHandPress)
                            {
                                bPushLeftHandFinished = true;
                                bPushLeftHandStarted  = false;
                            }
                        }
                        else
                        {
                            bPushLeftHandStarted = false;
                        }
                    }
                }
                else
                {
                    // no more hand release
                    bPushLeftHandFinished = false;
                    bPushLeftHandStarted  = false;
                }

                // process right hand
                rightHandState   = InteractionWrapper.GetRightHandState();
                handEvent        = (InteractionWrapper.InteractionHandEventType)InteractionWrapper.GetRightHandEvent();
                isRightHandPress = InteractionWrapper.GetRightHandPressed();

                InteractionWrapper.GetRightCursorPos(ref handPos);
                rightHandScreenPos.x = Mathf.Clamp01(handPos.x);
                rightHandScreenPos.y = 1.0f - Mathf.Clamp01(handPos.y);
                //rightHandScreenPos.z = Mathf.Clamp01(handPos.z);
                rightHandScreenPos.z = handPos.z;

                if (controlMouseCursor && (handEvent != lastRightHandEvent))
                {
                    if (controlMouseDrag && !dragInProgress && (handEvent == InteractionWrapper.InteractionHandEventType.Grip))
                    {
                        dragInProgress = true;
                        MouseControl.MouseDrag();
                    }
                    else if (dragInProgress && (handEvent == InteractionWrapper.InteractionHandEventType.Release))
                    {
                        MouseControl.MouseRelease();
                        dragInProgress = false;
                    }
                }

                rightHandEvent = handEvent;
                if (handEvent != InteractionWrapper.InteractionHandEventType.None)
                {
                    lastRightHandEvent = handEvent;
                }

                // if the hand is primary, set the cursor position
                if (bRightHandPrimaryNow)
                {
                    isRightHandPrimary = true;

                    // check for right hand click
                    float fClickDist = (rightHandScreenPos - lastRightHandPos).magnitude;
                    if (!dragInProgress && fClickDist < InteractionWrapper.Constants.ClickMaxDistance)
                    {
                        if ((Time.realtimeSinceStartup - lastRightHandTime) >= InteractionWrapper.Constants.ClickStayDuration)
                        {
                            if (!isRightHandClick)
                            {
                                isRightHandClick       = true;
                                rightHandClickProgress = 1f;

                                if (controlMouseCursor)
                                {
                                    MouseControl.MouseClick();

                                    isRightHandClick       = false;
                                    rightHandClickProgress = 0f;
                                    lastRightHandPos       = Vector3.zero;
                                    lastRightHandTime      = Time.realtimeSinceStartup;
                                }
                            }
                        }
                        else
                        {
                            rightHandClickProgress = (Time.realtimeSinceStartup - lastRightHandTime) / InteractionWrapper.Constants.ClickStayDuration;
                        }
                    }
                    else
                    {
                        isRightHandClick       = false;
                        rightHandClickProgress = 0f;
                        lastRightHandPos       = rightHandScreenPos;
                        lastRightHandTime      = Time.realtimeSinceStartup;
                    }

                    if ((rightHandClickProgress < 0.7f) /**&& !isRightHandPress*/)
                    {
                        cursorScreenPos = Vector3.Lerp(cursorScreenPos, rightHandScreenPos, smoothFactor * Time.deltaTime);
                    }
                    else
                    {
                        rightHandScreenPos = cursorScreenPos;
                    }

                    if (controlMouseCursor)
                    {
                        MouseControl.MouseMove(cursorScreenPos, debugText);
                    }
                }
                else
                {
                    isRightHandPrimary = false;
                }

//				// check for right hand Pull-gesture
//				if(lastRightHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
//				{
//					if(!bPullRightHandStarted)
//					{
//						if(rightHandScreenPos.z > pushWhenPressMoreThan)
//						{
//							bPullRightHandStarted = true;
//							bPullRightHandFinished = false;
//							pullRightHandStartedAt = Time.realtimeSinceStartup;
//						}
//					}
//				}

                // check for right hand Push-gesture
                if (lastRightHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
                {
                    if (!bPushRightHandStarted)
                    {
                        if (!isRightHandPress)
                        {
                            bPushRightHandStarted  = true;
                            bPushRightHandFinished = false;
                            pushRightHandStartedAt = Time.realtimeSinceStartup;
                        }
                    }
                }

//				if(lastRightHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
//				{
//					if(bPullRightHandStarted)
//					{
//						if((Time.realtimeSinceStartup - pullRightHandStartedAt) <= 1.5f)
//						{
//							if(rightHandScreenPos.z <= pullWhenPressLessThan)
//							{
//								bPullRightHandFinished = true;
//								bPullRightHandStarted = false;
//							}
//						}
//						else
//						{
//							bPullRightHandStarted = false;
//						}
//					}
//				}
//				else
//				{
//					// no more hand grip
//					bPullRightHandFinished = false;
//					bPullRightHandStarted = false;
//				}

                if (lastRightHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
                {
                    if (bPushRightHandStarted)
                    {
                        if ((Time.realtimeSinceStartup - pushRightHandStartedAt) <= 1.5f)
                        {
                            if (isRightHandPress)
                            {
                                bPushRightHandFinished = true;
                                bPushRightHandStarted  = false;
                            }
                        }
                        else
                        {
                            bPushRightHandStarted = false;
                        }
                    }
                }
                else
                {
                    // no more hand grip
                    bPushRightHandFinished = false;
                    bPushRightHandStarted  = false;
                }
            }
            else
            {
                leftHandState  = 0;
                rightHandState = 0;

                isLeftHandPrimary  = false;
                isRightHandPrimary = false;

                isLeftHandPress  = false;
                isRightHandPress = false;

                leftHandEvent  = InteractionWrapper.InteractionHandEventType.None;
                rightHandEvent = InteractionWrapper.InteractionHandEventType.None;

                lastLeftHandEvent  = InteractionWrapper.InteractionHandEventType.Release;
                lastRightHandEvent = InteractionWrapper.InteractionHandEventType.Release;

                if (controlMouseCursor && dragInProgress)
                {
                    MouseControl.MouseRelease();
                    dragInProgress = false;
                }
            }
        }
    }