cocos2d-x  3.0Beta0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
EventDispatcher Class Reference

This class manages event listener subscriptions and event dispatching. More...

#include <CCEventDispatcher.h>

Inheritance diagram for EventDispatcher:
Object

Classes

class  EventListenerVector
 The vector to store event listeners with scene graph based priority and fixed priority. More...
 

Public Member Functions

void addEventListenerWithSceneGraphPriority (EventListener *listener, Node *node)
 Adds a event listener for a specified event with the priority of scene graph. More...
 
void addEventListenerWithFixedPriority (EventListener *listener, int fixedPriority)
 Adds a event listener for a specified event with the fixed priority. More...
 
local addEventListenerWithFixedPriority ( local listener, local fixedPriority)
 Adds a event listener for a specified event with the fixed priority. More...
 
EventListenerCustomaddCustomEventListener (const std::string &eventName, std::function< void(EventCustom *)> callback)
 Adds a Custom event listener. More...
 
void removeEventListener (EventListener *listener)
 Remove a listener. More...
 
void removeEventListeners (EventListener::Type listenerType)
 Removes all listeners with the same event listener type. More...
 
void removeCustomEventListeners (const std::string &customEventName)
 Removes all custom listeners with the same event name. More...
 
void removeAllEventListeners ()
 Removes all listeners. More...
 
void setPriority (EventListener *listener, int fixedPriority)
 Sets listener's priority with fixed value. More...
 
void setEnabled (bool isEnabled)
 Whether to enable dispatching events. More...
 
bool isEnabled () const
 Checks whether dispatching events is enabled. More...
 
void dispatchEvent (Event *event)
 Dispatches the event Also removes all EventListeners marked for deletion from the event dispatcher list. More...
 
void dispatchCustomEvent (const std::string &eventName, void *optionalUserData)
 Dispatches a Custom Event with a event name an optional user data. More...
 
var dispatchCustomEvent ( var eventName, var optionalUserData)
 Dispatches a Custom Event with a event name an optional user data. More...
 
local dispatchCustomEvent ( local eventName, local optionalUserData)
 Dispatches a Custom Event with a event name an optional user data. More...
 
 EventDispatcher ()
 Constructor of EventDispatcher. More...
 
 ~EventDispatcher ()
 Destructor of EventDispatcher. More...
 
- Public Member Functions inherited from Object
 Object ()
 Constructor. More...
 
 Object ()
 Constructor. More...
 
virtual ~Object ()
void release ()
 Release the ownership immediately. More...
 
void retain ()
 Retains the ownership. More...
 
Objectautorelease ()
 Release the ownership sometime soon automatically. More...
 
bool isSingleReference () const
 Returns a boolean value that indicates whether there is only one reference to the object. More...
 
unsigned int retainCount () const
 Returns the object's current reference count. More...
 
local retainCount ()
 Returns the object's current reference count. More...
 
virtual bool isEqual (const Object *object)
 Returns a boolean value that indicates whether this object and a given object are equal. More...
 
virtual void acceptVisitor (DataVisitor &visitor)
virtual void update (float dt)

Protected Types

enum  DirtyFlag { NONE = 0, FIXED_PRITORY = 1 << 0, SCENE_GRAPH_PRIORITY = 1 << 1, ALL = FIXED_PRITORY | SCENE_GRAPH_PRIORITY }
 Priority dirty flag. More...
 

Protected Member Functions

void setDirtyForNode (Node *node)
 Sets the dirty flag for a node. More...
 
local setDirtyForNode ( local node)
 Sets the dirty flag for a node. More...
 
void pauseTarget (Node *node)
 Notifys event dispatcher that the node has been paused. More...
 
void resumeTarget (Node *node)
 Notifys event dispatcher that the node has been resumed. More...
 
void cleanTarget (Node *node)
 Notifys event dispatcher that the node has been deleted. More...
 
var cleanTarget ( var node)
 Notifys event dispatcher that the node has been deleted. More...
 
