[dispatcher] Using cocos2d-x memory model instead of std::shared_ptr. It should be unified.

This commit is contained in:
James Chen 2013-09-13 18:00:56 +08:00
parent 51548b9542
commit 2fd655dc57
10 changed files with 216 additions and 70 deletions

View File

@ -28,12 +28,8 @@
NS_CC_BEGIN
AccelerationEventListener::AccelerationEventListener()
: EventListener(AccelerationEvent::EVENT_TYPE, nullptr)
{
onEvent = [this](Event* event){
auto accEvent = static_cast<AccelerationEvent*>(event);
this->onAccelerationEvent(&accEvent->acc, event);
};
}
AccelerationEventListener::~AccelerationEventListener()
@ -41,11 +37,58 @@ AccelerationEventListener::~AccelerationEventListener()
CCLOGINFO("In the destructor of AccelerationEventListener. %p", this);
}
std::shared_ptr<AccelerationEventListener> AccelerationEventListener::create(std::function<void(Acceleration*, Event* event)> callback)
AccelerationEventListener* AccelerationEventListener::create(std::function<void(Acceleration*, Event* event)> callback)
{
std::shared_ptr<AccelerationEventListener> ret(new AccelerationEventListener());
ret->onAccelerationEvent = callback;
AccelerationEventListener* ret = new AccelerationEventListener();
if (ret && ret->init(callback))
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
bool AccelerationEventListener::init(std::function<void(Acceleration*, Event* event)> callback)
{
auto listener = [this](Event* event){
auto accEvent = static_cast<AccelerationEvent*>(event);
this->onAccelerationEvent(&accEvent->acc, event);
};
if (EventListener::init(AccelerationEvent::EVENT_TYPE, listener))
{
onAccelerationEvent = callback;
return true;
}
return false;
}
AccelerationEventListener* AccelerationEventListener::clone()
{
auto ret = new AccelerationEventListener();
if (ret && ret->init(onAccelerationEvent))
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
bool AccelerationEventListener::checkAvaiable()
{
CCASSERT(onAccelerationEvent, "");
return true;
}
NS_CC_END

View File

@ -33,11 +33,15 @@ NS_CC_BEGIN
class AccelerationEventListener : public EventListener
{
public:
static std::shared_ptr<AccelerationEventListener> create(std::function<void(Acceleration*, Event* event)> callback);
static AccelerationEventListener* create(std::function<void(Acceleration*, Event* event)> callback);
virtual AccelerationEventListener* clone() override;
~AccelerationEventListener();
private:
AccelerationEventListener();
virtual bool checkAvaiable() override;
bool init(std::function<void(Acceleration*, Event* event)> callback);
std::function<void(Acceleration*, Event*)> onAccelerationEvent;
};

View File

@ -94,9 +94,9 @@ void EventDispatcher::registerEventListenerWithItem(EventListenerItem* item)
listenerList->push_front(item);
}
int EventDispatcher::registerEventListenerWithSceneGraphPriority(std::shared_ptr<EventListener> listener, Node* node)
int EventDispatcher::registerEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)
{
CCASSERT(listener->_isRegistered, "The listener has been registered.");
CCASSERT(!listener->_isRegistered, "The listener has been registered.");
if (!listener->checkAvaiable())
return 0;
@ -106,6 +106,7 @@ int EventDispatcher::registerEventListenerWithSceneGraphPriority(std::shared_ptr
item->node = node;
item->fixedPriority = 0;
item->listener = listener;
item->listener->retain();
item->listener->_isRegistered = true;
registerEventListenerWithItem(item);
@ -116,9 +117,9 @@ int EventDispatcher::registerEventListenerWithSceneGraphPriority(std::shared_ptr
return item->id;
}
int EventDispatcher::registerEventListenerWithFixedPriority(std::shared_ptr<EventListener> listener, int fixedPriority)
int EventDispatcher::registerEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)
{
CCASSERT(listener->_isRegistered, "The listener has been registered.");
CCASSERT(!listener->_isRegistered, "The listener has been registered.");
if (!listener->checkAvaiable())
return 0;
@ -128,6 +129,7 @@ int EventDispatcher::registerEventListenerWithFixedPriority(std::shared_ptr<Even
item->node = nullptr;
item->fixedPriority = fixedPriority;
item->listener = listener;
item->listener->retain();
item->listener->_isRegistered = true;
registerEventListenerWithItem(item);
@ -150,6 +152,7 @@ void EventDispatcher::unregisterEventListener(int listenerId)
if (_inDispatch == 0)
{
(*itemIter)->listener->release();
delete (*itemIter);
iter->second->remove(*itemIter);
}
@ -268,7 +271,7 @@ void EventDispatcher::dispatchTouchEvent(TouchEvent* event)
for (auto iter = touchListeners->begin(); iter != touchListeners->end(); ++iter)
{
auto touchEventListener = std::static_pointer_cast<TouchEventListener>((*iter)->listener);
TouchEventListener* touchEventListener = static_cast<TouchEventListener*>((*iter)->listener);
if (touchEventListener->_dispatchMode == Touch::DispatchMode::ONE_BY_ONE)
{
@ -309,12 +312,11 @@ void EventDispatcher::dispatchTouchEvent(TouchEvent* event)
continue;
event->setCurrentTarget((*oneByOneIter)->node);
CCLOG("touch target : %p, %s", (*oneByOneIter)->node, typeid(*(*oneByOneIter)->node).name());
bool isClaimed = false;
std::vector<Touch*>::iterator removedIter;
auto touchEventListener = std::static_pointer_cast<TouchEventListener>((*oneByOneIter)->listener);
auto touchEventListener = static_cast<TouchEventListener*>((*oneByOneIter)->listener);
TouchEvent::EventCode eventCode = event->getEventCode();
if (eventCode == TouchEvent::EventCode::BEGAN)
@ -399,7 +401,7 @@ void EventDispatcher::dispatchTouchEvent(TouchEvent* event)
event->setCurrentTarget((*allInOneIter)->node);
auto touchEventListener = std::static_pointer_cast<TouchEventListener>((*allInOneIter)->listener);
auto touchEventListener = static_cast<TouchEventListener*>((*allInOneIter)->listener);
switch (event->getEventCode())
{
@ -541,6 +543,7 @@ void EventDispatcher::removeListenersForEventType(const std::string& eventType)
{
for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end(); ++iter)
{
(*iter)->listener->release();
delete (*iter);
}
@ -561,6 +564,7 @@ void EventDispatcher::removeAllListeners()
{
for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end(); ++iter)
{
(*iter)->listener->release();
delete (*iter);
}

View File

@ -57,12 +57,12 @@ public:
/** Registers a callback function for an specified event with the priority of scene graph.
* @return The unique ID for the listener.
*/
int registerEventListenerWithSceneGraphPriority(std::shared_ptr<EventListener> listener, Node* node);
int registerEventListenerWithSceneGraphPriority(EventListener* listener, Node* node);
/** Registers a callback function for an specified event with the fixed priority.
* @return The unique ID for the listener.
*/
int registerEventListenerWithFixedPriority(std::shared_ptr<EventListener> listener, int fixedPriority);
int registerEventListenerWithFixedPriority(EventListener* listener, int fixedPriority);
/** Unregisters a callback function by the unique ID. */
void unregisterEventListener(int listenerId);
@ -98,10 +98,10 @@ public:
private:
struct EventListenerItem
{
int id;
int fixedPriority; // The higher the number, the higher the priority
Node* node; // Weak reference.
std::shared_ptr<EventListener> listener;
int id;
int fixedPriority; // The higher the number, the higher the priority
Node* node; // Weak reference.
EventListener* listener;
};
/** Constructor of EventDispatcher */

View File

@ -27,16 +27,21 @@
NS_CC_BEGIN
std::shared_ptr<EventListener> EventListener::create(const std::string& eventType, std::function<void(Event*)> callback)
EventListener* EventListener::create(const std::string& eventType, std::function<void(Event*)> callback)
{
std::shared_ptr<EventListener> ret(new EventListener(eventType, callback));
EventListener* ret = new EventListener();
if (ret && ret->init(eventType, callback))
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
EventListener::EventListener(const std::string& t, std::function<void(Event*)>callback)
: onEvent(callback)
, type(t)
, _isRegistered(false)
EventListener::EventListener()
{}
EventListener::~EventListener()
@ -44,14 +49,31 @@ EventListener::~EventListener()
CCLOGINFO("In the destructor of EventListener. %p", this);
}
bool EventListener::init(const std::string& t, std::function<void(Event*)>callback)
{
onEvent = callback;
type = t;
_isRegistered = false;
return true;
}
bool EventListener::checkAvaiable()
{
return (onEvent != nullptr);
}
std::shared_ptr<EventListener> EventListener::clone()
EventListener* EventListener::clone()
{
std::shared_ptr<EventListener> ret(new EventListener(type, onEvent));
EventListener* ret = new EventListener();
if (ret && ret->init(type, onEvent))
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}

View File

@ -26,6 +26,7 @@
#define cocos2d_libs_EventListener_h
#include "platform/CCPlatformMacros.h"
#include "cocoa/CCObject.h"
#include <functional>
#include <string>
@ -35,18 +36,18 @@ NS_CC_BEGIN
class Event;
class EventListener
class EventListener : public Object
{
public:
static std::shared_ptr<EventListener> create(const std::string& eventType, std::function<void(Event*)> callback);
static EventListener* create(const std::string& eventType, std::function<void(Event*)> callback);
protected:
EventListener(const std::string& t, std::function<void(Event*)>callback);
EventListener();
bool init(const std::string& t, std::function<void(Event*)>callback);
public:
virtual ~EventListener();
virtual bool checkAvaiable();
virtual std::shared_ptr<EventListener> clone();
virtual EventListener* clone();
protected:
std::function<void(Event*)> onEvent;
std::string type;

View File

@ -36,28 +36,64 @@ bool KeyboardEventListener::checkAvaiable()
return true;
}
std::shared_ptr<KeyboardEventListener> KeyboardEventListener::create()
KeyboardEventListener* KeyboardEventListener::create()
{
std::shared_ptr<KeyboardEventListener> ret(new KeyboardEventListener());
auto ret = new KeyboardEventListener();
if (ret && ret->init())
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
KeyboardEventListener* KeyboardEventListener::clone()
{
auto ret = new KeyboardEventListener();
if (ret && ret->init())
{
ret->autorelease();
ret->onKeyPressed = onKeyPressed;
ret->onKeyReleased = onKeyReleased;
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
KeyboardEventListener::KeyboardEventListener()
: EventListener(KeyboardEvent::EVENT_TYPE, nullptr)
, onKeyPressed(nullptr)
: onKeyPressed(nullptr)
, onKeyReleased(nullptr)
{
onEvent = [this](Event* event){
}
bool KeyboardEventListener::init()
{
auto listener = [this](Event* event){
auto keyboardEvent = static_cast<KeyboardEvent*>(event);
if (keyboardEvent->_isPressed)
{
onKeyPressed(keyboardEvent->_keyCode, event);
if (onKeyPressed != nullptr)
onKeyPressed(keyboardEvent->_keyCode, event);
}
else
{
onKeyReleased(keyboardEvent->_keyCode, event);
if (onKeyReleased != nullptr)
onKeyReleased(keyboardEvent->_keyCode, event);
}
};
if (EventListener::init(KeyboardEvent::EVENT_TYPE, listener))
{
return true;
}
return false;
}
NS_CC_END

View File

@ -36,12 +36,15 @@ class Event;
class KeyboardEventListener : public EventListener
{
public:
static std::shared_ptr<KeyboardEventListener> create();
static KeyboardEventListener* create();
virtual KeyboardEventListener* clone() 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;
};

View File

@ -30,9 +30,8 @@
NS_CC_BEGIN
TouchEventListener::TouchEventListener(Touch::DispatchMode mode)
: EventListener(TouchEvent::EVENT_TYPE, nullptr)
, onTouchBegan(nullptr)
TouchEventListener::TouchEventListener()
: onTouchBegan(nullptr)
, onTouchMoved(nullptr)
, onTouchEnded(nullptr)
, onTouchCancelled(nullptr)
@ -41,19 +40,43 @@ TouchEventListener::TouchEventListener(Touch::DispatchMode mode)
, onTouchesEnded(nullptr)
, onTouchesCancelled(nullptr)
, _needSwallow(false)
, _dispatchMode(mode)
, _dispatchMode(Touch::DispatchMode::ALL_AT_ONCE)
{
}
TouchEventListener::~TouchEventListener()
{
CCLOGINFO("In the destructor of TouchEventListener, %p", this);
}
bool TouchEventListener::init(Touch::DispatchMode mode)
{
if (EventListener::init(TouchEvent::EVENT_TYPE, nullptr))
{
_dispatchMode = mode;
return true;
}
return false;
}
void TouchEventListener::setSwallowTouches(bool needSwallow)
{
CCASSERT(_dispatchMode == Touch::DispatchMode::ONE_BY_ONE, "Swallow touches only available in OneByOne mode.");
_needSwallow = needSwallow;
}
std::shared_ptr<TouchEventListener> TouchEventListener::create(Touch::DispatchMode mode)
TouchEventListener* TouchEventListener::create(Touch::DispatchMode mode)
{
std::shared_ptr<TouchEventListener> ret(new TouchEventListener(mode));
auto ret = new TouchEventListener();
if (ret && ret->init(mode))
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
@ -85,23 +108,30 @@ bool TouchEventListener::checkAvaiable()
return true;
}
std::shared_ptr<EventListener> TouchEventListener::clone()
TouchEventListener* TouchEventListener::clone()
{
std::shared_ptr<TouchEventListener> ret(new TouchEventListener(_dispatchMode));
ret->onTouchBegan = onTouchBegan;
ret->onTouchMoved = onTouchMoved;
ret->onTouchEnded = onTouchEnded;
ret->onTouchCancelled = onTouchCancelled;
ret->onTouchesBegan = onTouchesBegan;
ret->onTouchesMoved = onTouchesMoved;
ret->onTouchesEnded = onTouchesEnded;
ret->onTouchesCancelled = onTouchesCancelled;
ret->_claimedTouches = _claimedTouches;
ret->_dispatchMode = _dispatchMode;
ret->_needSwallow = _needSwallow;
auto ret = new TouchEventListener();
if (ret && ret->init(_dispatchMode))
{
ret->autorelease();
ret->onTouchBegan = onTouchBegan;
ret->onTouchMoved = onTouchMoved;
ret->onTouchEnded = onTouchEnded;
ret->onTouchCancelled = onTouchCancelled;
ret->onTouchesBegan = onTouchesBegan;
ret->onTouchesMoved = onTouchesMoved;
ret->onTouchesEnded = onTouchesEnded;
ret->onTouchesCancelled = onTouchesCancelled;
ret->_claimedTouches = _claimedTouches;
ret->_dispatchMode = _dispatchMode;
ret->_needSwallow = _needSwallow;
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}

View File

@ -36,11 +36,14 @@ NS_CC_BEGIN
class TouchEventListener : public EventListener
{
public:
static std::shared_ptr<TouchEventListener> create(Touch::DispatchMode mode);
virtual std::shared_ptr<EventListener> clone() override;
static TouchEventListener* create(Touch::DispatchMode mode);
virtual TouchEventListener* clone() override;
private:
TouchEventListener(Touch::DispatchMode mode);
TouchEventListener();
bool init(Touch::DispatchMode mode);
virtual ~TouchEventListener();
virtual bool checkAvaiable() override;
public: