2018-04-02 23:01:38 +08:00
|
|
|
|
#include "..\emanager.h"
|
|
|
|
|
|
#include "..\etool.h"
|
|
|
|
|
|
#include "..\enode.h"
|
2017-10-17 21:22:25 +08:00
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
static std::vector<e2d::Timer*> s_vTimers;
|
2017-10-17 21:22:25 +08:00
|
|
|
|
|
2017-10-21 19:09:31 +08:00
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
void e2d::TimerManager::__update()
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2018-02-07 16:37:12 +08:00
|
|
|
|
if (s_vTimers.empty() || Game::isPaused())
|
2017-10-21 19:09:31 +08:00
|
|
|
|
return;
|
|
|
|
|
|
|
2018-03-06 09:56:17 +08:00
|
|
|
|
for (size_t i = 0; i < s_vTimers.size(); i++)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2018-03-06 09:56:17 +08:00
|
|
|
|
auto pTimer = s_vTimers[i];
|
2018-03-03 17:02:08 +08:00
|
|
|
|
// <20><><EFBFBD>¶<EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
|
if (pTimer->isReady())
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2018-03-03 17:02:08 +08:00
|
|
|
|
pTimer->update();
|
|
|
|
|
|
}
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>Ķ<EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
|
if (pTimer->m_bClear)
|
|
|
|
|
|
{
|
|
|
|
|
|
pTimer->release();
|
2018-03-06 09:56:17 +08:00
|
|
|
|
s_vTimers.erase(s_vTimers.begin() + i);
|
|
|
|
|
|
i--;
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-28 17:56:04 +08:00
|
|
|
|
void e2d::TimerManager::start(double timeOut, Function func)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-03-28 17:56:04 +08:00
|
|
|
|
(new Timer(func, L"", timeOut, 1, false, true))->start();
|
2017-10-17 21:22:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-06 09:56:17 +08:00
|
|
|
|
void e2d::TimerManager::__add(Timer * pTimer)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-02-07 16:37:12 +08:00
|
|
|
|
WARN_IF(pTimer == nullptr, "Timer NULL pointer exception!");
|
2017-10-17 21:22:25 +08:00
|
|
|
|
|
2018-03-03 17:02:08 +08:00
|
|
|
|
if (pTimer)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-03-03 17:02:08 +08:00
|
|
|
|
auto findTimer = [](Timer * pTimer) -> bool
|
|
|
|
|
|
{
|
2018-04-01 23:08:11 +08:00
|
|
|
|
FOR_LOOP(t, s_vTimers)
|
2018-03-03 17:02:08 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (pTimer == t)
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool bHasTimer = findTimer(pTimer);
|
|
|
|
|
|
WARN_IF(bHasTimer, "The timer is already added, cannot be added again!");
|
|
|
|
|
|
|
|
|
|
|
|
if (!bHasTimer)
|
|
|
|
|
|
{
|
|
|
|
|
|
pTimer->retain();
|
|
|
|
|
|
s_vTimers.push_back(pTimer);
|
|
|
|
|
|
}
|
2017-10-17 21:22:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-30 01:41:29 +08:00
|
|
|
|
void e2d::TimerManager::start(String name)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-04-01 23:08:11 +08:00
|
|
|
|
FOR_LOOP(timer, s_vTimers)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-02-03 22:04:43 +08:00
|
|
|
|
if (timer->getName() == name)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-02-03 22:04:43 +08:00
|
|
|
|
timer->start();
|
2017-10-17 21:22:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-30 01:41:29 +08:00
|
|
|
|
void e2d::TimerManager::stop(String name)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-04-01 23:08:11 +08:00
|
|
|
|
FOR_LOOP(timer, s_vTimers)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-02-03 22:04:43 +08:00
|
|
|
|
if (timer->getName() == name)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-02-03 22:04:43 +08:00
|
|
|
|
timer->stop();
|
2017-10-17 21:22:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-30 01:41:29 +08:00
|
|
|
|
void e2d::TimerManager::clear(String name)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-04-01 23:08:11 +08:00
|
|
|
|
FOR_LOOP(timer, s_vTimers)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-03-03 17:02:08 +08:00
|
|
|
|
if (timer->getName() == name)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-03-03 17:02:08 +08:00
|
|
|
|
timer->stopAndClear();
|
2017-10-17 21:22:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-30 01:41:29 +08:00
|
|
|
|
std::vector<e2d::Timer*> e2d::TimerManager::get(String name)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-03-03 17:02:08 +08:00
|
|
|
|
std::vector<Timer*> vTimers;
|
2018-04-01 23:08:11 +08:00
|
|
|
|
FOR_LOOP(timer, s_vTimers)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-03-03 17:02:08 +08:00
|
|
|
|
if (timer->getName() == name)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-03-03 17:02:08 +08:00
|
|
|
|
vTimers.push_back(timer);
|
2017-10-17 21:22:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-03-03 17:02:08 +08:00
|
|
|
|
return std::move(vTimers);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-11 23:56:40 +08:00
|
|
|
|
void e2d::TimerManager::startAll()
|
2018-03-03 17:02:08 +08:00
|
|
|
|
{
|
2018-04-01 23:08:11 +08:00
|
|
|
|
FOR_LOOP(timer, s_vTimers)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-03-03 17:02:08 +08:00
|
|
|
|
timer->start();
|
2017-10-17 21:22:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-11 23:56:40 +08:00
|
|
|
|
void e2d::TimerManager::stopAll()
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-04-01 23:08:11 +08:00
|
|
|
|
FOR_LOOP(timer, s_vTimers)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-03-03 17:02:08 +08:00
|
|
|
|
timer->stop();
|
2017-10-17 21:22:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-11 23:56:40 +08:00
|
|
|
|
void e2d::TimerManager::stopAndClearAll()
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-04-01 23:08:11 +08:00
|
|
|
|
FOR_LOOP(timer, s_vTimers)
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-03-03 17:02:08 +08:00
|
|
|
|
timer->stop();
|
|
|
|
|
|
timer->release();
|
2017-10-17 21:22:25 +08:00
|
|
|
|
}
|
2018-03-03 17:02:08 +08:00
|
|
|
|
s_vTimers.clear();
|
2017-10-17 21:22:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-11 23:56:40 +08:00
|
|
|
|
std::vector<e2d::Timer*> e2d::TimerManager::getAll()
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-03-03 17:02:08 +08:00
|
|
|
|
return s_vTimers;
|
2017-10-17 21:22:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-03 17:02:08 +08:00
|
|
|
|
void e2d::TimerManager::__resetAllTimers()
|
|
|
|
|
|
{
|
2018-04-01 23:08:11 +08:00
|
|
|
|
FOR_LOOP(timer, s_vTimers)
|
2018-03-03 17:02:08 +08:00
|
|
|
|
{
|
|
|
|
|
|
timer->m_fLast = Time::getTotalTime();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void e2d::TimerManager::__uninit()
|
2017-10-17 21:22:25 +08:00
|
|
|
|
{
|
2018-04-01 23:08:11 +08:00
|
|
|
|
FOR_LOOP(timer, s_vTimers)
|
2018-03-03 17:02:08 +08:00
|
|
|
|
{
|
|
|
|
|
|
timer->release();
|
|
|
|
|
|
}
|
|
|
|
|
|
s_vTimers.clear();
|
2017-10-17 21:22:25 +08:00
|
|
|
|
}
|