change: update text size while updating layout

This commit is contained in:
Haibo 2018-11-16 16:16:16 +08:00 committed by Nomango
parent 9221b46e1b
commit 5875f0d2b1
9 changed files with 60 additions and 49 deletions

View File

@ -126,7 +126,7 @@ namespace easy2d
float radius_y
);
private:
protected:
float stroke_width_;
StrokeStyle stroke_;
ID2D1RenderTarget* render_target_;

View File

@ -574,6 +574,11 @@ namespace easy2d
return parent_;
}
Rect Node::GetBounds()
{
return Rect(Point{}, transform_.size);
}
Node::Nodes Node::GetChildren(const String& name) const
{
Nodes children;

View File

@ -56,7 +56,7 @@ namespace easy2d
virtual ~Node();
// 渲染节点
virtual void OnDraw() const {}
virtual void OnDraw() {}
// 更新节点
virtual void OnUpdate(Duration const& dt) {}
@ -124,6 +124,9 @@ namespace easy2d
// 获取父节点
spNode GetParent() const;
// 삿혤관鍋분
virtual Rect GetBounds();
// 设置节点是否显示
void SetVisible(
bool val
@ -358,14 +361,14 @@ namespace easy2d
);
protected:
void DrawBorder();
void Update(Duration const& dt);
virtual void Update(Duration const& dt);
void UpdateTransform();
void UpdateOpacity();
void DrawBorder();
protected:
String name_;
size_t hash_name_;

View File

@ -127,7 +127,7 @@ namespace easy2d
return image_;
}
void Sprite::OnDraw() const
void Sprite::OnDraw()
{
if (image_ && image_->GetBitmap())
{

View File

@ -81,9 +81,9 @@ namespace easy2d
spImage const& GetImage() const;
// äÖȾ¾«Áé
virtual void OnDraw() const override;
virtual void OnDraw() override;
private:
protected:
spImage image_;
};
}

View File

@ -68,7 +68,7 @@ namespace easy2d
void Reset();
private:
protected:
bool running_;
bool stopped_;
int run_times_;

View File

@ -131,7 +131,7 @@ namespace easy2d
return style_.outline_stroke;
}
int Text::GetLineCount() const
int Text::GetLineCount()
{
UpdateLayout();
if (text_layout_)
@ -145,18 +145,10 @@ namespace easy2d
return 0;
}
Rect Text::GetContentBounds() const
Rect Text::GetBounds()
{
UpdateLayout();
if (text_layout_)
{
DWRITE_TEXT_METRICS metrics;
if (SUCCEEDED(text_layout_->GetMetrics(&metrics)))
{
return Rect(0.f, 0.f, metrics.layoutWidth, metrics.height);
}
}
return Rect{};
return Node::GetBounds();
}
bool Text::IsItalic() const
@ -312,10 +304,8 @@ namespace easy2d
style_.outline_stroke = outline_stroke;
}
void Text::OnDraw() const
void Text::OnDraw()
{
UpdateLayout();
if (text_layout_)
{
auto graphics = devices::Graphics::Instance();
@ -331,7 +321,13 @@ namespace easy2d
}
}
void Text::UpdateLayout() const
void Text::Update(Duration const & dt)
{
UpdateLayout();
Node::Update(dt);
}
void Text::UpdateLayout()
{
if (!dirty_layout_)
return;
@ -376,6 +372,10 @@ namespace easy2d
style_.wrap_width
)
);
DWRITE_TEXT_METRICS metrics;
text_layout_->GetMetrics(&metrics);
this->SetSize(metrics.layoutWidth, metrics.height);
}
else
{
@ -390,6 +390,7 @@ namespace easy2d
DWRITE_TEXT_METRICS metrics;
text_layout_->GetMetrics(&metrics);
this->SetSize(metrics.width, metrics.height);
SafeRelease(text_layout_);
ThrowIfFailed(

View File

@ -87,10 +87,10 @@ namespace easy2d
StrokeStyle GetOutlineStroke() const;
// 获取文本显示行数
int GetLineCount() const;
int GetLineCount();
// 삿혤匡俚관鍋분
Rect GetContentBounds() const;
// »ñÈ¡°üΧºÐ
virtual Rect GetBounds() override;
// 是否是斜体
bool IsItalic() const;
@ -205,17 +205,19 @@ namespace easy2d
);
// 渲染文字
virtual void OnDraw() const override;
virtual void OnDraw() override;
private:
void UpdateLayout() const;
protected:
virtual void Update(Duration const& dt) override;
private:
String text_;
Font font_;
TextStyle style_;
mutable bool dirty_layout_;
mutable IDWriteTextFormat* text_format_;
mutable IDWriteTextLayout* text_layout_;
void UpdateLayout();
protected:
String text_;
Font font_;
TextStyle style_;
bool dirty_layout_;
IDWriteTextFormat* text_format_;
IDWriteTextLayout* text_layout_;
};
}

View File

@ -177,19 +177,19 @@ namespace easy2d
{
return static_cast<bool>(rhs);
}
}
template<class T>
inline SmartPointer<T> make_intrusive(T* ptr)
{
return SmartPointer<T>(ptr);
}
template<class T>
inline intrusive::SmartPointer<T> make_intrusive(T* ptr)
{
return intrusive::SmartPointer<T>(ptr);
}
// template class cannot support std::swap,
// so implement a swap function in easy2d namespace
template<class T>
inline void swap(SmartPointer<T>& lhs, SmartPointer<T>& rhs)
{
lhs.Swap(rhs);
}
// template class cannot specialize std::swap,
// so implement a swap function in easy2d namespace
template<class T>
inline void swap(intrusive::SmartPointer<T>& lhs, intrusive::SmartPointer<T>& rhs)
{
lhs.Swap(rhs);
}
}