local cleanTarget ( local node)
 Notifys event dispatcher that the node has been deleted. More...
 
void addEventListener (EventListener *listener)
 Adds an event listener with item. More...
 
void forceAddEventListener (EventListener *listener)
 Force adding an event listener. More...
 
EventListenerVectorgetListeners (const EventListener::ListenerID &listenerID)
 Gets event the listener list for the event listener type. More...
 
local getListeners ( local listenerID)
 Gets event the listener list for the event listener type. More...
 
void updateDirtyFlagForSceneGraph ()
 Update dirty flag. More...
 
void removeEventListenersForListenerID (const EventListener::ListenerID &listenerID)
 Removes all listeners with the same event listener ID. More...
 
void sortEventListeners (const EventListener::ListenerID &listenerID)
 Sort event listener. More...
 
local sortEventListeners ( local listenerID)
 Sort event listener. More...
 
void sortEventListenersOfSceneGraphPriority (const EventListener::ListenerID &listenerID)
 Sorts the listeners of specified type by scene graph priority. More...
 
void sortEventListenersOfFixedPriority (const EventListener::ListenerID &listenerID)
 Sorts the listeners of specified type by fixed priority. More...
 
void updateListeners (Event *event)
 Updates all listeners 1) Removes all listener items that have been marked as 'removed' when dispatching event. More...
 
void dispatchTouchEvent (EventTouch *event)
 Touch event needs to be processed different with other events since it needs support ALL_AT_ONCE and ONE_BY_NONE mode. More...
 
void associateNodeAndEventListener (Node *node, EventListener *listener)
 Associates node with event listener. More...
 
local associateNodeAndEventListener ( local node, local listener)
 Associates node with event listener. More...
 
void dissociateNodeAndEventListener (Node *node, EventListener *listener)
 Dissociates node with event listener. More...
 
void dispatchEventToListeners (EventListenerVector *listeners, std::function< bool(EventListener *)> onEvent)
 Dispatches event to listeners with a specified listener type. More...
 
void setDirty (const EventListener::ListenerID &listenerID, DirtyFlag flag)
 Sets the dirty flag for a specified listener ID. More...
 
void visitTarget (Node *node)
 Walks though scene graph to get the draw order for each node, it's called before sorting event listener with scene graph priority. More...
 

Protected Attributes

std::unordered_map
< EventListener::ListenerID,
EventListenerVector * > 
_listeners
 Listeners map. More...
 
std::unordered_map
< EventListener::ListenerID,
DirtyFlag
_priorityDirtyFlagMap
 The map of dirty flag. More...
 
std::unordered_map< Node
*, std::vector< EventListener * > * > 
_nodeListenersMap
 The map of node and event listeners. More...
 
std::unordered_map< Node *, int > _nodePriorityMap
 The map of node and its event priority. More...
 
var _nodePriorityMap
 The map of node and its event priority. More...
 
local _nodePriorityMap
 The map of node and its event priority. More...
 
std::vector< EventListener * > _toAddedListeners
 The listeners to be added after dispatching event. More...
 
var _toAddedListeners
 The listeners to be added after dispatching event. More...
 
local _toAddedListeners
 The listeners to be added after dispatching event. More...
 
std::set< Node * > _dirtyNodes
 The nodes were associated with scene graph based priority listeners. More...
 
int _inDispatch
 Whether the dispatcher is dispatching event. More...
 
var _inDispatch
 Whether the dispatcher is dispatching event. More...
 
local _inDispatch
 Whether the dispatcher is dispatching event. More...
 
bool _isEnabled
 Whether to enable dispatching event. More...
 
int _nodePriorityIndex
- Protected Attributes inherited from Object
unsigned int _reference
 count of references More...
 
var _reference
 count of references More...
 
local _reference
 count of references More...
 
unsigned int _autoReleaseCount
 count of autorelease More...
 
var _autoReleaseCount
 count of autorelease More...
 
local _autoReleaseCount
 count of autorelease More...
 

Friends

class Node

Additional Inherited Members

