From 192deb7952a5ad783bf3d6d551c1db510de6e503 Mon Sep 17 00:00:00 2001 From: samuele3 Date: Fri, 22 Nov 2013 11:51:07 +0800 Subject: [PATCH] issue #3228: Reconstruct the Callfunc create method to support std::function and add callFuncND + auto remove lua test case --- cocos/2d/CCActionInstant.cpp | 2 +- cocos/2d/CCActionInstant.h | 4 +- .../lua/bindings/LuaScriptHandlerMgr.cpp | 47 +++++++++++-------- .../lua/bindings/LuaScriptHandlerMgr.h | 12 +++-- .../lua_cocos2dx_manual.cpp.REMOVED.git-id | 2 +- .../luaScript/ActionsTest/ActionsTest.lua | 9 ++++ 6 files changed, 50 insertions(+), 26 deletions(-) diff --git a/cocos/2d/CCActionInstant.cpp b/cocos/2d/CCActionInstant.cpp index 8a862f5771..408a400e84 100644 --- a/cocos/2d/CCActionInstant.cpp +++ b/cocos/2d/CCActionInstant.cpp @@ -472,7 +472,7 @@ CallFuncN * CallFuncN::clone() const if( _selectorTarget) { a->initWithTarget(_selectorTarget, _callFuncN); } - else if( _function ){ + else if( _functionN ){ a->initWithFunction(_functionN); } diff --git a/cocos/2d/CCActionInstant.h b/cocos/2d/CCActionInstant.h index 01a26a3786..68ca323e2f 100644 --- a/cocos/2d/CCActionInstant.h +++ b/cocos/2d/CCActionInstant.h @@ -263,8 +263,8 @@ public: CC_DEPRECATED_ATTRIBUTE bool initWithTarget(Object* target); /** initializes the action with the std::function - * @js NK - * @lua NK + * @js NA + * @lua NA */ bool initWithFunction(const std::function& func); diff --git a/cocos/scripting/lua/bindings/LuaScriptHandlerMgr.cpp b/cocos/scripting/lua/bindings/LuaScriptHandlerMgr.cpp index ab4d01bd01..d627be2e18 100644 --- a/cocos/scripting/lua/bindings/LuaScriptHandlerMgr.cpp +++ b/cocos/scripting/lua/bindings/LuaScriptHandlerMgr.cpp @@ -48,31 +48,35 @@ void ScheduleHandlerDelegate::update(float elapse) } -LuaCallFunc * LuaCallFunc::create(int nHandler) +LuaCallFunc * LuaCallFunc::create(const std::function& func) { - LuaCallFunc *ret = new LuaCallFunc(); - if (NULL != ret ) - { + auto ret = new LuaCallFunc(); + + if (ret && ret->initWithFunction(func) ) { ret->autorelease(); - ScriptHandlerMgr::getInstance()->addObjectHandler((void*)ret, nHandler, ScriptHandlerMgr::HandlerType::CALLFUNC); return ret; } + + CC_SAFE_DELETE(ret); + return NULL; +} + +void LuaCallFunc::execute() +{ + if (_functionLua) + { + _functionLua((void*)this,_target); + } else { - CC_SAFE_DELETE(ret); - return NULL; + CallFuncN::execute(); } } -void LuaCallFunc::execute() + +bool LuaCallFunc::initWithFunction(const std::function &func) { - int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, ScriptHandlerMgr::HandlerType::CALLFUNC); - - if (0 == handler) - return ; - - BasicScriptData data((void*)this,(void*)_target); - ScriptEvent event(kCallFuncEvent,(void*)&data); - ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); + _functionLua = func; + return true; } LuaCallFunc* LuaCallFunc::clone() const @@ -83,13 +87,18 @@ LuaCallFunc* LuaCallFunc::clone() const return NULL; auto ret = new LuaCallFunc(); + + if( _functionLua ) + { + ret->initWithFunction(_functionLua); + } + + ret->autorelease(); int newscriptHandler = cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->reallocateScriptHandler(handler); ScriptHandlerMgr::getInstance()->addObjectHandler((void*)ret, newscriptHandler, ScriptHandlerMgr::HandlerType::CALLFUNC); - - ret->autorelease(); - + return ret; } diff --git a/cocos/scripting/lua/bindings/LuaScriptHandlerMgr.h b/cocos/scripting/lua/bindings/LuaScriptHandlerMgr.h index 3d0acfc87e..8d567019e2 100644 --- a/cocos/scripting/lua/bindings/LuaScriptHandlerMgr.h +++ b/cocos/scripting/lua/bindings/LuaScriptHandlerMgr.h @@ -44,14 +44,20 @@ private: class LuaCallFunc:public cocos2d::CallFuncN { public: - LuaCallFunc() + LuaCallFunc():_functionLua(nullptr) {} virtual ~LuaCallFunc() {} - static LuaCallFunc * create(int nHandler); - virtual void execute(); + static LuaCallFunc* create(const std::function& func); + bool initWithFunction(const std::function& func); virtual LuaCallFunc* clone() const; + virtual void execute() override; +protected: + /** + */ + std::function _functionLua; + }; class ScriptHandlerMgr diff --git a/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id b/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id index c59adc5e90..c3dcf3390c 100644 --- a/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id +++ b/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id @@ -1 +1 @@ -6f20ef3b233b2dffcfa11fe289879cfc5ee35d51 \ No newline at end of file +86fa141032de3d513df690fc82b20d2f2b4ab01b \ No newline at end of file diff --git a/samples/Lua/TestLua/Resources/luaScript/ActionsTest/ActionsTest.lua b/samples/Lua/TestLua/Resources/luaScript/ActionsTest/ActionsTest.lua index 52a3e5f892..414870acbb 100644 --- a/samples/Lua/TestLua/Resources/luaScript/ActionsTest/ActionsTest.lua +++ b/samples/Lua/TestLua/Resources/luaScript/ActionsTest/ActionsTest.lua @@ -835,6 +835,15 @@ local function ActionCallFuncND() centerSprites(1) + local function doRemoveFromParentAndCleanup(sender,table) + grossini:removeFromParentAndCleanup(table[1]) + end + + local action = cc.Sequence:create( + cc.MoveBy:create(2, cc.p(200,0)), + cc.CallFunc:create(doRemoveFromParentAndCleanup,{true})) + + grossini:runAction(action) Helper.titleLabel:setString("CallFuncND + auto remove") Helper.subtitleLabel:setString("CallFuncND + removeFromParent. Grossini dissapears in 2s")