issue #2087: Adding CustomEvent and CustomEventListener classes. Make Event and EventListener as abstract classes.

This commit is contained in:
James Chen 2013-09-19 09:14:51 +08:00
parent 40258ede68
commit 3fbf61ad52
23 changed files with 310 additions and 98 deletions

View File

@ -1 +1 @@
8b8a994b2fdf6fd8ef65f714619ce35ff1ca09cf
63ebe9b06aa15d229d0546427ccdae5cde18b857

View File

@ -47,6 +47,8 @@ effects/CCGrabber.cpp \
effects/CCGrid.cpp \
event_dispatcher/CCAccelerationEvent.cpp \
event_dispatcher/CCAccelerationEventListener.cpp \
event_dispatcher/CCCustomEvent.cpp \
event_dispatcher/CCCustomEventListener.cpp \
event_dispatcher/CCEvent.cpp \
event_dispatcher/CCEventDispatcher.cpp \
event_dispatcher/CCEventListener.cpp \

View File

@ -1323,7 +1323,7 @@ void Node::setDirtyForAllEventListeners()
for (auto& listener : _eventlisteners)
{
dispatcher->setDirtyForEventType(listener->type, true);
dispatcher->setDirtyForEventType(listener->_type, true);
}
}

View File

@ -34,12 +34,13 @@ class AccelerationEventListener : public EventListener
{
public:
static AccelerationEventListener* create(std::function<void(Acceleration*, Event* event)> callback);
virtual AccelerationEventListener* clone() override;
~AccelerationEventListener();
/// Overrides
virtual AccelerationEventListener* clone() override;
virtual bool checkAvaiable() override;
private:
AccelerationEventListener();
virtual bool checkAvaiable() override;
bool init(std::function<void(Acceleration*, Event* event)> callback);
std::function<void(Acceleration*, Event*)> onAccelerationEvent;

View File

@ -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

View File

@ -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__) */

View File

@ -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

View File

@ -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__) */

View File

@ -30,7 +30,6 @@ Event::Event(const std::string& type)
: _type(type)
, _isStopped(false)
, _currentTarget(nullptr)
, _userData(nullptr)
{
}

View File

@ -40,10 +40,10 @@ class Node;
*/
class Event
{
public:
protected:
/** Constructor */
Event(const std::string& type);
public:
/** Destructor */
virtual ~Event();
@ -63,12 +63,6 @@ public:
*/
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:
/** Sets current target */
inline void setCurrentTarget(Node* target) { _currentTarget = target; };
@ -76,7 +70,6 @@ protected:
std::string _type; ///< Event type
bool _isStopped; ///< whether the event has been stopped.
Node* _currentTarget; ///< Current target
void* _userData; ///< User data
friend class EventDispatcher;
};

View File

