issue #2087: Modify singleton implement for EventDispatcher, adding EventDispatcher::destroyInstance().

This commit is contained in:
James Chen 2013-09-18 13:00:08 +08:00
parent 37ce756d12
commit 80d1c315b3
2 changed files with 24 additions and 7 deletions

View File

@ -58,6 +58,8 @@ private:
NS_CC_BEGIN
static EventDispatcher* g_instance = nullptr;
EventDispatcher::EventListenerItem::~EventListenerItem()
{
CC_SAFE_RELEASE(this->node);
@ -73,12 +75,22 @@ EventDispatcher::EventDispatcher()
EventDispatcher::~EventDispatcher()
{
removeAllListeners();
}
EventDispatcher* EventDispatcher::getInstance()
{
static EventDispatcher _instance;
return &_instance;
if (g_instance == nullptr)
{
g_instance = new EventDispatcher();
}
return g_instance;
}
void EventDispatcher::destroyInstance()
{
CC_SAFE_DELETE(g_instance);
}
void EventDispatcher::addEventListenerWithItem(EventListenerItem* item)
@ -249,13 +261,15 @@ void EventDispatcher::setPriorityWithFixedValue(EventListener* listener, int fix
}
}
void EventDispatcher::dispatchEvent(Event* event, bool toSortListeners)
void EventDispatcher::dispatchEvent(Event* event, bool forceSortListeners)
{
if (_listeners == nullptr || !_isEnabled)
return;
if (toSortListeners)
if (forceSortListeners)
{
sortAllEventListenerItemsForType(event->_type);
}
DispatchGuard guard(_inDispatch);

View File

@ -55,6 +55,9 @@ public:
/** Gets the singleton of EventDispatcher */
static EventDispatcher* getInstance();
/** Destroys the singleton of EventDispatcher */
static void destroyInstance();
/** 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.
@ -98,7 +101,7 @@ public:
* Also removes all EventListeners marked for deletion from the
* event dispatcher list.
*/
void dispatchEvent(Event* event, bool toSortListeners = true);
void dispatchEvent(Event* event, bool forceSortListeners = true);
public:
/** Destructor of EventDispatcher */
@ -141,8 +144,8 @@ private:
std::map<std::string, std::vector<EventListenerItem*>*>* _listeners;
std::vector<EventListenerItem*> _toAddedListeners;
int _inDispatch;
bool _isEnabled;
int _inDispatch; ///< Whether it's in dispatching event
bool _isEnabled; ///< Whether to enable dispatching event
};