Magic_Game/Easy2D/Tool/ETimer.cpp

123 lines
2.1 KiB
C++
Raw Normal View History

2017-10-21 19:09:31 +08:00
#include "..\etools.h"
#include "..\Win\winbase.h"
2017-10-17 21:22:25 +08:00
e2d::ETimer::ETimer()
: m_bRunning(false)
, m_nRunTimes(0)
, m_pParentScene(nullptr)
, m_pParentNode(nullptr)
, m_Callback([](int) {})
2017-10-21 19:09:31 +08:00
, m_nInterval(0)
, m_nRepeatTimes(0)
, m_bAtOnce(false)
2017-10-17 21:22:25 +08:00
{
}
2017-10-21 19:09:31 +08:00
e2d::ETimer::ETimer(const TIMER_CALLBACK & callback, int repeatTimes /* = -1 */, LONGLONG interval /* = 0 */, bool atOnce /* = false */)
2017-10-17 21:22:25 +08:00
: ETimer()
{
2017-10-21 19:09:31 +08:00
this->setCallback(callback);
this->setRepeatTimes(repeatTimes);
this->setInterval(interval);
m_bAtOnce = atOnce;
2017-10-17 21:22:25 +08:00
}
2017-10-21 19:09:31 +08:00
e2d::ETimer::ETimer(const EString & name, const TIMER_CALLBACK & callback, int repeatTimes /* = -1 */, LONGLONG interval /* = 0 */, bool atOnce /* = false */)
2017-10-17 21:22:25 +08:00
: ETimer()
{
2017-10-21 19:09:31 +08:00
this->setName(name);
this->setCallback(callback);
this->setRepeatTimes(repeatTimes);
this->setInterval(interval);
m_bAtOnce = atOnce;
2017-10-17 21:22:25 +08:00
}
bool e2d::ETimer::isRunning() const
{
return m_bRunning;
2017-10-17 21:22:25 +08:00
}
void e2d::ETimer::start()
{
m_bRunning = true;
2017-10-19 00:50:04 +08:00
m_tLast = GetNow();
2017-10-17 21:22:25 +08:00
}
void e2d::ETimer::stop()
{
m_bRunning = false;
}
e2d::EString e2d::ETimer::getName() const
{
return m_sName;
}
e2d::EScene * e2d::ETimer::getParentScene() const
{
return m_pParentScene;
}
e2d::ENode * e2d::ETimer::getParentNode() const
{
return m_pParentNode;
}
void e2d::ETimer::setName(const EString & name)
{
m_sName = name;
}
void e2d::ETimer::setInterval(LONGLONG interval)
{
m_nInterval = max(interval, 0);
}
2017-10-21 19:09:31 +08:00
void e2d::ETimer::setCallback(const TIMER_CALLBACK & callback)
{
m_Callback = callback;
}
void e2d::ETimer::setRepeatTimes(int repeatTimes)
{
m_nRepeatTimes = repeatTimes;
}
2017-10-17 21:22:25 +08:00
void e2d::ETimer::bindWith(EScene * pParentScene)
{
ETimerManager::bindTimer(this, pParentScene);
}
void e2d::ETimer::bindWith(ENode * pParentNode)
{
ETimerManager::bindTimer(this, pParentNode);
}
2017-10-19 00:50:04 +08:00
void e2d::ETimer::_callOn()
2017-10-17 21:22:25 +08:00
{
2017-10-21 19:09:31 +08:00
if (m_nRunTimes == m_nRepeatTimes)
{
this->stop();
return;
}
2017-10-17 21:22:25 +08:00
m_Callback(m_nRunTimes);
m_nRunTimes++;
}
2017-10-21 19:09:31 +08:00
bool e2d::ETimer::_isReady()
{
if (m_bAtOnce && m_nRunTimes == 0)
return true;
if (m_nInterval == 0)
return true;
if (GetInterval(m_tLast) >= m_nInterval)
{
m_tLast += milliseconds(m_nInterval);
return true;
}
return false;
}