[deploy] refactor: TextLayout

This commit is contained in:
Nomango 2022-10-12 14:19:33 +08:00
parent fcad318265
commit abac7c9444
11 changed files with 45 additions and 121 deletions

View File

@ -182,13 +182,13 @@ public:
/// @param text 文字 /// @param text 文字
/// @param style 文字样式 /// @param style 文字样式
/// @param point 绘制文字的位置 /// @param point 绘制文字的位置
void DrawTextLayout(const String& text, const TextStyle& style, const Point& point); void DrawTextLayout(const String& text, const TextStyle& style, const Point& point, BrushPtr outline_brush = nullptr);
/// \~chinese /// \~chinese
/// @brief 绘制文字布局 /// @brief 绘制文字布局
/// @param layout 文字布局 /// @param layout 文字布局
/// @param point 绘制布局的位置 /// @param point 绘制布局的位置
void DrawTextLayout(TextLayoutPtr layout, const Point& point); void DrawTextLayout(TextLayoutPtr layout, const Point& point, BrushPtr outline_brush = nullptr);
/// \~chinese /// \~chinese
/// @brief 清空画布 /// @brief 清空画布
@ -426,18 +426,19 @@ inline void CanvasRenderContext::DrawSpriteFrame(const SpriteFrame& frame, const
this->DrawTexture(frame.GetTexture(), pos, size, &frame.GetCropRect()); this->DrawTexture(frame.GetTexture(), pos, size, &frame.GetCropRect());
} }
inline void CanvasRenderContext::DrawTextLayout(const String& text, const TextStyle& style, const Point& point) inline void CanvasRenderContext::DrawTextLayout(const String& text, const TextStyle& style, const Point& point,
BrushPtr outline_brush)
{ {
TextLayoutPtr layout = MakePtr<TextLayout>(text, style); TextLayoutPtr layout = MakePtr<TextLayout>(text, style);
this->DrawTextLayout(layout, point); this->DrawTextLayout(layout, point, outline_brush);
} }
inline void CanvasRenderContext::DrawTextLayout(TextLayoutPtr layout, const Point& point) inline void CanvasRenderContext::DrawTextLayout(TextLayoutPtr layout, const Point& point, BrushPtr outline_brush)
{ {
KGE_ASSERT(ctx_); KGE_ASSERT(ctx_);
if (layout) if (layout)
{ {
ctx_->DrawTextLayout(*layout, point); ctx_->DrawTextLayout(*layout, point, outline_brush);
} }
} }

View File

