| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | #include "..\etools.h"
 | 
					
						
							|  |  |  | #include "..\enodes.h"
 | 
					
						
							|  |  |  | #include "..\Win\winbase.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static e2d::EVector<e2d::ETimer*> s_vTimers; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-21 19:09:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void e2d::ETimerManager::TimerProc() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (EApp::isPaused()) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (size_t i = 0; i < s_vTimers.size(); i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		auto &t = s_vTimers[i]; | 
					
						
							|  |  |  | 		if (t->isRunning()) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (t->getParentScene() == EApp::getCurrentScene() || | 
					
						
							|  |  |  | 				(t->getParentNode() && (t->getParentNode()->getParentScene() == EApp::getCurrentScene()))) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				if (t->_isReady()) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					t->_callOn(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | void e2d::ETimerManager::bindTimer(ETimer * timer, EScene * pParentScene) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ASSERT( | 
					
						
							|  |  |  | 		(!timer->m_pParentScene) && (!timer->m_pParentNode), | 
					
						
							|  |  |  | 		"The timer is already binded, it cannot bind again!" | 
					
						
							|  |  |  | 	); | 
					
						
							|  |  |  | 	WARN_IF(timer == nullptr, "ETimer NULL pointer exception!"); | 
					
						
							|  |  |  | 	WARN_IF(pParentScene == nullptr, "Bind ETimer with a NULL EScene pointer!"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (timer && pParentScene) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		timer->start(); | 
					
						
							|  |  |  | 		timer->retain(); | 
					
						
							|  |  |  | 		timer->m_pParentScene = pParentScene; | 
					
						
							|  |  |  | 		s_vTimers.push_back(timer); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::ETimerManager::bindTimer(ETimer * timer, ENode * pParentNode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ASSERT( | 
					
						
							|  |  |  | 		(!timer->m_pParentScene) && (!timer->m_pParentNode), | 
					
						
							|  |  |  | 		"The timer is already binded, it cannot bind again!" | 
					
						
							|  |  |  | 	); | 
					
						
							|  |  |  | 	WARN_IF(timer == nullptr, "ETimer NULL pointer exception!"); | 
					
						
							|  |  |  | 	WARN_IF(pParentNode == nullptr, "Bind ETimer with a NULL ENode pointer!"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (timer && pParentNode) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		timer->start(); | 
					
						
							|  |  |  | 		timer->retain(); | 
					
						
							|  |  |  | 		timer->m_pParentNode = pParentNode; | 
					
						
							|  |  |  | 		s_vTimers.push_back(timer); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::ETimerManager::startTimers(const EString & name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (auto t : s_vTimers) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (t->getName() == name) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			t->start(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::ETimerManager::stopTimers(const EString & name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (auto t : s_vTimers) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (t->getName() == name) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			t->stop(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::ETimerManager::delTimers(const EString & name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	EVector<ETimer*>::iterator mIter; | 
					
						
							|  |  |  | 	for (mIter = s_vTimers.begin(); mIter != s_vTimers.end();) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if ((*mIter)->getName() == name) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-10-21 19:09:31 +08:00
										 |  |  | 			SafeReleaseAndClear(&(*mIter)); | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 			mIter = s_vTimers.erase(mIter); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			mIter++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::ETimerManager::startAllTimersBindedWith(EScene * pParentScene) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (auto t : s_vTimers) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (t->getParentScene() == pParentScene) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			t->start(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for (auto child : pParentScene->getChildren()) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		ETimerManager::startAllTimersBindedWith(child); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::ETimerManager::stopAllTimersBindedWith(EScene * pParentScene) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (auto t : s_vTimers) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (t->getParentScene() == pParentScene) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			t->stop(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for (auto child : pParentScene->getChildren()) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		ETimerManager::stopAllTimersBindedWith(child); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-21 19:09:31 +08:00
										 |  |  | void e2d::ETimerManager::_clearAllTimersBindedWith(EScene * pParentScene) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	for (size_t i = 0; i < s_vTimers.size();) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		auto t = s_vTimers[i]; | 
					
						
							|  |  |  | 		if (t->getParentScene() == pParentScene) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-10-21 19:09:31 +08:00
										 |  |  | 			SafeReleaseAndClear(&t); | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 			s_vTimers.erase(s_vTimers.begin() + i); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			i++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::ETimerManager::startAllTimersBindedWith(ENode * pParentNode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (auto t : s_vTimers) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (t->getParentNode() == pParentNode) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			t->start(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for (auto child : pParentNode->getChildren()) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		ETimerManager::startAllTimersBindedWith(child); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::ETimerManager::stopAllTimersBindedWith(ENode * pParentNode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (auto t : s_vTimers) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (t->getParentNode() == pParentNode) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			t->stop(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for (auto child : pParentNode->getChildren()) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		ETimerManager::startAllTimersBindedWith(child); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-21 19:09:31 +08:00
										 |  |  | void e2d::ETimerManager::_clearAllTimersBindedWith(ENode * pParentNode) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	for (size_t i = 0; i < s_vTimers.size();) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		auto t = s_vTimers[i]; | 
					
						
							|  |  |  | 		if (t->getParentNode() == pParentNode) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-10-21 19:09:31 +08:00
										 |  |  | 			SafeReleaseAndClear(&t); | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 			s_vTimers.erase(s_vTimers.begin() + i); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			i++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-20 00:59:26 +08:00
										 |  |  | void e2d::ETimerManager::_clearManager() | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-20 00:59:26 +08:00
										 |  |  | 	s_vTimers.clear(); | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-20 00:59:26 +08:00
										 |  |  | void e2d::ETimerManager::_resetAllTimers() | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-20 00:59:26 +08:00
										 |  |  | 	for (const auto & t : s_vTimers) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-10-20 00:59:26 +08:00
										 |  |  | 		t->m_tLast = GetNow(); | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::ETimerManager::startAllTimers() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ETimerManager::startAllTimersBindedWith(EApp::getCurrentScene()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::ETimerManager::stopAllTimers() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ETimerManager::stopAllTimersBindedWith(EApp::getCurrentScene()); | 
					
						
							|  |  |  | } |