2012-02-10 11:46:18 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2011 Erawppa
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
2012-02-10 11:46:18 +08:00
|
|
|
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"
|
2014-04-09 22:29:19 +08:00
|
|
|
#include "deprecated/CCArray.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#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-12-31 10:54:37 +08:00
|
|
|
static __NotificationCenter *s_sharedNotifCenter = nullptr;
|
2012-02-10 11:46:18 +08:00
|
|
|
|
2013-12-31 10:54:37 +08:00
|
|
|
__NotificationCenter::__NotificationCenter()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _scriptHandler(0)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +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-12-31 10:54:37 +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-12-31 10:54:37 +08:00
|
|
|
__NotificationCenter *__NotificationCenter::getInstance()
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
|
|
|
if (!s_sharedNotifCenter)
|
|
|
|
{
|
2013-12-31 10:54:37 +08:00
|
|
|
s_sharedNotifCenter = new __NotificationCenter;
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
return s_sharedNotifCenter;
|
|
|
|
}
|
|
|
|
|
2013-12-31 10:54:37 +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
|
2013-12-31 10:54:37 +08:00
|
|
|
__NotificationCenter *__NotificationCenter::sharedNotificationCenter(void)
|
2013-07-12 06:24:23 +08:00
|
|
|
{
|
2013-12-31 10:54:37 +08:00
|
|
|
return __NotificationCenter::getInstance();
|
2013-07-12 06:24:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: deprecated
|
2013-12-31 10:54:37 +08:00
|
|
|
void __NotificationCenter::purgeNotificationCenter(void)
|
2013-07-12 06:24:23 +08:00
|
|
|
{
|
2013-12-31 10:54:37 +08:00
|
|
|
__NotificationCenter::destroyInstance();
|
2013-07-12 06:24:23 +08:00
|
|
|
}
|
|
|
|
|
2012-02-10 11:46:18 +08:00
|
|
|
//
|
|
|
|
// internal functions
|
|
|
|
//
|
2014-02-20 10:53:49 +08:00
|
|
|
bool __NotificationCenter::observerExisted(Ref *target, const std::string& name, Ref *sender)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* obj = nullptr;
|
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-12-25 14:20:37 +08:00
|
|
|
if (observer->getName() == name && observer->getTarget() == target && observer->getSender() == sender)
|
2012-02-10 11:46:18 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// observer functions
|
|
|
|
//
|
2014-02-20 10:53:49 +08:00
|
|
|
void __NotificationCenter::addObserver(Ref *target,
|
2012-02-10 11:46:18 +08:00
|
|
|
SEL_CallFuncO selector,
|
2013-12-24 10:51:47 +08:00
|
|
|
const std::string& name,
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref *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
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __NotificationCenter::removeObserver(Ref *target, const std::string& name)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* obj = nullptr;
|
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;
|
|
|
|
|
2013-12-25 14:20:37 +08:00
|
|
|
if (observer->getName() == name && observer->getTarget() == target)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_observers->removeObject(observer);
|
2012-02-10 11:46:18 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
int __NotificationCenter::removeAllObservers(Ref *target)
|
2013-03-10 22:34:01 +08:00
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref *obj = nullptr;
|
2013-12-11 17:15:27 +08:00
|
|
|
__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-12-12 12:07:20 +08:00
|
|
|
return static_cast<int>(toRemove->count());
|
2013-03-10 22:34:01 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __NotificationCenter::registerScriptObserver(Ref *target, int handler,const std::string& name)
|
2012-08-28 12:08:15 +08:00
|
|
|
{
|
2013-05-24 11:42:27 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
if (this->observerExisted(target, name, nullptr))
|
2013-05-24 11:42:27 +08:00
|
|
|
return;
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
NotificationObserver *observer = new NotificationObserver(target, nullptr, name, nullptr);
|
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
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __NotificationCenter::unregisterScriptObserver(Ref *target,const std::string& name)
|
2013-05-24 13:45:06 +08:00
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* obj = nullptr;
|
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-12-25 14:20:37 +08:00
|
|
|
if ( observer->getName() == name && observer->getTarget() == target)
|
2013-05-24 13:45:06 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void __NotificationCenter::postNotification(const std::string& name, Ref *sender)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
__Array* ObserversCopy = __Array::createWithCapacity(_observers->count());
|
2013-06-15 14:03:30 +08:00
|
|
|
ObserversCopy->addObjectsFromArray(_observers);
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* obj = nullptr;
|
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-12-25 14:20:37 +08:00
|
|
|
if (observer->getName() == name && (observer->getSender() == sender || observer->getSender() == nullptr || sender == nullptr))
|
2013-05-24 11:42:27 +08:00
|
|
|
{
|
2013-12-27 10:35:44 +08:00
|
|
|
if (0 == observer->getHandler())
|
2013-05-24 11:42:27 +08:00
|
|
|
{
|
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-12-31 10:54:37 +08:00
|
|
|
void __NotificationCenter::postNotification(const std::string& name)
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
this->postNotification(name,nullptr);
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2013-12-31 10:54:37 +08:00
|
|
|
int __NotificationCenter::getObserverHandlerByName(const std::string& name)
|
2013-05-24 11:42:27 +08:00
|
|
|
{
|
2013-12-24 10:51:47 +08:00
|
|
|
if (name.empty())
|
2013-05-24 11:42:27 +08:00
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
return 0;
|
2013-05-24 11:42:27 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* obj = nullptr;
|
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-12-18 17:47:20 +08:00
|
|
|
if (nullptr == observer)
|
2013-05-24 11:42:27 +08:00
|
|
|
continue;
|
|
|
|
|
2013-12-25 14:20:37 +08:00
|
|
|
if ( observer->getName() == name )
|
2013-05-24 11:42:27 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2014-02-20 10:53:49 +08:00
|
|
|
NotificationObserver::NotificationObserver(Ref *target,
|
2012-02-10 11:46:18 +08:00
|
|
|
SEL_CallFuncO selector,
|
2013-12-24 10:51:47 +08:00
|
|
|
const std::string& name,
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref *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
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void NotificationObserver::performSelector(Ref *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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref *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-12-24 10:51:47 +08:00
|
|
|
const std::string& NotificationObserver::getName() const
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-12-24 10:51:47 +08:00
|
|
|
return _name;
|
2012-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* 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
|