Magic_Game/core/Tool/Timer.cpp

116 lines
1.8 KiB
C++
Raw Normal View History

2017-10-21 19:09:31 +08:00
#include "..\etools.h"
#include "..\enodes.h"
#include "..\emanagers.h"
2017-10-17 21:22:25 +08:00
e2d::Timer::Timer(Function func, String name, double interval /* = 0 */, int updateTimes /* = -1 */, bool atOnce /* = false */, bool autoRelease /* = false */)
2018-03-04 13:30:52 +08:00
: m_bRunning(false)
, m_nRunTimes(0)
, m_Callback(nullptr)
, m_fInterval(0)
, m_fLast(0)
, m_nUpdateTimes(-1)
, m_bAtOnce(false)
, m_bAutoRelease(false)
2018-03-08 15:03:12 +08:00
, m_bClear(true)
2017-10-17 21:22:25 +08:00
{
2017-10-21 19:09:31 +08:00
this->setName(name);
this->setFunction(func);
2018-03-04 13:30:52 +08:00
this->setUpdateTimes(updateTimes);
2017-10-21 19:09:31 +08:00
this->setInterval(interval);
2018-03-03 17:02:08 +08:00
m_bAutoRelease = autoRelease;
2017-10-21 19:09:31 +08:00
m_bAtOnce = atOnce;
2018-03-06 09:56:17 +08:00
TimerManager::__add(this);
2017-10-17 21:22:25 +08:00
}
bool e2d::Timer::isRunning() const
2017-10-17 21:22:25 +08:00
{
return m_bRunning;
2017-10-17 21:22:25 +08:00
}
void e2d::Timer::stop()
2017-10-17 21:22:25 +08:00
{
m_bRunning = false;
2017-10-17 21:22:25 +08:00
}
2018-03-03 17:02:08 +08:00
void e2d::Timer::stopAndClear()
{
m_bRunning = false;
m_bClear = true;
}
void e2d::Timer::start()
2017-10-17 21:22:25 +08:00
{
m_bRunning = true;
m_fLast = Time::getTotalTime();
2017-10-17 21:22:25 +08:00
}
e2d::String e2d::Timer::getName() const
2017-10-17 21:22:25 +08:00
{
return m_sName;
}
void e2d::Timer::setName(String& name)
2017-10-17 21:22:25 +08:00
{
m_sName = name;
}
2018-02-27 21:07:43 +08:00
void e2d::Timer::setInterval(double interval)
2017-10-17 21:22:25 +08:00
{
2018-01-30 16:45:38 +08:00
m_fInterval = max(interval, 0);
2017-10-17 21:22:25 +08:00
}
void e2d::Timer::setFunction(Function func)
2017-10-21 19:09:31 +08:00
{
m_Callback = func;
2017-10-21 19:09:31 +08:00
}
2018-03-04 13:30:52 +08:00
void e2d::Timer::setUpdateTimes(int updateTimes)
2017-10-21 19:09:31 +08:00
{
2018-03-04 13:30:52 +08:00
m_nUpdateTimes = updateTimes;
m_bClear = (m_nUpdateTimes == 0);
2017-10-21 19:09:31 +08:00
}
void e2d::Timer::setRunAtOnce(bool bAtOnce)
{
m_bAtOnce = bAtOnce;
}
2018-03-03 17:02:08 +08:00
void e2d::Timer::update()
2017-10-17 21:22:25 +08:00
{
2018-03-04 13:30:52 +08:00
if (m_Callback)
{
m_Callback();
}
2017-10-17 21:22:25 +08:00
m_nRunTimes++;
m_fLast += m_fInterval;
2017-10-17 21:22:25 +08:00
2018-03-04 13:30:52 +08:00
if (m_nRunTimes == m_nUpdateTimes)
2017-10-21 19:09:31 +08:00
{
2018-03-03 17:02:08 +08:00
if (m_bAutoRelease)
{
this->stopAndClear();
}
else
{
this->stop();
}
}
2017-10-17 21:22:25 +08:00
}
2017-10-21 19:09:31 +08:00
2018-03-03 17:02:08 +08:00
bool e2d::Timer::isReady() const
2017-10-21 19:09:31 +08:00
{
2018-03-04 13:30:52 +08:00
if (m_bRunning && !m_bClear)
{
if (m_bAtOnce && m_nRunTimes == 0)
return true;
2017-10-21 19:09:31 +08:00
2018-01-30 16:45:38 +08:00
if (m_fInterval == 0)
return true;
2017-10-21 19:09:31 +08:00
if ((Time::getTotalTime() - m_fLast) >= m_fInterval)
return true;
2017-10-21 19:09:31 +08:00
}
return false;
}