2013-09-19 09:14:51 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-09-19 09:14:51 +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 __cocos2d_libs__CCCustomEventListener__
|
|
|
|
#define __cocos2d_libs__CCCustomEventListener__
|
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCEventListener.h"
|
2013-09-19 09:14:51 +08:00
|
|
|
|
2015-03-24 18:22:52 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup base
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2013-09-19 09:14:51 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
class EventCustom;
|
2013-09-19 09:14:51 +08:00
|
|
|
|
2015-03-23 18:39:27 +08:00
|
|
|
/** @class EventListenerCustom
|
|
|
|
* @brief Custom event listener.
|
|
|
|
* @code Usage:
|
2013-10-29 15:02:26 +08:00
|
|
|
* auto dispatcher = Director::getInstance()->getEventDispatcher();
|
2013-09-19 09:14:51 +08:00
|
|
|
* Adds a listener:
|
|
|
|
*
|
2013-10-29 15:02:26 +08:00
|
|
|
* auto callback = [](EventCustom* event){ do_some_thing(); };
|
|
|
|
* auto listener = EventListenerCustom::create(callback);
|
2013-09-19 09:14:51 +08:00
|
|
|
* dispatcher->addEventListenerWithSceneGraphPriority(listener, one_node);
|
|
|
|
*
|
|
|
|
* Dispatchs a custom event:
|
|
|
|
*
|
2013-10-29 15:02:26 +08:00
|
|
|
* EventCustom event("your_event_type");
|
2013-09-19 09:14:51 +08:00
|
|
|
* dispatcher->dispatchEvent(&event);
|
|
|
|
*
|
|
|
|
* Removes a listener
|
|
|
|
*
|
2013-10-29 15:02:26 +08:00
|
|
|
* dispatcher->removeEventListener(listener);
|
2015-03-24 16:37:09 +08:00
|
|
|
* @js cc._EventListenerCustom
|
2013-09-19 09:14:51 +08:00
|
|
|
*/
|
2014-07-15 10:55:40 +08:00
|
|
|
class CC_DLL EventListenerCustom : public EventListener
|
2013-09-19 09:14:51 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Creates an event listener with type and callback.
|
2015-03-23 18:39:27 +08:00
|
|
|
* @param eventType The type of the event.
|
|
|
|
* @param callback The callback function when the specified event was emitted.
|
|
|
|
* @return An autoreleased EventListenerCustom object.
|
2013-09-19 09:14:51 +08:00
|
|
|
*/
|
2014-02-25 14:31:24 +08:00
|
|
|
static EventListenerCustom* create(const std::string& eventName, const std::function<void(EventCustom*)>& callback);
|
2013-09-19 09:14:51 +08:00
|
|
|
|
|
|
|
/// Overrides
|
2013-10-12 11:25:28 +08:00
|
|
|
virtual bool checkAvailable() override;
|
2013-09-20 19:19:31 +08:00
|
|
|
virtual EventListenerCustom* clone() override;
|
2013-09-19 09:14:51 +08:00
|
|
|
|
2014-03-21 13:44:29 +08:00
|
|
|
CC_CONSTRUCTOR_ACCESS:
|
2013-09-19 09:14:51 +08:00
|
|
|
/** Constructor */
|
2013-09-20 19:19:31 +08:00
|
|
|
EventListenerCustom();
|
2013-09-19 09:14:51 +08:00
|
|
|
|
|
|
|
/** Initializes event with type and callback function */
|
2014-02-25 14:31:24 +08:00
|
|
|
bool init(const ListenerID& listenerId, const std::function<void(EventCustom*)>& callback);
|
2013-09-19 09:14:51 +08:00
|
|
|
|
2014-03-21 13:44:29 +08:00
|
|
|
protected:
|
2013-09-20 19:19:31 +08:00
|
|
|
std::function<void(EventCustom*)> _onCustomEvent;
|
2013-12-22 22:10:21 +08:00
|
|
|
|
|
|
|
friend class LuaEventListenerCustom;
|
2013-09-19 09:14:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
2015-03-24 18:22:52 +08:00
|
|
|
// end of base group
|
|
|
|
/// @}
|
|
|
|
|
2013-09-19 09:14:51 +08:00
|
|
|
#endif /* defined(__cocos2d_libs__CCCustomEventListener__) */
|