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"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "cocoa/CCArray.h"
|
2012-08-28 12:08:15 +08:00
|
|
|
#include "script_support/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
|
|
|
|
|
|
|
static CCNotificationCenter *s_sharedNotifCenter = NULL;
|
|
|
|
|
|
|
|
CCNotificationCenter::CCNotificationCenter()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _scriptHandler(0)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_observers = CCArray::createWithCapacity(3);
|
|
|
|
_observers->retain();
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCNotificationCenter::~CCNotificationCenter()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_observers->release();
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2012-04-24 15:02:18 +08:00
|
|
|
CCNotificationCenter *CCNotificationCenter::sharedNotificationCenter(void)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
|
|
|
if (!s_sharedNotifCenter)
|
|
|
|
{
|
|
|
|
s_sharedNotifCenter = new CCNotificationCenter;
|
|
|
|
}
|
|
|
|
return s_sharedNotifCenter;
|
|
|
|
}
|
|
|
|
|
2012-04-24 15:02:18 +08:00
|
|
|
void CCNotificationCenter::purgeNotificationCenter(void)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(s_sharedNotifCenter);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// internal functions
|
|
|
|
//
|
|
|
|
bool CCNotificationCenter::observerExisted(CCObject *target,const char *name)
|
|
|
|
{
|
|
|
|
CCObject* obj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_observers, obj)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
|
|
|
CCNotificationObserver* observer = (CCNotificationObserver*) obj;
|
|
|
|
if (!observer)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!strcmp(observer->getName(),name) && observer->getTarget() == target)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// observer functions
|
|
|
|
//
|
|
|
|
void CCNotificationCenter::addObserver(CCObject *target,
|
|
|
|
SEL_CallFuncO selector,
|
|
|
|
const char *name,
|
|
|
|
CCObject *obj)
|
|
|
|
{
|
|
|
|
if (this->observerExisted(target, name))
|
|
|
|
return;
|
|
|
|
|
|
|
|
CCNotificationObserver *observer = new CCNotificationObserver(target, selector, name, obj);
|
|
|
|
if (!observer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
observer->autorelease();
|
2013-06-15 14:03:30 +08:00
|
|
|
_observers->addObject(observer);
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNotificationCenter::removeObserver(CCObject *target,const char *name)
|
|
|
|
{
|
|
|
|
CCObject* obj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_observers, obj)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
|
|
|
CCNotificationObserver* observer = (CCNotificationObserver*) obj;
|
|
|
|
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-03-10 22:34:01 +08:00
|
|
|
int CCNotificationCenter::removeAllObservers(CCObject *target)
|
|
|
|
{
|
|
|
|
CCObject *obj = NULL;
|
|
|
|
CCArray *toRemove = CCArray::create();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_observers, obj)
|
2013-03-10 22:34:01 +08:00
|
|
|
{
|
|
|
|
CCNotificationObserver *observer = (CCNotificationObserver *)obj;
|
|
|
|
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-05-24 11:42:27 +08:00
|
|
|
void CCNotificationCenter::registerScriptObserver( CCObject *target, int handler,const char* name)
|
2012-08-28 12:08:15 +08:00
|
|
|
{
|
2013-05-24 11:42:27 +08:00
|
|
|
|
|
|
|
if (this->observerExisted(target, name))
|
|
|
|
return;
|
|
|
|
|
|
|
|
CCNotificationObserver *observer = new CCNotificationObserver(target, NULL, name, NULL);
|
|
|
|
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-05-24 11:42:27 +08:00
|
|
|
void CCNotificationCenter::unregisterScriptObserver(CCObject *target,const char* name)
|
2013-05-24 13:45:06 +08:00
|
|
|
{
|
|
|
|
CCObject* obj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_observers, obj)
|
2012-08-28 12:08:15 +08:00
|
|
|
{
|
2013-05-24 13:45:06 +08:00
|
|
|
CCNotificationObserver* observer = (CCNotificationObserver*) obj;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-10 11:46:18 +08:00
|
|
|
void CCNotificationCenter::postNotification(const char *name, CCObject *object)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCArray* ObserversCopy = CCArray::createWithCapacity(_observers->count());
|
|
|
|
ObserversCopy->addObjectsFromArray(_observers);
|
2012-02-10 11:46:18 +08:00
|
|
|
CCObject* obj = NULL;
|
2012-10-15 09:55:37 +08:00
|
|
|
CCARRAY_FOREACH(ObserversCopy, obj)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
|
|
|
CCNotificationObserver* observer = (CCNotificationObserver*) obj;
|
|
|
|
if (!observer)
|
|
|
|
continue;
|
|
|
|
|
2012-09-19 15:28:36 +08:00
|
|
|
if (!strcmp(name,observer->getName()) && (observer->getObject() == object || observer->getObject() == NULL || object == NULL))
|
2013-05-24 11:42:27 +08:00
|
|
|
{
|
|
|
|
if (0 != observer->getHandler())
|
|
|
|
{
|
|
|
|
CCScriptEngineProtocol* engine = CCScriptEngineManager::sharedManager()->getScriptEngine();
|
|
|
|
engine->executeNotificationEvent(this, name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
observer->performSelector(object);
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 12:08:15 +08:00
|
|
|
}
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNotificationCenter::postNotification(const char *name)
|
|
|
|
{
|
|
|
|
this->postNotification(name,NULL);
|
|
|
|
}
|
|
|
|
|
2013-05-24 11:42:27 +08:00
|
|
|
int CCNotificationCenter::getObserverHandlerByName(const char* name)
|
|
|
|
{
|
|
|
|
if (NULL == name || strlen(name) == 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCObject* obj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_observers, obj)
|
2013-05-24 11:42:27 +08:00
|
|
|
{
|
|
|
|
CCNotificationObserver* observer = (CCNotificationObserver*) obj;
|
|
|
|
if (NULL == observer)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ( 0 == strcmp(observer->getName(),name) )
|
|
|
|
{
|
|
|
|
return observer->getHandler();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-02-10 11:46:18 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
///
|
|
|
|
/// CCNotificationObserver
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
CCNotificationObserver::CCNotificationObserver(CCObject *target,
|
|
|
|
SEL_CallFuncO selector,
|
|
|
|
const char *name,
|
|
|
|
CCObject *obj)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_target = target;
|
|
|
|
_selector = selector;
|
|
|
|
_object = obj;
|
2012-02-10 11:46:18 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_name = new char[strlen(name)+1];
|
|
|
|
memset(_name,0,strlen(name)+1);
|
2012-02-10 11:46:18 +08:00
|
|
|
|
|
|
|
string orig (name);
|
2013-06-15 14:03:30 +08:00
|
|
|
orig.copy(_name,strlen(name),0);
|
|
|
|
_handler = 0;
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCNotificationObserver::~CCNotificationObserver()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(_name);
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNotificationObserver::performSelector(CCObject *obj)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_target)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2012-09-25 14:54:13 +08:00
|
|
|
if (obj) {
|
2013-06-15 14:03:30 +08:00
|
|
|
(_target->*_selector)(obj);
|
2012-09-25 14:54:13 +08:00
|
|
|
} else {
|
2013-06-15 14:03:30 +08:00
|
|
|
(_target->*_selector)(_object);
|
2012-09-25 14:54:13 +08:00
|
|
|
}
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 08:25:14 +08:00
|
|
|
CCObject *CCNotificationObserver::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-14 08:25:14 +08:00
|
|
|
SEL_CallFuncO CCNotificationObserver::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-06-14 08:25:14 +08:00
|
|
|
char *CCNotificationObserver::getName() const
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _name;
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-14 08:25:14 +08:00
|
|
|
CCObject *CCNotificationObserver::getObject() const
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _object;
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2013-05-24 11:42:27 +08:00
|
|
|
int CCNotificationObserver::getHandler()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _handler;
|
2013-05-24 11:42:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNotificationObserver::setHandler(int var)
|
|
|
|
{
|
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
|