Merge pull request #1704 from minggo/cclayer_script_support

Cclayer script support
This commit is contained in:
minggo 2012-12-03 18:55:11 -08:00
commit bbcbfa71ef
8 changed files with 177 additions and 70 deletions

View File

@ -46,7 +46,9 @@ CCLayer::CCLayer()
: m_bTouchEnabled(false) : m_bTouchEnabled(false)
, m_bAccelerometerEnabled(false) , m_bAccelerometerEnabled(false)
, m_bKeypadEnabled(false) , m_bKeypadEnabled(false)
, m_pScriptHandlerEntry(NULL) ,m_pScriptTouchHandlerEntry(NULL)
,m_pScriptKeypadHandlerEntry(NULL)
,m_pScriptAccelerateHandlerEntry(NULL)
, m_eTouchMode(kCCTouchesAllAtOnce) , m_eTouchMode(kCCTouchesAllAtOnce)
, m_nTouchPriority(0) , m_nTouchPriority(0)
{ {
@ -57,6 +59,8 @@ CCLayer::CCLayer()
CCLayer::~CCLayer() CCLayer::~CCLayer()
{ {
unregisterScriptTouchHandler(); unregisterScriptTouchHandler();
unregisterScriptKeypadHandler();
unregisterScriptAccelerateHandler();
} }
bool CCLayer::init() bool CCLayer::init()
@ -102,19 +106,19 @@ void CCLayer::registerWithTouchDispatcher()
CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher(); CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher();
// Using LuaBindings // Using LuaBindings
if (m_pScriptHandlerEntry) if (m_pScriptTouchHandlerEntry)
{ {
if (m_pScriptHandlerEntry->isMultiTouches()) if (m_pScriptTouchHandlerEntry->isMultiTouches())
{ {
pDispatcher->addStandardDelegate(this, 0); pDispatcher->addStandardDelegate(this, 0);
LUALOG("[LUA] Add multi-touches event handler: %d", m_pScriptHandlerEntry->getHandler()); LUALOG("[LUA] Add multi-touches event handler: %d", m_pScriptTouchHandlerEntry->getHandler());
} }
else else
{ {
pDispatcher->addTargetedDelegate(this, pDispatcher->addTargetedDelegate(this,
m_pScriptHandlerEntry->getPriority(), m_pScriptTouchHandlerEntry->getPriority(),
m_pScriptHandlerEntry->getSwallowsTouches()); m_pScriptTouchHandlerEntry->getSwallowsTouches());
LUALOG("[LUA] Add touch event handler: %d", m_pScriptHandlerEntry->getHandler()); LUALOG("[LUA] Add touch event handler: %d", m_pScriptTouchHandlerEntry->getHandler());
} }
} }
else else
@ -130,13 +134,13 @@ 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::create(nHandler, bIsMultiTouches, nPriority, bSwallowsTouches); m_pScriptTouchHandlerEntry = CCTouchScriptHandlerEntry::create(nHandler, bIsMultiTouches, nPriority, bSwallowsTouches);
m_pScriptHandlerEntry->retain(); m_pScriptTouchHandlerEntry->retain();
} }
void CCLayer::unregisterScriptTouchHandler(void) void CCLayer::unregisterScriptTouchHandler(void)
{ {
CC_SAFE_RELEASE_NULL(m_pScriptHandlerEntry); CC_SAFE_RELEASE_NULL(m_pScriptTouchHandlerEntry);
} }
int CCLayer::excuteScriptTouchHandler(int nEventType, CCTouch *pTouch) int CCLayer::excuteScriptTouchHandler(int nEventType, CCTouch *pTouch)
@ -262,6 +266,17 @@ void CCLayer::didAccelerate(CCAcceleration* pAccelerationValue)
} }
} }
void CCLayer::registerScriptAccelerateHandler(int nHandler)
{
unregisterScriptAccelerateHandler();
m_pScriptAccelerateHandlerEntry = CCScriptHandlerEntry::create(nHandler);
m_pScriptAccelerateHandlerEntry->retain();
}
void CCLayer::unregisterScriptAccelerateHandler(void)
{
CC_SAFE_RELEASE_NULL(m_pScriptAccelerateHandlerEntry);
}
/// isKeypadEnabled getter /// isKeypadEnabled getter
bool CCLayer::isKeypadEnabled() bool CCLayer::isKeypadEnabled()
@ -290,6 +305,34 @@ void CCLayer::setKeypadEnabled(bool enabled)
} }
} }
void CCLayer::registerScriptKeypadHandler(int nHandler)
{
unregisterScriptKeypadHandler();
m_pScriptKeypadHandlerEntry = CCScriptHandlerEntry::create(nHandler);
m_pScriptKeypadHandlerEntry->retain();
}
void CCLayer::unregisterScriptKeypadHandler(void)
{
CC_SAFE_RELEASE_NULL(m_pScriptKeypadHandlerEntry);
}
void CCLayer::keyBackClicked(void)
{
if (m_pScriptKeypadHandlerEntry)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerKeypadEvent(this, kTypeBackClicked);
}
}
void CCLayer::keyMenuClicked(void)
{
if (m_pScriptKeypadHandlerEntry)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerKeypadEvent(this, kTypeMenuClicked);
}
}
/// Callbacks /// Callbacks
void CCLayer::onEnter() void CCLayer::onEnter()
{ {

View File

@ -86,6 +86,8 @@ public:
virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent); virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);
virtual void didAccelerate(CCAcceleration* pAccelerationValue); virtual void didAccelerate(CCAcceleration* pAccelerationValue);
void registerScriptAccelerateHandler(int nHandler);
void unregisterScriptAccelerateHandler(void);
/** If isTouchEnabled, this method is called onEnter. Override it to change the /** If isTouchEnabled, this method is called onEnter. Override it to change the
way CCLayer receives touch events. way CCLayer receives touch events.
@ -134,7 +136,17 @@ public:
virtual bool isKeypadEnabled(); virtual bool isKeypadEnabled();
virtual void setKeypadEnabled(bool value); virtual void setKeypadEnabled(bool value);
inline CCTouchScriptHandlerEntry* getScriptHandlerEntry() { return m_pScriptHandlerEntry; }; /** Register keypad events handler */
void registerScriptKeypadHandler(int nHandler);
/** Unregister keypad events handler */
void unregisterScriptKeypadHandler(void);
virtual void keyBackClicked(void);
virtual void keyMenuClicked(void);
inline CCTouchScriptHandlerEntry* getScriptTouchHandlerEntry() { return m_pScriptTouchHandlerEntry; };
inline CCScriptHandlerEntry* getScriptKeypadHandlerEntry() { return m_pScriptKeypadHandlerEntry; };
inline CCScriptHandlerEntry* getScriptAccelerateHandlerEntry() { return m_pScriptAccelerateHandlerEntry; };
protected: protected:
bool m_bTouchEnabled; bool m_bTouchEnabled;
bool m_bAccelerometerEnabled; bool m_bAccelerometerEnabled;
@ -142,7 +154,9 @@ protected:
private: private:
// Script touch events handler // Script touch events handler
CCTouchScriptHandlerEntry* m_pScriptHandlerEntry; CCTouchScriptHandlerEntry* m_pScriptTouchHandlerEntry;
CCScriptHandlerEntry* m_pScriptKeypadHandlerEntry;
CCScriptHandlerEntry* m_pScriptAccelerateHandlerEntry;
int m_nTouchPriority; int m_nTouchPriority;
ccTouchesMode m_eTouchMode; ccTouchesMode m_eTouchMode;

