axmol/cocos/2d/CCEventDispatcher.h

285 lines
10 KiB
C
Raw Normal View History

2013-09-20 22:23:13 +08:00
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
2013-09-20 22:23:13 +08:00
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CC_EVENT_DISPATCHER_H__
#define __CC_EVENT_DISPATCHER_H__
#include "CCPlatformMacros.h"
2013-09-20 22:23:13 +08:00
#include "CCEventListener.h"
#include "CCEvent.h"
2014-02-20 15:12:35 +08:00
#include "CCStdC.h"
2013-09-20 22:23:13 +08:00
#include <functional>
#include <string>
#include <unordered_map>
2013-09-20 22:23:13 +08:00
#include <list>
#include <vector>
NS_CC_BEGIN
class Event;
class EventTouch;
class Node;
2013-12-21 16:56:28 +08:00
class EventCustom;
class EventListenerCustom;
2013-09-20 22:23:13 +08:00
/**
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.
*/
class EventDispatcher : public Ref
2013-09-20 22:23:13 +08:00
{
public:
// Adds event listener
2013-09-20 22:23:13 +08:00
/** Adds a event listener for a specified event with the priority of scene graph.
* @param listener The listener of a specified event.
* @param node The 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 addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node);
/** Adds a event listener for a specified event with the fixed priority.
* @param listener The listener of a specified event.
* @param fixedPriority The 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 addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority);
2013-12-21 16:56:28 +08:00
/** Adds a Custom event listener.
It will use a fixed priority of 1.
@return the generated event. Needed in order to remove the event from the dispather
*/
EventListenerCustom* addCustomEventListener(const std::string &eventName, const std::function<void(EventCustom*)>& callback);
2013-12-21 16:56:28 +08:00
/////////////////////////////////////////////
// Removes event listener
/** Remove a listener
2013-09-20 22:23:13 +08:00
* @param listener The specified event listener which needs to be removed.
*/
void removeEventListener(EventListener* listener);
/** Removes all listeners with the same event listener type */
void removeEventListenersForType(EventListener::Type listenerType);
/** Removes all listeners which are associated with the specified target. */
void removeEventListenersForTarget(Node* target, bool recursive = false);
/** Removes all custom listeners with the same event name */
void removeCustomEventListeners(const std::string& customEventName);
2013-09-20 22:23:13 +08:00
/** Removes all listeners */
void removeAllEventListeners();
2013-09-20 22:23:13 +08:00
/////////////////////////////////////////////
// Pauses / Resumes event listener
/** Pauses all listeners which are associated the specified target. */
void pauseEventListenersForTarget(Node* target, bool recursive = false);
/** Resumes all listeners which are associated the specified target. */
void resumeEventListenersForTarget(Node* target, bool recursive = false);
/////////////////////////////////////////////
2013-09-20 22:23:13 +08:00
/** Sets listener's priority with fixed value. */
void setPriority(EventListener* listener, int fixedPriority);
/** Whether to enable dispatching events */
void setEnabled(bool isEnabled);
/** Checks whether dispatching events is enabled */
bool isEnabled() const;
/////////////////////////////////////////////
2013-09-20 22:23:13 +08:00
/** Dispatches the event
* Also removes all EventListeners marked for deletion from the
* event dispatcher list.
*/
void dispatchEvent(Event* event);
/** Dispatches a Custom Event with a event name an optional user data */
void dispatchCustomEvent(const std::string &eventName, void *optionalUserData = nullptr);
/////////////////////////////////////////////
/** Constructor of EventDispatcher */
EventDispatcher();
/** Destructor of EventDispatcher */
~EventDispatcher();
#if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS && COCOS2D_DEBUG > 0
Fix crashes in event dispatch other related safety checks/fixes 1: Add a fix to prevent events from being sent to scene nodes which are destroyed. Previously when a node was being destroyed it would try to unregister any event listeners it would have. This process would fail however in the case where event dispatching was already underway because we can't modify the listeners list while we are iterating through it. This is a pretty common occurrence since we often tear down & switch scenes in response to button clicks etc. Fix the problem by nulling out the slot for the listener we are removing in this case, so that it's node no longer receives any events after it is destroyed. The event dispatching code will cleanup this unused/nulled slot once the event processing loop has terminated. 2: When removing an event listener in cocos2d::EventDispatcher, ensure immediately it gets removed from the node priority map and dirty nodes set in order to ensure we don't inadvertently access any dangling pointers to nodes. 3: Add debug checks to ensure nodes have no associated event listeners after they are destroyed. This check will catch the problem fixed by (1) if it is still present. 4: Add some extra debug sanity checks in the event dispatcher to ensure nodes are not added to the lists twice and that they are being properly removed from the lists under certain conditions. 5: In cocos2d::Node's destructor NULL out members after releasing them in case they somehow happen to be accessed during the destructor. Move the release of the event dispatcher to the very end, since we need to check against it in debug.
2014-03-27 04:12:44 +08:00
/**
* To help track down event listener issues in debug builds.
* Verifies that the node has no event listeners associated with it when destroyed.
*/
void debugCheckNodeHasNoEventListenersOnDestruction(Node* node);
#endif
2013-12-21 16:56:28 +08:00
protected:
friend class Node;
2013-10-25 15:40:21 +08:00
/** Sets the dirty flag for a node. */
void setDirtyForNode(Node* node);
2013-09-20 22:23:13 +08:00
2013-10-25 15:40:21 +08:00
/**
* The vector to store event listeners with scene graph based priority and fixed priority.
*/
class EventListenerVector
{
public:
EventListenerVector();
~EventListenerVector();
size_t size() const;
bool empty() const;
void push_back(EventListener* item);
2013-10-24 17:27:22 +08:00
void clearSceneGraphListeners();
void clearFixedListeners();
void clear();
inline std::vector<EventListener*>* getFixedPriorityListeners() const { return _fixedListeners; };
inline std::vector<EventListener*>* getSceneGraphPriorityListeners() const { return _sceneGraphListeners; };
2013-12-12 14:15:08 +08:00
inline ssize_t getGt0Index() const { return _gt0Index; };
inline void setGt0Index(ssize_t index) { _gt0Index = index; };
private:
std::vector<EventListener*>* _fixedListeners;
std::vector<EventListener*>* _sceneGraphListeners;
2013-12-12 14:15:08 +08:00
ssize_t _gt0Index;
};
2013-09-20 22:23:13 +08:00
/** 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 forceAddEventListener
*/
void addEventListener(EventListener* listener);
2013-09-20 22:23:13 +08:00
/** Force adding an event listener
* @note force add an event listener which will ignore whether it's in dispatching.
* @see addEventListener
*/
void forceAddEventListener(EventListener* listener);
/** Gets event the listener list for the event listener type. */
EventListenerVector* getListeners(const EventListener::ListenerID& listenerID);
2013-09-20 22:23:13 +08:00
2013-10-25 15:40:21 +08:00
/** Update dirty flag */
void updateDirtyFlagForSceneGraph();
2013-09-20 22:23:13 +08:00
/** Removes all listeners with the same event listener ID */
void removeEventListenersForListenerID(const EventListener::ListenerID& listenerID);
/** Sort event listener */
void sortEventListeners(const EventListener::ListenerID& listenerID);
/** Sorts the listeners of specified type by scene graph priority */
void sortEventListenersOfSceneGraphPriority(const EventListener::ListenerID& listenerID, Node* rootNode);
/** Sorts the listeners of specified type by fixed priority */
void sortEventListenersOfFixedPriority(const EventListener::ListenerID& listenerID);
/** Updates all listeners
2013-09-20 22:23:13 +08:00
* 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 updateListeners(Event* event);
2013-09-20 22:23:13 +08:00
/** Touch event needs to be processed different with other events since it needs support ALL_AT_ONCE and ONE_BY_NONE mode. */
void dispatchTouchEvent(EventTouch* event);
2013-10-25 15:40:21 +08:00
/** Associates node with event listener */
void associateNodeAndEventListener(Node* node, EventListener* listener);
2013-10-25 15:40:21 +08:00
/** Dissociates node with event listener */
void dissociateNodeAndEventListener(Node* node, EventListener* listener);
2013-10-25 15:40:21 +08:00
/** Dispatches event to listeners with a specified listener type */
void dispatchEventToListeners(EventListenerVector* listeners, const std::function<bool(EventListener*)>& onEvent);
2013-10-25 15:40:21 +08:00
/// Priority dirty flag
enum class DirtyFlag
{
NONE = 0,
FIXED_PRIORITY = 1 << 0,
2013-10-25 15:40:21 +08:00
SCENE_GRAPH_PRIORITY = 1 << 1,
ALL = FIXED_PRIORITY | SCENE_GRAPH_PRIORITY
2013-10-25 15:40:21 +08:00
};
/** Sets the dirty flag for a specified listener ID */
void setDirty(const EventListener::ListenerID& listenerID, DirtyFlag flag);
2013-10-25 15:40:21 +08:00
/** Walks though scene graph to get the draw order for each node, it's called before sorting event listener with scene graph priority */
void visitTarget(Node* node, bool isRootNode);
2013-10-25 15:40:21 +08:00
/** Listeners map */
std::unordered_map<EventListener::ListenerID, EventListenerVector*> _listenerMap;
2013-09-20 22:23:13 +08:00
2013-10-25 15:40:21 +08:00
/** The map of dirty flag */
std::unordered_map<EventListener::ListenerID, DirtyFlag> _priorityDirtyFlagMap;
2013-10-25 15:40:21 +08:00
/** The map of node and event listeners */
std::unordered_map<Node*, std::vector<EventListener*>*> _nodeListenersMap;
2013-10-25 15:40:21 +08:00
/** The map of node and its event priority */
std::unordered_map<Node*, int> _nodePriorityMap;
2013-09-20 22:23:13 +08:00
/** key: Global Z Order, value: Sorted Nodes */
2014-01-22 08:36:19 +08:00
std::unordered_map<float, std::vector<Node*>> _globalZOrderNodeMap;
2013-10-25 15:40:21 +08:00
/** The listeners to be added after dispatching event */
std::vector<EventListener*> _toAddedListeners;
2013-10-25 15:40:21 +08:00
/** The nodes were associated with scene graph based priority listeners */
std::set<Node*> _dirtyNodes;
2013-09-20 22:23:13 +08:00
2013-10-25 15:40:21 +08:00
/** Whether the dispatcher is dispatching event */
int _inDispatch;
/** Whether to enable dispatching event */
bool _isEnabled;
int _nodePriorityIndex;
std::set<std::string> _internalCustomListenerIDs;
2013-09-20 22:23:13 +08:00
};
NS_CC_END
#endif // __CC_EVENT_DISPATCHER_H__