加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx-3.X中对事件处理分析(2)

发布时间:2020-12-14 16:33:23 所属栏目:百科 来源:网络整理
导读:上一篇中,我们大致分析了一下事件处理的整个概括,这一篇,我们来针对EventDispatcher这个核心的事件处理类中的一些函数进行分析,主要涉及到事件的添加,处理, 移除。1、添加事件:1.1. /** Adds a event listener for a specified event with the priori
上一篇中,我们大致分析了一下事件处理的整个概括,这一篇,我们来针对EventDispatcher这个核心的事件处理类中的一些函数进行分析,主要涉及到事件的添加,处理, 移除。
1、
添加事件:
1.1.
  /** Adds a event listener for a specified event with the priority of scene graph.
     *  @param listener The listener of a specified event.
     *  @param node The priority of the listener is based on the draw order of this node.
     *  @note  The priority of scene graph will be fixed value 0. So the order of listener item
     *          in the vector will be ' <0,scene graph (0 priority),>0'.
*/
//注释部分说的很明确,用这个函数,优先级固定为0
void addEventListenerWithSceneGraphPriority(EventListener* listener,Node* node);
-------》》》
void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener,Node* node)
{
    CCASSERT(listener && node,"Invalid parameters.");
    CCASSERT(!listener->isRegistered(),"The listener has been registered.");
    
    if (!listener->checkAvailable())
        return;
    
    listener->setAssociatedNode(node);
    listener->setFixedPriority(0); //这里就是对应上面的注释,优先级为0
    listener->setRegistered(true);
    
    addEventListener(listener); //添加监听器
}
1.2.
我们再来看另外一个添加函数:
    /** Adds a event listener for a specified event with the fixed priority.
     *  @param listener The listener of a specified event.
     *  @param fixedPriority The fixed priority of the listener.
     *  @note A lower priority will be called before the ones that have a higher value.
     *        0 priority is forbidden for fixed priority since it's used for scene graph based priority.
     */
//这个添加函数需要传入一个优先级,但是最终调用的也是addEventListener函数
    void addEventListenerWithFixedPriority(EventListener* listener,int fixedPriority);
1.3.添加Custom事件的函数
   /** Adds a Custom event listener.
     It will use a fixed priority of 1.
     * @param eventName A given name of the event.
     * @param callback A given callback method that associated the event name.
     * @return the generated event. Needed in order to remove the event from the dispather
     */
    EventListenerCustom* addCustomEventListener(const std::string &eventName,const std::function<void(EventCustom*)>& callback);
-→>>>
EventListenerCustom* EventDispatcher::addCustomEventListener(const std::string &eventName,const std::function<void(EventCustom*)>& callback)
{
    EventListenerCustom *listener = EventListenerCustom::create(eventName,callback);
    addEventListenerWithFixedPriority(listener,1); //最终还是调用addEventListener(listener);方法,下面我们就来看下这个方法:
    return listener;
}
1.4、
既然所有的添加事件,最终都会调用addEventListener函数,我们就去这个函数中看一下:
  /** Adds an event listener with item
     *  @note if it is dispatching event,the added operation will be delayed to the end of current dispatch
     *  @see forceAddEventListener
     */
    void addEventListener(EventListener* listener);
----→>>>
void EventDispatcher::addEventListener(EventListener* listener)
{
    if (_inDispatch == 0)
    {
        forceAddEventListener(listener); //主要是这个函数
    }
    else
    {
        _toAddedListeners.push_back(listener); //这个是个临时存储
    }

    listener->retain();
}

--→>>>我们进入这个函数中去看下:
void EventDispatcher::forceAddEventListener(EventListener* listener)
{
    EventListenerVector* listeners = nullptr;
//每一种事件都有一个特定的listenerID,其实就是一个字符串标记,如
const std::string EventListenerTouchOneByOne::LISTENER_ID = "__cc_touch_one_by_one";,根据这个标记,即可以对所有的事件进行分类,
每一种分类都会记录在下面这个map中。
    /** Listeners map */
    std::unordered_map<EventListener::ListenerID,EventListenerVector*> _listenerMap;
    EventListener::ListenerID listenerID = listener->getListenerID();
    auto itr = _listenerMap.find(listenerID);
    if (itr == _listenerMap.end())
    {
        
        listeners = new (std::nothrow) EventListenerVector();
        _listenerMap.insert(std::make_pair(listenerID,listeners));
    }
    else
    {
        listeners = itr->second;
    }
    
    listeners->push_back(listener); //假如到分类的列表中
    //这里对优先级为0的进行了特殊处理,刚才在上面有个添加函数,优先级固定为0,这里对他要进行一些特殊处理
    if (listener->getFixedPriority() == 0)
    {
        setDirty(listenerID,DirtyFlag::SCENE_GRAPH_PRIORITY);
        
        auto node = listener->getAssociatedNode();
        CCASSERT(node != nullptr,"Invalid scene graph priority!");
        
        associateNodeAndEventListener(node,listener);
        
        if (node->isRunning())
        {
            resumeEventListenersForTarget(node);
        }
    }
    else
    {
        setDirty(listenerID,DirtyFlag::FIXED_PRIORITY);
    }
}

注意:
    EventListenerCustom* addCustomEventListener(const std::string &eventName,const std::function<void(EventCustom*)>& callback);
在使用上面这个函数添加Custom事件时,Custom事件的ListenerID就是传进来的eventName函数。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读