//void BaloonExplosion(Ellipse baloon)
        //{
        //    if (baloon.Width < 50)
        //    {
        //        baloon.Height = baloon.Height + 0.4;
        //        baloon.Width = baloon.Height + 0.4;
        //    }
        //    else
        //    {

        //        BaloonsList.Remove(baloon);
        //        return;
        //    }

        //}
        #endregion

        //Check if baloon is caught
        public bool BaloonIsCaught(Joint jointHandLeft, Joint jointHandRight, Ellipse ellipse)
        {
            if (jointHandLeft.Position.X >= CoordinatesConverter.convertX(canvas, Canvas.GetLeft(ellipse)) - 0.1 &&
                jointHandLeft.Position.X <= CoordinatesConverter.convertX(canvas, Canvas.GetLeft(ellipse)) + 0.1 &&
                jointHandLeft.Position.Y >= CoordinatesConverter.convertY(canvas, Canvas.GetBottom(ellipse)) - 0.1 &&
                jointHandLeft.Position.Y <= CoordinatesConverter.convertY(canvas, Canvas.GetBottom(ellipse)) + 0.1 ||

                jointHandRight.Position.X >= CoordinatesConverter.convertX(canvas, Canvas.GetLeft(ellipse)) - 0.1 &&
                jointHandRight.Position.X <= CoordinatesConverter.convertX(canvas, Canvas.GetLeft(ellipse)) + 0.1 &&
                jointHandRight.Position.Y >= CoordinatesConverter.convertY(canvas, Canvas.GetBottom(ellipse)) - 0.1 &&
                jointHandRight.Position.Y <= CoordinatesConverter.convertY(canvas, Canvas.GetBottom(ellipse)) + 0.1)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        private void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            // Get a reference to the multi-frame
            var reference = e.FrameReference.AcquireFrame();

            //Body ver 2 reference
            //var bodyFrame = reference.BodyFrameReference.AcquireFrame();



            using (BodyFrame bodyFrame = reference.BodyFrameReference.AcquireFrame())
            {
                // Open color frame

                using (var frame = reference.ColorFrameReference.AcquireFrame())
                {
                    if (frame != null && bodyFrame != null)
                    {
                        camera.Source = ToBitmap(frame);

                        //Body ver 2
                        bodyFrame.GetAndRefreshBodyData(bodies);
                        imgCanvas.Children.Clear();
                        #region BodyDrawing

                        foreach (var body in bodies)
                        {
                            if (body.IsTracked)
                            {
                                #region Depth Tries
                                // IReadOnlyDictionary<JointType, Joint> joints = body.Joints;

                                // convert the joint points to depth (display) space
                                //Dictionary<JointType, Point> jointPoints = new Dictionary<JointType, Point>();

                                //foreach (JointType jointType in joints.Keys)
                                //{
                                //    // sometimes the depth(Z) of an inferred joint may show as negative
                                //    // clamp down to 0.1f to prevent coordinatemapper from returning (-Infinity, -Infinity)
                                //    CameraSpacePoint position = joints[jointType].Position;
                                //    if (position.Z < 0)
                                //    {
                                //        position.Z = InferredZPositionClamp;
                                //    }

                                //    DepthSpacePoint depthSpacePoint = this.coordinateMapper.MapCameraPointToDepthSpace(position);
                                //    jointPoints[jointType] = new Point(depthSpacePoint.X, depthSpacePoint.Y);
                                //}
                                #endregion

                                textBox_baloonsHitted.Text  = "Hitted: " + Generator.baloonsHitted;
                                textBox_baloonsFloated.Text = "Floated: " + Generator.baloonsFloated;

                                if (Generator.baloonsFloated > 0)
                                {
                                    double percent = Generator.baloonsHitted * 100 / Generator.baloonsFloated;
                                    textBox_percent.Text = percent + "%";
                                }

                                //CameraSpacePoint position = joints[JointType.HandLeft].Position;
                                //DepthSpacePoint depthSpacePoint = this.coordinateMapper.MapCameraPointToDepthSpace(position);
                                //var leftHandPosition = new Point(depthSpacePoint.X, depthSpacePoint.Y);

                                //var testJointEllipse = new Ellipse() { Height = 5, Width = 5, Fill = new SolidColorBrush(Color.FromArgb(255,0,255,0)) };

                                //Canvas.SetTop(testJointEllipse, leftHandPosition.Y);
                                //Canvas.SetLeft(testJointEllipse, leftHandPosition.X);


                                //bodyCanvas.Children.Add(testJointEllipse);

                                //Joint jointHandLeft = jointPoints[JointType.HandLeft];
                                //Joint jointHandRight = jointPoints[JointType.HandRight];


                                Joint jointHandLeft  = body.Joints[JointType.HandLeft];
                                Joint jointHandRight = body.Joints[JointType.HandRight];

                                textBoxHandPosition.Text = String.Format("{0:N2}", jointHandLeft.Position.X) + " : " + String.Format("{0:N2}", jointHandLeft.Position.Y);
                                #region foreachVersion
                                //foreach (var b in Generator.BaloonsList)
                                //{
                                //    //textBoxBaloonPosition.Text = Canvas.GetLeft(b) + " : " + Canvas.GetBottom(b);
                                //    textBoxBaloonPosition.Text = CoordinatesConverter.convertX(baloonCanvas, Canvas.GetLeft(b)) + " : "
                                //        + CoordinatesConverter.convertY(baloonCanvas, Canvas.GetBottom(b));

                                //    if (jointHandLeft.Position.X >= CoordinatesConverter.convertX(baloonCanvas, Canvas.GetLeft(b)) - 0.1 &&
                                //        jointHandLeft.Position.X <= CoordinatesConverter.convertX(baloonCanvas, Canvas.GetLeft(b)) + 0.1 &&
                                //        jointHandLeft.Position.Y >= CoordinatesConverter.convertY(baloonCanvas, Canvas.GetBottom(b)) - 0.1 &&
                                //        jointHandLeft.Position.Y <= CoordinatesConverter.convertY(baloonCanvas, Canvas.GetBottom(b)) + 0.1 ||

                                //        jointHandRight.Position.X >= CoordinatesConverter.convertX(baloonCanvas, Canvas.GetLeft(b)) - 0.1 &&
                                //        jointHandRight.Position.X <= CoordinatesConverter.convertX(baloonCanvas, Canvas.GetLeft(b)) + 0.1 &&
                                //        jointHandRight.Position.Y >= CoordinatesConverter.convertY(baloonCanvas, Canvas.GetBottom(b)) - 0.1 &&
                                //        jointHandRight.Position.Y <= CoordinatesConverter.convertY(baloonCanvas, Canvas.GetBottom(b)) + 0.1)
                                //    {
                                //        b.Fill = new SolidColorBrush(Color.FromRgb(255, 0, 0));
                                //        HittedBaloonsCounter++;
                                //        textBox.Text = "Counter: " + HittedBaloonsCounter;

                                //    }
                                //}
                                #endregion
                                for (int i = Generator.BaloonsList.Count - 1; i >= 0; i--)
                                {
                                    textBoxBaloonPosition.Text = CoordinatesConverter.convertX(baloonCanvas, Canvas.GetLeft(Generator.BaloonsList[i])) + " : "
                                                                 + CoordinatesConverter.convertY(baloonCanvas, Canvas.GetBottom(Generator.BaloonsList[i]));

                                    #region gootjoints
                                    //(jointHandLeft.Position.X >= CoordinatesConverter.convertX(baloonCanvas, Canvas.GetLeft(Generator.BaloonsList[i])) - 0.1 &&
                                    //    jointHandLeft.Position.X <= CoordinatesConverter.convertX(baloonCanvas, Canvas.GetLeft(Generator.BaloonsList[i])) + 0.1 &&
                                    //    jointHandLeft.Position.Y >= CoordinatesConverter.convertY(baloonCanvas, Canvas.GetBottom(Generator.BaloonsList[i])) - 0.1 &&
                                    //    jointHandLeft.Position.Y <= CoordinatesConverter.convertY(baloonCanvas, Canvas.GetBottom(Generator.BaloonsList[i])) + 0.1 ||

                                    //    jointHandRight.Position.X >= CoordinatesConverter.convertX(baloonCanvas, Canvas.GetLeft(Generator.BaloonsList[i])) - 0.1 &&
                                    //    jointHandRight.Position.X <= CoordinatesConverter.convertX(baloonCanvas, Canvas.GetLeft(Generator.BaloonsList[i])) + 0.1 &&
                                    //    jointHandRight.Position.Y >= CoordinatesConverter.convertY(baloonCanvas, Canvas.GetBottom(Generator.BaloonsList[i])) - 0.1 &&
                                    //    jointHandRight.Position.Y <= CoordinatesConverter.convertY(baloonCanvas, Canvas.GetBottom(Generator.BaloonsList[i])) + 0.1)
                                    #endregion

                                    if (jointHandLeft.Position.X >= CoordinatesConverter.convertX(baloonCanvas, Canvas.GetLeft(Generator.BaloonsList[i])) - 0.1 &&
                                        jointHandLeft.Position.X <= CoordinatesConverter.convertX(baloonCanvas, Canvas.GetLeft(Generator.BaloonsList[i])) + 0.1 &&
                                        jointHandLeft.Position.Y >= CoordinatesConverter.convertY(baloonCanvas, Canvas.GetBottom(Generator.BaloonsList[i])) - 0.1 &&
                                        jointHandLeft.Position.Y <= CoordinatesConverter.convertY(baloonCanvas, Canvas.GetBottom(Generator.BaloonsList[i])) + 0.1 ||

                                        jointHandRight.Position.X >= CoordinatesConverter.convertX(baloonCanvas, Canvas.GetLeft(Generator.BaloonsList[i])) - 0.1 &&
                                        jointHandRight.Position.X <= CoordinatesConverter.convertX(baloonCanvas, Canvas.GetLeft(Generator.BaloonsList[i])) + 0.1 &&
                                        jointHandRight.Position.Y >= CoordinatesConverter.convertY(baloonCanvas, Canvas.GetBottom(Generator.BaloonsList[i])) - 0.1 &&
                                        jointHandRight.Position.Y <= CoordinatesConverter.convertY(baloonCanvas, Canvas.GetBottom(Generator.BaloonsList[i])) + 0.1)
                                    {
                                        if (Generator.BaloonsList[i].AllowDrop == false)
                                        {
                                            Generator.BaloonsList[i].AllowDrop = true;
                                            Generator.DestroyBaloon(Generator.BaloonsList[i]);
                                        }



                                        #region withoutExplosion
                                        //Generator.baloonsHitted++;
                                        //Generator.BaloonsList.Remove(Generator.BaloonsList[i]);
                                        #endregion
                                    }
                                }

                                #region SkeletonDrawing
                                //SkeletonDrawing.DrawPoint(JointType.HandLeft, body, _sensor, bodyCanvas);
                                //SkeletonDrawing.DrawPoint(JointType.HandRight, body, _sensor, bodyCanvas);

                                //SkeletonDrawing.DrawLine(JointType.HandLeft, JointType.HandRight, body, bodyCanvas);
                                // SkeletonDrawing.DrawSceleton(body, imgCanvas);

                                #endregion
                            }
                        }
                        #endregion
                    }
                }
            }



            #region body v1
            // Body
            //using (var frame = reference.BodyFrameReference.AcquireFrame())
            //{
            //    if (frame != null)
            //    {
            //        canvas.Children.Clear();

            //        _bodies = new Body[frame.BodyFrameSource.BodyCount];

            //        frame.GetAndRefreshBodyData(_bodies);

            //        foreach (var body in _bodies)
            //        {
            //            if (body != null)
            //            {
            //                if (body.IsTracked)
            //                {
            //                    // Draw skeleton.
            //                    if (_displayBody)
            //                    {
            //                        canvas.DrawSkeleton(body);
            //                    }
            //                }
            //            }
            //        }
            //    }
            //}
            #endregion
        }
        private void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            // Get a reference to the multi-frame
            var reference = e.FrameReference.AcquireFrame();

            using (BodyFrame bodyFrame = reference.BodyFrameReference.AcquireFrame())
            {
                // Open color frame
                using (var frame = reference.ColorFrameReference.AcquireFrame())
                {
                    if (frame != null && bodyFrame != null)
                    {
                        camera.Source = ToBitmap(frame);

                        bodyFrame.GetAndRefreshBodyData(bodies);
                        //  imgCanvas.Children.Clear();

                        bodyCanvas.Children.Clear();



                        foreach (var body in bodies)
                        {
                            if (body.IsTracked)
                            {
                                //Hands joints
                                Joint jointHandLeft  = body.Joints[JointType.HandLeft];
                                Joint jointHandRight = body.Joints[JointType.HandRight];



                                // Textboxes for statistics
                                textBox_baloonsHitted.Text  = "Hitted: " + Generator.baloonsHitted;
                                textBox_baloonsFloated.Text = "Floated: " + Generator.baloonsFloated;
                                textBoxHandPosition.Text    = String.Format("{0:N2}", jointHandLeft.Position.X) + " : " + String.Format("{0:N2}", jointHandLeft.Position.Y);
                                if (Generator.baloonsFloated > 0)
                                {
                                    double percent = Generator.baloonsHitted * 100 / Generator.baloonsFloated;
                                    textBox_percent.Text = percent + "%";
                                }


                                for (int i = Generator.BaloonsList.Count - 1; i >= 0; i--)
                                {
                                    ////Shows position of last baloon
                                    textBoxBaloonPosition.Text = CoordinatesConverter.convertX(baloonCanvas, Canvas.GetLeft(Generator.BaloonsList[i])) + " : "
                                                                 + CoordinatesConverter.convertY(baloonCanvas, Canvas.GetBottom(Generator.BaloonsList[i]));

                                    //Check if ballon is caughted
                                    if (Generator.BaloonIsCaught(jointHandLeft, jointHandRight, Generator.BaloonsList[i]))
                                    {
                                        //Check if baloon is already caughted (so it wont count baloon as catched during explosion animation)
                                        if (Generator.BaloonsList[i].AllowDrop == false)
                                        {
                                            //Mark baloon as already caughted
                                            Generator.BaloonsList[i].AllowDrop = true;
                                            //Destroy baloon
                                            Generator.DestroyBaloon(Generator.BaloonsList[i]);
                                        }
                                    }
                                }



                                jointHandLeft = SkeletonDrawing.ScaleTo(jointHandLeft, bodyCanvas.Width, bodyCanvas.Height);


                                // DepthSpacePoint dsp = _sensor.CoordinateMapper.MapCameraPointToDepthSpace(jointHandLeft.Position);

                                var testJointEllipse = new Ellipse()
                                {
                                    Height = 10, Width = 10, Fill = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0))
                                };

                                bodyCanvas.Children.Add(testJointEllipse);

                                Canvas.SetLeft(testJointEllipse, jointHandLeft.Position.X);
                                Canvas.SetTop(testJointEllipse, jointHandLeft.Position.Y);



                                #region SkeletonDrawing
                                //SkeletonDrawing.DrawPoint(JointType.HandLeft, body, _sensor, bodyCanvas);
                                //SkeletonDrawing.DrawPoint(JointType.HandRight, body, _sensor, bodyCanvas);

                                //SkeletonDrawing.DrawLine(JointType.HandLeft, JointType.HandRight, body, bodyCanvas);
                                //SkeletonDrawing.DrawSceleton(body, bodyCanvas);

                                #endregion
                            }
                        }
                    }
                }
            }
        }