Exemplo n.º 1
0
        private void CheckForOverlap <T>(Vector2 point, T gesture, System.Action <FingersDPadScript, FingersDPadItem, T> action) where T : DigitalRubyShared.GestureRecognizer
        {
            if (action == null)
            {
                return;
            }

            FingersDPadItem item       = FingersDPadItem.None;
            int             count      = Physics2D.OverlapCircleNonAlloc(point, DeviceInfo.UnitsToPixels(TouchRadiusInUnits), overlap);
            float           horizontal = 0.0f;
            float           vertical   = 0.0f;

            for (int i = 0; i < count; i++)
            {
                if (overlap[i].gameObject == DPadCenterImageSelected.gameObject)
                {
                    DPadCenterImageSelected.enabled = true;
                    item |= FingersDPadItem.Center;
                }
                if (overlap[i].gameObject == DPadRightImageSelected.gameObject)
                {
                    DPadRightImageSelected.enabled = true;
                    item      |= FingersDPadItem.Right;
                    horizontal = 1.0f;
                }
                if (overlap[i].gameObject == DPadDownImageSelected.gameObject)
                {
                    DPadDownImageSelected.enabled = true;
                    item    |= FingersDPadItem.Down;
                    vertical = -1.0f;
                }
                if (overlap[i].gameObject == DPadLeftImageSelected.gameObject)
                {
                    DPadLeftImageSelected.enabled = true;
                    item      |= FingersDPadItem.Left;
                    horizontal = -1.0f;
                }
                if (overlap[i].gameObject == DPadUpImageSelected.gameObject)
                {
                    DPadUpImageSelected.enabled = true;
                    item    |= FingersDPadItem.Up;
                    vertical = 1.0f;
                }
            }
            if (item != FingersDPadItem.None)
            {
                action(this, item, gesture);
                if (horizontal != 0.0f && crossPlatformInputHorizontalAxisObject != null)
                {
                    FingersCrossPlatformInputReflectionScript.UpdateVirtualAxis(crossPlatformInputHorizontalAxisObject, horizontal);
                }
                if (vertical != 0.0f && crossPlatformInputVerticalAxisObject != null)
                {
                    FingersCrossPlatformInputReflectionScript.UpdateVirtualAxis(crossPlatformInputVerticalAxisObject, vertical);
                }
            }
        }
        private void CheckForOverlap <T>(Vector2 point, T gesture, System.Action <FingersDPadScript, FingersDPadItem, T> action) where T : DigitalRubyShared.GestureRecognizer
        {
            if (action == null)
            {
                return;
            }

            FingersDPadItem item  = FingersDPadItem.None;
            int             count = Physics2D.OverlapCircleNonAlloc(point, DeviceInfo.UnitsToPixels(TouchRadiusInUnits), overlap);

            for (int i = 0; i < count; i++)
            {
                if (overlap[i].gameObject == DPadCenterImageSelected.gameObject)
                {
                    DPadCenterImageSelected.enabled = true;
                    item |= FingersDPadItem.Center;
                }
                if (overlap[i].gameObject == DPadRightImageSelected.gameObject)
                {
                    DPadRightImageSelected.enabled = true;
                    item |= FingersDPadItem.Right;
                }
                if (overlap[i].gameObject == DPadDownImageSelected.gameObject)
                {
                    DPadDownImageSelected.enabled = true;
                    item |= FingersDPadItem.Down;
                }
                if (overlap[i].gameObject == DPadLeftImageSelected.gameObject)
                {
                    DPadLeftImageSelected.enabled = true;
                    item |= FingersDPadItem.Left;
                }
                if (overlap[i].gameObject == DPadUpImageSelected.gameObject)
                {
                    DPadUpImageSelected.enabled = true;
                    item |= FingersDPadItem.Up;
                }
            }
            if (item != FingersDPadItem.None)
            {
                action(this, item, gesture);
            }
        }
        private void CheckForSwipeCompletion(bool end)
        {
            if (Speed < DeviceInfo.UnitsToPixels(MinimumSpeedUnits) || !TrackedTouchCountIsWithinRange)
            {
                // reset focus to current position, we are not going fast enough
                CalculateFocus(CurrentTrackedTouches, true);
                return;
            }

            float distance = DistanceBetweenPoints(StartFocusX, StartFocusY, FocusX, FocusY);

            if (distance < MinimumDistanceUnits || !CalculateEndDirection(FocusX, FocusY))
            {
                // not enough distance covered to be a swipe, or direction failure
                return;
            }
            else if (Direction == SwipeGestureRecognizerDirection.Any || Direction == EndDirection)
            {
                if (end)
                {
                    bool temp = ClearTrackedTouchesOnEndOrFail;
                    if (EndMode != SwipeGestureRecognizerEndMode.EndContinusously)
                    {
                        ClearTrackedTouchesOnEndOrFail = true;
                    }
                    SetState(GestureRecognizerState.Ended);
                    ClearTrackedTouchesOnEndOrFail = temp;
                }
                else if (!SendBeginExecutingStates)
                {
                    SetState(GestureRecognizerState.Possible);
                }
                else if (State == GestureRecognizerState.Possible)
                {
                    SetState(GestureRecognizerState.Began);
                }
                else if (State == GestureRecognizerState.Began || State == GestureRecognizerState.Executing)
                {
                    SetState(GestureRecognizerState.Executing);
                }
            }
        }
