コード例 #1
0
        private void StartAddonGesture(Skeleton skeleton, string axis)
        {
            if (skeleton == null)
            {
                return;
            }
            if (!_hasStartPoint)
            {
                _startPoint = skeleton.Joints[JointType.HandLeft].Position;
                _lastPoint  = skeleton.Joints[JointType.HandLeft].Position;
                if (_timer != null)
                {
                    _timer.Start();
                }
                if (_startoffset != null)
                {
                    _startoffset.Start();
                }
                //Console.Out.WriteLine("Timer started");
                _hasStartPoint = true;
            }
            if (_timer != null && _timer.ElapsedMilliseconds > 200)
            {
                // get the amount of change between the previous point and the current one in cm.

                float delta = 0;
                if ("X".Equals(axis))
                {
                    delta = (skeleton.Joints[JointType.HandLeft].Position.X - _lastPoint.X) * 1000;
                }
                else if ("Y".Equals(axis))
                {
                    delta = (skeleton.Joints[JointType.HandLeft].Position.Y - _lastPoint.Y) * 1000;
                }
                else if ("Z".Equals(axis))
                {
                    delta = (skeleton.Joints[JointType.HandLeft].Position.Z - _lastPoint.Z) * 1000;
                }

                _lastPoint = skeleton.Joints[JointType.HandLeft].Position;
                // get the time elapsed between the last event in msec.
                var time = (_timer.ElapsedMilliseconds);

                // calculate the change in cm / mssec
                _rateOfChange = delta / time;

                ////Console.Out.WriteLine(rateOfChange);
                _timer.Restart();
            }


            if (_startoffset != null && _startoffset.ElapsedMilliseconds > 1000)
            {
                if (_stopTimer != null)
                {
                    _stopTimer.Start();
                    if (Math.Abs(_rateOfChange) > 0.15)
                    {
                        if (!_activityRunning)
                        {
                            _startPoint      = skeleton.Joints[JointType.HandLeft].Position;
                            _activityRunning = true;
                            //Console.Out.WriteLine("Activity started");
                        }

                        _stopTimer.Reset();
                    }
                    else
                    {
                        if (_stopTimer.ElapsedMilliseconds > 1000)
                        {
                            if (_activityRunning)
                            {
                                //Console.Out.WriteLine("Activity stopped");
                                _activityRunning = false;
                                _hasStartPoint   = false;
                                _addOnGesture    = false;
                            }
                        }
                    }
                }

                if (_activityRunning)
                {
                    float cmChange = 0;
                    if ("X".Equals(axis))
                    {
                        cmChange = (_lastPoint.X - _startPoint.X) * 100;
                    }
                    else if ("Y".Equals(axis))
                    {
                        cmChange = (_lastPoint.Y - _startPoint.Y) * 100;
                    }
                    else if ("Z".Equals(axis))
                    {
                        cmChange = (_lastPoint.Z - _startPoint.Z) * 100;
                    }


                    const float maxVal = 20;
                    const float minVal = -20;
                    var         value  = Math.Min(Math.Max(cmChange, minVal), maxVal) / maxVal;
                    if (_kinectHandler != null)
                    {
                        _kinectHandler.OnAddOnGestureValueChange(value);
                    }
                }
            }
        }