2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2009 Jason Booth
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2024-03-07 08:47:00 +08:00
|
|
|
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2024-06-10 02:25:43 +08:00
|
|
|
https://axmol.dev/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Elastic, Back and Bounce actions based on code from:
|
|
|
|
* http://github.com/NikhilK/silverlightfx/
|
|
|
|
*
|
|
|
|
* by http://github.com/NikhilK
|
|
|
|
*/
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "2d/ActionEase.h"
|
|
|
|
#include "2d/TweenFunction.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2024-08-26 00:25:33 +08:00
|
|
|
namespace ax
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#ifndef M_PI_X_2
|
2021-12-25 10:04:45 +08:00
|
|
|
# define M_PI_X_2 (float)M_PI * 2.0f
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// EaseAction
|
|
|
|
//
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ActionEase::initWithAction(ActionInterval* action)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(action != nullptr, "action couldn't be nullptr!");
|
2019-11-23 20:27:39 +08:00
|
|
|
if (action == nullptr)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ActionInterval::initWithDuration(action->getDuration()))
|
|
|
|
{
|
|
|
|
_inner = action;
|
|
|
|
action->retain();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionEase::~ActionEase()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_inner);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ActionEase::startWithTarget(Node* target)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (target && _inner)
|
|
|
|
{
|
|
|
|
ActionInterval::startWithTarget(target);
|
|
|
|
_inner->startWithTarget(_target);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-07 08:47:00 +08:00
|
|
|
AXLOGE("ActionEase::startWithTarget error: target or _inner is nullptr!");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActionEase::stop()
|
|
|
|
{
|
|
|
|
if (_inner)
|
|
|
|
_inner->stop();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ActionInterval::stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActionEase::update(float time)
|
|
|
|
{
|
|
|
|
_inner->update(time);
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionInterval* ActionEase::getInnerAction()
|
|
|
|
{
|
|
|
|
return _inner;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// EaseRateAction
|
|
|
|
//
|
|
|
|
|
|
|
|
EaseRateAction* EaseRateAction::create(ActionInterval* action, float rate)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(action != nullptr, "action cannot be nullptr!");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
EaseRateAction* easeRateAction = new EaseRateAction();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (easeRateAction->initWithAction(action, rate))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
easeRateAction->autorelease();
|
|
|
|
return easeRateAction;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(easeRateAction);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool EaseRateAction::initWithAction(ActionInterval* action, float rate)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (ActionEase::initWithAction(action))
|
|
|
|
{
|
|
|
|
_rate = rate;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// NOTE: Converting these macros into Templates is desirable, but please see
|
|
|
|
// issue #16159 [https://github.com/cocos2d/cocos2d-x/pull/16159] for further info
|
|
|
|
//
|
2021-12-25 10:04:45 +08:00
|
|
|
#define EASE_TEMPLATE_IMPL(CLASSNAME, TWEEN_FUNC, REVERSE_CLASSNAME) \
|
2022-08-08 18:02:17 +08:00
|
|
|
CLASSNAME* CLASSNAME::create(ax::ActionInterval* action) \
|
2021-12-25 10:04:45 +08:00
|
|
|
{ \
|
|
|
|
CLASSNAME* ease = new CLASSNAME(); \
|
|
|
|
if (ease->initWithAction(action)) \
|
|
|
|
ease->autorelease(); \
|
|
|
|
else \
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(ease); \
|
2021-12-25 10:04:45 +08:00
|
|
|
return ease; \
|
|
|
|
} \
|
|
|
|
CLASSNAME* CLASSNAME::clone() const \
|
|
|
|
{ \
|
|
|
|
if (_inner) \
|
|
|
|
return CLASSNAME::create(_inner->clone()); \
|
|
|
|
return nullptr; \
|
|
|
|
} \
|
|
|
|
void CLASSNAME::update(float time) { _inner->update(TWEEN_FUNC(time)); } \
|
|
|
|
ActionEase* CLASSNAME::reverse() const { return REVERSE_CLASSNAME::create(_inner->reverse()); }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
EASE_TEMPLATE_IMPL(EaseExponentialIn, tweenfunc::expoEaseIn, EaseExponentialOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseExponentialOut, tweenfunc::expoEaseOut, EaseExponentialIn);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseExponentialInOut, tweenfunc::expoEaseInOut, EaseExponentialInOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseSineIn, tweenfunc::sineEaseIn, EaseSineOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseSineOut, tweenfunc::sineEaseOut, EaseSineIn);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseSineInOut, tweenfunc::sineEaseInOut, EaseSineInOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseBounceIn, tweenfunc::bounceEaseIn, EaseBounceOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseBounceOut, tweenfunc::bounceEaseOut, EaseBounceIn);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseBounceInOut, tweenfunc::bounceEaseInOut, EaseBounceInOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseBackIn, tweenfunc::backEaseIn, EaseBackOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseBackOut, tweenfunc::backEaseOut, EaseBackIn);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseBackInOut, tweenfunc::backEaseInOut, EaseBackInOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseQuadraticActionIn, tweenfunc::quadraticIn, EaseQuadraticActionIn);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseQuadraticActionOut, tweenfunc::quadraticOut, EaseQuadraticActionOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseQuadraticActionInOut, tweenfunc::quadraticInOut, EaseQuadraticActionInOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseQuarticActionIn, tweenfunc::quartEaseIn, EaseQuarticActionIn);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseQuarticActionOut, tweenfunc::quartEaseOut, EaseQuarticActionOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseQuarticActionInOut, tweenfunc::quartEaseInOut, EaseQuarticActionInOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseQuinticActionIn, tweenfunc::quintEaseIn, EaseQuinticActionIn);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseQuinticActionOut, tweenfunc::quintEaseOut, EaseQuinticActionOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseQuinticActionInOut, tweenfunc::quintEaseInOut, EaseQuinticActionInOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseCircleActionIn, tweenfunc::circEaseIn, EaseCircleActionIn);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseCircleActionOut, tweenfunc::circEaseOut, EaseCircleActionOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseCircleActionInOut, tweenfunc::circEaseInOut, EaseCircleActionInOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseCubicActionIn, tweenfunc::cubicEaseIn, EaseCubicActionIn);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseCubicActionOut, tweenfunc::cubicEaseOut, EaseCubicActionOut);
|
|
|
|
EASE_TEMPLATE_IMPL(EaseCubicActionInOut, tweenfunc::cubicEaseInOut, EaseCubicActionInOut);
|
|
|
|
|
|
|
|
//
|
|
|
|
// NOTE: Converting these macros into Templates is desirable, but please see
|
|
|
|
// issue #16159 [https://github.com/cocos2d/cocos2d-x/pull/16159] for further info
|
|
|
|
//
|
2021-12-25 10:04:45 +08:00
|
|
|
#define EASERATE_TEMPLATE_IMPL(CLASSNAME, TWEEN_FUNC) \
|
2022-08-08 18:02:17 +08:00
|
|
|
CLASSNAME* CLASSNAME::create(ax::ActionInterval* action, float rate) \
|
2021-12-25 10:04:45 +08:00
|
|
|
{ \
|
|
|
|
CLASSNAME* ease = new CLASSNAME(); \
|
|
|
|
if (ease->initWithAction(action, rate)) \
|
|
|
|
ease->autorelease(); \
|
|
|
|
else \
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(ease); \
|
2021-12-25 10:04:45 +08:00
|
|
|
return ease; \
|
|
|
|
} \
|
|
|
|
CLASSNAME* CLASSNAME::clone() const \
|
|
|
|
{ \
|
|
|
|
if (_inner) \
|
|
|
|
return CLASSNAME::create(_inner->clone(), _rate); \
|
|
|
|
return nullptr; \
|
|
|
|
} \
|
|
|
|
void CLASSNAME::update(float time) { _inner->update(TWEEN_FUNC(time, _rate)); } \
|
|
|
|
EaseRateAction* CLASSNAME::reverse() const { return CLASSNAME::create(_inner->reverse(), 1.f / _rate); }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// NOTE: the original code used the same class for the `reverse()` method
|
|
|
|
EASERATE_TEMPLATE_IMPL(EaseIn, tweenfunc::easeIn);
|
|
|
|
EASERATE_TEMPLATE_IMPL(EaseOut, tweenfunc::easeOut);
|
|
|
|
EASERATE_TEMPLATE_IMPL(EaseInOut, tweenfunc::easeInOut);
|
|
|
|
|
|
|
|
//
|
|
|
|
// EaseElastic
|
|
|
|
//
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool EaseElastic::initWithAction(ActionInterval* action, float period /* = 0.3f*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (ActionEase::initWithAction(action))
|
|
|
|
{
|
|
|
|
_period = period;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// NOTE: Converting these macros into Templates is desirable, but please see
|
|
|
|
// issue #16159 [https://github.com/cocos2d/cocos2d-x/pull/16159] for further info
|
|
|
|
//
|
2021-12-25 10:04:45 +08:00
|
|
|
#define EASEELASTIC_TEMPLATE_IMPL(CLASSNAME, TWEEN_FUNC, REVERSE_CLASSNAME) \
|
2022-08-08 18:02:17 +08:00
|
|
|
CLASSNAME* CLASSNAME::create(ax::ActionInterval* action, float period /* = 0.3f*/) \
|
2021-12-25 10:04:45 +08:00
|
|
|
{ \
|
|
|
|
CLASSNAME* ease = new CLASSNAME(); \
|
|
|
|
if (ease->initWithAction(action, period)) \
|
|
|
|
ease->autorelease(); \
|
|
|
|
else \
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(ease); \
|
2021-12-25 10:04:45 +08:00
|
|
|
return ease; \
|
|
|
|
} \
|
|
|
|
CLASSNAME* CLASSNAME::clone() const \
|
|
|
|
{ \
|
|
|
|
if (_inner) \
|
|
|
|
return CLASSNAME::create(_inner->clone(), _period); \
|
|
|
|
return nullptr; \
|
|
|
|
} \
|
|
|
|
void CLASSNAME::update(float time) { _inner->update(TWEEN_FUNC(time, _period)); } \
|
|
|
|
EaseElastic* CLASSNAME::reverse() const { return REVERSE_CLASSNAME::create(_inner->reverse(), _period); }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
EASEELASTIC_TEMPLATE_IMPL(EaseElasticIn, tweenfunc::elasticEaseIn, EaseElasticOut);
|
|
|
|
EASEELASTIC_TEMPLATE_IMPL(EaseElasticOut, tweenfunc::elasticEaseOut, EaseElasticIn);
|
|
|
|
EASEELASTIC_TEMPLATE_IMPL(EaseElasticInOut, tweenfunc::elasticEaseInOut, EaseElasticInOut);
|
|
|
|
|
|
|
|
//
|
|
|
|
// EaseBezierAction
|
|
|
|
//
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
EaseBezierAction* EaseBezierAction::create(ax::ActionInterval* action)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
EaseBezierAction* ret = new EaseBezierAction();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (ret->initWithAction(action))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete ret;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void EaseBezierAction::setBezierParamer(float p0, float p1, float p2, float p3)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_p0 = p0;
|
|
|
|
_p1 = p1;
|
|
|
|
_p2 = p2;
|
|
|
|
_p3 = p3;
|
|
|
|
}
|
|
|
|
|
|
|
|
EaseBezierAction* EaseBezierAction::clone() const
|
|
|
|
{
|
|
|
|
// no copy constructor
|
|
|
|
if (_inner)
|
|
|
|
{
|
|
|
|
auto ret = EaseBezierAction::create(_inner->clone());
|
|
|
|
if (ret)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ret->setBezierParamer(_p0, _p1, _p2, _p3);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EaseBezierAction::update(float time)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_inner->update(tweenfunc::bezieratFunction(_p0, _p1, _p2, _p3, time));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EaseBezierAction* EaseBezierAction::reverse() const
|
|
|
|
{
|
|
|
|
EaseBezierAction* reverseAction = EaseBezierAction::create(_inner->reverse());
|
2021-12-25 10:04:45 +08:00
|
|
|
reverseAction->setBezierParamer(_p3, _p2, _p1, _p0);
|
2019-11-23 20:27:39 +08:00
|
|
|
return reverseAction;
|
|
|
|
}
|
|
|
|
|
2024-08-26 00:25:33 +08:00
|
|
|
}
|