| 
									
										
										
										
											2017-10-26 17:17:30 +08:00
										 |  |  |  | #include "..\emanagers.h"
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | #include "..\etools.h"
 | 
					
						
							|  |  |  |  | #include "..\enodes.h"
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-06 09:56:17 +08:00
										 |  |  |  | void e2d::TimerManager::start(double timeOut, TimerCallback callback) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-05 00:17:33 +08:00
										 |  |  |  | 	auto pTimer = new Timer(callback, timeOut, 1, false, true); | 
					
						
							| 
									
										
										
										
											2018-03-03 17:02:08 +08:00
										 |  |  |  | 	pTimer->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 | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			for (const auto &t : s_vTimers) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				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-03 17:02:08 +08:00
										 |  |  |  | void e2d::TimerManager::start(const String & name) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | 	for (auto 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-03 17:02:08 +08:00
										 |  |  |  | void e2d::TimerManager::stop(const String & name) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | 	for (auto 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-11 23:56:40 +08:00
										 |  |  |  | void e2d::TimerManager::clear(const String & name) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-03 17:02:08 +08:00
										 |  |  |  | 	for (auto 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-11 23:56:40 +08:00
										 |  |  |  | std::vector<e2d::Timer*> e2d::TimerManager::get(const String & name) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-03 17:02:08 +08:00
										 |  |  |  | 	std::vector<Timer*> vTimers; | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | 	for (auto 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
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	for (auto 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-03-03 17:02:08 +08:00
										 |  |  |  | 	for (auto 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-02-03 22:04:43 +08:00
										 |  |  |  | 	for (auto 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() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	for (auto timer : s_vTimers) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		timer->m_fLast = Time::getTotalTime(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::TimerManager::__uninit() | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-03 17:02:08 +08:00
										 |  |  |  | 	for (const auto timer : s_vTimers) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		timer->release(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	s_vTimers.clear(); | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | } |