diff --git a/src/kiwano/2d/animation/AnimationWrapper.h b/src/kiwano/2d/animation/AnimationWrapper.h index 8deb8c65..8640f51f 100644 --- a/src/kiwano/2d/animation/AnimationWrapper.h +++ b/src/kiwano/2d/animation/AnimationWrapper.h @@ -263,9 +263,9 @@ inline AnimationWrapper RotateTo(kiwano::Duration duration, float rotation) /// @brief 创建自定义动画 /// @param duration 动画时长 /// @param tween_func 动画回调函数 -inline AnimationWrapper Custom(kiwano::Duration duration, TweenFunc tween_func) +inline AnimationWrapper Custom(kiwano::Duration duration, Function tween_func) { - return AnimationWrapper(new CustomAnimation(duration, tween_func)); + return AnimationWrapper(CustomAnimation::Create(duration, tween_func)); } /// \~chinese diff --git a/src/kiwano/2d/animation/CustomAnimation.cpp b/src/kiwano/2d/animation/CustomAnimation.cpp index 3656eef3..57443eaf 100644 --- a/src/kiwano/2d/animation/CustomAnimation.cpp +++ b/src/kiwano/2d/animation/CustomAnimation.cpp @@ -23,29 +23,51 @@ namespace kiwano { -CustomAnimation::CustomAnimation(Duration duration, TweenFunc tween_func) +CustomAnimation::CustomAnimation(Duration duration) : TweenAnimation(duration) - , tween_func_(tween_func) { } -CustomAnimation* CustomAnimation::Clone() const +CustomAnimationPtr CustomAnimation::Create(Duration duration, Function tween_func) { - CustomAnimation* ptr = new CustomAnimation(GetDuration(), tween_func_); - DoClone(ptr); + class CallbackCustomAnimation : public CustomAnimation + { + public: + CallbackCustomAnimation(Duration duration, Function tween_func) + : CustomAnimation(duration) + , tween_func_(tween_func) + { + } + + void Init(Actor* target) override + { + if (!tween_func_) + this->Done(); + } + + void OnAnimationUpdate(Actor* target, float frac) override + { + if (tween_func_) + tween_func_(target, frac); + } + + CallbackCustomAnimation* Clone() const + { + CallbackCustomAnimation* ptr = new CallbackCustomAnimation(GetDuration(), tween_func_); + DoClone(ptr); + return ptr; + } + + private: + Function tween_func_; + }; + CustomAnimationPtr ptr = new CallbackCustomAnimation(duration, tween_func); return ptr; } -void CustomAnimation::Init(Actor* target) -{ - if (!tween_func_) - this->Done(); -} - void CustomAnimation::UpdateTween(Actor* target, float frac) { - if (tween_func_) - tween_func_(target, frac); + OnAnimationUpdate(target, frac); } } // namespace kiwano diff --git a/src/kiwano/2d/animation/CustomAnimation.h b/src/kiwano/2d/animation/CustomAnimation.h index 3c41c745..766ccfd5 100644 --- a/src/kiwano/2d/animation/CustomAnimation.h +++ b/src/kiwano/2d/animation/CustomAnimation.h @@ -30,34 +30,35 @@ KGE_DECLARE_SMART_PTR(CustomAnimation); * @{ */ - -/// \~chinese -/// @brief 补间动画回调函数 -/// @details 在动画更新时回调该函数,第一个参数是执行动画的目标,第二个参数是动画进度(0.0 - 1.0) -using TweenFunc = Function; - /// \~chinese /// @brief 自定义动画 class KGE_API CustomAnimation : public TweenAnimation { public: + /// \~chinese + /// @brief 创建自定义动画 + /// @param duration 动画时长 + CustomAnimation(Duration duration); + /// \~chinese /// @brief 创建自定义动画 /// @param duration 动画时长 /// @param tween_func 动画回调函数 - CustomAnimation(Duration duration, TweenFunc tween_func); + static CustomAnimationPtr Create(Duration duration, Function tween_func); /// \~chinese - /// @brief 获取动画回调函数 - TweenFunc GetTweenFunc() const; - - /// \~chinese - /// @brief 设置动画回调函数 - void SetTweenFunc(const TweenFunc& tween_func); + /// @brief 动画更新时 + /// @param target 执行动画的目标 + /// @param frac 动画进度(0.0 - 1.0) + virtual void OnAnimationUpdate(Actor* target, float frac) = 0; /// \~chinese /// @brief 获取该动画的拷贝对象 - CustomAnimation* Clone() const override; + CustomAnimation* Clone() const override + { + KGE_ERRORF("Clone() not supported in CustomAnimation"); + return nullptr; + } /// \~chinese /// @brief 获取该动画的倒转 @@ -68,26 +69,9 @@ public: } protected: - void Init(Actor* target) override; - void UpdateTween(Actor* target, float frac) override; - -private: - TweenFunc tween_func_; }; /** @} */ - - -inline TweenFunc CustomAnimation::GetTweenFunc() const -{ - return tween_func_; -} - -inline void CustomAnimation::SetTweenFunc(const TweenFunc& tween_func) -{ - tween_func_ = tween_func; -} - } // namespace kiwano