2012-02-10 11:46:18 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-02-10 11:46:18 +08:00
|
|
|
Copyright (c) 2011 Erawppa
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCNotificationCenter.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCArray.h"
|
|
|
|
#include "CCScriptSupport.h"
|
2012-03-20 15:04:53 +08:00
|
|
|
#include <string>
|
2012-04-17 11:12:05 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
using namespace std;
|
2012-02-10 11:46:18 +08:00
|
|
|
|
2012-10-22 13:48:11 +08:00
|
|
|
NS_CC_BEGIN
|
2012-02-10 11:46:18 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
static NotificationCenter *s_sharedNotifCenter = NULL;
|
2012-02-10 11:46:18 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
NotificationCenter::NotificationCenter()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _scriptHandler(0)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
_observers = Array::createWithCapacity(3);
|
2013-06-15 14:03:30 +08:00
|
|
|
_observers->retain();
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
NotificationCenter::~NotificationCenter()
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_observers->release();
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
NotificationCenter *NotificationCenter::getInstance()
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
|
|
|
if (!s_sharedNotifCenter)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
s_sharedNotifCenter = new NotificationCenter;
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
return s_sharedNotifCenter;
|
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
void NotificationCenter::destroyInstance()
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(s_sharedNotifCenter);
|
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
// XXX: deprecated
|
|
|
|
NotificationCenter *NotificationCenter::sharedNotificationCenter(void)
|
|
|
|
{
|
|
|
|
return NotificationCenter::getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: deprecated
|
|
|
|
void NotificationCenter::purgeNotificationCenter(void)
|
|
|
|
{
|
|
|
|
NotificationCenter::destroyInstance();
|
|
|
|
}
|
|
|
|
|
2012-02-10 11:46:18 +08:00
|
|
|
//
|
|
|
|
// internal functions
|
|
|
|
//
|
2013-07-27 04:20:19 +08:00
|
|
|
bool NotificationCenter::observerExisted(Object *target,const char *name, Object *sender)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* obj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_observers, obj)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
NotificationObserver* observer = (NotificationObserver*) obj;
|
2012-02-10 11:46:18 +08:00
|
|
|
if (!observer)
|
|
|
|
continue;
|
|
|
|
|
2013-07-27 04:20:19 +08:00
|
|
|
if (!strcmp(observer->getName(),name) && observer->getTarget() == target && observer->getSender() == sender)
|
2012-02-10 11:46:18 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// observer functions
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
void NotificationCenter::addObserver(Object *target,
|
2012-02-10 11:46:18 +08:00
|
|
|
SEL_CallFuncO selector,
|
|
|
|
const char *name,
|
2013-07-27 04:20:19 +08:00
|
|
|
Object *sender)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-07-27 04:20:19 +08:00
|
|
|
if (this->observerExisted(target, name, sender))
|
2012-02-10 11:46:18 +08:00
|
|
|
return;
|
|
|
|
|
2013-07-27 04:20:19 +08:00
|
|
|
NotificationObserver *observer = new NotificationObserver(target, selector, name, sender);
|
2012-02-10 11:46:18 +08:00
|
|
|
if (!observer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
observer->autorelease();
|
2013-06-15 14:03:30 +08:00
|
|
|
_observers->addObject(observer);
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void NotificationCenter::removeObserver(Object *target,const char *name)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* obj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_observers, obj)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
NotificationObserver* observer = static_cast<NotificationObserver*>(obj);
|
2012-02-10 11:46:18 +08:00
|
|
|
if (!observer)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!strcmp(observer->getName(),name) && observer->getTarget() == target)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_observers->removeObject(observer);
|
2012-02-10 11:46:18 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int NotificationCenter::removeAllObservers(Object *target)
|
2013-03-10 22:34:01 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object *obj = NULL;
|
|
|
|
Array *toRemove = Array::create();
|
2013-03-10 22:34:01 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_observers, obj)
|
2013-03-10 22:34:01 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
NotificationObserver *observer = static_cast<NotificationObserver *>(obj);
|
2013-03-10 22:34:01 +08:00
|
|
|
if (!observer)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (observer->getTarget() == target)
|
|
|
|
{
|
|
|
|
toRemove->addObject(observer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_observers->removeObjectsInArray(toRemove);
|
2013-03-10 22:34:01 +08:00
|
|
|
return toRemove->count();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void NotificationCenter::registerScriptObserver( Object *target, int handler,const char* name)
|
2012-08-28 12:08:15 +08:00
|
|
|
{
|
2013-05-24 11:42:27 +08:00
|
|
|
|
2013-07-27 04:42:00 +08:00
|
|
|
if (this->observerExisted(target, name, NULL))
|
2013-05-24 11:42:27 +08:00
|
|
|
return;
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
NotificationObserver *observer = new NotificationObserver(target, NULL, name, NULL);
|
2013-05-24 11:42:27 +08:00
|
|
|
if (!observer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
observer->setHandler(handler);
|
|
|
|
observer->autorelease();
|
2013-06-15 14:03:30 +08:00
|
|
|
_observers->addObject(observer);
|
2012-08-28 12:08:15 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void NotificationCenter::unregisterScriptObserver(Object *target,const char* name)
|
2013-05-24 13:45:06 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* obj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_observers, obj)
|
2012-08-28 12:08:15 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
NotificationObserver* observer = static_cast<NotificationObserver*>(obj);
|
2013-05-24 13:45:06 +08:00
|
|
|
if (!observer)
|
|
|
|
continue;
|
2013-05-24 11:42:27 +08:00
|
|
|
|
2013-05-24 13:45:06 +08:00
|
|
|
if ( !strcmp(observer->getName(),name) && observer->getTarget() == target)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_observers->removeObject(observer);
|
2013-05-24 11:42:27 +08:00
|
|
|
}
|
2012-08-28 12:08:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-27 04:20:19 +08:00
|
|
|
void NotificationCenter::postNotification(const char *name, Object *sender)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* ObserversCopy = Array::createWithCapacity(_observers->count());
|
2013-06-15 14:03:30 +08:00
|
|
|
ObserversCopy->addObjectsFromArray(_observers);
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* obj = NULL;
|
2012-10-15 09:55:37 +08:00
|
|
|
CCARRAY_FOREACH(ObserversCopy, obj)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
NotificationObserver* observer = static_cast<NotificationObserver*>(obj);
|
2012-02-10 11:46:18 +08:00
|
|
|
if (!observer)
|
|
|
|
continue;
|
|
|
|
|
2013-07-27 04:20:19 +08:00
|
|
|
if (!strcmp(name,observer->getName()) && (observer->getSender() == sender || observer->getSender() == NULL || sender == NULL))
|
2013-05-24 11:42:27 +08:00
|
|
|
{
|
|
|
|
if (0 != observer->getHandler())
|
2013-07-04 15:44:42 +08:00
|
|
|
{
|
2013-07-22 17:12:53 +08:00
|
|
|
BasicScriptData data(this, (void*)name);
|
2013-07-04 15:44:42 +08:00
|
|
|
ScriptEvent scriptEvent(kNotificationEvent,(void*)&data);
|
2013-07-22 17:24:54 +08:00
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
|
2013-05-24 11:42:27 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-27 04:20:19 +08:00
|
|
|
observer->performSelector(sender);
|
2013-05-24 11:42:27 +08:00
|
|
|
}
|
|
|
|
}
|
2012-08-28 12:08:15 +08:00
|
|
|
}
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void NotificationCenter::postNotification(const char *name)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
|
|
|
this->postNotification(name,NULL);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int NotificationCenter::getObserverHandlerByName(const char* name)
|
2013-05-24 11:42:27 +08:00
|
|
|
{
|
|
|
|
if (NULL == name || strlen(name) == 0)
|
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
return 0;
|
2013-05-24 11:42:27 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* obj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_observers, obj)
|
2013-05-24 11:42:27 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
NotificationObserver* observer = static_cast<NotificationObserver*>(obj);
|
2013-05-24 11:42:27 +08:00
|
|
|
if (NULL == observer)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ( 0 == strcmp(observer->getName(),name) )
|
|
|
|
{
|
|
|
|
return observer->getHandler();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 15:44:42 +08:00
|
|
|
return 0;
|
2013-05-24 11:42:27 +08:00
|
|
|
}
|
|
|
|
|
2012-02-10 11:46:18 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
///
|
2013-06-20 14:13:12 +08:00
|
|
|
/// NotificationObserver
|
2012-02-10 11:46:18 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-06-20 14:13:12 +08:00
|
|
|
NotificationObserver::NotificationObserver(Object *target,
|
2012-02-10 11:46:18 +08:00
|
|
|
SEL_CallFuncO selector,
|
|
|
|
const char *name,
|
2013-07-27 04:20:19 +08:00
|
|
|
Object *sender)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_target = target;
|
|
|
|
_selector = selector;
|
2013-07-27 04:20:19 +08:00
|
|
|
_sender = sender;
|
2012-02-10 11:46:18 +08:00
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
_name = name;
|
2013-06-15 14:03:30 +08:00
|
|
|
_handler = 0;
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
NotificationObserver::~NotificationObserver()
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-07-23 18:26:26 +08:00
|
|
|
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2013-07-27 04:20:19 +08:00
|
|
|
void NotificationObserver::performSelector(Object *sender)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_target)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-07-27 04:20:19 +08:00
|
|
|
if (sender) {
|
|
|
|
(_target->*_selector)(sender);
|
2012-09-25 14:54:13 +08:00
|
|
|
} else {
|
2013-07-27 04:20:19 +08:00
|
|
|
(_target->*_selector)(_sender);
|
2012-09-25 14:54:13 +08:00
|
|
|
}
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object *NotificationObserver::getTarget() const
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _target;
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SEL_CallFuncO NotificationObserver::getSelector() const
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _selector;
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
const char* NotificationObserver::getName() const
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-07-23 18:26:26 +08:00
|
|
|
return _name.c_str();
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2013-07-27 04:20:19 +08:00
|
|
|
Object* NotificationObserver::getSender() const
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-07-27 04:20:19 +08:00
|
|
|
return _sender;
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
int NotificationObserver::getHandler() const
|
2013-05-24 11:42:27 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _handler;
|
2013-05-24 11:42:27 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void NotificationObserver::setHandler(int var)
|
2013-05-24 11:42:27 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_handler = var;
|
2013-05-24 11:42:27 +08:00
|
|
|
}
|
|
|
|
|
2012-10-22 13:48:11 +08:00
|
|
|
NS_CC_END
|