Timer => Task
This commit is contained in:
parent
41671fa1f3
commit
430e46a5a3
|
|
@ -53,8 +53,8 @@
|
||||||
<ClInclude Include="..\..\src\kiwano\core\RefCounter.h" />
|
<ClInclude Include="..\..\src\kiwano\core\RefCounter.h" />
|
||||||
<ClInclude Include="..\..\src\kiwano\core\Resource.h" />
|
<ClInclude Include="..\..\src\kiwano\core\Resource.h" />
|
||||||
<ClInclude Include="..\..\src\kiwano\core\SmartPtr.hpp" />
|
<ClInclude Include="..\..\src\kiwano\core\SmartPtr.hpp" />
|
||||||
<ClInclude Include="..\..\src\kiwano\core\Timer.h" />
|
<ClInclude Include="..\..\src\kiwano\core\Task.h" />
|
||||||
<ClInclude Include="..\..\src\kiwano\core\TimerManager.h" />
|
<ClInclude Include="..\..\src\kiwano\core\TaskManager.h" />
|
||||||
<ClInclude Include="..\..\src\kiwano\math\Constants.h" />
|
<ClInclude Include="..\..\src\kiwano\math\Constants.h" />
|
||||||
<ClInclude Include="..\..\src\kiwano\math\EaseFunctions.h" />
|
<ClInclude Include="..\..\src\kiwano\math\EaseFunctions.h" />
|
||||||
<ClInclude Include="..\..\src\kiwano\math\Math.h" />
|
<ClInclude Include="..\..\src\kiwano\math\Math.h" />
|
||||||
|
|
@ -139,8 +139,8 @@
|
||||||
<ClCompile Include="..\..\src\kiwano\core\Resource.cpp" />
|
<ClCompile Include="..\..\src\kiwano\core\Resource.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\core\String.cpp" />
|
<ClCompile Include="..\..\src\kiwano\core\String.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\core\Time.cpp" />
|
<ClCompile Include="..\..\src\kiwano\core\Time.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\core\Timer.cpp" />
|
<ClCompile Include="..\..\src\kiwano\core\Task.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\core\TimerManager.cpp" />
|
<ClCompile Include="..\..\src\kiwano\core\TaskManager.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\platform\Application.cpp" />
|
<ClCompile Include="..\..\src\kiwano\platform\Application.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\platform\FileSystem.cpp" />
|
<ClCompile Include="..\..\src\kiwano\platform\FileSystem.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\platform\Input.cpp" />
|
<ClCompile Include="..\..\src\kiwano\platform\Input.cpp" />
|
||||||
|
|
|
||||||
|
|
@ -69,12 +69,6 @@
|
||||||
<Filter>core</Filter>
|
<Filter>core</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="..\..\src\kiwano\kiwano.h" />
|
<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">
|
<ClInclude Include="..\..\src\kiwano\2d\GifSprite.h">
|
||||||
<Filter>2d</Filter>
|
<Filter>2d</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
|
@ -327,6 +321,12 @@
|
||||||
<ClInclude Include="..\..\src\kiwano\render\FrameSequence.h">
|
<ClInclude Include="..\..\src\kiwano\render\FrameSequence.h">
|
||||||
<Filter>render</Filter>
|
<Filter>render</Filter>
|
||||||
</ClInclude>
|
</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>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="..\..\src\kiwano\2d\Canvas.cpp">
|
<ClCompile Include="..\..\src\kiwano\2d\Canvas.cpp">
|
||||||
|
|
@ -350,12 +350,6 @@
|
||||||
<ClCompile Include="..\..\src\kiwano\platform\Application.cpp">
|
<ClCompile Include="..\..\src\kiwano\platform\Application.cpp">
|
||||||
<Filter>platform</Filter>
|
<Filter>platform</Filter>
|
||||||
</ClCompile>
|
</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">
|
<ClCompile Include="..\..\src\kiwano\2d\GifSprite.cpp">
|
||||||
<Filter>2d</Filter>
|
<Filter>2d</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
|
@ -539,6 +533,12 @@
|
||||||
<ClCompile Include="..\..\src\kiwano\render\FrameSequence.cpp">
|
<ClCompile Include="..\..\src\kiwano\render\FrameSequence.cpp">
|
||||||
<Filter>render</Filter>
|
<Filter>render</Filter>
|
||||||
</ClCompile>
|
</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>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="suppress_warning.ruleset" />
|
<None Include="suppress_warning.ruleset" />
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ void Actor::Update(Duration dt)
|
||||||
{
|
{
|
||||||
UpdateActions(this, dt);
|
UpdateActions(this, dt);
|
||||||
UpdateComponents(dt);
|
UpdateComponents(dt);
|
||||||
UpdateTimers(dt);
|
UpdateTasks(dt);
|
||||||
|
|
||||||
if (!update_pausing_)
|
if (!update_pausing_)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <kiwano/core/ObjectBase.h>
|
#include <kiwano/core/ObjectBase.h>
|
||||||
#include <kiwano/core/Time.h>
|
#include <kiwano/core/Time.h>
|
||||||
#include <kiwano/core/TimerManager.h>
|
#include <kiwano/core/TaskManager.h>
|
||||||
#include <kiwano/core/EventDispatcher.h>
|
#include <kiwano/core/EventDispatcher.h>
|
||||||
#include <kiwano/math/Math.h>
|
#include <kiwano/math/Math.h>
|
||||||
#include <kiwano/2d/action/ActionManager.h>
|
#include <kiwano/2d/action/ActionManager.h>
|
||||||
|
|
@ -58,11 +58,11 @@ typedef IntrusiveList<ActorPtr> ActorList;
|
||||||
* \~chinese
|
* \~chinese
|
||||||
* @brief ½ÇÉ«
|
* @brief ½ÇÉ«
|
||||||
* @details
|
* @details
|
||||||
* 角色是舞台上最基本的元素,是完成渲染、更新、事件分发等功能的最小单位,也是动画、定时器、事件监听等功能的载体
|
* 角色是舞台上最基本的元素,是完成渲染、更新、事件分发等功能的最小单位,也是动画、任务、事件监听等功能的载体
|
||||||
*/
|
*/
|
||||||
class KGE_API Actor
|
class KGE_API Actor
|
||||||
: public ObjectBase
|
: public ObjectBase
|
||||||
, public TimerManager
|
, public TaskManager
|
||||||
, public ActionManager
|
, public ActionManager
|
||||||
, public EventDispatcher
|
, public EventDispatcher
|
||||||
, protected IntrusiveListValue<ActorPtr>
|
, protected IntrusiveListValue<ActorPtr>
|
||||||
|
|
|
||||||
|
|
@ -18,14 +18,14 @@
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
// THE SOFTWARE.
|
// THE SOFTWARE.
|
||||||
|
|
||||||
#include <kiwano/core/Timer.h>
|
#include <kiwano/core/Task.h>
|
||||||
|
|
||||||
namespace kiwano
|
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)
|
if (ptr)
|
||||||
{
|
{
|
||||||
ptr->SetCallback(cb);
|
ptr->SetCallback(cb);
|
||||||
|
|
@ -35,9 +35,9 @@ TimerPtr Timer::Create(const Callback& cb, Duration interval, int times)
|
||||||
return ptr;
|
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)
|
if (ptr)
|
||||||
{
|
{
|
||||||
ptr->SetName(name);
|
ptr->SetName(name);
|
||||||
|
|
@ -48,7 +48,7 @@ TimerPtr Timer::Create(const String& name, const Callback& cb, Duration interval
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Timer::Timer()
|
Task::Task()
|
||||||
: running_(true)
|
: running_(true)
|
||||||
, removeable_(false)
|
, removeable_(false)
|
||||||
, run_times_(0)
|
, run_times_(0)
|
||||||
|
|
@ -59,7 +59,7 @@ Timer::Timer()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Timer::Update(Duration dt)
|
void Task::Update(Duration dt)
|
||||||
{
|
{
|
||||||
if (total_times_ == 0)
|
if (total_times_ == 0)
|
||||||
{
|
{
|
||||||
|
|
@ -87,7 +87,7 @@ void Timer::Update(Duration dt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Timer::Reset()
|
void Task::Reset()
|
||||||
{
|
{
|
||||||
elapsed_ = 0;
|
elapsed_ = 0;
|
||||||
run_times_ = 0;
|
run_times_ = 0;
|
||||||
|
|
@ -25,105 +25,105 @@
|
||||||
|
|
||||||
namespace kiwano
|
namespace kiwano
|
||||||
{
|
{
|
||||||
class TimerManager;
|
class TaskManager;
|
||||||
|
|
||||||
KGE_DECLARE_SMART_PTR(Timer);
|
KGE_DECLARE_SMART_PTR(Task);
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 定时器列表
|
/// @brief 任务列表
|
||||||
typedef IntrusiveList<TimerPtr> TimerList;
|
typedef IntrusiveList<TaskPtr> TaskList;
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 定时器
|
/// @brief 任务
|
||||||
/// @details 定时器用于每隔一段时间执行一次回调函数,且可以指定执行总次数
|
/// @details 任务用于每隔一段时间执行一次回调函数,且可以指定执行总次数
|
||||||
class KGE_API Timer
|
class KGE_API Task
|
||||||
: public ObjectBase
|
: public ObjectBase
|
||||||
, protected IntrusiveListValue<TimerPtr>
|
, protected IntrusiveListValue<TaskPtr>
|
||||||
{
|
{
|
||||||
friend class TimerManager;
|
friend class TaskManager;
|
||||||
friend IntrusiveList<TimerPtr>;
|
friend IntrusiveList<TaskPtr>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 定时器回调函数
|
/// @brief 任务回调函数
|
||||||
/// @details
|
/// @details
|
||||||
/// 回调函数第一个参数是定时器自身,第二个参数是距离上次更新的时间间隔
|
/// 回调函数第一个参数是任务自身,第二个参数是距离上次执行任务的时间间隔
|
||||||
using Callback = Function<void(Timer* /* self */, Duration /* dt */)>;
|
using Callback = Function<void(Task* /* self */, Duration /* dt */)>;
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 创建定时器
|
/// @brief 创建任务
|
||||||
/// @param cb 回调函数
|
/// @param cb 回调函数
|
||||||
/// @param interval 时间间隔
|
/// @param interval 时间间隔
|
||||||
/// @param times 执行次数(设 -1 为永久执行)
|
/// @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
|
/// \~chinese
|
||||||
/// @brief 创建定时器
|
/// @brief 创建任务
|
||||||
/// @param name 名称
|
/// @param name 名称
|
||||||
/// @param cb 回调函数
|
/// @param cb 回调函数
|
||||||
/// @param interval 时间间隔
|
/// @param interval 时间间隔
|
||||||
/// @param times 执行次数(设 -1 为永久执行)
|
/// @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
|
/// \~chinese
|
||||||
/// @brief 构造空定时器
|
/// @brief 构造空任务
|
||||||
Timer();
|
Task();
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 启动定时器
|
/// @brief 启动任务
|
||||||
void Start();
|
void Start();
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 停止定时器
|
/// @brief 停止任务
|
||||||
void Stop();
|
void Stop();
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 移除定时器
|
/// @brief 移除任务
|
||||||
void Remove();
|
void Remove();
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 定时器是否在运行
|
/// @brief 任务是否在运行
|
||||||
bool IsRunning() const;
|
bool IsRunning() const;
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 定时器是否可移除
|
/// @brief 任务是否可移除
|
||||||
bool IsRemoveable() const;
|
bool IsRemoveable() const;
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 获取定时器执行过回调函数的次数
|
/// @brief 获取任务执行过回调函数的次数
|
||||||
int GetRunTimes() const;
|
int GetRunTimes() const;
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 获取定时器执行回调函数的总次数
|
/// @brief 获取任务执行回调函数的总次数
|
||||||
int GetTotalRunTimes() const;
|
int GetTotalRunTimes() const;
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 设置定时器执行回调函数的总次数
|
/// @brief 设置任务执行回调函数的总次数
|
||||||
void SetTotalRunTimes(int times);
|
void SetTotalRunTimes(int times);
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 获取定时器执行时间间隔
|
/// @brief 获取任务执行时间间隔
|
||||||
Duration GetInterval() const;
|
Duration GetInterval() const;
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 设置定时器执行时间间隔
|
/// @brief 设置任务执行时间间隔
|
||||||
void SetInterval(Duration interval);
|
void SetInterval(Duration interval);
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 获取定时器回调函数
|
/// @brief 获取任务回调函数
|
||||||
Callback GetCallback() const;
|
Callback GetCallback() const;
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 设置定时器回调函数
|
/// @brief 设置任务回调函数
|
||||||
void SetCallback(const Callback& callback);
|
void SetCallback(const Callback& callback);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 更新定时器
|
/// @brief 更新任务
|
||||||
void Update(Duration dt);
|
void Update(Duration dt);
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 重置定时器
|
/// @brief 重置任务
|
||||||
void Reset();
|
void Reset();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
@ -136,62 +136,62 @@ private:
|
||||||
Callback callback_;
|
Callback callback_;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline void Timer::Start()
|
inline void Task::Start()
|
||||||
{
|
{
|
||||||
running_ = true;
|
running_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Timer::Stop()
|
inline void Task::Stop()
|
||||||
{
|
{
|
||||||
running_ = false;
|
running_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Timer::Remove()
|
inline void Task::Remove()
|
||||||
{
|
{
|
||||||
removeable_ = true;
|
removeable_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Timer::IsRunning() const
|
inline bool Task::IsRunning() const
|
||||||
{
|
{
|
||||||
return running_;
|
return running_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Timer::IsRemoveable() const
|
inline bool Task::IsRemoveable() const
|
||||||
{
|
{
|
||||||
return removeable_;
|
return removeable_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int Timer::GetRunTimes() const
|
inline int Task::GetRunTimes() const
|
||||||
{
|
{
|
||||||
return run_times_;
|
return run_times_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int Timer::GetTotalRunTimes() const
|
inline int Task::GetTotalRunTimes() const
|
||||||
{
|
{
|
||||||
return total_times_;
|
return total_times_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Timer::SetTotalRunTimes(int times)
|
inline void Task::SetTotalRunTimes(int times)
|
||||||
{
|
{
|
||||||
total_times_ = times;
|
total_times_ = times;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Duration Timer::GetInterval() const
|
inline Duration Task::GetInterval() const
|
||||||
{
|
{
|
||||||
return interval_;
|
return interval_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Timer::SetInterval(Duration interval)
|
inline void Task::SetInterval(Duration interval)
|
||||||
{
|
{
|
||||||
interval_ = interval;
|
interval_ = interval;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Timer::Callback Timer::GetCallback() const
|
inline Task::Callback Task::GetCallback() const
|
||||||
{
|
{
|
||||||
return callback_;
|
return callback_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Timer::SetCallback(const Timer::Callback& callback)
|
inline void Task::SetCallback(const Task::Callback& callback)
|
||||||
{
|
{
|
||||||
callback_ = callback;
|
callback_ = callback;
|
||||||
}
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -19,70 +19,70 @@
|
||||||
// THE SOFTWARE.
|
// THE SOFTWARE.
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <kiwano/core/Timer.h>
|
#include <kiwano/core/Task.h>
|
||||||
|
|
||||||
namespace kiwano
|
namespace kiwano
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* \~chinese
|
* \~chinese
|
||||||
* @brief 定时器管理器
|
* @brief 任务管理器
|
||||||
*/
|
*/
|
||||||
class KGE_API TimerManager
|
class KGE_API TaskManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 添加定时器
|
/// @brief 添加任务
|
||||||
/// @param cb 回调函数
|
/// @param cb 回调函数
|
||||||
/// @param interval 时间间隔
|
/// @param interval 时间间隔
|
||||||
/// @param times 执行次数(设 -1 为永久执行)
|
/// @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
|
/// \~chinese
|
||||||
/// @brief 添加定时器
|
/// @brief 添加任务
|
||||||
/// @param name 定时器名称
|
/// @param name 任务名称
|
||||||
/// @param cb 回调函数
|
/// @param cb 回调函数
|
||||||
/// @param interval 时间间隔
|
/// @param interval 时间间隔
|
||||||
/// @param times 执行次数(设 -1 为永久执行)
|
/// @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
|
/// \~chinese
|
||||||
/// @brief 添加定时器
|
/// @brief 添加任务
|
||||||
Timer* AddTimer(TimerPtr timer);
|
Task* AddTask(TaskPtr task);
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 启动定时器
|
/// @brief 启动任务
|
||||||
void StartTimers(const String& timer_name);
|
void StartTasks(const String& task_name);
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 停止定时器
|
/// @brief 停止任务
|
||||||
void StopTimers(const String& timer_name);
|
void StopTasks(const String& task_name);
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 移除定时器
|
/// @brief 移除任务
|
||||||
void RemoveTimers(const String& timer_name);
|
void RemoveTasks(const String& task_name);
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 启动所有定时器
|
/// @brief 启动所有任务
|
||||||
void StartAllTimers();
|
void StartAllTasks();
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 停止所有定时器
|
/// @brief 停止所有任务
|
||||||
void StopAllTimers();
|
void StopAllTasks();
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 移除所有定时器
|
/// @brief 移除所有任务
|
||||||
void RemoveAllTimers();
|
void RemoveAllTasks();
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 获取所有定时器
|
/// @brief 获取所有任务
|
||||||
const TimerList& GetAllTimers() const;
|
const TaskList& GetAllTasks() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 更新定时器
|
/// @brief 更新任务
|
||||||
void UpdateTimers(Duration dt);
|
void UpdateTasks(Duration dt);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TimerList timers_;
|
TaskList tasks_;
|
||||||
};
|
};
|
||||||
} // namespace kiwano
|
} // namespace kiwano
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -52,8 +52,8 @@
|
||||||
#include <kiwano/core/Resource.h>
|
#include <kiwano/core/Resource.h>
|
||||||
#include <kiwano/core/SmartPtr.hpp>
|
#include <kiwano/core/SmartPtr.hpp>
|
||||||
#include <kiwano/core/Time.h>
|
#include <kiwano/core/Time.h>
|
||||||
#include <kiwano/core/Timer.h>
|
#include <kiwano/core/Task.h>
|
||||||
#include <kiwano/core/TimerManager.h>
|
#include <kiwano/core/TaskManager.h>
|
||||||
#include <kiwano/core/event/Event.h>
|
#include <kiwano/core/event/Event.h>
|
||||||
#include <kiwano/core/event/KeyEvent.h>
|
#include <kiwano/core/event/KeyEvent.h>
|
||||||
#include <kiwano/core/event/MouseEvent.h>
|
#include <kiwano/core/event/MouseEvent.h>
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <kiwano/core/Common.h>
|
#include <kiwano/core/Common.h>
|
||||||
#include <kiwano/core/Timer.h>
|
#include <kiwano/core/Time.h>
|
||||||
#include <kiwano/platform/Window.h>
|
#include <kiwano/platform/Window.h>
|
||||||
|
|
||||||
namespace kiwano
|
namespace kiwano
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue