예제 #1
0
    /// <summary>
    /// Process just-released touches and call triggered
    /// event delegates as part of Update.
    /// </summary>
    /// <param name='id'>
    /// The id of the relevant touch
    /// </param>
    /// <param name='position'>
    /// The position of the relevant touch
    /// </param>
    private void ProcessTouchUp(int id, Vector2 position)
    {
        ArTouch touch = GetTouch(id);

        // If this touch has not been added yet,
        // add it then remove it.
        // NOTE: (jonagill) Does adding and removing a touch in the same step
        // ever cause trouble?
        if (touch == null)
        {
            Debug.LogWarning("Tried to remove touch " + id + " before adding it.");
            ProcessTouchDown(id, position);
            touch = GetTouch(id);
        }

        // Store age for event processing
        float touchAge = touch.GetAge();

        // If a touch hasn't moved and is young enough, throw any assigned tap events
        if (!touch.hasMoved && touchAge <= TAP_MAXTIME)
        {
            // If the touch is near enough to a recent tap's location, throw any
            // assigned double tap events
            // NOTE: (jonagill) To prevent this touch throwing a basic tap
            // event as well, set touch.isDead in the double callback function
            bool doubleTap = false;
            foreach (ArTouch oldTap in _recentTaps)
            {
                if ((oldTap.position - touch.position).sqrMagnitude <= _doubleTapMaxSqdist)
                {
                    if (OnDoubleTap != null && !touch.isDead)
                    {
                        OnDoubleTap(ref touch);
                        _recentTaps.Remove(oldTap);
                        doubleTap = true;
                        // Break because we don't want to trigger OnDoubleTap multiple times
                        // if it's near multiple old taps
                        break;
                    }
                }
            }

            // Throw any assigned tap events
            if (OnTap != null && !touch.isDead)
            {
                OnTap(ref touch);
            }

            // Add this touch to the list of recent taps
            // if not used on a double tap (otherwise we enter
            // every tap on that spot will count as a double
            if (!doubleTap)
            {
                _recentTaps.Add(touch);
            }
        }
        // If a touch has moved and is young enough, throw any assigned flick events
        else if (touch.hasMoved && touchAge <= FLICK_MAXTIME)
        {
            if (OnFlick != null && !touch.isDead)
            {
                OnFlick(ref touch);
            }
        }

        // Call any assigned functions for generic TouchUp events
        if (OnTouchUp != null && !touch.isDead)
        {
            OnTouchUp(ref touch);
        }

        // Remove the touch from our references
        if (id == ArTouch.MOUSE_ID)
        {
            _mouseTouch = null;
        }
        else
        {
            _touches.Remove(touch);
        }
    }
예제 #2
0
    /// <summary>
    /// Process existing touches and call triggered
    /// event delegates as part of Update.
    /// </summary>
    /// <param name='id'>
    /// The id of the relevant touch
    /// </param>
    /// <param name='position'>
    /// The position of the relevant touch
    /// </param>
    private void ProcessTouchUpdate(int id, Vector2 position)
    {
        ArTouch touch = GetTouch(id);

        // If this touch has not been added yet, add it instead of updating
        if (touch == null)
        {
            Debug.LogWarning("Tried to update touch " + id + " before adding it.");
            ProcessTouchDown(id, position);
            return;
        }

        // Only update the touch's position if it has moved a significant distance
        Vector2 moveDelta = position - touch.position;
        bool    moved     = (moveDelta.sqrMagnitude >= _registerMoveSqdist);

        if (moved)
        {
            touch.deltaPosition = moveDelta;
            touch.position      = position;
        }

        // If touch hasn't moved far enough to throw move events yet, check if it has now
        if (!touch.hasMoved)
        {
            if ((touch.position - touch.startPosition).sqrMagnitude >= _startMoveSqdist)
            {
                touch.hasMoved = true;
            }
        }

        // Store age for event checks
        float touchAge = touch.GetAge();

        // If it still hasn't moved, see if it should throw any assigned press events
        if (!touch.hasMoved && touchAge > PRESS_MINTIME)
        {
            if (OnPress != null && !touch.isDead)
            {
                OnPress(ref touch);
            }
        }
        // If it has moved far enough to throw move events, process that
        else if (touch.hasMoved && moved)
        {
            // If we're not still waiting for flicks, throw any assigned drag events
            if (touchAge > DRAG_MINTIME)
            {
                if (OnDrag != null && !touch.isDead)
                {
                    OnDrag(ref touch);
                }
            }

            // Throw any assigned generic touch move events
            if (OnTouchMove != null && !touch.isDead)
            {
                OnTouchMove(ref touch);
            }
        }
    }