@ -98,12 +98,12 @@ void EventDispatcher::addEventListenerWithItem(EventListenerItem* item)
{
std::vector<EventListenerItem*>* listenerList = nullptr;
auto iter = _listeners.find(item->listener->type);
auto iter = _listeners.find(item->listener->_type);
if (iter == _listeners.end())
{
listenerList = new std::vector<EventListenerItem*>();
listenerList->reserve(100);
_listeners.insert(std::make_pair(item->listener->type, listenerList));
_listeners.insert(std::make_pair(item->listener->_type, listenerList));
}
else
{
@ -112,7 +112,7 @@ void EventDispatcher::addEventListenerWithItem(EventListenerItem* item)
listenerList->insert(listenerList->begin(), item);
setDirtyForEventType(item->listener->type, true);
setDirtyForEventType(item->listener->_type, true);
}
else
{
@ -196,7 +196,7 @@ void EventDispatcher::removeEventListener(EventListener* listener)
iter = _listeners.erase(iter);
CC_SAFE_DELETE(list);
_priorityDirtyFlagMap.erase(listener->type);
_priorityDirtyFlagMap.erase(listener->_type);
}
else
{
@ -225,7 +225,7 @@ void EventDispatcher::setPriority(EventListener* listener, int fixedPriority)
if (item->fixedPriority != fixedPriority)
{
item->fixedPriority = fixedPriority;
setDirtyForEventType(listener->type, true);
setDirtyForEventType(listener->_type, true);
}
return;
}
@ -272,7 +272,7 @@ void EventDispatcher::dispatchEvent(Event* event, bool forceSortListeners)
CCASSERT(item, "listener item is invalid.");
event->setCurrentTarget(item->node);
item->listener->onEvent(event);
item->listener->_onEvent(event);
if (event->isStopped())
break;
@ -515,12 +515,12 @@ void EventDispatcher::updateListenerItems()
for (auto& item : _toAddedListeners)
{
auto itr = _listeners.find(item->listener->type);
auto itr = _listeners.find(item->listener->_type);
if (itr == _listeners.end())
{
listenerList = new std::vector<EventListenerItem*>();
listenerList->reserve(100);
_listeners.insert(std::make_pair(item->listener->type, listenerList));
_listeners.insert(std::make_pair(item->listener->_type, listenerList));
}
else
{
@ -529,7 +529,7 @@ void EventDispatcher::updateListenerItems()
listenerList->push_back(item);
setDirtyForEventType(item->listener->type, true);
setDirtyForEventType(item->listener->_type, true);
}
_toAddedListeners.clear();
}

View File

@ -27,20 +27,6 @@
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()
{}
@ -49,10 +35,10 @@ EventListener::~EventListener()
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;
type = t;
_onEvent = callback;
_type = t;
_isRegistered = false;
return true;
@ -60,21 +46,7 @@ bool EventListener::init(const std::string& t, std::function<void(Event*)>callba
bool EventListener::checkAvaiable()
{
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;
return (_onEvent != nullptr);
}
NS_CC_END

View File

@ -39,33 +39,10 @@ class Event;
/**
* The base class of event listener.
* 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.
* 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);
* For instance, you could refer to AccelerationEventListener, KeyboardEventListener or TouchEventListener, CustomEventListener.
*/
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:
/** Constructor */
EventListener();
@ -77,13 +54,13 @@ public:
virtual ~EventListener();
/** Checks whether the listener is available. */
virtual bool checkAvaiable();
virtual bool checkAvaiable() = 0;
/** Clones the listener, its subclasses have to override this method. */
virtual EventListener* clone();
virtual EventListener* clone() = 0;
protected:
std::function<void(Event*)> onEvent; /// Event callback function
std::string type; /// Event type
std::function<void(Event*)> _onEvent; /// Event callback function
std::string _type; /// Event type
bool _isRegistered; /// Whether the listener has been added to dispatcher.
friend class EventDispatcher;

View File

@ -37,15 +37,16 @@ class KeyboardEventListener : public EventListener
{
public:
static KeyboardEventListener* create();
/// Overrides
virtual KeyboardEventListener* clone() override;
virtual bool checkAvaiable() override;
std::function<void(KeyboardEvent::KeyCode, Event* event)> onKeyPressed;
std::function<void(KeyboardEvent::KeyCode, Event* event)> onKeyReleased;
private:
KeyboardEventListener();
bool init();
virtual bool checkAvaiable() override;
};
NS_CC_END

View File

@ -37,15 +37,17 @@ class TouchEventListener : public EventListener
{
public:
static TouchEventListener* create(Touch::DispatchMode mode);
/// Overrides
virtual TouchEventListener* clone() override;
virtual bool checkAvaiable() override;
virtual ~TouchEventListener();
private:
TouchEventListener();
bool init(Touch::DispatchMode mode);
virtual ~TouchEventListener();
virtual bool checkAvaiable() override;
public:
std::function<bool(Touch*, Event*)> onTouchBegan;
std::function<void(Touch*, Event*)> onTouchMoved;

View File

@ -268,6 +268,8 @@ THE SOFTWARE.
#include "event_dispatcher/CCKeyboardEvent.h"
#include "event_dispatcher/CCAccelerationEvent.h"
#include "event_dispatcher/CCAccelerationEventListener.h"
#include "event_dispatcher/CCCustomEvent.h"
#include "event_dispatcher/CCCustomEventListener.h"
// root
#include "CCCamera.h"

View File

@ -55,6 +55,8 @@ SOURCES = ../actions/CCAction.cpp \
../event_dispatcher/CCTouch.cpp \
../event_dispatcher/CCTouchEvent.cpp \
../event_dispatcher/CCTouchEventListener.cpp \
../event_dispatcher/CCCustomEvent.cpp \
../event_dispatcher/CCCustomEventListener.cpp \
../label_nodes/CCFont.cpp \
../label_nodes/CCFontAtlas.cpp \
../label_nodes/CCFontAtlasCache.cpp \

View File

@ -49,6 +49,8 @@ SOURCES = ../actions/CCAction.cpp \
../event_dispatcher/CCTouch.cpp \
../event_dispatcher/CCTouchEvent.cpp \
../event_dispatcher/CCTouchEventListener.cpp \
../event_dispatcher/CCCustomEvent.cpp \
../event_dispatcher/CCCustomEventListener.cpp \
../draw_nodes/CCDrawingPrimitives.cpp \
../draw_nodes/CCDrawNode.cpp \
../effects/CCGrabber.cpp \

View File

@ -44,6 +44,8 @@ SOURCES = ../actions/CCAction.cpp \
../event_dispatcher/CCTouch.cpp \
../event_dispatcher/CCTouchEvent.cpp \
../event_dispatcher/CCTouchEventListener.cpp \
../event_dispatcher/CCCustomEvent.cpp \
../event_dispatcher/CCCustomEventListener.cpp \
../label_nodes/CCLabelAtlas.cpp \
../label_nodes/CCLabelBMFont.cpp \
../label_nodes/CCLabelTTF.cpp \

View File

@ -50,6 +50,8 @@ SOURCES += ../actions/CCAction.cpp \
../event_dispatcher/CCTouch.cpp \
../event_dispatcher/CCTouchEvent.cpp \
../event_dispatcher/CCTouchEventListener.cpp \
../event_dispatcher/CCCustomEvent.cpp \
../event_dispatcher/CCCustomEventListener.cpp \
../label_nodes/CCLabelAtlas.cpp \
../label_nodes/CCLabelBMFont.cpp \
../label_nodes/CCLabelTTF.cpp \

View File

@ -31,6 +31,11 @@ CCBReader/CCParticleSystemQuadLoader.cpp \
CCBReader/CCScale9SpriteLoader.cpp \
CCBReader/CCScrollViewLoader.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/CCBone.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/Reader/CCSGUIReader.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/CCControlButton.cpp \
GUI/CCControlExtension/CCControlColourPicker.cpp \

View File

@ -398,7 +398,7 @@ void CustomEventTest::onEnter()
statusLabel->setPosition(origin + Point(size.width/2, size.height-90));
addChild(statusLabel);
_listener = EventListener::create("game_custom_event", [=](Event* event){
_listener = CustomEventListener::create("game_custom_event", [=](CustomEvent* event){
std::string str("Custom event received, ");
char* buf = static_cast<char*>(event->getUserData());
str += buf;
@ -414,7 +414,7 @@ void CustomEventTest::onEnter()
++count;
char* buf = new char[10];
sprintf(buf, "%d", count);
Event event("game_custom_event");
CustomEvent event("game_custom_event");
event.setUserData(buf);
dispatcher->dispatchEvent(&event);
});

View File

@ -63,7 +63,7 @@ public:
virtual std::string title() override;
virtual std::string subtitle() override;
private:
EventListener* _listener;
CustomEventListener* _listener;
};
class LabelKeyboardEventTest : public EventDispatcherTestDemo