コード例 #1
0
        private void Kinect_OnTrackedBody(object sender, BodyEventArgs e)
        {
            Body body = e.BodyData;

            if (Mode == ControlMode.Disabled)
            {
                return;
            }

            for (int i = 1; i >= 0; i--) // Starts looking from right hand.
            {
                bool isLeft = (i == 0);
                if (body.IsHandLiftForward(isLeft))
                {
                    if (usedHandIndex == -1)
                    {
                        usedHandIndex = i;
                    }
                    else if (usedHandIndex != i)
                    {
                        // In two-hand control mode, non-used hand would be used for pressing/releasing mouse button.
                        if (Mode == ControlMode.MoveGripPressing)
                        {
                            DoMouseControlByHandState(i, body.GetHandState(isLeft));
                        }

                        continue;
                    }

                    if (body.IsHandLiftUpward(isLeft) && body.IsHandLiftUpward(!isLeft))
                    {
                        HandDist.Add(body.TwoHandsDistance());
                        //System.Diagnostics.Trace.WriteLine(HandDist.Count);
                        if (HandDist.Count == 2)
                        {
                            if (HandDist[1] - HandDist[0] >= 0.25)
                            {
                                MouseControl.Wheel(120);
                                //System.Diagnostics.Trace.WriteLine(1);
                                HandDist[0] = HandDist[1];
                                HandDist.RemoveAt(1);
                            }
                            else if (HandDist[0] - HandDist[1] >= 0.25)
                            {
                                MouseControl.Wheel(-120);
                                //System.Diagnostics.Trace.WriteLine(-1);
                                HandDist[0] = HandDist[1];
                                HandDist.RemoveAt(1);
                            }
                            else
                            {
                                HandDist.RemoveAt(1);
                            }
                        }
                        //System.Diagnostics.Trace.WriteLine(body.TwoHandsDistance());
                    }
                    else
                    {
                        HandDist.Clear();
                        kinectJointFilter.UpdateFilter(body);
                        //MVector2 handPos = body.GetHandRelativePosition(isLeft);
                        MVector2 handPos   = KinectBodyHelper.GetHandSmoothedRelativePosition(kinectJointFilter.GetFilteredJoints(), isLeft);
                        MVector2 targetPos = cursorMapper.GetSmoothedOutputPosition(handPos);
                        //System.Diagnostics.Trace.WriteLine(handPos.ToString());

                        MouseControl.MoveTo(targetPos.X, targetPos.Y);

                        if (Mode == ControlMode.GripToPress)
                        {
                            DoMouseControlByHandState(i, body.GetHandState(isLeft));
                        }
                        else if (Mode == ControlMode.HoverToClick)
                        {
                            if ((targetPos - lastCursorPos).Length() > HoverRange)
                            {
                                ToggleHoverTimer(false);
                                hoverClicked = false;
                            }

                            lastCursorPos = targetPos;
                        }
                    }
                }
                else
                {
                    if (usedHandIndex == i)
                    {
                        // Reset to none.
                        usedHandIndex = NONE_USED;
                        ReleaseGrip(i);
                    }
                    else if (Mode == ControlMode.MoveLiftClicking)
                    {
                        DoMouseClickByHandLifting(i, body.GetHandRelativePosition(isLeft));
                        //System.Diagnostics.Trace.WriteLine(body.GetHandRelativePosition(isLeft).Y);
                    }
                    else // Release mouse button when it's not regularly released, such as hand tracking lost.
                    {
                        ReleaseGrip(i);
                    }
                }
            }

            ToggleHoverTimer(Mode == ControlMode.HoverToClick && usedHandIndex != -1);
        }