From e0ee27cb17cacf4e50ed8717181edb1c6b9edabc Mon Sep 17 00:00:00 2001 From: dualface Date: Sun, 2 Dec 2012 15:17:34 +0800 Subject: [PATCH 1/2] [luabinding] fix CCLayer:getScriptTOuchHandlerEntry() [luabinding] add CCLayer keypad, accelerometer support --- .../CCLayer.cpp | 63 ++++++++++++++++--- .../layers_scenes_transitions_nodes/CCLayer.h | 28 ++++++--- cocos2dx/script_support/CCScriptSupport.h | 4 ++ .../lua/cocos2dx_support/CCLuaEngine.cpp | 43 ++++++++++++- scripting/lua/cocos2dx_support/CCLuaEngine.h | 3 +- 5 files changed, 121 insertions(+), 20 deletions(-) diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index 2094c3cbb9..2f6d0db77e 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -46,7 +46,9 @@ CCLayer::CCLayer() : m_bTouchEnabled(false) , m_bAccelerometerEnabled(false) , m_bKeypadEnabled(false) -, m_pScriptHandlerEntry(NULL) +,m_pScriptTouchHandlerEntry(NULL) +,m_pScriptKeypadHandlerEntry(NULL) +,m_pScriptAccelerateHandlerEntry(NULL) , m_eTouchMode(kCCTouchesAllAtOnce) , m_nTouchPriority(0) { @@ -57,6 +59,8 @@ CCLayer::CCLayer() CCLayer::~CCLayer() { unregisterScriptTouchHandler(); + unregisterScriptKeypadHandler(); + unregisterScriptAccelerateHandler(); } bool CCLayer::init() @@ -102,19 +106,19 @@ void CCLayer::registerWithTouchDispatcher() CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher(); // Using LuaBindings - if (m_pScriptHandlerEntry) + if (m_pScriptTouchHandlerEntry) { - if (m_pScriptHandlerEntry->isMultiTouches()) + if (m_pScriptTouchHandlerEntry->isMultiTouches()) { 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 { pDispatcher->addTargetedDelegate(this, - m_pScriptHandlerEntry->getPriority(), - m_pScriptHandlerEntry->getSwallowsTouches()); - LUALOG("[LUA] Add touch event handler: %d", m_pScriptHandlerEntry->getHandler()); + m_pScriptTouchHandlerEntry->getPriority(), + m_pScriptTouchHandlerEntry->getSwallowsTouches()); + LUALOG("[LUA] Add touch event handler: %d", m_pScriptTouchHandlerEntry->getHandler()); } } else @@ -130,13 +134,13 @@ void CCLayer::registerWithTouchDispatcher() void CCLayer::registerScriptTouchHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches) { unregisterScriptTouchHandler(); - m_pScriptHandlerEntry = CCTouchScriptHandlerEntry::create(nHandler, bIsMultiTouches, nPriority, bSwallowsTouches); - m_pScriptHandlerEntry->retain(); + m_pScriptTouchHandlerEntry = CCTouchScriptHandlerEntry::create(nHandler, bIsMultiTouches, nPriority, bSwallowsTouches); + m_pScriptTouchHandlerEntry->retain(); } void CCLayer::unregisterScriptTouchHandler(void) { - CC_SAFE_RELEASE_NULL(m_pScriptHandlerEntry); + CC_SAFE_RELEASE_NULL(m_pScriptTouchHandlerEntry); } 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 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 void CCLayer::onEnter() { diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h index 53a3493edf..5a1fb926c9 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h @@ -86,6 +86,8 @@ public: virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent); virtual void didAccelerate(CCAcceleration* pAccelerationValue); + void registerScriptAccelerateHandler(int nHandler); + void unregisterScriptAccelerateHandler(void); /** If isTouchEnabled, this method is called onEnter. Override it to change the way CCLayer receives touch events. @@ -133,8 +135,18 @@ public: */ virtual bool isKeypadEnabled(); 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: bool m_bTouchEnabled; bool m_bAccelerometerEnabled; @@ -142,7 +154,9 @@ protected: private: // Script touch events handler - CCTouchScriptHandlerEntry* m_pScriptHandlerEntry; + CCTouchScriptHandlerEntry* m_pScriptTouchHandlerEntry; + CCScriptHandlerEntry* m_pScriptKeypadHandlerEntry; + CCScriptHandlerEntry* m_pScriptAccelerateHandlerEntry; int m_nTouchPriority; ccTouchesMode m_eTouchMode; @@ -338,9 +352,9 @@ public: The current (old) layer will be removed from it's parent with 'cleanup:YES'. */ - /** initializes a CCMultiplexLayer with an array of layers - @since v2.1 - */ + /** initializes a CCMultiplexLayer with an array of layers + @since v2.1 + */ bool initWithArray(CCArray* arrayOfLayers); void switchTo(unsigned int n); @@ -353,8 +367,8 @@ public: static CCLayerMultiplex* node(); static CCLayerMultiplex* create(); - /** creates a CCMultiplexLayer with an array of layers. - @since v2.1 + /** creates a CCMultiplexLayer with an array of layers. + @since v2.1 */ static CCLayerMultiplex* createWithArray(CCArray* arrayOfLayers); }; diff --git a/cocos2dx/script_support/CCScriptSupport.h b/cocos2dx/script_support/CCScriptSupport.h index a5ea4a8c44..63257fb3f6 100644 --- a/cocos2dx/script_support/CCScriptSupport.h +++ b/cocos2dx/script_support/CCScriptSupport.h @@ -29,6 +29,7 @@ #include "CCAccelerometer.h" #include "touch_dispatcher/CCTouch.h" #include "cocoa/CCSet.h" +#include "CCAccelerometer.h" #include #include #include @@ -226,6 +227,9 @@ public: virtual int executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches) = 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 */ virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue) = 0; }; diff --git a/scripting/lua/cocos2dx_support/CCLuaEngine.cpp b/scripting/lua/cocos2dx_support/CCLuaEngine.cpp index 1453ad51df..ee5ddafc93 100644 --- a/scripting/lua/cocos2dx_support/CCLuaEngine.cpp +++ b/scripting/lua/cocos2dx_support/CCLuaEngine.cpp @@ -395,7 +395,7 @@ int CCLuaEngine::executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch int ret = 0; do { - CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptHandlerEntry(); + CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry(); CC_BREAK_IF(NULL == pScriptHandlerEntry); int nScriptHandler = pScriptHandlerEntry->getHandler(); CC_BREAK_IF(0 == nScriptHandler); @@ -415,7 +415,7 @@ int CCLuaEngine::executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet int ret = 0; do { - CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptHandlerEntry(); + CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry(); CC_BREAK_IF(NULL == pScriptHandlerEntry); int nScriptHandler = pScriptHandlerEntry->getHandler(); CC_BREAK_IF(0 == nScriptHandler); @@ -445,6 +445,45 @@ int CCLuaEngine::executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet 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) { if (pushFunction(nHandler)) /* stack: ... arg1 arg2 ... func */ diff --git a/scripting/lua/cocos2dx_support/CCLuaEngine.h b/scripting/lua/cocos2dx_support/CCLuaEngine.h index f2e93a9cde..f6afe62da4 100644 --- a/scripting/lua/cocos2dx_support/CCLuaEngine.h +++ b/scripting/lua/cocos2dx_support/CCLuaEngine.h @@ -195,8 +195,9 @@ public: virtual int executeSchedule(CCTimer* pTimer, float dt, CCNode* pNode = NULL); virtual int executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches); virtual int executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch); + virtual int executeLayerKeypadEvent(CCLayer* pLayer, int eventType); /** 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. @return A pointer to the lua_State that the script module is attached to. From 4e7fca3185156368af79545087330b9c49c22735 Mon Sep 17 00:00:00 2001 From: minggo Date: Tue, 4 Dec 2012 10:52:49 +0800 Subject: [PATCH 2/2] merge dualface's pull request for fixing CCLayer script support and fix error for js-binding --- .../project.pbxproj.REMOVED.git-id | 2 +- .../javascript/bindings/ScriptingCore.cpp | 103 +++++++++--------- scripting/javascript/bindings/ScriptingCore.h | 1 + 3 files changed, 56 insertions(+), 50 deletions(-) diff --git a/samples/TestJavascript/proj.ios/TestJavascript.xcodeproj/project.pbxproj.REMOVED.git-id b/samples/TestJavascript/proj.ios/TestJavascript.xcodeproj/project.pbxproj.REMOVED.git-id index 008898f1de..b53dd2f351 100644 --- a/samples/TestJavascript/proj.ios/TestJavascript.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/samples/TestJavascript/proj.ios/TestJavascript.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -a9d5159141c92d549c9d9e8c9fed53517792589d \ No newline at end of file +277dcf61e5fb78a98293b107aa71f6d71b836f93 \ No newline at end of file diff --git a/scripting/javascript/bindings/ScriptingCore.cpp b/scripting/javascript/bindings/ScriptingCore.cpp index f28cad6958..aba9cb6cff 100644 --- a/scripting/javascript/bindings/ScriptingCore.cpp +++ b/scripting/javascript/bindings/ScriptingCore.cpp @@ -809,6 +809,11 @@ int ScriptingCore::executeAccelerometerEvent(CCLayer *pLayer, CCAcceleration *pA return 1; } +int ScriptingCore::executeLayerKeypadEvent(CCLayer* pLayer, int eventType) +{ + return 0; +} + int ScriptingCore::executeCustomTouchesEvent(int eventType, CCSet *pTouches, JSObject *obj) @@ -925,39 +930,39 @@ CCAcceleration jsval_to_ccacceleration(JSContext *cx, jsval v) { return ret; } -CCArray* jsvals_variadic_to_ccarray( JSContext *cx, jsval *vp, int argc) -{ - JSBool ok = JS_FALSE; - CCArray* pArray = CCArray::create(); - for( int i=0; i < argc; i++ ) - { - double num = 0.0; - // optimization: JS_ValueToNumber is expensive. And can convert an string like "12" to a number - if( JSVAL_IS_NUMBER(*vp)) { - ok = JS_ValueToNumber(cx, *vp, &num ); - if (!ok) { - break; - } - pArray->addObject(CCInteger::create((int)num)); - } +CCArray* jsvals_variadic_to_ccarray( JSContext *cx, jsval *vp, int argc) +{ + JSBool ok = JS_FALSE; + CCArray* pArray = CCArray::create(); + for( int i=0; i < argc; i++ ) + { + double num = 0.0; + // optimization: JS_ValueToNumber is expensive. And can convert an string like "12" to a number + if( JSVAL_IS_NUMBER(*vp)) { + ok = JS_ValueToNumber(cx, *vp, &num ); + if (!ok) { + break; + } + pArray->addObject(CCInteger::create((int)num)); + } else if (JSVAL_IS_STRING(*vp)) { JSStringWrapper str(JSVAL_TO_STRING(*vp), cx); pArray->addObject(CCString::create(str)); - } - else - { - js_proxy_t* p; - JSObject* obj = JSVAL_TO_OBJECT(*vp); - JS_GET_NATIVE_PROXY(p, obj); - if (p) { - pArray->addObject((CCObject*)p->ptr); - } - } - // next - vp++; - } - return pArray; + } + else + { + js_proxy_t* p; + JSObject* obj = JSVAL_TO_OBJECT(*vp); + JS_GET_NATIVE_PROXY(p, obj); + if (p) { + pArray->addObject((CCObject*)p->ptr); + } + } + // next + vp++; + } + return pArray; } CCRect jsval_to_ccrect(JSContext *cx, jsval v) { @@ -1498,26 +1503,26 @@ JSBool jsSocketClose(JSContext* cx, unsigned argc, jsval* vp) return JS_TRUE; } -JSBool jsb_set_reserved_slot(JSObject *obj, uint32_t idx, jsval value) -{ - JSClass *klass = JS_GetClass(obj); - unsigned int slots = JSCLASS_RESERVED_SLOTS(klass); - if( idx >= slots ) - return JS_FALSE; - - JS_SetReservedSlot(obj, idx, value); - - return JS_TRUE; +JSBool jsb_set_reserved_slot(JSObject *obj, uint32_t idx, jsval value) +{ + JSClass *klass = JS_GetClass(obj); + unsigned int slots = JSCLASS_RESERVED_SLOTS(klass); + if( idx >= slots ) + return JS_FALSE; + + JS_SetReservedSlot(obj, idx, value); + + return JS_TRUE; } -JSBool jsb_get_reserved_slot(JSObject *obj, uint32_t idx, jsval& ret) -{ - JSClass *klass = JS_GetClass(obj); - unsigned int slots = JSCLASS_RESERVED_SLOTS(klass); - if( idx >= slots ) - return JS_FALSE; - - ret = JS_GetReservedSlot(obj, idx); - - return JS_TRUE; +JSBool jsb_get_reserved_slot(JSObject *obj, uint32_t idx, jsval& ret) +{ + JSClass *klass = JS_GetClass(obj); + unsigned int slots = JSCLASS_RESERVED_SLOTS(klass); + if( idx >= slots ) + return JS_FALSE; + + ret = JS_GetReservedSlot(obj, idx); + + return JS_TRUE; } diff --git a/scripting/javascript/bindings/ScriptingCore.h b/scripting/javascript/bindings/ScriptingCore.h index ed801d38cf..d7304a623e 100644 --- a/scripting/javascript/bindings/ScriptingCore.h +++ b/scripting/javascript/bindings/ScriptingCore.h @@ -84,6 +84,7 @@ public: virtual int executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches); virtual int executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch); virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue); + virtual int executeLayerKeypadEvent(CCLayer* pLayer, int eventType); bool executeFunctionWithObjectData(CCNode *self, const char *name, JSObject *obj); int executeFunctionWithOwner(jsval owner, const char *name, jsval data);