From b4a37d0714b05f883e86633f34de495d92563df1 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Wed, 12 Jun 2019 13:24:04 +0800 Subject: [PATCH] add ActionCustom --- Kiwano/2d/ActionHelper.h | 6 ++++++ Kiwano/2d/ActionTween.cpp | 39 ++++++++++++++++++++++++++++++++++----- Kiwano/2d/ActionTween.h | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 5 deletions(-) diff --git a/Kiwano/2d/ActionHelper.h b/Kiwano/2d/ActionHelper.h index 7bfcd099..5e1ad8b4 100644 --- a/Kiwano/2d/ActionHelper.h +++ b/Kiwano/2d/ActionHelper.h @@ -205,6 +205,12 @@ namespace kiwano return TweenHelper(new kiwano::Animation(0, frames)); } + static inline TweenHelper + Custom(kiwano::ActionCustom::TweenFunc tween_func) + { + return TweenHelper(new kiwano::ActionCustom(0, tween_func)); + } + static inline ActionHelper Delay(Duration delay) { diff --git a/Kiwano/2d/ActionTween.cpp b/Kiwano/2d/ActionTween.cpp index aff69266..13fd5e1a 100644 --- a/Kiwano/2d/ActionTween.cpp +++ b/Kiwano/2d/ActionTween.cpp @@ -259,16 +259,16 @@ namespace kiwano ActionScaleBy::ActionScaleBy(Duration duration, float scale, EaseFunc func) : ActionTween(duration, func) + , delta_x_(scale) + , delta_y_(scale) { - delta_x_ = scale; - delta_y_ = scale; } ActionScaleBy::ActionScaleBy(Duration duration, float scale_x, float scale_y, EaseFunc func) : ActionTween(duration, func) + , delta_x_(scale_x) + , delta_y_(scale_y) { - delta_x_ = scale_x; - delta_y_ = scale_y; } void ActionScaleBy::Init(NodePtr target) @@ -328,8 +328,8 @@ namespace kiwano ActionOpacityBy::ActionOpacityBy(Duration duration, float opacity, EaseFunc func) : ActionTween(duration, func) + , delta_val_(opacity) { - delta_val_ = opacity; } void ActionOpacityBy::Init(NodePtr target) @@ -389,6 +389,7 @@ namespace kiwano ActionRotateBy::ActionRotateBy(Duration duration, float rotation, EaseFunc func) : ActionTween(duration, func) + , start_val_() , delta_val_(rotation) { } @@ -485,6 +486,34 @@ namespace kiwano } + //------------------------------------------------------- + // ActionCustom + //------------------------------------------------------- + + ActionCustom::ActionCustom(Duration duration, TweenFunc tween_func, EaseFunc func) + : ActionTween(duration, func) + , tween_func_(tween_func) + { + } + + ActionPtr ActionCustom::Clone() const + { + return new ActionCustom(dur_, tween_func_); + } + + void ActionCustom::Init(NodePtr target) + { + if (!tween_func_) + this->Done(); + } + + void ActionCustom::UpdateTween(NodePtr target, float percent) + { + if (tween_func_) + tween_func_(target, percent); + } + + //------------------------------------------------------- // ActionDelay //------------------------------------------------------- diff --git a/Kiwano/2d/ActionTween.h b/Kiwano/2d/ActionTween.h index 69d1ca09..5c671a9c 100644 --- a/Kiwano/2d/ActionTween.h +++ b/Kiwano/2d/ActionTween.h @@ -470,6 +470,39 @@ namespace kiwano }; + // 自定义动作 + class KGE_API ActionCustom + : public ActionTween + { + public: + using TweenFunc = Closure; + + ActionCustom( + Duration duration, /* 持续时长 */ + TweenFunc tween_func, /* 过程函数 */ + EaseFunc func = nullptr /* 速度变化 */ + ); + + // 获取该动作的拷贝对象 + ActionPtr Clone() const override; + + // 获取该动作的倒转 + ActionPtr Reverse() const override + { + KGE_ERROR_LOG(L"Reverse() not supported in ActionCustom"); + return nullptr; + } + + protected: + void Init(NodePtr target) override; + + void UpdateTween(NodePtr target, float percent) override; + + protected: + TweenFunc tween_func_; + }; + + // 延时动作 class KGE_API ActionDelay : public Action