diff --git a/projects/kiwano/kiwano.vcxproj b/projects/kiwano/kiwano.vcxproj index 0c019d8f..c6948709 100644 --- a/projects/kiwano/kiwano.vcxproj +++ b/projects/kiwano/kiwano.vcxproj @@ -53,8 +53,8 @@ - - + + @@ -139,8 +139,8 @@ - - + + diff --git a/projects/kiwano/kiwano.vcxproj.filters b/projects/kiwano/kiwano.vcxproj.filters index e4220660..75592486 100644 --- a/projects/kiwano/kiwano.vcxproj.filters +++ b/projects/kiwano/kiwano.vcxproj.filters @@ -69,12 +69,6 @@ core - - core - - - core - 2d @@ -327,6 +321,12 @@ render + + core + + + core + @@ -350,12 +350,6 @@ platform - - core - - - core - 2d @@ -539,6 +533,12 @@ render + + core + + + core + diff --git a/src/kiwano/2d/Actor.cpp b/src/kiwano/2d/Actor.cpp index 3eca99a6..6e9a0340 100644 --- a/src/kiwano/2d/Actor.cpp +++ b/src/kiwano/2d/Actor.cpp @@ -79,7 +79,7 @@ void Actor::Update(Duration dt) { UpdateActions(this, dt); UpdateComponents(dt); - UpdateTimers(dt); + UpdateTasks(dt); if (!update_pausing_) { diff --git a/src/kiwano/2d/Actor.h b/src/kiwano/2d/Actor.h index 459f1f34..0c552b20 100644 --- a/src/kiwano/2d/Actor.h +++ b/src/kiwano/2d/Actor.h @@ -21,7 +21,7 @@ #pragma once #include #include -#include +#include #include #include #include @@ -58,11 +58,11 @@ typedef IntrusiveList ActorList; * \~chinese * @brief 角色 * @details - * 角色是舞台上最基本的元素,是完成渲染、更新、事件分发等功能的最小单位,也是动画、定时器、事件监听等功能的载体 + * 角色是舞台上最基本的元素,是完成渲染、更新、事件分发等功能的最小单位,也是动画、任务、事件监听等功能的载体 */ class KGE_API Actor : public ObjectBase - , public TimerManager + , public TaskManager , public ActionManager , public EventDispatcher , protected IntrusiveListValue diff --git a/src/kiwano/core/Timer.cpp b/src/kiwano/core/Task.cpp similarity index 85% rename from src/kiwano/core/Timer.cpp rename to src/kiwano/core/Task.cpp index ab72cb03..cea275b6 100644 --- a/src/kiwano/core/Timer.cpp +++ b/src/kiwano/core/Task.cpp @@ -18,14 +18,14 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#include +#include namespace kiwano { -TimerPtr Timer::Create(const Callback& cb, Duration interval, int times) +TaskPtr Task::Create(const Callback& cb, Duration interval, int times) { - TimerPtr ptr = memory::New(); + TaskPtr ptr = memory::New(); if (ptr) { ptr->SetCallback(cb); @@ -35,9 +35,9 @@ TimerPtr Timer::Create(const Callback& cb, Duration interval, int times) return ptr; } -TimerPtr Timer::Create(const String& name, const Callback& cb, Duration interval, int times) +TaskPtr Task::Create(const String& name, const Callback& cb, Duration interval, int times) { - TimerPtr ptr = memory::New(); + TaskPtr ptr = memory::New(); if (ptr) { ptr->SetName(name); @@ -48,7 +48,7 @@ TimerPtr Timer::Create(const String& name, const Callback& cb, Duration interval return ptr; } -Timer::Timer() +Task::Task() : running_(true) , removeable_(false) , run_times_(0) @@ -59,7 +59,7 @@ Timer::Timer() { } -void Timer::Update(Duration dt) +void Task::Update(Duration dt) { if (total_times_ == 0) { @@ -87,7 +87,7 @@ void Timer::Update(Duration dt) } } -void Timer::Reset() +void Task::Reset() { elapsed_ = 0; run_times_ = 0; diff --git a/src/kiwano/core/Timer.h b/src/kiwano/core/Task.h similarity index 62% rename from src/kiwano/core/Timer.h rename to src/kiwano/core/Task.h index 07a9480f..8d31ad41 100644 --- a/src/kiwano/core/Timer.h +++ b/src/kiwano/core/Task.h @@ -25,105 +25,105 @@ namespace kiwano { -class TimerManager; +class TaskManager; -KGE_DECLARE_SMART_PTR(Timer); +KGE_DECLARE_SMART_PTR(Task); /// \~chinese -/// @brief 定时器列表 -typedef IntrusiveList TimerList; +/// @brief 任务列表 +typedef IntrusiveList TaskList; /// \~chinese -/// @brief 定时器 -/// @details 定时器用于每隔一段时间执行一次回调函数,且可以指定执行总次数 -class KGE_API Timer +/// @brief 任务 +/// @details 任务用于每隔一段时间执行一次回调函数,且可以指定执行总次数 +class KGE_API Task : public ObjectBase - , protected IntrusiveListValue + , protected IntrusiveListValue { - friend class TimerManager; - friend IntrusiveList; + friend class TaskManager; + friend IntrusiveList; public: /// \~chinese - /// @brief 定时器回调函数 + /// @brief 任务回调函数 /// @details - /// 回调函数第一个参数是定时器自身,第二个参数是距离上次更新的时间间隔 - using Callback = Function; + /// 回调函数第一个参数是任务自身,第二个参数是距离上次执行任务的时间间隔 + using Callback = Function; /// \~chinese - /// @brief 创建定时器 + /// @brief 创建任务 /// @param cb 回调函数 /// @param interval 时间间隔 /// @param times 执行次数(设 -1 为永久执行) - static TimerPtr Create(const Callback& cb, Duration interval, int times = -1); + static TaskPtr Create(const Callback& cb, Duration interval, int times = -1); /// \~chinese - /// @brief 创建定时器 + /// @brief 创建任务 /// @param name 名称 /// @param cb 回调函数 /// @param interval 时间间隔 /// @param times 执行次数(设 -1 为永久执行) - static TimerPtr Create(const String& name, const Callback& cb, Duration interval, int times = -1); + static TaskPtr Create(const String& name, const Callback& cb, Duration interval, int times = -1); /// \~chinese - /// @brief 构造空定时器 - Timer(); + /// @brief 构造空任务 + Task(); /// \~chinese - /// @brief 启动定时器 + /// @brief 启动任务 void Start(); /// \~chinese - /// @brief 停止定时器 + /// @brief 停止任务 void Stop(); /// \~chinese - /// @brief 移除定时器 + /// @brief 移除任务 void Remove(); /// \~chinese - /// @brief 定时器是否在运行 + /// @brief 任务是否在运行 bool IsRunning() const; /// \~chinese - /// @brief 定时器是否可移除 + /// @brief 任务是否可移除 bool IsRemoveable() const; /// \~chinese - /// @brief 获取定时器执行过回调函数的次数 + /// @brief 获取任务执行过回调函数的次数 int GetRunTimes() const; /// \~chinese - /// @brief 获取定时器执行回调函数的总次数 + /// @brief 获取任务执行回调函数的总次数 int GetTotalRunTimes() const; /// \~chinese - /// @brief 设置定时器执行回调函数的总次数 + /// @brief 设置任务执行回调函数的总次数 void SetTotalRunTimes(int times); /// \~chinese - /// @brief 获取定时器执行时间间隔 + /// @brief 获取任务执行时间间隔 Duration GetInterval() const; /// \~chinese - /// @brief 设置定时器执行时间间隔 + /// @brief 设置任务执行时间间隔 void SetInterval(Duration interval); /// \~chinese - /// @brief 获取定时器回调函数 + /// @brief 获取任务回调函数 Callback GetCallback() const; /// \~chinese - /// @brief 设置定时器回调函数 + /// @brief 设置任务回调函数 void SetCallback(const Callback& callback); private: /// \~chinese - /// @brief 更新定时器 + /// @brief 更新任务 void Update(Duration dt); /// \~chinese - /// @brief 重置定时器 + /// @brief 重置任务 void Reset(); private: @@ -136,62 +136,62 @@ private: Callback callback_; }; -inline void Timer::Start() +inline void Task::Start() { running_ = true; } -inline void Timer::Stop() +inline void Task::Stop() { running_ = false; } -inline void Timer::Remove() +inline void Task::Remove() { removeable_ = true; } -inline bool Timer::IsRunning() const +inline bool Task::IsRunning() const { return running_; } -inline bool Timer::IsRemoveable() const +inline bool Task::IsRemoveable() const { return removeable_; } -inline int Timer::GetRunTimes() const +inline int Task::GetRunTimes() const { return run_times_; } -inline int Timer::GetTotalRunTimes() const +inline int Task::GetTotalRunTimes() const { return total_times_; } -inline void Timer::SetTotalRunTimes(int times) +inline void Task::SetTotalRunTimes(int times) { total_times_ = times; } -inline Duration Timer::GetInterval() const +inline Duration Task::GetInterval() const { return interval_; } -inline void Timer::SetInterval(Duration interval) +inline void Task::SetInterval(Duration interval) { interval_ = interval; } -inline Timer::Callback Timer::GetCallback() const +inline Task::Callback Task::GetCallback() const { return callback_; } -inline void Timer::SetCallback(const Timer::Callback& callback) +inline void Task::SetCallback(const Task::Callback& callback) { callback_ = callback; } diff --git a/src/kiwano/core/TaskManager.cpp b/src/kiwano/core/TaskManager.cpp new file mode 100644 index 00000000..ae556aa0 --- /dev/null +++ b/src/kiwano/core/TaskManager.cpp @@ -0,0 +1,140 @@ +// Copyright (c) 2016-2018 Kiwano - Nomango +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include +#include + +namespace kiwano +{ +void TaskManager::UpdateTasks(Duration dt) +{ + if (tasks_.IsEmpty()) + return; + + TaskPtr next; + for (auto task = tasks_.GetFirst(); task; task = next) + { + next = task->GetNext(); + + task->Update(dt); + + if (task->IsRemoveable()) + tasks_.Remove(task); + } +} + +Task* TaskManager::AddTask(const Task::Callback& cb, Duration interval, int times) +{ + return AddTask(String(), cb, interval, times); +} + +Task* TaskManager::AddTask(const String& name, const Task::Callback& cb, Duration interval, int times) +{ + TaskPtr task = Task::Create(name, cb, interval, times); + return AddTask(task); +} + +Task* TaskManager::AddTask(TaskPtr task) +{ + KGE_ASSERT(task && "AddTask failed, NULL pointer exception"); + + if (task) + { + task->Reset(); + tasks_.PushBack(task); + } + + return task.Get(); +} + +void TaskManager::StopTasks(const String& name) +{ + if (tasks_.IsEmpty()) + return; + + for (auto& task : tasks_) + { + if (task->IsName(name)) + { + task->Stop(); + } + } +} + +void TaskManager::StartTasks(const String& name) +{ + if (tasks_.IsEmpty()) + return; + + for (auto& task : tasks_) + { + if (task->IsName(name)) + { + task->Start(); + } + } +} + +void TaskManager::RemoveTasks(const String& name) +{ + if (tasks_.IsEmpty()) + return; + + for (auto& task : tasks_) + { + if (task->IsName(name)) + { + task->Remove(); + } + } +} + +void TaskManager::StopAllTasks() +{ + if (tasks_.IsEmpty()) + return; + + for (auto& task : tasks_) + { + task->Stop(); + } +} + +void TaskManager::StartAllTasks() +{ + if (tasks_.IsEmpty()) + return; + + for (auto& task : tasks_) + { + task->Start(); + } +} + +void TaskManager::RemoveAllTasks() +{ + tasks_.Clear(); +} + +const TaskList& TaskManager::GetAllTasks() const +{ + return tasks_; +} +} // namespace kiwano diff --git a/src/kiwano/core/TimerManager.h b/src/kiwano/core/TaskManager.h similarity index 63% rename from src/kiwano/core/TimerManager.h rename to src/kiwano/core/TaskManager.h index 5834e424..7cc8e495 100644 --- a/src/kiwano/core/TimerManager.h +++ b/src/kiwano/core/TaskManager.h @@ -19,70 +19,70 @@ // THE SOFTWARE. #pragma once -#include +#include namespace kiwano { /** * \~chinese - * @brief 定时器管理器 + * @brief 任务管理器 */ -class KGE_API TimerManager +class KGE_API TaskManager { public: /// \~chinese - /// @brief 添加定时器 + /// @brief 添加任务 /// @param cb 回调函数 /// @param interval 时间间隔 /// @param times 执行次数(设 -1 为永久执行) - Timer* AddTimer(const Timer::Callback& cb, Duration interval, int times = -1); + Task* AddTask(const Task::Callback& cb, Duration interval, int times = -1); /// \~chinese - /// @brief 添加定时器 - /// @param name 定时器名称 + /// @brief 添加任务 + /// @param name 任务名称 /// @param cb 回调函数 /// @param interval 时间间隔 /// @param times 执行次数(设 -1 为永久执行) - Timer* AddTimer(const String& name, const Timer::Callback& cb, Duration interval, int times = -1); + Task* AddTask(const String& name, const Task::Callback& cb, Duration interval, int times = -1); /// \~chinese - /// @brief 添加定时器 - Timer* AddTimer(TimerPtr timer); + /// @brief 添加任务 + Task* AddTask(TaskPtr task); /// \~chinese - /// @brief 启动定时器 - void StartTimers(const String& timer_name); + /// @brief 启动任务 + void StartTasks(const String& task_name); /// \~chinese - /// @brief 停止定时器 - void StopTimers(const String& timer_name); + /// @brief 停止任务 + void StopTasks(const String& task_name); /// \~chinese - /// @brief 移除定时器 - void RemoveTimers(const String& timer_name); + /// @brief 移除任务 + void RemoveTasks(const String& task_name); /// \~chinese - /// @brief 启动所有定时器 - void StartAllTimers(); + /// @brief 启动所有任务 + void StartAllTasks(); /// \~chinese - /// @brief 停止所有定时器 - void StopAllTimers(); + /// @brief 停止所有任务 + void StopAllTasks(); /// \~chinese - /// @brief 移除所有定时器 - void RemoveAllTimers(); + /// @brief 移除所有任务 + void RemoveAllTasks(); /// \~chinese - /// @brief 获取所有定时器 - const TimerList& GetAllTimers() const; + /// @brief 获取所有任务 + const TaskList& GetAllTasks() const; protected: /// \~chinese - /// @brief 更新定时器 - void UpdateTimers(Duration dt); + /// @brief 更新任务 + void UpdateTasks(Duration dt); private: - TimerList timers_; + TaskList tasks_; }; } // namespace kiwano diff --git a/src/kiwano/core/TimerManager.cpp b/src/kiwano/core/TimerManager.cpp deleted file mode 100644 index bf4d5c92..00000000 --- a/src/kiwano/core/TimerManager.cpp +++ /dev/null @@ -1,140 +0,0 @@ -// Copyright (c) 2016-2018 Kiwano - Nomango -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#include -#include - -namespace kiwano -{ -void TimerManager::UpdateTimers(Duration dt) -{ - if (timers_.IsEmpty()) - return; - - TimerPtr next; - for (auto timer = timers_.GetFirst(); timer; timer = next) - { - next = timer->GetNext(); - - timer->Update(dt); - - if (timer->IsRemoveable()) - timers_.Remove(timer); - } -} - -Timer* TimerManager::AddTimer(const Timer::Callback& cb, Duration interval, int times) -{ - return AddTimer(String(), cb, interval, times); -} - -Timer* TimerManager::AddTimer(const String& name, const Timer::Callback& cb, Duration interval, int times) -{ - TimerPtr timer = Timer::Create(name, cb, interval, times); - return AddTimer(timer); -} - -Timer* TimerManager::AddTimer(TimerPtr timer) -{ - KGE_ASSERT(timer && "AddTimer failed, NULL pointer exception"); - - if (timer) - { - timer->Reset(); - timers_.PushBack(timer); - } - - return timer.Get(); -} - -void TimerManager::StopTimers(const String& name) -{ - if (timers_.IsEmpty()) - return; - - for (auto& timer : timers_) - { - if (timer->IsName(name)) - { - timer->Stop(); - } - } -} - -void TimerManager::StartTimers(const String& name) -{ - if (timers_.IsEmpty()) - return; - - for (auto& timer : timers_) - { - if (timer->IsName(name)) - { - timer->Start(); - } - } -} - -void TimerManager::RemoveTimers(const String& name) -{ - if (timers_.IsEmpty()) - return; - - for (auto& timer : timers_) - { - if (timer->IsName(name)) - { - timer->Remove(); - } - } -} - -void TimerManager::StopAllTimers() -{ - if (timers_.IsEmpty()) - return; - - for (auto& timer : timers_) - { - timer->Stop(); - } -} - -void TimerManager::StartAllTimers() -{ - if (timers_.IsEmpty()) - return; - - for (auto& timer : timers_) - { - timer->Start(); - } -} - -void TimerManager::RemoveAllTimers() -{ - timers_.Clear(); -} - -const TimerList& TimerManager::GetAllTimers() const -{ - return timers_; -} -} // namespace kiwano diff --git a/src/kiwano/kiwano.h b/src/kiwano/kiwano.h index f63b0ea0..42708f53 100644 --- a/src/kiwano/kiwano.h +++ b/src/kiwano/kiwano.h @@ -52,8 +52,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/src/kiwano/platform/Runner.h b/src/kiwano/platform/Runner.h index a156b8a9..c21801a9 100644 --- a/src/kiwano/platform/Runner.h +++ b/src/kiwano/platform/Runner.h @@ -20,7 +20,7 @@ #pragma once #include -#include +#include #include namespace kiwano