From ad5a9ed52fd005a5c0ed97e95d3ed903c4c056c5 Mon Sep 17 00:00:00 2001 From: Nomango Date: Fri, 7 Sep 2018 18:20:07 +0800 Subject: [PATCH] refactoring: Task Class --- core/e2dobject.h | 94 ++++++++++++++++++- core/e2dtool.h | 106 --------------------- core/modules/Game.cpp | 7 +- core/objects/Node.cpp | 115 ++++++++++++++++++++++- core/{tools => objects}/Task.cpp | 7 +- core/tools/Timer.cpp | 129 -------------------------- project/vs2012/Easy2D.vcxproj | 3 +- project/vs2012/Easy2D.vcxproj.filters | 9 +- project/vs2013/Easy2D.vcxproj | 3 +- project/vs2013/Easy2D.vcxproj.filters | 9 +- project/vs2017/Easy2D.vcxproj | 3 +- project/vs2017/Easy2D.vcxproj.filters | 9 +- 12 files changed, 226 insertions(+), 268 deletions(-) rename core/{tools => objects}/Task.cpp (92%) delete mode 100644 core/tools/Timer.cpp diff --git a/core/e2dobject.h b/core/e2dobject.h index 1033777f..ce893f58 100644 --- a/core/e2dobject.h +++ b/core/e2dobject.h @@ -305,6 +305,59 @@ namespace e2d }; + // 定时任务 + class Task : + public Ref + { + friend class Node; + + public: + explicit Task( + const Function& func, /* 执行函数 */ + const String& name = L"" /* 任务名称 */ + ); + + explicit Task( + const Function& func, /* 执行函数 */ + float delay, /* 时间间隔(秒) */ + int times = -1, /* 执行次数(设 -1 为永久执行) */ + const String& name = L"" /* 任务名称 */ + ); + + // 启动任务 + void Start(); + + // 停止任务 + void Stop(); + + // 任务是否正在执行 + bool IsRunning() const; + + // 获取任务名称 + const String& GetName() const; + + // 任务是否就绪 + bool IsReady() const; + + // 执行任务 + void Update(); + + // 重置计时 + void ResetTime(); + + protected: + bool running_; + bool stopped_; + int run_times_; + int total_times_; + String name_; + Duration delay_; + Time last_time_; + Function callback_; + Node * target_; + }; + + // 绘图接口 class Drawable { @@ -380,6 +433,7 @@ namespace e2d public: typedef std::vector Nodes; typedef std::vector Actions; + typedef std::vector Tasks; Node(); @@ -713,8 +767,40 @@ namespace e2d // 获取所有动作 const Actions& GetAllActions() const; - // 刷新动作进度 - void UpdateActionsTime(); + // 添加任务 + void AddTask( + Task * task + ); + + // 启动任务 + void StartTasks( + const String& task_name + ); + + // 停止任务 + void StopTasks( + const String& task_name + ); + + // 移除任务 + void RemoveTasks( + const String& task_name + ); + + // 启动所有任务 + void StartAllTasks(); + + // 停止所有任务 + void StopAllTasks(); + + // 移除所有任务 + void RemoveAllTasks(); + + // 获取所有任务 + const Tasks& GetAllTasks() const; + + // 更新节点时间 + void UpdateTime(); // 分发鼠标消息 virtual bool Dispatch( @@ -757,6 +843,9 @@ namespace e2d // 更新动作 void UpdateActions(); + // 更新任务 + void UpdateTasks(); + protected: String name_; size_t hash_name_; @@ -780,6 +869,7 @@ namespace e2d Property extrapolate_; Color border_color_; Actions actions_; + Tasks tasks_; Nodes children_; ID2D1Geometry* border_; D2D1::Matrix3x2F initial_matrix_; diff --git a/core/e2dtool.h b/core/e2dtool.h index 35e0b6e7..7ec4168c 100644 --- a/core/e2dtool.h +++ b/core/e2dtool.h @@ -251,112 +251,6 @@ namespace e2d }; - class Timer; - - // 定时任务 - class Task : - public Ref - { - friend class Timer; - - public: - explicit Task( - const Function& func, /* 执行函数 */ - const String& name = L"" /* 任务名称 */ - ); - - explicit Task( - const Function& func, /* 执行函数 */ - float delay, /* 时间间隔(秒) */ - int times = -1, /* 执行次数(设 -1 为永久执行) */ - const String& name = L"" /* 任务名称 */ - ); - - // 启动任务 - void Start(); - - // 停止任务 - void Stop(); - - // 任务是否正在执行 - bool IsRunning() const; - - // 获取任务名称 - const String& GetName() const; - - protected: - // 执行任务 - void Update(); - - // 任务是否就绪 - bool IsReady() const; - - protected: - bool running_; - bool stopped_; - int run_times_; - int total_times_; - String name_; - Duration delay_; - Time last_time_; - Function callback_; - }; - - - // 定时器 - class Timer - { - public: - // 获取定时器实例 - static Timer * GetInstance(); - - // 添加任务 - void AddTask( - Task * task - ); - - // 启动任务 - void StartTasks( - const String& task_name - ); - - // 停止任务 - void StopTasks( - const String& task_name - ); - - // 移除任务 - void RemoveTasks( - const String& task_name - ); - - // 启动所有任务 - void StartAllTasks(); - - // 停止所有任务 - void StopAllTasks(); - - // 移除所有任务 - void RemoveAllTasks(); - - // 更新定时器 - void Update(); - - // 刷新所有任务计时 - void UpdateTime(); - - private: - Timer(); - - ~Timer(); - - E2D_DISABLE_COPY(Timer); - - private: - std::vector tasks_; - }; - - // 数据管理工具 class Data { diff --git a/core/modules/Game.cpp b/core/modules/Game.cpp index 8809dae8..b5e549a6 100644 --- a/core/modules/Game.cpp +++ b/core/modules/Game.cpp @@ -2,7 +2,6 @@ #include "..\e2dobject.h" #include "..\e2dtransition.h" #include "..\e2dmanager.h" -#include "..\e2dtool.h" #include @@ -50,7 +49,6 @@ void e2d::Game::Start() auto window = Window::GetInstance(); auto input = Input::GetInstance(); auto renderer = Renderer::GetInstance(); - auto timer = Timer::GetInstance(); const int minInterval = 5; Time last = Time::Now(); @@ -103,9 +101,10 @@ void e2d::Game::Resume() { if (paused_ && !quit_) { - Timer::GetInstance()->UpdateTime(); if (curr_scene_) - curr_scene_->GetRoot()->UpdateActionsTime(); + { + curr_scene_->GetRoot()->UpdateTime(); + } } paused_ = false; } diff --git a/core/objects/Node.cpp b/core/objects/Node.cpp index 9db5f33f..4ecdf114 100644 --- a/core/objects/Node.cpp +++ b/core/objects/Node.cpp @@ -52,6 +52,7 @@ e2d::Node::Node() , anchor_() , children_() , actions_() + , tasks_() , initial_matrix_(D2D1::Matrix3x2F::Identity()) , final_matrix_(D2D1::Matrix3x2F::Identity()) , border_color_(Color::Red, 0.6f) @@ -82,6 +83,7 @@ void e2d::Node::Visit() if (!Game::GetInstance()->IsPaused()) { UpdateActions(); + UpdateTasks(); auto updatableNode = dynamic_cast(this); if (updatableNode) @@ -978,16 +980,125 @@ const e2d::Node::Actions & e2d::Node::GetAllActions() const return actions_; } -void e2d::Node::UpdateActionsTime() +void e2d::Node::AddTask(Task * task) +{ + if (task) + { + auto iter = std::find(tasks_.begin(), tasks_.end(), task); + if (iter == tasks_.end()) + { + task->Retain(); + task->last_time_ = Time::Now(); + tasks_.push_back(task); + } + } +} + +void e2d::Node::StopTasks(const String& name) +{ + for (const auto& task : tasks_) + { + if (task->GetName() == name) + { + task->Stop(); + } + } +} + +void e2d::Node::StartTasks(const String& name) +{ + for (const auto& task : tasks_) + { + if (task->GetName() == name) + { + task->Start(); + } + } +} + +void e2d::Node::RemoveTasks(const String& name) +{ + for (const auto& task : tasks_) + { + if (task->GetName() == name) + { + task->stopped_ = true; + } + } +} + +void e2d::Node::StopAllTasks() +{ + for (const auto& task : tasks_) + { + task->Stop(); + } +} + +void e2d::Node::StartAllTasks() +{ + for (const auto& task : tasks_) + { + task->Start(); + } +} + +void e2d::Node::RemoveAllTasks() +{ + for (const auto& task : tasks_) + { + task->stopped_ = true; + } +} + +void e2d::Node::UpdateTasks() +{ + if (tasks_.empty()) + return; + + std::vector currTasks; + currTasks.reserve(tasks_.size()); + std::copy_if( + tasks_.begin(), + tasks_.end(), + std::back_inserter(currTasks), + [](Task* task) { return task->IsReady() && !task->stopped_; } + ); + + // 遍历就绪的任务 + for (const auto& task : currTasks) + task->Update(); + + // 清除结束的任务 + for (auto iter = tasks_.begin(); iter != tasks_.end();) + { + if ((*iter)->stopped_) + { + (*iter)->Release(); + iter = tasks_.erase(iter); + } + else + { + ++iter; + } + } +} + +void e2d::Node::UpdateTime() { for (const auto& action : actions_) { action->ResetTime(); } + for (const auto& task : tasks_) + { + task->ResetTime(); + } + for (const auto& child : children_) { - child->UpdateActionsTime(); + child->UpdateTime(); } } diff --git a/core/tools/Task.cpp b/core/objects/Task.cpp similarity index 92% rename from core/tools/Task.cpp rename to core/objects/Task.cpp index 5e11f18d..14cc0915 100644 --- a/core/tools/Task.cpp +++ b/core/objects/Task.cpp @@ -1,4 +1,4 @@ -#include "..\e2dtool.h" +#include "..\e2dobject.h" e2d::Task::Task(const Function & func, const String & name) @@ -57,6 +57,11 @@ void e2d::Task::Update() } } +void e2d::Task::ResetTime() +{ + last_time_ = Time::Now(); +} + bool e2d::Task::IsReady() const { if (running_) diff --git a/core/tools/Timer.cpp b/core/tools/Timer.cpp deleted file mode 100644 index 35d96c4a..00000000 --- a/core/tools/Timer.cpp +++ /dev/null @@ -1,129 +0,0 @@ -#include "..\e2dtool.h" - - -e2d::Timer * e2d::Timer::GetInstance() -{ - static Timer instance; - return &instance; -} - -e2d::Timer::Timer() - : tasks_() -{ -} - -e2d::Timer::~Timer() -{ -} - -void e2d::Timer::AddTask(Task * task) -{ - if (task) - { - auto iter = std::find(tasks_.begin(), tasks_.end(), task); - if (iter == tasks_.end()) - { - task->Retain(); - task->last_time_ = Time::Now(); - tasks_.push_back(task); - } - } -} - -void e2d::Timer::StopTasks(const String& name) -{ - for (const auto& task : tasks_) - { - if (task->GetName() == name) - { - task->Stop(); - } - } -} - -void e2d::Timer::StartTasks(const String& name) -{ - for (const auto& task : tasks_) - { - if (task->GetName() == name) - { - task->Start(); - } - } -} - -void e2d::Timer::RemoveTasks(const String& name) -{ - for (const auto& task : tasks_) - { - if (task->GetName() == name) - { - task->stopped_ = true; - } - } -} - -void e2d::Timer::StopAllTasks() -{ - for (const auto& task : tasks_) - { - task->Stop(); - } -} - -void e2d::Timer::StartAllTasks() -{ - for (const auto& task : tasks_) - { - task->Start(); - } -} - -void e2d::Timer::RemoveAllTasks() -{ - for (const auto& task : tasks_) - { - task->stopped_ = true; - } -} - -void e2d::Timer::Update() -{ - if (tasks_.empty()) - return; - - std::vector currTasks; - currTasks.reserve(tasks_.size()); - std::copy_if( - tasks_.begin(), - tasks_.end(), - std::back_inserter(currTasks), - [](Task* task) { return task->IsReady() && !task->stopped_; } - ); - - // 遍历就绪的任务 - for (const auto& task : currTasks) - task->Update(); - - // 清除结束的任务 - for (auto iter = tasks_.begin(); iter != tasks_.end();) - { - if ((*iter)->stopped_) - { - (*iter)->Release(); - iter = tasks_.erase(iter); - } - else - { - ++iter; - } - } -} - -void e2d::Timer::UpdateTime() -{ - for (const auto& task : tasks_) - { - task->last_time_ = Time::Now(); - } -} diff --git a/project/vs2012/Easy2D.vcxproj b/project/vs2012/Easy2D.vcxproj index a361043e..90e188cf 100644 --- a/project/vs2012/Easy2D.vcxproj +++ b/project/vs2012/Easy2D.vcxproj @@ -64,14 +64,13 @@ + - - diff --git a/project/vs2012/Easy2D.vcxproj.filters b/project/vs2012/Easy2D.vcxproj.filters index 9c84c07e..6e7ce3ab 100644 --- a/project/vs2012/Easy2D.vcxproj.filters +++ b/project/vs2012/Easy2D.vcxproj.filters @@ -195,12 +195,6 @@ tools - - tools - - - tools - transitions @@ -237,6 +231,9 @@ objects + + objects + utils diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index a2ccd3c1..c55111d2 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -208,14 +208,13 @@ + - - diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index 9c84c07e..6e7ce3ab 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -195,12 +195,6 @@ tools - - tools - - - tools - transitions @@ -237,6 +231,9 @@ objects + + objects + utils diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index 83a4de8f..77ae3978 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -241,14 +241,13 @@ + - - diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 9c84c07e..6e7ce3ab 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -195,12 +195,6 @@ tools - - tools - - - tools - transitions @@ -237,6 +231,9 @@ objects + + objects + utils