[Lua] fix CCLayer:registerScriptTouchHandler()

[Lua] make CCNotificationCenter script support
This commit is contained in:
YuLei 2012-08-28 12:08:15 +08:00
parent a38b8d4a00
commit 2c518e43b2
7 changed files with 131 additions and 70 deletions

View File

@ -605,14 +605,14 @@ void CCScheduler::unscheduleAllSelectorsForTarget(CCObject *pTarget)
unsigned int CCScheduler::scheduleScriptFunc(unsigned int nHandler, float fInterval, bool bPaused) 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) if (!m_pScriptHandlerEntries)
{ {
m_pScriptHandlerEntries = CCArray::createWithCapacity(20); m_pScriptHandlerEntries = CCArray::createWithCapacity(20);
m_pScriptHandlerEntries->retain(); m_pScriptHandlerEntries->retain();
} }
m_pScriptHandlerEntries->addObject(pEntry); m_pScriptHandlerEntries->addObject(pEntry);
return pEntry->getEntryID(); return pEntry->getEntryId();
} }
void CCScheduler::unscheduleScriptEntry(unsigned int uScheduleScriptEntryID) 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--) for (int i = m_pScriptHandlerEntries->count() - 1; i >= 0; i--)
{ {
CCSchedulerScriptHandlerEntry* pEntry = static_cast<CCSchedulerScriptHandlerEntry*>(m_pScriptHandlerEntries->objectAtIndex(i)); CCSchedulerScriptHandlerEntry* pEntry = static_cast<CCSchedulerScriptHandlerEntry*>(m_pScriptHandlerEntries->objectAtIndex(i));
if (pEntry->getEntryID() == uScheduleScriptEntryID) if (pEntry->getEntryId() == uScheduleScriptEntryID)
{ {
pEntry->markedForDeletion(); pEntry->markedForDeletion();
break; break;

View File

@ -120,7 +120,7 @@ void CCLayer::registerWithTouchDispatcher()
void CCLayer::registerScriptTouchHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches) void CCLayer::registerScriptTouchHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches)
{ {
unregisterScriptTouchHandler(); unregisterScriptTouchHandler();
m_pScriptHandlerEntry = CCTouchScriptHandlerEntry::entryWithHandler(nHandler, bIsMultiTouches, nPriority, bSwallowsTouches); m_pScriptHandlerEntry = CCTouchScriptHandlerEntry::create(nHandler, bIsMultiTouches, nPriority, bSwallowsTouches);
m_pScriptHandlerEntry->retain(); m_pScriptHandlerEntry->retain();
} }

View File

@ -27,75 +27,76 @@ THE SOFTWARE.
NS_CC_BEGIN 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 -
#pragma mark CCSchedulerScriptHandlerEntry #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(); CCSchedulerScriptHandlerEntry* pEntry = new CCSchedulerScriptHandlerEntry(nHandler);
pEntry->initWithHandler(nHandler, fInterval, bPaused); pEntry->init(fInterval, bPaused);
pEntry->autorelease(); pEntry->autorelease();
return pEntry; return pEntry;
} }
bool CCSchedulerScriptHandlerEntry::initWithHandler(int nHandler, float fInterval, bool bPaused) bool CCSchedulerScriptHandlerEntry::init(float fInterval, bool bPaused)
{ {
m_pTimer = new CCTimer(); m_pTimer = new CCTimer();
m_pTimer->initWithScriptHandler(nHandler, fInterval); m_pTimer->initWithScriptHandler(m_nHandler, fInterval);
m_pTimer->autorelease(); m_pTimer->autorelease();
m_pTimer->retain(); m_pTimer->retain();
m_nHandler = nHandler;
m_bPaused = bPaused; 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; 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) CCSchedulerScriptHandlerEntry::~CCSchedulerScriptHandlerEntry(void)
{ {
m_pTimer->release(); 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 -
#pragma mark CCTouchScriptHandlerEntry #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(); CCTouchScriptHandlerEntry* pEntry = new CCTouchScriptHandlerEntry(nHandler);
pEntry->initWithHandler(nHandler, bIsMultiTouches, nPriority, bSwallowsTouches); pEntry->init(bIsMultiTouches, nPriority, bSwallowsTouches);
pEntry->autorelease(); pEntry->autorelease();
return pEntry; return pEntry;
} }
CCTouchScriptHandlerEntry::CCTouchScriptHandlerEntry(void)
: m_nHandler(0)
, m_bIsMultiTouches(false)
, m_nPriority(0)
, m_bSwallowsTouches(false)
{
}
CCTouchScriptHandlerEntry::~CCTouchScriptHandlerEntry(void) CCTouchScriptHandlerEntry::~CCTouchScriptHandlerEntry(void)
{ {
CCScriptEngineManager::sharedManager()->getScriptEngine()->removeLuaHandler(m_nHandler); CCScriptEngineManager::sharedManager()->getScriptEngine()->removeLuaHandler(m_nHandler);
LUALOG("[LUA] Remove touch event handler: %d", 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_bIsMultiTouches = bIsMultiTouches;
m_nPriority = nPriority; m_nPriority = nPriority;
m_bSwallowsTouches = bSwallowsTouches; m_bSwallowsTouches = bSwallowsTouches;

View File

@ -40,81 +40,112 @@ typedef int LUA_FUNCTION;
typedef int LUA_TABLE; typedef int LUA_TABLE;
typedef int LUA_STRING; 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; class CCTimer;
/** class CCSchedulerScriptHandlerEntry : public CCScriptHandlerEntry
* @addtogroup script_support
* @{
*/
// Lua support for CCScheduler
class CCSchedulerScriptHandlerEntry : public CCObject
{ {
public: public:
// nHandler return by tolua_ref_function(), called from LuaCocos2d.cpp // 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); ~CCSchedulerScriptHandlerEntry(void);
inline cocos2d::CCTimer* getTimer(void) { cocos2d::CCTimer* getTimer(void) {
return m_pTimer; return m_pTimer;
} }
inline bool isPaused(void) { bool isPaused(void) {
return m_bPaused; return m_bPaused;
} }
inline int getEntryID(void) { void markedForDeletion(void) {
return m_nEntryID;
}
inline void markedForDeletion(void) {
m_bMarkedForDeletion = true; m_bMarkedForDeletion = true;
} }
inline bool isMarkedForDeletion(void) { bool isMarkedForDeletion(void) {
return m_bMarkedForDeletion; return m_bMarkedForDeletion;
} }
private: private:
CCSchedulerScriptHandlerEntry(void); CCSchedulerScriptHandlerEntry(int nHandler)
bool initWithHandler(int nHandler, float fInterval, bool bPaused); : CCScriptHandlerEntry(nHandler)
, m_pTimer(NULL)
, m_bPaused(false)
, m_bMarkedForDeletion(false)
{
}
bool init(float fInterval, bool bPaused);
cocos2d::CCTimer* m_pTimer; cocos2d::CCTimer* m_pTimer;
bool m_bPaused; bool m_bPaused;
bool m_bMarkedForDeletion; bool m_bMarkedForDeletion;
int m_nHandler;
int m_nEntryID;
}; };
// Lua support for touch events #pragma mark -
class CCTouchScriptHandlerEntry : public CCObject #pragma mark CCTouchScriptHandlerEntry
class CCTouchScriptHandlerEntry : public CCScriptHandlerEntry
{ {
public: 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); ~CCTouchScriptHandlerEntry(void);
inline int getHandler(void) { bool isMultiTouches(void) {
return m_nHandler;
}
inline bool isMultiTouches(void) {
return m_bIsMultiTouches; return m_bIsMultiTouches;
} }
inline int getPriority(void) { int getPriority(void) {
return m_nPriority; return m_nPriority;
} }
inline bool getSwallowsTouches(void) { bool getSwallowsTouches(void) {
return m_bSwallowsTouches; return m_bSwallowsTouches;
} }
private: private:
CCTouchScriptHandlerEntry(void); CCTouchScriptHandlerEntry(int nHandler)
bool initWithHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches); : 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; bool m_bIsMultiTouches;
int m_nPriority; int m_nPriority;
bool m_bSwallowsTouches; bool m_bSwallowsTouches;

View File

@ -24,6 +24,7 @@ THE SOFTWARE.
#include "CCNotificationCenter.h" #include "CCNotificationCenter.h"
#include "cocoa/CCArray.h" #include "cocoa/CCArray.h"
#include "script_support/CCScriptSupport.h"
#include <string> #include <string>
using namespace std; using namespace std;
@ -33,6 +34,7 @@ NS_CC_BEGIN;
static CCNotificationCenter *s_sharedNotifCenter = NULL; static CCNotificationCenter *s_sharedNotifCenter = NULL;
CCNotificationCenter::CCNotificationCenter() CCNotificationCenter::CCNotificationCenter()
: m_scriptHandler(0)
{ {
m_observers = CCArray::createWithCapacity(3); m_observers = CCArray::createWithCapacity(3);
m_observers->retain(); m_observers->retain();
@ -40,6 +42,7 @@ CCNotificationCenter::CCNotificationCenter()
CCNotificationCenter::~CCNotificationCenter() CCNotificationCenter::~CCNotificationCenter()
{ {
unregisterScriptObserver();
m_observers->release(); 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) void CCNotificationCenter::postNotification(const char *name, CCObject *object)
{ {
CCObject* obj = NULL; CCObject* obj = NULL;
@ -123,6 +141,13 @@ void CCNotificationCenter::postNotification(const char *name, CCObject *object)
if (!strcmp(name,observer->getName())) if (!strcmp(name,observer->getName()))
observer->performSelector(object); 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) void CCNotificationCenter::postNotification(const char *name)

View File

@ -46,6 +46,9 @@ public:
void removeObserver(CCObject *target,const char *name); void removeObserver(CCObject *target,const char *name);
void registerScriptObserver(int handler);
void unregisterScriptObserver(void);
void postNotification(const char *name); void postNotification(const char *name);
void postNotification(const char *name, CCObject *object); void postNotification(const char *name, CCObject *object);
@ -59,6 +62,7 @@ private:
// variables // variables
// //
CCArray *m_observers; CCArray *m_observers;
int m_scriptHandler;
}; };
class CC_DLL CCNotificationObserver : public CCObject class CC_DLL CCNotificationObserver : public CCObject

View File

@ -1 +1 @@
e11b5e2e3cc2bd734b167967c243e16e688b8ca6 1e35e173502d72efb4912d2ef6688d25618f881d