aboutsummaryrefslogtreecommitdiff
path: root/CruUI/timer.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-09-25 13:08:40 +0800
committercrupest <crupest@outlook.com>2018-09-25 13:08:40 +0800
commit4b86554a0354d78efeb40e551eaccaac0fecd1d1 (patch)
treec8a73d848401f523ff91fe8ed1b0887aa88bbfb8 /CruUI/timer.cpp
parentcea138417c54d6cf8043b6334c22e3af957d26f8 (diff)
downloadcru-4b86554a0354d78efeb40e551eaccaac0fecd1d1.tar.gz
cru-4b86554a0354d78efeb40e551eaccaac0fecd1d1.tar.bz2
cru-4b86554a0354d78efeb40e551eaccaac0fecd1d1.zip
Change the structure of project.
Diffstat (limited to 'CruUI/timer.cpp')
-rw-r--r--CruUI/timer.cpp79
1 files changed, 0 insertions, 79 deletions
diff --git a/CruUI/timer.cpp b/CruUI/timer.cpp
deleted file mode 100644
index ce800eee..00000000
--- a/CruUI/timer.cpp
+++ /dev/null
@@ -1,79 +0,0 @@
-#include "timer.h"
-
-namespace cru
-{
- UINT_PTR TimerManager::CreateTimer(const UINT milliseconds, const bool loop, ActionPtr action)
- {
- const auto id = current_count_++;
- ::SetTimer(Application::GetInstance()->GetGodWindow()->GetHandle(), id, milliseconds, nullptr);
- if (loop)
- map_[id] = std::move(action);
- else
- map_[id] = CreateActionPtr([this, action, id]() mutable {
- (*action)();
- this->KillTimer(id);
- });
- return id;
- }
-
- void TimerManager::KillTimer(const UINT_PTR id)
- {
- const auto find_result = map_.find(id);
- if (find_result != map_.cend())
- {
- ::KillTimer(Application::GetInstance()->GetGodWindow()->GetHandle(), id);
- // When timer is killed in tick action, we need to retain the action itself until the action finishes, so InvokeLater!
- InvokeLater([=]
- {
- map_.erase(find_result);
- });
- }
- }
-
- ActionPtr TimerManager::GetAction(const UINT_PTR id)
- {
- const auto find_result = map_.find(id);
- if (find_result == map_.cend())
- return nullptr;
- return find_result->second;
- }
-
- class TimerTaskImpl : public virtual ICancelable
- {
- public:
- explicit TimerTaskImpl(UINT_PTR id);
- TimerTaskImpl(const TimerTaskImpl& other) = delete;
- TimerTaskImpl(TimerTaskImpl&& other) = delete;
- TimerTaskImpl& operator=(const TimerTaskImpl& other) = delete;
- TimerTaskImpl& operator=(TimerTaskImpl&& other) = delete;
- ~TimerTaskImpl() override = default;
-
- void Cancel() override;
-
- private:
- UINT_PTR id_;
- };
-
- TimerTaskImpl::TimerTaskImpl(const UINT_PTR id)
- : id_(id)
- {
-
- }
-
- void TimerTaskImpl::Cancel()
- {
- TimerManager::GetInstance()->KillTimer(id_);
- }
-
- TimerTask SetTimeout(std::chrono::milliseconds milliseconds, ActionPtr action)
- {
- auto id = TimerManager::GetInstance()->CreateTimer(static_cast<UINT>(milliseconds.count()), false, std::move(action));
- return std::make_shared<TimerTaskImpl>(id);
- }
-
- TimerTask SetInterval(std::chrono::milliseconds milliseconds, ActionPtr action)
- {
- auto id = TimerManager::GetInstance()->CreateTimer(static_cast<UINT>(milliseconds.count()), true, std::move(action));
- return std::make_shared<TimerTaskImpl>(id);
- }
-}