rename Task => Timer

minor

minor

minor fixes
This commit is contained in:
Nomango 2019-03-21 18:16:41 +08:00 committed by Nomango
parent cd2c112a98
commit 1de100c294
15 changed files with 1247 additions and 998 deletions

View File

@ -62,7 +62,7 @@ namespace easy2d
return;
UpdateActions(this, dt);
UpdateTasks(dt);
UpdateTimers(dt);
if (cb_update_)
cb_update_(dt);

View File

@ -22,7 +22,7 @@
#include "include-forwards.h"
#include "Transform.hpp"
#include "ActionManager.h"
#include "../base/TaskManager.h"
#include "../base/TimerManager.h"
#include "../base/EventDispatcher.h"
namespace easy2d
@ -32,7 +32,7 @@ namespace easy2d
// ½Úµã
class E2D_API Node
: public virtual Object
, public TaskManager
, public TimerManager
, public ActionManager
, public EventDispatcher
, public IntrusiveListItem<NodePtr>

View File

@ -38,8 +38,8 @@
<ClInclude Include="base\RefCounter.hpp" />
<ClInclude Include="base\Resource.h" />
<ClInclude Include="base\SmartPtr.hpp" />
<ClInclude Include="base\Task.h" />
<ClInclude Include="base\TaskManager.h" />
<ClInclude Include="base\Timer.h" />
<ClInclude Include="base\TimerManager.h" />
<ClInclude Include="base\time.h" />
<ClInclude Include="base\window.h" />
<ClInclude Include="common\Array.h" />
@ -101,8 +101,8 @@
<ClCompile Include="base\logs.cpp" />
<ClCompile Include="base\Object.cpp" />
<ClCompile Include="base\Resource.cpp" />
<ClCompile Include="base\Task.cpp" />
<ClCompile Include="base\TaskManager.cpp" />
<ClCompile Include="base\Timer.cpp" />
<ClCompile Include="base\TimerManager.cpp" />
<ClCompile Include="base\time.cpp" />
<ClCompile Include="base\window.cpp" />
<ClCompile Include="platform\Application.cpp" />

View File

@ -153,12 +153,6 @@
<ClInclude Include="base\Resource.h">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="base\Task.h">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="base\TaskManager.h">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="base\time.h">
<Filter>base</Filter>
</ClInclude>
@ -243,6 +237,12 @@
<ClInclude Include="common\Json.h">
<Filter>common</Filter>
</ClInclude>
<ClInclude Include="base\Timer.h">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="base\TimerManager.h">
<Filter>base</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="ui\Button.cpp">
@ -317,12 +317,6 @@
<ClCompile Include="base\Resource.cpp">
<Filter>base</Filter>
</ClCompile>
<ClCompile Include="base\Task.cpp">
<Filter>base</Filter>
</ClCompile>
<ClCompile Include="base\TaskManager.cpp">
<Filter>base</Filter>
</ClCompile>
<ClCompile Include="base\time.cpp">
<Filter>base</Filter>
</ClCompile>
@ -368,5 +362,11 @@
<ClCompile Include="utils\DataUtil.cpp">
<Filter>utils</Filter>
</ClCompile>
<ClCompile Include="base\Timer.cpp">
<Filter>base</Filter>
</ClCompile>
<ClCompile Include="base\TimerManager.cpp">
<Filter>base</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -21,7 +21,6 @@
#pragma once
#include "../macros.h"
#include "../common/helper.h"
#include "../common/Json.h"
#include "RefCounter.hpp"
#include "SmartPtr.hpp"

View File