- Public Attributes inherited from Object
unsigned int _ID
 object id, ScriptSupport need public _ID More...
 
int _luaID
 Lua reference id. More...
 
var _luaID
 Lua reference id. More...
 
local _luaID
 Lua reference id. More...
 

Detailed Description

This class manages event listener subscriptions and event dispatching.

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.

Member Enumeration Documentation

enum DirtyFlag
strongprotected

Priority dirty flag.

Enumerator
NONE 
FIXED_PRITORY 
SCENE_GRAPH_PRIORITY 
ALL 
var DirtyFlag
strongprotected

Priority dirty flag.

Enumerator
NONE 
FIXED_PRITORY 
SCENE_GRAPH_PRIORITY 
ALL 
local DirtyFlag
strongprotected

Priority dirty flag.

Enumerator
NONE 
FIXED_PRITORY 
SCENE_GRAPH_PRIORITY 
ALL 

Constructor & Destructor Documentation

Constructor of EventDispatcher.

var EventDispatcher ( )

Constructor of EventDispatcher.

local EventDispatcher ( )

Constructor of EventDispatcher.

Destructor of EventDispatcher.

var ~EventDispatcher ( )

Destructor of EventDispatcher.

local ~EventDispatcher ( )

Destructor of EventDispatcher.

Member Function Documentation

EventListenerCustom*
addCustomEventListener
( const std::string &  eventName,
std::function< void(EventCustom *)>  callback 
)

Adds a Custom event listener.

It will use a fixed priority of 1.

Returns
the generated event. Needed in order to remove the event from the dispather
var addCustomEventListener ( var  eventName,
var  callback 
)

Adds a Custom event listener.

It will use a fixed priority of 1.

Returns
the generated event. Needed in order to remove the event from the dispather
local addCustomEventListener ( local  eventName,
local  callback 
)

Adds a Custom event listener.

It will use a fixed priority of 1.

Returns
the generated event. Needed in order to remove the event from the dispather
void addEventListener ( EventListener listener)
protected

Adds an event listener with item.

Note
if it is dispatching event, the added operation will be delayed to the end of current dispatch
See Also
forceAddEventListener
var addEventListener ( var  listener)
protected

Adds an event listener with item.

Note
if it is dispatching event, the added operation will be delayed to the end of current dispatch
See Also
forceAddEventListener
local addEventListener ( local  listener)
protected

Adds an event listener with item.

Note
if it is dispatching event, the added operation will be delayed to the end of current dispatch
See Also
forceAddEventListener
void
addEventListenerWithFixedPriority
( EventListener listener,
int  fixedPriority 
)

Adds a event listener for a specified event with the fixed priority.

Parameters
listenerThe listener of a specified event.
fixedPriorityThe fixed priority of the listener.
Note
A lower priority will be called before the ones that have a higher value. 0 priority is forbidden for fixed priority since it's used for scene graph based priority.
var
addEventListenerWithFixedPriority
( var  listener,
var  fixedPriority 
)

Adds a event listener for a specified event with the fixed priority.

Parameters
listenerThe listener of a specified event.
fixedPriorityThe fixed priority of the listener.
Note
A lower priority will be called before the ones that have a higher value. 0 priority is forbidden for fixed priority since it's used for scene graph based priority.
local
addEventListenerWithFixedPriority
( local  listener,
local  fixedPriority 
)

Adds a event listener for a specified event with the fixed priority.

Parameters
listenerThe listener of a specified event.
fixedPriorityThe fixed priority of the listener.
Note
A lower priority will be called before the ones that have a higher value. 0 priority is forbidden for fixed priority since it's used for scene graph based priority.
void
addEventListenerWithSceneGraphPriority
( EventListener listener,
Node node 
)

Adds a event listener for a specified event with the priority of scene graph.

Parameters
listenerThe listener of a specified event.
nodeThe priority of the listener is based on the draw order of this node.
Note
The priority of scene graph will be fixed value 0. So the order of listener item in the vector will be ' <0, scene graph (0 priority), >0'.
var
addEventListenerWithSceneGraphPriority
( var  listener,
var  node 
)

