issue #3069: Using `std::string` to initialize `EventCustom`. It will be easier for editor parser to emit callback event.

This commit is contained in:
James Chen 2013-10-28 10:49:21 +08:00
parent 48ce5e39fe
commit 0622434321
4 changed files with 10 additions and 9 deletions

View File

@ -24,14 +24,15 @@
#include "CCEventCustom.h" #include "CCEventCustom.h"
#include "ccMacros.h" #include "ccMacros.h"
#include <functional>
NS_CC_BEGIN NS_CC_BEGIN
EventCustom::EventCustom(Type type) EventCustom::EventCustom(const std::string& eventName)
: Event(type) : Event(std::hash<std::string>()(eventName))
, _userData(nullptr) , _userData(nullptr)
{ {
CCASSERT(type >= TYPE_CUSTOM, "custom type should be greater than TYPE_CUSTOM."); // CCASSERT(type >= TYPE_CUSTOM, "custom type should be greater than TYPE_CUSTOM.");
} }
NS_CC_END NS_CC_END

View File

@ -33,7 +33,7 @@ class EventCustom : public Event
{ {
public: public:
/** Constructor */ /** Constructor */
EventCustom(Type type); EventCustom(const std::string& eventName);
/** Set user data */ /** Set user data */
inline void setUserData(void* data) { _userData = data; }; inline void setUserData(void* data) { _userData = data; };

View File

@ -32,10 +32,10 @@ EventListenerCustom::EventListenerCustom()
{ {
} }
EventListenerCustom* EventListenerCustom::create(Type type, std::function<void(EventCustom*)> callback) EventListenerCustom* EventListenerCustom::create(const std::string& eventName, std::function<void(EventCustom*)> callback)
{ {
EventListenerCustom* ret = new EventListenerCustom(); EventListenerCustom* ret = new EventListenerCustom();
if (ret && ret->init(type, callback)) if (ret && ret->init(std::hash<std::string>()(eventName), callback))
{ {
ret->autorelease(); ret->autorelease();
} }
@ -46,7 +46,7 @@ EventListenerCustom* EventListenerCustom::create(Type type, std::function<void(E
return ret; return ret;
} }
bool EventListenerCustom::init(int type, std::function<void(EventCustom*)>callback) bool EventListenerCustom::init(Type type, std::function<void(EventCustom*)>callback)
{ {
bool ret = false; bool ret = false;

View File

@ -56,7 +56,7 @@ public:
* @param eventType The type of the event. * @param eventType The type of the event.
* @param callback The callback function when the specified event was emitted. * @param callback The callback function when the specified event was emitted.
*/ */
static EventListenerCustom* create(int type, std::function<void(EventCustom*)> callback); static EventListenerCustom* create(const std::string& eventName, std::function<void(EventCustom*)> callback);
/// Overrides /// Overrides
virtual bool checkAvailable() override; virtual bool checkAvailable() override;
@ -67,7 +67,7 @@ protected:
EventListenerCustom(); EventListenerCustom();
/** Initializes event with type and callback function */ /** Initializes event with type and callback function */
bool init(int type, std::function<void(EventCustom*)> callback); bool init(Type type, std::function<void(EventCustom*)> callback);
std::function<void(EventCustom*)> _onCustomEvent; std::function<void(EventCustom*)> _onCustomEvent;
}; };