Пример #1
0
        void OnUpdate(object sender, Nui.SkeletonFrameReadyEventArgs e)
        {
            // KINECT Add code to get joint data and smooth it
            if (Application.Current.MainWindow == null || MainWindow.Instance == null) return;

            Nui.SkeletonData skeleton = null;

            PreviousCursorPosition = CursorPosition;

            if (e.SkeletonFrame == null)
            {
                CursorPosition = Mouse.GetPosition(Application.Current.MainWindow);
            }
            else
            {
                foreach (Nui.SkeletonData sd in e.SkeletonFrame.Skeletons)
                {
                    if (sd.TrackingState == Nui.SkeletonTrackingState.Tracked)
                    {
                        skeleton = sd;
                        break;
                    }
                }

                if (skeleton == null) return;

                // Show User's Skeleton when they selects
                //if (MainWindow.Instance.showSkeletons)
                //{
                    drawJoints(skeleton, 1);
                //}

                #region Serialization

                // Initialize
                var _Skeleton = new KinectSkeleton { ankleLeft = skeleton.Joints[Nui.JointID.AnkleLeft],
                                                     ankleRight = skeleton.Joints[Nui.JointID.AnkleRight],
                                                     elbowLeft = skeleton.Joints[Nui.JointID.ElbowLeft],
                                                     elbowRight = skeleton.Joints[Nui.JointID.ElbowRight],
                                                     footLeft = skeleton.Joints[Nui.JointID.FootLeft],
                                                     footRight = skeleton.Joints[Nui.JointID.FootRight],
                                                     handLeft = skeleton.Joints[Nui.JointID.HandLeft],
                                                     handRight = skeleton.Joints[Nui.JointID.HandRight],
                                                     head = skeleton.Joints[Nui.JointID.Head],
                                                     hipCenter = skeleton.Joints[Nui.JointID.HipCenter],
                                                     hipLeft = skeleton.Joints[Nui.JointID.HipLeft],
                                                     hipRight = skeleton.Joints[Nui.JointID.HipRight],
                                                     kneeLeft = skeleton.Joints[Nui.JointID.KneeLeft],
                                                     kneeRight = skeleton.Joints[Nui.JointID.KneeRight],
                                                     shoulderCenter = skeleton.Joints[Nui.JointID.ShoulderCenter],
                                                     shoulderLeft = skeleton.Joints[Nui.JointID.ShoulderLeft],
                                                     shoulderRight = skeleton.Joints[Nui.JointID.ShoulderRight],
                                                     spine = skeleton.Joints[Nui.JointID.Spine],
                                                     wristLeft = skeleton.Joints[Nui.JointID.WristLeft],
                                                     wristRight = skeleton.Joints[Nui.JointID.WristRight] };

                var _Serializer = new XmlSerializer(typeof(KinectSkeleton));
                StringBuilder _Builder = new StringBuilder();
                using (StringWriter _Writer = new StringWriter(_Builder))
                {
                    _Serializer.Serialize(_Writer, _Skeleton);
                }

                m_xmlSkeleton = _Builder.ToString();

                #endregion Serialization

                Nui.Vector nuiv = skeleton.Joints[Nui.JointID.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 1.0f, 0.5f).Position;
                CursorPosition = new Point(nuiv.X, nuiv.Y);
            }

            // Update which graphical element the cursor is currently over
            if (!Passive)
                UpdateElementOver();

            if (MainWindow.Instance.NuiRuntime == null)
            {
                // For mouse, see if the right mouse button is down.
                if (_isPainting)
                {
                    if (Mouse.LeftButton == MouseButtonState.Released)
                    {
                        // MainWindow.Instance.StopPainting();
                    }
                }
                else
                {
                    if (Mouse.LeftButton == MouseButtonState.Pressed)
                    {
                        // MainWindow.Instance.StartPainting();
                    }
                }

                _isPainting = Mouse.LeftButton == MouseButtonState.Pressed;
            }
            else
            {
                if (MainWindow.Instance.isUserConnected)
                {
                    // Send my cursor position to remote user
                    MainWindow.Instance.SendCursorPosition((int)CursorPosition.X, (int)CursorPosition.Y, m_xmlSkeleton);
                }

                // To begin painting w/ Kinect, raise your left hand above your shoulder.
                // To stop painting, lower it.

                Nui.Joint lh = skeleton.Joints[Nui.JointID.HandLeft];
                Nui.Joint ls = skeleton.Joints[Nui.JointID.ShoulderLeft];

                bool isup = lh.Position.Y > ls.Position.Y;

                if (isup != _isPainting)
                {
                    _isPainting = isup;

                    if (_isPainting){
                        // MainWindow.Instance.StartPainting();
                    }
                    else{
                        // MainWindow.Instance.StopPainting();
                    }
                }

                // Image number
                int number = MainWindow.Instance.rectangleNumber;

                if (_isSlected && (number != 0))
                {
                    Rectangle selectedRectangle = new Rectangle();

                    switch (number)
                    {
                        case 0:
                            break;
                        case 1:
                            selectedRectangle = MainWindow.Instance.R1;
                            break;
                        case 2:
                            selectedRectangle = MainWindow.Instance.R2;
                            break;
                        case 3:
                            selectedRectangle = MainWindow.Instance.R3;
                            break;
                        case 4:
                            selectedRectangle = MainWindow.Instance.R4;
                            break;
                        case 5:
                            selectedRectangle = MainWindow.Instance.R5;
                            break;
                        case 6:
                            selectedRectangle = MainWindow.Instance.R6;
                            break;
                        case 7:
                            selectedRectangle = MainWindow.Instance.R7;
                            break;
                        case 8:
                            selectedRectangle = MainWindow.Instance.R8;
                            break;
                        case 9:
                            selectedRectangle = MainWindow.Instance.R9;
                            break;
                    }

                    if (selectedRectangle != null)
                    {
                        // When rectangle is moving
                        selectedRectangle.Opacity = 0.5;
                        // selectedRectangle.IsHitTestVisible = false;
                        Canvas.SetZIndex(selectedRectangle, 10);

                        if (isup)
                        {
                            _isSlected = false;
                            selectedRectangle.Opacity = 1.0;
                            // selectedRectangle.IsHitTestVisible = true;
                            Canvas.SetZIndex(selectedRectangle, 0);

                            // Send which image is moving to the remote user
                            MainWindow.Instance.SendMovingImage(number, _isSlected);
                        }
                        else
                        {
                            // Set moving image position on the Canvas
                            // Canvas.SetLeft(selectedRectangle, CursorPosition.X - selectedRectangle.ActualWidth / 2);
                            // Canvas.SetTop(selectedRectangle, CursorPosition.Y - (140 + selectedRectangle.ActualHeight / 2));

                            // Canvas.SetLeft(selectedRectangle, CursorPosition.X - selectedRectangle.ActualWidth / 2 - 140);
                            // Canvas.SetTop(selectedRectangle, CursorPosition.Y - (140 + selectedRectangle.ActualHeight / 2));

                            Canvas.SetLeft(selectedRectangle, CursorPosition.X - selectedRectangle.ActualWidth / 2);
                            Canvas.SetTop(selectedRectangle, CursorPosition.Y - selectedRectangle.ActualHeight / 2);

                            // Send which image is moving to the remote user
                            MainWindow.Instance.SendMovingImage(number, _isSlected);
                        }
                    }
                }
            }
        }
