2013-09-03 18:22:03 +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__
|
|
|
|
|
|
|
|
#include "platform/CCPlatformMacros.h"
|
|
|
|
#include "CCEventListener.h"
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <list>
|
2013-09-15 21:58:54 +08:00
|
|
|
#include <vector>
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class Event;
|
2013-09-12 17:31:37 +08:00
|
|
|
class TouchEvent;
|
2013-09-03 18:22:03 +08:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
class EventDispatcher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Gets the singleton of EventDispatcher */
|
|
|
|
static EventDispatcher* getInstance();
|
|
|
|
|
2013-09-18 13:00:08 +08:00
|
|
|
/** Destroys the singleton of EventDispatcher */
|
|
|
|
static void destroyInstance();
|
|
|
|
|
2013-09-15 11:06:19 +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.
|
2013-09-16 14:43:57 +08:00
|
|
|
* @note The priority of scene graph will be fixed value 0. So the order of listener item
|
2013-09-16 22:46:44 +08:00
|
|
|
* in the vector will be ' <0, scene graph (0 priority), >0'.
|
2013-09-03 18:22:03 +08:00
|
|
|
*/
|
2013-09-14 09:02:49 +08:00
|
|
|
void addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-09-15 11:06:19 +08:00
|
|
|
/** 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.
|
2013-09-16 14:43:57 +08:00
|
|
|
* @note A lower priority will be called before the ones that have a higher value.
|
2013-09-16 22:46:44 +08:00
|
|
|
* 0 priority is forbidden for fixed priority since it's used for scene graph based priority.
|
2013-09-03 18:22:03 +08:00
|
|
|
*/
|
2013-09-14 09:02:49 +08:00
|
|
|
void addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-09-16 14:43:57 +08:00
|
|
|
/** Remove a listener
|
|
|
|
* @param listener The specified event listener which needs to be removed.
|
|
|
|
*/
|
2013-09-14 09:02:49 +08:00
|
|
|
void removeEventListener(EventListener* listener);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-09-15 11:06:19 +08:00
|
|
|
/** Removes all listeners with the same event type */
|
2013-09-14 09:02:49 +08:00
|
|
|
void removeListenersForEventType(const std::string& eventType);
|
|
|
|
|
|
|
|
/** Removes all listeners */
|
|
|
|
void removeAllListeners();
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
/** Sets listener's priority with fixed value. */
|
2013-09-18 17:45:25 +08:00
|
|
|
void setPriority(EventListener* listener, int fixedPriority);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
/** Whether to enable dispatching events */
|
2013-09-14 09:02:49 +08:00
|
|
|
void setEnabled(bool isEnabled);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
/** Checks whether dispatching events is enabled */
|
2013-09-14 09:02:49 +08:00
|
|
|
bool isEnabled() const;
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
/** Dispatches the event
|
|
|
|
* Also removes all EventListeners marked for deletion from the
|
|
|
|
* event dispatcher list.
|
|
|
|
*/
|
2013-09-18 17:45:25 +08:00
|
|
|
void dispatchEvent(Event* event, bool forceSortListeners = false);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-09-18 17:45:25 +08:00
|
|
|
void setDirtyForEventType(const std::string& eventType, bool isDirty);
|
|
|
|
|
|
|
|
bool isDirtyForEventType(const std::string& eventType);
|
|
|
|
|
2013-09-12 20:45:35 +08:00
|
|
|
public:
|
|
|
|
/** Destructor of EventDispatcher */
|
|
|
|
~EventDispatcher();
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
private:
|
2013-09-12 20:45:35 +08:00
|
|
|
struct EventListenerItem
|
|
|
|
{
|
2013-09-13 18:00:56 +08:00
|
|
|
int fixedPriority; // The higher the number, the higher the priority
|
|
|
|
Node* node; // Weak reference.
|
|
|
|
EventListener* listener;
|
2013-09-17 16:59:54 +08:00
|
|
|
~EventListenerItem();
|
2013-09-12 20:45:35 +08:00
|
|
|
};
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
/** Constructor of EventDispatcher */
|
|
|
|
EventDispatcher();
|
2013-09-12 20:45:35 +08:00
|
|
|
|
2013-09-15 11:06:19 +08:00
|
|
|
/** Adds event listener with item */
|
|
|
|
void addEventListenerWithItem(EventListenerItem* item);
|
2013-09-12 20:45:35 +08:00
|
|
|
|
2013-09-12 17:31:37 +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(TouchEvent* event);
|
2013-09-12 20:45:35 +08:00
|
|
|
|
2013-09-15 11:06:19 +08:00
|
|
|
/** Gets event the listener list for the event type. */
|
2013-09-15 21:58:54 +08:00
|
|
|
std::vector<EventListenerItem*>* getListenerItemsForType(const std::string& eventType);
|
2013-09-12 20:45:35 +08:00
|
|
|
|
2013-09-15 11:06:19 +08:00
|
|
|
/** Sorts the listeners of specified type by priority */
|
|
|
|
void sortAllEventListenerItemsForType(const std::string& eventType);
|
2013-09-12 20:45:35 +08:00
|
|
|
|
2013-09-17 16:59:54 +08:00
|
|
|
/** Updates all listener items
|
|
|
|
* 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 updateListenerItems();
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Listeners map.
|
|
|
|
*/
|
2013-09-18 17:45:25 +08:00
|
|
|
std::map<std::string, std::vector<EventListenerItem*>*> _listeners;
|
|
|
|
|
|
|
|
/// Priority dirty flag
|
|
|
|
std::map<std::string, bool> _priorityDirtyFlagMap;
|
|
|
|
|
2013-09-17 16:59:54 +08:00
|
|
|
std::vector<EventListenerItem*> _toAddedListeners;
|
|
|
|
|
2013-09-18 13:00:08 +08:00
|
|
|
int _inDispatch; ///< Whether it's in dispatching event
|
|
|
|
bool _isEnabled; ///< Whether to enable dispatching event
|
2013-09-03 18:22:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
|
|
|
|
#endif // __CC_EVENT_DISPATCHER_H__
|