From 756a72cbcae356704bdf8fd61b90345fa1b04b09 Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Thu, 26 Dec 2013 19:43:49 +0800 Subject: [PATCH] Reconstruct the operation of handling lua script event --- cocos/2d/CCScriptSupport.h | 2 - cocos/scripting/lua/bindings/CCLuaEngine.cpp | 345 ++++++++++-------- cocos/scripting/lua/bindings/CCLuaEngine.h | 10 +- cocos/scripting/lua/bindings/CCLuaStack.cpp | 69 ++++ cocos/scripting/lua/bindings/CCLuaStack.h | 1 + .../lua_cocos2dx_extension_manual.cpp | 105 +++--- .../bindings/lua_cocos2dx_extension_manual.h | 10 +- 7 files changed, 314 insertions(+), 228 deletions(-) diff --git a/cocos/2d/CCScriptSupport.h b/cocos/2d/CCScriptSupport.h index 3ab4d31358..e1b676c9ea 100644 --- a/cocos/2d/CCScriptSupport.h +++ b/cocos/2d/CCScriptSupport.h @@ -210,8 +210,6 @@ enum ScriptEventType kAccelerometerEvent, kControlEvent, kCommonEvent, - kTableViewEvent,//Now it's only used in LuaBinding - kAssetsManagerEvent,//Now it's only used in Lua Binding kCocoStudioEventListener,//Now it's only used in Lua Binding kArmatureWrapper,//Now it's only used in Lua Binding kEventListenerAcc,//Now it's only used in Lua Binding diff --git a/cocos/scripting/lua/bindings/CCLuaEngine.cpp b/cocos/scripting/lua/bindings/CCLuaEngine.cpp index 7edc81b366..1d5bf6d12c 100644 --- a/cocos/scripting/lua/bindings/CCLuaEngine.cpp +++ b/cocos/scripting/lua/bindings/CCLuaEngine.cpp @@ -27,7 +27,6 @@ #include "cocos2d.h" #include "CCArray.h" #include "CCScheduler.h" -#include "LuaScriptHandlerMgr.h" #include "extensions/GUI/CCControlExtension/CCControl.h" #include "LuaOpengl.h" #include "lua_cocos2dx_manual.hpp" @@ -249,16 +248,6 @@ int LuaEngine::sendEvent(ScriptEvent* evt) return handlerControlEvent(evt->data); } break; - case kTableViewEvent: - { - return handleTableViewEvent(evt->data); - } - break; - case kAssetsManagerEvent: - { - return handleAssetsManagerEvent(evt->data); - } - break; case kCocoStudioEventListener: { return handleCocoStudioEventListener(evt->data); @@ -622,51 +611,6 @@ int LuaEngine::handleTouchesEvent(void* data) return ret; } -int LuaEngine::handleTableViewEvent(void* data) -{ - if (nullptr == data) - return 0; - - BasicScriptData* eventData = static_cast(data); - if (nullptr == eventData->nativeObject || nullptr == eventData->value) - return 0; - - LuaTableViewEventData* tableViewEventData = static_cast(eventData->value); - if (tableViewEventData->handlerType < ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL || tableViewEventData->handlerType > ScriptHandlerMgr::HandlerType::TABLEVIEW_NUMS_OF_CELLS ) - return 0; - - int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, tableViewEventData->handlerType); - - if (0 == handler) - return 0; - - int ret = 0; - switch (tableViewEventData->handlerType) - { - case ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL: - case ScriptHandlerMgr::HandlerType::SCROLLVIEW_ZOOM: - { - _stack->pushObject(static_cast(eventData->nativeObject), "TableView"); - ret = _stack->executeFunctionByHandler(handler, 1); - } - break; - case ScriptHandlerMgr::HandlerType::TABLECELL_TOUCHED: - case ScriptHandlerMgr::HandlerType::TABLECELL_HIGHLIGHT: - case ScriptHandlerMgr::HandlerType::TABLECELL_UNHIGHLIGHT: - case ScriptHandlerMgr::HandlerType::TABLECELL_WILL_RECYCLE: - { - _stack->pushObject(static_cast(eventData->nativeObject), "TableView"); - _stack->pushObject(static_cast(tableViewEventData->value), "TableViewCell"); - ret = _stack->executeFunctionByHandler(handler, 2); - } - break; - default: - break; - } - - return ret; -} - int LuaEngine::handlerControlEvent(void* data) { if ( NULL == data ) @@ -705,111 +649,10 @@ int LuaEngine::sendEventReturnArray(ScriptEvent* message,int numResults,Array& r { if (nullptr == message || numResults <= 0) return 0; - - switch (message->type) - { - case kTableViewEvent: - return handleTableViewEventReturnArray(message->data,numResults,resultArray); - break; - default: - break; - } + return 0; } -int LuaEngine::handleTableViewEventReturnArray(void* data,int numResults,Array& resultArray) -{ - if (nullptr == data || numResults <= 0) - return 0; - - if (nullptr == data) - return 0; - - BasicScriptData* eventData = static_cast(data); - if (nullptr == eventData->nativeObject || nullptr == eventData->value) - return 0; - - LuaTableViewEventData* tableViewEventData = static_cast(eventData->value); - if (tableViewEventData->handlerType < ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL || tableViewEventData->handlerType > ScriptHandlerMgr::HandlerType::TABLEVIEW_NUMS_OF_CELLS ) - return 0; - - int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, tableViewEventData->handlerType); - - if (0 == handler) - return 0; - - int ret = 0; - switch (tableViewEventData->handlerType) - { - case ScriptHandlerMgr::HandlerType::TABLECELL_SIZE_FOR_INDEX: - { - _stack->pushObject(static_cast(eventData->nativeObject), "TableView"); - _stack->pushLong(*((long*)tableViewEventData->value)); - ret = _stack->executeFunctionReturnArray(handler, 2, 2, resultArray); - } - break; - case ScriptHandlerMgr::HandlerType::TABLECELL_AT_INDEX: - { - _stack->pushObject(static_cast(eventData->nativeObject), "TableView"); - _stack->pushLong(*((long*)tableViewEventData->value)); - ret = _stack->executeFunctionReturnArray(handler, 2, 1, resultArray); - } - break; - case ScriptHandlerMgr::HandlerType::TABLEVIEW_NUMS_OF_CELLS: - { - _stack->pushObject(static_cast(eventData->nativeObject), "TableView"); - ret = _stack->executeFunctionReturnArray(handler, 1, 1, resultArray); - } - break; - default: - break; - } - - return ret; -} - -int LuaEngine::handleAssetsManagerEvent(void* data) -{ - if (nullptr == data) - return 0; - - BasicScriptData* eventData = static_cast(data); - if (nullptr == eventData->nativeObject || nullptr == eventData->value) - return 0; - - LuaAssetsManagerEventData* assetsManagerEventData = static_cast(eventData->value); - if (assetsManagerEventData->handlerType < ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS || assetsManagerEventData->handlerType > ScriptHandlerMgr::HandlerType::ASSETSMANAGER_ERROR ) - return 0; - - int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, assetsManagerEventData->handlerType); - - if (0 == handler) - return 0; - - int ret = 0; - switch (assetsManagerEventData->handlerType) - { - case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS: - case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_ERROR: - { - _stack->pushInt(assetsManagerEventData->value); - ret = _stack->executeFunctionByHandler(handler, 1); - } - break; - - case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_SUCCESS: - { - ret = _stack->executeFunctionByHandler(handler, 0); - } - break; - - default: - break; - } - - return ret; -} - int LuaEngine::handleCocoStudioEventListener(void* data) { @@ -1046,4 +889,190 @@ int LuaEngine::handleEventListenerCustom(void* data) return ret; } +int LuaEngine::handleEvent(ScriptHandlerMgr::HandlerType type,void* data) +{ + switch (type) + { + case ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL: + case ScriptHandlerMgr::HandlerType::SCROLLVIEW_ZOOM: + case ScriptHandlerMgr::HandlerType::TABLECELL_TOUCHED: + case ScriptHandlerMgr::HandlerType::TABLECELL_HIGHLIGHT: + case ScriptHandlerMgr::HandlerType::TABLECELL_UNHIGHLIGHT: + case ScriptHandlerMgr::HandlerType::TABLECELL_WILL_RECYCLE: + { + return handleTableViewEvent(type, data); + } + break; + case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS: + case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_ERROR: + case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_SUCCESS: + { + return handleAssetsManagerEvent(type, data); + } + break; + + default: + break; + } + + return 0; +} + +int LuaEngine::handleEvent(ScriptHandlerMgr::HandlerType type, void* data, int numResults, const std::function& func) +{ + switch (type) + { + case ScriptHandlerMgr::HandlerType::TABLECELL_SIZE_FOR_INDEX: + case ScriptHandlerMgr::HandlerType::TABLECELL_AT_INDEX: + case ScriptHandlerMgr::HandlerType::TABLEVIEW_NUMS_OF_CELLS: + { + return handlerTableViewEvent(type, data, numResults,func); + } + break; + default: + break; + } + + return 0; +} + +int LuaEngine::handleTableViewEvent(ScriptHandlerMgr::HandlerType type,void* data) +{ + if (nullptr == data) + return 0; + + BasicScriptData* eventData = static_cast(data); + if (nullptr == eventData->nativeObject || nullptr == eventData->value) + return 0; + + LuaTableViewEventData* tableViewData = static_cast(eventData->value); + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, type); + + if (0 == handler) + return 0; + + Object* obj = static_cast(eventData->nativeObject); + if (nullptr == obj) + return 0; + + int ret = 0; + switch (type) + { + case ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL: + case ScriptHandlerMgr::HandlerType::SCROLLVIEW_ZOOM: + { + toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"TableView"); + ret = _stack->executeFunctionByHandler(handler, 1); + } + break; + case ScriptHandlerMgr::HandlerType::TABLECELL_TOUCHED: + case ScriptHandlerMgr::HandlerType::TABLECELL_HIGHLIGHT: + case ScriptHandlerMgr::HandlerType::TABLECELL_UNHIGHLIGHT: + case ScriptHandlerMgr::HandlerType::TABLECELL_WILL_RECYCLE: + { + Object* cellObject = static_cast(tableViewData->value); + if (nullptr == cellObject) { + break; + } + toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"TableView"); + toluafix_pushusertype_ccobject(_stack->getLuaState(), cellObject->_ID, &(cellObject->_luaID), (void*)(cellObject),"TableViewCell"); + ret = _stack->executeFunctionByHandler(handler, 2); + } + break; + default: + break; + } + + return ret; +} + +int LuaEngine::handlerTableViewEvent(ScriptHandlerMgr::HandlerType handlerType,void* data, int numResults, const std::function func) +{ + if (nullptr == data || numResults <= 0) + return 0; + + BasicScriptData* eventData = static_cast(data); + if (nullptr == eventData->nativeObject || nullptr == eventData->value) + return 0; + + LuaTableViewEventData* tableViewData = static_cast(eventData->value); + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, handlerType); + + if (0 == handler) + return 0; + + Object* obj = static_cast(eventData->nativeObject); + if (nullptr == obj) + return 0; + + int ret = 0; + switch (handlerType) + { + case ScriptHandlerMgr::HandlerType::TABLECELL_SIZE_FOR_INDEX: + { + toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"TableView"); + _stack->pushLong(*((long*)tableViewData->value)); + ret = _stack->executeFunction(handler, 2, 2, func); + } + break; + case ScriptHandlerMgr::HandlerType::TABLECELL_AT_INDEX: + { + toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"TableView"); + _stack->pushLong(*((long*)tableViewData->value)); + ret = _stack->executeFunction(handler, 2, 1, func); + } + break; + case ScriptHandlerMgr::HandlerType::TABLEVIEW_NUMS_OF_CELLS: + { + toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"TableView"); + ret = _stack->executeFunction(handler, 1, 1, func); + } + break; + default: + break; + } + + return ret; +} + +int LuaEngine::handleAssetsManagerEvent(ScriptHandlerMgr::HandlerType type,void* data) +{ + if (nullptr == data) + return 0; + + BasicScriptData* eventData = static_cast(data); + if (nullptr == eventData->nativeObject || nullptr == eventData->value) + return 0; + + LuaAssetsManagerEventData* assetsManagerData = static_cast(eventData->value); + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, type); + + if (0 == handler) + return 0; + + int ret = 0; + switch (type) + { + case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS: + case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_ERROR: + { + _stack->pushInt(assetsManagerData->value); + ret = _stack->executeFunctionByHandler(handler, 1); + } + break; + + case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_SUCCESS: + { + ret = _stack->executeFunctionByHandler(handler, 0); + } + break; + + default: + break; + } + + return ret; +} + NS_CC_END diff --git a/cocos/scripting/lua/bindings/CCLuaEngine.h b/cocos/scripting/lua/bindings/CCLuaEngine.h index 12be7885d3..4b4bd95817 100644 --- a/cocos/scripting/lua/bindings/CCLuaEngine.h +++ b/cocos/scripting/lua/bindings/CCLuaEngine.h @@ -33,6 +33,7 @@ extern "C" { #include "CCScriptSupport.h" #include "CCLuaStack.h" #include "CCLuaValue.h" +#include "LuaScriptHandlerMgr.h" NS_CC_BEGIN @@ -117,6 +118,8 @@ public: virtual int sendEvent(ScriptEvent* message); virtual int sendEventReturnArray(ScriptEvent* message,int numResults,Array& resultArray); + virtual int handleEvent(ScriptHandlerMgr::HandlerType type,void* data); + virtual int handleEvent(ScriptHandlerMgr::HandlerType type, void* data, int numResults, const std::function& func); private: LuaEngine(void) : _stack(NULL) @@ -134,9 +137,6 @@ private: int handleTouchEvent(void* data); int handleTouchesEvent(void* data); int handlerControlEvent(void* data); - int handleTableViewEvent(void* data); - int handleTableViewEventReturnArray(void* data,int numResults,Array& resultArray); - int handleAssetsManagerEvent(void* data); int handleCocoStudioEventListener(void* data); int handleArmatureWrapper(void* data); int handleEventListenerAcc(void* data); @@ -145,6 +145,10 @@ private: int handleEventListenerTouches(void* data); int handleEventListenerMouse(void* data); int handleEventListenerCustom(void* data); + + int handleAssetsManagerEvent(ScriptHandlerMgr::HandlerType type,void* data); + int handleTableViewEvent(ScriptHandlerMgr::HandlerType type,void* data); + int handlerTableViewEvent(ScriptHandlerMgr::HandlerType type,void* data, int numResults, const std::function func); private: static LuaEngine* _defaultEngine; LuaStack *_stack; diff --git a/cocos/scripting/lua/bindings/CCLuaStack.cpp b/cocos/scripting/lua/bindings/CCLuaStack.cpp index 098c3e63ef..8464bef338 100644 --- a/cocos/scripting/lua/bindings/CCLuaStack.cpp +++ b/cocos/scripting/lua/bindings/CCLuaStack.cpp @@ -577,4 +577,73 @@ int LuaStack::executeFunctionReturnArray(int handler,int numArgs,int numResults, return 1; } +int LuaStack::executeFunction(int handler, int numArgs, int numResults, const std::function& func) +{ + if (pushFunctionByHandler(handler)) /* L: ... arg1 arg2 ... func */ + { + if (numArgs > 0) + { + lua_insert(_state, -(numArgs + 1)); /* L: ... func arg1 arg2 ... */ + } + + int functionIndex = -(numArgs + 1); + + if (!lua_isfunction(_state, functionIndex)) + { + CCLOG("value at stack [%d] is not function", functionIndex); + lua_pop(_state, numArgs + 1); // remove function and arguments + return 0; + } + + int traceCallback = 0; + lua_getglobal(_state, "__G__TRACKBACK__"); /* L: ... func arg1 arg2 ... G */ + if (!lua_isfunction(_state, -1)) + { + lua_pop(_state, 1); /* L: ... func arg1 arg2 ... */ + } + else + { + lua_insert(_state, functionIndex - 1); /* L: ... G func arg1 arg2 ... */ + traceCallback = functionIndex - 1; + } + + int error = 0; + ++_callFromLua; + error = lua_pcall(_state, numArgs, numResults, traceCallback); /* L: ... [G] ret1 ret2 ... retResults*/ + --_callFromLua; + + if (error) + { + if (traceCallback == 0) + { + CCLOG("[LUA ERROR] %s", lua_tostring(_state, - 1)); /* L: ... error */ + lua_pop(_state, 1); // remove error message from stack + } + else /* L: ... G error */ + { + lua_pop(_state, 2); // remove __G__TRACKBACK__ and error message from stack + } + return 0; + } + + // get return value,don't pass LUA_MULTRET to numResults, + if (numResults <= 0) + return 0; + + if (nullptr == func) + return 0; + + func(_state,numResults); + + if (traceCallback) + { + lua_pop(_state, 1); // remove __G__TRACKBACK__ from stack /* L: ... */ + } + } + + lua_settop(_state, 0); + + return 1; +} + NS_CC_END diff --git a/cocos/scripting/lua/bindings/CCLuaStack.h b/cocos/scripting/lua/bindings/CCLuaStack.h index 37088dd886..e48d7bb9d3 100644 --- a/cocos/scripting/lua/bindings/CCLuaStack.h +++ b/cocos/scripting/lua/bindings/CCLuaStack.h @@ -116,6 +116,7 @@ public: virtual int executeFunctionByHandler(int nHandler, int numArgs); virtual int executeFunctionReturnArray(int handler,int numArgs,int numResults,Array& resultArray); + virtual int executeFunction(int handler, int numArgs, int numResults, const std::function& func); virtual bool handleAssert(const char *msg); diff --git a/cocos/scripting/lua/bindings/lua_cocos2dx_extension_manual.cpp b/cocos/scripting/lua/bindings/lua_cocos2dx_extension_manual.cpp index fd58ccd5ba..8762825260 100644 --- a/cocos/scripting/lua/bindings/lua_cocos2dx_extension_manual.cpp +++ b/cocos/scripting/lua/bindings/lua_cocos2dx_extension_manual.cpp @@ -877,10 +877,9 @@ public: int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)view, ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL); if (0 != handler) { - LuaTableViewEventData eventData(ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL); + LuaTableViewEventData eventData; BasicScriptData data(view,&eventData); - ScriptEvent event(kTableViewEvent,(void*)&data); - LuaEngine::getInstance()->sendEvent(&event); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL, (void*)&data); } } } @@ -892,10 +891,9 @@ public: int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)view, ScriptHandlerMgr::HandlerType::SCROLLVIEW_ZOOM); if (0 != handler) { - LuaTableViewEventData eventData(ScriptHandlerMgr::HandlerType::SCROLLVIEW_ZOOM); + LuaTableViewEventData eventData; BasicScriptData data(view,&eventData); - ScriptEvent event(kTableViewEvent,(void*)&data); - LuaEngine::getInstance()->sendEvent(&event); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::SCROLLVIEW_ZOOM, (void*)&data); } } } @@ -907,10 +905,9 @@ public: int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)table, ScriptHandlerMgr::HandlerType::TABLECELL_TOUCHED); if (0 != handler) { - LuaTableViewEventData eventData(ScriptHandlerMgr::HandlerType::TABLECELL_TOUCHED,cell); + LuaTableViewEventData eventData(cell); BasicScriptData data(table,&eventData); - ScriptEvent event(kTableViewEvent,(void*)&data); - LuaEngine::getInstance()->sendEvent(&event); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::TABLECELL_TOUCHED,(void*)&data); } } } @@ -922,10 +919,9 @@ public: int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)table, ScriptHandlerMgr::HandlerType::TABLECELL_HIGHLIGHT); if (0 != handler) { - LuaTableViewEventData eventData(ScriptHandlerMgr::HandlerType::TABLECELL_HIGHLIGHT,cell); + LuaTableViewEventData eventData(cell); BasicScriptData data(table,&eventData); - ScriptEvent event(kTableViewEvent,(void*)&data); - LuaEngine::getInstance()->sendEvent(&event); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::TABLECELL_HIGHLIGHT,(void*)&data); } } } @@ -937,10 +933,9 @@ public: int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)table, ScriptHandlerMgr::HandlerType::TABLECELL_UNHIGHLIGHT); if (0 != handler) { - LuaTableViewEventData eventData(ScriptHandlerMgr::HandlerType::TABLECELL_UNHIGHLIGHT,cell); + LuaTableViewEventData eventData(cell); BasicScriptData data(table,&eventData); - ScriptEvent event(kTableViewEvent,(void*)&data); - LuaEngine::getInstance()->sendEvent(&event); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::TABLECELL_UNHIGHLIGHT,(void*)&data); } } } @@ -952,10 +947,9 @@ public: int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)table, ScriptHandlerMgr::HandlerType::TABLECELL_WILL_RECYCLE); if (0 != handler) { - LuaTableViewEventData eventData(ScriptHandlerMgr::HandlerType::TABLECELL_WILL_RECYCLE,cell); + LuaTableViewEventData eventData(cell); BasicScriptData data(table,&eventData); - ScriptEvent event(kTableViewEvent,(void*)&data); - LuaEngine::getInstance()->sendEvent(&event); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::TABLECELL_WILL_RECYCLE,(void*)&data); } } } @@ -1033,19 +1027,20 @@ public: int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)table, ScriptHandlerMgr::HandlerType::TABLECELL_SIZE_FOR_INDEX); if (0 != handler) { - Array resultArray; - resultArray.initWithCapacity(1); - LuaTableViewEventData eventData(ScriptHandlerMgr::HandlerType::TABLECELL_SIZE_FOR_INDEX,&idx); + LuaTableViewEventData eventData(&idx); BasicScriptData data(table,&eventData); - ScriptEvent event(kTableViewEvent,(void*)&data); - LuaEngine::getInstance()->sendEventReturnArray(&event, 2, resultArray); - CCASSERT(resultArray.count() == 2, "tableCellSizeForIndex Array count error"); - Double* width = dynamic_cast(resultArray.getObjectAtIndex(0)); - Double* height = dynamic_cast(resultArray.getObjectAtIndex(1)); - if (nullptr != width && nullptr != height) - { - return Size((float)width->getValue(), (float)height->getValue()); - } + float width = 0.0; + float height = 0.0; + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::TABLECELL_SIZE_FOR_INDEX, (void*)&data,2,[&](lua_State* L,int numReturn){ + CCASSERT(numReturn == 2, "tableCellSizeForIndex return count error"); + ValueVector vec; + width = (float)tolua_tonumber(L, -1, 0); + lua_pop(L, 1); + height = (float)tolua_tonumber(L, -1, 0); + lua_pop(L, 1); + }); + + return Size(width, height); } } @@ -1059,18 +1054,15 @@ public: int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)table, ScriptHandlerMgr::HandlerType::TABLECELL_AT_INDEX); if (0 != handler) { - Array resultArray; - resultArray.initWithCapacity(1); - LuaTableViewEventData eventData(ScriptHandlerMgr::HandlerType::TABLECELL_AT_INDEX,&idx); + LuaTableViewEventData eventData(&idx); BasicScriptData data(table,&eventData); - ScriptEvent event(kTableViewEvent,(void*)&data); - LuaEngine::getInstance()->sendEventReturnArray(&event, 1, resultArray); TableViewCell* viewCell = nullptr; - if (resultArray.count() > 0) - { - viewCell = dynamic_cast(resultArray.getObjectAtIndex(0)); - } - + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::TABLECELL_AT_INDEX, (void*)&data, 1, [&](lua_State* L, int numReturn){ + CCASSERT(numReturn == 1, "tableCellAtIndex return count error"); + viewCell = static_cast(tolua_tousertype(L, -1, nullptr)); + lua_pop(L, 1); + }); + return viewCell; } } @@ -1085,17 +1077,15 @@ public: int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)table, ScriptHandlerMgr::HandlerType::TABLEVIEW_NUMS_OF_CELLS); if (0 != handler) { - Array resultArray; - resultArray.initWithCapacity(1); - LuaTableViewEventData eventData(ScriptHandlerMgr::HandlerType::TABLEVIEW_NUMS_OF_CELLS); + LuaTableViewEventData eventData; BasicScriptData data(table,&eventData); - ScriptEvent event(kTableViewEvent,(void*)&data); - LuaEngine::getInstance()->sendEventReturnArray(&event, 1, resultArray); - Double* numbers = dynamic_cast(resultArray.getObjectAtIndex(0)); - if (NULL != numbers) - { - return (long)numbers->getValue(); - } + long counts = 0; + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::TABLEVIEW_NUMS_OF_CELLS, (void*)&data,1, [&](lua_State* L, int numReturn){ + CCASSERT(numReturn == 1, "numberOfCellsInTableView return count error"); + counts = (long)tolua_tonumber(L, -1, 0); + lua_pop(L, 1); + }); + return counts; } } return 0; @@ -1444,10 +1434,9 @@ public: int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS); if (0 != handler) { - LuaAssetsManagerEventData eventData(ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS,percent); + LuaAssetsManagerEventData eventData(percent); BasicScriptData data((void*)this,&eventData); - ScriptEvent event(kAssetsManagerEvent,(void*)&data); - LuaEngine::getInstance()->sendEvent(&event); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS, (void*)&data); } } @@ -1456,10 +1445,9 @@ public: int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, ScriptHandlerMgr::HandlerType::ASSETSMANAGER_SUCCESS); if (0 != handler) { - LuaAssetsManagerEventData eventData(ScriptHandlerMgr::HandlerType::ASSETSMANAGER_SUCCESS); + LuaAssetsManagerEventData eventData; BasicScriptData data((void*)this,&eventData); - ScriptEvent event(kAssetsManagerEvent,(void*)&data); - LuaEngine::getInstance()->sendEvent(&event); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::ASSETSMANAGER_SUCCESS, (void*)&data); } } @@ -1468,10 +1456,9 @@ public: int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, ScriptHandlerMgr::HandlerType::ASSETSMANAGER_ERROR); if (0 != handler) { - LuaAssetsManagerEventData eventData(ScriptHandlerMgr::HandlerType::ASSETSMANAGER_ERROR, (int)errorCode); + LuaAssetsManagerEventData eventData((int)errorCode); BasicScriptData data((void*)this,&eventData); - ScriptEvent event(kAssetsManagerEvent,(void*)&data); - LuaEngine::getInstance()->sendEvent(&event); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::ASSETSMANAGER_ERROR, (void*)&data); } } }; diff --git a/cocos/scripting/lua/bindings/lua_cocos2dx_extension_manual.h b/cocos/scripting/lua/bindings/lua_cocos2dx_extension_manual.h index 2c8b22eddb..39ebdaa7a2 100644 --- a/cocos/scripting/lua/bindings/lua_cocos2dx_extension_manual.h +++ b/cocos/scripting/lua/bindings/lua_cocos2dx_extension_manual.h @@ -16,22 +16,20 @@ TOLUA_API int register_cocos2dx_extension_CCBProxy(lua_State* tolua_S); struct LuaTableViewEventData { - cocos2d::ScriptHandlerMgr::HandlerType handlerType; void* value; // Constructor - LuaTableViewEventData(cocos2d::ScriptHandlerMgr::HandlerType _handleType,void* _value = nullptr) - : handlerType(_handleType),value(_value) + LuaTableViewEventData(void* _value = nullptr) + :value(_value) { } }; struct LuaAssetsManagerEventData { - cocos2d::ScriptHandlerMgr::HandlerType handlerType; int value; - - LuaAssetsManagerEventData(cocos2d::ScriptHandlerMgr::HandlerType _handleType, int _value = 0):handlerType(_handleType),value(_value) + + LuaAssetsManagerEventData(int _value = 0):value(_value) { } };