diff --git a/cocos2dx/CCScheduler.cpp b/cocos2dx/CCScheduler.cpp index b3af500a14..aa50cfeb23 100644 --- a/cocos2dx/CCScheduler.cpp +++ b/cocos2dx/CCScheduler.cpp @@ -605,14 +605,14 @@ void CCScheduler::unscheduleAllSelectorsForTarget(CCObject *pTarget) unsigned int CCScheduler::scheduleScriptFunc(unsigned int nHandler, float fInterval, bool bPaused) { - CCSchedulerScriptHandlerEntry* pEntry = CCSchedulerScriptHandlerEntry::entryWithHandler(nHandler, fInterval, bPaused); + CCSchedulerScriptHandlerEntry* pEntry = CCSchedulerScriptHandlerEntry::create(nHandler, fInterval, bPaused); if (!m_pScriptHandlerEntries) { m_pScriptHandlerEntries = CCArray::createWithCapacity(20); m_pScriptHandlerEntries->retain(); } m_pScriptHandlerEntries->addObject(pEntry); - return pEntry->getEntryID(); + return pEntry->getEntryId(); } void CCScheduler::unscheduleScriptEntry(unsigned int uScheduleScriptEntryID) @@ -620,7 +620,7 @@ void CCScheduler::unscheduleScriptEntry(unsigned int uScheduleScriptEntryID) for (int i = m_pScriptHandlerEntries->count() - 1; i >= 0; i--) { CCSchedulerScriptHandlerEntry* pEntry = static_cast(m_pScriptHandlerEntries->objectAtIndex(i)); - if (pEntry->getEntryID() == uScheduleScriptEntryID) + if (pEntry->getEntryId() == uScheduleScriptEntryID) { pEntry->markedForDeletion(); break; diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index 63f6f22be7..0fa0a01986 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -120,7 +120,7 @@ void CCLayer::registerWithTouchDispatcher() void CCLayer::registerScriptTouchHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches) { unregisterScriptTouchHandler(); - m_pScriptHandlerEntry = CCTouchScriptHandlerEntry::entryWithHandler(nHandler, bIsMultiTouches, nPriority, bSwallowsTouches); + m_pScriptHandlerEntry = CCTouchScriptHandlerEntry::create(nHandler, bIsMultiTouches, nPriority, bSwallowsTouches); m_pScriptHandlerEntry->retain(); } diff --git a/cocos2dx/script_support/CCScriptSupport.cpp b/cocos2dx/script_support/CCScriptSupport.cpp index fe3b2e7fc9..557154a4b2 100644 --- a/cocos2dx/script_support/CCScriptSupport.cpp +++ b/cocos2dx/script_support/CCScriptSupport.cpp @@ -27,75 +27,76 @@ THE SOFTWARE. NS_CC_BEGIN +#pragma mark - +#pragma mark CCScriptHandlerEntry + +CCScriptHandlerEntry* CCScriptHandlerEntry::create(int nHandler) +{ + CCScriptHandlerEntry* entry = new CCScriptHandlerEntry(nHandler); + entry->autorelease(); + return entry; +} + +CCScriptHandlerEntry::~CCScriptHandlerEntry(void) +{ + CCScriptEngineManager::sharedManager()->getScriptEngine()->removeLuaHandler(m_nHandler); +} + +#pragma mark - +#pragma mark CCNotificationObserverHandlerEntry + + #pragma mark - #pragma mark CCSchedulerScriptHandlerEntry -CCSchedulerScriptHandlerEntry* CCSchedulerScriptHandlerEntry::entryWithHandler(int nHandler, float fInterval, bool bPaused) +CCSchedulerScriptHandlerEntry* CCSchedulerScriptHandlerEntry::create(int nHandler, float fInterval, bool bPaused) { - CCSchedulerScriptHandlerEntry* pEntry = new CCSchedulerScriptHandlerEntry(); - pEntry->initWithHandler(nHandler, fInterval, bPaused); + CCSchedulerScriptHandlerEntry* pEntry = new CCSchedulerScriptHandlerEntry(nHandler); + pEntry->init(fInterval, bPaused); pEntry->autorelease(); return pEntry; } -bool CCSchedulerScriptHandlerEntry::initWithHandler(int nHandler, float fInterval, bool bPaused) +bool CCSchedulerScriptHandlerEntry::init(float fInterval, bool bPaused) { m_pTimer = new CCTimer(); - m_pTimer->initWithScriptHandler(nHandler, fInterval); + m_pTimer->initWithScriptHandler(m_nHandler, fInterval); m_pTimer->autorelease(); m_pTimer->retain(); - m_nHandler = nHandler; m_bPaused = bPaused; - LUALOG("[LUA] ADD script schedule: %d, entryID: %d", m_nHandler, m_nEntryID); + LUALOG("[LUA] ADD script schedule: %d, entryID: %d", m_nHandler, m_nEntryId); return true; } -CCSchedulerScriptHandlerEntry::CCSchedulerScriptHandlerEntry(void) -: m_pTimer(NULL) -, m_nHandler(0) -, m_bPaused(true) -, m_bMarkedForDeletion(false) -{ - static int nEntryCount = 0; - m_nEntryID = ++nEntryCount; -} - CCSchedulerScriptHandlerEntry::~CCSchedulerScriptHandlerEntry(void) { m_pTimer->release(); - CCScriptEngineManager::sharedManager()->getScriptEngine()->removeLuaHandler(m_nHandler); - LUALOG("[LUA] DEL script schedule %d, entryID: %d", m_nHandler, m_nEntryID); + LUALOG("[LUA] DEL script schedule %d, entryID: %d", m_nHandler, m_nEntryId); } #pragma mark - #pragma mark CCTouchScriptHandlerEntry -CCTouchScriptHandlerEntry* CCTouchScriptHandlerEntry::entryWithHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches) +CCTouchScriptHandlerEntry* CCTouchScriptHandlerEntry::create(int nHandler, + bool bIsMultiTouches, + int nPriority, + bool bSwallowsTouches) { - CCTouchScriptHandlerEntry* pEntry = new CCTouchScriptHandlerEntry(); - pEntry->initWithHandler(nHandler, bIsMultiTouches, nPriority, bSwallowsTouches); + CCTouchScriptHandlerEntry* pEntry = new CCTouchScriptHandlerEntry(nHandler); + pEntry->init(bIsMultiTouches, nPriority, bSwallowsTouches); pEntry->autorelease(); return pEntry; } -CCTouchScriptHandlerEntry::CCTouchScriptHandlerEntry(void) -: m_nHandler(0) -, m_bIsMultiTouches(false) -, m_nPriority(0) -, m_bSwallowsTouches(false) -{ -} - CCTouchScriptHandlerEntry::~CCTouchScriptHandlerEntry(void) { CCScriptEngineManager::sharedManager()->getScriptEngine()->removeLuaHandler(m_nHandler); LUALOG("[LUA] Remove touch event handler: %d", m_nHandler); } -bool CCTouchScriptHandlerEntry::initWithHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches) +bool CCTouchScriptHandlerEntry::init(bool bIsMultiTouches, int nPriority, bool bSwallowsTouches) { - m_nHandler = nHandler; m_bIsMultiTouches = bIsMultiTouches; m_nPriority = nPriority; m_bSwallowsTouches = bSwallowsTouches; diff --git a/cocos2dx/script_support/CCScriptSupport.h b/cocos2dx/script_support/CCScriptSupport.h index 845886a503..9c7bfb22d2 100644 --- a/cocos2dx/script_support/CCScriptSupport.h +++ b/cocos2dx/script_support/CCScriptSupport.h @@ -40,81 +40,112 @@ typedef int LUA_FUNCTION; typedef int LUA_TABLE; typedef int LUA_STRING; +#pragma mark - +#pragma mark CCScriptHandlerEntry + +class CCScriptHandlerEntry : public CCObject +{ +public: + static CCScriptHandlerEntry* create(int nHandler); + ~CCScriptHandlerEntry(void); + + int getHandler(void) { + return m_nHandler; + } + + int getEntryId(void) { + return m_nEntryId; + } + +protected: + CCScriptHandlerEntry(int nHandler) + : m_nHandler(nHandler) + { + static int newEntryId = 0; + newEntryId++; + m_nEntryId = newEntryId; + } + + int m_nHandler; + int m_nEntryId; +}; + + +#pragma mark - +#pragma mark CCSchedulerScriptHandlerEntry + class CCTimer; -/** - * @addtogroup script_support - * @{ - */ - -// Lua support for CCScheduler -class CCSchedulerScriptHandlerEntry : public CCObject +class CCSchedulerScriptHandlerEntry : public CCScriptHandlerEntry { public: // nHandler return by tolua_ref_function(), called from LuaCocos2d.cpp - static CCSchedulerScriptHandlerEntry* entryWithHandler(int nHandler, float fInterval, bool bPaused); + static CCSchedulerScriptHandlerEntry* create(int nHandler, float fInterval, bool bPaused); ~CCSchedulerScriptHandlerEntry(void); - inline cocos2d::CCTimer* getTimer(void) { + cocos2d::CCTimer* getTimer(void) { return m_pTimer; } - inline bool isPaused(void) { + bool isPaused(void) { return m_bPaused; } - inline int getEntryID(void) { - return m_nEntryID; - } - - inline void markedForDeletion(void) { + void markedForDeletion(void) { m_bMarkedForDeletion = true; } - inline bool isMarkedForDeletion(void) { + bool isMarkedForDeletion(void) { return m_bMarkedForDeletion; } private: - CCSchedulerScriptHandlerEntry(void); - bool initWithHandler(int nHandler, float fInterval, bool bPaused); + CCSchedulerScriptHandlerEntry(int nHandler) + : CCScriptHandlerEntry(nHandler) + , m_pTimer(NULL) + , m_bPaused(false) + , m_bMarkedForDeletion(false) + { + } + bool init(float fInterval, bool bPaused); cocos2d::CCTimer* m_pTimer; bool m_bPaused; bool m_bMarkedForDeletion; - int m_nHandler; - int m_nEntryID; }; -// Lua support for touch events -class CCTouchScriptHandlerEntry : public CCObject +#pragma mark - +#pragma mark CCTouchScriptHandlerEntry + +class CCTouchScriptHandlerEntry : public CCScriptHandlerEntry { public: - static CCTouchScriptHandlerEntry* entryWithHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches); + static CCTouchScriptHandlerEntry* create(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches); ~CCTouchScriptHandlerEntry(void); - inline int getHandler(void) { - return m_nHandler; - } - - inline bool isMultiTouches(void) { + bool isMultiTouches(void) { return m_bIsMultiTouches; } - inline int getPriority(void) { + int getPriority(void) { return m_nPriority; } - inline bool getSwallowsTouches(void) { + bool getSwallowsTouches(void) { return m_bSwallowsTouches; } private: - CCTouchScriptHandlerEntry(void); - bool initWithHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches); + CCTouchScriptHandlerEntry(int nHandler) + : CCScriptHandlerEntry(nHandler) + , m_bIsMultiTouches(false) + , m_nPriority(0) + , m_bSwallowsTouches(false) + { + } + bool init(bool bIsMultiTouches, int nPriority, bool bSwallowsTouches); - int m_nHandler; bool m_bIsMultiTouches; int m_nPriority; bool m_bSwallowsTouches; diff --git a/cocos2dx/support/CCNotificationCenter.cpp b/cocos2dx/support/CCNotificationCenter.cpp index a480e01ebb..10d315982f 100644 --- a/cocos2dx/support/CCNotificationCenter.cpp +++ b/cocos2dx/support/CCNotificationCenter.cpp @@ -24,6 +24,7 @@ THE SOFTWARE. #include "CCNotificationCenter.h" #include "cocoa/CCArray.h" +#include "script_support/CCScriptSupport.h" #include using namespace std; @@ -33,6 +34,7 @@ NS_CC_BEGIN; static CCNotificationCenter *s_sharedNotifCenter = NULL; CCNotificationCenter::CCNotificationCenter() +: m_scriptHandler(0) { m_observers = CCArray::createWithCapacity(3); m_observers->retain(); @@ -40,6 +42,7 @@ CCNotificationCenter::CCNotificationCenter() CCNotificationCenter::~CCNotificationCenter() { + unregisterScriptObserver(); m_observers->release(); } @@ -111,6 +114,21 @@ void CCNotificationCenter::removeObserver(CCObject *target,const char *name) } } +void CCNotificationCenter::registerScriptObserver(int handler) +{ + unregisterScriptObserver(); + m_scriptHandler = handler; +} + +void CCNotificationCenter::unregisterScriptObserver(void) +{ + if (m_scriptHandler) + { + CCScriptEngineManager::sharedManager()->getScriptEngine()->removeLuaHandler(m_scriptHandler); + } + m_scriptHandler = 0; +} + void CCNotificationCenter::postNotification(const char *name, CCObject *object) { CCObject* obj = NULL; @@ -123,6 +141,13 @@ void CCNotificationCenter::postNotification(const char *name, CCObject *object) if (!strcmp(name,observer->getName())) observer->performSelector(object); } + + if (m_scriptHandler) + { + CCScriptEngineProtocol* engine = CCScriptEngineManager::sharedManager()->getScriptEngine(); + engine->pushString(name); + engine->executeFunction(m_scriptHandler, 1); + } } void CCNotificationCenter::postNotification(const char *name) diff --git a/cocos2dx/support/CCNotificationCenter.h b/cocos2dx/support/CCNotificationCenter.h index 70c94a9d4d..e15d83e18b 100644 --- a/cocos2dx/support/CCNotificationCenter.h +++ b/cocos2dx/support/CCNotificationCenter.h @@ -46,6 +46,9 @@ public: void removeObserver(CCObject *target,const char *name); + void registerScriptObserver(int handler); + void unregisterScriptObserver(void); + void postNotification(const char *name); void postNotification(const char *name, CCObject *object); @@ -59,6 +62,7 @@ private: // variables // CCArray *m_observers; + int m_scriptHandler; }; class CC_DLL CCNotificationObserver : public CCObject diff --git a/scripting/lua/cocos2dx_support/LuaCocos2d.cpp.REMOVED.git-id b/scripting/lua/cocos2dx_support/LuaCocos2d.cpp.REMOVED.git-id index 80e1ddc66b..a202505b80 100644 --- a/scripting/lua/cocos2dx_support/LuaCocos2d.cpp.REMOVED.git-id +++ b/scripting/lua/cocos2dx_support/LuaCocos2d.cpp.REMOVED.git-id @@ -1 +1 @@ -e11b5e2e3cc2bd734b167967c243e16e688b8ca6 \ No newline at end of file +1e35e173502d72efb4912d2ef6688d25618f881d \ No newline at end of file