View File

@ -29,6 +29,7 @@
#include "CCAccelerometer.h" #include "CCAccelerometer.h"
#include "touch_dispatcher/CCTouch.h" #include "touch_dispatcher/CCTouch.h"
#include "cocoa/CCSet.h" #include "cocoa/CCSet.h"
#include "CCAccelerometer.h"
#include <map> #include <map>
#include <string> #include <string>
#include <list> #include <list>
@ -226,6 +227,9 @@ public:
virtual int executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches) = 0; virtual int executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches) = 0;
virtual int executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch) = 0; virtual int executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch) = 0;
/** functions for keypad event */
virtual int executeLayerKeypadEvent(CCLayer* pLayer, int eventType) = 0;
/** execute a accelerometer event */ /** execute a accelerometer event */
virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue) = 0; virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue) = 0;
}; };

View File

@ -1 +1 @@
a9d5159141c92d549c9d9e8c9fed53517792589d 277dcf61e5fb78a98293b107aa71f6d71b836f93

View File

@ -809,6 +809,11 @@ int ScriptingCore::executeAccelerometerEvent(CCLayer *pLayer, CCAcceleration *pA
return 1; return 1;
} }
int ScriptingCore::executeLayerKeypadEvent(CCLayer* pLayer, int eventType)
{
return 0;
}
int ScriptingCore::executeCustomTouchesEvent(int eventType, int ScriptingCore::executeCustomTouchesEvent(int eventType,
CCSet *pTouches, JSObject *obj) CCSet *pTouches, JSObject *obj)

View File

@ -84,6 +84,7 @@ public:
virtual int executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches); virtual int executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches);
virtual int executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch); virtual int executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch);
virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue); virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue);
virtual int executeLayerKeypadEvent(CCLayer* pLayer, int eventType);
bool executeFunctionWithObjectData(CCNode *self, const char *name, JSObject *obj); bool executeFunctionWithObjectData(CCNode *self, const char *name, JSObject *obj);
int executeFunctionWithOwner(jsval owner, const char *name, jsval data); int executeFunctionWithOwner(jsval owner, const char *name, jsval data);

View File

@ -395,7 +395,7 @@ int CCLuaEngine::executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch
int ret = 0; int ret = 0;
do do
{ {
CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptHandlerEntry(); CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry();
CC_BREAK_IF(NULL == pScriptHandlerEntry); CC_BREAK_IF(NULL == pScriptHandlerEntry);
int nScriptHandler = pScriptHandlerEntry->getHandler(); int nScriptHandler = pScriptHandlerEntry->getHandler();
CC_BREAK_IF(0 == nScriptHandler); CC_BREAK_IF(0 == nScriptHandler);
@ -415,7 +415,7 @@ int CCLuaEngine::executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet
int ret = 0; int ret = 0;
do do
{ {
CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptHandlerEntry(); CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry();
CC_BREAK_IF(NULL == pScriptHandlerEntry); CC_BREAK_IF(NULL == pScriptHandlerEntry);
int nScriptHandler = pScriptHandlerEntry->getHandler(); int nScriptHandler = pScriptHandlerEntry->getHandler();
CC_BREAK_IF(0 == nScriptHandler); CC_BREAK_IF(0 == nScriptHandler);
@ -445,6 +445,45 @@ int CCLuaEngine::executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet
return ret; return ret;
} }
int CCLuaEngine::executeLayerKeypadEvent(CCLayer* pLayer, int eventType)
{
int ret = 0;
do
{
CCScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptKeypadHandlerEntry();
CC_BREAK_IF(NULL == pScriptHandlerEntry);
int nScriptHandler = pScriptHandlerEntry->getHandler();
CC_BREAK_IF(0 == nScriptHandler);
cleanStack();
lua_newtable(m_state);
lua_pushinteger(m_state, eventType);
ret = executeFunctionByHandler(nScriptHandler, 1);
} while (0);
return ret;
}
int CCLuaEngine::executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue)
{
int ret = 0;
do
{
CCScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptAccelerateHandlerEntry();
CC_BREAK_IF(NULL == pScriptHandlerEntry);
int nScriptHandler = pScriptHandlerEntry->getHandler();
CC_BREAK_IF(0 == nScriptHandler);
cleanStack();
lua_newtable(m_state);
lua_pushnumber(m_state, pAccelerationValue->x);
lua_pushnumber(m_state, pAccelerationValue->y);
lua_pushnumber(m_state, pAccelerationValue->z);
lua_pushnumber(m_state, pAccelerationValue->timestamp);
ret = executeFunctionByHandler(nScriptHandler, 4);
} while (0);
return ret;
}
int CCLuaEngine::executeFunctionByHandler(int nHandler, int numArgs) int CCLuaEngine::executeFunctionByHandler(int nHandler, int numArgs)
{ {
if (pushFunction(nHandler)) /* stack: ... arg1 arg2 ... func */ if (pushFunction(nHandler)) /* stack: ... arg1 arg2 ... func */

View File

@ -195,8 +195,9 @@ public:
virtual int executeSchedule(CCTimer* pTimer, float dt, CCNode* pNode = NULL); virtual int executeSchedule(CCTimer* pTimer, float dt, CCNode* pNode = NULL);
virtual int executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches); virtual int executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches);
virtual int executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch); virtual int executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch);
virtual int executeLayerKeypadEvent(CCLayer* pLayer, int eventType);
/** execute a accelerometer event */ /** execute a accelerometer event */
virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue){ return 0;}; virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue);
/** /**
@brief Method used to get a pointer to the lua_State that the script module is attached to. @brief Method used to get a pointer to the lua_State that the script module is attached to.
@return A pointer to the lua_State that the script module is attached to. @return A pointer to the lua_State that the script module is attached to.