Timer => Task

This commit is contained in:
Nomango 2020-05-19 21:25:00 +08:00
parent 41671fa1f3
commit 430e46a5a3
11 changed files with 243 additions and 243 deletions

View File

@ -53,8 +53,8 @@
<ClInclude Include="..\..\src\kiwano\core\RefCounter.h" />
<ClInclude Include="..\..\src\kiwano\core\Resource.h" />
<ClInclude Include="..\..\src\kiwano\core\SmartPtr.hpp" />
<ClInclude Include="..\..\src\kiwano\core\Timer.h" />
<ClInclude Include="..\..\src\kiwano\core\TimerManager.h" />
<ClInclude Include="..\..\src\kiwano\core\Task.h" />
<ClInclude Include="..\..\src\kiwano\core\TaskManager.h" />
<ClInclude Include="..\..\src\kiwano\math\Constants.h" />
<ClInclude Include="..\..\src\kiwano\math\EaseFunctions.h" />
<ClInclude Include="..\..\src\kiwano\math\Math.h" />
@ -139,8 +139,8 @@
<ClCompile Include="..\..\src\kiwano\core\Resource.cpp" />
<ClCompile Include="..\..\src\kiwano\core\String.cpp" />
<ClCompile Include="..\..\src\kiwano\core\Time.cpp" />
<ClCompile Include="..\..\src\kiwano\core\Timer.cpp" />
<ClCompile Include="..\..\src\kiwano\core\TimerManager.cpp" />
<ClCompile Include="..\..\src\kiwano\core\Task.cpp" />
<ClCompile Include="..\..\src\kiwano\core\TaskManager.cpp" />
<ClCompile Include="..\..\src\kiwano\platform\Application.cpp" />
<ClCompile Include="..\..\src\kiwano\platform\FileSystem.cpp" />
<ClCompile Include="..\..\src\kiwano\platform\Input.cpp" />

View File

@ -69,12 +69,6 @@
<Filter>core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\kiwano.h" />
<ClInclude Include="..\..\src\kiwano\core\Timer.h">
<Filter>core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\core\TimerManager.h">
<Filter>core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\2d\GifSprite.h">
<Filter>2d</Filter>
</ClInclude>
@ -327,6 +321,12 @@
<ClInclude Include="..\..\src\kiwano\render\FrameSequence.h">
<Filter>render</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\core\Task.h">
<Filter>core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\core\TaskManager.h">
<Filter>core</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\kiwano\2d\Canvas.cpp">
@ -350,12 +350,6 @@
<ClCompile Include="..\..\src\kiwano\platform\Application.cpp">
<Filter>platform</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\core\Timer.cpp">
<Filter>core</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\core\TimerManager.cpp">
<Filter>core</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\2d\GifSprite.cpp">
<Filter>2d</Filter>
</ClCompile>
@ -539,6 +533,12 @@
<ClCompile Include="..\..\src\kiwano\render\FrameSequence.cpp">
<Filter>render</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\core\TaskManager.cpp">
<Filter>core</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\core\Task.cpp">
<Filter>core</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="suppress_warning.ruleset" />

View File

@ -79,7 +79,7 @@ void Actor::Update(Duration dt)
{
UpdateActions(this, dt);
UpdateComponents(dt);
UpdateTimers(dt);
UpdateTasks(dt);
if (!update_pausing_)
{

View File

@ -21,7 +21,7 @@
#pragma once
#include <kiwano/core/ObjectBase.h>
#include <kiwano/core/Time.h>
#include <kiwano/core/TimerManager.h>
#include <kiwano/core/TaskManager.h>
#include <kiwano/core/EventDispatcher.h>
#include <kiwano/math/Math.h>
#include <kiwano/2d/action/ActionManager.h>
@ -58,11 +58,11 @@ typedef IntrusiveList<ActorPtr> ActorList;
* \~chinese
* @brief ½ÇÉ«
* @details
*
*
*/
class KGE_API Actor
: public ObjectBase
, public TimerManager
, public TaskManager
, public ActionManager
, public EventDispatcher
, protected IntrusiveListValue<ActorPtr>

View File

@ -18,14 +18,14 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <kiwano/core/Timer.h>
#include <kiwano/core/Task.h>
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<Timer>();
TaskPtr ptr = memory::New<Task>();
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<Timer>();
TaskPtr ptr = memory::New<Task>();
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;

View File

@ -25,105 +25,105 @@
namespace kiwano
{
class TimerManager;
class TaskManager;
KGE_DECLARE_SMART_PTR(Timer);
KGE_DECLARE_SMART_PTR(Task);
/// \~chinese
/// @brief 定时器列表
typedef IntrusiveList<TimerPtr> TimerList;
/// @brief 任务列表
typedef IntrusiveList<TaskPtr> TaskList;
/// \~chinese
/// @brief 定时器
/// @details 定时器用于每隔一段时间执行一次回调函数,且可以指定执行总次数
class KGE_API Timer
/// @brief 任务
/// @details 任务用于每隔一段时间执行一次回调函数,且可以指定执行总次数
class KGE_API Task
: public ObjectBase
, protected IntrusiveListValue<TimerPtr>
, protected IntrusiveListValue<TaskPtr>
{
friend class TimerManager;
friend IntrusiveList<TimerPtr>;
friend class TaskManager;
friend IntrusiveList<TaskPtr>;
public:
/// \~chinese
/// @brief 定时器回调函数
/// @brief 任务回调函数
/// @details
/// 回调函数第一个参数是定时器自身,第二个参数是距离上次更新的时间间隔
using Callback = Function<void(Timer* /* self */, Duration /* dt */)>;
/// 回调函数第一个参数是任务自身,第二个参数是距离上次执行任务的时间间隔
using Callback = Function<void(Task* /* self */, Duration /* dt */)>;
/// \~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;
}

View File

@ -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 <kiwano/core/Logger.h>
#include <kiwano/core/TaskManager.h>
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

View File

@ -19,70 +19,70 @@
// THE SOFTWARE.
#pragma once
#include <kiwano/core/Timer.h>
#include <kiwano/core/Task.h>
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

View File

@ -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 <kiwano/core/Logger.h>
#include <kiwano/core/TimerManager.h>
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

View File

@ -52,8 +52,8 @@
#include <kiwano/core/Resource.h>
#include <kiwano/core/SmartPtr.hpp>
#include <kiwano/core/Time.h>
#include <kiwano/core/Timer.h>
#include <kiwano/core/TimerManager.h>
#include <kiwano/core/Task.h>
#include <kiwano/core/TaskManager.h>
#include <kiwano/core/event/Event.h>
#include <kiwano/core/event/KeyEvent.h>
#include <kiwano/core/event/MouseEvent.h>

View File

@ -20,7 +20,7 @@
#pragma once
#include <kiwano/core/Common.h>
#include <kiwano/core/Timer.h>
#include <kiwano/core/Time.h>
#include <kiwano/platform/Window.h>
namespace kiwano