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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCNOTIFICATIONCENTER_H__
|
|
|
|
#define __CCNOTIFICATIONCENTER_H__
|
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCRef.h"
|
|
|
|
#include "2d/ccTypes.h"
|
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
|
|
|
|
2014-04-09 22:29:19 +08:00
|
|
|
class __Array;
|
2013-07-11 13:38:25 +08:00
|
|
|
class ScriptHandlerMgr;
|
2014-04-09 22:29:19 +08:00
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
class CC_DLL __NotificationCenter : public Ref
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2013-07-10 15:04:00 +08:00
|
|
|
friend class ScriptHandlerMgr;
|
2012-02-10 11:46:18 +08:00
|
|
|
public:
|
2013-12-31 10:54:37 +08:00
|
|
|
/** __NotificationCenter constructor
|
2013-09-13 16:46:31 +08:00
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-12-31 10:54:37 +08:00
|
|
|
__NotificationCenter();
|
2012-10-22 13:48:11 +08:00
|
|
|
|
2013-12-31 10:54:37 +08:00
|
|
|
/** __NotificationCenter destructor
|
2013-09-13 13:52:42 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-12-31 10:54:37 +08:00
|
|
|
~__NotificationCenter();
|
2012-02-10 11:46:18 +08:00
|
|
|
|
2013-12-31 10:54:37 +08:00
|
|
|
/** Gets the single instance of __NotificationCenter. */
|
|
|
|
static __NotificationCenter *getInstance();
|
2012-10-22 13:48:11 +08:00
|
|
|
|
2013-12-31 10:54:37 +08:00
|
|
|
/** Destroys the single instance of __NotificationCenter. */
|
2013-07-12 06:24:23 +08:00
|
|
|
static void destroyInstance();
|
|
|
|
|
|
|
|
/** @deprecated use getInstance() instead */
|
2013-12-31 10:54:37 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static __NotificationCenter *sharedNotificationCenter(void);
|
2013-07-12 06:24:23 +08:00
|
|
|
|
|
|
|
/** @deprecated use destroyInstance() instead */
|
|
|
|
CC_DEPRECATED_ATTRIBUTE static void purgeNotificationCenter(void);
|
|
|
|
|
2012-02-10 11:46:18 +08:00
|
|
|
|
2012-10-22 15:49:33 +08:00
|
|
|
/** @brief Adds an observer for the specified target.
|
|
|
|
* @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.
|
2012-10-22 15:51:06 +08:00
|
|
|
* @param name The name of this notification.
|
2013-12-18 17:47:20 +08:00
|
|
|
* @param sender The object whose notifications the target wants to receive. Only notifications sent by this sender are delivered to the target. nullptr means that the sender is not used to decide whether to deliver the notification to target.
|
2012-10-22 13:48:11 +08:00
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
void 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-10-22 13:48:11 +08:00
|
|
|
|
2012-10-22 15:49:33 +08:00
|
|
|
/** @brief Removes the observer by the specified target and name.
|
|
|
|
* @param target The target of this notification.
|
|
|
|
* @param name The name of this notification.
|
2012-10-22 13:48:11 +08:00
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
void removeObserver(Ref *target,const std::string& name);
|
2012-02-10 11:46:18 +08:00
|
|
|
|
2013-03-10 22:34:01 +08:00
|
|
|
/** @brief Removes all notifications registered by this target
|
|
|
|
* @param target The target of this notification.
|
|
|
|
* @returns the number of observers removed
|
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
int removeAllObservers(Ref *target);
|
2013-03-10 22:34:01 +08:00
|
|
|
|
2012-10-22 15:49:33 +08:00
|
|
|
/** @brief Registers one hander for script binding.
|
2012-10-22 15:52:11 +08:00
|
|
|
* @note Only supports Lua Binding now.
|
2012-10-22 15:49:33 +08:00
|
|
|
* @param handler The lua handler.
|
2012-10-22 13:48:11 +08:00
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
void registerScriptObserver(Ref *target,int handler,const std::string& name);
|
2012-10-22 13:48:11 +08:00
|
|
|
|
2012-10-22 15:49:33 +08:00
|
|
|
/** Unregisters script observer */
|
2014-02-20 10:53:49 +08:00
|
|
|
void unregisterScriptObserver(Ref *target,const std::string& name);
|
2012-08-28 12:08:15 +08:00
|
|
|
|
2012-10-22 15:49:33 +08:00
|
|
|
/** @brief Posts one notification event by name.
|
|
|
|
* @param name The name of this notification.
|
2012-10-22 13:48:11 +08:00
|
|
|
*/
|
2013-12-24 10:51:47 +08:00
|
|
|
void postNotification(const std::string& name);
|
2012-10-22 13:48:11 +08:00
|
|
|
|
2012-10-22 15:49:33 +08:00
|
|
|
/** @brief Posts one notification event by name.
|
|
|
|
* @param name The name of this notification.
|
2013-12-18 17:47:20 +08:00
|
|
|
* @param sender The object posting the notification. Can be nullptr
|
2012-10-22 13:48:11 +08:00
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
void postNotification(const std::string& name, Ref *sender);
|
2012-02-10 11:46:18 +08:00
|
|
|
|
2012-10-22 15:49:33 +08:00
|
|
|
/** @brief Gets script handler.
|
2012-10-22 15:52:11 +08:00
|
|
|
* @note Only supports Lua Binding now.
|
2012-10-22 15:49:33 +08:00
|
|
|
* @return The script handle.
|
2012-10-22 13:48:11 +08:00
|
|
|
*/
|
2013-07-07 13:01:21 +08:00
|
|
|
inline int getScriptHandler() const { return _scriptHandler; };
|
2013-05-24 11:42:27 +08:00
|
|
|
|
|
|
|
/** @brief Gets observer script handler.
|
|
|
|
* @param name The name of this notification.
|
|
|
|
* @return The observer script handle.
|
|
|
|
*/
|
2013-12-24 10:51:47 +08:00
|
|
|
int getObserverHandlerByName(const std::string& name);
|
2012-02-10 11:46:18 +08:00
|
|
|
private:
|
|
|
|
// internal functions
|
2012-10-22 13:48:11 +08:00
|
|
|
|
|
|
|
// Check whether the observer exists by the specified target and name.
|
2014-02-20 10:53:49 +08:00
|
|
|
bool observerExisted(Ref *target,const std::string& name, Ref *sender);
|
2012-02-10 11:46:18 +08:00
|
|
|
|
|
|
|
// variables
|
|
|
|
//
|
2013-12-11 17:15:27 +08:00
|
|
|
__Array *_observers;
|
2013-06-15 14:03:30 +08:00
|
|
|
int _scriptHandler;
|
2012-02-10 11:46:18 +08:00
|
|
|
};
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
class CC_DLL NotificationObserver : public Ref
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-20 14:13:12 +08:00
|
|
|
/** @brief NotificationObserver constructor
|
2012-10-22 15:49:33 +08:00
|
|
|
* @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.
|
2013-12-18 17:47:20 +08:00
|
|
|
* @param sender The object whose notifications the target wants to receive. Only notifications sent by this sender are delivered to the target. nullptr means that the sender is not used to decide whether to deliver the notification to target.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-10-22 13:48:11 +08:00
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
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-10-22 13:48:11 +08:00
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** NotificationObserver destructor function
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
~NotificationObserver();
|
2012-02-10 11:46:18 +08:00
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Invokes the callback function of this observer
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
void performSelector(Ref *sender);
|
2013-07-23 18:26:26 +08:00
|
|
|
|
|
|
|
// Getters / Setters
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* getTarget() const;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-23 18:26:26 +08:00
|
|
|
SEL_CallFuncO getSelector() const;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-12-24 10:51:47 +08:00
|
|
|
const std::string& getName() const;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* getSender() const;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-23 18:26:26 +08:00
|
|
|
int getHandler() const;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-23 18:26:26 +08:00
|
|
|
void setHandler(int handler);
|
|
|
|
|
2012-02-10 11:46:18 +08:00
|
|
|
private:
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* _target;
|
2013-07-23 18:26:26 +08:00
|
|
|
SEL_CallFuncO _selector;
|
|
|
|
std::string _name;
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* _sender;
|
2013-07-23 18:26:26 +08:00
|
|
|
int _handler;
|
2012-02-10 11:46:18 +08:00
|
|
|
};
|
|
|
|
|
2012-10-22 13:48:11 +08:00
|
|
|
NS_CC_END
|
2012-02-10 11:46:18 +08:00
|
|
|
|
2013-03-10 22:34:01 +08:00
|
|
|
#endif//__CCNOTIFICATIONCENTER_H__
|