Magic_Game/src/kiwano/core/Timer.h

194 lines
4.7 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
// 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:
2020-01-21 10:09:55 +08:00
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
// 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.
#pragma once
2019-11-13 14:33:15 +08:00
#include <kiwano/core/ObjectBase.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/core/Time.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
class TimerManager;
KGE_DECLARE_SMART_PTR(Timer);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 定时器
/// @details 定时器用于每隔一段时间执行一次回调函数,且可以指定执行总次数
2020-01-21 10:09:55 +08:00
class KGE_API Timer
: public virtual ObjectBase
, protected IntrusiveListItem<TimerPtr>
{
friend class TimerManager;
friend IntrusiveList<TimerPtr>;
public:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 定时器回调函数
2020-01-21 10:09:55 +08:00
/// @details
2020-02-10 17:32:04 +08:00
/// 回调函数第一个参数是定时器自身,第二个参数是距离上次更新的时间间隔
2020-01-21 10:09:55 +08:00
using Callback = Function<void(Timer* /* self */, Duration /* dt */)>;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建定时器
/// @param cb 回调函数
/// @param interval 时间间隔
/// @param times 执行次数(设 -1 为永久执行)
2020-02-06 16:54:47 +08:00
static TimerPtr Create(Callback const& cb, Duration interval, int times = -1);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建定时器
/// @param name 名称
/// @param cb 回调函数
/// @param interval 时间间隔
/// @param times 执行次数(设 -1 为永久执行)
2020-02-06 16:54:47 +08:00
static TimerPtr Create(String const& name, Callback const& cb, Duration interval, int times = -1);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 构造空定时器
2020-02-06 16:54:47 +08:00
Timer();
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 启动定时器
2020-01-21 10:09:55 +08:00
void Start();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 停止定时器
2020-01-21 10:09:55 +08:00
void Stop();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 移除定时器
2020-01-21 10:09:55 +08:00
void Remove();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 定时器是否在运行
2020-01-21 10:09:55 +08:00
bool IsRunning() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 定时器是否可移除
2020-01-21 10:09:55 +08:00
bool IsRemoveable() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取定时器执行过回调函数的次数
2020-01-21 10:09:55 +08:00
int GetRunTimes() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取定时器执行回调函数的总次数
2020-01-21 10:09:55 +08:00
int GetTotalRunTimes() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置定时器执行回调函数的总次数
2020-01-21 10:09:55 +08:00
void SetTotalRunTimes(int times);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取定时器执行时间间隔
2020-01-21 10:09:55 +08:00
Duration GetInterval() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置定时器执行时间间隔
2020-01-21 10:09:55 +08:00
void SetInterval(Duration interval);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取定时器回调函数
2020-01-21 10:09:55 +08:00
Callback GetCallback() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置定时器回调函数
2020-01-21 10:09:55 +08:00
void SetCallback(const Callback& callback);
private:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 更新定时器
2020-01-21 10:09:55 +08:00
void Update(Duration dt);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 重置定时器
2020-01-21 10:09:55 +08:00
void Reset();
private:
bool running_;
bool removeable_;
int run_times_;
int total_times_;
Duration interval_;
Duration elapsed_;
Callback callback_;
};
inline void Timer::Start()
{
running_ = true;
}
inline void Timer::Stop()
{
running_ = false;
}
inline void Timer::Remove()
{
removeable_ = true;
}
inline bool Timer::IsRunning() const
{
return running_;
}
inline bool Timer::IsRemoveable() const
{
return removeable_;
}
inline int Timer::GetRunTimes() const
{
return run_times_;
}
inline int Timer::GetTotalRunTimes() const
{
return total_times_;
}
inline void Timer::SetTotalRunTimes(int times)
{
total_times_ = times;
}
inline Duration Timer::GetInterval() const
{
return interval_;
}
inline void Timer::SetInterval(Duration interval)
{
interval_ = interval;
}
inline Timer::Callback Timer::GetCallback() const
{
return callback_;
}
inline void Timer::SetCallback(const Timer::Callback& callback)
{
callback_ = callback;
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano