| 
									
										
										
										
											2018-09-06 23:26:32 +08:00
										 |  |  |  | #include "..\e2dobject.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-05 13:17:07 +08:00
										 |  |  |  | #include "..\e2dmodule.h"
 | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::Sprite::Sprite() | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +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-09-04 22:42:34 +08:00
										 |  |  |  | 	: image_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +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-09-04 22:42:34 +08:00
										 |  |  |  | 	: image_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 	Open(res); | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | e2d::Sprite::Sprite(const Resource& res, const Rect& crop_rect) | 
					
						
							|  |  |  |  | 	: image_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 	Open(res); | 
					
						
							|  |  |  |  | 	Crop(crop_rect); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | e2d::Sprite::Sprite(const String & file_name) | 
					
						
							|  |  |  |  | 	: image_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 	Open(file_name); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | e2d::Sprite::Sprite(const String & file_name, const Rect & crop_rect) | 
					
						
							|  |  |  |  | 	: image_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 	Open(file_name); | 
					
						
							|  |  |  |  | 	Crop(crop_rect); | 
					
						
							| 
									
										
										
										
											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-09-07 00:28:54 +08:00
										 |  |  |  | 	SafeRelease(image_); | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | bool e2d::Sprite::Open(Image * image) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	if (image) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-07 00:28:54 +08:00
										 |  |  |  | 		if (image_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			image_->Release(); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 		image_ = image; | 
					
						
							|  |  |  |  | 		image_->Retain(); | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 		Node::SetSize(image_->GetWidth(), image_->GetHeight()); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 		return true; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	return false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | bool e2d::Sprite::Open(const Resource& res) | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 	if (!image_) | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-07 00:28:54 +08:00
										 |  |  |  | 		image_ = new Image(); | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 		image_->Retain(); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 	if (image_->Open(res)) | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +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-09-04 22:42:34 +08:00
										 |  |  |  | bool e2d::Sprite::Open(const String & file_name) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 	if (!image_) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-07 00:28:54 +08:00
										 |  |  |  | 		image_ = new Image(); | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 		image_->Retain(); | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 	if (image_->Open(file_name)) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 		Node::SetSize(image_->GetWidth(), image_->GetHeight()); | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 		return true; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | void e2d::Sprite::Crop(const Rect& crop_rect) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 	image_->Crop(crop_rect); | 
					
						
							|  |  |  |  | 	Node::SetSize( | 
					
						
							|  |  |  |  | 		std::min(std::max(crop_rect.size.width, 0.f), image_->GetSourceWidth() - image_->GetCropX()), | 
					
						
							|  |  |  |  | 		std::min(std::max(crop_rect.size.height, 0.f), image_->GetSourceHeight() - image_->GetCropY()) | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | e2d::Image * e2d::Sprite::GetImage() const | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 	return image_; | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | void e2d::Sprite::Draw() const | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +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-09-04 22:42:34 +08:00
										 |  |  |  | 		float fCropX = image_->GetCropX(); | 
					
						
							|  |  |  |  | 		float fCropY = image_->GetCropY(); | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | 		// <20><>ȾͼƬ
 | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  |  | 		Renderer::GetInstance()->GetRenderTarget()->DrawBitmap( | 
					
						
							|  |  |  |  | 			image_->GetBitmap(), | 
					
						
							|  |  |  |  | 			D2D1::RectF(0, 0, size_.width, size_.height), | 
					
						
							|  |  |  |  | 			display_opacity_, | 
					
						
							| 
									
										
										
										
											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-09-04 22:42:34 +08:00
										 |  |  |  | 				fCropX + size_.width, | 
					
						
							|  |  |  |  | 				fCropY + size_.height | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  |  | 			) | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } |