From 33f78bb5d58fbb62328dc09b5657eb9d1ddffd36 Mon Sep 17 00:00:00 2001 From: Nomango Date: Tue, 27 Aug 2019 15:29:32 +0800 Subject: [PATCH] minor --- src/kiwano/2d/Actor.cpp | 13 +++ src/kiwano/2d/Actor.h | 10 +- src/kiwano/2d/GifSprite.cpp | 2 +- src/kiwano/2d/ShapeActor.cpp | 16 +-- src/kiwano/2d/Sprite.cpp | 2 +- src/kiwano/2d/Text.cpp | 2 +- src/kiwano/2d/TextStyle.hpp | 21 +--- src/kiwano/core/intrusive_ptr.hpp | 4 +- src/kiwano/math/Matrix.hpp | 5 + src/kiwano/renderer/Geometry.cpp | 25 +++-- src/kiwano/renderer/Geometry.h | 5 +- src/kiwano/renderer/RenderTarget.cpp | 4 +- src/kiwano/renderer/RenderTarget.h | 2 + src/kiwano/renderer/win32/TextRenderer.cpp | 123 +++++++++++---------- 14 files changed, 133 insertions(+), 101 deletions(-) diff --git a/src/kiwano/2d/Actor.cpp b/src/kiwano/2d/Actor.cpp index 681af26f..49b0bd4d 100644 --- a/src/kiwano/2d/Actor.cpp +++ b/src/kiwano/2d/Actor.cpp @@ -39,10 +39,12 @@ namespace kiwano Actor::Actor() : visible_(true) + , visible_in_rt_(true) , update_pausing_(false) , hover_(false) , pressed_(false) , responsible_(false) + , dirty_visibility_(true) , dirty_transform_(false) , dirty_transform_inverse_(false) , cascade_opacity_(false) @@ -139,6 +141,16 @@ namespace kiwano } } + bool Actor::CheckVisibilty(RenderTarget* rt) const + { + if (dirty_visibility_) + { + dirty_visibility_ = false; + visible_in_rt_ = rt->CheckVisibility(GetBounds(), GetTransformMatrix()); + } + return visible_in_rt_; + } + void Actor::Dispatch(Event& evt) { if (!visible_) @@ -224,6 +236,7 @@ namespace kiwano dirty_transform_ = false; dirty_transform_inverse_ = true; + dirty_visibility_ = true; if (is_fast_transform_) { diff --git a/src/kiwano/2d/Actor.h b/src/kiwano/2d/Actor.h index e08037bc..45e13233 100644 --- a/src/kiwano/2d/Actor.h +++ b/src/kiwano/2d/Actor.h @@ -390,6 +390,8 @@ namespace kiwano virtual void RenderBorder(RenderTarget* rt); + virtual bool CheckVisibilty(RenderTarget* rt) const; + void UpdateTransform() const; void UpdateOpacity(); @@ -400,12 +402,12 @@ namespace kiwano protected: bool visible_; - bool hover_; - bool pressed_; - bool responsible_; bool update_pausing_; bool cascade_opacity_; bool show_border_; + bool hover_; + bool pressed_; + bool responsible_; Int32 z_order_; Float32 opacity_; Float32 displayed_opacity_; @@ -419,6 +421,8 @@ namespace kiwano Transform transform_; bool is_fast_transform_; + mutable bool visible_in_rt_; + mutable bool dirty_visibility_; mutable bool dirty_transform_; mutable bool dirty_transform_inverse_; mutable Matrix3x2 transform_matrix_; diff --git a/src/kiwano/2d/GifSprite.cpp b/src/kiwano/2d/GifSprite.cpp index 7918f955..a3a4e1ef 100644 --- a/src/kiwano/2d/GifSprite.cpp +++ b/src/kiwano/2d/GifSprite.cpp @@ -89,7 +89,7 @@ namespace kiwano void GifSprite::OnRender(RenderTarget* rt) { - if (frame_.raw.IsValid() && rt->CheckVisibility(GetBounds(), GetTransformMatrix())) + if (frame_.raw.IsValid() && CheckVisibilty(rt)) { PrepareRender(rt); diff --git a/src/kiwano/2d/ShapeActor.cpp b/src/kiwano/2d/ShapeActor.cpp index 899e4c4c..b01477c0 100644 --- a/src/kiwano/2d/ShapeActor.cpp +++ b/src/kiwano/2d/ShapeActor.cpp @@ -85,7 +85,7 @@ namespace kiwano geo_ = geometry; if (geo_) { - bounds_ = geo_.GetBoundingBox(Matrix3x2()); + bounds_ = geo_.GetBoundingBox(); SetSize(bounds_.GetSize()); } else @@ -97,21 +97,21 @@ namespace kiwano void ShapeActor::OnRender(RenderTarget* rt) { - if (geo_ && rt->CheckVisibility(GetBounds(), GetTransformMatrix())) + if (geo_ && CheckVisibilty(rt)) { PrepareRender(rt); - rt->FillGeometry( - geo_, - fill_color_ - ); - rt->DrawGeometry( geo_, stroke_color_, - stroke_width_, + stroke_width_ * 2, // twice width for widening stroke_style_ ); + + rt->FillGeometry( + geo_, + fill_color_ + ); } } diff --git a/src/kiwano/2d/Sprite.cpp b/src/kiwano/2d/Sprite.cpp index ab331cfe..8e997884 100644 --- a/src/kiwano/2d/Sprite.cpp +++ b/src/kiwano/2d/Sprite.cpp @@ -104,7 +104,7 @@ namespace kiwano void Sprite::OnRender(RenderTarget* rt) { - if (frame_ && rt->CheckVisibility(GetBounds(), GetTransformMatrix())) + if (frame_ && CheckVisibilty(rt)) { PrepareRender(rt); diff --git a/src/kiwano/2d/Text.cpp b/src/kiwano/2d/Text.cpp index 875bfdc2..c8933f8b 100644 --- a/src/kiwano/2d/Text.cpp +++ b/src/kiwano/2d/Text.cpp @@ -204,7 +204,7 @@ namespace kiwano { UpdateLayout(); - if (text_layout_ && rt->CheckVisibility(GetBounds(), GetTransformMatrix())) + if (text_layout_ && CheckVisibilty(rt)) { PrepareRender(rt); rt->DrawTextLayout(text_layout_); diff --git a/src/kiwano/2d/TextStyle.hpp b/src/kiwano/2d/TextStyle.hpp index 023ed954..4f54c982 100644 --- a/src/kiwano/2d/TextStyle.hpp +++ b/src/kiwano/2d/TextStyle.hpp @@ -48,29 +48,16 @@ namespace kiwano StrokeStyle outline_stroke; // 描边线相交样式 public: - TextStyle() - : color(Color::White) - , alignment(TextAlign::Left) - , wrap_width(0.f) - , line_spacing(0.f) - , underline(false) - , strikethrough(false) - , outline(true) - , outline_color(Color(Color::Black, 0.5)) - , outline_width(1.f) - , outline_stroke(StrokeStyle::Round) - {} - TextStyle( - Color color, + Color color = Color::White, TextAlign alignment = TextAlign::Left, Float32 wrap_width = 0.f, - Float32 line_spacing = 0.f, + Float32 line_spacing = 0.f, bool underline = false, bool strikethrough = false, - bool outline = true, + bool outline = false, Color outline_color = Color(Color::Black, 0.5), - Float32 outline_width = 1.f, + Float32 outline_width = 1.f, StrokeStyle outline_stroke = StrokeStyle::Round ) : color(color) diff --git a/src/kiwano/core/intrusive_ptr.hpp b/src/kiwano/core/intrusive_ptr.hpp index 582eda5e..ec988f68 100644 --- a/src/kiwano/core/intrusive_ptr.hpp +++ b/src/kiwano/core/intrusive_ptr.hpp @@ -78,9 +78,9 @@ public: inline pointer_type get() const noexcept { return ptr_; } - inline void reset() noexcept + inline void reset(pointer_type ptr = nullptr) noexcept { - intrusive_ptr{}.swap(*this); + intrusive_ptr{ ptr }.swap(*this); } inline void swap(intrusive_ptr& other) noexcept diff --git a/src/kiwano/math/Matrix.hpp b/src/kiwano/math/Matrix.hpp index 084e7561..f859c91c 100644 --- a/src/kiwano/math/Matrix.hpp +++ b/src/kiwano/math/Matrix.hpp @@ -78,6 +78,9 @@ namespace kiwano { } +#pragma warning (push) +#pragma warning (disable: 26495) // ignore warning "always initialize member variables" + template Matrix3x2T(_MTy const& other) { @@ -85,6 +88,8 @@ namespace kiwano m[i] = other[i]; } +#pragma warning (pop) + inline value_type operator [](UInt32 index) const { return m[index]; diff --git a/src/kiwano/renderer/Geometry.cpp b/src/kiwano/renderer/Geometry.cpp index eba0f2fb..9cac826e 100644 --- a/src/kiwano/renderer/Geometry.cpp +++ b/src/kiwano/renderer/Geometry.cpp @@ -43,15 +43,26 @@ namespace kiwano return geo_ != nullptr; } + Rect Geometry::GetBoundingBox() const + { + Rect bounds; + if (geo_) + { + // no matter it failed or not + geo_->GetBounds(nullptr, DX::ConvertToRectF(&bounds)); + } + return bounds; + } + Rect Geometry::GetBoundingBox(Matrix3x2 const& transform) const { - if (!geo_) - return Rect{}; - - Rect rect; - // no matter it failed or not - geo_->GetBounds(DX::ConvertToMatrix3x2F(transform), DX::ConvertToRectF(&rect)); - return rect; + Rect bounds; + if (geo_) + { + // no matter it failed or not + geo_->GetBounds(DX::ConvertToMatrix3x2F(transform), DX::ConvertToRectF(&bounds)); + } + return bounds; } Float32 Geometry::GetLength() const diff --git a/src/kiwano/renderer/Geometry.h b/src/kiwano/renderer/Geometry.h index d10bd68e..da202ff4 100644 --- a/src/kiwano/renderer/Geometry.h +++ b/src/kiwano/renderer/Geometry.h @@ -44,9 +44,12 @@ namespace kiwano bool IsValid() const; + // 获取外切包围盒 + Rect GetBoundingBox() const; + // 获取外切包围盒 Rect GetBoundingBox( - Matrix3x2 const& transform = Matrix3x2() + Matrix3x2 const& transform ) const; // 判断图形是否包含点 diff --git a/src/kiwano/renderer/RenderTarget.cpp b/src/kiwano/renderer/RenderTarget.cpp index 6bfe1eed..c6441c03 100644 --- a/src/kiwano/renderer/RenderTarget.cpp +++ b/src/kiwano/renderer/RenderTarget.cpp @@ -391,12 +391,12 @@ namespace kiwano layout.GetTextStyle().outline_width, GetStrokeStyle(layout.GetTextStyle().outline_stroke).get() ); + + hr = layout.GetTextLayout()->Draw(nullptr, text_renderer_.get(), offset.x, offset.y); } if (SUCCEEDED(hr)) { - hr = layout.GetTextLayout()->Draw(nullptr, text_renderer_.get(), offset.x, offset.y); - IncreasePrimitivesCount(); } diff --git a/src/kiwano/renderer/RenderTarget.h b/src/kiwano/renderer/RenderTarget.h index 019ea940..bc95876a 100644 --- a/src/kiwano/renderer/RenderTarget.h +++ b/src/kiwano/renderer/RenderTarget.h @@ -190,6 +190,8 @@ namespace kiwano Int32 primitives; Time start; Duration duration; + + Status() : primitives(0) {} }; void SetCollectingStatus(bool collecting); diff --git a/src/kiwano/renderer/win32/TextRenderer.cpp b/src/kiwano/renderer/win32/TextRenderer.cpp index 136138d5..e88d845c 100644 --- a/src/kiwano/renderer/win32/TextRenderer.cpp +++ b/src/kiwano/renderer/win32/TextRenderer.cpp @@ -198,7 +198,7 @@ namespace kiwano sFillColor_ = fillColor; bShowOutline_ = outline; sOutlineColor_ = outlineColor; - fOutlineWidth = 2 * outlineWidth; + fOutlineWidth = outlineWidth; pCurrStrokeStyle_ = outlineJoin; if (pBrush_) pBrush_->SetOpacity(opacity); @@ -220,80 +220,87 @@ namespace kiwano HRESULT hr = S_OK; - ID2D1PathGeometry* pPathGeometry = NULL; - hr = pFactory_->CreatePathGeometry( - &pPathGeometry - ); - - ID2D1GeometrySink* pSink = NULL; - if (SUCCEEDED(hr)) + if (bShowOutline_) { - hr = pPathGeometry->Open( - &pSink + ID2D1GeometrySink* pSink = NULL; + ID2D1PathGeometry* pPathGeometry = NULL; + ID2D1TransformedGeometry* pTransformedGeometry = NULL; + + hr = pFactory_->CreatePathGeometry( + &pPathGeometry ); - } - if (SUCCEEDED(hr)) - { - hr = glyphRun->fontFace->GetGlyphRunOutline( - glyphRun->fontEmSize, - glyphRun->glyphIndices, - glyphRun->glyphAdvances, - glyphRun->glyphOffsets, - glyphRun->glyphCount, - glyphRun->isSideways, - glyphRun->bidiLevel % 2, - pSink - ); - } + if (SUCCEEDED(hr)) + { + hr = pPathGeometry->Open( + &pSink + ); - if (SUCCEEDED(hr)) - { - hr = pSink->Close(); - } + if (SUCCEEDED(hr)) + { + hr = glyphRun->fontFace->GetGlyphRunOutline( + glyphRun->fontEmSize, + glyphRun->glyphIndices, + glyphRun->glyphAdvances, + glyphRun->glyphOffsets, + glyphRun->glyphCount, + glyphRun->isSideways, + glyphRun->bidiLevel % 2, + pSink + ); + } - D2D1::Matrix3x2F const matrix = D2D1::Matrix3x2F( - 1.0f, 0.0f, - 0.0f, 1.0f, - baselineOriginX, baselineOriginY - ); + if (SUCCEEDED(hr)) + { + hr = pSink->Close(); + } - ID2D1TransformedGeometry* pTransformedGeometry = NULL; - if (SUCCEEDED(hr)) - { - hr = pFactory_->CreateTransformedGeometry( - pPathGeometry, - &matrix, - &pTransformedGeometry - ); - } + if (SUCCEEDED(hr)) + { + D2D1::Matrix3x2F const matrix = D2D1::Matrix3x2F( + 1.0f, 0.0f, + 0.0f, 1.0f, + baselineOriginX, baselineOriginY + ); - if (SUCCEEDED(hr) && bShowOutline_) - { - pBrush_->SetColor(sOutlineColor_); + if (SUCCEEDED(hr)) + { + hr = pFactory_->CreateTransformedGeometry( + pPathGeometry, + &matrix, + &pTransformedGeometry + ); + } - pRT_->DrawGeometry( - pTransformedGeometry, - pBrush_, - fOutlineWidth, - pCurrStrokeStyle_ - ); + if (SUCCEEDED(hr)) + { + pBrush_->SetColor(sOutlineColor_); + + pRT_->DrawGeometry( + pTransformedGeometry, + pBrush_, + fOutlineWidth * 2, // twice width for widening + pCurrStrokeStyle_ + ); + } + } + } + + DX::SafeRelease(pPathGeometry); + DX::SafeRelease(pSink); + DX::SafeRelease(pTransformedGeometry); } if (SUCCEEDED(hr)) { pBrush_->SetColor(sFillColor_); - pRT_->FillGeometry( - pTransformedGeometry, + pRT_->DrawGlyphRun( + D2D1::Point2F(baselineOriginX, baselineOriginY), + glyphRun, pBrush_ ); } - - DX::SafeRelease(pPathGeometry); - DX::SafeRelease(pSink); - DX::SafeRelease(pTransformedGeometry); - return hr; }