예제 #1
0
 public void add(ITouchListener listener, TouchPhase[] touchPhases)
 {
     for (int i = 0; i < touchPhases.Length; ++i)
     {
         TouchPhase phase = touchPhases[i];
         if (TouchPhase.Began.Equals(phase))
         {
             if (beganListeners == null)
             {
                 beganListeners = new List <ITouchListener>(3);
             }
             beganListeners.Add(listener);
         }
         // stationary and moved seems to be very related
         else if (TouchPhase.Stationary.Equals(phase) || TouchPhase.Moved.Equals(phase))
         {
             if (stationaryListeners == null)
             {
                 stationaryListeners = new List <ITouchListener>(3);
             }
             stationaryListeners.Add(listener);
         }
         else if (TouchPhase.Ended.Equals(phase) || TouchPhase.Canceled.Equals(phase))
         {
             if (endedListeners == null)
             {
                 endedListeners = new List <ITouchListener>(3);
             }
             endedListeners.Add(listener);
         }
         // add here else if for other phases
     }
 }
예제 #2
0
 /// <summary>
 /// Разрегистрировать слушателя.
 /// </summary>
 /// <param name="touchListener"> Ссылка на разрегистрируемого слушателя. </param>
 public static void UnregListener(ITouchListener touchListener)
 {
     ContextsList.RemoveAll((context) =>
     {
         return((context.TouchListener == touchListener) || (context.TouchListener == null));
     });
 }
예제 #3
0
 /// <summary>
 /// Invoke from OnDestroy() method in your per level game object.
 /// Uses the hash code to identify the listener over the list of many listeners.
 /// </summary>
 public void removeListener(ITouchListener listener)
 {
             #if TOUCH_EVENT_MANAGER_NO_QUADTREE
     dynamicListeners.remove(listener);
             #else
     if (!listener.isScreenStatic())
     {
         dynamicListeners.remove(listener);
     }
     else
     {
         // need to remove from every list the listener appears
         ListenerLists[] lls = staticQuadTree.traverse(listener.getScreenBoundsAA());
         for (int i = 0, c = lls.Length; i < c; ++i)
         {
             // first listenerList element being not valid means next ones are also invalid
             if (lls[i] == null)
             {
                 break;
             }
             lls[i].remove(listener);
         }
     }
             #endif
 }
예제 #4
0
 private void CheckForColliderExit()
 {
     if (touchTarget != null)
     {
         touchTarget.MouseExit();
         lastTouchTarget = null;
         touchTarget     = null;
     }
 }
예제 #5
0
    public ITouchListener[] endedListeners = new ITouchListener[2];      // size set experimentally

    public void add(ITouchListener listener, TouchPhase phase)
    {
        if (TouchPhase.Began == phase)
        {
            bool inserted = false;
            for (int i = 0, c = beganListeners.Length; i < c; ++i)
            {
                if (beganListeners[i] == null)
                {
                    beganListeners[i] = listener;
                    inserted          = true;
                    break;
                }
            }
            if (!inserted)
            {
                Debug.LogError("Limit of began touch listeners reached! Increment size in one unit more");
            }
        }
        else if (TouchPhase.Stationary == phase || TouchPhase.Moved == phase)
        {
            bool inserted = false;
            for (int i = 0, c = stationaryListeners.Length; i < c; ++i)
            {
                if (stationaryListeners[i] == null)
                {
                    stationaryListeners[i] = listener;
                    inserted = true;
                    break;
                }
            }
            if (!inserted)
            {
                Debug.LogError("Limit of stationary touch listeners reached! Increment size in one unit more");
            }
        }
        else if (TouchPhase.Ended == phase || TouchPhase.Canceled == phase)
        {
            bool inserted = false;
            for (int i = 0, c = endedListeners.Length; i < c; ++i)
            {
                if (endedListeners[i] == null)
                {
                    endedListeners[i] = listener;
                    inserted          = true;
                    break;
                }
            }
            if (!inserted)
            {
                Debug.LogError("Limit of ended touch listeners reached! Increment size in one unit more");
            }
        }
    }
예제 #6
0
 private void CheckForColliderEnter()
 {
     if (lastTouchTarget == null || lastTouchTarget != touchTarget)
     {
         colliderEnter   = true;
         lastTouchTarget = touchTarget;
         touchTarget.MouseEnter();
     }
     else if (colliderEnter)
     {
         colliderEnter = false;
     }
 }
