123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648 |
- /****************************************************************************
- Copyright (c) 2008-2010 Ricardo Quesada
- Copyright (c) 2010-2012 cocos2d-x.org
- Copyright (c) 2011 Zynga Inc.
- Copyright (c) 2013-2016 Chukong Technologies Inc.
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
-
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- #include "2d/CCActionInstant.h"
- #include "2d/CCNode.h"
- #include "2d/CCSprite.h"
- #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
- #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
- #elif _MSC_VER >= 1400 //vs 2005 or higher
- #pragma warning (push)
- #pragma warning (disable: 4996)
- #endif
- NS_CC_BEGIN
- //
- // InstantAction
- //
- void ActionInstant::startWithTarget(Node *target)
- {
- FiniteTimeAction::startWithTarget(target);
- _done = false;
- }
- bool ActionInstant::isDone() const
- {
- return _done;
- }
- void ActionInstant::step(float /*dt*/)
- {
- float updateDt = 1;
- #if CC_ENABLE_SCRIPT_BINDING
- if (_scriptType == kScriptTypeJavascript)
- {
- if (ScriptEngineManager::sendActionEventToJS(this, kActionUpdate, (void *)&updateDt))
- return;
- }
- #endif
- update(updateDt);
- }
- void ActionInstant::update(float /*time*/)
- {
- _done = true;
- }
- //
- // Show
- //
- Show* Show::create()
- {
- Show* ret = new (std::nothrow) Show();
- if (ret)
- {
- ret->autorelease();
- }
- return ret;
- }
- void Show::update(float time)
- {
- ActionInstant::update(time);
- _target->setVisible(true);
- }
- ActionInstant* Show::reverse() const
- {
- return Hide::create();
- }
- Show* Show::clone() const
- {
- // no copy constructor
- return Show::create();
- }
- //
- // Hide
- //
- Hide * Hide::create()
- {
- Hide *ret = new (std::nothrow) Hide();
- if (ret)
- {
- ret->autorelease();
- }
- return ret;
- }
- void Hide::update(float time)
- {
- ActionInstant::update(time);
- _target->setVisible(false);
- }
- ActionInstant *Hide::reverse() const
- {
- return Show::create();
- }
- Hide* Hide::clone() const
- {
- // no copy constructor
- return Hide::create();
- }
- //
- // ToggleVisibility
- //
- ToggleVisibility * ToggleVisibility::create()
- {
- ToggleVisibility *ret = new (std::nothrow) ToggleVisibility();
- if (ret)
- {
- ret->autorelease();
- }
- return ret;
- }
- void ToggleVisibility::update(float time)
- {
- ActionInstant::update(time);
- _target->setVisible(!_target->isVisible());
- }
- ToggleVisibility * ToggleVisibility::reverse() const
- {
- return ToggleVisibility::create();
- }
- ToggleVisibility * ToggleVisibility::clone() const
- {
- // no copy constructor
- return ToggleVisibility::create();
- }
- //
- // Remove Self
- //
- RemoveSelf * RemoveSelf::create(bool isNeedCleanUp /*= true*/)
- {
- RemoveSelf *ret = new (std::nothrow) RemoveSelf();
- if (ret && ret->init(isNeedCleanUp))
- {
- ret->autorelease();
- }
- return ret;
- }
- bool RemoveSelf::init(bool isNeedCleanUp)
- {
- _isNeedCleanUp = isNeedCleanUp;
- return true;
- }
- void RemoveSelf::update(float time)
- {
- ActionInstant::update(time);
- _target->removeFromParentAndCleanup(_isNeedCleanUp);
- }
- RemoveSelf *RemoveSelf::reverse() const
- {
- return RemoveSelf::create(_isNeedCleanUp);
- }
- RemoveSelf * RemoveSelf::clone() const
- {
- // no copy constructor
- return RemoveSelf::create(_isNeedCleanUp);
- }
- //
- // FlipX
- //
- FlipX *FlipX::create(bool x)
- {
- FlipX *ret = new (std::nothrow) FlipX();
- if (ret && ret->initWithFlipX(x))
- {
- ret->autorelease();
- return ret;
- }
- CC_SAFE_DELETE(ret);
- return nullptr;
- }
- bool FlipX::initWithFlipX(bool x)
- {
- _flipX = x;
- return true;
- }
- void FlipX::update(float time)
- {
- ActionInstant::update(time);
- static_cast<Sprite*>(_target)->setFlippedX(_flipX);
- }
- FlipX* FlipX::reverse() const
- {
- return FlipX::create(!_flipX);
- }
- FlipX * FlipX::clone() const
- {
- // no copy constructor
- return FlipX::create(_flipX);
- }
- //
- // FlipY
- //
- FlipY * FlipY::create(bool y)
- {
- FlipY *ret = new (std::nothrow) FlipY();
- if (ret && ret->initWithFlipY(y))
- {
- ret->autorelease();
- return ret;
- }
- CC_SAFE_DELETE(ret);
- return nullptr;
- }
- bool FlipY::initWithFlipY(bool y)
- {
- _flipY = y;
- return true;
- }
- void FlipY::update(float time)
- {
- ActionInstant::update(time);
- static_cast<Sprite*>(_target)->setFlippedY(_flipY);
- }
- FlipY* FlipY::reverse() const
- {
- return FlipY::create(!_flipY);
- }
- FlipY * FlipY::clone() const
- {
- // no copy constructor
- return FlipY::create(_flipY);
- }
- //
- // Place
- //
- Place* Place::create(const Vec2& pos)
- {
- Place *ret = new (std::nothrow) Place();
- if (ret && ret->initWithPosition(pos))
- {
- ret->autorelease();
- return ret;
- }
- delete ret;
- return nullptr;
- }
- bool Place::initWithPosition(const Vec2& pos)
- {
- _position = pos;
- return true;
- }
- Place * Place::clone() const
- {
- // no copy constructor
- return Place::create(_position);
- }
- Place * Place::reverse() const
- {
- // no reverse, just clone
- return this->clone();
- }
- void Place::update(float time)
- {
- ActionInstant::update(time);
- _target->setPosition(_position);
- }
- //
- // CallFunc
- //
- CallFunc * CallFunc::create(const std::function<void()> &func)
- {
- CallFunc *ret = new (std::nothrow) CallFunc();
- if (ret && ret->initWithFunction(func) )
- {
- ret->autorelease();
- return ret;
- }
- CC_SAFE_DELETE(ret);
- return nullptr;
- }
- CallFunc * CallFunc::create(Ref* selectorTarget, SEL_CallFunc selector)
- {
- CallFunc *ret = new (std::nothrow) CallFunc();
- if (ret && ret->initWithTarget(selectorTarget))
- {
- ret->_callFunc = selector;
- ret->autorelease();
- return ret;
- }
- CC_SAFE_DELETE(ret);
- return nullptr;
- }
- bool CallFunc::initWithFunction(const std::function<void()> &func)
- {
- _function = func;
- return true;
- }
- bool CallFunc::initWithTarget(Ref* target)
- {
- if (target)
- {
- target->retain();
- }
- if (_selectorTarget)
- {
- _selectorTarget->release();
- }
- _selectorTarget = target;
- return true;
- }
- CallFunc::~CallFunc()
- {
- CC_SAFE_RELEASE(_selectorTarget);
- }
- CallFunc * CallFunc::clone() const
- {
- // no copy constructor
- auto a = new (std::nothrow) CallFunc();
- if( _selectorTarget)
- {
- a->initWithTarget(_selectorTarget);
- a->_callFunc = _callFunc;
- }
- else if( _function )
- {
- a->initWithFunction(_function);
- }
- a->autorelease();
- return a;
- }
- CallFunc * CallFunc::reverse() const
- {
- // no reverse here, just return a clone
- return this->clone();
- }
- void CallFunc::update(float time)
- {
- ActionInstant::update(time);
- this->execute();
- }
- void CallFunc::execute()
- {
- if (_callFunc)
- {
- (_selectorTarget->*_callFunc)();
- }
- else if( _function )
- {
- _function();
- }
- }
- //
- // CallFuncN
- //
- CallFuncN * CallFuncN::create(const std::function<void(Node*)> &func)
- {
- auto ret = new (std::nothrow) CallFuncN();
- if (ret && ret->initWithFunction(func) )
- {
- ret->autorelease();
- return ret;
- }
- CC_SAFE_DELETE(ret);
- return nullptr;
- }
- // FIXME: deprecated
- CallFuncN * CallFuncN::create(Ref* selectorTarget, SEL_CallFuncN selector)
- {
- CallFuncN *ret = new (std::nothrow) CallFuncN();
- if (ret && ret->initWithTarget(selectorTarget, selector))
- {
- ret->autorelease();
- return ret;
- }
- CC_SAFE_DELETE(ret);
- return nullptr;
- }
- void CallFuncN::execute()
- {
- if (_callFuncN)
- {
- (_selectorTarget->*_callFuncN)(_target);
- }
- else if (_functionN)
- {
- _functionN(_target);
- }
- }
- bool CallFuncN::initWithFunction(const std::function<void (Node *)> &func)
- {
- _functionN = func;
- return true;
- }
- bool CallFuncN::initWithTarget(Ref* selectorTarget, SEL_CallFuncN selector)
- {
- if (CallFunc::initWithTarget(selectorTarget))
- {
- _callFuncN = selector;
- return true;
- }
- return false;
- }
- CallFuncN * CallFuncN::clone() const
- {
- // no copy constructor
- auto a = new (std::nothrow) CallFuncN();
- if( _selectorTarget)
- {
- a->initWithTarget(_selectorTarget, _callFuncN);
- }
- else if( _functionN ){
- a->initWithFunction(_functionN);
- }
- a->autorelease();
- return a;
- }
- //
- // CallFuncND
- //
- __CCCallFuncND * __CCCallFuncND::create(Ref* selectorTarget, SEL_CallFuncND selector, void* d)
- {
- __CCCallFuncND* ret = new (std::nothrow) __CCCallFuncND();
-
- if (ret && ret->initWithTarget(selectorTarget, selector, d))
- {
- ret->autorelease();
- return ret;
- }
-
- CC_SAFE_DELETE(ret);
- return nullptr;
- }
- bool __CCCallFuncND::initWithTarget(Ref* selectorTarget, SEL_CallFuncND selector, void* d)
- {
- if (CallFunc::initWithTarget(selectorTarget))
- {
- _data = d;
- _callFuncND = selector;
- return true;
- }
-
- return false;
- }
- void __CCCallFuncND::execute()
- {
- if (_callFuncND)
- {
- (_selectorTarget->*_callFuncND)(_target, _data);
- }
- }
- __CCCallFuncND * __CCCallFuncND::clone() const
- {
- // no copy constructor
- auto a = new (std::nothrow) __CCCallFuncND();
-
- if( _selectorTarget)
- {
- a->initWithTarget(_selectorTarget, _callFuncND, _data);
- }
-
- a->autorelease();
- return a;
- }
- //
- // CallFuncO
- //
- __CCCallFuncO::__CCCallFuncO() :
- _object(nullptr)
- {
- }
- __CCCallFuncO::~__CCCallFuncO()
- {
- CC_SAFE_RELEASE(_object);
- }
- void __CCCallFuncO::execute()
- {
- if (_callFuncO)
- {
- (_selectorTarget->*_callFuncO)(_object);
- }
- }
- __CCCallFuncO * __CCCallFuncO::create(Ref* selectorTarget, SEL_CallFuncO selector, Ref* object)
- {
- __CCCallFuncO *ret = new (std::nothrow) __CCCallFuncO();
-
- if (ret && ret->initWithTarget(selectorTarget, selector, object))
- {
- ret->autorelease();
- return ret;
- }
-
- CC_SAFE_DELETE(ret);
- return nullptr;
- }
- bool __CCCallFuncO::initWithTarget(Ref* selectorTarget, SEL_CallFuncO selector, Ref* object)
- {
- if (CallFunc::initWithTarget(selectorTarget))
- {
- _object = object;
- CC_SAFE_RETAIN(_object);
-
- _callFuncO = selector;
- return true;
- }
-
- return false;
- }
- __CCCallFuncO * __CCCallFuncO::clone() const
- {
- // no copy constructor
- auto a = new (std::nothrow) __CCCallFuncO();
-
- if( _selectorTarget)
- {
- a->initWithTarget(_selectorTarget, _callFuncO, _object);
- }
-
- a->autorelease();
- return a;
- }
- Ref* __CCCallFuncO::getObject() const
- {
- return _object;
- }
-
- void __CCCallFuncO::setObject(Ref* obj)
- {
- if (obj != _object)
- {
- CC_SAFE_RELEASE(_object);
- _object = obj;
- CC_SAFE_RETAIN(_object);
- }
- }
- NS_CC_END
- #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
- #pragma GCC diagnostic warning "-Wdeprecated-declarations"
- #elif _MSC_VER >= 1400 //vs 2005 or higher
- #pragma warning (pop)
- #endif
|