Cocos2d-x study notes (16) touch events and distribute EventTouch dispatchTouchEvent EventListenerTouch

1. EventTouch

Member variables of the touch event: Enumeration EventCode, Touch storage container.

EventCode different touch events on behalf of different timing, allowing the listener to call a different callback function.

    enum class EventCode
    {
        BEGAN,
        MOVED,
        ENDED,
        CANCELLED
    };
    static const int MAX_TOUCHES = 15;
   EventCode _eventCode; std::vector<Touch*> _touches;

Touch here related to the class:

Touch class

A touch information package, including a touch point type (single-point / multipoint), coordinates, start coordinates, ID, 3D touch pressure. There are set get method.

2. EventListenerTouch

Touch an event listener, depending on the type of touch points touch, touch the listener into a single / multi-touch listeners.

2.1 EventListenerTouchOneByOne

Single-touch listener callback function can only handle touch events in a touch.

ID unification:

const std::string EventListenerTouchOneByOne::LISTENER_ID = "__cc_touch_one_by_one";

This class four callback function, according to the touch event code different categories:

public : 

    typedef STD function :: <BOOL (Touch *, the Event *)> ccTouchBeganCallback; 
    typedef STD :: function <void (* Touch, the Event *)> ccTouchCallback; 

    ccTouchBeganCallback onTouchBegan; // start touch 
    ccTouchCallback onTouchMoved; // Touch mobile 
    ccTouchCallback onTouchEnded; // end touch 
    ccTouchCallback onTouchCancelled; // touch cancel

This class two member variables:

Private : 
    std :: the Vector <Touch *> _claimedTouches; touch // parameter storage processed BEGAN event callback function 
    bool _needSwallow; // event requires engulfed distributed to other listeners to stop the event when true, setSwallowTouches (bool) Set The default is false

In add listeners to the distributor, you first need to perform checkAvailable () for the listener. Single touch checkAvailable () of the listener, require the presence of claim onTouchBegan.

2.2 EventListenerTouchAllAtOnce

Multi-touch listener callback function can handle the event once all touch.

ID is:

const std::string EventListenerTouchAllAtOnce::LISTENER_ID = "__cc_touch_all_at_once";

4 callback function:

public:
    typedef std::function<void(const std::vector<Touch*>&, Event*)> ccTouchesCallback;
    ccTouchesCallback onTouchesBegan;
    ccTouchesCallback onTouchesMoved;
    ccTouchesCallback onTouchesEnded;
    ccTouchesCallback onTouchesCancelled;

checkAvailable () callback method of claim 4 to be present.

3. dispatchTouchEvent 

In the dispatchEvent method, when the parameter type of event TOUCH, call dispatchTouchEvent for event distribution.

Firstly container for two touch listener ID of Vector prioritized, after obtaining the touch event all touch, save container:

If there are two kinds of listeners, isNeedsMutableSet set to true.

Then distribute the event of a single touch listeners

Logic single touch event distribution is to touch each touch event in turn passed all sorted single touch listeners, listeners execute defined anonymous functions, rather than the listener's own _onEvent (single point _onEvent set nullptr) touch the listener init.

Callback function of each single-touch operation flow performed by the listener as follows:

Briefly, according to the touch event callback code to determine which execution start processing corresponding to the touch callback touch, good touch is stored in the processing vessel listener, the listener or to perform the subsequent movement of the touch / touch-end callback. Corresponding movement of the touch callback processing touch. End touching the corresponding callback processing touch, touch and touch the container delete start of touch, explain the entire procedure ends touch from the listener.

 

We know that when a callback function returns true listener, the event will stop distributing the follow-up to the listener.

In the single-touch listeners, there are two circumstances when the callback function returns true: Stop touching event / callback function touch event is processed + + registered listeners engulfed flag is set to true.

In the single-point touch listener in these cases when the callback function returns false: start touching the callback function is not processed event / listener to start touching no callback method / movement or touch event ends distributed to no (corresponding) beginning listeners touch data.

When the event is stopped, it will exit the current distribution, the end of the distribution.

The next event on the distribution of multi-touch listeners

Logic multi-touch event distribution is that each requires a multi-touch in touch mutableTouches already stored in container, the container passed all sorted multi-touch listeners, listeners execute defined anonymous functions, rather than the listener comes _onEvent (_onEvent set nullptr multi-touch listener init).

Each single-touch touch is in turn distributed to all listeners, multi-touch is to distribute all touch to all listeners.

Distributed to the multi-touch event listener anonymous callback function: by the judgment of the touch event code, decided to execute a callback function which own listener. When the event is stopped returns true, it stops the listener receives follow-up event.

4. 触摸事件分发时的Touch*容器 originalTouches mutableTouches

两个容器开始时都存储了本触摸事件的所有touch。

事件分发到单点触摸监听器,是遍历originalTouches,获取每个touch,再分发touch到监听器。

单点触摸回调函数中,如果一个touch在某个单点触摸监听器中处理完成,且_needSwallow为true,且存在多点触摸监听器,此时要从mutableTouches删除该touch。原因是_needSwallow为true说明本touch处理完后不再继续分发,而mutableTouches是需要分发到多点触摸监听器,故从mutableTouches中删除该touch。mutableTouches中的迭代器自动指向下一位。同时isSwallowed置true,即不用执行++mutableTouchesIter,迭代器已经实现了++操作。

还是上面的情况,如果没有多点触摸监听器,isSwallowed默认false,匿名回调函数返回true,停止本touch分发。接下来执行++mutableTouchesIter,和originalTouches一致。

简而言之,mutableTouches只在有多点触摸监听器的情况下可能发挥它独特的作用(只保存需要接下来多点触发的touch),否则和originalTouches无异,只是存储所有touch而已。

         if (isClaimed && listener->_isRegistered && listener->_needSwallow) //如果事件处理完成,监听器注册状态, 监听器吞没
                {
                    if (isNeedsMutableSet) //单点多点触摸的监听器都有
                    {
                        mutableTouchesIter = mutableTouches.erase(mutableTouchesIter); //删除此触摸点信息
                        isSwallowed = true; //
                    }
                    return true;
                }

       if (!isSwallowed)
             ++mutableTouchesIter;

      //接下来遍历下个touch

Guess you like

Origin www.cnblogs.com/deepcho/p/cocos2dx-eventtouch-dispatchtouchevent-eventlistenertouch.html