mirror of https://github.com/axmolengine/axmol.git
issue #2087: Adding CustomEvent and CustomEventListener classes. Make Event and EventListener as abstract classes.
This commit is contained in:
parent
40258ede68
commit
3fbf61ad52
|
@ -1 +1 @@
|
||||||
8b8a994b2fdf6fd8ef65f714619ce35ff1ca09cf
|
63ebe9b06aa15d229d0546427ccdae5cde18b857
|
|
@ -47,6 +47,8 @@ effects/CCGrabber.cpp \
|
||||||
effects/CCGrid.cpp \
|
effects/CCGrid.cpp \
|
||||||
event_dispatcher/CCAccelerationEvent.cpp \
|
event_dispatcher/CCAccelerationEvent.cpp \
|
||||||
event_dispatcher/CCAccelerationEventListener.cpp \
|
event_dispatcher/CCAccelerationEventListener.cpp \
|
||||||
|
event_dispatcher/CCCustomEvent.cpp \
|
||||||
|
event_dispatcher/CCCustomEventListener.cpp \
|
||||||
event_dispatcher/CCEvent.cpp \
|
event_dispatcher/CCEvent.cpp \
|
||||||
event_dispatcher/CCEventDispatcher.cpp \
|
event_dispatcher/CCEventDispatcher.cpp \
|
||||||
event_dispatcher/CCEventListener.cpp \
|
event_dispatcher/CCEventListener.cpp \
|
||||||
|
|
|
@ -1323,7 +1323,7 @@ void Node::setDirtyForAllEventListeners()
|
||||||
|
|
||||||
for (auto& listener : _eventlisteners)
|
for (auto& listener : _eventlisteners)
|
||||||
{
|
{
|
||||||
dispatcher->setDirtyForEventType(listener->type, true);
|
dispatcher->setDirtyForEventType(listener->_type, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,12 +34,13 @@ class AccelerationEventListener : public EventListener
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static AccelerationEventListener* create(std::function<void(Acceleration*, Event* event)> callback);
|
static AccelerationEventListener* create(std::function<void(Acceleration*, Event* event)> callback);
|
||||||
virtual AccelerationEventListener* clone() override;
|
|
||||||
|
|
||||||
~AccelerationEventListener();
|
~AccelerationEventListener();
|
||||||
|
|
||||||
|
/// Overrides
|
||||||
|
virtual AccelerationEventListener* clone() override;
|
||||||
|
virtual bool checkAvaiable() override;
|
||||||
private:
|
private:
|
||||||
AccelerationEventListener();
|
AccelerationEventListener();
|
||||||
virtual bool checkAvaiable() override;
|
|
||||||
|
|
||||||
bool init(std::function<void(Acceleration*, Event* event)> callback);
|
bool init(std::function<void(Acceleration*, Event* event)> callback);
|
||||||
std::function<void(Acceleration*, Event*)> onAccelerationEvent;
|
std::function<void(Acceleration*, Event*)> onAccelerationEvent;
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
/****************************************************************************
|
||||||
|
Copyright (c) 2013 cocos2d-x.org
|
||||||
|
|
||||||
|
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 "CCCustomEvent.h"
|
||||||
|
|
||||||
|
NS_CC_BEGIN
|
||||||
|
|
||||||
|
CustomEvent::CustomEvent(const std::string& eventName)
|
||||||
|
: Event(eventName)
|
||||||
|
, _userData(nullptr)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
NS_CC_END
|
|
@ -0,0 +1,50 @@
|
||||||
|
/****************************************************************************
|
||||||
|
Copyright (c) 2013 cocos2d-x.org
|
||||||
|
|
||||||
|
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__CCCustomEvent__
|
||||||
|
#define __cocos2d_libs__CCCustomEvent__
|
||||||
|
|
||||||
|
#include "CCEvent.h"
|
||||||
|
|
||||||
|
NS_CC_BEGIN
|
||||||
|
|
||||||
|
class CustomEvent : public Event
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/** Constructor */
|
||||||
|
CustomEvent(const std::string& eventName);
|
||||||
|
|
||||||
|
/** Set user data */
|
||||||
|
inline void setUserData(void* data) { _userData = data; };
|
||||||
|
|
||||||
|
/** Get user data */
|
||||||
|
inline void* getUserData() const { return _userData; };
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void* _userData; ///< User data
|
||||||
|
};
|
||||||
|
|
||||||
|
NS_CC_END
|
||||||
|
|
||||||
|
#endif /* defined(__cocos2d_libs__CCCustomEvent__) */
|
|
@ -0,0 +1,93 @@
|
||||||
|
/****************************************************************************
|
||||||
|
Copyright (c) 2013 cocos2d-x.org
|
||||||
|
|
||||||
|
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 "CCCustomEventListener.h"
|
||||||
|
#include "CCCustomEvent.h"
|
||||||
|
|
||||||
|
NS_CC_BEGIN
|
||||||
|
|
||||||
|
CustomEventListener::CustomEventListener()
|
||||||
|
: _onCustomEvent(nullptr)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
CustomEventListener* CustomEventListener::create(const std::string& eventName, std::function<void(CustomEvent*)> callback)
|
||||||
|
{
|
||||||
|
CustomEventListener* ret = new CustomEventListener();
|
||||||
|
if (ret && ret->init(eventName, callback))
|
||||||
|
{
|
||||||
|
ret->autorelease();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CC_SAFE_DELETE(ret);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CustomEventListener::init(const std::string& eventName, std::function<void(CustomEvent*)>callback)
|
||||||
|
{
|
||||||
|
bool ret = false;
|
||||||
|
|
||||||
|
_onCustomEvent = callback;
|
||||||
|
|
||||||
|
auto listener = [this](Event* event){
|
||||||
|
if (_onCustomEvent != nullptr)
|
||||||
|
{
|
||||||
|
_onCustomEvent(static_cast<CustomEvent*>(event));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (EventListener::init(eventName, listener))
|
||||||
|
{
|
||||||
|
ret = true;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
CustomEventListener* CustomEventListener::clone()
|
||||||
|
{
|
||||||
|
CustomEventListener* ret = new CustomEventListener();
|
||||||
|
if (ret && ret->init(_type, _onCustomEvent))
|
||||||
|
{
|
||||||
|
ret->autorelease();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CC_SAFE_DELETE(ret);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CustomEventListener::checkAvaiable()
|
||||||
|
{
|
||||||
|
bool ret = false;
|
||||||
|
if (EventListener::checkAvaiable() && _onCustomEvent != nullptr)
|
||||||
|
{
|
||||||
|
ret = true;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
NS_CC_END
|
|
@ -0,0 +1,77 @@
|
||||||
|
/****************************************************************************
|
||||||
|
Copyright (c) 2013 cocos2d-x.org
|
||||||
|
|
||||||
|
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__
|
||||||
|
|
||||||
|
#include "CCEventListener.h"
|
||||||
|
|
||||||
|
NS_CC_BEGIN
|
||||||
|
|
||||||
|
class CustomEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Usage:
|
||||||
|
* auto dispatcher = EventDispatcher::getInstance();
|
||||||
|
* Adds a listener:
|
||||||
|
*
|
||||||
|
* auto callback = [](CustomEvent* event){ do_some_thing(); };
|
||||||
|
* auto listener = CustomEventListener::create(callback);
|
||||||
|
* dispatcher->addEventListenerWithSceneGraphPriority(listener, one_node);
|
||||||
|
*
|
||||||
|
* Dispatchs a custom event:
|
||||||
|
*
|
||||||
|
* Event event("your_event_type");
|
||||||
|
* dispatcher->dispatchEvent(&event);
|
||||||
|
*
|
||||||
|
* Removes a listener
|
||||||
|
*
|
||||||
|
* dispatcher->removeListener(listener);
|
||||||
|
*/
|
||||||
|
class CustomEventListener : public EventListener
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/** Creates an event listener with type and callback.
|
||||||
|
* @param eventType The type of the event.
|
||||||
|
* @param callback The callback function when the specified event was emitted.
|
||||||
|
*/
|
||||||
|
static CustomEventListener* create(const std::string& eventName, std::function<void(CustomEvent*)> callback);
|
||||||
|
|
||||||
|
/// Overrides
|
||||||
|
virtual bool checkAvaiable() override;
|
||||||
|
virtual CustomEventListener* clone() override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/** Constructor */
|
||||||
|
CustomEventListener();
|
||||||
|
|
||||||
|
/** Initializes event with type and callback function */
|
||||||
|
bool init(const std::string& eventName, std::function<void(CustomEvent*)> callback);
|
||||||
|
|
||||||
|
std::function<void(CustomEvent*)> _onCustomEvent;
|
||||||
|
};
|
||||||
|
|
||||||
|
NS_CC_END
|
||||||
|
|
||||||
|
#endif /* defined(__cocos2d_libs__CCCustomEventListener__) */
|
|
@ -30,7 +30,6 @@ Event::Event(const std::string& type)
|
||||||
: _type(type)
|
: _type(type)
|
||||||
, _isStopped(false)
|
, _isStopped(false)
|
||||||
, _currentTarget(nullptr)
|
, _currentTarget(nullptr)
|
||||||
, _userData(nullptr)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,10 +40,10 @@ class Node;
|
||||||
*/
|
*/
|
||||||
class Event
|
class Event
|
||||||
{
|
{
|
||||||
public:
|
protected:
|
||||||
/** Constructor */
|
/** Constructor */
|
||||||
Event(const std::string& type);
|
Event(const std::string& type);
|
||||||
|
public:
|
||||||
/** Destructor */
|
/** Destructor */
|
||||||
virtual ~Event();
|
virtual ~Event();
|
||||||
|
|
||||||
|
@ -63,12 +63,6 @@ public:
|
||||||
*/
|
*/
|
||||||
inline Node* getCurrentTarget() { return _currentTarget; };
|
inline Node* getCurrentTarget() { return _currentTarget; };
|
||||||
|
|
||||||
/** Set user data */
|
|
||||||
inline void setUserData(void* data) { _userData = data; };
|
|
||||||
|
|
||||||
/** Get user data */
|
|
||||||
inline void* getUserData() const { return _userData; };
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/** Sets current target */
|
/** Sets current target */
|
||||||
inline void setCurrentTarget(Node* target) { _currentTarget = target; };
|
inline void setCurrentTarget(Node* target) { _currentTarget = target; };
|
||||||
|
@ -76,7 +70,6 @@ protected:
|
||||||
std::string _type; ///< Event type
|
std::string _type; ///< Event type
|
||||||
bool _isStopped; ///< whether the event has been stopped.
|
bool _isStopped; ///< whether the event has been stopped.
|
||||||
Node* _currentTarget; ///< Current target
|
Node* _currentTarget; ///< Current target
|
||||||
void* _userData; ///< User data
|
|
||||||
|
|
||||||
friend class EventDispatcher;
|
friend class EventDispatcher;
|
||||||
};
|
};
|
||||||
|
|
|
@ -98,12 +98,12 @@ void EventDispatcher::addEventListenerWithItem(EventListenerItem* item)
|
||||||
{
|
{
|
||||||
std::vector<EventListenerItem*>* listenerList = nullptr;
|
std::vector<EventListenerItem*>* listenerList = nullptr;
|
||||||
|
|
||||||
auto iter = _listeners.find(item->listener->type);
|
auto iter = _listeners.find(item->listener->_type);
|
||||||
if (iter == _listeners.end())
|
if (iter == _listeners.end())
|
||||||
{
|
{
|
||||||
listenerList = new std::vector<EventListenerItem*>();
|
listenerList = new std::vector<EventListenerItem*>();
|
||||||
listenerList->reserve(100);
|
listenerList->reserve(100);
|
||||||
_listeners.insert(std::make_pair(item->listener->type, listenerList));
|
_listeners.insert(std::make_pair(item->listener->_type, listenerList));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -112,7 +112,7 @@ void EventDispatcher::addEventListenerWithItem(EventListenerItem* item)
|
||||||
|
|
||||||
listenerList->insert(listenerList->begin(), item);
|
listenerList->insert(listenerList->begin(), item);
|
||||||
|
|
||||||
setDirtyForEventType(item->listener->type, true);
|
setDirtyForEventType(item->listener->_type, true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -196,7 +196,7 @@ void EventDispatcher::removeEventListener(EventListener* listener)
|
||||||
iter = _listeners.erase(iter);
|
iter = _listeners.erase(iter);
|
||||||
CC_SAFE_DELETE(list);
|
CC_SAFE_DELETE(list);
|
||||||
|
|
||||||
_priorityDirtyFlagMap.erase(listener->type);
|
_priorityDirtyFlagMap.erase(listener->_type);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -225,7 +225,7 @@ void EventDispatcher::setPriority(EventListener* listener, int fixedPriority)
|
||||||
if (item->fixedPriority != fixedPriority)
|
if (item->fixedPriority != fixedPriority)
|
||||||
{
|
{
|
||||||
item->fixedPriority = fixedPriority;
|
item->fixedPriority = fixedPriority;
|
||||||
setDirtyForEventType(listener->type, true);
|
setDirtyForEventType(listener->_type, true);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -272,7 +272,7 @@ void EventDispatcher::dispatchEvent(Event* event, bool forceSortListeners)
|
||||||
CCASSERT(item, "listener item is invalid.");
|
CCASSERT(item, "listener item is invalid.");
|
||||||
|
|
||||||
event->setCurrentTarget(item->node);
|
event->setCurrentTarget(item->node);
|
||||||
item->listener->onEvent(event);
|
item->listener->_onEvent(event);
|
||||||
|
|
||||||
if (event->isStopped())
|
if (event->isStopped())
|
||||||
break;
|
break;
|
||||||
|
@ -515,12 +515,12 @@ void EventDispatcher::updateListenerItems()
|
||||||
|
|
||||||
for (auto& item : _toAddedListeners)
|
for (auto& item : _toAddedListeners)
|
||||||
{
|
{
|
||||||
auto itr = _listeners.find(item->listener->type);
|
auto itr = _listeners.find(item->listener->_type);
|
||||||
if (itr == _listeners.end())
|
if (itr == _listeners.end())
|
||||||
{
|
{
|
||||||
listenerList = new std::vector<EventListenerItem*>();
|
listenerList = new std::vector<EventListenerItem*>();
|
||||||
listenerList->reserve(100);
|
listenerList->reserve(100);
|
||||||
_listeners.insert(std::make_pair(item->listener->type, listenerList));
|
_listeners.insert(std::make_pair(item->listener->_type, listenerList));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -529,7 +529,7 @@ void EventDispatcher::updateListenerItems()
|
||||||
|
|
||||||
listenerList->push_back(item);
|
listenerList->push_back(item);
|
||||||
|
|
||||||
setDirtyForEventType(item->listener->type, true);
|
setDirtyForEventType(item->listener->_type, true);
|
||||||
}
|
}
|
||||||
_toAddedListeners.clear();
|
_toAddedListeners.clear();
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,20 +27,6 @@
|
||||||
|
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
|
||||||
EventListener* EventListener::create(const std::string& eventType, std::function<void(Event*)> callback)
|
|
||||||
{
|
|
||||||
EventListener* ret = new EventListener();
|
|
||||||
if (ret && ret->init(eventType, callback))
|
|
||||||
{
|
|
||||||
ret->autorelease();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
CC_SAFE_DELETE(ret);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
EventListener::EventListener()
|
EventListener::EventListener()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
@ -49,10 +35,10 @@ EventListener::~EventListener()
|
||||||
CCLOGINFO("In the destructor of EventListener. %p", this);
|
CCLOGINFO("In the destructor of EventListener. %p", this);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EventListener::init(const std::string& t, std::function<void(Event*)>callback)
|
bool EventListener::init(const std::string& t, std::function<void(Event*)> callback)
|
||||||
{
|
{
|
||||||
onEvent = callback;
|
_onEvent = callback;
|
||||||
type = t;
|
_type = t;
|
||||||
_isRegistered = false;
|
_isRegistered = false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -60,21 +46,7 @@ bool EventListener::init(const std::string& t, std::function<void(Event*)>callba
|
||||||
|
|
||||||
bool EventListener::checkAvaiable()
|
bool EventListener::checkAvaiable()
|
||||||
{
|
{
|
||||||
return (onEvent != nullptr);
|
return (_onEvent != nullptr);
|
||||||
}
|
|
||||||
|
|
||||||
EventListener* EventListener::clone()
|
|
||||||
{
|
|
||||||
EventListener* ret = new EventListener();
|
|
||||||
if (ret && ret->init(type, onEvent))
|
|
||||||
{
|
|
||||||
ret->autorelease();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
CC_SAFE_DELETE(ret);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
|
@ -39,33 +39,10 @@ class Event;
|
||||||
/**
|
/**
|
||||||
* The base class of event listener.
|
* The base class of event listener.
|
||||||
* If you need custom listener which with different callback, you need to inherit this class.
|
* If you need custom listener which with different callback, you need to inherit this class.
|
||||||
* For instance, you could refer to AccelerationEventListener, KeyboardEventListener or TouchEventListener.
|
* For instance, you could refer to AccelerationEventListener, KeyboardEventListener or TouchEventListener, CustomEventListener.
|
||||||
* Usage:
|
|
||||||
* auto dispatcher = EventDispatcher::getInstance();
|
|
||||||
* Adds a listener:
|
|
||||||
*
|
|
||||||
* auto callback = [](Event* event){ do_some_thing(); };
|
|
||||||
* auto listener = EventListener::create("your_event_type", callback);
|
|
||||||
* dispatcher->addEventListenerWithSceneGraphPriority(listener, one_node);
|
|
||||||
*
|
|
||||||
* Dispatchs a custom event:
|
|
||||||
*
|
|
||||||
* Event event("your_event_type");
|
|
||||||
* dispatcher->dispatchEvent(&event);
|
|
||||||
*
|
|
||||||
* Removes a listener
|
|
||||||
*
|
|
||||||
* dispatcher->removeListener(listener);
|
|
||||||
*/
|
*/
|
||||||
class EventListener : public Object
|
class EventListener : public Object
|
||||||
{
|
{
|
||||||
public:
|
|
||||||
/** Creates an event listener with type and callback.
|
|
||||||
* @param eventType The type of the event.
|
|
||||||
* @param callback The callback function when the specified event was emitted.
|
|
||||||
*/
|
|
||||||
static EventListener* create(const std::string& eventType, std::function<void(Event*)> callback);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/** Constructor */
|
/** Constructor */
|
||||||
EventListener();
|
EventListener();
|
||||||
|
@ -77,13 +54,13 @@ public:
|
||||||
virtual ~EventListener();
|
virtual ~EventListener();
|
||||||
|
|
||||||
/** Checks whether the listener is available. */
|
/** Checks whether the listener is available. */
|
||||||
virtual bool checkAvaiable();
|
virtual bool checkAvaiable() = 0;
|
||||||
|
|
||||||
/** Clones the listener, its subclasses have to override this method. */
|
/** Clones the listener, its subclasses have to override this method. */
|
||||||
virtual EventListener* clone();
|
virtual EventListener* clone() = 0;
|
||||||
protected:
|
protected:
|
||||||
std::function<void(Event*)> onEvent; /// Event callback function
|
std::function<void(Event*)> _onEvent; /// Event callback function
|
||||||
std::string type; /// Event type
|
std::string _type; /// Event type
|
||||||
bool _isRegistered; /// Whether the listener has been added to dispatcher.
|
bool _isRegistered; /// Whether the listener has been added to dispatcher.
|
||||||
|
|
||||||
friend class EventDispatcher;
|
friend class EventDispatcher;
|
||||||
|
|
|
@ -37,15 +37,16 @@ class KeyboardEventListener : public EventListener
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static KeyboardEventListener* create();
|
static KeyboardEventListener* create();
|
||||||
|
|
||||||
|
/// Overrides
|
||||||
virtual KeyboardEventListener* clone() override;
|
virtual KeyboardEventListener* clone() override;
|
||||||
|
virtual bool checkAvaiable() override;
|
||||||
|
|
||||||
std::function<void(KeyboardEvent::KeyCode, Event* event)> onKeyPressed;
|
std::function<void(KeyboardEvent::KeyCode, Event* event)> onKeyPressed;
|
||||||
std::function<void(KeyboardEvent::KeyCode, Event* event)> onKeyReleased;
|
std::function<void(KeyboardEvent::KeyCode, Event* event)> onKeyReleased;
|
||||||
private:
|
private:
|
||||||
KeyboardEventListener();
|
KeyboardEventListener();
|
||||||
bool init();
|
bool init();
|
||||||
|
|
||||||
virtual bool checkAvaiable() override;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
|
@ -37,15 +37,17 @@ class TouchEventListener : public EventListener
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static TouchEventListener* create(Touch::DispatchMode mode);
|
static TouchEventListener* create(Touch::DispatchMode mode);
|
||||||
|
|
||||||
|
/// Overrides
|
||||||
virtual TouchEventListener* clone() override;
|
virtual TouchEventListener* clone() override;
|
||||||
|
virtual bool checkAvaiable() override;
|
||||||
|
|
||||||
|
virtual ~TouchEventListener();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TouchEventListener();
|
TouchEventListener();
|
||||||
bool init(Touch::DispatchMode mode);
|
bool init(Touch::DispatchMode mode);
|
||||||
|
|
||||||
virtual ~TouchEventListener();
|
|
||||||
virtual bool checkAvaiable() override;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::function<bool(Touch*, Event*)> onTouchBegan;
|
std::function<bool(Touch*, Event*)> onTouchBegan;
|
||||||
std::function<void(Touch*, Event*)> onTouchMoved;
|
std::function<void(Touch*, Event*)> onTouchMoved;
|
||||||
|
|
|
@ -268,6 +268,8 @@ THE SOFTWARE.
|
||||||
#include "event_dispatcher/CCKeyboardEvent.h"
|
#include "event_dispatcher/CCKeyboardEvent.h"
|
||||||
#include "event_dispatcher/CCAccelerationEvent.h"
|
#include "event_dispatcher/CCAccelerationEvent.h"
|
||||||
#include "event_dispatcher/CCAccelerationEventListener.h"
|
#include "event_dispatcher/CCAccelerationEventListener.h"
|
||||||
|
#include "event_dispatcher/CCCustomEvent.h"
|
||||||
|
#include "event_dispatcher/CCCustomEventListener.h"
|
||||||
|
|
||||||
// root
|
// root
|
||||||
#include "CCCamera.h"
|
#include "CCCamera.h"
|
||||||
|
|
|
@ -55,6 +55,8 @@ SOURCES = ../actions/CCAction.cpp \
|
||||||
../event_dispatcher/CCTouch.cpp \
|
../event_dispatcher/CCTouch.cpp \
|
||||||
../event_dispatcher/CCTouchEvent.cpp \
|
../event_dispatcher/CCTouchEvent.cpp \
|
||||||
../event_dispatcher/CCTouchEventListener.cpp \
|
../event_dispatcher/CCTouchEventListener.cpp \
|
||||||
|
../event_dispatcher/CCCustomEvent.cpp \
|
||||||
|
../event_dispatcher/CCCustomEventListener.cpp \
|
||||||
../label_nodes/CCFont.cpp \
|
../label_nodes/CCFont.cpp \
|
||||||
../label_nodes/CCFontAtlas.cpp \
|
../label_nodes/CCFontAtlas.cpp \
|
||||||
../label_nodes/CCFontAtlasCache.cpp \
|
../label_nodes/CCFontAtlasCache.cpp \
|
||||||
|
|
|
@ -49,6 +49,8 @@ SOURCES = ../actions/CCAction.cpp \
|
||||||
../event_dispatcher/CCTouch.cpp \
|
../event_dispatcher/CCTouch.cpp \
|
||||||
../event_dispatcher/CCTouchEvent.cpp \
|
../event_dispatcher/CCTouchEvent.cpp \
|
||||||
../event_dispatcher/CCTouchEventListener.cpp \
|
../event_dispatcher/CCTouchEventListener.cpp \
|
||||||
|
../event_dispatcher/CCCustomEvent.cpp \
|
||||||
|
../event_dispatcher/CCCustomEventListener.cpp \
|
||||||
../draw_nodes/CCDrawingPrimitives.cpp \
|
../draw_nodes/CCDrawingPrimitives.cpp \
|
||||||
../draw_nodes/CCDrawNode.cpp \
|
../draw_nodes/CCDrawNode.cpp \
|
||||||
../effects/CCGrabber.cpp \
|
../effects/CCGrabber.cpp \
|
||||||
|
|
|
@ -44,6 +44,8 @@ SOURCES = ../actions/CCAction.cpp \
|
||||||
../event_dispatcher/CCTouch.cpp \
|
../event_dispatcher/CCTouch.cpp \
|
||||||
../event_dispatcher/CCTouchEvent.cpp \
|
../event_dispatcher/CCTouchEvent.cpp \
|
||||||
../event_dispatcher/CCTouchEventListener.cpp \
|
../event_dispatcher/CCTouchEventListener.cpp \
|
||||||
|
../event_dispatcher/CCCustomEvent.cpp \
|
||||||
|
../event_dispatcher/CCCustomEventListener.cpp \
|
||||||
../label_nodes/CCLabelAtlas.cpp \
|
../label_nodes/CCLabelAtlas.cpp \
|
||||||
../label_nodes/CCLabelBMFont.cpp \
|
../label_nodes/CCLabelBMFont.cpp \
|
||||||
../label_nodes/CCLabelTTF.cpp \
|
../label_nodes/CCLabelTTF.cpp \
|
||||||
|
|
|
@ -50,6 +50,8 @@ SOURCES += ../actions/CCAction.cpp \
|
||||||
../event_dispatcher/CCTouch.cpp \
|
../event_dispatcher/CCTouch.cpp \
|
||||||
../event_dispatcher/CCTouchEvent.cpp \
|
../event_dispatcher/CCTouchEvent.cpp \
|
||||||
../event_dispatcher/CCTouchEventListener.cpp \
|
../event_dispatcher/CCTouchEventListener.cpp \
|
||||||
|
../event_dispatcher/CCCustomEvent.cpp \
|
||||||
|
../event_dispatcher/CCCustomEventListener.cpp \
|
||||||
../label_nodes/CCLabelAtlas.cpp \
|
../label_nodes/CCLabelAtlas.cpp \
|
||||||
../label_nodes/CCLabelBMFont.cpp \
|
../label_nodes/CCLabelBMFont.cpp \
|
||||||
../label_nodes/CCLabelTTF.cpp \
|
../label_nodes/CCLabelTTF.cpp \
|
||||||
|
|
|
@ -31,6 +31,11 @@ CCBReader/CCParticleSystemQuadLoader.cpp \
|
||||||
CCBReader/CCScale9SpriteLoader.cpp \
|
CCBReader/CCScale9SpriteLoader.cpp \
|
||||||
CCBReader/CCScrollViewLoader.cpp \
|
CCBReader/CCScrollViewLoader.cpp \
|
||||||
CCBReader/CCSpriteLoader.cpp \
|
CCBReader/CCSpriteLoader.cpp \
|
||||||
|
CocoStudio/Action/CCActionFrame.cpp \
|
||||||
|
CocoStudio/Action/CCActionFrameEasing.cpp \
|
||||||
|
CocoStudio/Action/CCActionManagerEx.cpp \
|
||||||
|
CocoStudio/Action/CCActionNode.cpp \
|
||||||
|
CocoStudio/Action/CCActionObject.cpp \
|
||||||
CocoStudio/Armature/CCArmature.cpp \
|
CocoStudio/Armature/CCArmature.cpp \
|
||||||
CocoStudio/Armature/CCBone.cpp \
|
CocoStudio/Armature/CCBone.cpp \
|
||||||
CocoStudio/Armature/animation/CCArmatureAnimation.cpp \
|
CocoStudio/Armature/animation/CCArmatureAnimation.cpp \
|
||||||
|
@ -85,11 +90,6 @@ CocoStudio/Json/lib_json/json_value.cpp \
|
||||||
CocoStudio/Json/lib_json/json_writer.cpp \
|
CocoStudio/Json/lib_json/json_writer.cpp \
|
||||||
CocoStudio/Reader/CCSGUIReader.cpp \
|
CocoStudio/Reader/CCSGUIReader.cpp \
|
||||||
CocoStudio/Reader/CCSSceneReader.cpp \
|
CocoStudio/Reader/CCSSceneReader.cpp \
|
||||||
CocoStudio/Action/CCActionFrame.cpp \
|
|
||||||
CocoStudio/Action/CCActionFrameEasing.cpp \
|
|
||||||
CocoStudio/Action/CCActionManagerEx.cpp \
|
|
||||||
CocoStudio/Action/CCActionNode.cpp \
|
|
||||||
CocoStudio/Action/CCActionObject.cpp \
|
|
||||||
GUI/CCControlExtension/CCControl.cpp \
|
GUI/CCControlExtension/CCControl.cpp \
|
||||||
GUI/CCControlExtension/CCControlButton.cpp \
|
GUI/CCControlExtension/CCControlButton.cpp \
|
||||||
GUI/CCControlExtension/CCControlColourPicker.cpp \
|
GUI/CCControlExtension/CCControlColourPicker.cpp \
|
||||||
|
|
|
@ -398,7 +398,7 @@ void CustomEventTest::onEnter()
|
||||||
statusLabel->setPosition(origin + Point(size.width/2, size.height-90));
|
statusLabel->setPosition(origin + Point(size.width/2, size.height-90));
|
||||||
addChild(statusLabel);
|
addChild(statusLabel);
|
||||||
|
|
||||||
_listener = EventListener::create("game_custom_event", [=](Event* event){
|
_listener = CustomEventListener::create("game_custom_event", [=](CustomEvent* event){
|
||||||
std::string str("Custom event received, ");
|
std::string str("Custom event received, ");
|
||||||
char* buf = static_cast<char*>(event->getUserData());
|
char* buf = static_cast<char*>(event->getUserData());
|
||||||
str += buf;
|
str += buf;
|
||||||
|
@ -414,7 +414,7 @@ void CustomEventTest::onEnter()
|
||||||
++count;
|
++count;
|
||||||
char* buf = new char[10];
|
char* buf = new char[10];
|
||||||
sprintf(buf, "%d", count);
|
sprintf(buf, "%d", count);
|
||||||
Event event("game_custom_event");
|
CustomEvent event("game_custom_event");
|
||||||
event.setUserData(buf);
|
event.setUserData(buf);
|
||||||
dispatcher->dispatchEvent(&event);
|
dispatcher->dispatchEvent(&event);
|
||||||
});
|
});
|
||||||
|
|
|
@ -63,7 +63,7 @@ public:
|
||||||
virtual std::string title() override;
|
virtual std::string title() override;
|
||||||
virtual std::string subtitle() override;
|
virtual std::string subtitle() override;
|
||||||
private:
|
private:
|
||||||
EventListener* _listener;
|
CustomEventListener* _listener;
|
||||||
};
|
};
|
||||||
|
|
||||||
class LabelKeyboardEventTest : public EventDispatcherTestDemo
|
class LabelKeyboardEventTest : public EventDispatcherTestDemo
|
||||||
|
|
Loading…
Reference in New Issue