Ejemplo n.º 1
0
        public Cursor()
        {
            InitializeComponent();
            toggleIdle(true);
            Kinect kinect = Kinect.getInstance();

            parentBounds = new Size(0, 0);

            Loaded += (source, args) => {
                parentWindow = Window.GetWindow(this);

                // Kinect controls
                kinect.FingerPositionChanged += (p) => {
                    Position = kinect.ColorToInterface(p, parentBounds);
                    Mode     = kinect.CursorMode;
                };
                kinect.ModeStart += (m) => {
                    Position = kinect.ColorToInterface(kinect.FingerPosition, parentBounds);
                    Mode     = m;

                    ModeStart?.Invoke(m);
                };
                kinect.ModeEnd += (m) => {
                    Position = kinect.ColorToInterface(kinect.FingerPosition, parentBounds);
                    Mode     = m;

                    ModeEnd?.Invoke(m);
                };

                /* Mouse controls
                 * parentWindow.MouseMove += (s, e) => {
                 *  Position = Mouse.GetPosition(parentWindow);
                 *  Mode = mouseMode();
                 *
                 *  Moved?.Invoke(Position);
                 * };
                 * parentWindow.PreviewMouseDown += (s, e) => {
                 *  Position = Mouse.GetPosition(parentWindow);
                 *  Mode = mouseMode();
                 *
                 *  ModeStart?.Invoke(Mode);
                 * };
                 * parentWindow.PreviewMouseUp += (s, e) => {
                 *  Position = Mouse.GetPosition(parentWindow);
                 *  Mode = mouseMode();
                 *
                 *  ModeEnd?.Invoke(Mode);
                 * }; */
            };
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates an ImageSource based on the latest MultiSourceFrame
        /// and triggers a BitMapReady event
        /// </summary>
        /// <param name="sender">The Sender of the frame (Kinect.sensor)</param>
        /// <param name="e">The MultiSourceFrameEventArgs</param>
        private void frameReader_frameArrived(Object sender, MultiSourceFrameArrivedEventArgs e)
        {
            var reference = e.FrameReference.AcquireFrame();

            using (var frame = reference.ColorFrameReference.AcquireFrame()) {
                if (frame != null)
                {
                    BitMapReadyHandler handler = BitMapReady;
                    ImageSource        img     = ToBitmap(frame);
                    //Allow the image to be accessible outside this thread
                    img.Freeze();
                    Application.Current.Dispatcher.Invoke(new Action(() => handler?.Invoke(img)));
                }
            }

            if (activeBody != null && activeBody.IsTracked)
            {
                HandState handState = (rightHand) ? activeBody.HandRightState :
                                      activeBody.HandLeftState;

                var colorPoint = coordinateMapper.MapCameraPointToColorSpace(
                    activeBody.Joints[handTip].Position);

                Point point = smoother.Next(new System.Drawing.PointF(colorPoint.X, colorPoint.Y));
                if (!point.Equals(prevPoint))
                {
                    FingerPosition = point;
                    prevPoint      = point;
                    Application.Current.Dispatcher.Invoke(new Action(() => FingerPositionChanged?.Invoke(point)));
                }

                CursorModes mode;
                switch (handState)
                {
                case HandState.Lasso:
                    mode = CursorModes.Draw;
                    break;

                case HandState.Open:
                    mode = CursorModes.Erase;
                    break;

                case HandState.Closed:
                case HandState.NotTracked:
                    mode = CursorModes.Idle;
                    break;

                default:
                    return;
                }

                if (mode != CursorMode)
                {
                    if (mode == CursorModes.Idle)
                    {
                        //Compare the tip of the hand to the hand blob
                        //Attempt to see if a finger is extended
                        if (Math.Abs(activeBody.Joints[hand].Position.Z - activeBody.Joints[handTip].Position.Z) > 0.05)
                        {
                            mode = CursorModes.Draw;
                            return;
                        }
                    }

                    if (mode == NextMode)
                    {
                        if (++ModeFrameSkip == FRAME_SKIP_HAND_STATUS)
                        {
                            Application.Current.Dispatcher.Invoke(new Action(() => ModeEnd?.Invoke(CursorMode)));
                            Application.Current.Dispatcher.Invoke(new Action(() => ModeStart?.Invoke(mode)));
                            CursorMode    = mode;
                            ModeFrameSkip = 0;
                        }
                    }
                    else
                    {
                        NextMode = mode;
                    }
                }
            }
        }