Merge pull request #3681 from dumganhar/iss2087-new-event-dispatcher

issue #2087: Small fix for EventDispatcher. Adding EventDispatcher::destroyInstance method.[ci skip]
This commit is contained in:
James Chen 2013-09-17 22:44:13 -07:00
commit 39259c0553
5 changed files with 35 additions and 11 deletions

View File

@ -49,6 +49,7 @@ THE SOFTWARE.
#include "actions/CCActionManager.h"
#include "sprite_nodes/CCAnimationCache.h"
#include "event_dispatcher/CCTouch.h"
#include "event_dispatcher/CCEventDispatcher.h"
#include "support/user_default/CCUserDefault.h"
#include "shaders/ccGLStateCache.h"
#include "shaders/CCShaderCache.h"
@ -696,7 +697,8 @@ void Director::purgeDirector()
// cocos2d-x specific data structures
UserDefault::destroyInstance();
NotificationCenter::destroyInstance();
EventDispatcher::destroyInstance();
GL::invalidateStateCache();
CHECK_GL_ERROR_DEBUG();

View File

@ -126,6 +126,8 @@ Node::Node(void)
, _isTransitionFinished(false)
, _updateScriptHandler(0)
, _componentContainer(NULL)
, _eventPriority(0)
, _oldEventPriority(0)
{
// set default scheduler and actionManager
Director *director = Director::getInstance();
@ -1307,9 +1309,11 @@ void Node::dissociateEventListener(EventListener* listener)
void Node::removeAllEventListeners()
{
auto dispatcher = EventDispatcher::getInstance();
for (auto& listener : _eventlisteners)
{
EventDispatcher::getInstance()->removeEventListener(listener);
dispatcher->removeEventListener(listener);
}
}

View File

@ -1464,8 +1464,9 @@ protected:
ComponentContainer *_componentContainer; ///< Dictionary of components
int _eventPriority;
static int _globalEventPriorityIndex;
int _eventPriority; ///< The scene graph based priority of event listener.
int _oldEventPriority; ///< The old scene graph based priority of event listener.
static int _globalEventPriorityIndex; ///< The index of global event priority.
};
//#pragma mark - NodeRGBA

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
};