示例#1
0
        /// <summary>
        /// 手势的处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void OnInteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            using (InteractionFrame frame = e.OpenInteractionFrame())
            {
                if (frame != null)
                {
                    if (this.userInfos == null)
                    {
                        this.userInfos = new UserInfo[InteractionFrame.UserInfoArrayLength];
                    }

                    frame.CopyInteractionDataTo(this.userInfos);
                }
                else
                {
                    return;
                }
            }
            /////////////
            foreach (UserInfo userInfo in this.userInfos)
            {
                foreach (InteractionHandPointer handPointer in userInfo.HandPointers)
                {
                    // string action = null;

                    switch (handPointer.HandEventType)
                    {
                    case InteractionHandEventType.Grip:
                        //action = "gripped";
                        //isDrawing.Content = "isDrawing: True";
                        isgrip = true;
                        break;

                    case InteractionHandEventType.GripRelease:
                        // action = "released";
                        isgrip = false;
                        break;
                    }

                    //if (action != null)
                    //{
                    //    string handSide = "unknown";

                    //    switch (handPointer.HandType)
                    //    {
                    //        case InteractionHandType.Left:
                    //            handSide = "left";
                    //            break;

                    //        case InteractionHandType.Right:
                    //            handSide = "right";
                    //            break;
                    //    }
                    // Console.WriteLine("User " + userInfo.SkeletonTrackingId + " " + action + " their " + handSide + "hand.");
                    //}
                }
            }
        }
示例#2
0
 void stream_InteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
 {
     using (InteractionFrame interactionFrame = e.OpenInteractionFrame())
     {
         if (interactionFrame != null)
         {
             interactionFrame.CopyInteractionDataTo(this.infos);
         }
     }
 }
示例#3
0
        /// <summary>
        /// Evènement : si on a les informations de profondeur et du squelette -> définit l'état actuel de la main
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EventInteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            UserInfo[] userInfos = new UserInfo[InteractionFrame.UserInfoArrayLength];
            using (InteractionFrame interactionFrame = e.OpenInteractionFrame())
            {
                if (interactionFrame == null)
                {
                    return;
                }

                interactionFrame.CopyInteractionDataTo(userInfos);
            }

            // On parcourt les informations trouvées
            bool handFound = false;

            foreach (UserInfo userInfo in userInfos)
            {
                // Si mal reconnu ou on déjà trouvé une main qui est affichée
                if (userInfo.SkeletonTrackingId == 0 || handFound)
                {
                    continue;
                }

                var hands = userInfo.HandPointers;
                if (hands.Count == 0) // Si aucune main reconnue
                {
                    continue;
                }

                foreach (var hand in hands)
                {
                    // La main doit être trackée, active et la main 'primaire' (= première levée des deux)
                    if (!hand.IsActive || !hand.IsTracked)
                    {
                        continue;
                    }

                    handFound      = true;
                    actualHandType = hand.HandType;

                    // Si la main devient grip et c'était pas son ancien état
                    if (hand.HandEventType == InteractionHandEventType.Grip && hand.HandEventType != actualHandState)
                    {
                        actualHandState = InteractionHandEventType.Grip;
                    }
                    // Si la main lâche le grip
                    else if (hand.HandEventType == InteractionHandEventType.GripRelease && hand.HandEventType != actualHandState)
                    {
                        actualHandState = InteractionHandEventType.GripRelease;
                    }
                }
            }
        }