Adds a event listener for a specified event with the priority of scene graph.

Parameters
listenerThe listener of a specified event.
nodeThe priority of the listener is based on the draw order of this node.
Note
The priority of scene graph will be fixed value 0. So the order of listener item in the vector will be ' <0, scene graph (0 priority), >0'.
local
addEventListenerWithSceneGraphPriority
( local  listener,
local  node 
)

Adds a event listener for a specified event with the priority of scene graph.

Parameters
listenerThe listener of a specified event.
nodeThe priority of the listener is based on the draw order of this node.
Note
The priority of scene graph will be fixed value 0. So the order of listener item in the vector will be ' <0, scene graph (0 priority), >0'.
void associateNodeAndEventListener ( Node node,
EventListener listener 
)
protected

Associates node with event listener.

var associateNodeAndEventListener ( var  node,
var  listener 
)
protected

Associates node with event listener.

local associateNodeAndEventListener ( local  node,
local  listener 
)
protected

Associates node with event listener.

void cleanTarget ( Node node)
protected

Notifys event dispatcher that the node has been deleted.

var cleanTarget ( var  node)
protected

Notifys event dispatcher that the node has been deleted.

local cleanTarget ( local  node)
protected

Notifys event dispatcher that the node has been deleted.

void dispatchCustomEvent ( const std::string &  eventName,
void *  optionalUserData 
)

Dispatches a Custom Event with a event name an optional user data.

var dispatchCustomEvent ( var  eventName,
var  optionalUserData 
)

Dispatches a Custom Event with a event name an optional user data.

local dispatchCustomEvent ( local  eventName,
local  optionalUserData 
)

Dispatches a Custom Event with a event name an optional user data.

void dispatchEvent ( Event event)

Dispatches the event Also removes all EventListeners marked for deletion from the event dispatcher list.

var dispatchEvent ( var  event)

Dispatches the event Also removes all EventListeners marked for deletion from the event dispatcher list.

local dispatchEvent ( local  event)

Dispatches the event Also removes all EventListeners marked for deletion from the event dispatcher list.

void dispatchEventToListeners ( EventListenerVector listeners,
std::function< bool(EventListener *)>  onEvent 
)
protected

Dispatches event to listeners with a specified listener type.

var dispatchEventToListeners ( var  listeners,
var  onEvent 
)
protected

Dispatches event to listeners with a specified listener type.

local dispatchEventToListeners ( local  listeners,
local  onEvent 
)
protected

Dispatches event to listeners with a specified listener type.

void dispatchTouchEvent ( EventTouch event)
protected

Touch event needs to be processed different with other events since it needs support ALL_AT_ONCE and ONE_BY_NONE mode.

var dispatchTouchEvent ( var  event)
protected

Touch event needs to be processed different with other events since it needs support ALL_AT_ONCE and ONE_BY_NONE mode.

local dispatchTouchEvent ( local  event)
protected

Touch event needs to be processed different with other events since it needs support ALL_AT_ONCE and ONE_BY_NONE mode.

void dissociateNodeAndEventListener ( Node node,
EventListener listener 
)
protected

Dissociates node with event listener.

var dissociateNodeAndEventListener ( var  node,
var  listener 
)
protected

Dissociates node with event listener.

local
dissociateNodeAndEventListener
( local  node,
local  listener 
)
protected

Dissociates node with event listener.

void forceAddEventListener ( EventListener listener)
protected

Force adding an event listener.

Note
force add an event listener which will ignore whether it's in dispatching.
See Also
addEventListener
var forceAddEventListener ( var  listener)
protected

Force adding an event listener.

Note
force add an event listener which will ignore whether it's in dispatching.
See Also
addEventListener
local forceAddEventListener ( local  listener)
protected

Force adding an event listener.

Note
force add an event listener which will ignore whether it's in dispatching.
See Also
addEventListener
EventListenerVector* getListeners ( const EventListener::ListenerID listenerID)
protected

