From ef77e76c6c16859a542a3b6cb315eebd618472e1 Mon Sep 17 00:00:00 2001 From: Nomango Date: Fri, 27 Dec 2019 23:42:51 +0800 Subject: [PATCH] Update Brush & RenderTarget --- src/kiwano-imgui/ImGuiLayer.cpp | 2 +- src/kiwano/2d/Actor.cpp | 29 +++- src/kiwano/2d/Actor.h | 2 +- src/kiwano/2d/Canvas.cpp | 128 +++++++------- src/kiwano/2d/Canvas.h | 103 +++++++++--- src/kiwano/2d/DebugActor.cpp | 12 +- src/kiwano/2d/DebugActor.h | 6 +- src/kiwano/2d/Frame.cpp | 15 -- src/kiwano/2d/Frame.h | 43 +---- src/kiwano/2d/GifSprite.cpp | 35 ++-- src/kiwano/2d/GifSprite.h | 15 -- src/kiwano/2d/Layer.cpp | 53 +----- src/kiwano/2d/Layer.h | 38 +---- src/kiwano/2d/ShapeActor.cpp | 105 ++++-------- src/kiwano/2d/ShapeActor.h | 105 ++++++------ src/kiwano/2d/Sprite.cpp | 38 +---- src/kiwano/2d/Sprite.h | 30 +--- src/kiwano/2d/Stage.cpp | 17 ++ src/kiwano/2d/Stage.h | 44 +++++ src/kiwano/2d/TextActor.cpp | 15 +- src/kiwano/2d/TextActor.h | 40 +++-- src/kiwano/2d/Transition.cpp | 4 +- src/kiwano/platform/Director.cpp | 1 - src/kiwano/renderer/Brush.cpp | 52 ++---- src/kiwano/renderer/Brush.h | 135 +++++++-------- src/kiwano/renderer/Color.h | 91 ++++++---- src/kiwano/renderer/RenderTarget.cpp | 114 +++---------- src/kiwano/renderer/RenderTarget.h | 57 ++++--- src/kiwano/renderer/Renderer.cpp | 6 +- src/kiwano/renderer/TextLayout.h | 33 ++-- src/kiwano/renderer/TextStyle.hpp | 106 +++++++----- src/kiwano/renderer/win32/TextRenderer.cpp | 186 ++++++++------------- src/kiwano/renderer/win32/TextRenderer.h | 12 +- src/kiwano/utils/ResourceCache.cpp | 3 +- 34 files changed, 757 insertions(+), 918 deletions(-) diff --git a/src/kiwano-imgui/ImGuiLayer.cpp b/src/kiwano-imgui/ImGuiLayer.cpp index ad29c3e1..94890048 100644 --- a/src/kiwano-imgui/ImGuiLayer.cpp +++ b/src/kiwano-imgui/ImGuiLayer.cpp @@ -35,7 +35,7 @@ namespace kiwano void ImGuiLayer::OnRender(RenderTarget* rt) { - PrepareRender(rt); + PrepareToRender(rt); for (const auto& pipeline : pipelines_) { pipeline.second(); diff --git a/src/kiwano/2d/Actor.cpp b/src/kiwano/2d/Actor.cpp index c4b03a4a..6d1205df 100644 --- a/src/kiwano/2d/Actor.cpp +++ b/src/kiwano/2d/Actor.cpp @@ -93,7 +93,11 @@ namespace kiwano if (children_.empty()) { - OnRender(rt); + if (CheckVisibilty(rt)) + { + PrepareToRender(rt); + OnRender(rt); + } } else { @@ -108,7 +112,11 @@ namespace kiwano child = child->next_item().get(); } - OnRender(rt); + if (CheckVisibilty(rt)) + { + PrepareToRender(rt); + OnRender(rt); + } while (child) { @@ -118,10 +126,9 @@ namespace kiwano } } - void Actor::PrepareRender(RenderTarget* rt) + void Actor::PrepareToRender(RenderTarget* rt) { rt->SetTransform(transform_matrix_); - rt->SetOpacity(displayed_opacity_); } void Actor::RenderBorder(RenderTarget* rt) @@ -132,10 +139,10 @@ namespace kiwano rt->SetTransform(transform_matrix_); - rt->SetDefaultBrushColor(Color(Color::Red, .4f)); + rt->SetCurrentBrush(GetStage()->GetBorderFillBrush()); rt->FillRectangle(bounds); - rt->SetDefaultBrushColor(Color(Color::Red, .8f)); + rt->SetCurrentBrush(GetStage()->GetBorderStrokeBrush()); rt->DrawRectangle(bounds, 2.f); } @@ -150,7 +157,15 @@ namespace kiwano if (dirty_visibility_) { dirty_visibility_ = false; - visible_in_rt_ = rt->CheckVisibility(GetBounds(), GetTransformMatrix()); + + if (size_.IsOrigin()) + { + visible_in_rt_ = false; + } + else + { + visible_in_rt_ = rt->CheckVisibility(GetBounds(), GetTransformMatrix()); + } } return visible_in_rt_; } diff --git a/src/kiwano/2d/Actor.h b/src/kiwano/2d/Actor.h index 9fcfc9fe..a73473f0 100644 --- a/src/kiwano/2d/Actor.h +++ b/src/kiwano/2d/Actor.h @@ -398,7 +398,7 @@ namespace kiwano virtual void Render(RenderTarget* rt); - virtual void PrepareRender(RenderTarget* rt); + virtual void PrepareToRender(RenderTarget* rt); virtual void RenderBorder(RenderTarget* rt); diff --git a/src/kiwano/2d/Canvas.cpp b/src/kiwano/2d/Canvas.cpp index 545f0929..0f009977 100644 --- a/src/kiwano/2d/Canvas.cpp +++ b/src/kiwano/2d/Canvas.cpp @@ -27,11 +27,8 @@ namespace kiwano Canvas::Canvas() : cache_expired_(false) , stroke_width_(1.0f) - , fill_color_(0, 0, 0) - , stroke_color_(Color::White) , stroke_style_(StrokeStyle::Miter) { - Renderer::instance().CreateTextureRenderTarget(rt_); } Canvas::~Canvas() @@ -40,11 +37,13 @@ namespace kiwano void Canvas::BeginDraw() { + InitRenderTargetAndBrushs(); rt_->BeginDraw(); } void Canvas::EndDraw() { + InitRenderTargetAndBrushs(); rt_->EndDraw(); cache_expired_ = true; } @@ -55,51 +54,17 @@ namespace kiwano if (texture_cached_ && texture_cached_->IsValid()) { - PrepareRender(rt); + PrepareToRender(rt); Rect bitmap_rect(0.f, 0.f, texture_cached_->GetWidth(), texture_cached_->GetHeight()); rt->DrawTexture(*texture_cached_, bitmap_rect, bitmap_rect); } } - void Canvas::SetStrokeColor(Color const& color) + void Canvas::SetBrush(BrushPtr brush) { - stroke_color_ = color; - } - - void Canvas::SetFillColor(Color const& color) - { - fill_color_ = color; - } - - void Canvas::SetStrokeWidth(float width) - { - stroke_width_ = std::max(width, 0.f); - } - - void Canvas::SetStrokeStyle(StrokeStyle stroke_style) - { - stroke_style_ = stroke_style; - } - - void Canvas::SetTextStyle(TextStyle const & text_style) - { - text_style_ = text_style; - } - - void Canvas::SetBrushOpacity(float opacity) - { - rt_->SetOpacity(opacity); - } - - Color Canvas::GetStrokeColor() const - { - return stroke_color_; - } - - Color Canvas::GetFillColor() const - { - return fill_color_; + InitRenderTargetAndBrushs(); + rt_->SetCurrentBrush(brush); } float Canvas::GetStrokeWidth() const @@ -107,44 +72,46 @@ namespace kiwano return stroke_width_; } - float Canvas::GetBrushOpacity() const - { - return rt_->GetOpacity(); - } - void Canvas::SetBrushTransform(Transform const& transform) { + InitRenderTargetAndBrushs(); rt_->SetTransform(transform.ToMatrix()); } void Canvas::SetBrushTransform(Matrix3x2 const & transform) { + InitRenderTargetAndBrushs(); rt_->SetTransform(transform); } void Canvas::PushLayerArea(LayerArea& area) { + InitRenderTargetAndBrushs(); rt_->PushLayer(area); } void Canvas::PopLayerArea() { + InitRenderTargetAndBrushs(); rt_->PopLayer(); } void Canvas::PushClipRect(Rect const& clip_rect) { + InitRenderTargetAndBrushs(); rt_->PushClipRect(clip_rect); } void Canvas::PopClipRect() { + InitRenderTargetAndBrushs(); rt_->PopClipRect(); } void Canvas::DrawLine(Point const& begin, Point const& end) { - rt_->SetDefaultBrushColor(stroke_color_); + InitRenderTargetAndBrushs(); + rt_->SetCurrentBrush(stroke_brush_); rt_->DrawLine( begin, end, @@ -156,7 +123,8 @@ namespace kiwano void Canvas::DrawCircle(Point const& center, float radius) { - rt_->SetDefaultBrushColor(stroke_color_); + InitRenderTargetAndBrushs(); + rt_->SetCurrentBrush(stroke_brush_); rt_->DrawEllipse( center, Vec2(radius, radius), @@ -168,7 +136,8 @@ namespace kiwano void Canvas::DrawEllipse(Point const& center, Vec2 const& radius) { - rt_->SetDefaultBrushColor(stroke_color_); + InitRenderTargetAndBrushs(); + rt_->SetCurrentBrush(stroke_brush_); rt_->DrawEllipse( center, radius, @@ -180,7 +149,8 @@ namespace kiwano void Canvas::DrawRect(Rect const& rect) { - rt_->SetDefaultBrushColor(stroke_color_); + InitRenderTargetAndBrushs(); + rt_->SetCurrentBrush(stroke_brush_); rt_->DrawRectangle( rect, stroke_width_, @@ -191,7 +161,8 @@ namespace kiwano void Canvas::DrawRoundedRect(Rect const& rect, Vec2 const& radius) { - rt_->SetDefaultBrushColor(stroke_color_); + InitRenderTargetAndBrushs(); + rt_->SetCurrentBrush(stroke_brush_); rt_->DrawRoundedRectangle( rect, radius, @@ -203,7 +174,8 @@ namespace kiwano void Canvas::FillCircle(Point const& center, float radius) { - rt_->SetDefaultBrushColor(fill_color_); + InitRenderTargetAndBrushs(); + rt_->SetCurrentBrush(fill_brush_); rt_->FillEllipse( center, Vec2(radius, radius) @@ -213,7 +185,8 @@ namespace kiwano void Canvas::FillEllipse(Point const& center, Vec2 const& radius) { - rt_->SetDefaultBrushColor(fill_color_); + InitRenderTargetAndBrushs(); + rt_->SetCurrentBrush(fill_brush_); rt_->FillEllipse( center, radius @@ -223,7 +196,8 @@ namespace kiwano void Canvas::FillRect(Rect const& rect) { - rt_->SetDefaultBrushColor(fill_color_); + InitRenderTargetAndBrushs(); + rt_->SetCurrentBrush(fill_brush_); rt_->FillRectangle( rect ); @@ -232,7 +206,8 @@ namespace kiwano void Canvas::FillRoundedRect(Rect const& rect, Vec2 const& radius) { - rt_->SetDefaultBrushColor(fill_color_); + InitRenderTargetAndBrushs(); + rt_->SetCurrentBrush(fill_brush_); rt_->FillRoundedRectangle( rect, radius @@ -244,6 +219,7 @@ namespace kiwano { if (texture) { + InitRenderTargetAndBrushs(); rt_->DrawTexture(*texture, src_rect, dest_rect); cache_expired_ = true; } @@ -262,6 +238,7 @@ namespace kiwano void Canvas::DrawTextLayout(TextLayout const& layout, Point const& point) { + InitRenderTargetAndBrushs(); rt_->DrawTextLayout(layout, point); } @@ -297,7 +274,8 @@ namespace kiwano void Canvas::StrokePath() { - rt_->SetDefaultBrushColor(stroke_color_); + InitRenderTargetAndBrushs(); + rt_->SetCurrentBrush(stroke_brush_); rt_->DrawGeometry( geo_sink_.GetGeometry(), stroke_width_, @@ -308,7 +286,8 @@ namespace kiwano void Canvas::FillPath() { - rt_->SetDefaultBrushColor(fill_color_); + InitRenderTargetAndBrushs(); + rt_->SetCurrentBrush(fill_brush_); rt_->FillGeometry( geo_sink_.GetGeometry() ); @@ -317,12 +296,14 @@ namespace kiwano void Canvas::Clear() { + InitRenderTargetAndBrushs(); rt_->Clear(); cache_expired_ = true; } void Canvas::Clear(Color const& clear_color) { + InitRenderTargetAndBrushs(); rt_->Clear(clear_color); cache_expired_ = true; } @@ -333,12 +314,39 @@ namespace kiwano return texture_cached_; } + void Canvas::InitRenderTargetAndBrushs() + { + if (!rt_) + { + Renderer::instance().CreateTextureRenderTarget(rt_); + } + + if (!stroke_brush_) + { + stroke_brush_ = new Brush; + stroke_brush_->SetColor(Color::White); + } + + if (!fill_brush_) + { + fill_brush_ = new Brush; + fill_brush_->SetColor(Color::White); + } + } + void Canvas::UpdateCache() const { - if (cache_expired_) + if (cache_expired_ && rt_) { - texture_cached_ = rt_->GetOutput(); - cache_expired_ = false; + if (!texture_cached_) + { + texture_cached_ = new Texture; + } + + if (rt_->GetOutput(*texture_cached_)) + { + cache_expired_ = false; + } } } diff --git a/src/kiwano/2d/Canvas.h b/src/kiwano/2d/Canvas.h index b7c848d8..a91b740a 100644 --- a/src/kiwano/2d/Canvas.h +++ b/src/kiwano/2d/Canvas.h @@ -183,11 +183,21 @@ namespace kiwano /// @param color 填充颜色 void SetFillColor(Color const& color); + /// \~chinese + /// @brief 设置填充画刷 + /// @param[in] brush 填充画刷 + void SetFillBrush(BrushPtr brush); + /// \~chinese /// @brief 设置轮廓颜色 /// @param color 轮廓颜色 void SetStrokeColor(Color const& color); + /// \~chinese + /// @brief 设置轮廓画刷 + /// @param[in] brush 轮廓画刷 + void SetStrokeBrush(BrushPtr brush); + /// \~chinese /// @brief 设置轮廓宽度 /// @param width 轮廓宽度 @@ -204,17 +214,17 @@ namespace kiwano void SetTextStyle(TextStyle const& text_style); /// \~chinese - /// @brief 设置画笔透明度 - /// @param opacity 透明度,范围 [0.0 - 1.0] - void SetBrushOpacity(float opacity); + /// @brief 设置画刷 + /// @param[in] brush 画刷 + void SetBrush(BrushPtr brush); /// \~chinese - /// @brief 设置画笔二维变换 + /// @brief 设置画刷二维变换 /// @param transform 二维变换 void SetBrushTransform(Transform const& transform); /// \~chinese - /// @brief 设置画笔二维变换矩阵 + /// @brief 设置画刷二维变换矩阵 /// @param transform 二维变换矩阵 void SetBrushTransform(Matrix3x2 const& transform); @@ -236,21 +246,17 @@ namespace kiwano /// @brief 删除最近添加的裁剪区域 void PopClipRect(); - /// \~chinese - /// @brief 获取填充颜色 - Color GetFillColor() const; - - /// \~chinese - /// @brief 获取轮廓颜色 - Color GetStrokeColor() const; - /// \~chinese /// @brief 获取轮廓宽度 float GetStrokeWidth() const; /// \~chinese - /// @brief 获取画笔透明度 - float GetBrushOpacity() const; + /// @brief 获取填充画刷 + BrushPtr GetFillBrush() const; + + /// \~chinese + /// @brief 获取轮廓画刷 + BrushPtr GetStrokeBrush() const; /// \~chinese /// @brief 导出纹理 @@ -259,21 +265,70 @@ namespace kiwano void OnRender(RenderTarget* rt) override; private: + void InitRenderTargetAndBrushs(); + void UpdateCache() const; private: - float stroke_width_; - Color fill_color_; - Color stroke_color_; - TextStyle text_style_; - StrokeStyle stroke_style_; - GeometrySink geo_sink_; - TextureRenderTargetPtr rt_; + float stroke_width_; + TextStyle text_style_; + StrokeStyle stroke_style_; + GeometrySink geo_sink_; + BrushPtr fill_brush_; + BrushPtr stroke_brush_; - mutable bool cache_expired_; - mutable TexturePtr texture_cached_; + mutable bool cache_expired_; + mutable TexturePtr texture_cached_; + mutable TextureRenderTargetPtr rt_; }; /** @} */ + inline void Canvas::SetStrokeWidth(float width) + { + stroke_width_ = std::max(width, 0.f); + } + + inline void Canvas::SetStrokeStyle(StrokeStyle stroke_style) + { + stroke_style_ = stroke_style; + } + + inline void Canvas::SetTextStyle(TextStyle const& text_style) + { + text_style_ = text_style; + } + + inline void Canvas::SetStrokeColor(Color const& color) + { + InitRenderTargetAndBrushs(); + stroke_brush_->SetColor(color); + } + + inline void Canvas::SetFillColor(Color const& color) + { + InitRenderTargetAndBrushs(); + fill_brush_->SetColor(color); + } + + inline void Canvas::SetFillBrush(BrushPtr brush) + { + fill_brush_ = brush; + } + + inline void Canvas::SetStrokeBrush(BrushPtr brush) + { + stroke_brush_ = brush; + } + + inline BrushPtr Canvas::GetFillBrush() const + { + return fill_brush_; + } + + inline BrushPtr Canvas::GetStrokeBrush() const + { + return stroke_brush_; + } + } diff --git a/src/kiwano/2d/DebugActor.cpp b/src/kiwano/2d/DebugActor.cpp index 893dbc1c..7297e468 100644 --- a/src/kiwano/2d/DebugActor.cpp +++ b/src/kiwano/2d/DebugActor.cpp @@ -47,13 +47,14 @@ namespace kiwano } DebugActor::DebugActor() - : background_color_(0.0f, 0.0f, 0.0f, 0.7f) { SetName(L"kiwano-debug-actor"); SetPosition(Point{ 10, 10 }); SetResponsible(true); SetCascadeOpacityEnabled(true); + background_brush_->SetColor(Color(0.0f, 0.0f, 0.0f, 0.7f)); + debug_text_ = new TextActor; debug_text_->SetPosition(Point{ 10, 10 }); this->AddChild(debug_text_); @@ -75,9 +76,7 @@ namespace kiwano void DebugActor::OnRender(RenderTarget* rt) { - PrepareRender(rt); - - rt->SetDefaultBrushColor(background_color_); + rt->SetCurrentBrush(background_brush_); rt->FillRoundedRectangle(GetBounds(), Vec2{ 5.f, 5.f }); } @@ -136,4 +135,9 @@ namespace kiwano } } + bool DebugActor::CheckVisibilty(RenderTarget* rt) const + { + return true; + } + } diff --git a/src/kiwano/2d/DebugActor.h b/src/kiwano/2d/DebugActor.h index b2a4b996..e35f2344 100644 --- a/src/kiwano/2d/DebugActor.h +++ b/src/kiwano/2d/DebugActor.h @@ -22,6 +22,7 @@ #include #include #include +#include namespace kiwano { @@ -46,8 +47,11 @@ namespace kiwano void OnUpdate(Duration dt) override; + protected: + bool CheckVisibilty(RenderTarget* rt) const override; + private: - Color background_color_; + BrushPtr background_brush_; TextActorPtr debug_text_; Vector