aboutsummaryrefslogtreecommitdiff
path: root/src/ui/animations
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-11-05 20:54:48 +0800
committercrupest <crupest@outlook.com>2018-11-05 20:54:48 +0800
commit1dab244aaad8694ba37ef43caedd8c8ba0310c00 (patch)
treef70f6489a0f88520a0bdc095cd9713d03f83687b /src/ui/animations
parent252519effe30881825dd02e26dc41bd9cde34782 (diff)
downloadcru-1dab244aaad8694ba37ef43caedd8c8ba0310c00.tar.gz
cru-1dab244aaad8694ba37ef43caedd8c8ba0310c00.tar.bz2
cru-1dab244aaad8694ba37ef43caedd8c8ba0310c00.zip
...
Diffstat (limited to 'src/ui/animations')
-rw-r--r--src/ui/animations/animation.cpp114
-rw-r--r--src/ui/animations/animation.h88
2 files changed, 99 insertions, 103 deletions
diff --git a/src/ui/animations/animation.cpp b/src/ui/animations/animation.cpp
index 9d05860a..ca0fe8bc 100644
--- a/src/ui/animations/animation.cpp
+++ b/src/ui/animations/animation.cpp
@@ -1,6 +1,5 @@
#include "animation.h"
-#include <cassert>
#include <utility>
namespace cru::ui::animations
@@ -39,15 +38,12 @@ namespace cru::ui::animations
class Animation : public Object
{
public:
- Animation(
- String tag,
- AnimationTimeUnit duration,
- Vector<AnimationStepHandlerPtr> step_handlers,
- Vector<AnimationStartHandlerPtr> start_handlers,
- Vector<ActionPtr> finish_handlers,
- Vector<ActionPtr> cancel_handlers,
- AnimationDelegatePtr delegate
- );
+ Animation(AnimationInfo info, AnimationDelegatePtr delegate)
+ : info_(std::move(info)), delegate_(std::move(delegate))
+ {
+
+ }
+
Animation(const Animation& other) = delete;
Animation(Animation&& other) = delete;
Animation& operator=(const Animation& other) = delete;
@@ -60,35 +56,17 @@ namespace cru::ui::animations
String GetTag() const
{
- return tag_;
+ return info_.tag;
}
private:
- const String tag_;
- const AnimationTimeUnit duration_;
- Vector<AnimationStepHandlerPtr> step_handlers_;
- Vector<AnimationStartHandlerPtr> start_handlers_;
- Vector<ActionPtr> finish_handlers_;
- Vector<ActionPtr> cancel_handlers_;
- AnimationDelegatePtr delegate_;
+ const AnimationInfo info_;
+ const AnimationDelegatePtr delegate_;
AnimationTimeUnit current_time_ = AnimationTimeUnit::zero();
};
AnimationManager::AnimationManager()
- : timer_action_(CreateActionPtr([this]()
- {
- auto i = animations_.cbegin();
- while (i != animations_.cend())
- {
- auto current_i = i++;
- if (current_i->second->Step(frame_step_time))
- animations_.erase(current_i);
- }
-
- if (animations_.empty())
- KillTimer();
- }))
{
}
@@ -98,16 +76,14 @@ namespace cru::ui::animations
KillTimer();
}
- AnimationDelegatePtr AnimationManager::CreateAnimation(String tag, AnimationTimeUnit duration,
- Vector<AnimationStepHandlerPtr> step_handlers, Vector<AnimationStartHandlerPtr> start_handlers,
- Vector<ActionPtr> finish_handlers, Vector<ActionPtr> cancel_handlers)
+ AnimationDelegatePtr AnimationManager::CreateAnimation(AnimationInfo info)
{
if (animations_.empty())
SetTimer();
+ const auto tag = info.tag;
auto delegate = std::make_shared<AnimationDelegateImpl>(tag);
-
- animations_[tag] = std::make_unique<Animation>(tag, duration, std::move(step_handlers), std::move(start_handlers), std::move(finish_handlers), std::move(cancel_handlers), delegate);
+ animations_[tag] = std::make_unique<Animation>(std::move(info), delegate);
return delegate;
}
@@ -124,67 +100,63 @@ namespace cru::ui::animations
void AnimationManager::SetTimer()
{
- if (timer_ == nullptr)
- timer_ = SetInterval(std::chrono::duration_cast<std::chrono::milliseconds>(frame_step_time), timer_action_);
+ if (!timer_.has_value())
+ timer_ = SetInterval(std::chrono::duration_cast<std::chrono::milliseconds>(frame_step_time), [this]()
+ {
+ auto i = animations_.cbegin();
+ while (i != animations_.cend())
+ {
+ auto current_i = i++;
+ if (current_i->second->Step(frame_step_time))
+ animations_.erase(current_i);
+ }
+
+ if (animations_.empty())
+ KillTimer();
+ });
}
void AnimationManager::KillTimer()
{
- if (timer_ != nullptr)
+ if (timer_.has_value())
{
- timer_->Cancel();
- timer_ = nullptr;
+ timer_.value().Cancel();
+ timer_ = std::nullopt;
}
}
- Animation::Animation(
- String tag,
- AnimationTimeUnit duration,
- Vector<AnimationStepHandlerPtr> step_handlers,
- Vector<AnimationStartHandlerPtr> start_handlers,
- Vector<ActionPtr> finish_handlers,
- Vector<ActionPtr> cancel_handlers,
- AnimationDelegatePtr delegate
- ) : tag_(std::move(tag)), duration_(duration),
- step_handlers_(std::move(step_handlers)),
- start_handlers_(std::move(start_handlers)),
- finish_handlers_(std::move(finish_handlers)),
- cancel_handlers_(std::move(cancel_handlers)),
- delegate_(std::move(delegate))
- {
-
- }
-
Animation::~Animation()
{
- if (current_time_ < duration_)
- for (auto& handler : cancel_handlers_)
- (*handler)();
+ if (current_time_ < info_.duration)
+ for (const auto& handler : info_.cancel_handlers)
+ handler();
}
bool Animation::Step(const AnimationTimeUnit time)
{
current_time_ += time;
- if (current_time_ > duration_)
+ if (current_time_ > info_.duration)
{
- for (auto& handler : step_handlers_)
- (*handler)(delegate_, 1);
- for (auto& handler : finish_handlers_)
- (*handler)();
+ for (const auto& handler : info_.step_handlers)
+ handler(delegate_, 1);
+ for (const auto& handler : info_.finish_handlers)
+ handler();
return true;
}
else
{
- for (auto& handler : step_handlers_)
- (*handler)(delegate_, current_time_ / duration_);
+ for (const auto& handler : info_.step_handlers)
+ handler(delegate_, current_time_ / info_.duration);
return false;
}
}
}
- AnimationDelegatePtr AnimationBuilder::Start() const
+ AnimationDelegatePtr AnimationBuilder::Start()
{
- return details::AnimationManager::GetInstance()->CreateAnimation(tag, duration, step_handlers_, start_handlers_, finish_handlers_, cancel_handlers_);
+ CheckValid();
+ valid_ = false;
+ return details::AnimationManager::GetInstance()->CreateAnimation(std::move(info_));
}
}
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<void(AnimationDelegatePtr, double)>;
- using AnimationStartHandlerPtr = FunctionPtr<void(AnimationDelegatePtr)>;
+ using AnimationDelegatePtr = std::shared_ptr<IAnimationDelegate>;
+ using AnimationStepHandler = std::function<void(AnimationDelegatePtr, double)>;
+ using AnimationStartHandler = std::function<void(AnimationDelegatePtr)>;
+ using AnimationFinishHandler = std::function<void()>;
+ using AnimationCancelHandler = std::function<void()>;
namespace details
{
class Animation;
using AnimationPtr = std::unique_ptr<Animation>;
+ 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<AnimationStepHandler> step_handlers{};
+ std::vector<AnimationStartHandler> start_handlers{};
+ std::vector<AnimationFinishHandler> finish_handlers{};
+ std::vector<AnimationCancelHandler> 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<AnimationStepHandlerPtr> step_handlers,
- Vector<AnimationStartHandlerPtr> start_handlers,
- Vector<ActionPtr> finish_handlers,
- Vector<ActionPtr> cancel_handlers
- );
+ AnimationDelegatePtr CreateAnimation(AnimationInfo info);
void RemoveAnimation(const String& tag);
private:
@@ -55,8 +75,7 @@ namespace cru::ui::animations
private:
std::unordered_map<String, AnimationPtr> animations_;
- std::shared_ptr<ICancelable> timer_;
- ActionPtr timer_action_;
+ std::optional<TimerTask> 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<AnimationStepHandlerPtr> step_handlers_;
- Vector<AnimationStartHandlerPtr> start_handlers_;
- Vector<ActionPtr> finish_handlers_;
- Vector<ActionPtr> cancel_handlers_;
+ void CheckValid() const
+ {
+ if (!valid_)
+ throw std::runtime_error("The animation builder is invalid.");
+ }
+
+ bool valid_ = true;
+ details::AnimationInfo info_;
};
}