| 
									
										
										
										
											2018-04-21 21:24:46 +08:00
										 |  |  |  | #include "..\e2dtool.h"
 | 
					
						
							|  |  |  |  | #include "..\e2dnode.h"
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 23:55:53 +08:00
										 |  |  |  | namespace e2d | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-22 23:55:53 +08:00
										 |  |  |  | 	class TimerEntity | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-05-22 23:55:53 +08:00
										 |  |  |  | 	public: | 
					
						
							| 
									
										
										
										
											2018-05-24 16:25:05 +08:00
										 |  |  |  | 		explicit TimerEntity( | 
					
						
							| 
									
										
										
										
											2018-05-22 23:55:53 +08:00
										 |  |  |  | 			const e2d::Function& func, | 
					
						
							|  |  |  |  | 			const e2d::String& name, | 
					
						
							|  |  |  |  | 			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) | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 23:55:53 +08:00
										 |  |  |  | 		void update() | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-05-22 23:55:53 +08:00
										 |  |  |  | 			if (callback) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				callback(); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			++runTimes; | 
					
						
							|  |  |  |  | 			lastTime += delay; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (runTimes == totalTimes) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				stopped = true; | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 23:55:53 +08:00
										 |  |  |  | 		bool ready() | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-05-22 23:55:53 +08:00
										 |  |  |  | 			if (this->running) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				if (this->delay == 0) | 
					
						
							|  |  |  |  | 					return true; | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 23:55:53 +08:00
										 |  |  |  | 				if ((e2d::Time::getTotalTime() - this->lastTime) >= this->delay) | 
					
						
							|  |  |  |  | 					return true; | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 			return false; | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 23:55:53 +08:00
										 |  |  |  | 	public: | 
					
						
							|  |  |  |  | 		bool	running; | 
					
						
							|  |  |  |  | 		bool	stopped; | 
					
						
							|  |  |  |  | 		int		runTimes; | 
					
						
							|  |  |  |  | 		int		totalTimes; | 
					
						
							|  |  |  |  | 		double	delay; | 
					
						
							|  |  |  |  | 		double	lastTime; | 
					
						
							|  |  |  |  | 		e2d::String		name; | 
					
						
							|  |  |  |  | 		e2d::Function	callback; | 
					
						
							|  |  |  |  | 	}; | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 23:55:53 +08:00
										 |  |  |  | static std::vector<e2d::TimerEntity*> s_vTimers; | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 22:22:24 +08:00
										 |  |  |  | void e2d::Timer::add(const Function& func, double delay, int updateTimes, bool paused, const String& name) | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-22 23:55:53 +08:00
										 |  |  |  | 	auto timer = new (std::nothrow) TimerEntity(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-26 22:22:24 +08:00
										 |  |  |  | void e2d::Timer::add(const Function& func, const String& name) | 
					
						
							| 
									
										
										
										
											2018-04-21 00:46:26 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-26 22:22:24 +08:00
										 |  |  |  | 	Timer::add(func, 0, -1, false, name); | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 22:22:24 +08:00
										 |  |  |  | void e2d::Timer::start(double timeout, const Function& func) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-26 22:22:24 +08:00
										 |  |  |  | 	auto timer = new (std::nothrow) TimerEntity(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-26 22:22:24 +08:00
										 |  |  |  | void e2d::Timer::stop(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-26 22:22:24 +08:00
										 |  |  |  | void e2d::Timer::start(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-26 22:22:24 +08:00
										 |  |  |  | void e2d::Timer::remove(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-05-26 22:22:24 +08:00
										 |  |  |  | void e2d::Timer::stopAll() | 
					
						
							| 
									
										
										
										
											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-05-26 22:22:24 +08:00
										 |  |  |  | void e2d::Timer::startAll() | 
					
						
							| 
									
										
										
										
											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-05-26 22:22:24 +08:00
										 |  |  |  | void e2d::Timer::removeAll() | 
					
						
							| 
									
										
										
										
											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() | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-03 01:49:20 +08:00
										 |  |  |  | 	if (s_vTimers.empty() || Game::getInstance()->isPaused()) | 
					
						
							| 
									
										
										
										
											2018-04-24 09:02:06 +08:00
										 |  |  |  | 		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(); | 
					
						
							|  |  |  |  | } |