Update Brush & TextActor

This commit is contained in:
Nomango 2020-01-10 09:30:30 +08:00
parent a3d425b008
commit f9598f3856
6 changed files with 60 additions and 54 deletions

View File

@ -49,6 +49,7 @@ namespace kiwano
const TextStyle& TextActor::GetDefaultStyle() const TextStyle& TextActor::GetDefaultStyle()
{ {
InitDefaultTextStyle();
return text_default_style; return text_default_style;
} }
@ -58,7 +59,7 @@ namespace kiwano
} }
TextActor::TextActor(String const& text) TextActor::TextActor(String const& text)
: TextActor(text, text_default_style) : TextActor(text, GetDefaultStyle())
{ {
} }
@ -66,8 +67,6 @@ namespace kiwano
: show_underline_(false) : show_underline_(false)
, show_strikethrough_(false) , show_strikethrough_(false)
{ {
InitDefaultTextStyle();
SetText(text); SetText(text);
SetStyle(style); SetStyle(style);
} }
@ -91,7 +90,10 @@ namespace kiwano
if (text_layout_.IsDirty()) if (text_layout_.IsDirty())
{ {
text_layout_.Update(); text_layout_.Update();
}
if (text_layout_.GetDirtyFlag() & TextLayout::DirtyFlag::Updated)
{
if (show_underline_) if (show_underline_)
text_layout_.SetUnderline(true, 0, text_layout_.GetText().length()); text_layout_.SetUnderline(true, 0, text_layout_.GetText().length());
@ -109,7 +111,7 @@ namespace kiwano
void TextActor::SetFillColor(Color const& color) void TextActor::SetFillColor(Color const& color)
{ {
if (!text_layout_.GetFillBrush()) if (!text_layout_.GetFillBrush() || text_layout_.GetFillBrush() == GetDefaultStyle().fill_brush)
{ {
BrushPtr brush = new Brush; BrushPtr brush = new Brush;
text_layout_.SetFillBrush(brush); text_layout_.SetFillBrush(brush);
@ -119,11 +121,12 @@ namespace kiwano
void TextActor::SetOutlineColor(Color const& outline_color) 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; BrushPtr brush = new Brush;
text_layout_.SetOutlineBrush(brush); text_layout_.SetOutlineBrush(brush);
} }
text_layout_.GetOutlineBrush()->SetColor(outline_color); text_layout_.GetOutlineBrush()->SetColor(outline_color);
} }
} }

View File

@ -82,25 +82,25 @@ namespace kiwano
{ {
if (type_ == Type::SolidColor && raw_) if (type_ == Type::SolidColor && raw_)
{ {
auto solid_brush = dynamic_cast<ID2D1SolidColorBrush*>(raw_.get()); ComPtr<ID2D1SolidColorBrush> solid_brush;
KGE_ASSERT(solid_brush != nullptr);
solid_brush->SetColor(DX::ConvertToColorF(color)); if (SUCCEEDED(raw_->QueryInterface(&solid_brush)))
}
else
{ {
Renderer::instance().CreateSolidBrush(*this, color); solid_brush->SetColor(DX::ConvertToColorF(color));
return;
} }
} }
Renderer::instance().CreateSolidBrush(*this, color);
}
void Brush::SetStyle(LinearGradientStyle const& style) 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) 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<ID2D1Brush> brush, Type type) void Brush::SetBrush(ComPtr<ID2D1Brush> brush, Type type)

View File