Пример #2
0
        //void drawJoints(Nui.SkeletonData data, int playerIndx)
        void drawJoints(KinectSkeleton data, int playerIndx, bool showSkeletons)
        {
            // Draw bones
            Brush brush = Brushes.Black;
            ArrayList CurrentBodySegments = new ArrayList();
            ArrayList CurrentBodyJoints = new ArrayList();
            ArrayList _Joints = new ArrayList();

            _Joints.Add(data.ankleLeft);
            _Joints.Add(data.ankleRight);
            _Joints.Add(data.elbowLeft);
            _Joints.Add(data.elbowRight);
            _Joints.Add(data.footLeft);
            _Joints.Add(data.footRight);
            _Joints.Add(data.handLeft);
            _Joints.Add(data.handRight);
            _Joints.Add(data.head);
            _Joints.Add(data.hipCenter);
            _Joints.Add(data.hipLeft);
            _Joints.Add(data.hipRight);
            _Joints.Add(data.kneeLeft);
            _Joints.Add(data.kneeRight);
            _Joints.Add(data.shoulderCenter);
            _Joints.Add(data.shoulderLeft);
            _Joints.Add(data.shoulderRight);
            _Joints.Add(data.spine);
            _Joints.Add(data.wristLeft);
            _Joints.Add(data.wristRight);

            switch (playerIndx)
            {
                case 1:
                    brush = Brushes.Black;
                    CurrentBodySegments = BodySegments;
                    CurrentBodyJoints = BodyJoints;
                    break;
            }

            //remove current skeleton from canvas
            foreach (Polyline p in CurrentBodySegments)
            {
                MainWindow.Instance.canvas1.Children.Remove(p);
            }

            //clear BodySegments Array
            CurrentBodySegments.Clear();

            if (data != null)
            {
                //BodySegments add
                CurrentBodySegments.Add(getBodySegment(brush, data.hipCenter, data.spine, data.shoulderCenter, data.head));
                CurrentBodySegments.Add(getBodySegment(brush, data.shoulderCenter, data.shoulderLeft, data.elbowLeft, data.wristLeft, data.handLeft));
                CurrentBodySegments.Add(getBodySegment(brush, data.shoulderCenter, data.shoulderRight, data.elbowRight, data.wristRight, data.handRight));
                CurrentBodySegments.Add(getBodySegment(brush, data.hipCenter, data.hipLeft, data.kneeLeft, data.ankleLeft, data.footLeft));
                CurrentBodySegments.Add(getBodySegment(brush, data.hipCenter, data.hipRight, data.kneeRight, data.ankleRight, data.footRight));

                //BodySegments.Add(getBodySegment(data.Joints, brush, JointID.ShoulderRight, JointID.HipRight, JointID.HipLeft, JointID.ShoulderLeft));

                foreach (Polyline p in CurrentBodySegments)
                {
                    p.Opacity = 0.50;

                    // Show Body Skeletons when 'showSkeletons' is true
                    if (showSkeletons)
                    {
                        MainWindow.Instance.canvas1.Children.Add(p);
                    }
                    else
                    {
                        MainWindow.Instance.canvas1.Children.Remove(p);
                    }
                }
            }

            foreach (UIElement l in CurrentBodyJoints)
            {
                MainWindow.Instance.canvas1.Children.Remove(l);
            }
            CurrentBodyJoints.Clear();

            // Draw joints
            if (data != null)
            {
                foreach (Joint joint in _Joints)
                {
                    // Console.WriteLine("join name: " + joint.ID);
                    Point jointPos = getDisplayPosition(joint);
                    if (joint.ID == JointID.Head)
                    {
                        // Console.WriteLine("FOUND THE HEAD");
                        Ellipse head = new Ellipse();
                        head.Width = 50;
                        head.Height = 50;
                        head.Fill = Brushes.Black;
                        head.Opacity = 0.20;

                        Canvas.SetLeft(head, (int)jointPos.X - 25);
                        Canvas.SetTop(head, (int)jointPos.Y - 25);

                        CurrentBodyJoints.Add(head);

                        TextBlock name = new TextBlock();
                        name.Text = "Friend";
                        name.FontSize = 30;
                        name.Width = 100;
                        name.Height = 50;
                        name.Foreground = Brushes.Blue;

                        Canvas.SetLeft(name, (int)jointPos.X - 25 + 80);
                        Canvas.SetTop(name, (int)jointPos.Y - 25 - 20);

                        CurrentBodyJoints.Add(name);

                    }
                    else if (joint.ID == JointID.HandRight)
                    {
                        Ellipse el = new Ellipse();
                        el.Width = 20;
                        el.Height = 20;
                        el.Fill = Brushes.Black;
                        el.Opacity = 0.20;

                        Canvas.SetLeft(el, (int)jointPos.X - 25);
                        Canvas.SetTop(el, (int)jointPos.Y - 25);

                        CurrentBodyJoints.Add(el);
                    }
                }

                foreach (UIElement l in CurrentBodyJoints)
                {
                    // Show Head, RightHand, Name
                    if (showSkeletons)
                    {
                        MainWindow.Instance.canvas1.Children.Add(l);
                    }
                    else
                    {
                        MainWindow.Instance.canvas1.Children.Remove(l);
                    }
                }
            }
        }