Exemple #1
0
        private static int GetTouchSwipeMatch(GestureEventData touch, ISwipeGesture handler)
        {
            int   matchDir        = -1;
            float matchDotProduct = 0;

            for (int i = 0; i < handler.SwipeDirections.Length; i++)
            {
                Vector2 testDir  = handler.SwipeDirections[i];
                Vector2 swipeDir = touch.pointer.position - touch.startPosition;

                //float distance = resIndependent.magnitude;
                //Debug.LogFormat("Distance is {0}", distance);

                float dotProduct = Vector2.Dot(testDir.normalized, swipeDir.normalized);

                float deviance = 1f - dotProduct;
                if (deviance <= handler.SwipeMaxDeviance)
                {
                    float testDeviance = 1f - matchDotProduct;
                    if (deviance < testDeviance)
                    {
                        matchDir        = i;
                        matchDotProduct = dotProduct;
                    }
                }
            }
            return(matchDir);
        }
Exemple #2
0
        private static void HandleSwipe(ISwipeGesture handler, BaseEventData eventData)
        {
            GestureEventData data = eventData as GestureEventData;

            if (handler == null || data == null)
            {
                return;
            }

            if (handler.SwipeTouchCount != events.Length || data.released == false)
            {
                return;
            }

            int matchDir = -1;

            // Loop through the touch events, finding the matching swipe direction
            for (int i = 0; i < events.Length; i++)
            {
                int match = GetTouchSwipeMatch(events[i], handler);

                if (i == 0)
                {
                    matchDir = match;
                    continue;
                }

                if (match != matchDir)
                {
                    matchDir = -1;
                    break;
                }
            }

            if (matchDir >= 0)
            {
                handler.OnSwipeGesture(data.pointer, matchDir);
            }
        }