@ -1,130 +0,0 @@
// Copyright (c) 2016-2018 Easy2D - 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 "TaskManager.h"
#include "../base/logs.h"
namespace easy2d
{
void TaskManager::UpdateTasks(Duration dt)
{
if (tasks_.IsEmpty())
return;
TaskPtr next;
for (auto task = tasks_.First(); task; task = next)
{
next = task->NextItem();
bool remove_after_update = false;
task->Update(dt, remove_after_update);
if (remove_after_update)
tasks_.Remove(task);
}
}
void TaskManager::AddTask(TaskPtr const& task)
{
E2D_ASSERT(task && "AddTask failed, NULL pointer exception");
if (task)
{
task->Reset();
tasks_.PushBack(task);
}
}
void TaskManager::StopTasks(String const& name)
{
if (tasks_.IsEmpty())
return;
for (auto task = tasks_.First().Get(); task; task = task->NextItem().Get())
{
if (task->IsName(name))
{
task->Stop();
}
}
}
void TaskManager::StartTasks(String const& name)
{
if (tasks_.IsEmpty())
return;
for (auto task = tasks_.First().Get(); task; task = task->NextItem().Get())
{
if (task->IsName(name))
{
task->Start();
}
}
}
void TaskManager::RemoveTasks(String const& name)
{
if (tasks_.IsEmpty())
return;
TaskPtr next;
for (auto task = tasks_.First(); task; task = next)
{
next = task->NextItem();
if (task->IsName(name))
{
tasks_.Remove(task);
}
}
}
void TaskManager::StopAllTasks()
{
if (tasks_.IsEmpty())
return;
for (auto task = tasks_.First().Get(); task; task = task->NextItem().Get())
{
task->Stop();
}
}
void TaskManager::StartAllTasks()
{
if (tasks_.IsEmpty())
return;
for (auto task = tasks_.First().Get(); task; task = task->NextItem().Get())
{
task->Start();
}
}
void TaskManager::RemoveAllTasks()
{
tasks_.Clear();
}
const TaskManager::Tasks & TaskManager::GetAllTasks() const
{
return tasks_;
}
}

View File

@ -18,16 +18,16 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "Task.h"
#include "Timer.h"
namespace easy2d
{
Task::Task(Callback const& func, String const& name)
: Task(func, Duration{}, -1, name)
Timer::Timer(Callback const& func, String const& name)
: Timer(func, Duration{}, -1, name)
{
}
Task::Task(Callback const& func, Duration delay, int times, String const& name)
Timer::Timer(Callback const& func, Duration delay, int times, String const& name)
: running_(true)
, run_times_(0)
, total_times_(times)
@ -38,17 +38,17 @@ namespace easy2d
SetName(name);
}
void Task::Start()
void Timer::Start()
{
running_ = true;
}
void Task::Stop()
void Timer::Stop()
{
running_ = false;
}
void Task::Update(Duration dt, bool& remove_after_update)
void Timer::Update(Duration dt, bool& remove_after_update)
{
if (!running_)
return;
@ -80,13 +80,13 @@ namespace easy2d
}
}
void Task::Reset()
void Timer::Reset()
{
delta_ = Duration{};
run_times_ = 0;
}
bool Task::IsRunning() const
bool Timer::IsRunning() const
{
return running_;
}

View File

