Esempio n. 1
0
        private void UpdateTouches()
        {
            if (input == null || input.TouchCount() == 0)
            {
                return;
            }
            if (input.TouchCount() == 1)
            {
                TouchInfo touchInfo = input.GetTouch(0);
                if (currentTouch.GestureType == GestureType.None)
                {
                    actionStartTime = Time.realtimeSinceStartup;
                }
                touchInfo.StartTime = actionStartTime;
                if (touchInfo.Phase == TouchPhase.Began)
                {
                    Touch touch = new Touch
                    {
                        TouchInfo      = touchInfo,
                        GestureType    = GestureType.None,
                        SwipeDirection = SwipeDirection.None,
                        SwipeLength    = 0,
                        SwipeVector    = Vector2.zero
                    };
                    currentTouch = touch;
                    OnTouchStart?.Invoke(touch);
                }
                if (touchInfo.Phase == TouchPhase.Moved)
                {
                    OnTouch?.Invoke(new Touch
                    {
                        TouchInfo   = touchInfo,
                        GestureType = GestureType.None,
                    });
                    if (touchInfo.PositionDelta.magnitude >= settings.GetSwipeLengthInPixels())
                    {
                        Touch touch = new Touch
                        {
                            TouchInfo      = touchInfo,
                            GestureType    = GestureType.Swipe,
                            SwipeDirection = GetSwipeDirection(touchInfo.Position - touchInfo.PositionDelta, touchInfo.Position),
                            SwipeLength    = touchInfo.PositionDelta.magnitude,
                            SwipeVector    = touchInfo.PositionDelta
                        };
                        switch (currentTouch.GestureType)
                        {
                        case GestureType.None:
                            OnSwipeStart?.Invoke(touch);
                            break;

                        case GestureType.Swipe:
                            OnSwipe?.Invoke(touch);
                            break;
                        }
                        currentTouch = touch;
                        return;
                    }
                }

                if (touchInfo.Phase == TouchPhase.Stationary)
                {
                    OnTouch?.Invoke(new Touch
                    {
                        TouchInfo   = touchInfo,
                        GestureType = GestureType.None,
                    });
                    touchInfo.ActionTime = Time.realtimeSinceStartup - touchInfo.StartTime;
                    currentTouch         = new Touch
                    {
                        TouchInfo      = touchInfo,
                        GestureType    = GestureType.Tap,
                        SwipeDirection = SwipeDirection.None,
                        SwipeLength    = 0,
                        SwipeVector    = Vector2.zero
                    };
                }
                if (touchInfo.Phase == TouchPhase.Ended)
                {
                    OnTouchEnd?.Invoke(new Touch
                    {
                        TouchInfo   = touchInfo,
                        GestureType = GestureType.None,
                    });
                    switch (currentTouch.GestureType)
                    {
                    case GestureType.Swipe:
                        OnSwipeEnd?.Invoke(currentTouch);
                        break;

                    case GestureType.Tap when currentTouch.TouchInfo.ActionTime <= settings.TapTime:
                        OnTap?.Invoke(currentTouch);
                        break;
                    }
                }
            }
            if (input.TouchCount() == 2)
            {
                TouchInfo touch0 = input.GetTouch(0);
                TouchInfo touch1 = input.GetTouch(1);
                if (currentTouch.GestureType == GestureType.None)
                {
                    actionStartTime = Time.realtimeSinceStartup;
                }
                touch0.StartTime = actionStartTime;
                if (touch0.Phase == TouchPhase.Began || touch1.Phase == TouchPhase.Began)
                {
                    Pinch pinch = new Pinch
                    {
                        TouchInfos  = new [] { touch0, touch1 },
                        GestureType = GestureType.None,
                        PinchVector = touch0.Position - touch1.Position
                    };
                    currentPinch = pinch;
                    OnPinchStart?.Invoke(currentPinch);
                }
                if (touch0.Phase != TouchPhase.Stationary ||
                    touch0.Phase == TouchPhase.Moved ||
                    touch1.Phase != TouchPhase.Stationary ||
                    touch1.Phase == TouchPhase.Moved)
                {
                    Pinch pinch = new Pinch
                    {
                        TouchInfos  = new [] { touch0, touch1 },
                        GestureType = GestureType.Pinch,
                        PinchVector = touch0.Position - touch1.Position
                    };
                    currentPinch = pinch;
                    OnPinch?.Invoke(currentPinch);
                }
                if (touch0.Phase == TouchPhase.Ended || touch1.Phase == TouchPhase.Ended)
                {
                    Pinch pinch = new Pinch
                    {
                        TouchInfos  = new [] { touch0, touch1 },
                        GestureType = GestureType.Pinch,
                        PinchVector = touch0.Position - touch1.Position
                    };
                    currentPinch = pinch;
                    OnPinchEnd?.Invoke(currentPinch);
                }
            }
        }
 public void SwipeStart(Swipe swipe) => OnSwipeStart?.Invoke(swipe);