예제 #7
0
    public void remove(ITouchListener listener)
    {
        int id = listener.GetHashCode();

        if (beganListeners != null)
        {
            for (int i = 0, c = beganListeners.Count; i < c; ++i)
            {
                /*if (beganListeners[i] == null)
                 *      continue;*/
                if (id == beganListeners[i].GetHashCode())
                {
                    //beganListeners[i] = null;
                    beganListeners.RemoveAt(i);
                    break;
                }
            }
        }

        if (stationaryListeners != null)
        {
            for (int i = 0, c = stationaryListeners.Count; i < c; ++i)
            {
                /*if (stationaryListeners[i] == null)
                 *      continue;*/
                if (id == stationaryListeners[i].GetHashCode())
                {
                    //stationaryListeners[i] = null;
                    stationaryListeners.RemoveAt(i);
                    break;
                }
            }
        }

        if (endedListeners != null)
        {
            for (int i = 0, c = endedListeners.Count; i < c; ++i)
            {
                /*if (endedListeners[i] == null)
                 *      continue;*/
                if (id == endedListeners[i].GetHashCode())
                {
                    //endedListeners[i] = null;
                    endedListeners.RemoveAt(i);
                    break;
                }
            }
        }
        // add here if else for other traverses for different touch phases
    }
예제 #8
0
    private void LateUpdate()
    {
        RaycastHit hit;
        Ray        ray = mainCamera.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit))
        {
            if (hit.collider.gameObject.GetComponent <ITouchListener>() != null)
            {
                touchListener = hit.collider.gameObject.GetComponent <ITouchListener>();
                if (hit.collider.gameObject != currentGameObject)
                {
                    colliderEnter = false;
                }
                if (!colliderEnter)
                {
                    touchListener.MouseEnter();
                    currentGameObject = hit.collider.gameObject;
                    colliderEnter     = true;
                }
                if (Input.GetButtonDown("Fire1") && colliderEnter)
                {
                    touchListener.MouseDown();
                    lastChoseGameObject       = hit.collider.gameObject;
                    currentSelectedGameObject = lastChoseGameObject;
                }
                if (!Input.GetButtonDown("Fire1") && currentSelectedGameObject != null)
                {
                    currentSelectedGameObject = null;
                }
            }
            else if (colliderEnter)
            {
                colliderEnter = false;
                touchListener.MouseExit();
                touchListener             = null;
                currentGameObject         = null;
                currentSelectedGameObject = null;
            }
            else if (hit.collider == null && colliderEnter)
            {
                colliderEnter = false;
                touchListener.MouseExit();
                touchListener             = null;
                currentGameObject         = null;
                currentSelectedGameObject = null;
            }
        }
    }
예제 #9
0
    public void remove(ITouchListener listener)
    {
        int id = listener.GetHashCode();

        for (int i = 0, c = beganListeners.Length; i < c; ++i)
        {
            if (beganListeners[i] == null)
            {
                continue;
            }
            if (id == beganListeners[i].GetHashCode())
            {
                beganListeners[i] = null;
                break;
            }
        }

        for (int i = 0, c = stationaryListeners.Length; i < c; ++i)
        {
            if (stationaryListeners[i] == null)
            {
                continue;
            }
            if (id == stationaryListeners[i].GetHashCode())
            {
                stationaryListeners[i] = null;
                break;
            }
        }

        for (int i = 0, c = endedListeners.Length; i < c; ++i)
        {
            if (endedListeners[i] == null)
            {
                continue;
            }
            if (id == endedListeners[i].GetHashCode())
            {
                endedListeners[i] = null;
                break;
            }
        }
    }
예제 #10
0
    void LateUpdate()
    {
        RaycastHit hit;
        Ray        ray = mainCamera.ScreenPointToRay(Input.mousePosition);

        //currentMouseRay = mainCamera.ScreenToWorldPoint(Input.mousePosition);
        if (Physics.Raycast(ray, out hit))
        {
            currentMouseRay = hit.point;
            if (hit.collider?.gameObject.GetComponent <ITouchListener>() != null && (touchTarget == null || hit.collider?.gameObject.GetComponent <ITouchListener>() == touchTarget))
            {
                touchTarget = hit.collider.gameObject.GetComponent <ITouchListener>();
                CheckForColliderEnter();
                CheckForColliderDown();
            }
            else if ((hit.collider == null && touchTarget != null) || (touchTarget != null && hit.collider != null && hit.collider?.gameObject.GetComponent <ITouchListener>() == null))
            {
                CheckForColliderExit();
            }
        }
    }
