From a13c519882002f9b715d3cf91e1fa19a2816039d Mon Sep 17 00:00:00 2001 From: Milos Jakovljevic Date: Sat, 18 Apr 2015 14:35:14 +0200 Subject: [PATCH] Adding FloatAction to animate any property over time. --- cocos/2d/CCActionInterval.cpp | 56 +++++++++++++++++++ cocos/2d/CCActionInterval.h | 48 ++++++++++++++++ .../Classes/ActionsTest/ActionsTest.cpp | 40 +++++++++++++ .../Classes/ActionsTest/ActionsTest.h | 9 +++ 4 files changed, 153 insertions(+) diff --git a/cocos/2d/CCActionInterval.cpp b/cocos/2d/CCActionInterval.cpp index 8817626d05..ecc88a1e16 100644 --- a/cocos/2d/CCActionInterval.cpp +++ b/cocos/2d/CCActionInterval.cpp @@ -2511,4 +2511,60 @@ void TargetedAction::setForcedTarget(Node* forcedTarget) } } +// Float Action + +FloatAction* FloatAction::create(float duration, float from, float to, FloatActionCallback callback) +{ + auto ref = new (std::nothrow) FloatAction(); + if (ref && ref->initWithDuration(duration, from, to, callback)) + { + ref->autorelease(); + return ref; + } + CC_SAFE_DELETE(ref); + return ref; +} + +bool FloatAction::initWithDuration(float duration, float from, float to, FloatActionCallback callback) +{ + if (ActionInterval::initWithDuration(duration)) + { + _from = from; + _to = to; + _callback = callback; + return true; + } + return false; +} + +FloatAction* FloatAction::clone() const +{ + auto a = new (std::nothrow) FloatAction(); + a->initWithDuration(_duration, _from, _to, _callback); + a->autorelease(); + return a; +} + +void FloatAction::startWithTarget(cocos2d::Node *target) +{ + ActionInterval::startWithTarget(target); + _delta = _to - _from; +} + +void FloatAction::update(float delta) +{ + float value = _to - _delta * (1 - delta); + + if (_callback) + { + // report back + _callback(value); + } +} + +FloatAction* FloatAction::reverse() const +{ + return FloatAction::create(_duration, _to, _from, _callback); +} + NS_CC_END diff --git a/cocos/2d/CCActionInterval.h b/cocos/2d/CCActionInterval.h index a489d56639..95cb693964 100644 --- a/cocos/2d/CCActionInterval.h +++ b/cocos/2d/CCActionInterval.h @@ -1525,6 +1525,54 @@ private: CC_DISALLOW_COPY_AND_ASSIGN(TargetedAction); }; +/** + * @class FloatAction + * @brief Action used to animate any value in range [from,to] over specified time interval + */ +class CC_DLL FloatAction : public ActionInterval +{ +public: + /** + * Callback function used to report back result + */ + typedef std::function FloatActionCallback; + + /** + * Creates FloatAction with specified duration, from value, to value and callback to report back + * results + * @param duration + * @param from value + * @param to value + * @param callback to report back result + * + * @return An autoreleased FloatAction object + */ + static FloatAction* create(float duration, float from, float to, FloatActionCallback callback); + + /** + * Overrided ActionInterval methods + */ + void startWithTarget(Node* target) override; + void update(float delta) override; + FloatAction* reverse() const override; + FloatAction* clone() const override; + +CC_CONSTRUCTOR_ACCESS: + FloatAction() {}; + virtual ~FloatAction() {}; + + bool initWithDuration(float duration, float from, float to, FloatActionCallback callback); + +protected: + float _from; + float _to; + float _delta; + + FloatActionCallback _callback; +private: + CC_DISALLOW_COPY_AND_ASSIGN(FloatAction); +}; + // end of actions group /// @} diff --git a/tests/cpp-tests/Classes/ActionsTest/ActionsTest.cpp b/tests/cpp-tests/Classes/ActionsTest/ActionsTest.cpp index 52bb9e2c5e..7f0d5df737 100644 --- a/tests/cpp-tests/Classes/ActionsTest/ActionsTest.cpp +++ b/tests/cpp-tests/Classes/ActionsTest/ActionsTest.cpp @@ -86,6 +86,7 @@ ActionsTests::ActionsTests() ADD_TEST_CASE(Issue1327); ADD_TEST_CASE(Issue1398); ADD_TEST_CASE(Issue2599) + ADD_TEST_CASE(ActionFloat); } std::string ActionsDemo::title() const @@ -2218,3 +2219,42 @@ std::string ActionRemoveSelf::subtitle() const { return "Sequence: Move + Rotate + Scale + RemoveSelf"; } + +//------------------------------------------------------------------ +// +// ActionFloat +// +//------------------------------------------------------------------ +void ActionFloat::onEnter() +{ + ActionsDemo::onEnter(); + + centerSprites(3); + + auto s = Director::getInstance()->getWinSize(); + + auto actionFloat = FloatAction::create(2.f, 0, 3, [this](float value) { + _tamara->setScale(value); + }); + + float grossiniY = _grossini->getPositionY(); + + auto actionFloat1 = FloatAction::create(3.f, grossiniY, grossiniY + 50, [this](float value) { + _grossini->setPositionY(value); + }); + + auto actionFloat2 = FloatAction::create(3.f, 3, 1, [this] (float value) { + _kathia->cocos2d::Node::setScale(value); + }); + + _tamara->runAction(actionFloat); + _grossini->runAction(actionFloat1); + _kathia->runAction(actionFloat2); +} + +std::string ActionFloat::subtitle() const +{ + return "ActionFloat"; +} + + diff --git a/tests/cpp-tests/Classes/ActionsTest/ActionsTest.h b/tests/cpp-tests/Classes/ActionsTest/ActionsTest.h index c639ced391..b1ec27af7e 100644 --- a/tests/cpp-tests/Classes/ActionsTest/ActionsTest.h +++ b/tests/cpp-tests/Classes/ActionsTest/ActionsTest.h @@ -580,4 +580,13 @@ private: cocos2d::Vector _pausedTargets; }; +class ActionFloat : public ActionsDemo +{ +public: + CREATE_FUNC(ActionFloat); + + virtual void onEnter() override; + virtual std::string subtitle() const override; +}; + #endif