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-07-19 17:22:36 +08:00
|
|
|
NS_CC_BEGIN;
|
2012-02-10 11:46:18 +08:00
|
|
|
|
|
|
|
static CCNotificationCenter *s_sharedNotifCenter = NULL;
|
|
|
|
|
|
|
|
CCNotificationCenter::CCNotificationCenter()
|
2012-08-28 12:08:15 +08:00
|
|
|
: m_scriptHandler(0)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2012-07-23 22:49:11 +08:00
|
|
|
m_observers = CCArray::createWithCapacity(3);
|
2012-02-10 11:46:18 +08:00
|
|
|
m_observers->retain();
|
|
|
|
}
|
|
|
|
|
|
|
|
CCNotificationCenter::~CCNotificationCenter()
|
|
|
|
{
|
2012-08-28 12:08:15 +08:00
|
|
|
unregisterScriptObserver();
|
2012-02-10 11:46:18 +08:00
|
|
|
m_observers->release();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
CCARRAY_FOREACH(m_observers, obj)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
m_observers->addObject(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCNotificationCenter::removeObserver(CCObject *target,const char *name)
|
|
|
|
{
|
|
|
|
CCObject* obj = NULL;
|
|
|
|
CCARRAY_FOREACH(m_observers, obj)
|
|
|
|
{
|
|
|
|
CCNotificationObserver* observer = (CCNotificationObserver*) obj;
|
|
|
|
if (!observer)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!strcmp(observer->getName(),name) && observer->getTarget() == target)
|
|
|
|
{
|
|
|
|
m_observers->removeObject(observer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 12:08:15 +08:00
|
|
|
void CCNotificationCenter::registerScriptObserver(int handler)
|
|
|
|
{
|
|
|
|
unregisterScriptObserver();
|
|
|
|
m_scriptHandler = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCNotificationCenter::unregisterScriptObserver(void)
|
|
|
|
{
|
|
|
|
if (m_scriptHandler)
|
|
|
|
{
|
2012-09-11 14:02:33 +08:00
|
|
|
CCScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptHandler(m_scriptHandler);
|
2012-08-28 12:08:15 +08:00
|
|
|
}
|
|
|
|
m_scriptHandler = 0;
|
|
|
|
}
|
|
|
|
|
2012-02-10 11:46:18 +08:00
|
|
|
void CCNotificationCenter::postNotification(const char *name, CCObject *object)
|
|
|
|
{
|
2012-10-15 09:55:37 +08:00
|
|
|
CCArray* ObserversCopy = CCArray::createWithCapacity(m_observers->count());
|
|
|
|
ObserversCopy->addObjectsFromArray(m_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))
|
2012-10-15 09:55:37 +08:00
|
|
|
observer->performSelector(object);
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
2012-08-28 12:08:15 +08:00
|
|
|
|
|
|
|
if (m_scriptHandler)
|
|
|
|
{
|
|
|
|
CCScriptEngineProtocol* engine = CCScriptEngineManager::sharedManager()->getScriptEngine();
|
2012-09-11 14:02:33 +08:00
|
|
|
engine->executeNotificationEvent(this, name);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
///
|
|
|
|
/// CCNotificationObserver
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
CCNotificationObserver::CCNotificationObserver(CCObject *target,
|
|
|
|
SEL_CallFuncO selector,
|
|
|
|
const char *name,
|
|
|
|
CCObject *obj)
|
|
|
|
{
|
|
|
|
m_target = target;
|
|
|
|
m_selector = selector;
|
|
|
|
m_object = obj;
|
|
|
|
|
|
|
|
m_name = new char[strlen(name)+1];
|
|
|
|
memset(m_name,0,strlen(name)+1);
|
|
|
|
|
|
|
|
string orig (name);
|
|
|
|
orig.copy(m_name,strlen(name),0);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCNotificationObserver::~CCNotificationObserver()
|
|
|
|
{
|
2012-09-26 15:51:14 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(m_name);
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNotificationObserver::performSelector(CCObject *obj)
|
|
|
|
{
|
|
|
|
if (m_target)
|
|
|
|
{
|
2012-09-25 14:54:13 +08:00
|
|
|
if (obj) {
|
|
|
|
(m_target->*m_selector)(obj);
|
|
|
|
} else {
|
|
|
|
(m_target->*m_selector)(m_object);
|
|
|
|
}
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CCObject *CCNotificationObserver::getTarget()
|
|
|
|
{
|
|
|
|
return m_target;
|
|
|
|
}
|
|
|
|
|
|
|
|
SEL_CallFuncO CCNotificationObserver::getSelector()
|
|
|
|
{
|
|
|
|
return m_selector;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *CCNotificationObserver::getName()
|
|
|
|
{
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCObject *CCNotificationObserver::getObject()
|
|
|
|
{
|
|
|
|
return m_object;
|
|
|
|
}
|
|
|
|
|
2012-07-19 17:22:36 +08:00
|
|
|
NS_CC_END;
|