diff --git a/src/kiwano/2d/TextActor.cpp b/src/kiwano/2d/TextActor.cpp index c52403f5..999181e0 100644 --- a/src/kiwano/2d/TextActor.cpp +++ b/src/kiwano/2d/TextActor.cpp @@ -49,6 +49,7 @@ namespace kiwano const TextStyle& TextActor::GetDefaultStyle() { + InitDefaultTextStyle(); return text_default_style; } @@ -58,7 +59,7 @@ namespace kiwano } TextActor::TextActor(String const& text) - : TextActor(text, text_default_style) + : TextActor(text, GetDefaultStyle()) { } @@ -66,8 +67,6 @@ namespace kiwano : show_underline_(false) , show_strikethrough_(false) { - InitDefaultTextStyle(); - SetText(text); SetStyle(style); } @@ -91,7 +90,10 @@ namespace kiwano if (text_layout_.IsDirty()) { text_layout_.Update(); + } + if (text_layout_.GetDirtyFlag() & TextLayout::DirtyFlag::Updated) + { if (show_underline_) text_layout_.SetUnderline(true, 0, text_layout_.GetText().length()); @@ -109,7 +111,7 @@ namespace kiwano void TextActor::SetFillColor(Color const& color) { - if (!text_layout_.GetFillBrush()) + if (!text_layout_.GetFillBrush() || text_layout_.GetFillBrush() == GetDefaultStyle().fill_brush) { BrushPtr brush = new Brush; text_layout_.SetFillBrush(brush); @@ -119,11 +121,12 @@ namespace kiwano void TextActor::SetOutlineColor(Color const& outline_color) { - if (!text_layout_.GetOutlineBrush()) + if (!text_layout_.GetOutlineBrush() || text_layout_.GetOutlineBrush() == GetDefaultStyle().outline_brush) { BrushPtr brush = new Brush; text_layout_.SetOutlineBrush(brush); } text_layout_.GetOutlineBrush()->SetColor(outline_color); } + } diff --git a/src/kiwano/renderer/Brush.cpp b/src/kiwano/renderer/Brush.cpp index 8d2f922a..2ed2f65e 100644 --- a/src/kiwano/renderer/Brush.cpp +++ b/src/kiwano/renderer/Brush.cpp @@ -82,25 +82,25 @@ namespace kiwano { if (type_ == Type::SolidColor && raw_) { - auto solid_brush = dynamic_cast(raw_.get()); - KGE_ASSERT(solid_brush != nullptr); + ComPtr solid_brush; - solid_brush->SetColor(DX::ConvertToColorF(color)); - } - else - { - Renderer::instance().CreateSolidBrush(*this, color); + if (SUCCEEDED(raw_->QueryInterface(&solid_brush))) + { + solid_brush->SetColor(DX::ConvertToColorF(color)); + return; + } } + Renderer::instance().CreateSolidBrush(*this, color); } void Brush::SetStyle(LinearGradientStyle const& style) { - Renderer::instance().CreateLinearGradientBrush(*this, style.begin, style.end, style.stops, style.extend_mode); + Renderer::instance().CreateLinearGradientBrush(*this, style); } void Brush::SetStyle(RadialGradientStyle const& style) { - Renderer::instance().CreateRadialGradientBrush(*this, style.center, style.offset, style.radius, style.stops, style.extend_mode); + Renderer::instance().CreateRadialGradientBrush(*this, style); } void Brush::SetBrush(ComPtr brush, Type type) diff --git a/src/kiwano/renderer/Renderer.cpp b/src/kiwano/renderer/Renderer.cpp index f6bc8446..4b81f6ad 100644 --- a/src/kiwano/renderer/Renderer.cpp +++ b/src/kiwano/renderer/Renderer.cpp @@ -884,7 +884,7 @@ namespace kiwano win32::ThrowIfFailed(hr); } - void Renderer::CreateLinearGradientBrush(Brush& brush, Point const& begin, Point const& end, Vector const& stops, GradientExtendMode extend_mode) + void Renderer::CreateLinearGradientBrush(Brush& brush, LinearGradientStyle const& style) { HRESULT hr = S_OK; if (!d2d_res_) @@ -896,10 +896,10 @@ namespace kiwano { ComPtr collection; hr = d2d_res_->GetDeviceContext()->CreateGradientStopCollection( - reinterpret_cast(&stops[0]), - UINT32(stops.size()), + reinterpret_cast(&style.stops[0]), + UINT32(style.stops.size()), D2D1_GAMMA_2_2, - D2D1_EXTEND_MODE(extend_mode), + D2D1_EXTEND_MODE(style.extend_mode), &collection ); @@ -908,8 +908,8 @@ namespace kiwano ComPtr output; hr = d2d_res_->GetDeviceContext()->CreateLinearGradientBrush( D2D1::LinearGradientBrushProperties( - DX::ConvertToPoint2F(begin), - DX::ConvertToPoint2F(end) + DX::ConvertToPoint2F(style.begin), + DX::ConvertToPoint2F(style.end) ), collection.get(), &output @@ -925,8 +925,7 @@ namespace kiwano win32::ThrowIfFailed(hr); } - void Renderer::CreateRadialGradientBrush(Brush& brush, Point const& center, Vec2 const& offset, Vec2 const& radius, - Vector const& stops, GradientExtendMode extend_mode) + void Renderer::CreateRadialGradientBrush(Brush& brush, RadialGradientStyle const& style) { HRESULT hr = S_OK; if (!d2d_res_) @@ -938,10 +937,10 @@ namespace kiwano { ComPtr collection; hr = d2d_res_->GetDeviceContext()->CreateGradientStopCollection( - reinterpret_cast(&stops[0]), - UINT32(stops.size()), + reinterpret_cast(&style.stops[0]), + UINT32(style.stops.size()), D2D1_GAMMA_2_2, - D2D1_EXTEND_MODE(extend_mode), + D2D1_EXTEND_MODE(style.extend_mode), &collection ); @@ -950,10 +949,10 @@ namespace kiwano ComPtr output; hr = d2d_res_->GetDeviceContext()->CreateRadialGradientBrush( D2D1::RadialGradientBrushProperties( - DX::ConvertToPoint2F(center), - DX::ConvertToPoint2F(offset), - radius.x, - radius.y + DX::ConvertToPoint2F(style.center), + DX::ConvertToPoint2F(style.offset), + style.radius.x, + style.radius.y ), collection.get(), &output diff --git a/src/kiwano/renderer/Renderer.h b/src/kiwano/renderer/Renderer.h index c38634b5..c0b27f39 100644 --- a/src/kiwano/renderer/Renderer.h +++ b/src/kiwano/renderer/Renderer.h @@ -243,33 +243,19 @@ namespace kiwano /// \~chinese /// @brief 创建线性渐变画刷 /// @param[out] brush 画刷 - /// @param[in] begin 渐变起始点 - /// @param[in] end 渐变终止点 - /// @param[in] stops 渐变转换点集合 - /// @param[in] extend_mode 渐变扩充模式 + /// @param[in] style 线性渐变样式 void CreateLinearGradientBrush( Brush& brush, - Point const& begin, - Point const& end, - Vector const& stops, - GradientExtendMode extend_mode + LinearGradientStyle const& style ); /// \~chinese /// @brief 创建径向渐变画刷 /// @param[out] brush 画刷 - /// @param[in] center 径向渐变圆心 - /// @param[in] offset 径向渐变偏移 - /// @param[in] radius 径向渐变半径 - /// @param[in] stops 渐变转换点集合 - /// @param[in] extend_mode 渐变扩充模式 + /// @param[in] style 径向渐变样式 void CreateRadialGradientBrush( Brush& brush, - Point const& center, - Vec2 const& offset, - Vec2 const& radius, - Vector const& stops, - GradientExtendMode extend_mode + RadialGradientStyle const& style ); public: diff --git a/src/kiwano/renderer/TextLayout.cpp b/src/kiwano/renderer/TextLayout.cpp index f44a0fd8..125cabad 100644 --- a/src/kiwano/renderer/TextLayout.cpp +++ b/src/kiwano/renderer/TextLayout.cpp @@ -57,7 +57,7 @@ namespace kiwano } } - dirty_flag_ = DirtyFlag::Clean; + dirty_flag_ = DirtyFlag::Updated; } void TextLayout::SetText(const String& text) diff --git a/src/kiwano/renderer/TextLayout.h b/src/kiwano/renderer/TextLayout.h index d75ed5c3..b637f4f0 100644 --- a/src/kiwano/renderer/TextLayout.h +++ b/src/kiwano/renderer/TextLayout.h @@ -151,6 +151,20 @@ namespace kiwano /// @param length 长度 void SetStrikethrough(bool enable, uint32_t start, uint32_t length); + /// \~chinese + /// @brief 脏数据标志 + enum DirtyFlag : uint8_t + { + Clean = 0, ///< 干净数据 + DirtyFormat = 1, ///< 文字格式待更新 + DirtyLayout = 1 << 1, ///< 文字布局待更新 + Updated = 1 << 2, ///< 数据已更新 + }; + + uint8_t GetDirtyFlag() const; + + void SetDirtyFlag(uint8_t flag); + private: ComPtr GetTextFormat() const; @@ -161,12 +175,6 @@ namespace kiwano void SetTextLayout(ComPtr layout); private: - enum DirtyFlag : uint8_t - { - Clean = 0, - DirtyFormat = 1, - DirtyLayout = 1 << 1, - }; uint8_t dirty_flag_; ComPtr text_format_; @@ -198,6 +206,16 @@ namespace kiwano return style_; } + inline uint8_t TextLayout::GetDirtyFlag() const + { + return dirty_flag_; + } + + inline void TextLayout::SetDirtyFlag(uint8_t flag) + { + dirty_flag_ = flag; + } + inline ComPtr TextLayout::GetTextFormat() const { return text_format_;