add ease functions

This commit is contained in:
Haibo 2018-11-21 01:04:34 +08:00 committed by Nomango
parent d88fc5afbe
commit bb03e6df5f
3 changed files with 234 additions and 33 deletions

View File

@ -56,51 +56,96 @@ namespace easy2d
case EaseFunc::Linear:
ease_func_ = math::Linear;
break;
case EaseFunc::EaseIn:
case EaseFunc::In:
ease_func_ = MakeEaseIn(2.f);
break;
case EaseFunc::EaseOut:
case EaseFunc::Out:
ease_func_ = MakeEaseOut(2.f);
break;
case EaseFunc::EaseInOut:
case EaseFunc::InOut:
ease_func_ = MakeEaseInOut(2.f);
break;
case EaseFunc::EaseExponentialIn:
case EaseFunc::ExpoIn:
ease_func_ = math::EaseExponentialIn;
break;
case EaseFunc::EaseExponentialOut:
case EaseFunc::ExpoOut:
ease_func_ = math::EaseExponentialOut;
break;
case EaseFunc::EaseExponentialInOut:
case EaseFunc::ExpoInOut:
ease_func_ = math::EaseExponentialInOut;
break;
case EaseFunc::EaseBounceIn:
case EaseFunc::BounceIn:
ease_func_ = math::EaseBounceIn;
break;
case EaseFunc::EaseBounceOut:
case EaseFunc::BounceOut:
ease_func_ = math::EaseBounceOut;
break;
case EaseFunc::EaseBounceInOut:
case EaseFunc::BounceInOut:
ease_func_ = math::EaseBounceInOut;
break;
case EaseFunc::EaseElasticIn:
case EaseFunc::ElasticIn:
ease_func_ = MakeEaseElasticIn(0.3f);
break;
case EaseFunc::EaseElasticOut:
case EaseFunc::ElasticOut:
ease_func_ = MakeEaseElasticOut(0.3f);
break;
case EaseFunc::EaseElasticInOut:
case EaseFunc::ElasticInOut:
ease_func_ = MakeEaseElasticInOut(0.3f);
break;
case EaseFunc::EaseSineIn:
case EaseFunc::SineIn:
ease_func_ = math::EaseSineIn;
break;
case EaseFunc::EaseSineOut:
case EaseFunc::SineOut:
ease_func_ = math::EaseSineOut;
break;
case EaseFunc::EaseSineInOut:
case EaseFunc::SineInOut:
ease_func_ = math::EaseSineInOut;
break;
case EaseFunc::BackIn:
ease_func_ = math::EaseBackIn;
break;
case EaseFunc::BackOut:
ease_func_ = math::EaseBackOut;
break;
case EaseFunc::BackInOut:
ease_func_ = math::EaseBackInOut;
break;
case EaseFunc::QuadIn:
ease_func_ = math::EaseQuadIn;
break;
case EaseFunc::QuadOut:
ease_func_ = math::EaseQuadOut;
break;
case EaseFunc::QuadInOut:
ease_func_ = math::EaseQuadInOut;
break;
case EaseFunc::CubicIn:
ease_func_ = math::EaseCubicIn;
break;
case EaseFunc::CubicOut:
ease_func_ = math::EaseCubicOut;
break;
case EaseFunc::CubicInOut:
ease_func_ = math::EaseCubicInOut;
break;
case EaseFunc::QuartIn:
ease_func_ = math::EaseQuartIn;
break;
case EaseFunc::QuartOut:
ease_func_ = math::EaseQuartOut;
break;
case EaseFunc::QuartInOut:
ease_func_ = math::EaseQuartInOut;
break;
case EaseFunc::QuintIn:
ease_func_ = math::EaseQuintIn;
break;
case EaseFunc::QuintOut:
ease_func_ = math::EaseQuintOut;
break;
case EaseFunc::QuintInOut:
ease_func_ = math::EaseQuintInOut;
break;
default:
break;
}

View File

@ -25,26 +25,44 @@
namespace easy2d
{
// 缓动函数枚举
// More infomation about ease functions, see https://easings.net
enum class EaseFunc
{
Linear, // 线性
EaseIn, // 由慢变快
EaseOut, // 由快变慢
EaseInOut, // 由慢变快, 再由快变慢
EaseExponentialIn, // 由慢变极快
EaseExponentialOut, // 由极快变慢
EaseExponentialInOut, // 由慢至极快, 再由极快边慢
EaseBounceIn, // 自起点赋予反弹力
EaseBounceOut, // 自终点赋予反弹力
EaseBounceInOut, // 在起点和终点赋予反弹力
EaseElasticIn, // 自起点赋予弹性
EaseElasticOut, // 自终点赋予弹性
EaseElasticInOut, // 再起点和终点赋予弹性
EaseSineIn, // 由快变慢, 采用正弦变换速度
EaseSineOut, // 由慢变快, 采用正弦变换速度
EaseSineInOut, // 由慢至快, 再由快至慢, 采用正弦变换速度
Linear, // 线性
In, // 由慢变快
Out, // 由快变慢
InOut, // 由慢变快, 再由快变慢
ExpoIn, // 由慢变极快
ExpoOut, // 由极快变慢
ExpoInOut, // 由慢至极快, 再由极快边慢
ElasticIn, // 自起点赋予弹性
ElasticOut, // 自终点赋予弹性
ElasticInOut, // 再起点和终点赋予弹性
BounceIn, // 自起点赋予反弹力
BounceOut, // 自终点赋予反弹力
BounceInOut, // 在起点和终点赋予反弹力
BackIn,
BackOut,
BackInOut,
QuadIn,
QuadOut,
QuadInOut,
CubicIn,
CubicOut,
CubicInOut,
QuartIn,
QuartOut,
QuartInOut,
QuintIn,
QuintOut,
QuintInOut,
SineIn,
SineOut,
SineInOut,
};
// 缓动函数
using EaseFunction = std::function<float(float)>;
inline EaseFunction MakeEaseIn(float rate) { return std::bind(math::EaseIn, std::placeholders::_1, rate); }
@ -54,6 +72,8 @@ namespace easy2d
inline EaseFunction MakeEaseElasticOut(float period) { return std::bind(math::EaseElasticOut, std::placeholders::_1, period); }
inline EaseFunction MakeEaseElasticInOut(float period) { return std::bind(math::EaseElasticInOut, std::placeholders::_1, period); }
// 补间动画
class Tween
: public Action
{
@ -65,12 +85,12 @@ namespace easy2d
EaseFunc func
);
// 设置速度变化曲线
// 设置缓动函数
void SetEaseFunction(
EaseFunc func
);
// 自定义速度变化曲线
// 自定义缓动函数
void SetEaseFunction(
EaseFunction func
);

View File

@ -30,6 +30,9 @@ namespace easy2d
return step;
}
// Ease
inline float EaseIn(float step, float rate)
{
return math::Pow(step, rate);
@ -47,6 +50,9 @@ namespace easy2d
return 1.f - .5f * math::Pow(2.f - 2 * step, rate);
}
// Exponential Ease
inline float EaseExponentialIn(float step)
{
return math::Pow(2.f, 10 * (step - 1));
@ -64,6 +70,9 @@ namespace easy2d
return 0.5f * (2 - math::Pow(2, -10 * (step * 2 - 1)));
}
// Bounce Ease
inline float EaseBounceOut(float step)
{
if (step < 1 / 2.75f)
@ -102,6 +111,9 @@ namespace easy2d
}
}
// Elastic Ease
inline float EaseElasticIn(float step, float period)
{
if (step == 0 || step == 1)
@ -132,6 +144,39 @@ namespace easy2d
return math::Pow(2, -10 * step) * math::Sin((step - period / 4) * 360.f / period) * 0.5f + 1;
}
// Back Ease
inline float EaseBackIn(float step)
{
const float overshoot = 1.70158f;
return step * step * ((overshoot + 1) * step - overshoot);
}
inline float EaseBackOut(float step)
{
const float overshoot = 1.70158f;
step = step - 1;
return step * step * ((overshoot + 1) * step + overshoot) + 1;
}
inline float EaseBackInOut(float step)
{
const float overshoot = 1.70158f * 1.525f;
step = step * 2;
if (step < 1)
{
return (step * step * ((overshoot + 1) * step - overshoot)) / 2;
}
step = step - 2;
return (step * step * ((overshoot + 1) * step + overshoot)) / 2 + 1;
}
// Sine Ease
inline float EaseSineIn(float step)
{
return 1.f - math::Cos(step * 90);
@ -146,5 +191,96 @@ namespace easy2d
{
return -0.5f * (math::Cos(step * 180) - 1);
}
// Quad Ease
inline float EaseQuadIn(float step)
{
return step * step;
}
inline float EaseQuadOut(float step)
{
return -1 * step * (step - 2);
}
inline float EaseQuadInOut(float step)
{
step = step * 2;
if (step < 1)
return 0.5f * step * step;
--step;
return -0.5f * (step * (step - 2) - 1);
}
// Cubic Ease
inline float EaseCubicIn(float step)
{
return step * step * step;
}
inline float EaseCubicOut(float step)
{
step -= 1;
return (step * step * step + 1);
}
inline float EaseCubicInOut(float step)
{
step = step * 2;
if (step < 1)
return 0.5f * step * step * step;
step -= 2;
return 0.5f * (step * step * step + 2);
}
// Quart Ease
inline float EaseQuartIn(float step)
{
return step * step * step * step;
}
inline float EaseQuartOut(float step)
{
step -= 1;
return -(step * step * step * step - 1);
}
inline float EaseQuartInOut(float step)
{
step = step * 2;
if (step < 1)
return 0.5f * step * step * step * step;
step -= 2;
return -0.5f * (step * step * step * step - 2);
}
// Quint Ease
inline float EaseQuintIn(float step)
{
return step * step * step * step * step;
}
inline float EaseQuintOut(float step)
{
step -= 1;
return (step * step * step * step * step + 1);
}
inline float EaseQuintInOut(float step)
{
step = step * 2;
if (step < 1)
return 0.5f * step * step * step * step * step;
step -= 2;
return 0.5f * (step * step * step * step * step + 2);
}
}
}