@ -57,15 +57,11 @@ DebugActor::DebugActor()
comma_locale_ = std::locale(std::locale(), new comma_numpunct); comma_locale_ = std::locale(std::locale(), new comma_numpunct);
background_brush_ = MakePtr<Brush>(); background_brush_ = MakePtr<Brush>(Color::Rgba(0x000000, 0.7f));
background_brush_->SetColor(Color::Rgba(0x000000, 0.7f)); debug_text_brush_ = MakePtr<Brush>(Color::White);
BrushPtr fill_brush = MakePtr<Brush>();
fill_brush->SetColor(Color::White);
debug_text_style_.font = new Font("Arial", 16.0f, FontWeight::Normal); debug_text_style_.font = new Font("Arial", 16.0f, FontWeight::Normal);
debug_text_style_.line_spacing = 20.f; debug_text_style_.line_spacing = 20.f;
debug_text_style_.fill_brush = fill_brush;
AddListener<MouseHoverEvent>([=](Event*) { SetOpacity(0.4f); }); AddListener<MouseHoverEvent>([=](Event*) { SetOpacity(0.4f); });
AddListener<MouseOutEvent>([=](Event*) { SetOpacity(1.f); }); AddListener<MouseOutEvent>([=](Event*) { SetOpacity(1.f); });
@ -77,7 +73,9 @@ void DebugActor::OnRender(RenderContext& ctx)
{ {
ctx.SetCurrentBrush(background_brush_); ctx.SetCurrentBrush(background_brush_);
ctx.FillRoundedRectangle(GetBounds(), Vec2{ 5.f, 5.f }); ctx.FillRoundedRectangle(GetBounds(), Vec2{ 5.f, 5.f });
ctx.DrawTextLayout(debug_text_, Point(10, 10));
ctx.SetCurrentBrush(debug_text_brush_);
ctx.DrawTextLayout(debug_text_, Point(10, 10), nullptr);
frame_buffer_.PushBack(Time::Now()); frame_buffer_.PushBack(Time::Now());
while (frame_buffer_.Back() - frame_buffer_.Front() >= time::Second) while (frame_buffer_.Back() - frame_buffer_.Front() >= time::Second)

View File

@ -143,6 +143,7 @@ protected:
private: private:
std::locale comma_locale_; std::locale comma_locale_;
BrushPtr background_brush_; BrushPtr background_brush_;
BrushPtr debug_text_brush_;
TextStyle debug_text_style_; TextStyle debug_text_style_;
TextLayout debug_text_; TextLayout debug_text_;

View File

@ -44,7 +44,9 @@ void TextActor::OnRender(RenderContext& ctx)
{ {
if (layout_) if (layout_)
{ {
ctx.DrawTextLayout(*layout_); ctx.SetCurrentBrush(fill_brush_);
ctx.SetCurrentStrokeStyle(outline_stroke_);
ctx.DrawTextLayout(*layout_, Point{}, outline_brush_);
} }
} }
@ -141,39 +143,24 @@ void TextActor::SetAlignment(TextAlign align)
void TextActor::SetFillBrush(BrushPtr brush) void TextActor::SetFillBrush(BrushPtr brush)
{ {
if (style_.fill_brush != brush) fill_brush_ = brush;
{
style_.fill_brush = brush;
if (layout_)
layout_->SetFillBrush(brush);
}
} }
void TextActor::SetOutlineBrush(BrushPtr brush) void TextActor::SetOutlineBrush(BrushPtr brush)
{ {
if (style_.outline_brush != brush) outline_brush_ = brush;
{
style_.outline_brush = brush;
if (layout_)
layout_->SetOutlineBrush(brush);
}
} }
void TextActor::SetOutlineStrokeStyle(StrokeStylePtr stroke) void TextActor::SetOutlineStrokeStyle(StrokeStylePtr stroke)
{ {
if (style_.outline_stroke != stroke) outline_stroke_ = stroke;
{
style_.outline_stroke = stroke;
if (layout_)
layout_->SetOutlineStrokeStyle(stroke);
}
} }
void TextActor::SetFillColor(const Color& color) void TextActor::SetFillColor(const Color& color)
{ {
if (style_.fill_brush) if (fill_brush_)
{ {
style_.fill_brush->SetColor(color); fill_brush_->SetColor(color);
} }
else else
{ {
@ -183,9 +170,9 @@ void TextActor::SetFillColor(const Color& color)
void TextActor::SetOutlineColor(const Color& outline_color) void TextActor::SetOutlineColor(const Color& outline_color)
{ {
if (style_.outline_brush) if (outline_brush_)
{ {
style_.outline_brush->SetColor(outline_color); outline_brush_->SetColor(outline_color);
} }
else else
{ {

View File

@ -163,6 +163,9 @@ private:
String content_; String content_;
TextStyle style_; TextStyle style_;
TextLayoutPtr layout_; TextLayoutPtr layout_;
BrushPtr fill_brush_;
BrushPtr outline_brush_;
StrokeStylePtr outline_stroke_;
}; };
/** @} */ /** @} */
@ -189,17 +192,17 @@ inline TextLayoutPtr TextActor::GetLayout() const
inline BrushPtr TextActor::GetFillBrush() const inline BrushPtr TextActor::GetFillBrush() const
{ {
return style_.fill_brush; return fill_brush_;
} }
inline BrushPtr TextActor::GetOutlineBrush() const inline BrushPtr TextActor::GetOutlineBrush() const
{ {
return style_.outline_brush; return outline_brush_;
} }
inline StrokeStylePtr TextActor::GetOutlineStrokeStyle() const inline StrokeStylePtr TextActor::GetOutlineStrokeStyle() const
{ {
return style_.outline_stroke; return outline_stroke_;
} }
} // namespace kiwano } // namespace kiwano

View File

@ -121,16 +121,16 @@ void RenderContextImpl::DrawTexture(const Texture& texture, const Rect* src_rect
} }
} }
void RenderContextImpl::DrawTextLayout(const TextLayout& layout, const Point& offset) void RenderContextImpl::DrawTextLayout(const TextLayout& layout, const Point& offset, BrushPtr current_outline_brush)
{ {
KGE_ASSERT(text_renderer_ && "Text renderer has not been initialized!"); KGE_ASSERT(text_renderer_ && "Text renderer has not been initialized!");
if (layout.IsValid()) if (layout.IsValid())
{ {
auto native = NativePtr::Get<IDWriteTextLayout>(layout); auto native = NativePtr::Get<IDWriteTextLayout>(layout);
auto fill_brush = NativePtr::Get<ID2D1Brush>(layout.GetFillBrush()); auto fill_brush = NativePtr::Get<ID2D1Brush>(current_brush_);
auto outline_brush = NativePtr::Get<ID2D1Brush>(layout.GetOutlineBrush()); auto outline_brush = NativePtr::Get<ID2D1Brush>(current_outline_brush);
auto outline_stroke = NativePtr::Get<ID2D1StrokeStyle>(layout.GetOutlineStrokeStyle()); auto outline_stroke = NativePtr::Get<ID2D1StrokeStyle>(current_stroke_);
float outline_width = 1.0f; float outline_width = 1.0f;
if (fill_brush) if (fill_brush)
@ -143,9 +143,9 @@ void RenderContextImpl::DrawTextLayout(const TextLayout& layout, const Point& of
outline_brush->SetOpacity(brush_opacity_); outline_brush->SetOpacity(brush_opacity_);
} }
if (layout.GetOutlineStrokeStyle()) if (current_stroke_)
{ {
outline_width = layout.GetOutlineStrokeStyle()->GetWidth(); outline_width = current_stroke_->GetWidth();
} }
HRESULT hr = text_renderer_->DrawTextLayout(native.Get(), offset.x, offset.y, fill_brush.Get(), HRESULT hr = text_renderer_->DrawTextLayout(native.Get(), offset.x, offset.y, fill_brush.Get(),

View File

@ -42,7 +42,7 @@ public:
void DrawTexture(const Texture& texture, const Rect* src_rect, const Rect* dest_rect) override; void DrawTexture(const Texture& texture, const Rect* src_rect, const Rect* dest_rect) override;
void DrawTextLayout(const TextLayout& layout, const Point& offset) override; void DrawTextLayout(const TextLayout& layout, const Point& offset, BrushPtr outline_brush) override;
void DrawShape(const Shape& shape) override; void DrawShape(const Shape& shape) override;

View File

@ -83,7 +83,8 @@ public:
/// @brief 绘制文本布局 /// @brief 绘制文本布局
/// @param layout 文本布局 /// @param layout 文本布局
/// @param offset 偏移量 /// @param offset 偏移量
virtual void DrawTextLayout(const TextLayout& layout, const Point& offset = Point()) = 0; /// @param outline_brush Ãè±ß»­Ë¢
virtual void DrawTextLayout(const TextLayout& layout, const Point& offset, BrushPtr outline_brush) = 0;
/// \~chinese /// \~chinese
/// @brief 绘制形状轮廓 /// @brief 绘制形状轮廓

View File

@ -52,10 +52,6 @@ void TextLayout::Reset(const String& content, const TextStyle& style)
SetWrapWidth(style.wrap_width); SetWrapWidth(style.wrap_width);
SetLineSpacing(style.line_spacing); SetLineSpacing(style.line_spacing);
SetFillBrush(style.fill_brush);
SetOutlineBrush(style.outline_brush);
SetOutlineStrokeStyle(style.outline_stroke);
if (style.show_underline) if (style.show_underline)
SetUnderline(style.show_underline); SetUnderline(style.show_underline);

View File

@ -68,18 +68,6 @@ public:
/// @brief 获取文本行数 /// @brief 获取文本行数
uint32_t GetLineCount() const; uint32_t GetLineCount() const;
/// \~chinese
/// @brief 获取默认填充画刷
BrushPtr GetFillBrush() const;
/// \~chinese
/// @brief 获取默认描边画刷
BrushPtr GetOutlineBrush() const;
/// \~chinese
/// @brief 获取默认描边线条样式
StrokeStylePtr GetOutlineStrokeStyle() const;
/// \~chinese /// \~chinese
/// @brief 设置字体 /// @brief 设置字体
/// @param font 字体 /// @param font 字体
@ -95,21 +83,6 @@ public:
/// @param enable 是否显示删除线 /// @param enable 是否显示删除线
void SetStrikethrough(bool enable); void SetStrikethrough(bool enable);
/// \~chinese
/// @brief 设置文字填充画刷,描边画刷和描边线宽
/// @param brush 画刷
void SetFillBrush(BrushPtr brush);
/// \~chinese
/// @brief 设置文字描边画刷
/// @param brush 画刷
void SetOutlineBrush(BrushPtr brush);
/// \~chinese
/// @brief 设置描边线条样式
/// @param stroke 线条样式
void SetOutlineStrokeStyle(StrokeStylePtr stroke);
/// \~chinese /// \~chinese
/// @brief 设置对齐方式 /// @brief 设置对齐方式
/// @param align 对齐方式 /// @param align 对齐方式
@ -149,9 +122,6 @@ private:
uint32_t line_count_; uint32_t line_count_;
uint32_t content_length_; uint32_t content_length_;
Size size_; Size size_;
BrushPtr fill_brush_;
BrushPtr outline_brush_;
StrokeStylePtr outline_stroke_;
}; };
/** @} */ /** @} */
@ -176,34 +146,4 @@ inline void TextLayout::SetDirtyFlag(TextLayout::DirtyFlag flag)
dirty_flag_ = flag; dirty_flag_ = flag;
} }
inline BrushPtr TextLayout::GetFillBrush() const
{
return fill_brush_;
}
inline BrushPtr TextLayout::GetOutlineBrush() const
{
return outline_brush_;
}
inline StrokeStylePtr TextLayout::GetOutlineStrokeStyle() const
{
return outline_stroke_;
}
inline void TextLayout::SetFillBrush(BrushPtr brush)
{
fill_brush_ = brush;
}
inline void TextLayout::SetOutlineBrush(BrushPtr brush)
{
outline_brush_ = brush;
}
inline void TextLayout::SetOutlineStrokeStyle(StrokeStylePtr stroke)
{
outline_stroke_ = stroke;
}
} // namespace kiwano } // namespace kiwano

View File

@ -52,9 +52,6 @@ class KGE_API TextStyle
public: public:
FontPtr font; ///< 字体 FontPtr font; ///< 字体
TextAlign alignment; ///< 对齐方式 TextAlign alignment; ///< 对齐方式
BrushPtr fill_brush; ///< Ìî³ä»­Ë¢
BrushPtr outline_brush; ///< Ãè±ß»­Ë¢
StrokeStylePtr outline_stroke; ///< Ãè±ßÑùʽ
float wrap_width; ///< 自动换行宽度 float wrap_width; ///< 自动换行宽度
float line_spacing; ///< 行间距 float line_spacing; ///< 行间距
bool show_underline; ///< 显示下划线 bool show_underline; ///< 显示下划线