#ifndef cocos2d_libs_EventListener_h
#define cocos2d_libs_EventListener_h
#include "CCPlatformMacros.h"
#include "CCObject.h"
#include <functional>
#include <string>
#include <memory>
#include <set>
NS_CC_BEGIN
class Event;
class Node;
/**
* The base class of event listener.
* If you need custom listener which with different callback, you need to inherit this class.
* For instance, you could refer to EventListenerAcceleration, EventListenerKeyboard, EventListenerTouchOneByOne, EventListenerCustom.
*/
class EventListener : public Object
{
public:
enum class Type
{
UNKNOWN,
TOUCH_ONE_BY_ONE,
TOUCH_ALL_AT_ONCE,
KEYBOARD,
MOUSE,
ACCELERATION,
CUSTOM
};
typedef std::size_t ListenerID;
protected:
/** Constructor */
EventListener();
/** Initializes event with type and callback function */
bool init(Type t, ListenerID listenerID, std::function<void(Event*)>callback);
public:
/** Destructor */
virtual ~EventListener();
/** 检查该监听是否有效 */
virtual bool checkAvailable() = 0;
/** Clones the listener, its subclasses have to override this method. */
virtual EventListener* clone() = 0;
protected:
inline void setPaused(bool paused) { _paused = paused; };
inline bool isPaused() const { return _paused; };
inline void setRegistered(bool registered) { _isRegistered = registered; };
inline bool isRegistered() const { return _isRegistered; };
inline Type getType() const { return _type; };
inline ListenerID getListenerID() const { return _listenerID; };
inline void setFixedPriority(int fixedPriority) { _fixedPriority = fixedPriority; };
inline int getFixedPriority() const { return _fixedPriority; };
inline void setSceneGraphPriority(Node* node) { _node = node; };
inline Node* getSceneGraphPriority() const { return _node; };
std::function<void(Event*)> _onEvent; /// 事件响应函数; c++11
Type _type; /// 事件类型,区分重力事件,键盘事件,touch 事件等
ListenerID _listenerID; /// Event listener ID
bool _isRegistered; /// 该监听是否被注册到dispather中
// The priority of event listener
int _fixedPriority; // 数值越大,优先级越高。0则表示基于场景图优先级
Node* _node; // 场景图优先级
bool _paused; // 该监听是否暂停
friend class EventDispatcher;
};
NS_CC_END
#endif
cocos2dx-3.0 : EventListener
最新推荐文章于 2020-03-27 17:19:08 发布