| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											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:
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | // The above copyright notice and this permission notice shall be included in
 | 
					
						
							|  |  |  | // all copies or substantial portions of the Software.
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | // 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-12-23 18:05:08 +08:00
										 |  |  | #include <kiwano/2d/TextActor.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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 16:54:47 +08:00
										 |  |  | TextActorPtr TextActor::Create(const String& text) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-06 16:54:47 +08:00
										 |  |  |     TextActorPtr ptr = new (std::nothrow) TextActor; | 
					
						
							|  |  |  |     if (ptr) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ptr->SetText(text); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ptr; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 16:54:47 +08:00
										 |  |  | TextActorPtr TextActor::Create(const String& text, const TextStyle& style) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-06 16:54:47 +08:00
										 |  |  |     TextActorPtr ptr = new (std::nothrow) TextActor; | 
					
						
							|  |  |  |     if (ptr) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ptr->SetStyle(style); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         ptr->SetText(text); | 
					
						
							| 
									
										
										
										
											2020-02-06 16:54:47 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return ptr; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 16:54:47 +08:00
										 |  |  | TextActor::TextActor() | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     layout_ = TextLayout::Create(); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TextActor::~TextActor() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::OnRender(RenderContext& ctx) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     if (layout_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ctx.DrawTextLayout(*layout_); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 12:06:29 +08:00
										 |  |  | Size TextActor::GetSize() const | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-17 12:06:29 +08:00
										 |  |  |     const_cast<TextActor*>(this)->UpdateDirtyLayout(); | 
					
						
							|  |  |  |     return Actor::GetSize(); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::SetText(String const& text) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-17 17:01:12 +08:00
										 |  |  |     layout_->Reset(text, style_); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | void TextActor::SetStyle(const TextStyle& style) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     style_ = style; | 
					
						
							| 
									
										
										
										
											2020-02-17 17:01:12 +08:00
										 |  |  |     layout_->Reset(style); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::SetFont(FontPtr font) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     if (style_.font != font) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         style_.font = font; | 
					
						
							| 
									
										
										
										
											2020-02-17 17:01:12 +08:00
										 |  |  |         layout_->SetFont(font, { 0, layout_->GetContentLength() }); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | void TextActor::SetFontFamily(String const& family) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (style_.font_family != family) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         style_.font_family = family; | 
					
						
							| 
									
										
										
										
											2020-02-17 17:01:12 +08:00
										 |  |  |         layout_->SetFontFamily(family, { 0, layout_->GetContentLength() }); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::SetFontSize(float size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (style_.font_size != size) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         style_.font_size = size; | 
					
						
							| 
									
										
										
										
											2020-02-17 17:01:12 +08:00
										 |  |  |         layout_->SetFontSize(size, { 0, layout_->GetContentLength() }); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::SetFontWeight(uint32_t weight) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (style_.font_weight != weight) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         style_.font_weight = weight; | 
					
						
							| 
									
										
										
										
											2020-02-17 17:01:12 +08:00
										 |  |  |         layout_->SetFontWeight(weight, { 0, layout_->GetContentLength() }); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::SetItalic(bool italic) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (style_.italic != italic) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         style_.italic = italic; | 
					
						
							| 
									
										
										
										
											2020-02-17 17:01:12 +08:00
										 |  |  |         layout_->SetItalic(italic, { 0, layout_->GetContentLength() }); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::SetUnderline(bool enable) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (style_.show_underline != enable) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         style_.show_underline = enable; | 
					
						
							| 
									
										
										
										
											2020-02-17 17:01:12 +08:00
										 |  |  |         layout_->SetUnderline(enable, { 0, layout_->GetContentLength() }); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::SetStrikethrough(bool enable) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (style_.show_strikethrough != enable) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         style_.show_strikethrough = enable; | 
					
						
							| 
									
										
										
										
											2020-02-17 17:01:12 +08:00
										 |  |  |         layout_->SetStrikethrough(enable, { 0, layout_->GetContentLength() }); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::SetWrapWidth(float wrap_width) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (style_.wrap_width != wrap_width) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         style_.wrap_width = wrap_width; | 
					
						
							| 
									
										
										
										
											2020-02-17 17:01:12 +08:00
										 |  |  |         layout_->SetWrapWidth(wrap_width); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::SetLineSpacing(float line_spacing) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (style_.line_spacing != line_spacing) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         style_.line_spacing = line_spacing; | 
					
						
							| 
									
										
										
										
											2020-02-17 17:01:12 +08:00
										 |  |  |         layout_->SetLineSpacing(line_spacing); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::SetAlignment(TextAlign align) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (style_.alignment != align) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         style_.alignment = align; | 
					
						
							| 
									
										
										
										
											2020-02-17 17:01:12 +08:00
										 |  |  |         layout_->SetAlignment(align); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::SetFillBrush(BrushPtr brush) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (style_.fill_brush != brush) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         style_.fill_brush = brush; | 
					
						
							|  |  |  |         layout_->SetDefaultFillBrush(brush); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::SetOutlineBrush(BrushPtr brush) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (style_.outline_brush != brush) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         style_.outline_brush = brush; | 
					
						
							|  |  |  |         layout_->SetDefaultOutlineBrush(brush); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::SetOutlineStrokeStyle(StrokeStylePtr stroke) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     if (style_.outline_stroke != stroke) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         style_.outline_stroke = stroke; | 
					
						
							|  |  |  |         layout_->SetDefaultOutlineStrokeStyle(stroke); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-18 12:53:18 +08:00
										 |  |  | void TextActor::SetFillColor(Color const& color) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (style_.fill_brush) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         style_.fill_brush->SetColor(color); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         SetFillBrush(Brush::Create(color)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::SetOutlineColor(Color const& outline_color) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (style_.outline_brush) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         style_.outline_brush->SetColor(outline_color); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         SetFillBrush(Brush::Create(outline_color)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::SetTextLayout(TextLayoutPtr layout) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     KGE_ASSERT(layout && "TextLayout must not be nullptr"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (layout_ != layout) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         layout_ = layout; | 
					
						
							|  |  |  |         ForceUpdateLayout(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 12:06:29 +08:00
										 |  |  | void TextActor::Update(Duration dt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     this->UpdateDirtyLayout(); | 
					
						
							|  |  |  |     Actor::Update(dt); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool TextActor::CheckVisibility(RenderContext& ctx) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return layout_ && layout_->IsValid() && Actor::CheckVisibility(ctx); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::UpdateDirtyLayout() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     KGE_ASSERT(layout_); | 
					
						
							|  |  |  |     if (layout_->UpdateWhenDirty()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ForceUpdateLayout(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextActor::ForceUpdateLayout() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     KGE_ASSERT(layout_); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     layout_->UpdateWhenDirty(); | 
					
						
							| 
									
										
										
										
											2020-02-17 17:01:12 +08:00
										 |  |  |     SetSize(layout_->GetSize()); | 
					
						
							| 
									
										
										
										
											2020-02-17 12:06:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | }  // namespace kiwano
 |