Esempio n. 1
0
        private void DoMouseClickByHandLifting(int handIndex, MVector2 handRelativePos)
        {
            UpdateHandMouseControl(handIndex, handRelativePos.Y > HandLiftYForClick ? MouseControlState.ShouldClick : MouseControlState.ShouldRelease);

            //DoMouseControlByHandLifting(with press and releas rather than just a click):
            //MouseControlState controlState = handRelativePos.Y > HandLiftYForClick ? MouseControlState.ShouldPress : MouseControlState.ShouldRelease;
            //UpdateHandMouseControl(handIndex, controlState);
        }
Esempio n. 2
0
 public MVector2 GetOutputPosition(MVector2 inputPosition)
 {
     //double dist = Math.Sqrt(Math.Pow(inputPosition.X - _inputRect.Center.X, 2) + Math.Pow(inputPosition.Y - _inputRect.Center.Y, 2));
     //if(dist > 0.3)
     //    return _inputRect.Center;
     //else
     //    return _outputRect.Center + (inputPosition - _inputRect.Center) * totalScale + moveOffset;
     return(_outputRect.Center + (inputPosition - _inputRect.Center) * totalScale + moveOffset);
 }
Esempio n. 3
0
        public MRect(double left, double top, double right, double bottom)
        {
            Left   = left;
            Top    = top;
            Right  = right;
            Bottom = bottom;

            DeltaX = right - left;
            DeltaY = bottom - top;
            Width  = Math.Abs(DeltaX);
            Height = Math.Abs(DeltaY);
            Center = new MVector2(Left + DeltaX * 0.5f, Top + DeltaY * 0.5f);
        }
        public void UpdateMapping()
        {
            if (_scaleAlign == ScaleAlignment.None)
            {
                return;
            }

            double scaleX = OutputRect.DeltaX / InputRect.DeltaX;
            double scaleY = OutputRect.DeltaY / InputRect.DeltaY;

            switch (_scaleAlign)
            {
            case ScaleAlignment.Both:
                _alignScale.X = scaleX;
                _alignScale.Y = scaleY;
                break;

            case ScaleAlignment.Horizontal:
                _alignScale.X = scaleX;
                _alignScale.Y = scaleX;
                break;

            case ScaleAlignment.LongerRange:
                double scaleOfLongerRange = Math.Abs(OutputRect.Width > OutputRect.Height ? scaleX : scaleY);
                _alignScale.X = scaleOfLongerRange * (scaleX < 0 ? -1 : 1);
                _alignScale.Y = scaleOfLongerRange * (scaleY < 0 ? -1 : 1);
                break;

            case ScaleAlignment.Vertical:
                _alignScale.X = scaleY;
                _alignScale.Y = scaleY;
                break;

            case ScaleAlignment.ShorterRange:
                double scaleOfShorterRange = Math.Abs(OutputRect.Width < OutputRect.Height ? scaleX : scaleY);
                _alignScale.X = scaleOfShorterRange * (scaleX < 0 ? -1 : 1);
                _alignScale.Y = scaleOfShorterRange * (scaleY < 0 ? -1 : 1);
                break;

            default:
                _alignScale.X = scaleX < 0 ? -1 : 1;
                _alignScale.Y = scaleY < 0 ? -1 : 1;
                break;
            }

            moveOffset.X = _outputRect.Left;
            moveOffset.Y = _outputRect.Top;

            totalScale = _moveScale * _alignScale;
        }
Esempio n. 5
0
        private void Kinect_OnTrackedBody(object sender, BodyEventArgs e)
        {
            Body body = e.BodyData;

            if (Mode == ControlMode.Disabled)
            {
                return;
            }

            for (int i = 1; i >= 0; i--) // Starts looking from right hand.
            {
                bool isLeft = i == 0;
                if (body.IsHandLiftForward(isLeft))
                {
                    if (usedHandIndex == -1)
                    {
                        usedHandIndex = i;
                    }
                    else if (usedHandIndex != i)
                    {
                        // In two-hand control mode, non-used hand would be used for pressing/releasing mouse button.
                        if (Mode == ControlMode.MoveGripPressing)
                        {
                            DoMouseControlByHandState(i, body.GetHandState(isLeft));
                        }

                        continue;
                    }

                    MVector2 handPos   = body.GetHandRelativePosition(isLeft);
                    MVector2 targetPos = cursorMapper.GetSmoothedOutputPosition(handPos);

                    MouseControl.MoveTo(targetPos.X, targetPos.Y);

                    if (Mode == ControlMode.GripToPress)
                    {
                        MouseControlState state = DoMouseControlByHandState(i, body.GetHandState(isLeft));
                        Data d = new Data(targetPos.X, targetPos.Y, state, body);
                        if (task_num != 0)
                        {
                            DataCollector.CollectData(d);
                        }
                        PositionDataUpdated?.Invoke(this, d);
                    }
                    else if (Mode == ControlMode.HoverToClick)
                    {
                        if ((targetPos - lastCursorPos).Length() > HoverRange)
                        {
                            ToggleHoverTimer(false);
                            hoverClicked = false;
                        }

                        lastCursorPos = targetPos;
                    }
                }
                else
                {
                    if (usedHandIndex == i)
                    {
                        // Reset to none.
                        usedHandIndex = NONE_USED;
                        ReleaseGrip(i);
                    }
                    else if (Mode == ControlMode.MoveLiftClicking)
                    {
                        DoMouseClickByHandLifting(i, body.GetHandRelativePosition(isLeft));
                        //System.Diagnostics.Trace.WriteLine(body.GetHandRelativePosition(isLeft).Y);
                    }
                    else // Release mouse button when it's not regularly released, such as hand tracking lost.
                    {
                        ReleaseGrip(i);
                    }
                }
            }

            ToggleHoverTimer(Mode == ControlMode.HoverToClick && usedHandIndex != -1);
        }