@ -27,27 +27,27 @@
namespace easy2d
{
class TaskManager;
class TimerManager;
E2D_DECLARE_SMART_PTR(Task);
E2D_DECLARE_SMART_PTR(Timer);
// 定时任务
class E2D_API Task
class E2D_API Timer
: public virtual Object
, protected IntrusiveListItem<TaskPtr>
, protected IntrusiveListItem<TimerPtr>
{
friend class TaskManager;
friend class IntrusiveList<TaskPtr>;
friend class TimerManager;
friend class IntrusiveList<TimerPtr>;
using Callback = std::function<void()>;
public:
explicit Task(
explicit Timer(
Callback const& func, /* 执行函数 */
String const& name = L"" /* 任务名称 */
);
explicit Task(
explicit Timer(
Callback const& func, /* 执行函数 */
Duration delay, /* 时间间隔(秒) */
int times = -1, /* 执行次数(设 -1 为永久执行) */

View File

@ -0,0 +1,130 @@
// Copyright (c) 2016-2018 Easy2D - 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 "TimerManager.h"
#include "../base/logs.h"
namespace easy2d
{
void TimerManager::UpdateTimers(Duration dt)
{
if (timers_.IsEmpty())
return;
TimerPtr next;
for (auto timer = timers_.First(); timer; timer = next)
{
next = timer->NextItem();
bool remove_after_update = false;
timer->Update(dt, remove_after_update);
if (remove_after_update)
timers_.Remove(timer);
}
}
void TimerManager::AddTimer(TimerPtr const& timer)
{
E2D_ASSERT(timer && "AddTimer failed, NULL pointer exception");
if (timer)
{
timer->Reset();
timers_.PushBack(timer);
}
}
void TimerManager::StopTimers(String const& name)
{
if (timers_.IsEmpty())
return;
for (auto timer = timers_.First().Get(); timer; timer = timer->NextItem().Get())
{
if (timer->IsName(name))
{
timer->Stop();
}
}
}
void TimerManager::StartTimers(String const& name)
{
if (timers_.IsEmpty())
return;
for (auto timer = timers_.First().Get(); timer; timer = timer->NextItem().Get())
{
if (timer->IsName(name))
{
timer->Start();
}
}
}
void TimerManager::RemoveTimers(String const& name)
{
if (timers_.IsEmpty())
return;
TimerPtr next;
for (auto timer = timers_.First(); timer; timer = next)
{
next = timer->NextItem();
if (timer->IsName(name))
{
timers_.Remove(timer);
}
}
}
void TimerManager::StopAllTimers()
{
if (timers_.IsEmpty())
return;
for (auto timer = timers_.First().Get(); timer; timer = timer->NextItem().Get())
{
timer->Stop();
}
}
void TimerManager::StartAllTimers()
{
if (timers_.IsEmpty())
return;
for (auto timer = timers_.First().Get(); timer; timer = timer->NextItem().Get())
{
timer->Start();
}
}
void TimerManager::RemoveAllTimers()
{
timers_.Clear();
}
const TimerManager::Timers & TimerManager::GetAllTimers() const
{
return timers_;
}
}

View File

@ -19,51 +19,51 @@
// THE SOFTWARE.
#pragma once
#include "Task.h"
#include "Timer.h"
namespace easy2d
{
class E2D_API TaskManager
class E2D_API TimerManager
{
using Tasks = IntrusiveList<TaskPtr>;
using Timers = IntrusiveList<TimerPtr>;
public:
// 添加任务
void AddTask(
TaskPtr const& task
void AddTimer(
TimerPtr const& timer
);
// 启动任务
void StartTasks(
String const& task_name
void StartTimers(
String const& timer_name
);
// 停止任务
void StopTasks(
String const& task_name
void StopTimers(
String const& timer_name
);
// 移除任务
void RemoveTasks(
String const& task_name
void RemoveTimers(
String const& timer_name
);
// 启动所有任务
void StartAllTasks();
void StartAllTimers();
// 停止所有任务
void StopAllTasks();
void StopAllTimers();
// 移除所有任务
void RemoveAllTasks();
void RemoveAllTimers();
// 获取所有任务
const Tasks& GetAllTasks() const;
const Timers& GetAllTimers() const;
protected:
void UpdateTasks(Duration dt);
void UpdateTimers(Duration dt);
protected:
Tasks tasks_;
Timers timers_;
};
}

File diff suppressed because it is too large Load Diff

View File

@ -1080,7 +1080,7 @@ namespace easy2d
using size_type = String::size_type;
using traits = String::char_traits;
const typename ostream::sentry ok(os);
const ostream::sentry ok(os);
std::ios_base::iostate state = std::ios_base::goodbit;
if (!ok)
@ -1142,7 +1142,7 @@ namespace easy2d
using traits = String::char_traits;
bool changed = false;
const typename istream::sentry ok(is);
const istream::sentry ok(is);
std::ios_base::iostate state = std::ios_base::goodbit;
if (ok)

View File

@ -48,7 +48,4 @@ namespace easy2d
template<typename _Kty, typename _Ty, typename... _Args>
using UnorderedMap = std::unordered_map<_Kty, _Ty, _Args...>;
template <bool _Boolean, typename _Ty = void>
using enable_if_t = typename std::enable_if<_Boolean, _Ty>::type;
}

View File

@ -71,8 +71,8 @@
#include "base/Event.hpp"
#include "base/EventListener.h"
#include "base/EventDispatcher.h"
#include "base/Task.h"
#include "base/TaskManager.h"
#include "base/Timer.h"
#include "base/TimerManager.h"
#include "2d/Font.hpp"
#include "2d/Color.h"

View File

@ -29,7 +29,6 @@ class DemoApp
public:
DemoApp()
{
ShowConsole();
// 使用 Audio 组件
Use(&Audio::Instance());