mirror of https://github.com/axmolengine/axmol.git
Merge pull request #3285 from gkosciolek/fix_for_CCNotificationCenter
closed 2611: Fixing the bug that observers with the same target and selector but different sender are the same observer in NotificationCenter.
This commit is contained in:
commit
c215a1d5a2
|
@ -74,7 +74,7 @@ void NotificationCenter::purgeNotificationCenter(void)
|
|||
//
|
||||
// internal functions
|
||||
//
|
||||
bool NotificationCenter::observerExisted(Object *target,const char *name)
|
||||
bool NotificationCenter::observerExisted(Object *target,const char *name, Object *sender)
|
||||
{
|
||||
Object* obj = NULL;
|
||||
CCARRAY_FOREACH(_observers, obj)
|
||||
|
@ -83,7 +83,7 @@ bool NotificationCenter::observerExisted(Object *target,const char *name)
|
|||
if (!observer)
|
||||
continue;
|
||||
|
||||
if (!strcmp(observer->getName(),name) && observer->getTarget() == target)
|
||||
if (!strcmp(observer->getName(),name) && observer->getTarget() == target && observer->getSender() == sender)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -95,12 +95,12 @@ bool NotificationCenter::observerExisted(Object *target,const char *name)
|
|||
void NotificationCenter::addObserver(Object *target,
|
||||
SEL_CallFuncO selector,
|
||||
const char *name,
|
||||
Object *obj)
|
||||
Object *sender)
|
||||
{
|
||||
if (this->observerExisted(target, name))
|
||||
if (this->observerExisted(target, name, sender))
|
||||
return;
|
||||
|
||||
NotificationObserver *observer = new NotificationObserver(target, selector, name, obj);
|
||||
NotificationObserver *observer = new NotificationObserver(target, selector, name, sender);
|
||||
if (!observer)
|
||||
return;
|
||||
|
||||
|
@ -149,7 +149,7 @@ int NotificationCenter::removeAllObservers(Object *target)
|
|||
void NotificationCenter::registerScriptObserver( Object *target, int handler,const char* name)
|
||||
{
|
||||
|
||||
if (this->observerExisted(target, name))
|
||||
if (this->observerExisted(target, name, NULL))
|
||||
return;
|
||||
|
||||
NotificationObserver *observer = new NotificationObserver(target, NULL, name, NULL);
|
||||
|
@ -177,7 +177,7 @@ void NotificationCenter::unregisterScriptObserver(Object *target,const char* nam
|
|||
}
|
||||
}
|
||||
|
||||
void NotificationCenter::postNotification(const char *name, Object *object)
|
||||
void NotificationCenter::postNotification(const char *name, Object *sender)
|
||||
{
|
||||
Array* ObserversCopy = Array::createWithCapacity(_observers->count());
|
||||
ObserversCopy->addObjectsFromArray(_observers);
|
||||
|
@ -188,7 +188,7 @@ void NotificationCenter::postNotification(const char *name, Object *object)
|
|||
if (!observer)
|
||||
continue;
|
||||
|
||||
if (!strcmp(name,observer->getName()) && (observer->getObject() == object || observer->getObject() == NULL || object == NULL))
|
||||
if (!strcmp(name,observer->getName()) && (observer->getSender() == sender || observer->getSender() == NULL || sender == NULL))
|
||||
{
|
||||
if (0 != observer->getHandler())
|
||||
{
|
||||
|
@ -198,7 +198,7 @@ void NotificationCenter::postNotification(const char *name, Object *object)
|
|||
}
|
||||
else
|
||||
{
|
||||
observer->performSelector(object);
|
||||
observer->performSelector(sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -241,11 +241,11 @@ int NotificationCenter::getObserverHandlerByName(const char* name)
|
|||
NotificationObserver::NotificationObserver(Object *target,
|
||||
SEL_CallFuncO selector,
|
||||
const char *name,
|
||||
Object *obj)
|
||||
Object *sender)
|
||||
{
|
||||
_target = target;
|
||||
_selector = selector;
|
||||
_object = obj;
|
||||
_sender = sender;
|
||||
|
||||
_name = name;
|
||||
_handler = 0;
|
||||
|
@ -256,14 +256,14 @@ NotificationObserver::~NotificationObserver()
|
|||
|
||||
}
|
||||
|
||||
void NotificationObserver::performSelector(Object *obj)
|
||||
void NotificationObserver::performSelector(Object *sender)
|
||||
{
|
||||
if (_target)
|
||||
{
|
||||
if (obj) {
|
||||
(_target->*_selector)(obj);
|
||||
if (sender) {
|
||||
(_target->*_selector)(sender);
|
||||
} else {
|
||||
(_target->*_selector)(_object);
|
||||
(_target->*_selector)(_sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -283,9 +283,9 @@ const char* NotificationObserver::getName() const
|
|||
return _name.c_str();
|
||||
}
|
||||
|
||||
Object* NotificationObserver::getObject() const
|
||||
Object* NotificationObserver::getSender() const
|
||||
{
|
||||
return _object;
|
||||
return _sender;
|
||||
}
|
||||
|
||||
int NotificationObserver::getHandler() const
|
||||
|
|
|
@ -58,12 +58,12 @@ public:
|
|||
* @param target The target which wants to observe notification events.
|
||||
* @param selector The callback function which will be invoked when the specified notification event was posted.
|
||||
* @param name The name of this notification.
|
||||
* @param obj The extra parameter which will be passed to the callback function.
|
||||
* @param sender The object whose notifications the target wants to receive. Only notifications sent by this sender are delivered to the target. NULL means that the sender is not used to decide whether to deliver the notification to target.
|
||||
*/
|
||||
void addObserver(Object *target,
|
||||
SEL_CallFuncO selector,
|
||||
const char *name,
|
||||
Object *obj);
|
||||
Object *sender);
|
||||
|
||||
/** @brief Removes the observer by the specified target and name.
|
||||
* @param target The target of this notification.
|
||||
|
@ -93,9 +93,9 @@ public:
|
|||
|
||||
/** @brief Posts one notification event by name.
|
||||
* @param name The name of this notification.
|
||||
* @param object The extra parameter.
|
||||
* @param sender The object posting the notification. Can be NULL
|
||||
*/
|
||||
void postNotification(const char *name, Object *object);
|
||||
void postNotification(const char *name, Object *sender);
|
||||
|
||||
/** @brief Gets script handler.
|
||||
* @note Only supports Lua Binding now.
|
||||
|
@ -112,7 +112,7 @@ private:
|
|||
// internal functions
|
||||
|
||||
// Check whether the observer exists by the specified target and name.
|
||||
bool observerExisted(Object *target,const char *name);
|
||||
bool observerExisted(Object *target,const char *name, Object *sender);
|
||||
|
||||
// variables
|
||||
//
|
||||
|
@ -127,24 +127,24 @@ public:
|
|||
* @param target The target which wants to observer notification events.
|
||||
* @param selector The callback function which will be invoked when the specified notification event was posted.
|
||||
* @param name The name of this notification.
|
||||
* @param obj The extra parameter which will be passed to the callback function.
|
||||
* @param sender The object whose notifications the target wants to receive. Only notifications sent by this sender are delivered to the target. NULL means that the sender is not used to decide whether to deliver the notification to target.
|
||||
*/
|
||||
NotificationObserver(Object *target,
|
||||
SEL_CallFuncO selector,
|
||||
const char *name,
|
||||
Object *obj);
|
||||
Object *sender);
|
||||
|
||||
/** NotificationObserver destructor function */
|
||||
~NotificationObserver();
|
||||
|
||||
/** Invokes the callback function of this observer */
|
||||
void performSelector(Object *obj);
|
||||
void performSelector(Object *sender);
|
||||
|
||||
// Getters / Setters
|
||||
Object* getTarget() const;
|
||||
SEL_CallFuncO getSelector() const;
|
||||
const char* getName() const;
|
||||
Object* getObject() const;
|
||||
Object* getSender() const;
|
||||
int getHandler() const;
|
||||
void setHandler(int handler);
|
||||
|
||||
|
@ -152,7 +152,7 @@ private:
|
|||
Object* _target;
|
||||
SEL_CallFuncO _selector;
|
||||
std::string _name;
|
||||
Object* _object;
|
||||
Object* _sender;
|
||||
int _handler;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue