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.
@ -133,8 +135,18 @@ public:
*/ */
virtual bool isKeypadEnabled(); virtual bool isKeypadEnabled();
virtual void setKeypadEnabled(bool value); virtual void setKeypadEnabled(bool value);
/** 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* getScriptHandlerEntry() { return m_pScriptHandlerEntry; }; 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;
@ -338,9 +352,9 @@ public:
The current (old) layer will be removed from it's parent with 'cleanup:YES'. The current (old) layer will be removed from it's parent with 'cleanup:YES'.
*/ */
/** initializes a CCMultiplexLayer with an array of layers /** initializes a CCMultiplexLayer with an array of layers
@since v2.1 @since v2.1
*/ */
bool initWithArray(CCArray* arrayOfLayers); bool initWithArray(CCArray* arrayOfLayers);
void switchTo(unsigned int n); void switchTo(unsigned int n);
@ -353,8 +367,8 @@ public:
static CCLayerMultiplex* node(); static CCLayerMultiplex* node();
static CCLayerMultiplex* create(); static CCLayerMultiplex* create();
/** creates a CCMultiplexLayer with an array of layers. /** creates a CCMultiplexLayer with an array of layers.
@since v2.1 @since v2.1
*/ */
static CCLayerMultiplex* createWithArray(CCArray* arrayOfLayers); static CCLayerMultiplex* createWithArray(CCArray* arrayOfLayers);
}; };

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)
@ -925,39 +930,39 @@ CCAcceleration jsval_to_ccacceleration(JSContext *cx, jsval v) {
return ret; return ret;
} }
CCArray* jsvals_variadic_to_ccarray( JSContext *cx, jsval *vp, int argc) CCArray* jsvals_variadic_to_ccarray( JSContext *cx, jsval *vp, int argc)
{ {
JSBool ok = JS_FALSE; JSBool ok = JS_FALSE;
CCArray* pArray = CCArray::create(); CCArray* pArray = CCArray::create();
for( int i=0; i < argc; i++ ) for( int i=0; i < argc; i++ )
{ {
double num = 0.0; double num = 0.0;
// optimization: JS_ValueToNumber is expensive. And can convert an string like "12" to a number // optimization: JS_ValueToNumber is expensive. And can convert an string like "12" to a number
if( JSVAL_IS_NUMBER(*vp)) { if( JSVAL_IS_NUMBER(*vp)) {
ok = JS_ValueToNumber(cx, *vp, &num ); ok = JS_ValueToNumber(cx, *vp, &num );
if (!ok) { if (!ok) {
break; break;
} }
pArray->addObject(CCInteger::create((int)num)); pArray->addObject(CCInteger::create((int)num));
} }
else if (JSVAL_IS_STRING(*vp)) else if (JSVAL_IS_STRING(*vp))
{ {
JSStringWrapper str(JSVAL_TO_STRING(*vp), cx); JSStringWrapper str(JSVAL_TO_STRING(*vp), cx);
pArray->addObject(CCString::create(str)); pArray->addObject(CCString::create(str));
} }
else else
{ {
js_proxy_t* p; js_proxy_t* p;
JSObject* obj = JSVAL_TO_OBJECT(*vp); JSObject* obj = JSVAL_TO_OBJECT(*vp);
JS_GET_NATIVE_PROXY(p, obj); JS_GET_NATIVE_PROXY(p, obj);
if (p) { if (p) {
pArray->addObject((CCObject*)p->ptr); pArray->addObject((CCObject*)p->ptr);
} }
} }
// next // next
vp++; vp++;
} }
return pArray; return pArray;
} }
CCRect jsval_to_ccrect(JSContext *cx, jsval v) { CCRect jsval_to_ccrect(JSContext *cx, jsval v) {
@ -1498,26 +1503,26 @@ JSBool jsSocketClose(JSContext* cx, unsigned argc, jsval* vp)
return JS_TRUE; return JS_TRUE;
} }
JSBool jsb_set_reserved_slot(JSObject *obj, uint32_t idx, jsval value) JSBool jsb_set_reserved_slot(JSObject *obj, uint32_t idx, jsval value)
{ {
JSClass *klass = JS_GetClass(obj); JSClass *klass = JS_GetClass(obj);
unsigned int slots = JSCLASS_RESERVED_SLOTS(klass); unsigned int slots = JSCLASS_RESERVED_SLOTS(klass);
if( idx >= slots ) if( idx >= slots )
return JS_FALSE; return JS_FALSE;
JS_SetReservedSlot(obj, idx, value); JS_SetReservedSlot(obj, idx, value);
return JS_TRUE; return JS_TRUE;
} }
JSBool jsb_get_reserved_slot(JSObject *obj, uint32_t idx, jsval& ret) JSBool jsb_get_reserved_slot(JSObject *obj, uint32_t idx, jsval& ret)
{ {
JSClass *klass = JS_GetClass(obj); JSClass *klass = JS_GetClass(obj);
unsigned int slots = JSCLASS_RESERVED_SLOTS(klass); unsigned int slots = JSCLASS_RESERVED_SLOTS(klass);
if( idx >= slots ) if( idx >= slots )
return JS_FALSE; return JS_FALSE;
ret = JS_GetReservedSlot(obj, idx); ret = JS_GetReservedSlot(obj, idx);
return JS_TRUE; return JS_TRUE;
} }

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.