| 
									
										
										
										
											2018-04-21 21:24:46 +08:00
										 |  |  |  | #include "..\e2dnode.h"
 | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::Sprite::Sprite() | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	: _image(nullptr) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::Sprite::Sprite(Image * image) | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	: _image(nullptr) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-02 23:49:57 +08:00
										 |  |  |  | 	open(image); | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | e2d::Sprite::Sprite(const Resource& res) | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	: _image(nullptr) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	open(res); | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | e2d::Sprite::Sprite(const Resource& res, const Rect& cropRect) | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	: _image(nullptr) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	open(res); | 
					
						
							|  |  |  |  | 	crop(cropRect); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | e2d::Sprite::Sprite(const String & fileName) | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	: _image(nullptr) | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	open(fileName); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | e2d::Sprite::Sprite(const String & fileName, const Rect & cropRect) | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	: _image(nullptr) | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	open(fileName); | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | 	crop(cropRect); | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::Sprite::~Sprite() | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-22 21:22:27 +08:00
										 |  |  |  | 	GC::getInstance()->safeRelease(_image); | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | bool e2d::Sprite::open(Image * image) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	if (image) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-07-22 21:22:27 +08:00
										 |  |  |  | 		if (_image) _image->release(); | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 		_image = image; | 
					
						
							| 
									
										
										
										
											2018-07-22 21:22:27 +08:00
										 |  |  |  | 		_image->retain(); | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 		Node::setSize(_image->getWidth(), _image->getHeight()); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 		return true; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	return false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | bool e2d::Sprite::open(const Resource& res) | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	if (!_image) | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-07-06 12:59:32 +08:00
										 |  |  |  | 		_image = new (e2d::autorelease) Image(); | 
					
						
							| 
									
										
										
										
											2018-07-22 21:22:27 +08:00
										 |  |  |  | 		_image->retain(); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	if (_image->open(res)) | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 		Node::setSize(_image->getWidth(), _image->getHeight()); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | bool e2d::Sprite::open(const String & fileName) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	return open(Resource(fileName)); | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | void e2d::Sprite::crop(const Rect& cropRect) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | 	_image->crop(cropRect); | 
					
						
							| 
									
										
										
										
											2018-03-01 19:28:22 +08:00
										 |  |  |  | 	Node::setSize( | 
					
						
							| 
									
										
										
										
											2018-07-04 12:49:05 +08:00
										 |  |  |  | 		std::min(std::max(cropRect.size.width, 0.0), _image->getSourceWidth() - _image->getCropX()), | 
					
						
							|  |  |  |  | 		std::min(std::max(cropRect.size.height, 0.0), _image->getSourceHeight() - _image->getCropY()) | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::Image * e2d::Sprite::getImage() const | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	return _image; | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 18:44:37 +08:00
										 |  |  |  | void e2d::Sprite::onRender() const | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	if (_image && _image->getBitmap()) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 		// <20><>ȡͼƬ<CDBC>ü<EFBFBD>λ<EFBFBD><CEBB>
 | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 		float fCropX = float(_image->getCropX()); | 
					
						
							|  |  |  |  | 		float fCropY = float(_image->getCropY()); | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | 		// <20><>ȾͼƬ
 | 
					
						
							| 
									
										
										
										
											2018-07-03 18:16:26 +08:00
										 |  |  |  | 		Renderer::getInstance()->getRenderTarget()->DrawBitmap( | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 			_image->getBitmap(), | 
					
						
							|  |  |  |  | 			D2D1::RectF(0, 0, _width, _height), | 
					
						
							|  |  |  |  | 			_displayOpacity, | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | 			D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, | 
					
						
							|  |  |  |  | 			D2D1::RectF( | 
					
						
							| 
									
										
										
										
											2018-04-06 11:31:24 +08:00
										 |  |  |  | 				fCropX, | 
					
						
							|  |  |  |  | 				fCropY, | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 				fCropX + _width, | 
					
						
							|  |  |  |  | 				fCropY + _height | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | 			) | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } |