Exemplo n.º 1
0
        private LaserRay FindLaserUnder(Ellipse hand)
        {
            var point = new Point(Canvas.GetLeft(hand), canvLaser.ActualHeight - Canvas.GetBottom(hand));
            FrameworkElement touched = canvLaser.InputHitTest(point) as FrameworkElement;

            if (touched == null)
            {
                return(null);
            }
            LaserRay selectedLaser = touched.TemplatedParent as LaserRay;

            if (hand.Tag != null && hand.Tag != selectedLaser) // ancien laser sous la main != laser actuelle
            {
                //((LaserRay)hand.Tag).Fill = Brushes.LightGreen;
                ((LaserRay)hand.Tag).Untouched();
                hand.Tag = null;
            }

            if (selectedLaser != null)
            {
                debugValue.Text = selectedLaser.Tag.ToString();
                //selectedLaser.Fill = Brushes.White;
                selectedLaser.Touched(0, 0);
                hand.Tag = selectedLaser;
            }
            else
            {
                debugValue.Text = string.Format("NO LASER TOUCHED : {0}", touched.Name);
            }
            return(selectedLaser);
        }
Exemplo n.º 2
0
        void kinectRuntime_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            SkeletonFrame skeletonFrame = e.OpenSkeletonFrame();

            if (skeletonFrame == null)
            {
                debugValue.Text = "Skeleton not tracked";
                return;
            }

            Skeleton[] rawskels = new Skeleton[skeletonFrame.SkeletonArrayLength];
            skeletonFrame.CopySkeletonDataTo(rawskels);

            var trackedSkels = rawskels.Where((s) => s.TrackingState != SkeletonTrackingState.NotTracked).Select((s) => s).ToList();


            if (trackedSkels.Count == 0)
            {
                nbFrameWithoutSkeleton++;
                if (nbFrameWithoutSkeleton == 30)
                {
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        sbHideLaser.Begin();
                    }));
                }
                return;
            }

            //SkeletonData data = (from s in skeletonFrame.Skeletons
            //                     where s.TrackingState == SkeletonTrackingState.Tracked
            //                     select s).FirstOrDefault();    // on ne traite que le 1er skelete détecté
            //if (data == null)
            //{
            //    debugValue.Text = "Skeleton not tracked";
            //    return;
            //}

            if (nbFrameWithoutSkeleton != 0)
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    sbShowLaser.Begin();
                }));
            }

            nbFrameWithoutSkeleton = 0;
            if (laserHidden)
            {
                laserHidden = false;
                sbShowLaser.Begin();
                //if (canvLaser.Visibility != Visibility.Visible)
                //    canvLaser.Visibility = Visibility.Visible;
            }

            //var neck = data.Joints[JointID.ShoulderCenter];
            var neck = trackedSkels[0].Joints[JointType.ShoulderCenter];

            MovetoJointPosition(cou, neck);

            //var leftHandJoint = data.Joints[JointID.HandLeft];
            var leftHandJoint = trackedSkels[0].Joints[JointType.HandLeft];

            MovetoJointPosition(leftHand, leftHandJoint);

            if (neck.Position.Z - leftHandJoint.Position.Z > 0.30f) // si la main est au moins a 36cm en avant des épaules.
            {
                leftHand.Fill = Brushes.White;
                LaserRay oldleftLaser = leftHand.Tag as LaserRay;
                var      newleftlaser = FindLaserUnder(leftHand);
                if (newleftlaser != oldleftLaser)
                {
                    if (oldleftLaser != null)
                    {
                        StopNoteFor((string)oldleftLaser.Tag);
                    }
                    if (newleftlaser != null)
                    {
                        PlayMidiNoteFor((string)newleftlaser.Tag);
                    }
                }
            }
            else
            {
                LaserRay leftLaser = leftHand.Tag as LaserRay;
                if (leftLaser != null)
                {
                    StopNoteFor((string)leftLaser.Tag);
                    leftLaser.Untouched();
                }
                leftHand.Fill = Brushes.Red;
            }

            //var rightHandJoint = data.Joints[JointID.HandRight];
            var rightHandJoint = trackedSkels[0].Joints[JointType.HandRight];

            MovetoJointPosition(rightHand, rightHandJoint);
            if (neck.Position.Z - rightHandJoint.Position.Z > 0.30f) // si la main est au moins a 36cm en avant des épaules
            {
                rightHand.Fill = Brushes.White;
                LaserRay oldrightLaser = rightHand.Tag as LaserRay;
                var      newlaser      = FindLaserUnder(rightHand);
                if (newlaser != oldrightLaser)
                {
                    if (oldrightLaser != null)
                    {
                        StopNoteFor((string)oldrightLaser.Tag);
                    }
                    if (newlaser != null)
                    {
                        PlayMidiNoteFor((string)newlaser.Tag);
                    }
                }
            }
            else
            {
                LaserRay rightLaser = rightHand.Tag as LaserRay;
                if (rightLaser != null)
                {
                    StopNoteFor((string)rightLaser.Tag);
                    rightLaser.Untouched();
                }

                rightHand.Fill = Brushes.Red;
            }
        }