Exemple #1
0
    public void OnBeginDrag(PointerEventData data)
    {
        dragID       = data.pointerId;
        dragLock     = DragLockDirection.None;
        dragStart    = data.position;
        dragPrevious = data.position;

        // don't forward the BeginDrag event until the user leaves the deadzone
    }
Exemple #2
0
    public void OnEndDrag(PointerEventData data)
    {
        if (dragID != data.pointerId)
        {
            return;
        }
        else if (dragLock != DragLockDirection.None)
        {
            ForwardEndDrag(data);
        }

        dragLock     = DragLockDirection.None;
        dragStart    = Vector2.zero;
        dragPrevious = Vector2.zero;
    }
Exemple #3
0
    public void OnDrag(PointerEventData data)
    {
        // Forwarding the drag events mutates the event data position, but we
        // need the original value to assign it to the dragPrevious position
        // after evaluating this drag event.
        Vector2 originalPosition = data.position;

        if (dragID != data.pointerId)
        {
            return;
        }
        else if (dragLock == DragLockDirection.None)
        {
            Vector2 delta = data.position - dragStart;

            // start locking the drag if the user has dragged further than the deadzone radius
            if ((deadzone * deadzone) < delta.sqrMagnitude)
            {
                float absx = Mathf.Abs(delta.x);
                float absy = Mathf.Abs(delta.y);

                dragLock = (absx < absy) ? DragLockDirection.Vertical : DragLockDirection.Horizontal;

                if (Debug.isDebugBuild)
                {
                    string direction = (dragLock == DragLockDirection.Vertical) ? "vertical" : "horizontal";
                    Debug.Log("Locking drag to " + direction);
                }

                // keep a cast reference to the event handler for this direction
                dragEventReceiver = (dragLock == DragLockDirection.Horizontal ? horizontalEventReceiver : verticalEventReceiver) as ILockedDragReceiver;

                // forward the BeginDrag event now that we're locked to one direction
                ForwardBeginDrag(data);
            }
        }
        else
        {
            ForwardDragEvent(data);
        }

        dragPrevious = originalPosition;
    }