Gets event the listener list for the event listener type.

var getListeners ( var  listenerID)
protected

Gets event the listener list for the event listener type.

local getListeners ( local  listenerID)
protected

Gets event the listener list for the event listener type.

bool isEnabled ( ) const

Checks whether dispatching events is enabled.

var isEnabled ( )

Checks whether dispatching events is enabled.

local isEnabled ( )

Checks whether dispatching events is enabled.

void pauseTarget ( Node node)
protected

Notifys event dispatcher that the node has been paused.

var pauseTarget ( var  node)
protected

Notifys event dispatcher that the node has been paused.

local pauseTarget ( local  node)
protected

Notifys event dispatcher that the node has been paused.

void removeAllEventListeners ( )

Removes all listeners.

var removeAllEventListeners ( )

Removes all listeners.

local removeAllEventListeners ( )

Removes all listeners.

void removeCustomEventListeners ( const std::string &  customEventName)

Removes all custom listeners with the same event name.

var removeCustomEventListeners ( var  customEventName)

Removes all custom listeners with the same event name.

local removeCustomEventListeners ( local  customEventName)

Removes all custom listeners with the same event name.

void removeEventListener ( EventListener listener)

Remove a listener.

Parameters
listenerThe specified event listener which needs to be removed.
var removeEventListener ( var  listener)

Remove a listener.

Parameters
listenerThe specified event listener which needs to be removed.
local removeEventListener ( local  listener)

Remove a listener.

Parameters
listenerThe specified event listener which needs to be removed.
void removeEventListeners ( EventListener::Type  listenerType)

Removes all listeners with the same event listener type.

var removeEventListeners ( var  listenerType)

Removes all listeners with the same event listener type.

local removeEventListeners ( local  listenerType)

Removes all listeners with the same event listener type.

void
removeEventListenersForListenerID
( const EventListener::ListenerID listenerID)
protected

Removes all listeners with the same event listener ID.

var
removeEventListenersForListenerID
( var  listenerID)
protected

Removes all listeners with the same event listener ID.

local
removeEventListenersForListenerID
( local  listenerID)
protected

Removes all listeners with the same event listener ID.

void resumeTarget ( Node node)
protected

Notifys event dispatcher that the node has been resumed.

var resumeTarget ( var  node)
protected

Notifys event dispatcher that the node has been resumed.

local resumeTarget ( local  node)
protected

Notifys event dispatcher that the node has been resumed.

void setDirty ( const EventListener::ListenerID listenerID,
DirtyFlag  flag 
)
protected

Sets the dirty flag for a specified listener ID.

var setDirty ( var  listenerID,
var  flag 
)
protected

Sets the dirty flag for a specified listener ID.

local setDirty ( local  listenerID,
local  flag 
)
protected

Sets the dirty flag for a specified listener ID.

void setDirtyForNode ( Node node)
protected

Sets the dirty flag for a node.

var setDirtyForNode ( var  node)
protected

Sets the dirty flag for a node.

local setDirtyForNode ( local  node)
protected

Sets the dirty flag for a node.

void setEnabled ( bool  isEnabled)

Whether to enable dispatching events.

var setEnabled ( var  isEnabled)

Whether to enable dispatching events.

local setEnabled ( local  isEnabled)

Whether to enable dispatching events.

void setPriority ( EventListener listener,
int  fixedPriority 
)

Sets listener's priority with fixed value.

var setPriority ( var  listener,
var  fixedPriority 
)

Sets listener's priority with fixed value.

local setPriority ( local  listener,
local  fixedPriority 
)

Sets listener's priority with fixed value.

void sortEventListeners ( const EventListener::ListenerID listenerID)
protected

Sort event listener.

var sortEventListeners ( var  listenerID)
protected

Sort event listener.

local sortEventListeners ( local  listenerID)
protected

Sort event listener.

void
sortEventListenersOfFixedPriority
( const EventListener::ListenerID listenerID)
protected

Sorts the listeners of specified type by fixed priority.

