diff options
Diffstat (limited to 'CruUI/ui/animations/animation.h')
-rw-r--r-- | CruUI/ui/animations/animation.h | 176 |
1 files changed, 76 insertions, 100 deletions
diff --git a/CruUI/ui/animations/animation.h b/CruUI/ui/animations/animation.h index fb6ba93e..69b08b0c 100644 --- a/CruUI/ui/animations/animation.h +++ b/CruUI/ui/animations/animation.h @@ -1,131 +1,107 @@ #pragma once -#include <map> +#include <unordered_map> #include "base.h" #include "application.h" #include "timer.h" -#include "builder.h" namespace cru::ui::animations { - class Animation; + using AnimationTimeUnit = FloatSecond; + + using IAnimationDelegate = ICancelable; + using AnimationDelegatePtr = CancelablePtr; - class AnimationManager : public Object - { - public: - static AnimationManager* GetInstance() - { - return Application::GetInstance()->GetAnimationManager(); - } - - public: - AnimationManager(); - AnimationManager(const AnimationManager& other) = delete; - AnimationManager(AnimationManager&& other) = delete; - AnimationManager& operator=(const AnimationManager& other) = delete; - AnimationManager& operator=(AnimationManager&& other) = delete; - ~AnimationManager() override; + using AnimationStepHandlerPtr = FunctionPtr<void(AnimationDelegatePtr, double)>; + using AnimationStartHandlerPtr = FunctionPtr<void(AnimationDelegatePtr)>; - void AddAnimation(Animation* animation); - void RemoveAnimation(Animation* animation); - private: - std::map<String, Animation*> animations_; - std::shared_ptr<ITimerTask> timer_; - std::shared_ptr<Action<>> timer_action_; - }; - - class Animation : public Object + namespace details { - friend class AnimationManager; - protected: - Animation( - String tag, - double duration, - const Vector<std::shared_ptr<Action<Animation*, double>>>& step_handlers, - const Vector<std::shared_ptr<Action<Animation*>>>& start_handlers, - const Vector<std::shared_ptr<Action<Animation*>>>& finish_handlers, - const Vector<std::shared_ptr<Action<Animation*>>>& cancel_handlers - ); + class Animation; + using AnimationPtr = std::unique_ptr<Animation>; - public: - Animation(const Animation& other) = delete; - Animation(Animation&& other) = delete; - Animation& operator=(const Animation& other) = delete; - Animation& operator=(Animation&& other) = delete; - ~Animation() override = default; // The animation will never destroy by users. - - bool Step(double time); - void Cancel(); - String GetTag() const + class AnimationManager : public Object { - return tag_; - } + public: + static AnimationManager* GetInstance() + { + return Application::GetInstance()->GetAnimationManager(); + } - private: - const String tag_; - const double duration_; - Vector<std::shared_ptr<Action<Animation*, double>>> step_handlers_; - Vector<std::shared_ptr<Action<Animation*>>> start_handlers_; - Vector<std::shared_ptr<Action<Animation*>>> finish_handlers_; - Vector<std::shared_ptr<Action<Animation*>>> cancel_handlers_; + public: + AnimationManager(); + AnimationManager(const AnimationManager& other) = delete; + AnimationManager(AnimationManager&& other) = delete; + AnimationManager& operator=(const AnimationManager& other) = delete; + AnimationManager& operator=(AnimationManager&& other) = delete; + ~AnimationManager() override; + + AnimationDelegatePtr CreateAnimation( + String tag, + AnimationTimeUnit duration, + Vector<AnimationStepHandlerPtr> step_handlers, + Vector<AnimationStartHandlerPtr> start_handlers, + Vector<ActionPtr> finish_handlers, + Vector<ActionPtr> cancel_handlers + ); + void RemoveAnimation(const String& tag); - double current_time_ = 0; + private: + void SetTimer(); + void KillTimer(); + private: + std::unordered_map<String, AnimationPtr> animations_; + std::shared_ptr<ICancelable> timer_; + ActionPtr timer_action_; + }; + } + + class AnimationBuilder : public Object + { public: - class Builder : public OneTimeBuilder<Animation> + AnimationBuilder(String tag, const AnimationTimeUnit duration) + : tag(std::move(tag)), duration(duration) { - public: - Builder(String tag, const double duration) - : tag(std::move(tag)), duration(duration) - { - } + } - String tag; - double duration; + String tag; + AnimationTimeUnit duration; - Builder& AddStepHandler(Action<Animation*, double>&& handler) - { - if (IsValid()) - step_handlers_.push_back(std::make_shared<Action<Animation*, double>>(std::move(handler))); - return *this; - } + AnimationBuilder& AddStepHandler(AnimationStepHandlerPtr handler) + { + step_handlers_.push_back(std::move(handler)); + return *this; + } - Builder& AddStartHandler(Action<Animation*>&& handler) - { - if (IsValid()) - start_handlers_.push_back(std::make_shared<Action<Animation*>>(std::move(handler))); - return *this; - } + AnimationBuilder& AddStartHandler(AnimationStartHandlerPtr handler) + { + start_handlers_.push_back(std::move(handler)); + return *this; + } - Builder& AddFinishHandler(Action<Animation*>&& handler) - { - if (IsValid()) - finish_handlers_.push_back(std::make_shared<Action<Animation*>>(std::move(handler))); - return *this; - } + AnimationBuilder& AddFinishHandler(ActionPtr handler) + { + finish_handlers_.push_back(std::move(handler)); + return *this; + } - Builder& AddCancelHandler(Action<Animation*>&& handler) - { - if (IsValid()) - cancel_handlers_.push_back(std::make_shared<Action<Animation*>>(std::move(handler))); - return *this; - } + AnimationBuilder& AddCancelHandler(ActionPtr handler) + { + cancel_handlers_.push_back(std::move(handler)); + return *this; + } - protected: - Animation* OnCreate() override - { - return new Animation(std::move(tag), duration, step_handlers_, start_handlers_, finish_handlers_, cancel_handlers_); - } + AnimationDelegatePtr Start() const; - private: - Vector<std::shared_ptr<Action<Animation*, double>>> step_handlers_; - Vector<std::shared_ptr<Action<Animation*>>> start_handlers_; - Vector<std::shared_ptr<Action<Animation*>>> finish_handlers_; - Vector<std::shared_ptr<Action<Animation*>>> cancel_handlers_; - }; + private: + Vector<AnimationStepHandlerPtr> step_handlers_; + Vector<AnimationStartHandlerPtr> start_handlers_; + Vector<ActionPtr> finish_handlers_; + Vector<ActionPtr> cancel_handlers_; }; } |