aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-03-28 20:38:52 +0800
committercrupest <crupest@outlook.com>2019-03-28 20:38:52 +0800
commit37216f211b0e22205a3a0d3373d985fc68aea59b (patch)
treef46ef303ee87a8e3814ea8743bd7062d432bfee3 /src
parentd26845d0565f246bf9680810510b365d32772cdb (diff)
downloadcru-37216f211b0e22205a3a0d3373d985fc68aea59b.tar.gz
cru-37216f211b0e22205a3a0d3373d985fc68aea59b.tar.bz2
cru-37216f211b0e22205a3a0d3373d985fc68aea59b.zip
...
Diffstat (limited to 'src')
-rw-r--r--src/ui/animations/animation.cpp172
-rw-r--r--src/ui/animations/animation.hpp130
2 files changed, 0 insertions, 302 deletions
diff --git a/src/ui/animations/animation.cpp b/src/ui/animations/animation.cpp
deleted file mode 100644
index b1f92a3e..00000000
--- a/src/ui/animations/animation.cpp
+++ /dev/null
@@ -1,172 +0,0 @@
-#include "animation.hpp"
-
-#include <utility>
-
-#include "application.hpp"
-
-namespace cru::ui::animations
-{
- namespace details
- {
- class AnimationDelegateImpl;
- constexpr double frame_rate = 60;
- constexpr AnimationTimeUnit frame_step_time = AnimationTimeUnit(1) / frame_rate;
-
-
- class AnimationDelegateImpl : public virtual IAnimationDelegate
- {
- public:
- explicit AnimationDelegateImpl(String tag)
- : tag_(std::move(tag))
- {
-
- }
- AnimationDelegateImpl(const AnimationDelegateImpl& other) = delete;
- AnimationDelegateImpl(AnimationDelegateImpl&& other) = delete;
- AnimationDelegateImpl& operator=(const AnimationDelegateImpl& other) = delete;
- AnimationDelegateImpl& operator=(AnimationDelegateImpl&& other) = delete;
- ~AnimationDelegateImpl() override = default;
-
- void Cancel() override
- {
- AnimationManager::GetInstance()->RemoveAnimation(tag_);
- }
-
- private:
- String tag_;
- };
-
-
- class Animation : public Object
- {
- public:
- 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;
- Animation& operator=(Animation&& other) = delete;
- ~Animation() override;
-
-
- // If finish or invalid, return false.
- bool Step(AnimationTimeUnit time);
-
- String GetTag() const
- {
- return info_.tag;
- }
-
- private:
- const AnimationInfo info_;
- const AnimationDelegatePtr delegate_;
-
- AnimationTimeUnit current_time_ = AnimationTimeUnit::zero();
- };
-
- AnimationManager* AnimationManager::GetInstance()
- {
- return Application::GetInstance()->ResolveSingleton<AnimationManager>([](auto)
- {
- return new AnimationManager{};
- });
- }
-
- AnimationManager::AnimationManager()
- {
-
- }
-
- AnimationManager::~AnimationManager()
- {
- KillTimer();
- }
-
- 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>(std::move(info), delegate);
-
- return delegate;
- }
-
- void AnimationManager::RemoveAnimation(const String& tag)
- {
- const auto find_result = animations_.find(tag);
- if (find_result != animations_.cend())
- animations_.erase(find_result);
-
- if (animations_.empty())
- KillTimer();
- }
-
- void AnimationManager::SetTimer()
- {
- 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_.has_value())
- {
- timer_.value().Cancel();
- timer_ = std::nullopt;
- }
- }
-
- Animation::~Animation()
- {
- 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_ > info_.duration)
- {
- for (const auto& handler : info_.step_handlers)
- handler(delegate_, 1);
- for (const auto& handler : info_.finish_handlers)
- handler();
- return true;
- }
- else
- {
- for (const auto& handler : info_.step_handlers)
- handler(delegate_, current_time_ / info_.duration);
- return false;
- }
- }
-
- }
-
- AnimationDelegatePtr AnimationBuilder::Start()
- {
- CheckValid();
- valid_ = false;
- return details::AnimationManager::GetInstance()->CreateAnimation(std::move(info_));
- }
-}
diff --git a/src/ui/animations/animation.hpp b/src/ui/animations/animation.hpp
deleted file mode 100644
index 2226f021..00000000
--- a/src/ui/animations/animation.hpp
+++ /dev/null
@@ -1,130 +0,0 @@
-#pragma once
-
-// ReSharper disable once CppUnusedIncludeDirective
-#include "pre.hpp"
-
-#include <unordered_map>
-
-#include "base.hpp"
-#include "timer.hpp"
-
-namespace cru::ui::animations
-{
- using AnimationTimeUnit = FloatSecond;
-
- struct IAnimationDelegate : virtual Interface
- {
- virtual void Cancel() = 0;
- };
-
- 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:
- static AnimationManager* GetInstance();
- private:
- AnimationManager();
- public:
- AnimationManager(const AnimationManager& other) = delete;
- AnimationManager(AnimationManager&& other) = delete;
- AnimationManager& operator=(const AnimationManager& other) = delete;
- AnimationManager& operator=(AnimationManager&& other) = delete;
- ~AnimationManager() override;
-
- AnimationDelegatePtr CreateAnimation(AnimationInfo info);
- void RemoveAnimation(const String& tag);
-
- private:
- void SetTimer();
- void KillTimer();
-
- private:
- std::unordered_map<String, AnimationPtr> animations_;
- std::optional<TimerTask> timer_;
- };
- }
-
- class AnimationBuilder : public Object
- {
- public:
- AnimationBuilder(String tag, const AnimationTimeUnit duration)
- : info_(std::move(tag), duration)
- {
-
- }
-
- AnimationBuilder& AddStepHandler(const AnimationStepHandler& handler)
- {
- CheckValid();
- info_.step_handlers.push_back(handler);
- return *this;
- }
-
- AnimationBuilder& AddStartHandler(const AnimationStartHandler& handler)
- {
- CheckValid();
- info_.start_handlers.push_back(handler);
- return *this;
- }
-
- AnimationBuilder& AddFinishHandler(const AnimationFinishHandler& handler)
- {
- CheckValid();
- info_.finish_handlers.push_back(handler);
- return *this;
- }
-
- AnimationBuilder& AddCancelHandler(const AnimationCancelHandler& handler)
- {
- CheckValid();
- info_.cancel_handlers.push_back(handler);
- return *this;
- }
-
- AnimationDelegatePtr Start();
-
- private:
- void CheckValid() const
- {
- if (!valid_)
- throw std::runtime_error("The animation builder is invalid.");
- }
-
- bool valid_ = true;
- details::AnimationInfo info_;
- };
-}