| 
									
										
										
										
											2018-10-03 22:02:46 +08:00
										 |  |  | // Copyright (c) 2016-2018 Easy2D - Nomango
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
					
						
							|  |  |  | // of this software and associated documentation files (the "Software"), to deal
 | 
					
						
							|  |  |  | // in the Software without restriction, including without limitation the rights
 | 
					
						
							|  |  |  | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
					
						
							|  |  |  | // copies of the Software, and to permit persons to whom the Software is
 | 
					
						
							|  |  |  | // furnished to do so, subject to the following conditions:
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // The above copyright notice and this permission notice shall be included in
 | 
					
						
							|  |  |  | // all copies or substantial portions of the Software.
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
					
						
							|  |  |  | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
					
						
							|  |  |  | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
					
						
							|  |  |  | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
					
						
							|  |  |  | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
					
						
							|  |  |  | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					
						
							|  |  |  | // THE SOFTWARE.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-10-16 14:13:15 +08:00
										 |  |  | easy2d::Sprite::Sprite() | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	: image_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  | easy2d::Sprite::Sprite(Image * image) | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	: image_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-06 10:25:29 +08:00
										 |  |  | 	Load(image); | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-28 23:24:01 +08:00
										 |  |  | easy2d::Sprite::Sprite(Resource& res) | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	: image_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-06 10:25:29 +08:00
										 |  |  | 	Load(res); | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-28 23:24:01 +08:00
										 |  |  | easy2d::Sprite::Sprite(Resource& res, const Rect& crop_rect) | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	: image_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-06 10:25:29 +08:00
										 |  |  | 	Load(res); | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	Crop(crop_rect); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-28 23:24:01 +08:00
										 |  |  | easy2d::Sprite::Sprite(const std::wstring & file_name) | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	: image_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-06 10:25:29 +08:00
										 |  |  | 	Load(file_name); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-28 23:24:01 +08:00
										 |  |  | easy2d::Sprite::Sprite(const std::wstring & file_name, const Rect & crop_rect) | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	: image_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-06 10:25:29 +08:00
										 |  |  | 	Load(file_name); | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 	Crop(crop_rect); | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 14:13:15 +08:00
										 |  |  | easy2d::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-10-16 14:13:15 +08:00
										 |  |  | bool easy2d::Sprite::Load(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-10-28 23:24:01 +08:00
										 |  |  | bool easy2d::Sprite::Load(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-10-06 10:25:29 +08:00
										 |  |  | 	if (image_->Load(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-10-28 23:24:01 +08:00
										 |  |  | bool easy2d::Sprite::Load(const std::wstring & 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-10-06 10:25:29 +08:00
										 |  |  | 	if (image_->Load(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-10-16 14:13:15 +08:00
										 |  |  | void easy2d::Sprite::Crop(const Rect& crop_rect) | 
					
						
							| 
									
										
										
										
											2018-09-30 14:54:43 +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-10-16 14:13:15 +08:00
										 |  |  | easy2d::Image * easy2d::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-10-18 00:17:27 +08:00
										 |  |  | void easy2d::Sprite::OnDraw() 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-09-16 16:26:42 +08:00
										 |  |  | 		auto crop_pos = image_->GetCropPos(); | 
					
						
							| 
									
										
										
										
											2018-10-03 18:04:04 +08:00
										 |  |  | 		Device::GetGraphics()->GetRenderTarget()->DrawBitmap( | 
					
						
							| 
									
										
										
										
											2018-09-04 22:42:34 +08:00
										 |  |  | 			image_->GetBitmap(), | 
					
						
							| 
									
										
										
										
											2018-10-18 00:17:27 +08:00
										 |  |  | 			D2D1::RectF(0, 0, GetTransform().size.width, GetTransform().size.height), | 
					
						
							|  |  |  | 			GetDisplayOpacity(), | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  | 			D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, | 
					
						
							|  |  |  | 			D2D1::RectF( | 
					
						
							| 
									
										
										
										
											2018-09-16 16:26:42 +08:00
										 |  |  | 				crop_pos.x, | 
					
						
							|  |  |  | 				crop_pos.y, | 
					
						
							| 
									
										
										
										
											2018-10-18 00:17:27 +08:00
										 |  |  | 				crop_pos.y + GetTransform().size.width, | 
					
						
							|  |  |  | 				crop_pos.y + GetTransform().size.height | 
					
						
							| 
									
										
										
										
											2018-01-30 16:45:38 +08:00
										 |  |  | 			) | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |