From d88fc5afbec4d39fae1ac2512649de9ebaa20a9a Mon Sep 17 00:00:00 2001 From: Haibo Date: Wed, 21 Nov 2018 00:01:37 +0800 Subject: [PATCH] add: EaseFunction maker --- core/base/ActionTween.cpp | 15 +++++++-------- core/base/ActionTween.h | 20 +++++++++++++++----- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/core/base/ActionTween.cpp b/core/base/ActionTween.cpp index 7e402b76..1957a5e9 100644 --- a/core/base/ActionTween.cpp +++ b/core/base/ActionTween.cpp @@ -22,7 +22,6 @@ #include "Geometry.h" #include "base.hpp" #include "Node.h" -#include "../math/ease.hpp" #include #include @@ -58,13 +57,13 @@ namespace easy2d ease_func_ = math::Linear; break; case EaseFunc::EaseIn: - ease_func_ = std::bind(math::EaseIn, std::placeholders::_1, 2.f); + ease_func_ = MakeEaseIn(2.f); break; case EaseFunc::EaseOut: - ease_func_ = std::bind(math::EaseOut, std::placeholders::_1, 2.f); + ease_func_ = MakeEaseOut(2.f); break; case EaseFunc::EaseInOut: - ease_func_ = std::bind(math::EaseInOut, std::placeholders::_1, 2.f); + ease_func_ = MakeEaseInOut(2.f); break; case EaseFunc::EaseExponentialIn: ease_func_ = math::EaseExponentialIn; @@ -85,13 +84,13 @@ namespace easy2d ease_func_ = math::EaseBounceInOut; break; case EaseFunc::EaseElasticIn: - ease_func_ = std::bind(math::EaseElasticIn, std::placeholders::_1, 0.3f); + ease_func_ = MakeEaseElasticIn(0.3f); break; case EaseFunc::EaseElasticOut: - ease_func_ = std::bind(math::EaseElasticOut, std::placeholders::_1, 0.3f); + ease_func_ = MakeEaseElasticOut(0.3f); break; case EaseFunc::EaseElasticInOut: - ease_func_ = std::bind(math::EaseElasticInOut, std::placeholders::_1, 0.3f); + ease_func_ = MakeEaseElasticInOut(0.3f); break; case EaseFunc::EaseSineIn: ease_func_ = math::EaseSineIn; @@ -107,7 +106,7 @@ namespace easy2d } } - void Tween::SetEaseFunction(std::function func) + void Tween::SetEaseFunction(EaseFunction func) { ease_func_ = func; ease_type_ = EaseFunc(-1); diff --git a/core/base/ActionTween.h b/core/base/ActionTween.h index 6e5c5b6f..e32d35f1 100644 --- a/core/base/ActionTween.h +++ b/core/base/ActionTween.h @@ -21,6 +21,7 @@ #pragma once #include "Action.hpp" #include "logs.h" +#include "../math/ease.hpp" namespace easy2d { @@ -44,6 +45,15 @@ namespace easy2d EaseSineInOut, // 由慢至快, 再由快至慢, 采用正弦变换速度 }; + using EaseFunction = std::function; + + inline EaseFunction MakeEaseIn(float rate) { return std::bind(math::EaseIn, std::placeholders::_1, rate); } + inline EaseFunction MakeEaseOut(float rate) { return std::bind(math::EaseOut, std::placeholders::_1, rate); } + inline EaseFunction MakeEaseInOut(float rate) { return std::bind(math::EaseInOut, std::placeholders::_1, rate); } + inline EaseFunction MakeEaseElasticIn(float period) { return std::bind(math::EaseElasticIn, std::placeholders::_1, period); } + 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 { @@ -62,7 +72,7 @@ namespace easy2d // 自定义速度变化曲线 void SetEaseFunction( - std::function func + EaseFunction func ); virtual void Reset() override; @@ -79,10 +89,10 @@ namespace easy2d virtual void UpdateStep(Node* target, float step) = 0; protected: - Duration duration_; - Duration elapsed_; - EaseFunc ease_type_; - std::function ease_func_; + Duration duration_; + Duration elapsed_; + EaseFunc ease_type_; + EaseFunction ease_func_; };