Exemplo n.º 4
0
        private void ProcessMouseWheel()
        {
            // if the mouse is not setup or the user doesn't want the mouse treated as touches, return right away
            if (!Input.mousePresent || !TreatMousePointerAsFinger)
            {
                return;
            }

            // the mouse wheel will act as a rotate and pinch / zoom
            Vector2 delta       = Input.mouseScrollDelta;
            float   scrollDelta = (delta.y == 0.0f ? delta.x : delta.y) * MouseWheelDeltaMultiplier;
            float   threshold   = DeviceInfo.UnitsToPixels(MouseDistanceInUnitsForScaleAndRotate * 0.5f);

            // add type 1 = moved, 2 = begin, 3 = ended, 4 = none
            int addType1 = 4;
            int addType2 = 4;

            // left or right control initial down means begin
            if (!RequireControlKeyForMouseZoom)
            {
                if (delta == Vector2.zero)
                {
                    if (lastMouseWheelTime != System.DateTime.MinValue)
                    {
                        if ((System.DateTime.UtcNow - lastMouseWheelTime).TotalSeconds < 1.0f)
                        {
                            // continue zooming
                            pinchScale = Mathf.Max(0.35f, pinchScale + scrollDelta);
                            addType1   = 1;
                        }
                        else
                        {
                            // stop zooming
                            lastMouseWheelTime = System.DateTime.MinValue;
                            addType1           = 3;
                        }
                    }
                }
                else if (lastMouseWheelTime == System.DateTime.MinValue)
                {
                    // start zooming
                    addType1           = 2;
                    lastMouseWheelTime = System.DateTime.UtcNow;
                }
                else
                {
                    // continue zooming
                    pinchScale         = Mathf.Max(0.35f, pinchScale + scrollDelta);
                    addType1           = 1;
                    lastMouseWheelTime = System.DateTime.UtcNow;
                }
            }
            else if (Input.GetKeyDown(KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl))
            {
                // initial start of scale
                addType1 = 2;
            }
            // left or right control still down means move
            else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
            {
                pinchScale = Mathf.Max(0.35f, pinchScale + scrollDelta);
                addType1   = 1;
            }
            // left or right control initial up means end
            else if (Input.GetKeyUp(KeyCode.LeftControl) || Input.GetKeyUp(KeyCode.RightControl))
            {
                addType1 = 3;
            }

            // left or right shift initial down means begin
            if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
            {
                addType2 = 2;
            }
            // left or right shift still down means move
            else if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
            {
                rotateAngle += scrollDelta;
                addType2     = 1;
            }
            // left or right shift initial up means end
            else if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
            {
                addType2 = 3;
            }

            // use the minimum add type so that moves are preferred over begins and begins are preferred over ends
            int addType = Mathf.Min(addType1, addType2);

            // no begins, moves or ends, set defaults and end
            if (addType == 4)
            {
                pinchScale  = 1.0f;
                rotateAngle = 0.0f;
                return;
            }

            // calculate rotation
            float x        = Input.mousePosition.x;
            float y        = Input.mousePosition.y;
            float xRot1    = x - threshold;
            float yRot1    = y;
            float xRot2    = x + threshold;
            float yRot2    = y;
            float distance = threshold * pinchScale;

            xRot1 = x - distance;
            yRot1 = y;
            xRot2 = x + distance;
            yRot2 = y;
            RotateAroundPoint(ref xRot1, ref yRot1, x, y, rotateAngle);
            RotateAroundPoint(ref xRot2, ref yRot2, x, y, rotateAngle);

#if DEBUG
            if (scrollDelta != 0.0f)
            {
                //Debug.LogFormat("Mouse delta: {0}", scrollDelta);
            }
#endif

            // calculate rotation and zoom based on mouse values
            if (addType == 1)
            {
                // moved
                rotatePinch1 = new GestureTouch(int.MaxValue - 5, xRot1, yRot1, rotatePinch1.X, rotatePinch1.Y, 0.0f, null, TouchPhase.Moved);
                rotatePinch2 = new GestureTouch(int.MaxValue - 6, xRot2, yRot2, rotatePinch2.X, rotatePinch2.Y, 0.0f, null, TouchPhase.Moved);
                FingersProcessTouch(ref rotatePinch1);
                FingersProcessTouch(ref rotatePinch2);
            }
            else if (addType == 2)
            {
                // begin
                rotatePinch1 = new GestureTouch(int.MaxValue - 5, xRot1, yRot1, xRot1, yRot1, 0.0f, null, TouchPhase.Began);
                rotatePinch2 = new GestureTouch(int.MaxValue - 6, xRot2, yRot2, xRot2, yRot2, 0.0f, null, TouchPhase.Began);
                FingersProcessTouch(ref rotatePinch1);
                FingersProcessTouch(ref rotatePinch2);
            }
            else if (addType == 3)
            {
                // end
                rotatePinch1 = new GestureTouch(int.MaxValue - 5, xRot1, yRot1, xRot1, yRot1, 0.0f, null, TouchPhase.Ended);
                rotatePinch2 = new GestureTouch(int.MaxValue - 6, xRot2, yRot2, xRot2, yRot2, 0.0f, null, TouchPhase.Ended);
                FingersProcessTouch(ref rotatePinch1);
                FingersProcessTouch(ref rotatePinch2);
            }
        }