本文来自http://blog.youkuaiyun.com/runaying ,引用必须注明出处!
cocos2d-x节点(CCEventDispatcher.h)API
温馨提醒:为了大家能更好学习,强烈推荐大家看看本人的这篇博客 Cocos2d-X权威指南笔记
事件调度
///cocos2d/cocos2d-x-3.0alpha0/cocos2dx/event_dispatcher
//事件调度
#ifndef __CC_EVENT_DISPATCHER_H__
#define __CC_EVENT_DISPATCHER_H__
#include "platform/CCPlatformMacros.h"
#include "CCEventListener.h"
#include <functional>
#include <string>
#include <map>
#include <list>
#include <vector>
NS_CC_BEGIN
class Event;
class EventTouch;
class Node;
/**
这个类管理事件侦听器 subscriptions(订阅)
和事件调度.
EventListener 以这样一种方式管理事件监听,当事件被调度时,可以从 EventListener里面 added/removed event listeners
The EventListener list is managed in such a way that
event listeners can be added and removed even
from within an EventListener, while events are being
dispatched.
*/
class EventDispatcher
{
public:
/** 获取 EventDispatcher 单例 */
static EventDispatcher* getInstance();
/** 销毁 EventDispatcher 单例 */
static void destroyInstance();
/** 使用 场景图的优先级 为指定事件添加一个监听.
* @param listener 指定要监听的事件.
* @param node 这个节点的绘制顺序是基于监听优先级.
* @note 场景图优先级的值将被固定为 0.所以为了监听 item
* 矢量将被设为 ' <0, scene graph (0 priority), >0'.
*/
void addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node);
/** 使用 一定的优先级 为指定事件添加一个监听.
* @param listener 指定要监听的事件.
* @param fixedPriority 这个监听器的固定优先级.
* @note 较低的优先级,将在那些较高的优先级调用之前调用
* 0 优先级不能作为一定的优先级,因为他是场景图的基本优先级
*/
void addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority);
/** Remove 一个监听
* @param listener 需要被删除的指定的事件侦听器。
*/
void removeEventListener(EventListener* listener);
/** 移除事件类型对应的所有监听器 */
void removeListenersForEventType(const std::string& eventType);
/** Removes 所有监听器*/
void removeAllListeners();
/** Sets listener's 的优先级-. */
void setPriority(EventListener* listener, int fixedPriority);
/** 是否启用调度事件 */
void setEnabled(bool isEnabled);
/**检查是否启用了调度事件 */
bool isEnabled() const;
/** Dispatches (调度)该事件
* 从 事件调度列表里面 删除所有被标记为删除的 EventListeners
*
*/
void dispatchEvent(Event* event, bool forceSortListeners = false);
void setDirtyForEventType(const std::string& eventType, bool isDirty);
bool isDirtyForEventType(const std::string& eventType);
public:
/** EventDispatcher的析构函数 */
~EventDispatcher();
private:
struct EventListenerItem
{
int fixedPriority; // 数字越大,优先级越高
Node* node; // 弱引用。
EventListener* listener;
~EventListenerItem();
};
/** EventDispatcher的构造函数 */
EventDispatcher();
/** 使用 item 项添加事件监听*/
void addEventListenerWithItem(EventListenerItem* item);
/** 自从 触摸事件支持 ALL_AT_ONCE 、 ONE_BY_NONE mode 模式后,触摸事件的处理和其它事件不同. */
void dispatchTouchEvent(EventTouch* event);
/** 获取指定类型的事件监听器列表. */
std::vector<EventListenerItem*>* getListenerItemsForType(const std::string& eventType);
/** 根据优先级排序指定类型的监听器 */
void sortAllEventListenerItemsForType(const std::string& eventType);
/**更新所有侦听器项目
* 1) 在调度事件里面移除所有被标记为 "removed" 的监听器.
* 2) 在调度事件里面添加所有被标记为 "added" 的监听器.
*/
void updateListenerItems();
private:
/**
* Listeners map.
*/
std::map<std::string, std::vector<EventListenerItem*>*> _listeners;
/// Priority dirty flag 正在设置优先级的标记
std::map<std::string, bool> _priorityDirtyFlagMap;
std::vector<EventListenerItem*> _toAddedListeners;
int _inDispatch; ///< Whether it's in dispatching event
bool _isEnabled; ///< 是否启用调度事件
};
NS_CC_END
#endif // __CC_EVENT_DISPATCHER_H__