示例#1
0
        protected internal override void update(EventLayer eventLayer, bool allowProcessing, Clock clock)
        {
            var touches = eventLayer.EventManager.Touches;
            var fingers = touches.Fingers;

            if (fingers.Count == fingerCount)
            {
                Vector2 primaryFingerVec = new Vector2(fingers[0].PixelDeltaX, fingers[0].PixelDeltaY);
                float   primaryFingerLen = primaryFingerVec.length();
                Vector2 longestLengthVec = primaryFingerVec;
                float   longestLength    = primaryFingerLen;
                if (primaryFingerLen > 0)
                {
                    bool allVectorsSameDirection = true;
                    for (int i = 1; i < fingerCount && allVectorsSameDirection; ++i)
                    {
                        Vector2 testFingerVec = new Vector2(fingers[i].PixelDeltaX, fingers[i].PixelDeltaY);
                        float   testFingerLen = testFingerVec.length();
                        if (testFingerLen > 0)
                        {
                            float cosTheta = primaryFingerVec.dot(ref testFingerVec) / (primaryFingerLen * testFingerLen);
                            if (cosTheta > 0.5f)
                            {
                                if (testFingerLen > longestLength)
                                {
                                    longestLengthVec = testFingerVec;
                                    longestLength    = testFingerLen;
                                }
                            }
                            else
                            {
                                allVectorsSameDirection = false;
                            }
                        }
                    }
                    if (allVectorsSameDirection)
                    {
                        DeltaX    = longestLengthVec.x;
                        DeltaY    = longestLengthVec.y;
                        AbsoluteX = fingers[0].PixelX;
                        AbsoluteY = fingers[0].PixelY;

                        if (!gestureStarted)
                        {
                            cancelMomentum();

                            gestureStarted = true;
                            if (GestureStarted != null)
                            {
                                GestureStarted.Invoke(eventLayer, this);
                            }
                        }

                        didGesture = true;
                        averageSpeed[averageSpeedCursor] = longestLengthVec;
                        averageSpeedCursor++;
                        averageSpeedCursor %= averageSpeed.Length;

                        //Make sure to fire event last so momentum can be canceled if needed
                        if (Dragged != null)
                        {
                            Dragged.Invoke(eventLayer, this);
                        }
                    }
                }
                else if (gestureStarted)
                {
                    //If we have done the gesture once and the correct number of fingers are down, keep reporting that we did the gesture
                    didGesture = true;
                }
            }

            if (!didGesture)
            {
                if (gestureStarted)
                {
                    momentumStarted = true;
                    momentum        = new IntVector2(0, 0);
                    for (int i = 0; i < averageSpeed.Length; ++i)
                    {
                        momentum += averageSpeed[i];
                    }
                    momentum /= averageSpeed.Length;

                    momentumDirection = new Vector2(1.0f, 1.0f);
                    if (momentum.x < 0.0f)
                    {
                        momentum.x          = -momentum.x;
                        momentumDirection.x = -1.0f;
                    }
                    if (momentum.y < 0.0f)
                    {
                        momentum.y          = -momentum.y;
                        momentumDirection.y = -1.0f;
                    }
                    if (momentum.x < minimumMomentum)
                    {
                        momentum.x = 0.0f;
                    }
                    if (momentum.y < minimumMomentum)
                    {
                        momentum.y = 0.0f;
                    }
                    deceleration = momentum / decelerationTime;

                    //Important to fire event last so momentum can be canceled if needed
                    if (MomentumStarted != null)
                    {
                        MomentumStarted(eventLayer, this);
                    }
                }
                gestureStarted = false;

                if (momentum.length2() != 0.0f)
                {
                    momentum -= deceleration * clock.DeltaSeconds;
                    if (momentum.x < 0.0f)
                    {
                        momentum.x = 0.0f;
                    }
                    if (momentum.y <= 0.0f)
                    {
                        momentum.y = 0.0f;
                    }

                    Vector2 finalMomentum = momentum * momentumDirection;
                    DeltaX = finalMomentum.x;
                    DeltaY = finalMomentum.y;

                    if (Dragged != null)
                    {
                        Dragged.Invoke(eventLayer, this);
                    }
                }
                else if (momentumStarted)
                {
                    momentumStarted = false;
                    if (MomentumEnded != null)
                    {
                        MomentumEnded.Invoke(eventLayer, this);
                    }
                }
            }

            didGesture = false;
        }
示例#2
0
        protected internal override void update(EventLayer eventLayer, bool allowProcessing, Clock clock)
        {
            var touches = eventLayer.EventManager.Touches;
            var fingers = touches.Fingers;

            if (fingers.Count == 2)
            {
                Finger  finger1    = fingers[0];
                Finger  finger2    = fingers[1];
                Vector2 finger1Vec = new Vector2(finger1.NrmlDeltaX, finger1.NrmlDeltaY);
                Vector2 finger2Vec = new Vector2(finger2.NrmlDeltaX, finger2.NrmlDeltaY);
                float   finger1Len = finger1Vec.length2();
                float   finger2Len = finger2Vec.length2();
                if (finger1Len > 0 || finger2Len > 0)
                {
                    if (finger1Len > 0 && finger2Len > 0)
                    {
                        float cosTheta = finger1Vec.dot(ref finger2Vec) / (finger1Vec.length() * finger2Vec.length());
                        if (cosTheta < -0.5f && Zoom != null)
                        {
                            computeZoom(eventLayer, ref didGesture, finger1, finger2, ref finger1Vec, ref finger2Vec);
                        }
                    }
                    else if (finger1Len == 0 && finger2Len > 0)
                    {
                        computeZoom(eventLayer, ref didGesture, finger1, finger2, ref finger1Vec, ref finger2Vec);
                    }
                    else if (finger2Len == 0 && finger1Len > 0)
                    {
                        computeZoom(eventLayer, ref didGesture, finger1, finger2, ref finger1Vec, ref finger2Vec);
                    }
                }
            }

            //Momentum
            if (!didGesture)
            {
                if (momentum > 0.0f)
                {
                    zoomDelta = momentum * momentumDirection;

                    if (!momentumStarted)
                    {
                        momentumStarted = true;
                        if (MomentumStarted != null)
                        {
                            MomentumStarted.Invoke(eventLayer, this);
                        }
                    }

                    momentum -= deceleration * clock.DeltaSeconds;
                    if (momentum < 0.0f)
                    {
                        momentum = 0.0f;
                    }
                    if (Zoom != null)
                    {
                        Zoom.Invoke(eventLayer, this);
                    }
                }
                else if (momentumStarted)
                {
                    zoomDelta = 0.0f;

                    momentumStarted = false;
                    gestureStarted  = false;
                    if (MomentumEnded != null)
                    {
                        MomentumEnded.Invoke(eventLayer, this);
                    }
                }
            }
            didGesture = false;
        }