Esempio n. 6
0
 public bool Equals(MVector2 other)
 {
     return((X == other.X) && (Y == other.Y));
 }
 /// <summary>
 /// Get smoothed position.
 /// </summary>
 /// <param name="inputPosition">Position from input.</param>
 /// <param name="extraScale">
 /// Used as an extra control on how much the position is moving other than the moveAmount for smoothing.
 /// e.g. You can insert past time duration so the smoothing can be time-depended. And you may need to adjust Smoothing due to a big result change influenced by this value.
 /// </param>
 /// <returns></returns>
 public MVector2 GetSmoothedOutputPosition(MVector2 inputPosition, double extraScale = 1)
 {
     smoothedPosition += (GetOutputPosition(inputPosition) - smoothedPosition) * moveAmount * extraScale;
     return(smoothedPosition);
 }
 public MVector2 GetOutputPosition(MVector2 inputPosition)
 {
     return(_outputRect.Center + (inputPosition - _inputRect.Center) * totalScale + moveOffset);
 }
Esempio n. 9
0
        private void Kinect_OnTrackedBody(object sender, BodyEventArgs e)
        {
            Body body = e.BodyData;

            if (Mode == ControlMode.Disabled)
            {
                return;
            }

            for (int i = 1; i >= 0; i--) // Starts looking from right hand.
            {
                bool isLeft = (i == 0);
                if (body.IsHandLiftForward(isLeft))
                {
                    if (usedHandIndex == -1)
                    {
                        usedHandIndex = i;
                    }
                    else if (usedHandIndex != i)
                    {
                        // In two-hand control mode, non-used hand would be used for pressing/releasing mouse button.
                        if (Mode == ControlMode.MoveGripPressing)
                        {
                            DoMouseControlByHandState(i, body.GetHandState(isLeft));
                        }

                        continue;
                    }

                    if (body.IsHandLiftUpward(isLeft) && body.IsHandLiftUpward(!isLeft))
                    {
                        HandDist.Add(body.TwoHandsDistance());
                        //System.Diagnostics.Trace.WriteLine(HandDist.Count);
                        if (HandDist.Count == 2)
                        {
                            if (HandDist[1] - HandDist[0] >= 0.25)
                            {
                                MouseControl.Wheel(120);
                                //System.Diagnostics.Trace.WriteLine(1);
                                HandDist[0] = HandDist[1];
                                HandDist.RemoveAt(1);
                            }
                            else if (HandDist[0] - HandDist[1] >= 0.25)
                            {
                                MouseControl.Wheel(-120);
                                //System.Diagnostics.Trace.WriteLine(-1);
                                HandDist[0] = HandDist[1];
                                HandDist.RemoveAt(1);
                            }
                            else
                            {
                                HandDist.RemoveAt(1);
                            }
                        }
                        //System.Diagnostics.Trace.WriteLine(body.TwoHandsDistance());
                    }
                    else
                    {
                        HandDist.Clear();
                        kinectJointFilter.UpdateFilter(body);
                        //MVector2 handPos = body.GetHandRelativePosition(isLeft);
                        MVector2 handPos   = KinectBodyHelper.GetHandSmoothedRelativePosition(kinectJointFilter.GetFilteredJoints(), isLeft);
                        MVector2 targetPos = cursorMapper.GetSmoothedOutputPosition(handPos);
                        //System.Diagnostics.Trace.WriteLine(handPos.ToString());

                        MouseControl.MoveTo(targetPos.X, targetPos.Y);

                        if (Mode == ControlMode.GripToPress)
                        {
                            DoMouseControlByHandState(i, body.GetHandState(isLeft));
                        }
                        else if (Mode == ControlMode.HoverToClick)
                        {
                            if ((targetPos - lastCursorPos).Length() > HoverRange)
                            {
                                ToggleHoverTimer(false);
                                hoverClicked = false;
                            }

                            lastCursorPos = targetPos;
                        }
                    }
                }
                else
                {
                    if (usedHandIndex == i)
                    {
                        // Reset to none.
                        usedHandIndex = NONE_USED;
                        ReleaseGrip(i);
                    }
                    else if (Mode == ControlMode.MoveLiftClicking)
                    {
                        DoMouseClickByHandLifting(i, body.GetHandRelativePosition(isLeft));
                        //System.Diagnostics.Trace.WriteLine(body.GetHandRelativePosition(isLeft).Y);
                    }
                    else // Release mouse button when it's not regularly released, such as hand tracking lost.
                    {
                        ReleaseGrip(i);
                    }
                }
            }

            ToggleHoverTimer(Mode == ControlMode.HoverToClick && usedHandIndex != -1);
        }