var
sortEventListenersOfFixedPriority
( var  listenerID)
protected

Sorts the listeners of specified type by fixed priority.

local
sortEventListenersOfFixedPriority
( local  listenerID)
protected

Sorts the listeners of specified type by fixed priority.

void
sortEventListenersOfSceneGraphPriority
( const EventListener::ListenerID listenerID)
protected

Sorts the listeners of specified type by scene graph priority.

var
sortEventListenersOfSceneGraphPriority
( var  listenerID)
protected

Sorts the listeners of specified type by scene graph priority.

local
sortEventListenersOfSceneGraphPriority
( local  listenerID)
protected

Sorts the listeners of specified type by scene graph priority.

void updateDirtyFlagForSceneGraph ( )
protected

Update dirty flag.

var updateDirtyFlagForSceneGraph ( )
protected

Update dirty flag.

local updateDirtyFlagForSceneGraph ( )
protected

Update dirty flag.

void updateListeners ( Event event)
protected

Updates all listeners 1) Removes all listener items that have been marked as 'removed' when dispatching event.

2) Adds all listener items that have been marked as 'added' when dispatching event.

var updateListeners ( var  event)
protected

Updates all listeners 1) Removes all listener items that have been marked as 'removed' when dispatching event.

2) Adds all listener items that have been marked as 'added' when dispatching event.

local updateListeners ( local  event)
protected

Updates all listeners 1) Removes all listener items that have been marked as 'removed' when dispatching event.

2) Adds all listener items that have been marked as 'added' when dispatching event.

void visitTarget ( Node node)
protected

Walks though scene graph to get the draw order for each node, it's called before sorting event listener with scene graph priority.

var visitTarget ( var  node)
protected

Walks though scene graph to get the draw order for each node, it's called before sorting event listener with scene graph priority.

local visitTarget ( local  node)
protected

Walks though scene graph to get the draw order for each node, it's called before sorting event listener with scene graph priority.

Friends And Related Function Documentation

friend class Node
friend

Member Data Documentation

std::set<Node*> _dirtyNodes
protected

The nodes were associated with scene graph based priority listeners.

var _dirtyNodes
protected

The nodes were associated with scene graph based priority listeners.

local _dirtyNodes
protected

The nodes were associated with scene graph based priority listeners.

int _inDispatch
protected

Whether the dispatcher is dispatching event.

var _inDispatch
protected

Whether the dispatcher is dispatching event.

local _inDispatch
protected

Whether the dispatcher is dispatching event.

bool _isEnabled
protected

Whether to enable dispatching event.

var _isEnabled
protected

Whether to enable dispatching event.

local _isEnabled
protected

Whether to enable dispatching event.

std::unordered_map
<EventListener::ListenerID,
EventListenerVector*>
_listeners
protected

Listeners map.

var _listeners
protected

Listeners map.

local _listeners
protected

Listeners map.

std::unordered_map<Node*,
std::vector<EventListener*>
*> _nodeListenersMap
protected

The map of node and event listeners.

var _nodeListenersMap
protected

The map of node and event listeners.

local _nodeListenersMap
protected

The map of node and event listeners.

int _nodePriorityIndex
protected
var _nodePriorityIndex
protected
local _nodePriorityIndex
protected
std::unordered_map<Node*, int>
_nodePriorityMap
protected

The map of node and its event priority.

var _nodePriorityMap
protected

The map of node and its event priority.

local _nodePriorityMap
protected

The map of node and its event priority.

std::unordered_map
<EventListener::ListenerID,
DirtyFlag>
_priorityDirtyFlagMap
protected

The map of dirty flag.

var _priorityDirtyFlagMap
protected

The map of dirty flag.

local _priorityDirtyFlagMap
protected

The map of dirty flag.

std::vector<EventListener*>
_toAddedListeners
protected

The listeners to be added after dispatching event.

var _toAddedListeners
protected

The listeners to be added after dispatching event.

local _toAddedListeners
protected

The listeners to be added after dispatching event.


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