示例#4
0
        private void interactStream_InteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            using (InteractionFrame interactFrame = e.OpenInteractionFrame())
            {
                if (interactFrame != null && masterKinectSettings.mergeSkeletons)
                {
                    UserInfo[] tempUserInfo = new UserInfo[6];
                    interactFrame.CopyInteractionDataTo(tempUserInfo);

                    foreach (UserInfo interactionInfo in tempUserInfo)
                    {
                        foreach (InteractionHandPointer hand in interactionInfo.HandPointers)
                        {
                            if (hand.HandEventType == InteractionHandEventType.Grip)
                            {
                                for (int i = 0; i < skeletonHandGrabData.Count; i++)
                                {
                                    if (skeletonHandGrabData[i].skeletonTrackingID == interactionInfo.SkeletonTrackingId)
                                    {
                                        if (hand.HandType == InteractionHandType.Left)
                                        {
                                            skeletonHandGrabData[i].leftHandClosed = true;
                                        }
                                        else if (hand.HandType == InteractionHandType.Right)
                                        {
                                            skeletonHandGrabData[i].rightHandClosed = true;
                                        }
                                        break;
                                    }
                                }
                            }
                            else if (hand.HandEventType == InteractionHandEventType.GripRelease)
                            {
                                for (int i = 0; i < skeletonHandGrabData.Count; i++)
                                {
                                    if (skeletonHandGrabData[i].skeletonTrackingID == interactionInfo.SkeletonTrackingId)
                                    {
                                        if (hand.HandType == InteractionHandType.Left)
                                        {
                                            skeletonHandGrabData[i].leftHandClosed = false;
                                        }
                                        else if (hand.HandType == InteractionHandType.Right)
                                        {
                                            skeletonHandGrabData[i].rightHandClosed = false;
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
示例#5
0
        static void OnInteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            using (InteractionFrame frame = e.OpenInteractionFrame())
            {
                if (frame != null)
                {
                    frame.CopyInteractionDataTo(userInfos);

                    foreach (UserInfo userInfo in userInfos)
                    {
                        foreach (InteractionHandPointer handPointer in userInfo.HandPointers)
                        {
                            string action = null;

                            switch (handPointer.HandEventType)
                            {
                            case InteractionHandEventType.Grip:
                                action = "gripped";
                                break;

                            case InteractionHandEventType.GripRelease:
                                action = "released";
                                break;
                            }

                            if (action != null)
                            {
                                string handSide = "unknown";

                                switch (handPointer.HandType)
                                {
                                case InteractionHandType.Left:
                                    handSide = "left";
                                    break;

                                case InteractionHandType.Right:
                                    handSide = "right";
                                    break;
                                }

                                Console.WriteLine("{\"hand\":" + handSide + ",\"action\":" + action + "}");
                                foreach (var socket in _clients)
                                {
                                    socket.Send("{\"hand\":\"" + handSide + "\",\"action\":\"" + action + "\"}");
                                }
                            }
                        }
                    }
                }
            }
        }
示例#6
0
        private void fluxoInteracao_InteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            using (InteractionFrame quadro = e.OpenInteractionFrame())
            {
                if (quadro == null)
                {
                    return;
                }

                UserInfo[] informacoesUsuarios = new UserInfo[6];
                quadro.CopyInteractionDataTo(informacoesUsuarios);
                IEnumerable <UserInfo> usuariosRastreados = informacoesUsuarios.Where(info => info.SkeletonTrackingId != 0);
                if (usuariosRastreados.Count() > 0)
                {
                    UserInfo usuarioPrincipal = usuariosRastreados.First();

                    if (usuarioPrincipal.HandPointers[0].HandEventType == InteractionHandEventType.Grip)
                    {
                        configuracaoMaoEsquerda.DesenhoAtivo = true;
                    }
                    else if (usuarioPrincipal.HandPointers[0].HandEventType == InteractionHandEventType.GripRelease)
                    {
                        configuracaoMaoEsquerda.DesenhoAtivo = false;
                    }

                    if (usuarioPrincipal.HandPointers[1].HandEventType == InteractionHandEventType.Grip)
                    {
                        configuracaoMaoDireita.DesenhoAtivo = true;
                    }
                    else if (usuarioPrincipal.HandPointers[1].HandEventType == InteractionHandEventType.GripRelease)
                    {
                        configuracaoMaoDireita.DesenhoAtivo = false;
                    }
                }
            }
        }
        private void interStream_InteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            using (InteractionFrame inf = e.OpenInteractionFrame())
            {
                if (inf == null)
                {
                    return;
                }
                inf.CopyInteractionDataTo(userInfos);
            }

            foreach (UserInfo ui in userInfos)
            {
                var hands = ui.HandPointers;
                if (ui.SkeletonTrackingId == 0)
                {
                    continue;
                }

                foreach (InteractionHandPointer hand in hands)
                {
                    last = hand.HandEventType == InteractionHandEventType.None ? last : hand.HandEventType;
                    if (last == InteractionHandEventType.Grip)
                    {
                        checkDraw.IsChecked = true;
                    }
                    else
                    {
                        if (checkDraw.IsChecked == true)
                        {
                            checkDraw.IsChecked = false;
                        }
                    }
                }
            }
        }
示例#8
0
        private void StreamOnInteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            using (InteractionFrame interactionFrame = e.OpenInteractionFrame())
            {
                if (interactionFrame != null)
                {
                    if (userInfos == null)
                    {
                        userInfos = new UserInfo[InteractionFrame.UserInfoArrayLength];
                    }
                    interactionFrame.CopyInteractionDataTo(userInfos);
                }
                else
                {
                    return;
                }

                foreach (UserInfo player in userInfos)
                {
                    foreach (InteractionHandPointer handPointer in player.HandPointers)
                    {
                        int id = player.SkeletonTrackingId;

                        for (int i = 0; i < skeletonList.Length; i++)
                        {
                            if (id == skeletonList[i].TrackingId)
                            {
                                index = i;
                            }
                        }

                        //Trace.WriteLine(index);
                        headText = skeletonList[index].Joints[JointType.Head].Position.X + " " + skeletonList[index].Joints[JointType.Head].Position.Y + " " + skeletonList[index].Joints[JointType.Head].Position.Z;
                        string action = null;
                        switch (handPointer.HandEventType)
                        {
                        case InteractionHandEventType.Grip:
                            action = "Hand Gripped";
                            break;

                        case InteractionHandEventType.GripRelease:
                            action = "Hand Released";
                            break;
                        }
                        if (action != null)
                        {
                            string handSide = "unknown";
                            switch (handPointer.HandType)
                            {
                            case InteractionHandType.Left:
                                handSide = "Left";
                                break;

                            case InteractionHandType.Right:
                                handSide = "Right";
                                break;
                            }

                            handText = handSide + " " + action;
                            //this.label1.Content = labelText;
                            udpConnection.SendData(System.Text.Encoding.Default.GetBytes(handText));
                        }
                        if (handPointer.HandType == InteractionHandType.Right && handPointer.IsTracked)
                        {
                            string pos = "pos: " + handPointer.RawX + " " + handPointer.RawY + " " + handPointer.RawZ + " " + headText;
                            Trace.WriteLine(pos);
                            udpConnection.SendData(pos);
                        }
                    }
                }
            }
        }
        void inStream_InteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            // if wrong state
            //onRaisedVoiceCommand(new VoiceCommandEventArgs("I'm in state" + state + " and I got interaciton data"));
            if (state < 2 || state > 3)
            {
                return;
            }
            if (frameout < 4)
            {
                frameout++;
                return;
            }
            //onRaisedVoiceCommand(new VoiceCommandEventArgs("I'm in state" + state));
            InteractionFrame intFrame = e.OpenInteractionFrame();

            UserInfo[] userInfos = new UserInfo[6];
            if (intFrame != null)
            {
                intFrame.CopyInteractionDataTo(userInfos); // copy userinfos to array
                frameout = 0;
            }
            else
            {
                return;
            }
            foreach (UserInfo userInfo in userInfos)
            {
                foreach (InteractionHandPointer handPointer in userInfo.HandPointers)
                {
                    // if at state 2 and grip is detected
                    if (handPointer.IsPrimaryForUser && handPointer.IsTracked &&
                        handPointer.HandEventType == InteractionHandEventType.Grip && state == 2)
                    {
                        skeletonID = userInfo.SkeletonTrackingId;
                        if (this.command != "ZOOM")
                        {
                            initialValue = handPointer.RawY;
                        }
                        else
                        {
                            initialValue = handPointer.RawZ;
                        }
                        handType = handPointer.HandType;
                        String message = "Gesture command " + command.ToString() + " began with an initial value of " + initialValue;
                        //onRaisedGestureCommand(new GestureEventArgs(message, command, initialValue));
                        onRaisedVoiceCommand(new VoiceCommandEventArgs(message));
                        state = 3; // move onto next stage
                    }
                    // at state 3 grip has not released
                    if (handPointer.IsPrimaryForUser && handPointer.IsTracked && userInfo.SkeletonTrackingId == skeletonID &&
                        handPointer.HandEventType == InteractionHandEventType.None && state == 3 && handPointer.HandType == handType)
                    {
                        if (this.command != "ZOOM")
                        {
                            currentValue = handPointer.RawY;
                        }
                        else
                        {
                            currentValue = handPointer.RawZ;
                        }
                        String message = "Gesture command " + command.ToString() + " with a value of " + currentValue;
                        onRaisedGestureCommand(new GestureEventArgs(message, command, initialValue - currentValue));
                    }
                    // grip released detected at state 3
                    if (handPointer.IsPrimaryForUser && handPointer.IsTracked && userInfo.SkeletonTrackingId == skeletonID &&
                        handPointer.HandEventType == InteractionHandEventType.GripRelease && state == 3 && handPointer.HandType == handType)
                    {
                        //currentValue = handPointer.RawY;
                        String message = "Gesture command " + command.ToString() + " ended with a value of " + currentValue + " and initial value of " + initialValue + " and a difference of " + (currentValue - initialValue);
                        onRaisedGestureCommand(new GestureEventArgs(message, command, initialValue - currentValue));
                        state = 1; // return to initial stage
                    }
                }
            }
        }
示例#10
0
        private void StreamOnInteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            using (InteractionFrame interactionFrame = e.OpenInteractionFrame())
            {
                if (interactionFrame != null)
                {
                    if (userInfos == null)
                    {
                        userInfos = new UserInfo[InteractionFrame.UserInfoArrayLength];
                    }
                    interactionFrame.CopyInteractionDataTo(userInfos);
                }
                else
                {
                    return;
                }

                foreach (UserInfo player in userInfos)
                {
                    foreach (InteractionHandPointer handPointer in player.HandPointers)
                    {
                        string action = null;
                        switch (handPointer.HandEventType)
                        {
                        case InteractionHandEventType.Grip:
                            action = "Hand Gripped";
                            break;

                        case InteractionHandEventType.GripRelease:
                            action = "Hand Released";
                            break;
                        }
                        if (action != null)
                        {
                            string handSide = "unknown";
                            switch (handPointer.HandType)
                            {
                            case InteractionHandType.Left:
                                handSide = "Left";
                                break;

                            case InteractionHandType.Right:
                                handSide = "Right";
                                break;
                            }

                            labelText = handSide + " " + action;



                            this.label1.Content = labelText;
                            udpConnection.SendData(System.Text.Encoding.Default.GetBytes(labelText));
                        }
                        if (handPointer.HandType == InteractionHandType.Right && handPointer.IsTracked)
                        {
                            string pos = "pos: " + handPointer.RawX + " " + handPointer.RawY + " " + handPointer.RawZ;
                            Trace.WriteLine(pos);
                            udpConnection.SendData(pos);
                        }
                    }
                }
            }
        }
示例#11
0
        private void interactStream_InteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            using (InteractionFrame interactFrame = e.OpenInteractionFrame())
            {
                if (interactFrame != null && server.serverMasterOptions.kinectOptions[kinectID].trackSkeletons)
                {
                    bool       changeMade   = false;
                    UserInfo[] tempUserInfo = new UserInfo[6];
                    interactFrame.CopyInteractionDataTo(tempUserInfo);

                    foreach (UserInfo interactionInfo in tempUserInfo)
                    {
                        foreach (InteractionHandPointer hand in interactionInfo.HandPointers)
                        {
                            if (hand.HandEventType == InteractionHandEventType.Grip)
                            {
                                for (int i = 0; i < skeletonData.actualSkeletons.Count; i++)
                                {
                                    if (skeletonData.actualSkeletons[i].skeleton.TrackingId == interactionInfo.SkeletonTrackingId)
                                    {
                                        if (hand.HandType == InteractionHandType.Left)
                                        {
                                            skeletonData.actualSkeletons[i].leftHandClosed = true;
                                            changeMade = true;
                                        }
                                        else if (hand.HandType == InteractionHandType.Right)
                                        {
                                            skeletonData.actualSkeletons[i].rightHandClosed = true;
                                            changeMade = true;
                                        }
                                        break;
                                    }
                                }
                            }
                            else if (hand.HandEventType == InteractionHandEventType.GripRelease)
                            {
                                for (int i = 0; i < skeletonData.actualSkeletons.Count; i++)
                                {
                                    if (skeletonData.actualSkeletons[i].skeleton.TrackingId == interactionInfo.SkeletonTrackingId)
                                    {
                                        if (hand.HandType == InteractionHandType.Left)
                                        {
                                            skeletonData.actualSkeletons[i].leftHandClosed = false;
                                            changeMade = true;
                                        }
                                        else if (hand.HandType == InteractionHandType.Right)
                                        {
                                            skeletonData.actualSkeletons[i].rightHandClosed = false;
                                            changeMade = true;
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    lock (skeletonData)
                    {
                        //The grab isn't going to update very often, so lets just force an update when it is actually needed
                        skeletonData.needsUpdate = changeMade;
                    }
                }
            }
        }
示例#12
0
        /// <summary>
        /// Event handler for InteractionStream's InteractionFrameReady event
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void InteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            // Check for a null userInfos since we may still get posted events
            // from the stream after we have unregistered our event handler and
            // deleted our buffers.
            if (this.userInfos == null)
            {
                return;
            }

            UserInfo[] localUserInfos = null;
            long       timestamp      = 0;

            using (InteractionFrame interactionFrame = e.OpenInteractionFrame())
            {
                if (interactionFrame != null)
                {
                    // Copy interaction frame data so we can dispose interaction frame
                    // right away, even if data processing/event handling takes a while.
                    interactionFrame.CopyInteractionDataTo(this.userInfos);
                    timestamp      = interactionFrame.Timestamp;
                    localUserInfos = this.userInfos;
                }
            }

            if (localUserInfos != null)
            {
                //// TODO: Process user info data, perform hit testing with UI, route UI events, etc.
                //// TODO: See KinectRegion and KinectAdapter in Microsoft.Kinect.Toolkit.Controls assembly
                //// TODO: For a more comprehensive example on how to do this.

                var currentUserSet = new HashSet <int>();
                var usersToRemove  = new HashSet <int>();

                // Keep track of current users in scene
                foreach (var info in localUserInfos)
                {
                    if (info.SkeletonTrackingId == InvalidTrackingId)
                    {
                        // Only look at user information corresponding to valid users
                        continue;
                    }

                    if (!this.trackedUsers.Contains(info.SkeletonTrackingId))
                    {
                        Console.WriteLine(Converter.EncodeNewInteractionUser(info.SkeletonTrackingId));
                    }

                    currentUserSet.Add(info.SkeletonTrackingId);
                    this.trackedUsers.Add(info.SkeletonTrackingId);

                    // Perform hit testing and look for Grip and GripRelease events
                    foreach (var handPointer in info.HandPointers)
                    {
                        Console.WriteLine(Converter.EncodeInteraction(info.SkeletonTrackingId,
                                                                      (HandEventType)handPointer.HandEventType,
                                                                      (HandType)handPointer.HandType, (float)handPointer.X, (float)handPointer.Y, (float)handPointer.PressExtent,
                                                                      handPointer.IsActive, handPointer.IsInteractive, handPointer.IsPressed, handPointer.IsTracked));
                    }
                }

                foreach (var id in this.trackedUsers)
                {
                    if (!currentUserSet.Contains(id))
                    {
                        usersToRemove.Add(id);
                    }
                }

                foreach (var id in usersToRemove)
                {
                    this.trackedUsers.Remove(id);
                    Console.WriteLine(Converter.EncodeInteractionUserLeft(id));
                }
            }
        }
示例#13
0
        /// <summary>
        /// Manejador de eventos para el evento del sensor de kinect InteractionStream sacado y adaptado de:
        /// http://dotneteers.net/blogs/vbandi/archive/2013/05/03/kinect-interactions-with-wpf-part-iii-demystifying-the-interaction-stream.aspx
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void InteractionStreamOnInteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            using (InteractionFrame frame = e.OpenInteractionFrame())
            {
                if (frame != null)
                {
                    if (this.userInfos == null)
                    {
                        this.userInfos = new UserInfo[InteractionFrame.UserInfoArrayLength];
                    }
                    //Copiamos los datos de interacción.
                    frame.CopyInteractionDataTo(this.userInfos);
                }
                else
                {
                    return;
                }
            }

            foreach (UserInfo userInfo in this.userInfos)
            {
                foreach (InteractionHandPointer handPointer in userInfo.HandPointers)
                {
                    //Inicializamos la acción que realizará.
                    string action = null;

                    //Comprobamos cual es la acción y asignamos la acción que realice el evento de la mano.
                    switch (handPointer.HandEventType)
                    {
                    //Si cerramos la mano asignamos gripped a la acción
                    case InteractionHandEventType.Grip:
                        action = "gripped";
                        break;

                    //Si abrimos la mano asinamos released a la acción
                    case InteractionHandEventType.GripRelease:
                        action = "released";

                        break;
                    }

                    if (action != null)
                    {
                        //Iniciamos que mano es la que está realizando la acción a desconocida.
                        string handSide = "unknown";

                        //Comprobamos que mano es la que realiza la acción y asignamos left o right dependiendo de que mano es.
                        switch (handPointer.HandType)
                        {
                        case InteractionHandType.Left:
                            handSide = "left";
                            break;

                        case InteractionHandType.Right:
                            handSide = "right";
                            break;
                        }

                        // Dependiendo de la mano hará una acción u otra al abrir y cerrar,
                        // en nuestro caso son las mismas acciones para ambas manos pero dibujará un puño cerrado o abierto dependiendo de que mano esté realizando la acción.
                        //Si la acción es released llamaremos a la función soltar de la clase Puzzle.cs
                        //Si es coger llamará a la función coger de la clase Puzzle.cs
                        //Ver Puzzle.cs para saber lo que hacen.
                        if (handSide == "left")
                        {
                            if (action == "released")
                            {
                                puzzle.soltar(handSide);
                                manoIzq = manoIzqA;
                            }
                            else
                            {
                                puzzle.coger(handSide);
                                manoIzq = manoIzqC;
                            }
                        }
                        else
                        {
                            if (action == "released")
                            {
                                puzzle.soltar(handSide);
                                manoDer = manoDerA;
                            }
                            else
                            {
                                puzzle.coger(handSide);
                                manoDer = manoDerC;
                            }
                        }
                    }
                }
            }
        }
        private void InteractionStream_InteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            using (InteractionFrame interactionFrame = e.OpenInteractionFrame()) //dispose as soon as possible
            {
                if (interactionFrame == null)
                {
                    return;
                }

                interactionFrame.CopyInteractionDataTo(userInfos);
            }

            //select our user and get his data
            var userInfo = userInfos.FirstOrDefault(uInfo => uInfo.SkeletonTrackingId != 0);

            if (userInfo == null)
            {
                return;
            }

            var userID = userInfo.SkeletonTrackingId;

            hands = userInfo.HandPointers;

            //if at least one hand has been found
            if (hands.Count > 0)
            {
                var mySkeleton = skeletons.FirstOrDefault(skeleton => skeleton.TrackingState == SkeletonTrackingState.Tracked);

                var leftHand  = hands.FirstOrDefault(hand => hand.HandType == InteractionHandType.Left);
                var rightHand = hands.FirstOrDefault(hand => hand.HandType == InteractionHandType.Right);

                if (leftHand == null || rightHand == null)
                {
                    return;
                }

                var leftHandEvent  = DetermineLastHandEvent(leftHand, userID);
                var rightHandEvent = DetermineLastHandEvent(rightHand, userID);

                var leftHandJoint  = mySkeleton.Joints[JointType.HandLeft];
                var rightHandJoint = mySkeleton.Joints[JointType.HandRight];

                LHandText = $"Tracking status: {leftHand.IsTracked}\nX: {Math.Round(leftHand.RawX, 1)} Y: {Math.Round(leftHand.RawY, 1)} Z: {Math.Round(leftHand.RawZ, 1)}\nState: {leftHandEvent}";
                RHandText = $"Tracking status: {rightHand.IsTracked}\nX: {Math.Round(rightHand.RawX, 1)} Y: {Math.Round(rightHand.RawY, 1)} Z: {Math.Round(rightHand.RawZ, 1)}\nState: {rightHandEvent}";

                LeftHandMarker.UpdateUI(kinectSensor, leftHandJoint, leftHandEvent, leftHandEvent);
                NotifyPropertyChanged("LeftHandMarker");
                RightHandMarker.UpdateUI(kinectSensor, rightHandJoint, leftHandEvent, rightHandEvent);
                NotifyPropertyChanged("RightHandMarker");

                if (Manipulator == null || !Manipulator.Connected)
                {
                    return;
                }

                if (leftHandEvent == InteractionHandEventType.Grip)
                {
                    const int multiplication = 100; //tbd

                    if (sample)
                    {
                        if (rightHandEvent == InteractionHandEventType.Grip && lastGripCommand.Equals(GripOpen))
                        {
                            Manipulator?.SendCustom(GripClose);
                            lastGripCommand = GripClose;
                            wasGripSent     = true;
                        }
                        else if (rightHandEvent == InteractionHandEventType.GripRelease && lastGripCommand.Equals(GripClose))
                        {
                            Manipulator?.SendCustom(GripOpen);
                            lastGripCommand = GripOpen;
                            wasGripSent     = true;
                        }


                        if (!wasGripSent)
                        {
                            var moveCommand = $"DS {(int)sumZ}, {(int)sumX}, {(int)sumY}";
                            Manipulator?.SendCustom(moveCommand);
                        }
                        else
                        {
                            wasGripSent = false;
                        }

                        timer.Start();
                        ResetSums();
                        sample       = false;
                        lastPosition = rightHandJoint.Position;
                    }
                    else
                    {
                        var deltaX = (rightHandJoint.Position.X - lastPosition.X) * multiplication;
                        var deltaY = (rightHandJoint.Position.Y - lastPosition.Y) * multiplication;
                        var deltaZ = (rightHandJoint.Position.Z - lastPosition.Z) * multiplication;

                        sumX += deltaX;
                        sumY += deltaY;
                        sumZ += deltaZ;
                    }

                    //lastPosition = rightHandJoint.Position;
                }
                else
                {
                    lastPosition = rightHandJoint.Position;
                }
            }
        }
        private void InteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            // Check for a null userInfos since we may still get posted events
            // from the stream after we have unregistered our event handler and
            // deleted our buffers.
            if (this.userInfos == null)
            {
                return;
            }

            if (this.kinectAdapter.IsInInteractionFrame)
            {
                Debugger.Log(
                    0,
                    "warning",
                    "Application might have raised modal UI (e.g.: message box) while handling KinectRegion event. Applications are advised to raise modal UI via a call to System.Windows.Threading.Dispatcher.BeginInvoke instead.\n");
                return;
            }

            UserInfo[] localUserInfos = null;
            long       timestamp      = 0;

            using (InteractionFrame interactionFrame = e.OpenInteractionFrame())
            {
                if (interactionFrame != null)
                {
                    // Copy interaction frame data so we can dispose interaction frame
                    // right away, even if data processing/event handling takes a while.
                    interactionFrame.CopyInteractionDataTo(this.userInfos);
                    timestamp      = interactionFrame.Timestamp;
                    localUserInfos = this.userInfos;
                }
            }

            if (localUserInfos != null)
            {
                this.kinectAdapter.BeginInteractionFrame();

                try
                {
                    bool wasProcessingAborted = false;

                    // Distribute routed events based on the state of all hand pointers
                    for (int userIndex = 0; userIndex < localUserInfos.Length; ++userIndex)
                    {
                        var user = localUserInfos[userIndex];
                        foreach (var handPointer in user.HandPointers)
                        {
                            this.HandleHandPointerData(timestamp, user, userIndex, handPointer);

                            if (localUserInfos != this.userInfos)
                            {
                                // Double-check that user info data being processed is still valid.
                                // Client might have invalidated it by changing the KinectSensor
                                // while handling a KinectRegion event.
                                wasProcessingAborted = true;
                                break;
                            }
                        }
                    }

                    if (wasProcessingAborted)
                    {
                        Debugger.Log(
                            0,
                            "warning",
                            "Application might have changed KinectSensor while handling KinectRegion event. Applications are advised to change KinectSensor via a call to System.Windows.Threading.Dispatcher.BeginInvoke instead.\n");
                    }
                    else
                    {
                        this.PrimaryUserTrackingId = this.kinectAdapter.ChoosePrimaryUser(timestamp, this.PrimaryUserTrackingId, this.QueryPrimaryUserTrackingIdCallback);
                    }
                }
                finally
                {
                    this.kinectAdapter.EndInteractionFrame();
                }
            }
        }
        private void SensorInteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            // Do ball movement and collisions if we have a human being tracked
            if (humanNumber != -1)
            {
                runCollisions();
            }

            // Acquire data from SensorInteractionFramesReadyEventArgs and do stuff with it
            UserInfo[] interactionData;

            using (InteractionFrame interactionFrame = e.OpenInteractionFrame()) //dispose as soon as possible
            {
                if (interactionFrame == null)
                {
                    return;
                }
                interactionData = new UserInfo[InteractionFrame.UserInfoArrayLength];
                interactionFrame.CopyInteractionDataTo(interactionData);
                bool sawUserThisFrame = false;
                foreach (UserInfo u in interactionData)
                {
                    if (u.SkeletonTrackingId != 0)
                    {
                        if (humanNumber == -1)
                        {
                            humanNumber = u.SkeletonTrackingId;
                            quitTimer.Stop();
                        }
                        if (humanNumber != u.SkeletonTrackingId)
                        {
                            continue;
                        }
                        sawUserThisFrame = true;
                        foreach (InteractionHandPointer pointer in u.HandPointers)
                        {
                            if (!pointer.IsPrimaryForUser)
                            {
                                continue;
                            }

                            double x = pointer.X;
                            if (x < 0)
                            {
                                x = 0;
                            }
                            if (x > 1)
                            {
                                x = 1;
                            }
                            x          = x * (screenWidth - paddle.width) + paddle.width / 2;
                            paddle.loc = new Point(x, paddle.loc.Y);
                        }
                    }
                }
                if (!sawUserThisFrame)
                {
                    humanNumber = -1;
                    quitTimer.Start();
                }
            }
            draw();
        }
示例#17
0
        private void InteractionStreamOnInteractionFrameReady(object sender, InteractionFrameReadyEventArgs e)
        {
            using (InteractionFrame frame = e.OpenInteractionFrame())
            {
                if (frame != null)
                {
                    if (this.userInfos == null)
                    {
                        this.userInfos = new UserInfo[InteractionFrame.UserInfoArrayLength];
                    }

                    frame.CopyInteractionDataTo(this.userInfos);
                }
                else
                {
                    return;
                }
            }



            foreach (UserInfo userInfo in this.userInfos)
            {
                foreach (InteractionHandPointer handPointer in userInfo.HandPointers)
                {
                    string action = null;

                    switch (handPointer.HandEventType)
                    {
                    case InteractionHandEventType.Grip:
                        action = "gripped";
                        break;

                    case InteractionHandEventType.GripRelease:
                        action = "released";

                        break;
                    }

                    if (action != null)
                    {
                        string handSide = "unknown";

                        switch (handPointer.HandType)
                        {
                        case InteractionHandType.Left:
                            handSide = "left";
                            break;

                        case InteractionHandType.Right:
                            handSide = "right";
                            break;
                        }
                        //comprobar si conincide con la mano primario y en caso de coincidir
                        //hand.JointType == JointType.HandRight
                        if (handSide == "left")
                        {
                            if (action == "released")
                            {
                                // left hand released code here
                            }
                            else
                            {
                                // left hand gripped code here
                            }
                        }
                        else
                        {
                            if (action == "released")
                            {
                                // right hand released code here
                            }
                            else
                            {
                                // right hand gripped code here
                            }
                        }
                    }
                }
            }
        }