From 2228614748aea602ac968c36fad52a51b2e4cff7 Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 22 Jul 2013 17:12:53 +0800 Subject: [PATCH] issue #2344: JSB also uses 'ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(XXX);' now. --- cocos2dx/base_nodes/CCNode.cpp | 41 +--- .../CCLayer.cpp | 66 +++--- .../layers_scenes_transitions_nodes/CCLayer.h | 4 +- cocos2dx/menu_nodes/CCMenuItem.cpp | 8 +- cocos2dx/script_support/CCScriptSupport.h | 103 +++++---- cocos2dx/support/CCNotificationCenter.cpp | 2 +- .../GUI/CCControlExtension/CCControl.cpp | 2 +- .../luaScript/ExtensionTest/ExtensionTest.lua | 1 + .../javascript/bindings/ScriptingCore.cpp | 166 ++++++++++---- scripting/javascript/bindings/ScriptingCore.h | 18 +- .../lua/cocos2dx_support/CCLuaEngine.cpp | 209 +++++++++++------- scripting/lua/cocos2dx_support/CCLuaEngine.h | 1 + 12 files changed, 353 insertions(+), 268 deletions(-) diff --git a/cocos2dx/base_nodes/CCNode.cpp b/cocos2dx/base_nodes/CCNode.cpp index d52d1da82b..efc5652266 100644 --- a/cocos2dx/base_nodes/CCNode.cpp +++ b/cocos2dx/base_nodes/CCNode.cpp @@ -506,17 +506,13 @@ void Node::cleanup() this->stopAllActions(); this->unscheduleAllSelectors(); - if ( _scriptType == kScriptTypeLua) + if ( _scriptType != kScriptTypeNone) { int action = kNodeOnCleanup; - BasicScriptData data((void*)this,(void*)&action); + BasicScriptData data(this,(void*)&action); ScriptEvent scriptEvent(kNodeEvent,(void*)&data); ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent); } - else if(_scriptType == kScriptTypeJavascript) - { - ScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kNodeOnCleanup); - } // timers arrayMakeObjectsPerformSelector(_children, cleanup, Node*); @@ -875,17 +871,13 @@ void Node::onEnter() _running = true; - if (_scriptType == kScriptTypeLua) + if (_scriptType != kScriptTypeNone) { int action = kNodeOnEnter; - BasicScriptData data((void*)this,(void*)&action); + BasicScriptData data(this,(void*)&action); ScriptEvent scriptEvent(kNodeEvent,(void*)&data); ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent); } - else if(_scriptType == kScriptTypeJavascript) - { - ScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kNodeOnEnter); - } } void Node::onEnterTransitionDidFinish() @@ -894,31 +886,24 @@ void Node::onEnterTransitionDidFinish() arrayMakeObjectsPerformSelector(_children, onEnterTransitionDidFinish, Node*); - if (_scriptType == kScriptTypeLua) + if (_scriptType != kScriptTypeNone) { int action = kNodeOnEnterTransitionDidFinish; - BasicScriptData data((void*)this,(void*)&action); + BasicScriptData data(this,(void*)&action); ScriptEvent scriptEvent(kNodeEvent,(void*)&data); ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent); } - else if (_scriptType == kScriptTypeJavascript) - { - ScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kNodeOnEnterTransitionDidFinish); - } } void Node::onExitTransitionDidStart() { arrayMakeObjectsPerformSelector(_children, onExitTransitionDidStart, Node*); - if (_scriptType == kScriptTypeLua) + if (_scriptType != kScriptTypeNone) { int action = kNodeOnExitTransitionDidStart; - BasicScriptData data((void*)this,(void*)&action); + BasicScriptData data(this,(void*)&action); ScriptEvent scriptEvent(kNodeEvent,(void*)&data); - ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent); } - else if (_scriptType == kScriptTypeJavascript) - { - ScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kNodeOnExitTransitionDidStart); + ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent); } } @@ -927,17 +912,13 @@ void Node::onExit() this->pauseSchedulerAndActions(); _running = false; - if (_scriptType == kScriptTypeLua) + if (_scriptType != kScriptTypeNone) { int action = kNodeOnExit; - BasicScriptData data((void*)this,(void*)&action); + BasicScriptData data(this,(void*)&action); ScriptEvent scriptEvent(kNodeEvent,(void*)&data); ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent); } - else if ( _scriptType == kScriptTypeJavascript) - { - ScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kNodeOnExit); - } arrayMakeObjectsPerformSelector(_children, onExit, Node*); } diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index 19210468e3..022e5416fb 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -103,36 +103,28 @@ void Layer::registerWithTouchDispatcher() } } -int Layer::excuteScriptTouchHandler(int nEventType, Touch *pTouch) +int Layer::executeScriptTouchHandler(int eventType, Touch* touch) { - if (kScriptTypeLua == _scriptType) + if (kScriptTypeNone != _scriptType) { - Set touches; - touches.addObject((Object*)pTouch); - TouchesScriptData data(nEventType,(void*)this,&touches); - ScriptEvent event(kTouchesEvent,&data); + TouchScriptData data(eventType, this, touch); + ScriptEvent event(kTouchEvent, &data); return ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event); } - else if(kScriptTypeJavascript == _scriptType) - { - return ScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerTouchEvent(this, nEventType, pTouch); - } + //can not reach it return 0; } -int Layer::excuteScriptTouchHandler(int nEventType, Set *pTouches) +int Layer::executeScriptTouchesHandler(int eventType, Set* touches) { - if (kScriptTypeLua == _scriptType) + if (kScriptTypeNone != _scriptType) { - TouchesScriptData data(nEventType,(void*)this,pTouches); - ScriptEvent event(kTouchesEvent,&data); + TouchesScriptData data(eventType, this, touches); + ScriptEvent event(kTouchesEvent, &data); return ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event); } - else if(kScriptTypeJavascript == _scriptType) - { - return ScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerTouchesEvent(this, nEventType, pTouches); - } + return 0; } @@ -141,6 +133,7 @@ bool Layer::isTouchEnabled() const { return _touchEnabled; } + /// isTouchEnabled setter void Layer::setTouchEnabled(bool enabled) { @@ -242,14 +235,11 @@ void Layer::setAccelerometerInterval(double interval) { void Layer::didAccelerate(Acceleration* pAccelerationValue) { - CC_UNUSED_PARAM(pAccelerationValue); - if (kScriptTypeJavascript == _scriptType) + CC_UNUSED_PARAM(pAccelerationValue); + + if(kScriptTypeNone == _scriptType) { - ScriptEngineManager::sharedManager()->getScriptEngine()->executeAccelerometerEvent(this, pAccelerationValue); - } - else if(kScriptTypeLua == _scriptType) - { - BasicScriptData data((void*)this,(void*)pAccelerationValue); + BasicScriptData data(this,(void*)pAccelerationValue); ScriptEvent event(kAccelerometerEvent,&data); ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event); } @@ -310,23 +300,19 @@ void Layer::setKeypadEnabled(bool enabled) void Layer::keyBackClicked(void) { - if (kScriptTypeLua == _scriptType) + if (kScriptTypeNone != _scriptType) { - KeypadScriptData data(kTypeBackClicked,(void*)this); + KeypadScriptData data(kTypeBackClicked, this); ScriptEvent event(kKeypadEvent,(void*)&data); ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event); } - else if(kScriptTypeJavascript == _scriptType) - { - ScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerKeypadEvent(this, kTypeBackClicked); - } } void Layer::keyMenuClicked(void) { if (kScriptTypeLua == _scriptType) { - KeypadScriptData data(kTypeMenuClicked,(void*)this); + KeypadScriptData data(kTypeMenuClicked, this); ScriptEvent event(kKeypadEvent,(void*)&data); ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event); } @@ -397,7 +383,7 @@ bool Layer::ccTouchBegan(Touch *pTouch, Event *pEvent) { if (kScriptTypeNone != _scriptType) { - return excuteScriptTouchHandler(CCTOUCHBEGAN, pTouch) == 0 ? false : true; + return executeScriptTouchHandler(CCTOUCHBEGAN, pTouch) == 0 ? false : true; } CC_UNUSED_PARAM(pTouch); @@ -410,7 +396,7 @@ void Layer::ccTouchMoved(Touch *pTouch, Event *pEvent) { if (kScriptTypeNone != _scriptType) { - excuteScriptTouchHandler(CCTOUCHMOVED, pTouch); + executeScriptTouchHandler(CCTOUCHMOVED, pTouch); return; } @@ -422,7 +408,7 @@ void Layer::ccTouchEnded(Touch *pTouch, Event *pEvent) { if (kScriptTypeNone != _scriptType) { - excuteScriptTouchHandler(CCTOUCHENDED, pTouch); + executeScriptTouchHandler(CCTOUCHENDED, pTouch); return; } @@ -434,7 +420,7 @@ void Layer::ccTouchCancelled(Touch *pTouch, Event *pEvent) { if (kScriptTypeNone != _scriptType) { - excuteScriptTouchHandler(CCTOUCHCANCELLED, pTouch); + executeScriptTouchHandler(CCTOUCHCANCELLED, pTouch); return; } @@ -446,7 +432,7 @@ void Layer::ccTouchesBegan(Set *pTouches, Event *pEvent) { if (kScriptTypeNone != _scriptType) { - excuteScriptTouchHandler(CCTOUCHBEGAN, pTouches); + executeScriptTouchesHandler(CCTOUCHBEGAN, pTouches); return; } @@ -458,7 +444,7 @@ void Layer::ccTouchesMoved(Set *pTouches, Event *pEvent) { if (kScriptTypeNone != _scriptType) { - excuteScriptTouchHandler(CCTOUCHMOVED, pTouches); + executeScriptTouchesHandler(CCTOUCHMOVED, pTouches); return; } @@ -470,7 +456,7 @@ void Layer::ccTouchesEnded(Set *pTouches, Event *pEvent) { if (kScriptTypeNone != _scriptType) { - excuteScriptTouchHandler(CCTOUCHENDED, pTouches); + executeScriptTouchesHandler(CCTOUCHENDED, pTouches); return; } @@ -482,7 +468,7 @@ void Layer::ccTouchesCancelled(Set *pTouches, Event *pEvent) { if (kScriptTypeNone != _scriptType) { - excuteScriptTouchHandler(CCTOUCHCANCELLED, pTouches); + executeScriptTouchesHandler(CCTOUCHCANCELLED, pTouches); return; } diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h index 0fdbb355f0..cc1ced690a 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h @@ -150,8 +150,8 @@ private: int _touchPriority; ccTouchesMode _touchMode; - int excuteScriptTouchHandler(int nEventType, Touch *pTouch); - int excuteScriptTouchHandler(int nEventType, Set *pTouches); + int executeScriptTouchHandler(int eventType, Touch* touch); + int executeScriptTouchesHandler(int eventType, Set* touches); }; #ifdef __apple__ diff --git a/cocos2dx/menu_nodes/CCMenuItem.cpp b/cocos2dx/menu_nodes/CCMenuItem.cpp index 0db4bc6b25..771c48e233 100644 --- a/cocos2dx/menu_nodes/CCMenuItem.cpp +++ b/cocos2dx/menu_nodes/CCMenuItem.cpp @@ -112,16 +112,12 @@ void MenuItem::activate() _callback(this); } - if (kScriptTypeLua == _scriptType) + if (kScriptTypeNone != _scriptType) { - BasicScriptData data((void*)this); + BasicScriptData data(this); ScriptEvent scriptEvent(kMenuClickedEvent,&data); ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent); } - else if (kScriptTypeJavascript == _scriptType) - { - ScriptEngineManager::sharedManager()->getScriptEngine()->executeMenuItemEvent(this); - } } } diff --git a/cocos2dx/script_support/CCScriptSupport.h b/cocos2dx/script_support/CCScriptSupport.h index a39b648040..26f9a55c0c 100644 --- a/cocos2dx/script_support/CCScriptSupport.h +++ b/cocos2dx/script_support/CCScriptSupport.h @@ -163,6 +163,7 @@ enum ScriptEventType kNotificationEvent, kCallFuncEvent, kScheduleEvent, + kTouchEvent, kTouchesEvent, kKeypadEvent, kAccelerometerEvent, @@ -172,27 +173,31 @@ enum ScriptEventType struct BasicScriptData { - //nativeobject:to get handler for lua or to get jsobject for js + // nativeobject:to get handler for lua or to get jsobject for js void* nativeObject; - //value: a pointer to a object that already defined + // value: a pointer to a object that already defined void* value; + + // Constructor BasicScriptData(void* inObject,void* inValue = NULL) - :nativeObject(inObject),value(inValue) + : nativeObject(inObject),value(inValue) { } }; struct SchedulerScriptData { - //lua use + // lua use int handler; float elapse; - //js use - Node* node; - SchedulerScriptData(int inHandler,float inElapse,Node* inNode = NULL) - :handler(inHandler), - elapse(inElapse), - node(inNode) + // js use + void* node; + + // Constructor + SchedulerScriptData(int inHandler,float inElapse,void* inNode = NULL) + : handler(inHandler), + elapse(inElapse), + node(inNode) { } }; @@ -202,10 +207,27 @@ struct TouchesScriptData int actionType; void* nativeObject; Set* touches; - TouchesScriptData(int inActionType,void* inNativeObject,Set* inTouches) - :actionType(inActionType), - nativeObject(inNativeObject), - touches(inTouches) + + // Constructor + TouchesScriptData(int inActionType, void* inNativeObject, Set* inTouches) + : actionType(inActionType), + nativeObject(inNativeObject), + touches(inTouches) + { + } +}; + +struct TouchScriptData +{ + int actionType; + void* nativeObject; + Touch* touch; + + // Constructor + TouchScriptData(int inActionType, void* inNativeObject, Touch* inTouch) + : actionType(inActionType), + nativeObject(inNativeObject), + touch(inTouch) { } }; @@ -214,22 +236,26 @@ struct KeypadScriptData { int actionType; void* nativeObject; + + // Constructor KeypadScriptData(int inActionType,void* inNativeObject) - :actionType(inActionType),nativeObject(inNativeObject) + : actionType(inActionType),nativeObject(inNativeObject) { } }; struct CommonScriptData { - //now,only use lua + // Now this struct is only used in LuaBinding. int handler; char eventName[64]; Object* eventSource; char eventSourceClassName[64]; + + // Constructor CommonScriptData(int inHandler,const char* inName,Object* inSource = NULL,const char* inClassName = NULL) - :handler(inHandler), - eventSource(inSource) + : handler(inHandler), + eventSource(inSource) { strncpy(eventName, inName, 64); @@ -248,9 +274,11 @@ struct ScriptEvent { ScriptEventType type; void* data; + + // Constructor ScriptEvent(ScriptEventType inType,void* inData) - :type(inType), - data(inData) + : type(inType), + data(inData) { } }; @@ -297,44 +325,13 @@ public: */ virtual int executeGlobalFunction(const char* functionName) = 0; - /** - @brief Execute a node event function - @param pNode which node produce this event - @param nAction kNodeOnEnter,kNodeOnExit,kMenuItemActivated,kNodeOnEnterTransitionDidFinish,kNodeOnExitTransitionDidStart - @return The integer value returned from the script function. - */ - virtual int executeNodeEvent(Node* pNode, int nAction) = 0; + //when trigger a script event ,call this func,add params needed into ScriptEvent object.nativeObject is object triggering the event, can be NULL in lua + virtual int sendEvent(ScriptEvent* evt) = 0; - virtual int executeMenuItemEvent(MenuItem* pMenuItem) = 0; - /** Execute a notification event function */ - virtual int executeNotificationEvent(NotificationCenter* pNotificationCenter, const char* pszName) = 0; - - /** execute a callfun event */ - virtual int executeCallFuncActionEvent(CallFunc* pAction, Object* target = NULL) = 0; - /** execute a schedule function */ - virtual int executeSchedule(int nHandler, float dt, Node* pNode = NULL) = 0; - - /** functions for executing touch event */ - virtual int executeLayerTouchesEvent(Layer* pLayer, int eventType, Set *pTouches) = 0; - virtual int executeLayerTouchEvent(Layer* pLayer, int eventType, Touch *pTouch) = 0; - - /** functions for keypad event */ - virtual int executeLayerKeypadEvent(Layer* pLayer, int eventType) = 0; - - /** execute a accelerometer event */ - virtual int executeAccelerometerEvent(Layer* pLayer, Acceleration* pAccelerationValue) = 0; - - /** function for common event */ - virtual int executeEvent(int nHandler, const char* pEventName, Object* pEventSource = NULL, const char* pEventSourceClassName = NULL) = 0; - /** called by CCAssert to allow scripting engine to handle failed assertions * @return true if the assert was handled by the script engine, false otherwise. */ virtual bool handleAssert(const char *msg) = 0; - - //when trigger a script event ,call this func,add params needed into ScriptEvent object.nativeObject is object triggering the event, can be NULL in lua - virtual int sendEvent(ScriptEvent* message){ return 0;} - // }; /** diff --git a/cocos2dx/support/CCNotificationCenter.cpp b/cocos2dx/support/CCNotificationCenter.cpp index 115a7318f5..4b275f10b6 100644 --- a/cocos2dx/support/CCNotificationCenter.cpp +++ b/cocos2dx/support/CCNotificationCenter.cpp @@ -192,7 +192,7 @@ void NotificationCenter::postNotification(const char *name, Object *object) { if (0 != observer->getHandler()) { - BasicScriptData data((void*)this,(void*)name); + BasicScriptData data(this, (void*)name); ScriptEvent scriptEvent(kNotificationEvent,(void*)&data); ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent); } diff --git a/extensions/GUI/CCControlExtension/CCControl.cpp b/extensions/GUI/CCControlExtension/CCControl.cpp index ea468d4a1b..00459f2d07 100644 --- a/extensions/GUI/CCControlExtension/CCControl.cpp +++ b/extensions/GUI/CCControlExtension/CCControl.cpp @@ -128,7 +128,7 @@ void Control::sendActionsForControlEvents(ControlEvent controlEvents) //Call ScriptFunc if (kScriptTypeLua == _scriptType) { - cocos2d::BasicScriptData data((void*)this,(void*)&controlEvents); + cocos2d::BasicScriptData data(this,(void*)&controlEvents); cocos2d::ScriptEvent event(cocos2d::kControlEvent,(void*)&data); cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event); } diff --git a/samples/Lua/TestLua/Resources/luaScript/ExtensionTest/ExtensionTest.lua b/samples/Lua/TestLua/Resources/luaScript/ExtensionTest/ExtensionTest.lua index 76b7d8a237..02a24eed9f 100644 --- a/samples/Lua/TestLua/Resources/luaScript/ExtensionTest/ExtensionTest.lua +++ b/samples/Lua/TestLua/Resources/luaScript/ExtensionTest/ExtensionTest.lua @@ -1120,6 +1120,7 @@ local function ExtensionsMainLayer() item:setPosition(s.width / 2, s.height - i * LINE_SPACE) menu:addChild(item, kItemTagBasic + i) if ((i == ExtensionTestEnum.TEST_WEBSOCKET + 1) and (false == bSupportWebSocket)) + or ( i == ExtensionTestEnum.TEST_TABLEVIEW + 1) or ((i == ExtensionTestEnum.TEST_EDITBOX + 1) and (false == bSupportEdit)) then item:setEnabled(false) end diff --git a/scripting/javascript/bindings/ScriptingCore.cpp b/scripting/javascript/bindings/ScriptingCore.cpp index fef8d63049..c307a84105 100644 --- a/scripting/javascript/bindings/ScriptingCore.cpp +++ b/scripting/javascript/bindings/ScriptingCore.cpp @@ -733,47 +733,66 @@ void ScriptingCore::cleanupSchedulesAndActions(js_proxy_t* p) } } -int ScriptingCore::executeNodeEvent(Node* pNode, int nAction) +int ScriptingCore::handleNodeEvent(void* data) { - js_proxy_t * p = jsb_get_native_proxy(pNode); + if (NULL == data) + return 0; + + BasicScriptData* basicScriptData = static_cast(data); + if (NULL == basicScriptData->nativeObject || NULL == basicScriptData->value) + return 0; + + Node* node = static_cast(basicScriptData->nativeObject); + int action = *((int*)(basicScriptData->value)); + + js_proxy_t * p = jsb_get_native_proxy(node); if (!p) return 0; jsval retval; jsval dataVal = INT_TO_JSVAL(1); - if(nAction == kNodeOnEnter) + if(action == kNodeOnEnter) { executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "onEnter", 1, &dataVal, &retval); resumeSchedulesAndActions(p); } - else if(nAction == kNodeOnExit) + else if(action == kNodeOnExit) { executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "onExit", 1, &dataVal, &retval); pauseSchedulesAndActions(p); } - else if(nAction == kNodeOnEnterTransitionDidFinish) + else if(action == kNodeOnEnterTransitionDidFinish) { executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "onEnterTransitionDidFinish", 1, &dataVal, &retval); } - else if(nAction == kNodeOnExitTransitionDidStart) + else if(action == kNodeOnExitTransitionDidStart) { executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "onExitTransitionDidStart", 1, &dataVal, &retval); } - else if(nAction == kNodeOnCleanup) { + else if(action == kNodeOnCleanup) { cleanupSchedulesAndActions(p); } return 1; } -int ScriptingCore::executeMenuItemEvent(MenuItem* pMenuItem) +int ScriptingCore::handleMenuClickedEvent(void* data) { - js_proxy_t * p = jsb_get_native_proxy(pMenuItem); + if (NULL == data) + return 0; + + BasicScriptData* basicScriptData = static_cast(data); + if (NULL == basicScriptData->nativeObject) + return 0; + + MenuItem* menuItem = static_cast(basicScriptData->nativeObject); + + js_proxy_t * p = jsb_get_native_proxy(menuItem); if (!p) return 0; jsval retval; jsval dataVal; - js_proxy_t *proxy = jsb_get_native_proxy(pMenuItem); + js_proxy_t *proxy = jsb_get_native_proxy(menuItem); dataVal = (proxy ? OBJECT_TO_JSVAL(proxy->obj) : JSVAL_NULL); executeJSFunctionFromReservedSpot(this->cx_, p->obj, dataVal, retval); @@ -781,31 +800,19 @@ int ScriptingCore::executeMenuItemEvent(MenuItem* pMenuItem) return 1; } -int ScriptingCore::executeNotificationEvent(NotificationCenter* pNotificationCenter, const char* pszName) -{ - return 1; -} - -int ScriptingCore::executeCallFuncActionEvent(CallFunc* pAction, Object* pTarget/* = NULL*/) -{ - return 1; -} - -int ScriptingCore::executeSchedule(int nHandler, float dt, Node* pNode/* = NULL*/) -{ - js_proxy_t * p = jsb_get_native_proxy(pNode); - if (!p) return 0; - - jsval retval; - jsval dataVal = DOUBLE_TO_JSVAL(dt); - - executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "update", 1, &dataVal, &retval); - - return 1; -} - -int ScriptingCore::executeLayerTouchesEvent(Layer* pLayer, int eventType, Set *pTouches) +int ScriptingCore::handleTouchesEvent(void* data) { + if (NULL == data) + return 0; + + TouchesScriptData* touchesScriptData = static_cast(data); + if (NULL == touchesScriptData->nativeObject || NULL == touchesScriptData->touches) + return 0; + + Layer* pLayer = static_cast(touchesScriptData->nativeObject); + int eventType = touchesScriptData->actionType; + Set *pTouches = touchesScriptData->touches; + std::string funcName = ""; getTouchesFuncName(eventType, funcName); @@ -833,8 +840,20 @@ int ScriptingCore::executeLayerTouchesEvent(Layer* pLayer, int eventType, Set *p return 1; } -int ScriptingCore::executeLayerTouchEvent(Layer* pLayer, int eventType, Touch *pTouch) +int ScriptingCore::handleTouchEvent(void* data) { + if (NULL == data) + return 0; + + TouchScriptData* touchScriptData = static_cast(data); + if (NULL == touchScriptData->nativeObject || NULL == touchScriptData->touch) + return 0; + + Layer* pLayer = static_cast(touchScriptData->nativeObject); + int eventType = touchScriptData->actionType; + Touch *pTouch = touchScriptData->touch; + + std::string funcName = ""; getTouchFuncName(eventType, funcName); @@ -897,23 +916,42 @@ JSBool ScriptingCore::executeFunctionWithOwner(jsval owner, const char *name, ui return bRet; } -int ScriptingCore::executeAccelerometerEvent(Layer *pLayer, Acceleration *pAccelerationValue) { - - jsval value = ccacceleration_to_jsval(this->getGlobalContext(), *pAccelerationValue); +int ScriptingCore::handleAccelerometerEvent(void* data) +{ + if (NULL == data) + return 0; + + BasicScriptData* basicScriptData = static_cast(data); + if (NULL == basicScriptData->nativeObject || NULL == basicScriptData->value) + return 0; + + Acceleration* accelerationValue = static_cast(basicScriptData->value); + Layer* layer = static_cast(basicScriptData->nativeObject); + + jsval value = ccacceleration_to_jsval(this->getGlobalContext(), *accelerationValue); JS_AddValueRoot(this->getGlobalContext(), &value); - executeFunctionWithObjectData(pLayer, "onAccelerometer", JSVAL_TO_OBJECT(value)); + executeFunctionWithObjectData(layer, "onAccelerometer", JSVAL_TO_OBJECT(value)); JS_RemoveValueRoot(this->getGlobalContext(), &value); return 1; } -int ScriptingCore::executeLayerKeypadEvent(Layer* pLayer, int eventType) +int ScriptingCore::handleKeypadEvent(void* data) { - js_proxy_t * p = jsb_get_native_proxy(pLayer); + if (NULL == data) + return 0; + + KeypadScriptData* keypadScriptData = static_cast(data); + if (NULL == keypadScriptData->nativeObject) + return 0; + + int action = keypadScriptData->actionType; + + js_proxy_t * p = jsb_get_native_proxy(keypadScriptData->nativeObject); if(p){ - switch(eventType){ + switch(action){ case kTypeBackClicked: executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "backClicked"); break; @@ -998,6 +1036,50 @@ int ScriptingCore::executeCustomTouchEvent(int eventType, } +int ScriptingCore::sendEvent(ScriptEvent* evt) +{ + if (NULL == evt) + return 0; + + switch (evt->type) + { + case kNodeEvent: + { + return handleNodeEvent(evt->data); + } + break; + case kMenuClickedEvent: + { + return handleMenuClickedEvent(evt->data); + } + break; + case kTouchEvent: + { + return handleTouchEvent(evt->data); + } + break; + case kTouchesEvent: + { + return handleTouchesEvent(evt->data); + } + break; + case kKeypadEvent: + { + return handleKeypadEvent(evt->data); + } + break; + case kAccelerometerEvent: + { + return handleAccelerometerEvent(evt->data); + } + break; + default: + break; + } + + return 0; +} + #pragma mark - Conversion Routines JSBool jsval_to_int32( JSContext *cx, jsval vp, int32_t *outval ) { diff --git a/scripting/javascript/bindings/ScriptingCore.h b/scripting/javascript/bindings/ScriptingCore.h index 14ca594e61..4a54bce2bb 100644 --- a/scripting/javascript/bindings/ScriptingCore.h +++ b/scripting/javascript/bindings/ScriptingCore.h @@ -87,16 +87,7 @@ public: */ virtual int executeGlobalFunction(const char* functionName) { return 0; } - virtual int executeNodeEvent(Node* pNode, int nAction); - virtual int executeMenuItemEvent(MenuItem* pMenuItem); - virtual int executeNotificationEvent(NotificationCenter* pNotificationCenter, const char* pszName); - virtual int executeCallFuncActionEvent(CallFunc* pAction, Object* pTarget = NULL); - virtual int executeSchedule(int nHandler, float dt, Node* pNode = NULL); - virtual int executeLayerTouchesEvent(Layer* pLayer, int eventType, Set *pTouches); - virtual int executeLayerTouchEvent(Layer* pLayer, int eventType, Touch *pTouch); - virtual int executeAccelerometerEvent(Layer* pLayer, Acceleration* pAccelerationValue); - virtual int executeLayerKeypadEvent(Layer* pLayer, int eventType); - virtual int executeEvent(int nHandler, const char* pEventName, Object* pEventSource = NULL, const char* pEventSourceClassName = NULL) { return 0; } + virtual int sendEvent(ScriptEvent* message) override; virtual bool handleAssert(const char *msg) { return false; } @@ -205,6 +196,13 @@ public: private: void string_report(jsval val); + + int handleTouchesEvent(void* data); + int handleTouchEvent(void* data); + int handleNodeEvent(void* data); + int handleMenuClickedEvent(void* data); + int handleAccelerometerEvent(void* data); + int handleKeypadEvent(void* data); }; // some utility functions diff --git a/scripting/lua/cocos2dx_support/CCLuaEngine.cpp b/scripting/lua/cocos2dx_support/CCLuaEngine.cpp index 3a9fae18b8..64d410828a 100644 --- a/scripting/lua/cocos2dx_support/CCLuaEngine.cpp +++ b/scripting/lua/cocos2dx_support/CCLuaEngine.cpp @@ -180,64 +180,70 @@ int LuaEngine::reallocateScriptHandler(int nHandler) return nRet; } -int LuaEngine::sendEvent(ScriptEvent* message) +int LuaEngine::sendEvent(ScriptEvent* evt) { - if (NULL == message) + if (NULL == evt) return 0; - switch (message->type) + + switch (evt->type) { case kNodeEvent: - { - return handleNodeEvent(message->data); - } - break; + { + return handleNodeEvent(evt->data); + } + break; case kMenuClickedEvent: - { - return handleMenuClickedEvent(message->data); - } - break; + { + return handleMenuClickedEvent(evt->data); + } + break; case kNotificationEvent: - { - return handleNotificationEvent(message->data); - } - break; + { + return handleNotificationEvent(evt->data); + } + break; case kCallFuncEvent: - { - return handleCallFuncActionEvent(message->data); - } - break; + { + return handleCallFuncActionEvent(evt->data); + } + break; case kScheduleEvent: - { - return handleScheduler(message->data); - } - break; + { + return handleScheduler(evt->data); + } + break; + case kTouchEvent: + { + return handleTouchEvent(evt->data); + } + break; case kTouchesEvent: - { - return handleTouchesEvent(message->data); - } - break; + { + return handleTouchesEvent(evt->data); + } + break; case kKeypadEvent: - { - return handleKeypadEvent(message->data); - } - break; + { + return handleKeypadEvent(evt->data); + } + break; case kAccelerometerEvent: - { - return handleAccelerometerEvent(message->data); - } - break; + { + return handleAccelerometerEvent(evt->data); + } + break; case kCommonEvent: - { - return handleCommonEvent(message->data); - } - break; + { + return handleCommonEvent(evt->data); + } + break; case kControlEvent: - { - return handlerControlEvent(message->data); - } - break; + { + return handlerControlEvent(evt->data); + } + break; default: - break; + break; } return 0; @@ -297,9 +303,9 @@ int LuaEngine::handleMenuClickedEvent(void* data) if (NULL == basicScriptData->nativeObject) return 0; - MenuItem* menuItem = (MenuItem*)(basicScriptData->nativeObject); + MenuItem* menuItem = static_cast(basicScriptData->nativeObject); - int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)menuItem, ScriptHandlerMgr::kMenuClickHandler); + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(menuItem, ScriptHandlerMgr::kMenuClickHandler); if (0 == handler) return 0; @@ -319,7 +325,7 @@ int LuaEngine::handleNotificationEvent(void* data) if (NULL == basicScriptData->nativeObject ||NULL == basicScriptData->value) return 0; - NotificationCenter* center = (NotificationCenter*)(basicScriptData->nativeObject); + NotificationCenter* center = static_cast(basicScriptData->nativeObject); int handler = center->getObserverHandlerByName((const char*)basicScriptData->value); @@ -337,7 +343,7 @@ int LuaEngine::handleCallFuncActionEvent(void* data) if (NULL == data) return 0; - BasicScriptData* basicScriptData = (BasicScriptData*)(data); + BasicScriptData* basicScriptData = static_cast(data); if (NULL == basicScriptData->nativeObject) return 0; @@ -346,7 +352,7 @@ int LuaEngine::handleCallFuncActionEvent(void* data) if (0 == handler) return 0; - Object* target = (Object*)(basicScriptData->value); + Object* target = static_cast(basicScriptData->value); if (NULL != target) { _stack->pushObject(target, "CCNode"); @@ -361,7 +367,7 @@ int LuaEngine::handleScheduler(void* data) if (NULL == data) return 0; - SchedulerScriptData* schedulerInfo = (SchedulerScriptData*)data; + SchedulerScriptData* schedulerInfo = static_cast(data); _stack->pushFloat(schedulerInfo->elapse); int ret = _stack->executeFunctionByHandler(schedulerInfo->handler, 1); @@ -375,11 +381,11 @@ int LuaEngine::handleKeypadEvent(void* data) if (NULL == data) return 0; - KeypadScriptData* keypadScriptData = (KeypadScriptData*)data; + KeypadScriptData* keypadScriptData = static_cast(data); if (NULL == keypadScriptData->nativeObject) return 0; - int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)keypadScriptData->nativeObject, ScriptHandlerMgr::kKeypadHandler); + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(keypadScriptData->nativeObject, ScriptHandlerMgr::kKeypadHandler); if (0 == handler) return 0; @@ -409,7 +415,7 @@ int LuaEngine::handleAccelerometerEvent(void* data) if (NULL == data) return 0; - BasicScriptData* basicScriptData = (BasicScriptData*)data; + BasicScriptData* basicScriptData = static_cast(data); if (NULL == basicScriptData->nativeObject || NULL == basicScriptData->value) return 0; @@ -417,7 +423,7 @@ int LuaEngine::handleAccelerometerEvent(void* data) if (0 == handler) return 0; - Acceleration* accelerationValue = (Acceleration*)(basicScriptData->value); + Acceleration* accelerationValue = static_cast(basicScriptData->value); _stack->pushFloat(accelerationValue->x); _stack->pushFloat(accelerationValue->y); _stack->pushFloat(accelerationValue->z); @@ -432,7 +438,7 @@ int LuaEngine::handleCommonEvent(void* data) if (NULL == data) return 0; - CommonScriptData* commonInfo = (CommonScriptData*)data; + CommonScriptData* commonInfo = static_cast(data); if (NULL == commonInfo->eventName || 0 == commonInfo->handler) return 0; @@ -453,12 +459,61 @@ int LuaEngine::handleCommonEvent(void* data) return ret; } +int LuaEngine::handleTouchEvent(void* data) +{ + if (NULL == data) + return 0; + + TouchScriptData* touchScriptData = static_cast(data); + if (NULL == touchScriptData->nativeObject || NULL == touchScriptData->touch) + return 0; + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)touchScriptData->nativeObject, ScriptHandlerMgr::kTouchesHandler); + + if (0 == handler) + return 0; + + switch (touchScriptData->actionType) + { + case CCTOUCHBEGAN: + _stack->pushString("began"); + break; + + case CCTOUCHMOVED: + _stack->pushString("moved"); + break; + + case CCTOUCHENDED: + _stack->pushString("ended"); + break; + + case CCTOUCHCANCELLED: + _stack->pushString("cancelled"); + break; + + default: + return 0; + } + + int ret = 0; + + Touch* touch = touchScriptData->touch; + if (NULL != touch) { + const Point pt = Director::getInstance()->convertToGL(touch->getLocationInView()); + _stack->pushFloat(pt.x); + _stack->pushFloat(pt.y); + ret = _stack->executeFunctionByHandler(handler, 3); + } + _stack->clean(); + return ret; +} + int LuaEngine::handleTouchesEvent(void* data) { if (NULL == data) return 0; - TouchesScriptData* touchesScriptData = (TouchesScriptData*)data; + TouchesScriptData* touchesScriptData = static_cast(data); if (NULL == touchesScriptData->nativeObject || NULL == touchesScriptData->touches) return 0; @@ -491,35 +546,23 @@ int LuaEngine::handleTouchesEvent(void* data) Director* pDirector = Director::getInstance(); lua_State *L = _stack->getLuaState(); - int count = touchesScriptData->touches->count(); int ret = 0; - if (count == 1) + + lua_newtable(L); + int i = 1; + for (SetIterator it = touchesScriptData->touches->begin(); it != touchesScriptData->touches->end(); ++it) { - Touch* touch = (Touch*)*(touchesScriptData->touches->begin()); - if (NULL != touch) { - const Point pt = Director::getInstance()->convertToGL(touch->getLocationInView()); - _stack->pushFloat(pt.x); - _stack->pushFloat(pt.y); - ret = _stack->executeFunctionByHandler(handler, 3); - } - } - else if(count > 1) - { - lua_newtable(L); - int i = 1; - for (SetIterator it = touchesScriptData->touches->begin(); it != touchesScriptData->touches->end(); ++it) - { - Touch* pTouch = (Touch*)*it; - Point pt = pDirector->convertToGL(pTouch->getLocationInView()); - lua_pushnumber(L, pt.x); - lua_rawseti(L, -2, i++); - lua_pushnumber(L, pt.y); - lua_rawseti(L, -2, i++); - lua_pushinteger(L, pTouch->getID()); - lua_rawseti(L, -2, i++); - } - ret = _stack->executeFunctionByHandler(handler, 2); + Touch* pTouch = static_cast(*it); + Point pt = pDirector->convertToGL(pTouch->getLocationInView()); + lua_pushnumber(L, pt.x); + lua_rawseti(L, -2, i++); + lua_pushnumber(L, pt.y); + lua_rawseti(L, -2, i++); + lua_pushinteger(L, pTouch->getID()); + lua_rawseti(L, -2, i++); } + ret = _stack->executeFunctionByHandler(handler, 2); + _stack->clean(); return ret; } @@ -529,7 +572,7 @@ int LuaEngine::handlerControlEvent(void* data) if ( NULL == data ) return 0; - BasicScriptData* basicScriptData = (BasicScriptData*)data; + BasicScriptData* basicScriptData = static_cast(data); if (NULL == basicScriptData->nativeObject) return 0; diff --git a/scripting/lua/cocos2dx_support/CCLuaEngine.h b/scripting/lua/cocos2dx_support/CCLuaEngine.h index beacf42d49..1a3754d33f 100644 --- a/scripting/lua/cocos2dx_support/CCLuaEngine.h +++ b/scripting/lua/cocos2dx_support/CCLuaEngine.h @@ -134,6 +134,7 @@ private: int handleKeypadEvent(void* data); int handleAccelerometerEvent(void* data); int handleCommonEvent(void* data); + int handleTouchEvent(void* data); int handleTouchesEvent(void* data); int handlerControlEvent(void* data); void extendNode(lua_State* lua_S);