cocos2d-x  3.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
EventListener Class Referenceabstract

The base class of event listener. More...

#include <CCEventListener.h>

Inheritance diagram for EventListener:
Ref EventListenerAcceleration EventListenerController EventListenerCustom EventListenerFocus EventListenerKeyboard EventListenerMouse EventListenerTouchAllAtOnce EventListenerTouchOneByOne

Public Types

enum  Type {
  UNKNOWN, TOUCH_ONE_BY_ONE, TOUCH_ALL_AT_ONCE, KEYBOARD,
  MOUSE, ACCELERATION, FOCUS, GAME_CONTROLLER,
  CUSTOM
}
 
typedef std::string ListenerID
 

Public Member Functions

bool init (Type t, const ListenerID &listenerID, const std::function< void(Event *)> &callback)
 Initializes event with type and callback function. More...
 
virtual ~EventListener ()
 Destructor. More...
 
virtual bool checkAvailable ()=0
 Checks whether the listener is available. More...
 
virtual EventListenerclone ()=0
 Clones the listener, its subclasses have to override this method. More...
 
void setEnabled (bool enabled)
 Enables or disables the listener. More...
 
bool isEnabled () const
 Checks whether the listener is enabled. More...
 
- Public Member Functions inherited from Ref
void retain ()
 Retains the ownership. More...
 
void release ()
 Releases the ownership immediately. More...
 
Refautorelease ()
 Releases the ownership sometime soon automatically. More...
 
unsigned int getReferenceCount () const
 Returns the Ref's current reference count. More...
 
virtual ~Ref ()
 NA NA More...
 

Public Attributes

CC_CONSTRUCTOR_ACCESS __pad0__: EventListener()
 

Protected Member Functions

void setPaused (bool paused)
 Sets paused state for the listener The paused state is only used for scene graph priority listeners. More...
 
bool isPaused () const
 Checks whether the listener is paused. More...
 
void setRegistered (bool registered)
 Marks the listener was registered by EventDispatcher. More...
 
bool isRegistered () const
 Checks whether the listener was registered by EventDispatcher. More...
 
Type getType () const
 Gets the type of this listener. More...
 
const ListenerIDgetListenerID () const
 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. More...
 
void setFixedPriority (int fixedPriority)
 Sets the fixed priority for this listener. More...
 
int getFixedPriority () const
 Gets the fixed priority of this listener. More...
 
void setAssociatedNode (Node *node)
 Sets the node associated with this listener. More...
 
NodegetAssociatedNode () const
 Gets the node associated with this listener. More...
 
- Protected Member Functions inherited from Ref
 Ref ()
 Constructor. More...
 

Protected Attributes

std::function< void(Event *)> _onEvent
 
Type _type
 Event callback function. More...
 
ListenerID _listenerID
 Event listener type. More...
 
bool _isRegistered
 Event listener ID. More...
 
int _fixedPriority
 Whether the listener has been added to dispatcher. More...
 
Node_node
 
bool _paused
 
bool _isEnabled
 
- Protected Attributes inherited from Ref
unsigned int _referenceCount
 count of references More...
 

Friends

class EventDispatcher
 

Detailed Description

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.

Member Typedef Documentation

typedef std::string ListenerID

Member Enumeration Documentation

enum Type
strong
Enumerator
UNKNOWN 
TOUCH_ONE_BY_ONE 
TOUCH_ALL_AT_ONCE 
KEYBOARD 
MOUSE 
ACCELERATION 
FOCUS 
GAME_CONTROLLER 
CUSTOM 

Constructor & Destructor Documentation

virtual ~EventListener ( )
virtual

Destructor.

Member Function Documentation

Node* getAssociatedNode ( ) const
inlineprotected

Gets the node associated with this listener.

Returns
nullptr if it's a fixed priority listener, otherwise return non-nullptr
int getFixedPriority ( ) const
inlineprotected

Gets the fixed priority of this listener.

Returns
0 if it's a scene graph priority listener, non-zero for fixed priority listener
const ListenerID& getListenerID ( ) const
inlineprotected

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.

Type getType ( ) const
inlineprotected

Gets the type of this listener.

Note
It's different from EventType, e.g. TouchEvent has two kinds of event listeners - EventListenerOneByOne, EventListenerAllAtOnce
bool init ( Type  t,
const ListenerID listenerID,
const std::function< void(Event *)> &  callback 
)

Initializes event with type and callback function.

bool isEnabled ( ) const
inline

Checks whether the listener is enabled.

bool isPaused ( ) const
inlineprotected

Checks whether the listener is paused.

bool isRegistered ( ) const
inlineprotected

Checks whether the listener was registered by EventDispatcher.

void setAssociatedNode ( Node node)
inlineprotected

Sets the node associated with this listener.

void setEnabled ( bool  enabled)
inline

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.
void setFixedPriority ( int  fixedPriority)
inlineprotected

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
void setPaused ( bool  paused)
inlineprotected

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.
void setRegistered ( bool  registered)
inlineprotected

Marks the listener was registered by EventDispatcher.

Friends And Related Function Documentation

friend class EventDispatcher
friend

Member Data Documentation

int _fixedPriority
protected

Whether the listener has been added to dispatcher.

bool _isEnabled
protected
bool _isRegistered
protected

Event listener ID.

ListenerID _listenerID
protected

Event listener type.

Node* _node
protected
std::function<void(Event*)> _onEvent
protected
bool _paused
protected
Type _type
protected

Event callback function.


The documentation for this class was generated from the following file: