private bool CancelLeftRightMoveGesture(NuiElement newGestureData)
        {
            bool conditionResult = false;
            NuiElement latestElement = null;
            SkeletonData latestSkeleton = null;
            float threshold;

            do
            {
                latestElement = newGestureData;

                latestSkeleton = (from skeletons in latestElement.GetSkeletonFrame().Skeletons
                                  where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                   select skeletons).FirstOrDefault();

                threshold = Math.Abs(latestSkeleton.Joints[JointID.FootLeft].Position.X - latestSkeleton.Joints[JointID.FootRight].Position.X);

                if (threshold < 0.4)
                {
                    conditionResult = true;
                    logger.Debug("Legs are close together");
                }
                else
                {
                    conditionResult = false;
                }
            } while (false);

            return conditionResult;
        }
        private int DetectLeftMoveGesture(NuiElement newGestureData)
        {
            int conditionResult = (int)ResultCodes.GestureDetectionFailed;
            NuiElement latestElement = null;
            NuiElement oldestElement = null;
            NuiElement previousElement = null;

            SkeletonData latestSkeleton = null;
            SkeletonData oldestSkeleton = null;
            SkeletonData previouSkeleton = null;

            do
            {
                leftFrameBuffer.Add(newGestureData);

                logger.Debug("LeftMove: Frame added to queue and queue count = " + leftFrameBuffer.Count);

                if (leftFrameBuffer.Count <= 1)//buffer size should be more than 1 element for comparison purpose
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    break;
                }

                oldestElement = leftFrameBuffer[0];
                previousElement = leftFrameBuffer[leftFrameBuffer.Count - 2];
                latestElement = leftFrameBuffer[leftFrameBuffer.Count - 1];

                if (latestElement.GetSkeletonFrame() == null)
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    break;
                }

                if (latestElement.GetSkeletonFrame().Skeletons == null)
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    break;
                }

                latestSkeleton = (from skeletons in latestElement.GetSkeletonFrame().Skeletons
                                  where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                   select skeletons).FirstOrDefault();

                previouSkeleton = ( from skeletons in previousElement.GetSkeletonFrame().Skeletons
                                    where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                    select skeletons).FirstOrDefault();

                oldestSkeleton = ( from skeletons in oldestElement.GetSkeletonFrame().Skeletons
                                   where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                   select skeletons).FirstOrDefault();

                if (latestSkeleton == null || previouSkeleton == null || oldestSkeleton == null)
                {
                    conditionResult = (int)ResultCodes.NullTrackedSkeleton;
                    break;
                }

                //Negative condition to break
                if (latestSkeleton.Joints[JointID.AnkleLeft].Position.X >= previouSkeleton.Joints[JointID.AnkleLeft].Position.X)
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    leftFrameBuffer.Clear();
                    break;
                }

                //Good to check if gesture passes
                if ((latestElement.GetTimeStamp() - oldestElement.GetTimeStamp()).TotalMilliseconds < thresholdGestureTime &&
                    (latestSkeleton.Joints[JointID.AnkleLeft].Position.X - oldestSkeleton.Joints[JointID.AnkleLeft].Position.X) <= -thresholdDistance)
                {
                    logger.Debug("LeftMove: Passed distance and timing condition!!!!!!!!!!!!!!!!!");
                    conditionResult = (int)ResultCodes.GestureDetected;
                    leftFrameBuffer.Clear();
                    break;
                }

            } while (false);

            return conditionResult;
        }
        private int DetectUpMoveGesture(NuiElement newGestureData)
        {
            int conditionResult = (int)ResultCodes.GestureDetectionFailed;
            NuiElement latestElement = null;
            NuiElement oldestElement = null;
            NuiElement previousElement = null;

            SkeletonData latestSkeleton = null;
            SkeletonData oldestSkeleton = null;
            SkeletonData previouSkeleton = null;

            do
            {
                upFrameBuffer.Add(newGestureData);

                logger.Debug("UPMove: Frame added to queue and queue count = " + upFrameBuffer.Count);

                if (upFrameBuffer.Count <= 1)//buffer size should be more than 1 element for comparison purpose
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    break;
                }

                oldestElement = upFrameBuffer[0];
                previousElement = upFrameBuffer[upFrameBuffer.Count - 2];
                latestElement = upFrameBuffer[upFrameBuffer.Count - 1];

                if (latestElement.GetSkeletonFrame() == null)
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    break;
                }

                if (latestElement.GetSkeletonFrame().Skeletons == null)
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    break;
                }

                latestSkeleton = (from skeletons in latestElement.GetSkeletonFrame().Skeletons
                                  where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                  select skeletons).FirstOrDefault();

                previouSkeleton = ( from skeletons in previousElement.GetSkeletonFrame().Skeletons
                                    where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                    select skeletons).FirstOrDefault();

                oldestSkeleton = ( from skeletons in oldestElement.GetSkeletonFrame().Skeletons
                                   where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                   select skeletons).FirstOrDefault();

                if (latestSkeleton == null || previouSkeleton == null || oldestSkeleton == null)
                {
                    conditionResult = (int)ResultCodes.NullTrackedSkeleton;
                    break;
                }

                //Right leg move forward
                if (latestSkeleton.Joints[JointID.AnkleRight].Position.Z >= previouSkeleton.Joints[JointID.AnkleRight].Position.Z ||
                    latestSkeleton.Joints[JointID.AnkleLeft].Position.Z >= previouSkeleton.Joints[JointID.AnkleLeft].Position.Z)
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    upFrameBuffer.Clear();
                    logger.Debug("UPMove: Failed condition!!!!!!!!!!!!!!!!!");
                    break;
                }

                if ((latestElement.GetTimeStamp() - oldestElement.GetTimeStamp()).TotalMilliseconds < thresholdGestureTime &&
                    (oldestSkeleton.Joints[JointID.AnkleRight].Position.Z - latestSkeleton.Joints[JointID.AnkleRight].Position.Z) >= thresholdDistance)
                {
                    logger.Debug("UPMove:Right leg Passed distance and timing condition!!!!!!!!!!!!!!!!!");
                    conditionResult = (int)ResultCodes.GestureDetected;
                    upFrameBuffer.Clear();
                    break;
                }

                if ((latestElement.GetTimeStamp() - oldestElement.GetTimeStamp()).TotalMilliseconds < thresholdGestureTime &&
                    (oldestSkeleton.Joints[JointID.AnkleLeft].Position.Z - latestSkeleton.Joints[JointID.AnkleLeft].Position.Z) >= thresholdDistance)
                {
                    logger.Debug("UPMove:Left leg Passed distance and timing condition!!!!!!!!!!!!!!!!!");
                    conditionResult = (int)ResultCodes.GestureDetected;
                    upFrameBuffer.Clear();
                    break;
                }

            } while (false);

            return conditionResult;
        }
        private int ProcessNewGestureData(NuiElement newGestureData)
        {
            //if gesture detected raise event to send to keyboard
            int detectResult = -1;

            do
            {

                if (!this.isMoveGestureDetected)
                {

                }
                else
                {
                    if (CancelLeftRightMoveGesture(newGestureData))
                    {
                        //Global detection true
                        isMoveGestureDetected = false;
                        logger.Debug("Cancel Left right move gesture detected");
                        RaiseGestureEvent((int)GestureID.RIGHT_LEG_MOVED, (int)KeyboardAction.MOVE_LEFT, (int)KeyboardPersistance.RELEASE);

                        RaiseGestureEvent((int)GestureID.RIGHT_LEG_MOVED, (int)KeyboardAction.MOVE_RIGHT, (int)KeyboardPersistance.RELEASE);

                        detectResult = (int)ResultCodes.Success;
                        break;
                    }

                }

                if ((int)ResultCodes.GestureDetected == DetectRightMoveGesture(newGestureData))
                {
                    //Global detection true
                    isMoveGestureDetected = true;

                    //event to press key and hold
                    RaiseGestureEvent((int)GestureID.RIGHT_LEG_MOVED, (int)KeyboardAction.MOVE_LEFT, (int)KeyboardPersistance.RELEASE);

                    RaiseGestureEvent((int)GestureID.RIGHT_LEG_MOVED, (int)KeyboardAction.MOVE_RIGHT, (int)KeyboardPersistance.PRESS);

                    detectResult = (int)ResultCodes.Success;
                    break;
                }

                if ((int)ResultCodes.GestureDetected == DetectLeftMoveGesture(newGestureData))
                {
                    //Global detection true
                    isMoveGestureDetected = true;

                    //event to press key and hold
                    RaiseGestureEvent((int)GestureID.LEFT_LEG_MOVED, (int)KeyboardAction.MOVE_RIGHT, (int)KeyboardPersistance.RELEASE);

                    RaiseGestureEvent((int)GestureID.LEFT_LEG_MOVED, (int)KeyboardAction.MOVE_LEFT, (int)KeyboardPersistance.PRESS);

                    detectResult = (int)ResultCodes.Success;
                    break;
                }

            } while (false);

            return detectResult;
        }
        /// <summary>
        /// This function is called when new NuiElement data is available to be processed
        /// </summary>
        /// <param name="newGestureData">the NuiElement with skeleton values</param>
        /// <returns>result of the processing</returns>
        private int ProcessNewGestureData(NuiElement newGestureData)
        {
            int gestureResult = (int)ResultCodes.Success;

            SkeletonData latestSkeleton = (from skeletons in newGestureData.GetSkeletonFrame().Skeletons
                                           where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                           select skeletons).FirstOrDefault();
            if (!this.thresholds.IsThresholdSet)
            {
                this.thresholds.SetThresholdValues(latestSkeleton);
            }

            gestureResult = CheckAimConditions(latestSkeleton);

            gestureResult = CheckShootConditions(latestSkeleton, newGestureData);

            return gestureResult;
        }
        private int ProcessNewGestureData(NuiElement newGestureData)
        {
            SkeletonData latestSkeleton = (from skeletons in newGestureData.GetSkeletonFrame().Skeletons
                                           where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                           select skeletons).FirstOrDefault();
            if (latestSkeleton == null)
            {
                return (int)ResultCodes.OutOfMemory;
            }

            bool isInside = IsInsideBox(latestSkeleton);

            //IsFistRolled(latestSkeleton);

            if (isInside)
            {
                //this.logger.Debug("Hand inside the box is true");
                float depthX, depthY, tmpX, tmpY, scaledDepthX, scaledDepthY;

                Joint leftHand = latestSkeleton.Joints[JointID.WristLeft];
                Joint leftHip = latestSkeleton.Joints[JointID.HipLeft];
                Joint leftShoulder = latestSkeleton.Joints[JointID.ShoulderLeft];
                Joint rightHip = latestSkeleton.Joints[JointID.HipRight];

                depthX = leftHand.Position.X;
                depthY = leftHand.Position.Y;

                if (frameNumber <= 1)
                {
                    prevDepthX = depthX;
                    prevDepthY = depthY;
                    frameNumber++;
                }
                else
                {
                    //applying relativety
                    tmpX = depthX;
                    tmpY = depthY;
                    depthX = depthX - prevDepthX;
                    depthY = depthY - prevDepthY;
                    prevDepthX = tmpX;
                    prevDepthY = tmpY;

                    //applying scaling
                    scaledDepthX = depthX * 800;
                    scaledDepthY = -1 * depthY * 600;

                    this.logger.Debug("FrameNumber = " + frameNumber);
                    this.logger.Debug("Moving relativly X by = " + scaledDepthX);
                    this.logger.Debug("Moving relativly Y by = " + scaledDepthY);

                    MouseEventData mouseData = new MouseEventData();
                    mouseData.MouseAction = (int)MouseButton.MOUSE_MOVE;
                    mouseData.XCoordinate = (int)scaledDepthX;
                    mouseData.YCoordinate = (int)scaledDepthY;

                    this.RaiseMouseEvent(mouseData);
                }
            }
            else
            {
                frameNumber = 0;
            }

            return 0;
        }
        // Cancel shoot condition detector
        private bool IsShootcancel(NuiElement newGestureData)
        {
            bool cancelShootResult = false;

               int conditionResult = (int)ResultCodes.GestureDetectionFailed;
               NuiElement latestElement = null;
               NuiElement oldestElement = null;
               NuiElement previousElement = null;

               SkeletonData latestSkeleton = null;
               SkeletonData oldestSkeleton = null;
               SkeletonData previouSkeleton = null;

               do
               {
               cancelShootFrameBuffer.Add(newGestureData);

               logger.Debug("ShootContinousFire: Frame added to queue and queue count = " + cancelShootFrameBuffer.Count);

               if (cancelShootFrameBuffer.Count <= 1)//buffer size should be more than 1 element for comparison purpose
               {
                   conditionResult = (int)ResultCodes.GestureDetectionFailed;
                   break;
               }

               oldestElement = cancelShootFrameBuffer[0];
               previousElement = cancelShootFrameBuffer[cancelShootFrameBuffer.Count - 2];
               latestElement = cancelShootFrameBuffer[cancelShootFrameBuffer.Count - 1];

               if (latestElement.GetSkeletonFrame() == null)
               {
                   conditionResult = (int)ResultCodes.GestureDetectionFailed;
                   break;
               }

               if (latestElement.GetSkeletonFrame().Skeletons == null)
               {
                   conditionResult = (int)ResultCodes.GestureDetectionFailed;
                   break;
               }

               latestSkeleton = (from skeletons in latestElement.GetSkeletonFrame().Skeletons
                                 where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                 select skeletons).FirstOrDefault();

               previouSkeleton = (from skeletons in previousElement.GetSkeletonFrame().Skeletons
                                  where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                  select skeletons).FirstOrDefault();

               oldestSkeleton = (from skeletons in oldestElement.GetSkeletonFrame().Skeletons
                                 where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                 select skeletons).FirstOrDefault();

               if (latestSkeleton == null || previouSkeleton == null || oldestSkeleton == null)
               {
                   conditionResult = (int)ResultCodes.NullTrackedSkeleton;
                   break;
               }

               float thresholdDistance = this.thresholds.ShootThresholdDistance;
               float thresholdXY = this.thresholds.ShootThresholdXY;

               //Negative condition to break. If the user hand is moving backward, detection failed. Clear buffer.
               if (latestSkeleton.Joints[JointID.HandRight].Position.Z > previouSkeleton.Joints[JointID.HandRight].Position.Z)
               {
                   conditionResult = (int)ResultCodes.GestureDetectionFailed;
                   cancelShootFrameBuffer.Clear();
                   break;
               }

               //Negative condition. Too much variation in Y.
               if (latestSkeleton.Joints[JointID.HandRight].Position.Y > (oldestSkeleton.Joints[JointID.HandRight].Position.Y + thresholdXY)
                   || latestSkeleton.Joints[JointID.HandRight].Position.Y < (oldestSkeleton.Joints[JointID.HandRight].Position.Y - thresholdXY))
               {
                   conditionResult = (int)ResultCodes.GestureDetectionFailed;
                   this.logger.Debug("Too much variation in Y");
                   cancelShootFrameBuffer.Clear();
                   break;
               }

               //Negative condition. Too much variation in X.
               if (latestSkeleton.Joints[JointID.HandRight].Position.X > (oldestSkeleton.Joints[JointID.HandRight].Position.X + thresholdXY)
                   || latestSkeleton.Joints[JointID.HandRight].Position.X < (oldestSkeleton.Joints[JointID.HandRight].Position.X - thresholdXY))
               {
                   conditionResult = (int)ResultCodes.GestureDetectionFailed;
                   this.logger.Debug("Too much variation in X");
                   cancelShootFrameBuffer.Clear();
                   break;
               }

               // User has terminated shooting if he drops his hand
               if (latestSkeleton.Joints[JointID.HandRight].Position.Y < latestSkeleton.Joints[JointID.HipCenter].Position.Y - thresholdXY)
               {
                   conditionResult = (int)ResultCodes.GestureDetected;
                   this.logger.Debug("hand is just falling down...below the hip + threashold");
                   cancelShootFrameBuffer.Clear();
                   break;
               }

               // User has terminated shooting if he drops his hand is 2.5 times threshold distance away
               if (latestSkeleton.Joints[JointID.HandRight].Position.Z < latestSkeleton.Joints[JointID.HipCenter].Position.Z - 2.5*thresholdDistance)
               {
                   conditionResult = (int)ResultCodes.GestureDetected;
                   this.logger.Debug("ttt hand is far from body");
                   cancelShootFrameBuffer.Clear();
                   break;
               }

               // This condition will pass if the user has moved the hand back to threshold distance
               // within a threshold time.
               if ((latestElement.GetTimeStamp() - oldestElement.GetTimeStamp()).TotalMilliseconds > 200 &&
                   (latestElement.GetTimeStamp() - oldestElement.GetTimeStamp()).TotalMilliseconds < thresholdGestureTime &&
                   (oldestSkeleton.Joints[JointID.HandRight].Position.Z - latestSkeleton.Joints[JointID.HandRight].Position.Z) > thresholdDistance)
               {
                   logger.Debug("Cancel shoot: Passed distance and timing condition!!!!!!!!!!!!!!!!!");
                   this.logger.Debug("Time diff: " + (latestElement.GetTimeStamp() - oldestElement.GetTimeStamp()).TotalMilliseconds + " < " + thresholdGestureTime);
                   this.logger.Debug("Distance diff: " + (latestSkeleton.Joints[JointID.HandRight].Position.Z - oldestSkeleton.Joints[JointID.HandRight].Position.Z) + " > " + thresholdDistance);

                   conditionResult = (int)ResultCodes.GestureDetected;
                   trueCondtionSkeleton = latestSkeleton;
                   cancelShootFrameBuffer.Clear();
                   break;
               }

               } while (false);

               if (conditionResult == (int)ResultCodes.GestureDetected)
               {
               cancelShootResult = true;
               }
               else
               {
               cancelShootResult = false;
               }

               return cancelShootResult;
        }
        private bool IsShootTrue(NuiElement newGestureData)
        {
            bool shootResult = false;

               int conditionResult = (int)ResultCodes.GestureDetectionFailed;
            NuiElement latestElement = null;
            NuiElement oldestElement = null;
            NuiElement previousElement = null;

            SkeletonData latestSkeleton = null;
            SkeletonData oldestSkeleton = null;
            SkeletonData previouSkeleton = null;

            do
            {
                shootFrameBuffer.Add(newGestureData);

                logger.Debug("ShootContinousFire: Frame added to queue and queue count = " + shootFrameBuffer.Count);

                if (shootFrameBuffer.Count <= 1)//buffer size should be more than 1 element for comparison purpose
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    break;
                }

                oldestElement = shootFrameBuffer[0];
                previousElement = shootFrameBuffer[shootFrameBuffer.Count - 2];
                latestElement = shootFrameBuffer[shootFrameBuffer.Count - 1];

                if (latestElement.GetSkeletonFrame() == null)
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    break;
                }

                if (latestElement.GetSkeletonFrame().Skeletons == null)
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    break;
                }

                latestSkeleton = (from skeletons in latestElement.GetSkeletonFrame().Skeletons
                                  where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                  select skeletons).FirstOrDefault();

                previouSkeleton = (from skeletons in previousElement.GetSkeletonFrame().Skeletons
                                   where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                   select skeletons).FirstOrDefault();

                oldestSkeleton = (from skeletons in oldestElement.GetSkeletonFrame().Skeletons
                                  where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                  select skeletons).FirstOrDefault();

                if (latestSkeleton == null || previouSkeleton == null || oldestSkeleton == null)
                {
                    conditionResult = (int)ResultCodes.NullTrackedSkeleton;
                    break;
                }

                float thresholdDistance = this.thresholds.ShootThresholdDistance;
                float thresholdXY = this.thresholds.ShootThresholdXY;

                //Negative condition to break. If the user hand is moving forward, detection failed. Clear buffer.
                if (latestSkeleton.Joints[JointID.HandRight].Position.Z < previouSkeleton.Joints[JointID.HandRight].Position.Z)
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    this.logger.Debug("Latest skeleton right Z = " + latestSkeleton.Joints[JointID.HandRight].Position.Z);
                    this.logger.Debug("Previous skeleton right z = " + previouSkeleton.Joints[JointID.HandRight].Position.Z);
                    this.logger.Debug("Time stamp difference = " + (latestElement.GetTimeStamp() - oldestElement.GetTimeStamp()).TotalMilliseconds);
                    this.logger.Debug("Latest Z - Oldest Z = " + (latestSkeleton.Joints[JointID.HandRight].Position.Z - oldestSkeleton.Joints[JointID.HandRight].Position.Z));
                    shootFrameBuffer.Clear();
                    break;
                }

                //Negative condition. Too much variation in Y.
                if (latestSkeleton.Joints[JointID.HandRight].Position.Y > (oldestSkeleton.Joints[JointID.HandRight].Position.Y + thresholdXY)
                    || latestSkeleton.Joints[JointID.HandRight].Position.Y < (oldestSkeleton.Joints[JointID.HandRight].Position.Y - thresholdXY))
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    this.logger.Debug("Too much variation in Y");
                    shootFrameBuffer.Clear();
                    break;
                }

                //Negative condition. Too much variation in X.
                if (latestSkeleton.Joints[JointID.HandRight].Position.X > (oldestSkeleton.Joints[JointID.HandRight].Position.X + thresholdXY)
                    || latestSkeleton.Joints[JointID.HandRight].Position.X < (oldestSkeleton.Joints[JointID.HandRight].Position.X - thresholdXY))
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    this.logger.Debug("Too much variation in X");
                    shootFrameBuffer.Clear();
                    break;
                }

                //NEW CONDITION ADDED TO BLOCK THE HAND FALLING DOWN
                if (latestSkeleton.Joints[JointID.HandRight].Position.Y < latestSkeleton.Joints[JointID.HipCenter].Position.Y - thresholdXY)
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    this.logger.Debug("hand is just falling down...below the hip + threashold");
                    shootFrameBuffer.Clear();
                    break;
                }

                //NEW CONDITION ADDED TO BLOCK THE HAND ABOVE THE FACE
                if (latestSkeleton.Joints[JointID.HandRight].Position.Y > latestSkeleton.Joints[JointID.Head].Position.Y + thresholdXY)
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    this.logger.Debug("Too much variation in X");
                    shootFrameBuffer.Clear();
                    break;
                }

                // This condition will pass if the user has moved the hand back to threshold distance
                // within a threshold time.
                if ((latestElement.GetTimeStamp() - oldestElement.GetTimeStamp()).TotalMilliseconds > minthresholdGestureTime &&
                    (latestElement.GetTimeStamp() - oldestElement.GetTimeStamp()).TotalMilliseconds < thresholdGestureTime &&
                    (latestSkeleton.Joints[JointID.HandRight].Position.Z - oldestSkeleton.Joints[JointID.HandRight].Position.Z) > thresholdDistance)
                {

                    logger.Debug("ShootContinousFire: Passed distance and timing condition!!!!!!!!!!!!!!!!!");
                    this.logger.Debug( "Time diff: " + (latestElement.GetTimeStamp() - oldestElement.GetTimeStamp()).TotalMilliseconds + " < " + thresholdGestureTime);
                    this.logger.Debug( "Distance diff: " + (latestSkeleton.Joints[JointID.HandRight].Position.Z - oldestSkeleton.Joints[JointID.HandRight].Position.Z) + " > " + thresholdDistance);

                    conditionResult = (int)ResultCodes.GestureDetected;
                    trueCondtionSkeleton = latestSkeleton;
                    shootFrameBuffer.Clear();
                    break;
                }

            } while (false);

            if (conditionResult == (int)ResultCodes.GestureDetected)
            {
                shootResult = true;
            }
            else
            {
                shootResult = false;
            }

            return shootResult;
        }
        bool IsInSideShootBox(NuiElement newGestureData)
        {
            bool boxResult = false;

               //If the true skeleton has been detected
               if (this.trueCondtionSkeleton != null)
               {
               SkeletonData currentSkeleton = (from skeletons in newGestureData.GetSkeletonFrame().Skeletons
                                               where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                               select skeletons).FirstOrDefault();

               float currentRelativeX = currentSkeleton.Joints[JointID.HandRight].Position.X;
               float currentRelativeY = currentSkeleton.Joints[JointID.HandRight].Position.Y;
               float currentRelativeZ = currentSkeleton.Joints[JointID.HandRight].Position.Z;

               float trueCondtionSkeletonX = trueCondtionSkeleton.Joints[JointID.HandRight].Position.X;
               float trueCondtionSkeletonY = trueCondtionSkeleton.Joints[JointID.HandRight].Position.Y;
               float trueCondtionSkeletonZ = trueCondtionSkeleton.Joints[JointID.HandRight].Position.Z;

               float thresholdX = this.thresholds.InsideBoxThresholdX;
               float thresholdY = this.thresholds.InsideBoxThresholdY;
               float thresholdZ = this.thresholds.InsideBoxThresholdZ;

               this.logger.Debug("threshold X = " + thresholdX);
               this.logger.Debug("threshold Y = " + thresholdY);
               this.logger.Debug("threshold Z = " + thresholdZ);

               //True conditions to be outside the tolerance box
               if (currentRelativeX > (trueCondtionSkeletonX + thresholdX))
               {

                   trueCondtionSkeleton = null;
                   this.logger.Debug("Not in box because of current relative X = " + currentRelativeX );
               }
               else if (currentRelativeX < (trueCondtionSkeletonX - thresholdX))
               {
                   trueCondtionSkeleton = null;
                   this.logger.Debug("Not in box because of current relative X (-threshold) = " + currentRelativeX);
               }
               else if (currentRelativeY > (trueCondtionSkeletonY + thresholdY))
               {
                   trueCondtionSkeleton = null;
                   this.logger.Debug("Not in box because of current relative Y = " + currentRelativeY);
               }
               else if (currentRelativeY < (trueCondtionSkeletonY - thresholdY))
               {
                   trueCondtionSkeleton = null;
                   this.logger.Debug("Not in box because of current relative Y (-threashold) = " + currentRelativeY);
               }
               else if (currentRelativeZ < (trueCondtionSkeletonZ - thresholdZ))
               {
                   trueCondtionSkeleton = null;
                   this.logger.Debug("ttt Not in box because of current relative Z = " + currentRelativeZ);
               }
               else
               {
                   boxResult = true;
                   this.logger.Debug("Currently inside the shoot box!!!!!!!!!!!!!!!!!!");
               }
               }

               if (trueCondtionSkeleton == null)
               {
               boxResult = false;
               }

               return boxResult;
        }
        //Check various conditions to detect shoot gesture
        private int CheckShootConditions(SkeletonData latestSkeleton, NuiElement newGestureData)
        {
            int gestureResult = (int)ResultCodes.Success;

            do
            {
                bool shoot = false;
                bool isInsideBox = false;

                if (!this.shootDetected)
                {
                    shoot = IsShootTrue(newGestureData);
                }

                // This condition becomes true when shoot is triggerd but it was not detected previously
                // If the user is already in shoot mode and still pulls the hand back this condition will not pass
                // Unless or until he moves his hand out of the box (set shootDetected to false) and
                // does shoot gesture again this condition will not pass.
                if (shoot && !this.shootDetected)
                {
                    MouseEventData mouseData = new MouseEventData();
                    mouseData.MouseAction = (int)MouseButton.LEFT_MOUSE_BUTTON;
                    mouseData.XCoordinate = -1;
                    mouseData.YCoordinate = -1;
                    mouseData.KeyPersistance = (int)MousePresistance.DOUBLE_CLICK_HOLD;

                    this.RaiseMouseEvent(mouseData);
                    this.shootDetected = true;
                    logger.Debug(" Shoot detected first time");
                    break;
                }

                isInsideBox = IsInSideShootBox(newGestureData);

                if (this.shootDetected)
                {
                    logger.Debug("ttt shootinggggg ");
                }

                //Orig end
                //New start
                if (this.shootDetected)
                {

                    if (IsShootcancel(newGestureData))
                    {
                        logger.Debug("ttt Shoot Cancel gesture detected!!");
                        MouseEventData mouseData = new MouseEventData();
                        mouseData.MouseAction = (int)MouseButton.LEFT_MOUSE_BUTTON;
                        mouseData.XCoordinate = -1;
                        mouseData.YCoordinate = -1;
                        mouseData.KeyPersistance = (int)MousePresistance.RELEASE;

                        this.RaiseMouseEvent(mouseData);
                        this.shootDetected = false;
                        this.trueCondtionSkeleton = null;
                        logger.Debug(" Shoot detected while outside the box Negative ");
                        break;
                    }

                }
                //New end
            } while (false);

            return gestureResult;
        }
        private int DetectJumpGesture(NuiElement newGestureData)
        {
            int conditionResult = (int)ResultCodes.GestureDetectionFailed;
            NuiElement latestElement = null;
            NuiElement oldestElement = null;
            NuiElement previousElement = null;

            SkeletonData latestSkeleton = null;
            SkeletonData oldestSkeleton = null;
            SkeletonData previouSkeleton = null;

            do
            {
                jumpFrameBuffer.Add(newGestureData);

                logger.Debug("JumpGesture: Frame added to queue and queue count = " + jumpFrameBuffer.Count);

                if (jumpFrameBuffer.Count <= 1)//buffer size should be more than 1 element for comparison purpose
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    break;
                }

                oldestElement = jumpFrameBuffer[0];
                previousElement = jumpFrameBuffer[jumpFrameBuffer.Count - 2];
                latestElement = jumpFrameBuffer[jumpFrameBuffer.Count - 1];

                if (latestElement.GetSkeletonFrame() == null)
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    break;
                }

                if (latestElement.GetSkeletonFrame().Skeletons == null)
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    break;
                }

                latestSkeleton = (from skeletons in latestElement.GetSkeletonFrame().Skeletons
                                  where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                  select skeletons).FirstOrDefault();

                previouSkeleton = (from skeletons in previousElement.GetSkeletonFrame().Skeletons
                                   where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                   select skeletons).FirstOrDefault();

                oldestSkeleton = (from skeletons in oldestElement.GetSkeletonFrame().Skeletons
                                  where skeletons.TrackingState == SkeletonTrackingState.Tracked
                                  select skeletons).FirstOrDefault();

                if (latestSkeleton == null || previouSkeleton == null || oldestSkeleton == null)
                {
                    conditionResult = (int)ResultCodes.NullTrackedSkeleton;
                    break;
                }

                if (latestSkeleton.Joints[JointID.HipCenter].Position.Y <= previouSkeleton.Joints[JointID.HipCenter].Position.Y)
                {
                    conditionResult = (int)ResultCodes.GestureDetectionFailed;
                    jumpFrameBuffer.Clear();
                    break;
                }

                if ((latestElement.GetTimeStamp() - oldestElement.GetTimeStamp()).TotalMilliseconds < thresholdGestureTime &&
                    (latestSkeleton.Joints[JointID.HipCenter].Position.Y - oldestSkeleton.Joints[JointID.HipCenter].Position.Y) >= thresholdDistance)
                {

                    logger.Debug("Jump: Passed distance and timing condition!!!!!!!!!!!!!!!!!");
                    logger.Debug("latestSkeleton.Joints[JointID.HipCenter].Position.Y = " + latestSkeleton.Joints[JointID.HipCenter].Position.Y);
                    logger.Debug("oldestSkeleton.Joints[JointID.HipCenter].Position.Y = " + oldestSkeleton.Joints[JointID.HipCenter].Position.Y);
                    logger.Debug("Jump: latestSkeleton.Joints[JointID.HipCenter].Position.Y - oldestSkeleton.Joints[JointID.HipCenter].Position.Y) = " + (latestSkeleton.Joints[JointID.HipCenter].Position.Y - oldestSkeleton.Joints[JointID.HipCenter].Position.Y));

                    conditionResult = (int)ResultCodes.GestureDetected;
                    jumpFrameBuffer.Clear();
                    break;
                }

            } while (false);

            return conditionResult;
        }
        private int ProcessNewGestureData(NuiElement newGestureData)
        {
            //if gesture detected raise event to send to keyboard
            int detectResult = -1;

            do
            {
                if ((int)ResultCodes.GestureDetected == DetectJumpGesture(newGestureData))
                {

                    //event to press key and hold

                    RaiseGestureEvent((int)GestureID.RIGHT_LEG_MOVED, (int)KeyboardAction.JUMP, (int)KeyboardPersistance.PRESS_AND_RELEASE);

                    detectResult = (int)ResultCodes.Success;
                    break;
                }

            } while (false);

            return detectResult;
        }
        public int AddToQueue(NuiElement nuiData)
        {
            //Make sure spot is available in the queue
            this.requestKeyboardData.WaitOne();

            lock (_threadLock)
            {
                dataQueue.Enqueue(nuiData);
            }

            //Increament the semaphore to indicate there is job to do
            int previousCount = handleRequests.Release();

            return previousCount;
        }