예제 #11
0
 /// <summary>
 /// Register the specified iListener in the adequate list to be processed on every touch event.
 /// </summary>
 /// <param name='listener'>
 /// The object you are registering. Needed for callback invocations.
 /// </param>
 /// <param name='touchPhases'>
 /// All the touch phases you want your gameobject be registered to.
 /// </param>
 public void register(ITouchListener listener, params TouchPhase[] touchPhases)
 {
     if (!listener.isStatic())
     {
         dynamicListeners.add(listener, touchPhases);
     }
     else
     {
         /// adding the listener's screen rect into the quad tree will return a list of
         /// as much as 4 ListenerLists elems, since the listener can fall in more than one quadrant
         ListenerLists[] leaves = staticQuadTree.add(listener.getScreenBoundsAA());
         for (int i = 0, c = leaves.Length; i < c; ++i)
         {
             // first listenerList element being not valid means next ones are also invalid
             if (leaves[i] == null)
             {
                 break;
             }
             leaves[i].add(listener, touchPhases);
         }
     }
 }
예제 #12
0
 /// <summary>
 /// Register the specified iListener in the adequate list to be processed on every touch event.
 /// </summary>
 /// <param name='listener'>
 /// The object you are registering. Needed for callback invocations.
 /// </param>
 /// <param name='touchPhases'>
 /// All the touch phases you want your gameobject be registered to.
 /// </param>
 public void register(ITouchListener listener, params TouchPhase[] touchPhases)
 {
             #if TOUCH_EVENT_MANAGER_NO_QUADTREE
     for (int i = 0, c = touchPhases.Length; i < c; ++i)
     {
         dynamicListeners.add(listener, touchPhases[i]);
     }
             #else
     if (!listener.isScreenStatic())
     {
         for (int i = 0, c = touchPhases.Length; i < c; ++i)
         {
             dynamicListeners.add(listener, touchPhases[i]);
         }
     }
     else
     {
         /// adding the listener's screen rect into the quad tree will return a list of
         /// as much as 4 ListenerLists elems, since the listener can fall in more than one quadrant
         Rect            screenBounds = listener.getScreenBoundsAA();
         ListenerLists[] leaves       = staticQuadTree.add(screenBounds);
         for (int i = 0, c = leaves.Length; i < c; ++i)
         {
             // first listenerList element being not valid means next ones are also invalid
             if (leaves[i] == null)
             {
                 break;
             }
             for (int j = 0, d = touchPhases.Length; j < d; ++j)
             {
                 leaves[i].add(listener, touchPhases[j]);
             }
         }
     }
             #endif
 }
예제 #13
0
 public void PoolOnReturn()
 {
     History.Clear();
     TouchListener = null;
 }
예제 #14
0
 public void PoolOnObtain()
 {
     History.Clear();
     TouchListener = null;
 }
예제 #15
0
 public void AddListener(ITouchListener listener)
 {
     _listener = listener;
 }
예제 #16
0
 public ListenerContext(ITouchListener touchListener)
 {
     TouchListener = touchListener;
 }
예제 #17
0
 public static void AddDirectionListener(ITouchListener listener)
 {
     TouchListeners.Add(listener);
 }
