美文网首页cocos2d-Lua
Cocos2d-x 3.x 版本事件机制-EventListen

Cocos2d-x 3.x 版本事件机制-EventListen

作者: 最怕认真 | 来源:发表于2015-11-05 11:29 被阅读253次

    这个类是所有事件监听器的基类,所谓监听器,其实就是一个从事件到监听函数的绑定关系,如果让我们自己来实现监听器,你肯定也是会在这个类中包含事件和事件处理函数两个部分的信息在其中,先来看类的定义。

    class CC_DLL EventListener : public Ref
    {
    public:
        /** Type Event type.*/
        enum class Type
        {
            UNKNOWN,
            TOUCH_ONE_BY_ONE,
            TOUCH_ALL_AT_ONCE,
            KEYBOARD,
            MOUSE,
            ACCELERATION,
            FOCUS,
            GAME_CONTROLLER,
            CUSTOM
        };
    
        typedef std::string ListenerID;
    
    CC_CONSTRUCTOR_ACCESS:
        /**
         * Constructor
         * @js ctor
         */
        EventListener();
    
        /** 
         * Initializes event with type and callback function
         * @js NA
         */
        bool init(Type t, const ListenerID& listenerID, const std::function<void(Event*)>& callback);
    public:
        /** Destructor.
         * @js NA 
         */
        virtual ~EventListener();
    
        /** Checks whether the listener is available.
         * 
         * @return True if the listener is available.
         */
        virtual bool checkAvailable() = 0;
    
        /** Clones the listener, its subclasses have to override this method.
         */
        virtual EventListener* clone() = 0;
    
        /** Enables or disables the listener.
         * @note Only listeners with `enabled` state will be able to receive events.
         *        When an listener was initialized, it's enabled by default.
         *        An event listener can receive events when it is enabled and is not paused.
         *        paused state is always false when it is a fixed priority listener.
         *
         * @param enabled True if enables the listener.
         */
        inline void setEnabled(bool enabled) { _isEnabled = enabled; };
    
        /** Checks whether the listener is enabled.
         *
         * @return True if the listenrt is enabled.
         */
        inline bool isEnabled() const { return _isEnabled; };
    
    protected:
    
        /** Sets paused state for the listener
         *  The paused state is only used for scene graph priority listeners.
         *  `EventDispatcher::resumeAllEventListenersForTarget(node)` will set the paused state to `true`,
         *  while `EventDispatcher::pauseAllEventListenersForTarget(node)` will set it to `false`.
         *  @note 1) Fixed priority listeners will never get paused. If a fixed priority doesn't want to receive events,
         *           call `setEnabled(false)` instead.
         *        2) In `Node`'s onEnter and onExit, the `paused state` of the listeners which associated with that node will be automatically updated.
         */
        inline void setPaused(bool paused) { _paused = paused; };
    
        /** Checks whether the listener is paused */
        inline bool isPaused() const { return _paused; };
    
        /** Marks the listener was registered by EventDispatcher */
        inline void setRegistered(bool registered) { _isRegistered = registered; };
    
        /** Checks whether the listener was registered by EventDispatcher */
        inline bool isRegistered() const { return _isRegistered; };
    
        /** Gets the type of this listener
         *  @note It's different from `EventType`, e.g. TouchEvent has two kinds of event listeners - EventListenerOneByOne, EventListenerAllAtOnce
         */
        inline Type getType() const { return _type; };
    
        /** Gets the listener ID of this listener
         *  When event is being dispatched, listener ID is used as key for searching listeners according to event type.
         */
        inline const ListenerID& getListenerID() const { return _listenerID; };
    
        /** Sets the fixed priority for this listener
         *  @note This method is only used for `fixed priority listeners`, it needs to access a non-zero value.
         *  0 is reserved for scene graph priority listeners
         */
        inline void setFixedPriority(int fixedPriority) { _fixedPriority = fixedPriority; };
    
        /** Gets the fixed priority of this listener
         *  @return 0 if it's a scene graph priority listener, non-zero for fixed priority listener
         */
        inline int getFixedPriority() const { return _fixedPriority; };
    
        /** Sets the node associated with this listener */
        inline void setAssociatedNode(Node* node) { _node = node; };
    
        /** Gets the node associated with this listener
         *  @return nullptr if it's a fixed priority listener, otherwise return non-nullptr
         */
        inline Node* getAssociatedNode() const { return _node; };
    
        ///////////////
        // Properties
        //////////////
        std::function<void(Event*)> _onEvent;   /// Event callback function
    
        Type _type;                             /// Event listener type
        ListenerID _listenerID;                 /// Event listener ID
        bool _isRegistered;                     /// Whether the listener has been added to dispatcher.
    
        int   _fixedPriority;   // The higher the number, the higher the priority, 0 is for scene graph base priority.
        Node* _node;            // scene graph based priority
        bool _paused;           // Whether the listener is paused
        bool _isEnabled;        // Whether the listener is enabled
        friend class EventDispatcher;
    };
    
    NS_CC_END
    

    在这个类中,有一个枚举类型

    enum class Type { UNKNOWN, TOUCH_ONE_BY_ONE, TOUCH_ALL_AT_ONCE, KEYBOARD, MOUSE, ACCELERATION, FOCUS, GAME_CONTROLLER, CUSTOM };
    

    这个枚举类型便是所有的事件类型了,分别是未知,单点触摸,多点触摸,键盘时间,鼠标时间,加速器事件(多用于手机),焦点事件,手柄,以及自定义事件。
    而在类变量中,Type _type;便是指当前事件类型,但如果只是事件类型那么监听器之间肯定是会混淆的,所以加入了一个ListenerID _listenerID;监听器id,这个ListenerID也是个宏,是string类型的。
    有了id就可以保证每个监听器是独一无二的,那么这个事件交由谁处理?std::function<void(Event*)> _onEvent;就交由这个_onEvent来处理了。而其他的一些变量,则多是用来辅助的。
    Node* _node;表示的是绑定的node
    bool _isRegistered;表示该监听器是否已经注册过
    int _fixedPriority;监听器的优先级,越小则先执行
    bool _paused;是否暂停这个监听器,如果暂停,那么监听函数也将暂停执行
    bool _isEnabled;是否启用该监听器
    而对于类中的函数,则多是对属性的读写操作,可以看到,这个类定义了一个友元类,EventDispatcher他便是整个事件调度的核心所在

    相关文章

      网友评论

        本文标题:Cocos2d-x 3.x 版本事件机制-EventListen

        本文链接:https://www.haomeiwen.com/subject/svfchttx.html