| 
									
										
										
										
											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-11-16 15:53:39 +08:00
										 |  |  | #include "base.hpp"
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | #include "Canvas.h"
 | 
					
						
							|  |  |  | #include "render.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  | #include "logs.h"
 | 
					
						
							| 
									
										
										
										
											2018-08-23 00:03:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | namespace easy2d | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-12 02:10:35 +08:00
										 |  |  | 	///////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 	///////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 	// FIXME!!!
 | 
					
						
							|  |  |  | 	///////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 	///////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	Canvas::Canvas(float width, float height) | 
					
						
							|  |  |  | 		: render_target_(nullptr) | 
					
						
							|  |  |  | 		, fill_brush_(nullptr) | 
					
						
							|  |  |  | 		, line_brush_(nullptr) | 
					
						
							|  |  |  | 		, stroke_style_(nullptr) | 
					
						
							|  |  |  | 		, stroke_width_(1.0f) | 
					
						
							| 
									
										
										
										
											2018-11-12 02:10:35 +08:00
										 |  |  | 		, stroke_(StrokeStyle::Miter) | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-12 02:10:35 +08:00
										 |  |  | 		// render_target_ = render::D2D.HwndRenderTarget;
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 		render_target_->AddRef(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ThrowIfFailed( | 
					
						
							|  |  |  | 			render_target_->CreateSolidColorBrush( | 
					
						
							|  |  |  | 				D2D1::ColorF(D2D1::ColorF::White), | 
					
						
							|  |  |  | 				&fill_brush_ | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ThrowIfFailed( | 
					
						
							|  |  |  | 			render_target_->CreateSolidColorBrush( | 
					
						
							|  |  |  | 				D2D1::ColorF(D2D1::ColorF::White), | 
					
						
							|  |  |  | 				&line_brush_ | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		this->SetClipEnabled(true); | 
					
						
							|  |  |  | 		this->SetWidth(width); | 
					
						
							|  |  |  | 		this->SetHeight(height); | 
					
						
							|  |  |  | 		this->SetStrokeStyle(stroke_); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-23 00:03:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	Canvas::~Canvas() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		SafeRelease(stroke_style_); | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 		SafeRelease(line_brush_); | 
					
						
							|  |  |  | 		SafeRelease(fill_brush_); | 
					
						
							|  |  |  | 		SafeRelease(render_target_); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-23 00:03:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	void Canvas::SetLineColor(const Color & color) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		line_brush_->SetColor(D2D_COLOR_F(color)); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-23 00:03:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	void Canvas::SetFillColor(const Color & color) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		fill_brush_->SetColor(D2D_COLOR_F(color)); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-23 00:03:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	void Canvas::SetStrokeWidth(float width) | 
					
						
							| 
									
										
										
										
											2018-08-23 00:03:26 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 		stroke_width_ = std::max(width, 0.f); | 
					
						
							| 
									
										
										
										
											2018-08-23 00:03:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 02:10:35 +08:00
										 |  |  | 	void Canvas::SetStrokeStyle(StrokeStyle stroke) | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		SafeRelease(stroke_style_); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-15 14:35:19 +08:00
										 |  |  | 		stroke_style_ = devices::Graphics::Instance()->GetStrokeStyle(stroke); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (stroke_style_) | 
					
						
							|  |  |  | 			stroke_style_->AddRef(); | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-23 12:25:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	Color Canvas::GetLineColor() const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return line_brush_->GetColor(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-23 12:25:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	Color Canvas::GetFillColor() const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return fill_brush_->GetColor(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-23 12:25:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	float Canvas::GetStrokeWidth() const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return stroke_width_; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-23 12:25:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 02:10:35 +08:00
										 |  |  | 	StrokeStyle Canvas::GetStrokeStyle() const | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return stroke_; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::DrawLine(const Point & begin, const Point & end) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		render_target_->DrawLine( | 
					
						
							|  |  |  | 			D2D1::Point2F(begin.x, begin.y), | 
					
						
							|  |  |  | 			D2D1::Point2F(end.x, end.y), | 
					
						
							|  |  |  | 			line_brush_, | 
					
						
							|  |  |  | 			stroke_width_, | 
					
						
							|  |  |  | 			stroke_style_ | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::DrawCircle(const Point & center, float radius) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		render_target_->DrawEllipse( | 
					
						
							|  |  |  | 			D2D1::Ellipse( | 
					
						
							|  |  |  | 				D2D1::Point2F( | 
					
						
							|  |  |  | 					center.x, | 
					
						
							|  |  |  | 					center.y | 
					
						
							|  |  |  | 				), | 
					
						
							|  |  |  | 				radius, | 
					
						
							|  |  |  | 				radius | 
					
						
							| 
									
										
										
										
											2018-08-23 12:25:31 +08:00
										 |  |  | 			), | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 			line_brush_, | 
					
						
							|  |  |  | 			stroke_width_, | 
					
						
							|  |  |  | 			stroke_style_ | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::DrawEllipse(const Point & center, float radius_x, float radius_y) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		render_target_->DrawEllipse( | 
					
						
							|  |  |  | 			D2D1::Ellipse( | 
					
						
							|  |  |  | 				D2D1::Point2F( | 
					
						
							|  |  |  | 					center.x, | 
					
						
							|  |  |  | 					center.y | 
					
						
							|  |  |  | 				), | 
					
						
							|  |  |  | 				radius_x, | 
					
						
							|  |  |  | 				radius_y | 
					
						
							| 
									
										
										
										
											2018-08-23 12:25:31 +08:00
										 |  |  | 			), | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 			line_brush_, | 
					
						
							|  |  |  | 			stroke_width_, | 
					
						
							|  |  |  | 			stroke_style_ | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::DrawRect(const Rect & rect) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		render_target_->DrawRectangle( | 
					
						
							| 
									
										
										
										
											2018-08-23 00:03:26 +08:00
										 |  |  | 			D2D1::RectF( | 
					
						
							|  |  |  | 				rect.origin.x, | 
					
						
							|  |  |  | 				rect.origin.y, | 
					
						
							|  |  |  | 				rect.origin.x + rect.size.width, | 
					
						
							|  |  |  | 				rect.origin.y + rect.size.height | 
					
						
							|  |  |  | 			), | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 			line_brush_, | 
					
						
							|  |  |  | 			stroke_width_, | 
					
						
							|  |  |  | 			stroke_style_ | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::DrawRoundedRect(const Rect & rect, float radius_x, float radius_y) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		render_target_->DrawRoundedRectangle( | 
					
						
							|  |  |  | 			D2D1::RoundedRect( | 
					
						
							|  |  |  | 				D2D1::RectF( | 
					
						
							|  |  |  | 					rect.origin.x, | 
					
						
							|  |  |  | 					rect.origin.y, | 
					
						
							|  |  |  | 					rect.origin.x + rect.size.width, | 
					
						
							|  |  |  | 					rect.origin.y + rect.size.height | 
					
						
							|  |  |  | 				), | 
					
						
							|  |  |  | 				radius_x, | 
					
						
							|  |  |  | 				radius_y | 
					
						
							| 
									
										
										
										
											2018-08-23 12:25:31 +08:00
										 |  |  | 			), | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 			line_brush_, | 
					
						
							|  |  |  | 			stroke_width_, | 
					
						
							|  |  |  | 			stroke_style_ | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::FillCircle(const Point & center, float radius) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		render_target_->FillEllipse( | 
					
						
							|  |  |  | 			D2D1::Ellipse( | 
					
						
							|  |  |  | 				D2D1::Point2F( | 
					
						
							|  |  |  | 					center.x, | 
					
						
							|  |  |  | 					center.y | 
					
						
							|  |  |  | 				), | 
					
						
							|  |  |  | 				radius, | 
					
						
							|  |  |  | 				radius | 
					
						
							| 
									
										
										
										
											2018-08-23 12:25:31 +08:00
										 |  |  | 			), | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 			fill_brush_ | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::FillEllipse(const Point & center, float radius_x, float radius_y) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		render_target_->FillEllipse( | 
					
						
							|  |  |  | 			D2D1::Ellipse( | 
					
						
							|  |  |  | 				D2D1::Point2F( | 
					
						
							|  |  |  | 					center.x, | 
					
						
							|  |  |  | 					center.y | 
					
						
							|  |  |  | 				), | 
					
						
							|  |  |  | 				radius_x, | 
					
						
							|  |  |  | 				radius_y | 
					
						
							|  |  |  | 			), | 
					
						
							|  |  |  | 			fill_brush_ | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::FillRect(const Rect & rect) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		render_target_->FillRectangle( | 
					
						
							| 
									
										
										
										
											2018-08-23 00:03:26 +08:00
										 |  |  | 			D2D1::RectF( | 
					
						
							|  |  |  | 				rect.origin.x, | 
					
						
							|  |  |  | 				rect.origin.y, | 
					
						
							|  |  |  | 				rect.origin.x + rect.size.width, | 
					
						
							|  |  |  | 				rect.origin.y + rect.size.height | 
					
						
							|  |  |  | 			), | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 			fill_brush_ | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::FillRoundedRect(const Rect & rect, float radius_x, float radius_y) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		render_target_->FillRoundedRectangle( | 
					
						
							|  |  |  | 			D2D1::RoundedRect( | 
					
						
							|  |  |  | 				D2D1::RectF( | 
					
						
							|  |  |  | 					rect.origin.x, | 
					
						
							|  |  |  | 					rect.origin.y, | 
					
						
							|  |  |  | 					rect.origin.x + rect.size.width, | 
					
						
							|  |  |  | 					rect.origin.y + rect.size.height | 
					
						
							|  |  |  | 				), | 
					
						
							|  |  |  | 				radius_x, | 
					
						
							|  |  |  | 				radius_y | 
					
						
							|  |  |  | 			), | 
					
						
							|  |  |  | 			fill_brush_ | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |