From 1dab244aaad8694ba37ef43caedd8c8ba0310c00 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 5 Nov 2018 20:54:48 +0800 Subject: ... --- src/ui/animations/animation.h | 88 +++++++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 32 deletions(-) (limited to 'src/ui/animations/animation.h') diff --git a/src/ui/animations/animation.h b/src/ui/animations/animation.h index 69b08b0c..91a666c9 100644 --- a/src/ui/animations/animation.h +++ b/src/ui/animations/animation.h @@ -10,19 +10,46 @@ namespace cru::ui::animations { using AnimationTimeUnit = FloatSecond; - - using IAnimationDelegate = ICancelable; - using AnimationDelegatePtr = CancelablePtr; + struct IAnimationDelegate : virtual Interface + { + virtual void Cancel() = 0; + }; - using AnimationStepHandlerPtr = FunctionPtr; - using AnimationStartHandlerPtr = FunctionPtr; + using AnimationDelegatePtr = std::shared_ptr; + using AnimationStepHandler = std::function; + using AnimationStartHandler = std::function; + using AnimationFinishHandler = std::function; + using AnimationCancelHandler = std::function; namespace details { class Animation; using AnimationPtr = std::unique_ptr; + class AnimationInfo + { + public: + AnimationInfo(String tag, const AnimationTimeUnit duration) + : tag(std::move(tag)), + duration(duration) + { + + } + AnimationInfo(const AnimationInfo& other) = default; + AnimationInfo(AnimationInfo&& other) = default; + AnimationInfo& operator=(const AnimationInfo& other) = default; + AnimationInfo& operator=(AnimationInfo&& other) = default; + ~AnimationInfo() = default; + + String tag; + AnimationTimeUnit duration; + std::vector step_handlers{}; + std::vector start_handlers{}; + std::vector finish_handlers{}; + std::vector cancel_handlers{}; + }; + class AnimationManager : public Object { public: @@ -39,14 +66,7 @@ namespace cru::ui::animations AnimationManager& operator=(AnimationManager&& other) = delete; ~AnimationManager() override; - AnimationDelegatePtr CreateAnimation( - String tag, - AnimationTimeUnit duration, - Vector step_handlers, - Vector start_handlers, - Vector finish_handlers, - Vector cancel_handlers - ); + AnimationDelegatePtr CreateAnimation(AnimationInfo info); void RemoveAnimation(const String& tag); private: @@ -55,8 +75,7 @@ namespace cru::ui::animations private: std::unordered_map animations_; - std::shared_ptr timer_; - ActionPtr timer_action_; + std::optional timer_; }; } @@ -64,44 +83,49 @@ namespace cru::ui::animations { public: AnimationBuilder(String tag, const AnimationTimeUnit duration) - : tag(std::move(tag)), duration(duration) + : info_(std::move(tag), duration) { } - String tag; - AnimationTimeUnit duration; - - AnimationBuilder& AddStepHandler(AnimationStepHandlerPtr handler) + AnimationBuilder& AddStepHandler(const AnimationStepHandler& handler) { - step_handlers_.push_back(std::move(handler)); + CheckValid(); + info_.step_handlers.push_back(handler); return *this; } - AnimationBuilder& AddStartHandler(AnimationStartHandlerPtr handler) + AnimationBuilder& AddStartHandler(const AnimationStartHandler& handler) { - start_handlers_.push_back(std::move(handler)); + CheckValid(); + info_.start_handlers.push_back(handler); return *this; } - AnimationBuilder& AddFinishHandler(ActionPtr handler) + AnimationBuilder& AddFinishHandler(const AnimationFinishHandler& handler) { - finish_handlers_.push_back(std::move(handler)); + CheckValid(); + info_.finish_handlers.push_back(handler); return *this; } - AnimationBuilder& AddCancelHandler(ActionPtr handler) + AnimationBuilder& AddCancelHandler(const AnimationCancelHandler& handler) { - cancel_handlers_.push_back(std::move(handler)); + CheckValid(); + info_.cancel_handlers.push_back(handler); return *this; } - AnimationDelegatePtr Start() const; + AnimationDelegatePtr Start(); private: - Vector step_handlers_; - Vector start_handlers_; - Vector finish_handlers_; - Vector cancel_handlers_; + void CheckValid() const + { + if (!valid_) + throw std::runtime_error("The animation builder is invalid."); + } + + bool valid_ = true; + details::AnimationInfo info_; }; } -- cgit v1.2.3