| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | // 
 | 
					
						
							|  |  |  | // 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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-11 21:55:29 +08:00
										 |  |  | #include <kiwano/2d/Canvas.h>
 | 
					
						
							| 
									
										
										
										
											2019-11-13 14:33:15 +08:00
										 |  |  | #include <kiwano/core/Logger.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-17 16:55:47 +08:00
										 |  |  | #include <kiwano/render/Renderer.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | namespace kiwano | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	Canvas::Canvas() | 
					
						
							|  |  |  | 		: cache_expired_(false) | 
					
						
							|  |  |  | 		, stroke_width_(1.0f) | 
					
						
							| 
									
										
										
										
											2020-01-10 11:43:07 +08:00
										 |  |  | 		, stroke_style_() | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Canvas::~Canvas() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::BeginDraw() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->BeginDraw(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::EndDraw() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->EndDraw(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		cache_expired_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 	void Canvas::OnRender(RenderContext& ctx) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 		UpdateCache(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2019-12-26 14:15:25 +08:00
										 |  |  | 		if (texture_cached_ && texture_cached_->IsValid()) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 			PrepareToRender(ctx); | 
					
						
							| 
									
										
										
										
											2019-08-14 00:28:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 14:15:25 +08:00
										 |  |  | 			Rect bitmap_rect(0.f, 0.f, texture_cached_->GetWidth(), texture_cached_->GetHeight()); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 			ctx.DrawTexture(*texture_cached_, bitmap_rect, bitmap_rect); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 	void Canvas::SetBrush(BrushPtr brush) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->SetCurrentBrush(brush); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 22:23:13 +08:00
										 |  |  | 	float Canvas::GetStrokeWidth() const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return stroke_width_; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 	void Canvas::SetBrushTransform(Transform const& transform) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->SetTransform(transform.ToMatrix()); | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 23:36:29 +08:00
										 |  |  | 	void Canvas::SetBrushTransform(Matrix3x2 const & transform) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->SetTransform(transform); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-20 19:32:36 +08:00
										 |  |  | 	void Canvas::PushLayerArea(LayerArea& area) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->PushLayer(area); | 
					
						
							| 
									
										
										
										
											2019-08-20 19:32:36 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::PopLayerArea() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->PopLayer(); | 
					
						
							| 
									
										
										
										
											2019-08-20 19:32:36 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::PushClipRect(Rect const& clip_rect) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->PushClipRect(clip_rect); | 
					
						
							| 
									
										
										
										
											2019-08-20 19:32:36 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::PopClipRect() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->PopClipRect(); | 
					
						
							| 
									
										
										
										
											2019-08-20 19:32:36 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 	void Canvas::DrawLine(Point const& begin, Point const& end) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->SetCurrentBrush(stroke_brush_); | 
					
						
							|  |  |  | 		ctx_->DrawLine( | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			begin, | 
					
						
							|  |  |  | 			end, | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			stroke_width_, | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			stroke_style_ | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 		cache_expired_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 22:23:13 +08:00
										 |  |  | 	void Canvas::DrawCircle(Point const& center, float radius) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->SetCurrentBrush(stroke_brush_); | 
					
						
							|  |  |  | 		ctx_->DrawEllipse( | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			center, | 
					
						
							|  |  |  | 			Vec2(radius, radius), | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			stroke_width_, | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			stroke_style_ | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 		cache_expired_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 	void Canvas::DrawEllipse(Point const& center, Vec2 const& radius) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->SetCurrentBrush(stroke_brush_); | 
					
						
							|  |  |  | 		ctx_->DrawEllipse( | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			center, | 
					
						
							|  |  |  | 			radius, | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			stroke_width_, | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			stroke_style_ | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 		cache_expired_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 	void Canvas::DrawRect(Rect const& rect) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->SetCurrentBrush(stroke_brush_); | 
					
						
							|  |  |  | 		ctx_->DrawRectangle( | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			rect, | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			stroke_width_, | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			stroke_style_ | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 		cache_expired_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 	void Canvas::DrawRoundedRect(Rect const& rect, Vec2 const& radius) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->SetCurrentBrush(stroke_brush_); | 
					
						
							|  |  |  | 		ctx_->DrawRoundedRectangle( | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			rect, | 
					
						
							|  |  |  | 			radius, | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			stroke_width_, | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			stroke_style_ | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 		cache_expired_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 22:23:13 +08:00
										 |  |  | 	void Canvas::FillCircle(Point const& center, float radius) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->SetCurrentBrush(fill_brush_); | 
					
						
							|  |  |  | 		ctx_->FillEllipse( | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			center, | 
					
						
							| 
									
										
										
										
											2019-09-09 22:02:53 +08:00
										 |  |  | 			Vec2(radius, radius) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		); | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 		cache_expired_ = true; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 	void Canvas::FillEllipse(Point const& center, Vec2 const& radius) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->SetCurrentBrush(fill_brush_); | 
					
						
							|  |  |  | 		ctx_->FillEllipse( | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			center, | 
					
						
							| 
									
										
										
										
											2019-09-09 22:02:53 +08:00
										 |  |  | 			radius | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 		cache_expired_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 	void Canvas::FillRect(Rect const& rect) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->SetCurrentBrush(fill_brush_); | 
					
						
							|  |  |  | 		ctx_->FillRectangle( | 
					
						
							| 
									
										
										
										
											2019-09-09 22:02:53 +08:00
										 |  |  | 			rect | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 		cache_expired_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 	void Canvas::FillRoundedRect(Rect const& rect, Vec2 const& radius) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->SetCurrentBrush(fill_brush_); | 
					
						
							|  |  |  | 		ctx_->FillRoundedRectangle( | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			rect, | 
					
						
							| 
									
										
										
										
											2019-09-09 22:02:53 +08:00
										 |  |  | 			radius | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 		cache_expired_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 14:15:25 +08:00
										 |  |  | 	void Canvas::DrawTexture(TexturePtr texture, const Rect* src_rect, const Rect* dest_rect) | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-26 14:15:25 +08:00
										 |  |  | 		if (texture) | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 			InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 			ctx_->DrawTexture(*texture, src_rect, dest_rect); | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			cache_expired_ = true; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-23 18:05:08 +08:00
										 |  |  | 	void Canvas::DrawTextLayout(String const& text, Point const& point) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 		if (text.empty()) | 
					
						
							|  |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 		TextLayout layout; | 
					
						
							|  |  |  | 		layout.SetStyle(text_style_); | 
					
						
							|  |  |  | 		layout.SetText(text); | 
					
						
							| 
									
										
										
										
											2019-12-23 18:05:08 +08:00
										 |  |  | 		DrawTextLayout(layout, point); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-23 18:05:08 +08:00
										 |  |  | 	void Canvas::DrawTextLayout(TextLayout const& layout, Point const& point) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->DrawTextLayout(layout, point); | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::BeginPath(Point const& begin_pos) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		geo_sink_.BeginPath(begin_pos); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::EndPath(bool closed) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 		geo_sink_.EndPath(closed); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::AddLine(Point const & point) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 		geo_sink_.AddLine(point); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 21:16:38 +08:00
										 |  |  | 	void Canvas::AddLines(Vector<Point> const& points) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 		geo_sink_.AddLines(points); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::AddBezier(Point const & point1, Point const & point2, Point const & point3) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 		geo_sink_.AddBezier(point1, point2, point3); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-23 18:05:08 +08:00
										 |  |  | 	void Canvas::AddArc(Point const & point, Size const & radius, float rotation, bool clockwise, bool is_small) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 		geo_sink_.AddArc(point, radius, rotation, clockwise, is_small); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::StrokePath() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->SetCurrentBrush(stroke_brush_); | 
					
						
							|  |  |  | 		ctx_->DrawGeometry( | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			geo_sink_.GetGeometry(), | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			stroke_width_, | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 			stroke_style_ | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 		cache_expired_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::FillPath() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->SetCurrentBrush(fill_brush_); | 
					
						
							|  |  |  | 		ctx_->FillGeometry( | 
					
						
							| 
									
										
										
										
											2019-09-09 22:02:53 +08:00
										 |  |  | 			geo_sink_.GetGeometry() | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 		cache_expired_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::Clear() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->Clear(); | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 		cache_expired_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Canvas::Clear(Color const& clear_color) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		InitRenderTargetAndBrushs(); | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		ctx_->Clear(clear_color); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		cache_expired_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 14:15:25 +08:00
										 |  |  | 	TexturePtr Canvas::ExportToTexture() const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 		UpdateCache(); | 
					
						
							| 
									
										
										
										
											2019-08-21 16:33:41 +08:00
										 |  |  | 		return texture_cached_; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 	void Canvas::InitRenderTargetAndBrushs() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		if (!ctx_) | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-01-17 11:24:24 +08:00
										 |  |  | 			Renderer::Instance().CreateTextureRenderTarget(ctx_); | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!stroke_brush_) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			stroke_brush_ = new Brush; | 
					
						
							|  |  |  | 			stroke_brush_->SetColor(Color::White); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!fill_brush_) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			fill_brush_ = new Brush; | 
					
						
							|  |  |  | 			fill_brush_->SetColor(Color::White); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 	void Canvas::UpdateCache() const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 		if (cache_expired_ && ctx_) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 			if (!texture_cached_) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				texture_cached_ = new Texture; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-10 15:22:12 +08:00
										 |  |  | 			if (ctx_->GetOutput(*texture_cached_)) | 
					
						
							| 
									
										
										
										
											2019-12-27 23:42:51 +08:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				cache_expired_ = false; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-08 14:15:06 +08:00
										 |  |  | } |