2013-09-20 22:23:13 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
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__
|
|
|
|
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCPlatformMacros.h"
|
2013-09-20 22:23:13 +08:00
|
|
|
#include "CCEventListener.h"
|
2013-10-25 10:35:48 +08:00
|
|
|
#include "CCEvent.h"
|
2013-09-20 22:23:13 +08:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
2013-10-24 20:50:21 +08:00
|
|
|
#include <unordered_map>
|
2013-09-20 22:23:13 +08:00
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class Event;
|
|
|
|
class EventTouch;
|
|
|
|
class Node;
|
|
|
|
|
|
|
|
/**
|
|
|
|
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.
|
|
|
|
*/
|
2013-10-26 15:04:01 +08:00
|
|
|
class EventDispatcher : public Object
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** 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);
|
|
|
|
|
|
|
|
/** Remove a listener
|
|
|
|
* @param listener The specified event listener which needs to be removed.
|
|
|
|
*/
|
|
|
|
void removeEventListener(EventListener* listener);
|
|
|
|
|
2013-10-28 16:00:01 +08:00
|
|
|
/** Removes all listeners with the same event listener ID */
|
|
|
|
void removeEventListeners(EventListener::ListenerID listenerID);
|
2013-09-20 22:23:13 +08:00
|
|
|
|
|
|
|
/** Removes all listeners */
|
2013-10-25 16:34:26 +08:00
|
|
|
void removeAllEventListeners();
|
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;
|
|
|
|
|
|
|
|
/** Dispatches the event
|
|
|
|
* Also removes all EventListeners marked for deletion from the
|
|
|
|
* event dispatcher list.
|
|
|
|
*/
|
2013-10-23 11:27:24 +08:00
|
|
|
void dispatchEvent(Event* event);
|
|
|
|
|
2013-10-25 15:40:21 +08:00
|
|
|
/** Sets the dirty flag for a node. */
|
2013-10-23 11:27:24 +08:00
|
|
|
void setDirtyForNode(Node* node);
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-25 15:40:21 +08:00
|
|
|
/** Notifys event dispatcher that the node has been paused. */
|
2013-10-23 11:27:24 +08:00
|
|
|
void pauseTarget(Node* node);
|
2013-10-25 15:40:21 +08:00
|
|
|
|
|
|
|
/** Notifys event dispatcher that the node has been resumed. */
|
2013-10-23 11:27:24 +08:00
|
|
|
void resumeTarget(Node* node);
|
2013-10-25 15:40:21 +08:00
|
|
|
|
|
|
|
/** Notifys event dispatcher that the node has been deleted. */
|
2013-10-23 11:27:24 +08:00
|
|
|
void cleanTarget(Node* node);
|
2013-10-21 17:22:42 +08:00
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
/** Constructor of EventDispatcher */
|
|
|
|
EventDispatcher();
|
2013-09-20 22:23:13 +08:00
|
|
|
/** Destructor of EventDispatcher */
|
|
|
|
~EventDispatcher();
|
|
|
|
|
|
|
|
private:
|
2013-10-21 17:22:42 +08:00
|
|
|
|
2013-10-25 15:40:21 +08:00
|
|
|
/**
|
|
|
|
* The vector to store event listeners with scene graph based priority and fixed priority.
|
|
|
|
*/
|
2013-10-23 11:27:24 +08:00
|
|
|
class EventListenerVector
|
2013-10-21 17:22:42 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-10-23 11:27:24 +08:00
|
|
|
EventListenerVector();
|
|
|
|
~EventListenerVector();
|
2013-10-21 17:22:42 +08:00
|
|
|
size_t size() const;
|
|
|
|
bool empty() const;
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
void push_back(EventListener* item);
|
2013-10-24 17:27:22 +08:00
|
|
|
void clearSceneGraphListeners();
|
|
|
|
void clearFixedListeners();
|
2013-10-23 11:27:24 +08:00
|
|
|
void clear();
|
2013-10-21 17:22:42 +08:00
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
inline std::vector<EventListener*>* getFixedPriorityListeners() const { return _fixedListeners; };
|
|
|
|
inline std::vector<EventListener*>* getSceneGraphPriorityListeners() const { return _sceneGraphListeners; };
|
|
|
|
inline int getGt0Index() const { return _gt0Index; };
|
|
|
|
inline void setGt0Index(int index) { _gt0Index = index; };
|
2013-10-21 17:22:42 +08:00
|
|
|
private:
|
2013-10-23 11:27:24 +08:00
|
|
|
std::vector<EventListener*>* _fixedListeners;
|
|
|
|
std::vector<EventListener*>* _sceneGraphListeners;
|
|
|
|
int _gt0Index;
|
2013-10-21 17:22:42 +08:00
|
|
|
};
|
2013-09-20 22:23:13 +08:00
|
|
|
|
|
|
|
/** Adds event listener with item */
|
2013-10-23 11:27:24 +08:00
|
|
|
void addEventListener(EventListener* listener);
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-25 10:35:48 +08:00
|
|
|
/** Gets event the listener list for the event listener type. */
|
2013-10-28 16:00:01 +08:00
|
|
|
EventListenerVector* getListeners(EventListener::ListenerID listenerID);
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-25 15:40:21 +08:00
|
|
|
/** Update dirty flag */
|
2013-10-23 11:27:24 +08:00
|
|
|
void updateDirtyFlagForSceneGraph();
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-25 10:35:48 +08:00
|
|
|
/** Sort event listener */
|
2013-10-28 16:00:01 +08:00
|
|
|
void sortEventListeners(EventListener::ListenerID listenerID);
|
2013-10-25 10:35:48 +08:00
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
/** Sorts the listeners of specified type by scene graph priority */
|
2013-10-28 16:00:01 +08:00
|
|
|
void sortEventListenersOfSceneGraphPriority(EventListener::ListenerID listenerID);
|
2013-10-23 11:27:24 +08:00
|
|
|
|
|
|
|
/** Sorts the listeners of specified type by fixed priority */
|
2013-10-28 16:00:01 +08:00
|
|
|
void sortEventListenersOfFixedPriority(EventListener::ListenerID listenerID);
|
2013-10-23 11:27:24 +08:00
|
|
|
|
|
|
|
/** 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.
|
|
|
|
*/
|
2013-10-28 16:00:01 +08:00
|
|
|
void updateListeners(Event* event);
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-25 10:35:48 +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 */
|
2013-10-23 11:27:24 +08:00
|
|
|
void associateNodeAndEventListener(Node* node, EventListener* listener);
|
2013-10-25 15:40:21 +08:00
|
|
|
|
|
|
|
/** Dissociates node with event listener */
|
2013-10-23 11:27:24 +08:00
|
|
|
void dissociateNodeAndEventListener(Node* node, EventListener* listener);
|
|
|
|
|
2013-10-25 15:40:21 +08:00
|
|
|
/** Dispatches event to listeners with a specified listener type */
|
2013-10-23 11:27:24 +08:00
|
|
|
void dispatchEventToListeners(EventListenerVector* listeners, std::function<bool(EventListener*)> onEvent);
|
|
|
|
|
2013-10-25 15:40:21 +08:00
|
|
|
/// Priority dirty flag
|
|
|
|
enum class DirtyFlag
|
|
|
|
{
|
|
|
|
NONE = 0,
|
|
|
|
FIXED_PRITORY = 1 << 0,
|
|
|
|
SCENE_GRAPH_PRIORITY = 1 << 1,
|
|
|
|
ALL = FIXED_PRITORY | SCENE_GRAPH_PRIORITY
|
|
|
|
};
|
|
|
|
|
2013-10-28 16:00:01 +08:00
|
|
|
/** Sets the dirty flag for a specified listener ID */
|
|
|
|
void setDirty(EventListener::ListenerID listenerID, DirtyFlag flag);
|
2013-10-24 20:50:21 +08:00
|
|
|
|
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 */
|
2013-10-24 20:50:21 +08:00
|
|
|
void visitTarget(Node* node);
|
|
|
|
|
2013-09-20 22:23:13 +08:00
|
|
|
private:
|
2013-10-25 15:40:21 +08:00
|
|
|
/** Listeners map */
|
2013-10-28 16:00:01 +08:00
|
|
|
std::unordered_map<EventListener::ListenerID, EventListenerVector*> _listeners;
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-25 15:40:21 +08:00
|
|
|
/** The map of dirty flag */
|
2013-10-28 16:00:01 +08:00
|
|
|
std::unordered_map<EventListener::ListenerID, DirtyFlag> _priorityDirtyFlagMap;
|
2013-10-23 11:27:24 +08:00
|
|
|
|
2013-10-25 15:40:21 +08:00
|
|
|
/** The map of node and event listeners */
|
2013-10-24 20:50:21 +08:00
|
|
|
std::unordered_map<Node*, std::vector<EventListener*>*> _nodeListenersMap;
|
2013-10-25 15:40:21 +08:00
|
|
|
|
|
|
|
/** The map of node and its event priority */
|
2013-10-24 20:50:21 +08:00
|
|
|
std::unordered_map<Node*, int> _nodePriorityMap;
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-10-25 15:40:21 +08:00
|
|
|
/** The listeners to be added after dispatching event */
|
2013-10-23 11:27:24 +08:00
|
|
|
std::vector<EventListener*> _toAddedListeners;
|
2013-10-25 15:40:21 +08:00
|
|
|
|
|
|
|
/** The nodes were associated with scene graph based priority listeners */
|
2013-10-23 11:27:24 +08:00
|
|
|
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;
|
2013-10-25 17:03:50 +08:00
|
|
|
|
|
|
|
int _nodePriorityIndex;
|
2013-09-20 22:23:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
|
|
|
|
#endif // __CC_EVENT_DISPATCHER_H__
|