issue #3228: Reconstruct the Callfunc create method to support std::function and add callFuncND + auto remove lua test case

This commit is contained in:
samuele3 2013-11-22 11:51:07 +08:00
parent 07ea8647c3
commit 192deb7952
6 changed files with 50 additions and 26 deletions

View File

@ -472,7 +472,7 @@ CallFuncN * CallFuncN::clone() const
if( _selectorTarget) {
a->initWithTarget(_selectorTarget, _callFuncN);
}
else if( _function ){
else if( _functionN ){
a->initWithFunction(_functionN);
}

View File

@ -263,8 +263,8 @@ public:
CC_DEPRECATED_ATTRIBUTE bool initWithTarget(Object* target);
/** initializes the action with the std::function<void()>
* @js NK
* @lua NK
* @js NA
* @lua NA
*/
bool initWithFunction(const std::function<void()>& func);

View File

@ -48,31 +48,35 @@ void ScheduleHandlerDelegate::update(float elapse)
}
LuaCallFunc * LuaCallFunc::create(int nHandler)
LuaCallFunc * LuaCallFunc::create(const std::function<void(void* ,Node*)>& 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<void (void*, Node*)> &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;
}

View File

@ -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<void(void* self,Node*)>& func);
bool initWithFunction(const std::function<void(void* self,Node*)>& func);
virtual LuaCallFunc* clone() const;
virtual void execute() override;
protected:
/**
*/
std::function<void(void* self,Node*)> _functionLua;
};
class ScriptHandlerMgr

View File

@ -1 +1 @@
6f20ef3b233b2dffcfa11fe289879cfc5ee35d51
86fa141032de3d513df690fc82b20d2f2b4ab01b

View File

@ -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")