public void Update()
        {
            if (TouchWrapper.IsFingerDown == false)
            {
                isInputOnLockedArea = false;
            }

            bool pinchToDragCurrentFrame = false;

            if (isInputOnLockedArea == false)
            {
                #region pinch
                if (isPinching == false)
                {
                    if (TouchWrapper.TouchCount == 2)
                    {
                        StartPinch();
                        isPinching = true;
                    }
                }
                else
                {
                    if (TouchWrapper.TouchCount < 2)
                    {
                        StopPinch();
                        isPinching = false;
                    }
                    else if (TouchWrapper.TouchCount == 2)
                    {
                        UpdatePinch();
                    }
                }
                #endregion

                #region drag
                if (isPinching == false)
                {
                    if (wasPinchingLastFrame == false)
                    {
                        if (wasFingerDownLastFrame == true && TouchWrapper.IsFingerDown)
                        {
                            if (isDragging == false)
                            {
                                float dragDistance = GetRelativeDragDistance(TouchWrapper.Touch0.Position, dragStartPos);
                                float dragTime     = Time.realtimeSinceStartup - lastFingerDownTimeReal;

                                bool isLongTap = dragTime > clickDurationThreshold;
                                if (OnLongTapProgress != null)
                                {
                                    float longTapProgress = 0;
                                    if (Mathf.Approximately(clickDurationThreshold, 0) == false)
                                    {
                                        longTapProgress = Mathf.Clamp01(dragTime / clickDurationThreshold);
                                    }
                                    OnLongTapProgress(longTapProgress);
                                }

                                if ((dragDistance >= dragStartDistanceThresholdRelative && dragTime >= dragDurationThreshold) ||
                                    (longTapStartsDrag == true && isLongTap == true))
                                {
                                    isDragging      = true;
                                    dragStartOffset = lastFinger0DownPos - dragStartPos;
                                    dragStartPos    = lastFinger0DownPos;
                                    DragStart(dragStartPos, isLongTap, true);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (TouchWrapper.IsFingerDown == true)
                        {
                            isDragging   = true;
                            dragStartPos = TouchWrapper.Touch0.Position;
                            DragStart(dragStartPos, false, false);
                            pinchToDragCurrentFrame = true;
                        }
                    }

                    if (isDragging == true && TouchWrapper.IsFingerDown == true)
                    {
                        DragUpdate(TouchWrapper.Touch0.Position);
                    }

                    if (isDragging == true && TouchWrapper.IsFingerDown == false)
                    {
                        isDragging = false;
                        DragStop(lastFinger0DownPos);
                    }
                }
                #endregion

                #region click
                if (isPinching == false && isDragging == false && wasPinchingLastFrame == false && wasDraggingLastFrame == false && isClickPrevented == false)
                {
                    if (wasFingerDownLastFrame == false && TouchWrapper.IsFingerDown)
                    {
                        lastFingerDownTimeReal = Time.realtimeSinceStartup;
                        dragStartPos           = TouchWrapper.Touch0.Position;
                        FingerDown(TouchWrapper.AverageTouchPos);
                    }

                    if (wasFingerDownLastFrame == true && TouchWrapper.IsFingerDown == false)
                    {
                        float fingerDownUpDuration = Time.realtimeSinceStartup - lastFingerDownTimeReal;

                        if (wasDraggingLastFrame == false && wasPinchingLastFrame == false)
                        {
                            float clickDuration = Time.realtimeSinceStartup - lastClickTimeReal;

                            bool isDoubleClick = clickDuration < doubleclickDurationThreshold;
                            bool isLongTap     = fingerDownUpDuration > clickDurationThreshold;

                            if (OnInputClick != null)
                            {
                                OnInputClick.Invoke(lastFinger0DownPos, isDoubleClick, isLongTap);
                            }

                            lastClickTimeReal = Time.realtimeSinceStartup;
                        }
                    }
                }
                #endregion
            }

            if (isDragging && TouchWrapper.IsFingerDown && pinchToDragCurrentFrame == false)
            {
                DragFinalMomentumVector.Add(TouchWrapper.Touch0.Position - lastFinger0DownPos);
                if (DragFinalMomentumVector.Count > momentumSamplesCount)
                {
                    DragFinalMomentumVector.RemoveAt(0);
                }
            }

            if (isInputOnLockedArea == false)
            {
                wasFingerDownLastFrame = TouchWrapper.IsFingerDown;
            }
            if (wasFingerDownLastFrame == true)
            {
                lastFinger0DownPos = TouchWrapper.Touch0.Position;
            }

            wasDraggingLastFrame = isDragging;
            wasPinchingLastFrame = isPinching;

            if (TouchWrapper.TouchCount == 0)
            {
                isClickPrevented = false;
                if (isFingerDown == true)
                {
                    FingerUp();
                }
            }
        }
Example #2
0
        public void Update()
        {
            if (TouchWrapper.IsFingerDown == false)
            {
                isInputOnLockedArea = false;
            }

            bool pinchToDragCurrentFrame = false;

            if (isInputOnLockedArea == false)
            {
                #region pinch
                if (isPinching == false)
                {
                    if (TouchWrapper.TouchCount == 2)
                    {
                        StartPinch();
                        isPinching = true;
                    }
                }
                else
                {
                    if (TouchWrapper.TouchCount < 2)
                    {
                        StopPinch();
                        isPinching = false;
                    }
                    else if (TouchWrapper.TouchCount == 2)
                    {
                        UpdatePinch();
                    }
                }
                #endregion

                #region drag
                if (isPinching == false)
                {
                    if (wasPinchingLastFrame == false)
                    {
                        if (wasFingerDownLastFrame == true && TouchWrapper.IsFingerDown)
                        {
                            if (isDragging == false)
                            {
                                Vector2 dragVector   = TouchWrapper.Touch0.Position - dragStartPos;
                                float   dragDistance = new Vector2(dragVector.x / Screen.width, dragVector.y / Screen.height).magnitude;
                                float   dragTime     = Time.realtimeSinceStartup - lastFingerDownTimeReal;
                                if ((dragDistance >= dragStartDistanceThresholdRelative && dragTime >= dragDurationThreshold) ||
                                    dragTime > doubleclickDurationThreshold)
                                {
                                    isDragging      = true;
                                    dragStartOffset = lastFinger0DownPos - dragStartPos;
                                    dragStartPos    = lastFinger0DownPos;
                                    DragStart(dragStartPos);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (TouchWrapper.IsFingerDown == true)
                        {
                            isDragging   = true;
                            dragStartPos = TouchWrapper.Touch0.Position;
                            DragStart(dragStartPos);
                            pinchToDragCurrentFrame = true;
                        }
                    }

                    if (isDragging == true && TouchWrapper.IsFingerDown == true)
                    {
                        DragUpdate(TouchWrapper.Touch0.Position);
                    }

                    if (isDragging == true && TouchWrapper.IsFingerDown == false)
                    {
                        isDragging = false;
                        DragStop(lastFinger0DownPos);
                    }
                }
                #endregion

                #region click
                if (isPinching == false && isDragging == false && wasPinchingLastFrame == false && wasDraggingLastFrame == false && isClickPrevented == false)
                {
                    if (wasFingerDownLastFrame == false && TouchWrapper.IsFingerDown)
                    {
                        lastFingerDownTimeReal = Time.realtimeSinceStartup;
                        dragStartPos           = TouchWrapper.Touch0.Position;
                        FingerDown(TouchWrapper.AverageTouchPos);
                    }

                    if (wasFingerDownLastFrame == true && TouchWrapper.IsFingerDown == false)
                    {
                        float fingerDownUpDuration = Time.realtimeSinceStartup - lastFingerDownTimeReal;

                        if (fingerDownUpDuration < clickDurationThreshold)
                        {
                            if (wasDraggingLastFrame == false && wasPinchingLastFrame == false)
                            {
                                float clickDuration = Time.realtimeSinceStartup - lastClickTimeReal;

                                bool isDoubleClick = clickDuration < doubleclickDurationThreshold;

                                if (OnInputClick != null)
                                {
                                    OnInputClick.Invoke(lastFinger0DownPos, isDoubleClick);
                                }

                                lastClickTimeReal = Time.realtimeSinceStartup;
                            }
                        }
                    }
                }
                #endregion
            }

            if (isDragging && TouchWrapper.IsFingerDown && pinchToDragCurrentFrame == false)
            {
                DragFinalMomentumVector.Add(TouchWrapper.Touch0.Position - lastFinger0DownPos);
                if (DragFinalMomentumVector.Count > momentumSamplesCount)
                {
                    DragFinalMomentumVector.RemoveAt(0);
                }
            }

            wasFingerDownLastFrame = TouchWrapper.IsFingerDown;
            if (wasFingerDownLastFrame == true)
            {
                lastFinger0DownPos = TouchWrapper.Touch0.Position;
            }

            wasDraggingLastFrame = isDragging;
            wasPinchingLastFrame = isPinching;

            if (TouchWrapper.TouchCount == 0)
            {
                isClickPrevented = false;
                if (isFingerDown == true)
                {
                    FingerUp();
                }
            }
        }