| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | #include "..\e2dtool.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | e2d::Task::Task(const Function & func, const String & name) | 
					
						
							|  |  |  | 	: _running(true) | 
					
						
							|  |  |  | 	, _stopped(false) | 
					
						
							|  |  |  | 	, _runTimes(0) | 
					
						
							|  |  |  | 	, _totalTimes(-1) | 
					
						
							| 
									
										
										
										
											2018-08-02 00:27:45 +08:00
										 |  |  | 	, _delay() | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	, _callback(func) | 
					
						
							|  |  |  | 	, _name(name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 20:06:27 +08:00
										 |  |  | e2d::Task::Task(const Function & func, float delay, int times, const String & name) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	: _running(true) | 
					
						
							|  |  |  | 	, _stopped(false) | 
					
						
							|  |  |  | 	, _runTimes(0) | 
					
						
							| 
									
										
										
										
											2018-07-28 20:06:27 +08:00
										 |  |  | 	, _delay(std::max(delay, 0.f)) | 
					
						
							| 
									
										
										
										
											2018-08-02 00:27:45 +08:00
										 |  |  | 	, _totalTimes(times) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	, _callback(func) | 
					
						
							|  |  |  | 	, _name(name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 13:24:31 +08:00
										 |  |  | void e2d::Task::start() | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-12 13:24:31 +08:00
										 |  |  | 	_running = true; | 
					
						
							|  |  |  | 	_lastTime = Time::now(); | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 13:24:31 +08:00
										 |  |  | void e2d::Task::stop() | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-12 13:24:31 +08:00
										 |  |  | 	_running = false; | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 13:24:31 +08:00
										 |  |  | void e2d::Task::_update() | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-02 00:27:45 +08:00
										 |  |  | 	if (_totalTimes == 0) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-08-02 00:27:45 +08:00
										 |  |  | 		_stopped = true; | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	++_runTimes; | 
					
						
							|  |  |  | 	_lastTime += _delay; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-02 00:27:45 +08:00
										 |  |  | 	if (_callback) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		_callback(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	if (_runTimes == _totalTimes) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		_stopped = true; | 
					
						
							| 
									
										
										
										
											2018-08-02 00:27:45 +08:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 13:24:31 +08:00
										 |  |  | bool e2d::Task::_isReady() const | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (_running) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-08-02 00:27:45 +08:00
										 |  |  | 		if (_delay.milliseconds() == 0) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-08-02 00:27:45 +08:00
										 |  |  | 		if (Time::now() - _lastTime >= _delay) | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool e2d::Task::isRunning() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return _running; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  | e2d::String e2d::Task::getName() const | 
					
						
							| 
									
										
										
										
											2018-07-05 22:05:23 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	return _name; | 
					
						
							|  |  |  | } |