/// <summary>
        /// Read body frames
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void bodyFrameReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
        {
            bool dataReceived = false;

            using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame())
            {
                if (bodyFrame != null)
                {
                    if (this.bodies == null)
                    {
                        this.bodies = new Body[bodyFrame.BodyCount];
                    }

                    // The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array.
                    // As long as those body objects are not disposed and not set to null in the array,
                    // those body objects will be re-used.
                    bodyFrame.GetAndRefreshBodyData(this.bodies);
                    dataReceived = true;
                }
            }

            if (!dataReceived)
            {
                return;
            }

            foreach (Body body in this.bodies)
            {
                // get first tracked body only, notice there's a break below.
                if (body.IsTracked)
                {
                    //鼠标控制
                    if (control_mouse)
                    {
                        MouseControl.Mouse_Driver(body);
                    }
                    //动作识别
                    grs.recoginze(body);
                    //姿势识别
                    prs.recoginze(body);
                }
            }
        }
Beispiel #2
0
        public static void Mouse_Driver(Body body)
        {
            cal_ave(ref lnx, ref lny, ref lbx, ref lby, false);
            cal_ave(ref rnx, ref rny, ref rbx, ref rby, true);

            if (nowHand != null)
            {
                beforeHands = nowHand;
                if (HandsStateList.Count >= STATE_RECORD_NUM)
                {
                    HandsStateList.RemoveAt(0);
                }
                HandsStateList.Add(new HandsState(body));
                nowHand = findState();
            }
            else
            {
                beforeHands = new HandsState();
                nowHand     = new HandsState(body);
                HandsStateList.Add(nowHand);
            }

            //operation start
            if (nowHand.operation == Operation.no_operation)
            {
                StateClear();
                MouseControl.SetCursorPos(screenWidth / 2, screenHeight / 2);
                lx = rx = screenWidth / 2;
                ly = ry = screenHeight / 2;
            }
            else if (nowHand.operation == Operation.left_down)
            {
                //left
                move(lnx, lny, lbx, lby, nowHand.isRight, true);
                //right
                move(rnx, rny, rbx, rby, nowHand.isRight, false);
                if (beforeHands.operation == Operation.left_down)
                {
                    //return;
                }
                else if (beforeHands.operation == Operation.middle_down)
                {
                    MouseMiddleUp();
                }
                else if (beforeHands.operation == Operation.right_down)
                {
                    MouseRightUp();
                }
                MouseLeftDown();
            }
            else if (nowHand.operation == Operation.middle_down)
            {
                //left
                move(lnx, lny, lbx, lby, nowHand.isRight, true);
                //right
                move(rnx, rny, rbx, rby, nowHand.isRight, false);
                if (beforeHands.operation == Operation.left_down)
                {
                    MouseLeftUp();
                }
                else if (beforeHands.operation == Operation.middle_down)
                {
                    return;
                }
                else if (beforeHands.operation == Operation.right_down)
                {
                    MouseRightUp();
                }
                MouseMiddleDown();
            }
            else if (nowHand.operation == Operation.right_down)
            {
                //left
                move(lnx, lny, lbx, lby, nowHand.isRight, true);
                //right
                move(rnx, rny, rbx, rby, nowHand.isRight, false);
                if (beforeHands.operation == Operation.left_down)
                {
                    MouseLeftUp();
                }
                else if (beforeHands.operation == Operation.middle_down)
                {
                    MouseMiddleUp();
                }
                else if (beforeHands.operation == Operation.right_down)
                {
                    return;
                }
                MouseRightDown();
            }
            else if (nowHand.operation == Operation.move)
            {
                StateClear();
                //left
                move(lnx, lny, lbx, lby, nowHand.isRight, true);
                //right
                move(rnx, rny, rbx, rby, nowHand.isRight, false);
            }
            else if (nowHand.operation == Operation.wheel)
            {
                StateClear();
                //wheel
                if (beforeHands.operation != Operation.wheel)
                {
                    wheeldy = beforeHands.primeHandy;
                }
                int dy = nowHand.primeHandy - wheeldy;
                MouseRoll(dy);
            }
            cursor.UpdateHandCursor(nowHand.input);
        }
Beispiel #3
0
        private static void move(float nx, float ny, float bx, float by, bool mouse, bool leftHand)
        {
            float dx = nx - bx;
            float dy = ny - by;
            // get current cursor position
            Point curPos = MouseControl.GetCursorPosition();
            // smoothing for using should be 0 - 0.95f. The way we smooth the cusor is: oldPos + (newPos - oldPos) * smoothValue
            float smoothing = 1 - cursor_smoothing;

            // set cursor position

            if (leftHand)
            {
                lx = (int)(lx + (dx * mouse_sensity * screenWidth) * smoothing);
                ly = (int)(ly - (dy * mouse_sensity * screenHeight) * smoothing);
                if (lx < 0)
                {
                    lx = 0;
                }
                if (lx > screenWidth)
                {
                    lx = screenWidth;
                }
                if (ly < 0)
                {
                    ly = 0;
                }
                if (ly > screenHeight)
                {
                    ly = screenHeight;
                }
                nowHand.input._dx = lx;
                nowHand.input._dy = ly;
                if (!mouse)
                {
                    MouseControl.SetCursorPos(lx, ly);
                }
            }
            else
            {
                rx = (int)(rx + (dx * mouse_sensity * screenWidth) * smoothing);
                ry = (int)(ry - (dy * mouse_sensity * screenHeight) * smoothing);
                if (rx < 0)
                {
                    rx = 0;
                }
                if (rx > screenWidth)
                {
                    rx = screenWidth;
                }
                if (ry < 0)
                {
                    ry = 0;
                }
                if (ry > screenHeight)
                {
                    ry = screenHeight;
                }
                nowHand.input._dx1 = rx;
                nowHand.input._dy1 = ry;
                if (mouse)
                {
                    MouseControl.SetCursorPos(rx, ry);
                }
            }
        }