예제 #18
0
    private bool sendEvent(Touch tch, ListenerLists ll)
    {
        if (ll == null)
        {
            return(false);
        }

        // NOTE: traverse the arrays and ask if something hitten in the correct order:
        //    Began, Stationary/Move, Ended, Canceled

        ITouchListener[] arrays = null;

        // which lists to traverse? according to touch phase
        switch (tch.phase)
        {
        case TouchPhase.Began:
            arrays = ll.beganListeners;
            break;

        case TouchPhase.Stationary:
        case TouchPhase.Moved:
            arrays = ll.stationaryListeners;
            break;

        default:
            // Ended and Canceled
            arrays = ll.endedListeners;
            break;
            // add here if else for other touch phases
        }

        // one loop for scene only, another loop for global
        bool atLeastOneHit = false;

        for (int i = 0, c = arrays.Length; i < c; ++i)
        {
            ITouchListener listener = arrays[i];
            if (listener == null)
            {
                continue;
            }

            bool hitInner = false;

            // Use detection as in chipmunk platformer, since here I don't use physx colliders
            // (Or use cpShapeQuerySegment (see online documentation from release, cpShape class))
            // Now testing AABB using the screen rect of the GUI element
            hitInner = listener.getScreenBoundsAA().Contains(tch.position);

            if (!hitInner)
            {
                continue;
            }

            // invoke callback
            switch (tch.phase)
            {
            case TouchPhase.Began:
                listener.OnBeganTouch(tch);
                break;

            case TouchPhase.Stationary:
            case TouchPhase.Moved:
                listener.OnStationaryTouch(tch);
                break;

            // Ended and Canceled
            default:
                listener.OnEndedTouch(tch);
                break;
                // add here if else for other methods depending on touch phases
            }

            if (!ALLOW_TOUCH_HITS_OVERLAPPED_OBJECTS)
            {
                return(true);
            }
            atLeastOneHit = true;
        }

        if (!ALLOW_TOUCH_HITS_OVERLAPPED_OBJECTS && atLeastOneHit)
        {
            return(true);
        }

        return(false);
    }
예제 #19
0
    private static bool sendEvent(Touch t, ListenerLists ll)
    {
        if (ll == null)
        {
            return(false);
        }

        // NOTE: traverse the lists and ask if something hitten in the correct order:
        //    Began, Stationary/Move, Ended, Canceled

        List <ITouchListener> list = null;

        // which lists to traverse? according to touch phase
        switch (t.phase)
        {
        case TouchPhase.Began:
            list = ll.beganListeners;
            break;

        case TouchPhase.Stationary:
        case TouchPhase.Moved:
            list = ll.stationaryListeners;
            break;

        default:
            // Ended and Canceled
            list = ll.endedListeners;
            break;
            // add here if else for other touch phases
        }

        if (list == null)
        {
            return(false);
        }

        // one loop for scene only, another loop for global
        bool atLeastOneHit = false;

        for (int i = 0, c = list.Count; i < c; ++i)
        {
            ITouchListener listener = list[i];

            /*if (listener == null)
             *      continue;*/

            GameObject go       = listener.getGameObject();
            bool       hitInner = false;

            // check for GUI Texture
            if (go.guiTexture != null)
            {
                hitInner = go.guiTexture.HitTest(t.position);
            }
            // check for GUI Text
            else if (go.guiText != null)
            {
                hitInner = go.guiText.HitTest(t.position);
            }
            // check for game object
            else
            {
                // use detection as in chipmunk platformer, since here I don't use physx colliders
                // or use cpShapeQuerySegment (see online documentation from release, cpShape class)
                hitInner = GameObjectTools.testHitFromScreenPos(go.transform, t.position);
            }

            if (!hitInner)
            {
                continue;
            }

            // invoke callback
            switch (t.phase)
            {
            case TouchPhase.Began:
                list[i].OnBeganTouch(t);
                break;

            case TouchPhase.Stationary:
            case TouchPhase.Moved:
                list[i].OnStationaryTouch(t);
                break;

            // Ended and Canceled
            default:
                list[i].OnEndedTouch(t);
                break;
                // add here if else for other methods depending on touch phases
            }

            if (!ALLOW_TOUCH_HITS_OVERLAPPED_OBJECTS)
            {
                return(true);
            }
            atLeastOneHit = true;
        }

        if (!ALLOW_TOUCH_HITS_OVERLAPPED_OBJECTS && atLeastOneHit)
        {
            return(true);
        }

        return(false);
    }
예제 #20
0
 /// <summary> Removes the touch listener from the list </summary>
 /// <param name="listener"></param>
 internal void RemoveTouchListener(ITouchListener listener)
 {
     _TouchListeners.Remove(listener);
 }
예제 #21
0
 /// <summary> Adds the listener to the TouchManager </summary>
 /// <param name="listener"></param>
 internal void AddTouchListener(ITouchListener listener)
 {
     _TouchListeners.Add(listener);
     _TouchListenersChanged = true;
 }
예제 #22
0
 /// <summary>
 /// Зарегистрировать слушателя.
 /// </summary>
 /// <param name="touchListener"> Ссылка на регистрируемого слушателя. </param>
 public static void RegListener(ITouchListener touchListener)
 {
     ContextsList.Add(new ListenerContext(touchListener));
 }