cocos2dx-3.0 : EventListener

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值