| 
									
										
										
										
											2018-04-21 21:24:46 +08:00
										 |  |  |  | #include "..\e2dtool.h"
 | 
					
						
							|  |  |  |  | #include "..\e2dnode.h"
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | class TimerInfo | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | public: | 
					
						
							|  |  |  |  | 	TimerInfo( | 
					
						
							| 
									
										
										
										
											2018-05-08 11:37:11 +08:00
										 |  |  |  | 		const e2d::Function& func, | 
					
						
							| 
									
										
										
										
											2018-05-07 15:48:06 +08:00
										 |  |  |  | 		const e2d::String& name, | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 		double delay, | 
					
						
							|  |  |  |  | 		int updateTimes, | 
					
						
							|  |  |  |  | 		bool paused | 
					
						
							|  |  |  |  | 	) | 
					
						
							|  |  |  |  | 		: running(!paused) | 
					
						
							|  |  |  |  | 		, stopped(false) | 
					
						
							|  |  |  |  | 		, runTimes(0) | 
					
						
							|  |  |  |  | 		, totalTimes(updateTimes) | 
					
						
							|  |  |  |  | 		, delay(max(delay, 0)) | 
					
						
							|  |  |  |  | 		, lastTime(e2d::Time::getTotalTime()) | 
					
						
							|  |  |  |  | 		, callback(func) | 
					
						
							|  |  |  |  | 		, name(name) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 	void update() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-05-14 22:51:40 +08:00
										 |  |  |  | 		if (callback) | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-05-14 22:51:40 +08:00
										 |  |  |  | 			callback(); | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-14 22:51:40 +08:00
										 |  |  |  | 		++runTimes; | 
					
						
							|  |  |  |  | 		lastTime += delay; | 
					
						
							| 
									
										
										
										
											2018-03-03 17:02:08 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-14 22:51:40 +08:00
										 |  |  |  | 		if (runTimes == totalTimes) | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-05-14 22:51:40 +08:00
										 |  |  |  | 			stopped = true; | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	bool ready() | 
					
						
							| 
									
										
										
										
											2018-04-21 00:46:26 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 		if (this->running) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			if (this->delay == 0) | 
					
						
							|  |  |  |  | 				return true; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if ((e2d::Time::getTotalTime() - this->lastTime) >= this->delay) | 
					
						
							|  |  |  |  | 				return true; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2018-04-21 00:46:26 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | public: | 
					
						
							|  |  |  |  | 	bool	running; | 
					
						
							|  |  |  |  | 	bool	stopped; | 
					
						
							|  |  |  |  | 	int		runTimes; | 
					
						
							|  |  |  |  | 	int		totalTimes; | 
					
						
							|  |  |  |  | 	double	delay; | 
					
						
							|  |  |  |  | 	double	lastTime; | 
					
						
							|  |  |  |  | 	e2d::String		name; | 
					
						
							|  |  |  |  | 	e2d::Function	callback; | 
					
						
							|  |  |  |  | }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | static std::vector<TimerInfo*> s_vTimers; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-08 11:37:11 +08:00
										 |  |  |  | void e2d::Timer::start(const Function& func, double delay, int updateTimes, bool paused, const String& name) | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-15 23:59:58 +08:00
										 |  |  |  | 	auto timer = new (std::nothrow) TimerInfo(func, name, delay, updateTimes, paused); | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 	s_vTimers.push_back(timer); | 
					
						
							| 
									
										
										
										
											2018-04-21 00:46:26 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-08 11:37:11 +08:00
										 |  |  |  | void e2d::Timer::start(const Function& func, const String& name) | 
					
						
							| 
									
										
										
										
											2018-04-21 00:46:26 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 	Timer::start(func, 0, -1, false, name); | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-08 11:37:11 +08:00
										 |  |  |  | void e2d::Timer::startOnce(const Function& func, double timeOut) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-15 23:59:58 +08:00
										 |  |  |  | 	auto timer = new (std::nothrow) TimerInfo(func, L"", timeOut, 1, false); | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 	s_vTimers.push_back(timer); | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 15:48:06 +08:00
										 |  |  |  | void e2d::Timer::pause(const String& name) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-24 20:22:41 +08:00
										 |  |  |  | 	for (auto timer : s_vTimers) | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (timer->name == name) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			timer->running = false; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 15:48:06 +08:00
										 |  |  |  | void e2d::Timer::resume(const String& name) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-24 20:22:41 +08:00
										 |  |  |  | 	for (auto timer : s_vTimers) | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (timer->name == name) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			timer->running = true; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 15:48:06 +08:00
										 |  |  |  | void e2d::Timer::stop(const String& name) | 
					
						
							| 
									
										
										
										
											2017-10-21 19:09:31 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-24 20:22:41 +08:00
										 |  |  |  | 	for (auto timer : s_vTimers) | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (timer->name == name) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			timer->stopped = true; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-21 19:09:31 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | void e2d::Timer::pauseAll() | 
					
						
							| 
									
										
										
										
											2017-10-21 19:09:31 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-24 20:22:41 +08:00
										 |  |  |  | 	for (auto timer : s_vTimers) | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		timer->running = false; | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-21 19:09:31 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | void e2d::Timer::resumeAll() | 
					
						
							| 
									
										
										
										
											2017-12-04 11:18:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-24 20:22:41 +08:00
										 |  |  |  | 	for (auto timer : s_vTimers) | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		timer->running = true; | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-04 11:18:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | void e2d::Timer::stopAll() | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-24 20:22:41 +08:00
										 |  |  |  | 	for (auto timer : s_vTimers) | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 		timer->stopped = true; | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | void e2d::Timer::__update() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (s_vTimers.empty() || Game::isPaused()) | 
					
						
							|  |  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 	for (size_t i = 0; i < s_vTimers.size();) | 
					
						
							| 
									
										
										
										
											2017-10-21 19:09:31 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 		auto timer = s_vTimers[i]; | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹͣ<CDA3>Ķ<EFBFBD>ʱ<EFBFBD><CAB1>
 | 
					
						
							|  |  |  |  | 		if (timer->stopped) | 
					
						
							| 
									
										
										
										
											2018-03-03 17:02:08 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 			delete timer; | 
					
						
							|  |  |  |  | 			s_vTimers.erase(s_vTimers.begin() + i); | 
					
						
							| 
									
										
										
										
											2018-03-03 17:02:08 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 		else | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 			// <20><><EFBFBD>¶<EFBFBD>ʱ<EFBFBD><CAB1>
 | 
					
						
							|  |  |  |  | 			if (timer->ready()) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				timer->update(); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			++i; | 
					
						
							| 
									
										
										
										
											2018-03-03 17:02:08 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-11-07 22:20:46 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-21 19:09:31 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | void e2d::Timer::__resetAll() | 
					
						
							| 
									
										
										
										
											2017-10-21 19:09:31 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-24 20:22:41 +08:00
										 |  |  |  | 	for (auto timer : s_vTimers) | 
					
						
							| 
									
										
										
										
											2017-11-08 15:23:07 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 		timer->lastTime = Time::getTotalTime(); | 
					
						
							| 
									
										
										
										
											2017-10-21 19:09:31 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Timer::__uninit() | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-24 20:22:41 +08:00
										 |  |  |  | 	for (auto timer : s_vTimers) | 
					
						
							| 
									
										
										
										
											2018-04-24 13:28:21 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		delete timer; | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 	s_vTimers.clear(); | 
					
						
							|  |  |  |  | } |