Merge pull request #2727 from ricardoquesada/callfunction_cplusplus_11_bis

Adds std::function support in CCCallFunc
This commit is contained in:
minggo 2013-06-03 18:52:28 -07:00
commit ef4dd973db
10 changed files with 177 additions and 38 deletions

View File

@ -38,6 +38,7 @@ CCActionInstant::CCActionInstant() {
}
CCObject * CCActionInstant::copyWithZone(CCZone *pZone) {
CCZone *pNewZone = NULL;
CCActionInstant *pRet = NULL;
@ -376,6 +377,20 @@ void CCPlace::update(float time) {
//
// CallFunc
//
CCCallFunc * CCCallFunc::create(const std::function<void()> &func)
{
CCCallFunc *pRet = new CCCallFunc();
if (pRet && pRet->initWithFunction(func) ) {
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
CCCallFunc * CCCallFunc::create(CCObject* pSelectorTarget, SEL_CallFunc selector)
{
CCCallFunc *pRet = new CCCallFunc();
@ -404,6 +419,12 @@ CCCallFunc * CCCallFunc::create(int nHandler)
return pRet;
}
bool CCCallFunc::initWithFunction(const std::function<void()> &func)
{
_function = func;
return true;
}
bool CCCallFunc::initWithTarget(CCObject* pSelectorTarget) {
if (pSelectorTarget)
{
@ -441,8 +462,13 @@ CCObject * CCCallFunc::copyWithZone(CCZone *pZone) {
}
CCActionInstant::copyWithZone(pZone);
if( m_pSelectorTarget) {
pRet->initWithTarget(m_pSelectorTarget);
pRet->m_pCallFunc = m_pCallFunc;
}
else if( _function )
pRet->initWithFunction(_function);
CC_SAFE_DELETE(pNewZone);
return pRet;
}
@ -455,7 +481,8 @@ void CCCallFunc::update(float time) {
void CCCallFunc::execute() {
if (m_pCallFunc) {
(m_pSelectorTarget->*m_pCallFunc)();
}
} else if( _function )
_function();
if (m_nScriptHandler) {
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeCallFuncActionEvent(this);
}

View File

@ -28,6 +28,8 @@ THE SOFTWARE.
#define __CCINSTANT_ACTION_H__
#include <string>
#include <functional>
#include "ccTypeInfo.h"
#include "CCAction.h"
@ -201,6 +203,7 @@ protected:
CCPoint m_tPosition;
};
/** @brief Calls a 'callback'
*/
class CC_DLL CCCallFunc : public CCActionInstant //<NSCopying>
@ -210,13 +213,20 @@ public:
: m_pSelectorTarget(NULL)
, m_nScriptHandler(0)
, m_pCallFunc(NULL)
, _function(NULL)
{
}
virtual ~CCCallFunc();
/** creates the action with the callback of type std::function<void()>.
This is the preferred way to create the callback.
*/
static CCCallFunc * create(const std::function<void()>& func);
/** creates the action with the callback
typedef void (CCObject::*SEL_CallFunc)();
@deprecated Use the std::function API instead.
*/
static CCCallFunc * create(CCObject* pSelectorTarget, SEL_CallFunc selector);
@ -228,6 +238,11 @@ public:
typedef void (CCObject::*SEL_CallFunc)();
*/
virtual bool initWithTarget(CCObject* pSelectorTarget);
/** initializes the action with the std::function<void()>
*/
virtual bool initWithFunction(const std::function<void()>& func);
/** executes the callback */
virtual void execute();
//super methods
@ -256,6 +271,9 @@ protected:
int m_nScriptHandler;
/** function that will be called */
std::function<void()> _function;
union
{
SEL_CallFunc m_pCallFunc;

View File

@ -1 +1 @@
fb235c551f274fd0fbed6d249d17c8bb2c2f097c
e8ebb7f5fd50bd7d91c24ce0aac9d63a92f4f43c

View File

@ -1 +1 @@
7a57beef4f0e945e6906a31ebd8ee65489e00d3c
125800bcfe2802d3c6a965a3aa337147d20bf187

View File

@ -27,6 +27,7 @@ TESTLAYER_CREATE_FUNC(ActionRepeat);
TESTLAYER_CREATE_FUNC(ActionRepeatForever);
TESTLAYER_CREATE_FUNC(ActionRotateToRepeat);
TESTLAYER_CREATE_FUNC(ActionRotateJerk);
TESTLAYER_CREATE_FUNC(ActionCallFunction);
TESTLAYER_CREATE_FUNC(ActionCallFunc);
TESTLAYER_CREATE_FUNC(ActionCallFuncND);
TESTLAYER_CREATE_FUNC(ActionReverseSequence);
@ -76,6 +77,7 @@ static NEWTESTFUNC createFunctions[] = {
CF(ActionRepeatForever),
CF(ActionRotateToRepeat),
CF(ActionRotateJerk),
CF(ActionCallFunction),
CF(ActionCallFunc),
CF(ActionCallFuncND),
CF(ActionReverseSequence),
@ -819,9 +821,9 @@ void ActionSequence2::onEnter()
CCPlace::create(ccp(200,200)),
CCShow::create(),
CCMoveBy::create(1, ccp(100,0)),
CCCallFunc::create(this, callfunc_selector(ActionSequence2::callback1)),
CCCallFuncN::create(this, callfuncN_selector(ActionSequence2::callback2)),
CCCallFuncND::create(this, callfuncND_selector(ActionSequence2::callback3), (void*)0xbebabeba),
CCCallFunc::create(std::bind(&ActionSequence2::callback1,this)),
CCCallFunc::create(std::bind(&ActionSequence2::callback2,this,m_grossini)),
CCCallFunc::create(std::bind(&ActionSequence2::callback3,this,m_grossini,(void*)0xbebabeba)),
NULL);
m_grossini->runAction(action);
@ -862,6 +864,7 @@ std::string ActionSequence2::subtitle()
//------------------------------------------------------------------
//
// ActionCallFunc
// DEPRECATED. Use the std::function() API instead
//
//------------------------------------------------------------------
void ActionCallFunc::onEnter()
@ -921,12 +924,13 @@ void ActionCallFunc::callback3(CCNode* pTarget, void* data)
std::string ActionCallFunc::subtitle()
{
return "Callbacks: CallFunc and friends";
return "Callbacks: CallFunc. Old way. Avoid it";
}
//------------------------------------------------------------------
//
// ActionCallFuncND
// DEPRECATED. Use the std::function() API instead
//
//------------------------------------------------------------------
void ActionCallFuncND::onEnter()
@ -958,6 +962,83 @@ void ActionCallFuncND::removeFromParentAndCleanup(CCNode* pSender, void* data)
m_grossini->removeFromParentAndCleanup(bCleanUp);
}
//------------------------------------------------------------------
//
// ActionCallFunction
//
//------------------------------------------------------------------
void ActionCallFunction::onEnter()
{
ActionsDemo::onEnter();
centerSprites(3);
CCFiniteTimeAction* action1 = CCSequence::create(
CCMoveBy::create(2, ccp(200,0)),
CCCallFunc::create( std::bind(&ActionCallFunction::callback1, this) ),
CCCallFunc::create(
// lambda
[&](){
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF *label = CCLabelTTF::create("called:lambda callback", "Marker Felt", 16);
label->setPosition(ccp( s.width/4*1,s.height/2-40));
this->addChild(label);
} ),
NULL);
CCFiniteTimeAction* action2 = CCSequence::create(
CCScaleBy::create(2 , 2),
CCFadeOut::create(2),
CCCallFunc::create( std::bind(&ActionCallFunction::callback2, this, m_tamara) ),
NULL);
CCFiniteTimeAction* action3 = CCSequence::create(
CCRotateBy::create(3 , 360),
CCFadeOut::create(2),
CCCallFunc::create( std::bind(&ActionCallFunction::callback3, this, m_kathia, (void*)42) ),
NULL);
m_grossini->runAction(action1);
m_tamara->runAction(action2);
m_kathia->runAction(action3);
}
void ActionCallFunction::callback1()
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF *label = CCLabelTTF::create("callback 1 called", "Marker Felt", 16);
label->setPosition(ccp( s.width/4*1,s.height/2));
addChild(label);
}
void ActionCallFunction::callback2(CCNode* sender)
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF *label = CCLabelTTF::create("callback 2 called", "Marker Felt", 16);
label->setPosition(ccp( s.width/4*2,s.height/2));
addChild(label);
CCLOG("sender is: %p", sender);
}
void ActionCallFunction::callback3(CCNode* sender, void* data)
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF *label = CCLabelTTF::create("callback 3 called", "Marker Felt", 16);
label->setPosition(ccp( s.width/4*3,s.height/2));
addChild(label);
CCLOG("target is: %p, data is: %ld", sender, (long)data);
}
std::string ActionCallFunction::subtitle()
{
return "Callbacks: CallFunc with std::function()";
}
//------------------------------------------------------------------
//
// ActionSpawn
@ -998,7 +1079,7 @@ void ActionRepeatForever::onEnter()
CCFiniteTimeAction* action = CCSequence::create(
CCDelayTime::create(1),
CCCallFuncN::create( this, callfuncN_selector(ActionRepeatForever::repeatForever) ),
CCCallFunc::create( std::bind( &ActionRepeatForever::repeatForever, this, m_grossini) ),
NULL);
m_grossini->runAction(action);
@ -1704,7 +1785,7 @@ void Issue1305::onEnter()
}] );
*/
m_pSpriteTmp->runAction(CCCallFuncN::create(this, callfuncN_selector(Issue1305::log)));
m_pSpriteTmp->runAction(CCCallFunc::create(std::bind(&Issue1305::log, this, m_pSpriteTmp)));
m_pSpriteTmp->retain();
scheduleOnce(schedule_selector(Issue1305::addSprite), 2);
@ -1766,13 +1847,13 @@ void Issue1305_2::onEnter()
});
*/
CCCallFunc* act2 = CCCallFunc::create(this, callfunc_selector(Issue1305_2::printLog1));
CCCallFunc* act2 = CCCallFunc::create( std::bind( &Issue1305_2::printLog1, this));
CCMoveBy* act3 = CCMoveBy::create(2, ccp(0, -100));
CCCallFunc* act4 = CCCallFunc::create(this, callfunc_selector(Issue1305_2::printLog2));
CCCallFunc* act4 = CCCallFunc::create( std::bind( &Issue1305_2::printLog2, this));
CCMoveBy* act5 = CCMoveBy::create(2, ccp(100, -100));
CCCallFunc* act6 = CCCallFunc::create(this, callfunc_selector(Issue1305_2::printLog3));
CCCallFunc* act6 = CCCallFunc::create( std::bind( &Issue1305_2::printLog3, this));
CCMoveBy* act7 = CCMoveBy::create(2, ccp(-100, 0));
CCCallFunc* act8 = CCCallFunc::create(this, callfunc_selector(Issue1305_2::printLog4));
CCCallFunc* act8 = CCCallFunc::create( std::bind( &Issue1305_2::printLog4, this));
CCFiniteTimeAction* actF = CCSequence::create(act1, act2, act3, act4, act5, act6, act7, act8, NULL);
@ -1871,15 +1952,15 @@ void Issue1327::onEnter()
spr->setPosition(ccp(100, 100));
addChild(spr);
CCCallFuncN* act1 = CCCallFuncN::create(this, callfuncN_selector(Issue1327::logSprRotation));
CCCallFunc* act1 = CCCallFunc::create( std::bind(&Issue1327::logSprRotation, this, spr));
CCRotateBy* act2 = CCRotateBy::create(0.25, 45);
CCCallFuncN* act3 = CCCallFuncN::create(this, callfuncN_selector(Issue1327::logSprRotation));
CCCallFunc* act3 = CCCallFunc::create( std::bind(&Issue1327::logSprRotation, this, spr));
CCRotateBy* act4 = CCRotateBy::create(0.25, 45);
CCCallFuncN* act5 = CCCallFuncN::create(this, callfuncN_selector(Issue1327::logSprRotation));
CCCallFunc* act5 = CCCallFunc::create( std::bind(&Issue1327::logSprRotation, this, spr));
CCRotateBy* act6 = CCRotateBy::create(0.25, 45);
CCCallFuncN* act7 = CCCallFuncN::create(this, callfuncN_selector(Issue1327::logSprRotation));
CCCallFunc* act7 = CCCallFunc::create( std::bind(&Issue1327::logSprRotation, this, spr));
CCRotateBy* act8 = CCRotateBy::create(0.25, 45);
CCCallFuncN* act9 = CCCallFuncN::create(this, callfuncN_selector(Issue1327::logSprRotation));
CCCallFunc* act9 = CCCallFunc::create( std::bind(&Issue1327::logSprRotation, this, spr));
CCFiniteTimeAction* actF = CCSequence::create(act1, act2, act3, act4, act5, act6, act7, act8, act9, NULL);
spr->runAction(actF);
@ -1917,18 +1998,18 @@ void Issue1398::onEnter()
this->runAction(
CCSequence::create(
CCCallFuncND::create(this, callfuncND_selector(Issue1398::incrementIntegerCallback), (void*)"1"),
CCCallFuncND::create(this, callfuncND_selector(Issue1398::incrementIntegerCallback), (void*)"2"),
CCCallFuncND::create(this, callfuncND_selector(Issue1398::incrementIntegerCallback), (void*)"3"),
CCCallFuncND::create(this, callfuncND_selector(Issue1398::incrementIntegerCallback), (void*)"4"),
CCCallFuncND::create(this, callfuncND_selector(Issue1398::incrementIntegerCallback), (void*)"5"),
CCCallFuncND::create(this, callfuncND_selector(Issue1398::incrementIntegerCallback), (void*)"6"),
CCCallFuncND::create(this, callfuncND_selector(Issue1398::incrementIntegerCallback), (void*)"7"),
CCCallFuncND::create(this, callfuncND_selector(Issue1398::incrementIntegerCallback), (void*)"8"),
CCCallFunc::create( std::bind(&Issue1398::incrementIntegerCallback, this, (void*)"1")),
CCCallFunc::create( std::bind(&Issue1398::incrementIntegerCallback, this, (void*)"2")),
CCCallFunc::create( std::bind(&Issue1398::incrementIntegerCallback, this, (void*)"3")),
CCCallFunc::create( std::bind(&Issue1398::incrementIntegerCallback, this, (void*)"4")),
CCCallFunc::create( std::bind(&Issue1398::incrementIntegerCallback, this, (void*)"5")),
CCCallFunc::create( std::bind(&Issue1398::incrementIntegerCallback, this, (void*)"6")),
CCCallFunc::create( std::bind(&Issue1398::incrementIntegerCallback, this, (void*)"7")),
CCCallFunc::create( std::bind(&Issue1398::incrementIntegerCallback, this, (void*)"8")),
NULL));
}
void Issue1398::incrementIntegerCallback(CCNode* pSender, void* data)
void Issue1398::incrementIntegerCallback(void* data)
{
this->incrementInteger();
CCLog("%s", (char*)data);

View File

@ -36,6 +36,7 @@ enum
ACTION_ROTATEJERK_LAYER,
ACTION_CALLFUNC_LAYER,
ACTION_CALLFUNCND_LAYER,
ACTION_CALLFUNCTION_LAYER,
ACTION_REVERSESEQUENCE_LAYER,
ACTION_REVERSESEQUENCE2_LAYER,
ACTION_ORBIT_LAYER,
@ -299,6 +300,18 @@ public:
void removeFromParentAndCleanup(CCNode* pSender, void* data);
};
class ActionCallFunction : public ActionsDemo
{
public:
virtual void onEnter();
virtual std::string subtitle();
void callback1();
void callback2(CCNode* pTarget);
void callback3(CCNode* pTarget, void* data);
};
class ActionFollow : public ActionsDemo
{
public:
@ -426,7 +439,7 @@ class Issue1398 : public ActionsDemo
{
public:
void incrementInteger();
void incrementIntegerCallback(CCNode* pSender, void* data);
void incrementIntegerCallback(void* data);
virtual void onEnter();
virtual std::string subtitle();
virtual std::string title();

View File

@ -1 +1 @@
c12820b10f5bb33955c15eaf4cae17a4628455f8
dd49000446b982d873b507d09499340ea5735400

View File

@ -1 +1 @@
1ecba86ffd5c89b2e005f581c9bf60f334b9ce01
647c786f7e749b31546a70420232f01ca7c89f6f

View File

@ -11,7 +11,7 @@ android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include
android_flags = -D_SIZE_T_DEFINED_
clang_headers = -I%(clangllvmdir)s/lib/clang/3.1/include
clang_flags = -nostdinc -x c++
clang_flags = -nostdinc -x c++ -std=c++11
cocos_headers = -I%(cocosdir)s/cocos2dx/include -I%(cocosdir)s/cocos2dx/platform -I%(cocosdir)s/cocos2dx/platform/android -I%(cocosdir)s/cocos2dx -I%(cocosdir)s/cocos2dx/kazmath/include
cocos_flags = -DANDROID -DCOCOS2D_JAVASCRIPT

View File

@ -11,7 +11,7 @@ android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include
android_flags = -D_SIZE_T_DEFINED_
clang_headers = -I%(clangllvmdir)s/lib/clang/3.1/include
clang_flags = -nostdinc -x c++
clang_flags = -nostdinc -x c++ -std=c++11
cocos_headers = -I%(cocosdir)s/cocos2dx/include -I%(cocosdir)s/cocos2dx/platform -I%(cocosdir)s/cocos2dx/platform/android -I%(cocosdir)s/cocos2dx -I%(cocosdir)s/cocos2dx/kazmath/include -I%(cocosdir)s/extensions
cocos_flags = -DANDROID -DCOCOS2D_JAVASCRIPT