@ -884,7 +884,7 @@ namespace kiwano
win32::ThrowIfFailed(hr); win32::ThrowIfFailed(hr);
} }
void Renderer::CreateLinearGradientBrush(Brush& brush, Point const& begin, Point const& end, Vector<GradientStop> const& stops, GradientExtendMode extend_mode) void Renderer::CreateLinearGradientBrush(Brush& brush, LinearGradientStyle const& style)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
if (!d2d_res_) if (!d2d_res_)
@ -896,10 +896,10 @@ namespace kiwano
{ {
ComPtr<ID2D1GradientStopCollection> collection; ComPtr<ID2D1GradientStopCollection> collection;
hr = d2d_res_->GetDeviceContext()->CreateGradientStopCollection( hr = d2d_res_->GetDeviceContext()->CreateGradientStopCollection(
reinterpret_cast<const D2D1_GRADIENT_STOP*>(&stops[0]), reinterpret_cast<const D2D1_GRADIENT_STOP*>(&style.stops[0]),
UINT32(stops.size()), UINT32(style.stops.size()),
D2D1_GAMMA_2_2, D2D1_GAMMA_2_2,
D2D1_EXTEND_MODE(extend_mode), D2D1_EXTEND_MODE(style.extend_mode),
&collection &collection
); );
@ -908,8 +908,8 @@ namespace kiwano
ComPtr<ID2D1LinearGradientBrush> output; ComPtr<ID2D1LinearGradientBrush> output;
hr = d2d_res_->GetDeviceContext()->CreateLinearGradientBrush( hr = d2d_res_->GetDeviceContext()->CreateLinearGradientBrush(
D2D1::LinearGradientBrushProperties( D2D1::LinearGradientBrushProperties(
DX::ConvertToPoint2F(begin), DX::ConvertToPoint2F(style.begin),
DX::ConvertToPoint2F(end) DX::ConvertToPoint2F(style.end)
), ),
collection.get(), collection.get(),
&output &output
@ -925,8 +925,7 @@ namespace kiwano
win32::ThrowIfFailed(hr); win32::ThrowIfFailed(hr);
} }
void Renderer::CreateRadialGradientBrush(Brush& brush, Point const& center, Vec2 const& offset, Vec2 const& radius, void Renderer::CreateRadialGradientBrush(Brush& brush, RadialGradientStyle const& style)
Vector<GradientStop> const& stops, GradientExtendMode extend_mode)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
if (!d2d_res_) if (!d2d_res_)
@ -938,10 +937,10 @@ namespace kiwano
{ {
ComPtr<ID2D1GradientStopCollection> collection; ComPtr<ID2D1GradientStopCollection> collection;
hr = d2d_res_->GetDeviceContext()->CreateGradientStopCollection( hr = d2d_res_->GetDeviceContext()->CreateGradientStopCollection(
reinterpret_cast<const D2D1_GRADIENT_STOP*>(&stops[0]), reinterpret_cast<const D2D1_GRADIENT_STOP*>(&style.stops[0]),
UINT32(stops.size()), UINT32(style.stops.size()),
D2D1_GAMMA_2_2, D2D1_GAMMA_2_2,
D2D1_EXTEND_MODE(extend_mode), D2D1_EXTEND_MODE(style.extend_mode),
&collection &collection
); );
@ -950,10 +949,10 @@ namespace kiwano
ComPtr<ID2D1RadialGradientBrush> output; ComPtr<ID2D1RadialGradientBrush> output;
hr = d2d_res_->GetDeviceContext()->CreateRadialGradientBrush( hr = d2d_res_->GetDeviceContext()->CreateRadialGradientBrush(
D2D1::RadialGradientBrushProperties( D2D1::RadialGradientBrushProperties(
DX::ConvertToPoint2F(center), DX::ConvertToPoint2F(style.center),
DX::ConvertToPoint2F(offset), DX::ConvertToPoint2F(style.offset),
radius.x, style.radius.x,
radius.y style.radius.y
), ),
collection.get(), collection.get(),
&output &output

View File

@ -243,33 +243,19 @@ namespace kiwano
/// \~chinese /// \~chinese
/// @brief 创建线性渐变画刷 /// @brief 创建线性渐变画刷
/// @param[out] brush 画刷 /// @param[out] brush 画刷
/// @param[in] begin 渐变起始点 /// @param[in] style 线性渐变样式
/// @param[in] end 渐变终止点
/// @param[in] stops 渐变转换点集合
/// @param[in] extend_mode 渐变扩充模式
void CreateLinearGradientBrush( void CreateLinearGradientBrush(
Brush& brush, Brush& brush,
Point const& begin, LinearGradientStyle const& style
Point const& end,
Vector<GradientStop> const& stops,
GradientExtendMode extend_mode
); );
/// \~chinese /// \~chinese
/// @brief 创建径向渐变画刷 /// @brief 创建径向渐变画刷
/// @param[out] brush 画刷 /// @param[out] brush 画刷
/// @param[in] center 径向渐变圆心 /// @param[in] style 径向渐变样式
/// @param[in] offset 径向渐变偏移
/// @param[in] radius 径向渐变半径
/// @param[in] stops 渐变转换点集合
/// @param[in] extend_mode 渐变扩充模式
void CreateRadialGradientBrush( void CreateRadialGradientBrush(
Brush& brush, Brush& brush,
Point const& center, RadialGradientStyle const& style
Vec2 const& offset,
Vec2 const& radius,
Vector<GradientStop> const& stops,
GradientExtendMode extend_mode
); );
public: public:

View File

@ -57,7 +57,7 @@ namespace kiwano
} }
} }
dirty_flag_ = DirtyFlag::Clean; dirty_flag_ = DirtyFlag::Updated;
} }
void TextLayout::SetText(const String& text) void TextLayout::SetText(const String& text)

View File

@ -151,6 +151,20 @@ namespace kiwano
/// @param length ³¤¶È /// @param length ³¤¶È
void SetStrikethrough(bool enable, uint32_t start, uint32_t 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: private:
ComPtr<IDWriteTextFormat> GetTextFormat() const; ComPtr<IDWriteTextFormat> GetTextFormat() const;
@ -161,12 +175,6 @@ namespace kiwano
void SetTextLayout(ComPtr<IDWriteTextLayout> layout); void SetTextLayout(ComPtr<IDWriteTextLayout> layout);
private: private:
enum DirtyFlag : uint8_t
{
Clean = 0,
DirtyFormat = 1,
DirtyLayout = 1 << 1,
};
uint8_t dirty_flag_; uint8_t dirty_flag_;
ComPtr<IDWriteTextFormat> text_format_; ComPtr<IDWriteTextFormat> text_format_;
@ -198,6 +206,16 @@ namespace kiwano
return style_; return style_;
} }
inline uint8_t TextLayout::GetDirtyFlag() const
{
return dirty_flag_;
}
inline void TextLayout::SetDirtyFlag(uint8_t flag)
{
dirty_flag_ = flag;
}
inline ComPtr<IDWriteTextFormat> TextLayout::GetTextFormat() const inline ComPtr<IDWriteTextFormat> TextLayout::GetTextFormat() const
{ {
return text_format_; return text_format_;