diff --git a/core/base/Canvas.h b/core/base/Canvas.h index c6b532e7..c82d1657 100644 --- a/core/base/Canvas.h +++ b/core/base/Canvas.h @@ -126,7 +126,7 @@ namespace easy2d float radius_y ); - private: + protected: float stroke_width_; StrokeStyle stroke_; ID2D1RenderTarget* render_target_; diff --git a/core/base/Node.cpp b/core/base/Node.cpp index 8a2f5b44..b5e941a1 100644 --- a/core/base/Node.cpp +++ b/core/base/Node.cpp @@ -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; diff --git a/core/base/Node.h b/core/base/Node.h index 8338f9c8..2554e74f 100644 --- a/core/base/Node.h +++ b/core/base/Node.h @@ -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_; diff --git a/core/base/Sprite.cpp b/core/base/Sprite.cpp index a0b23d4d..d6b5befd 100644 --- a/core/base/Sprite.cpp +++ b/core/base/Sprite.cpp @@ -127,7 +127,7 @@ namespace easy2d return image_; } - void Sprite::OnDraw() const + void Sprite::OnDraw() { if (image_ && image_->GetBitmap()) { diff --git a/core/base/Sprite.h b/core/base/Sprite.h index 1d36d564..8fafce3a 100644 --- a/core/base/Sprite.h +++ b/core/base/Sprite.h @@ -81,9 +81,9 @@ namespace easy2d spImage const& GetImage() const; // 渲染精灵 - virtual void OnDraw() const override; + virtual void OnDraw() override; - private: + protected: spImage image_; }; } diff --git a/core/base/Task.h b/core/base/Task.h index e070e1b8..1f881fe7 100644 --- a/core/base/Task.h +++ b/core/base/Task.h @@ -68,7 +68,7 @@ namespace easy2d void Reset(); - private: + protected: bool running_; bool stopped_; int run_times_; diff --git a/core/base/Text.cpp b/core/base/Text.cpp index 76bb0fae..0cb1bfcc 100644 --- a/core/base/Text.cpp +++ b/core/base/Text.cpp @@ -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( diff --git a/core/base/Text.h b/core/base/Text.h index bee080a4..1f8d4887 100644 --- a/core/base/Text.h +++ b/core/base/Text.h @@ -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_; }; } \ No newline at end of file diff --git a/core/base/intrusive/SmartPointer.hpp b/core/base/intrusive/SmartPointer.hpp index d0b52a4d..fefd098e 100644 --- a/core/base/intrusive/SmartPointer.hpp +++ b/core/base/intrusive/SmartPointer.hpp @@ -177,19 +177,19 @@ namespace easy2d { return static_cast(rhs); } + } - template - inline SmartPointer make_intrusive(T* ptr) - { - return SmartPointer(ptr); - } + template + inline intrusive::SmartPointer make_intrusive(T* ptr) + { + return intrusive::SmartPointer(ptr); + } - // template class cannot support std::swap, - // so implement a swap function in easy2d namespace - template - inline void swap(SmartPointer& lhs, SmartPointer& rhs) - { - lhs.Swap(rhs); - } + // template class cannot specialize std::swap, + // so implement a swap function in easy2d namespace + template + inline void swap(intrusive::SmartPointer& lhs, intrusive::SmartPointer& rhs) + { + lhs.Swap(rhs); } }