diff options
52 files changed, 521 insertions, 491 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index df3e99f9..db155296 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.12) # use 3.12 because VS internal cmake is 3.12 +cmake_minimum_required(VERSION 3.14) project(CruUI) diff --git a/CMakeSettings.json b/CMakeSettings.json index 4304869d..8358bab1 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -5,6 +5,7 @@ "generator": "Visual Studio 16 2019", "inheritEnvironments": [ "msvc_x86_x64" ], "configurationType": "Debug", + "cmakeCommandArgs": "-T host=x64", "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}", "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}" }, @@ -13,22 +14,25 @@ "generator": "Visual Studio 16 2019", "inheritEnvironments": [ "msvc_x86_x64" ], "configurationType": "Release", + "cmakeCommandArgs": "-T host=x64", "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}", "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}" }, { "name": "x64-Debug", "generator": "Visual Studio 16 2019 Win64", - "inheritEnvironments": [ "msvc_x64" ], + "inheritEnvironments": [ "msvc_x64_x64" ], "configurationType": "Debug", + "cmakeCommandArgs": "-T host=x64", "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}", "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}" }, { "name": "x64-Release", "generator": "Visual Studio 16 2019 Win64", - "inheritEnvironments": [ "msvc_x64" ], + "inheritEnvironments": [ "msvc_x64_x64" ], "configurationType": "Release", + "cmakeCommandArgs": "-T host=x64", "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}", "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}" } diff --git a/include/cru/common/auto_delete.hpp b/include/cru/common/auto_delete.hpp new file mode 100644 index 00000000..ae66f7bf --- /dev/null +++ b/include/cru/common/auto_delete.hpp @@ -0,0 +1,14 @@ +#pragma once +#include "base.hpp" + +namespace cru { +// A instance of class implementing this interface is able to +// delete itseft when program exits. Such as IGraphFactory, +// IUiApplication. +struct IAutoDelete : virtual Interface { + // Get whether it will delete itself when program exits. + virtual bool IsAutoDelete() const = 0; + // Set whether it will delete itself when program exits. + virtual void SetAutoDelete(bool value) = 0; +}; +} // namespace cru diff --git a/include/cru/common/endable.hpp b/include/cru/common/endable.hpp new file mode 100644 index 00000000..4459b069 --- /dev/null +++ b/include/cru/common/endable.hpp @@ -0,0 +1,21 @@ +#pragma once +#include "base.hpp" + +namespace cru { +// Although c++ has destructor called automatically. But there is +// occasion when an instance of class needs to end with a result +// and release all internal resources. +// eg. IGeometryBuild will end with building a Geometry and release +// some resources. IPainter will end drawing and release some +// resources and map the drawing result onto target. +// note: You can't call End twice. And most methods on the object +// is invalid to call after End is called. Get whether it is ended +// by IsEnded. +template<typename TResult> +struct IEndable : virtual Interface { + // Get whether the object is ended. + virtual bool IsEnded() const = 0; + // End the object with a result. + virtual TResult End() = 0; +}; +} diff --git a/include/cru/platform/graph/brush.hpp b/include/cru/platform/graph/brush.hpp index 8ffbfc97..7688ec4a 100644 --- a/include/cru/platform/graph/brush.hpp +++ b/include/cru/platform/graph/brush.hpp @@ -4,9 +4,9 @@ #include "cru/common/ui_base.hpp" namespace cru::platform::graph { -struct Brush : public virtual Interface {}; +struct IBrush : public virtual Interface {}; -struct SolidColorBrush : public virtual Brush { +struct ISolidColorBrush : public virtual IBrush { virtual ui::Color GetColor() = 0; virtual void SetColor(const ui::Color& color) = 0; }; diff --git a/include/cru/platform/graph/font.hpp b/include/cru/platform/graph/font.hpp index 5af369e5..f74dc7db 100644 --- a/include/cru/platform/graph/font.hpp +++ b/include/cru/platform/graph/font.hpp @@ -2,7 +2,7 @@ #include "cru/common/base.hpp" namespace cru::platform::graph { -struct FontDescriptor : virtual Interface { +struct IFontDescriptor : virtual Interface { }; } diff --git a/include/cru/platform/graph/geometry.hpp b/include/cru/platform/graph/geometry.hpp index 7a7ad8b2..71bf8c73 100644 --- a/include/cru/platform/graph/geometry.hpp +++ b/include/cru/platform/graph/geometry.hpp @@ -1,25 +1,19 @@ #pragma once #include "cru/common/base.hpp" +#include "cru/common/endable.hpp" #include "cru/common/ui_base.hpp" namespace cru::platform::graph { -struct Geometry : virtual Interface { +struct IGeometry : virtual Interface { virtual bool FillContains(const ui::Point& point) = 0; }; -struct GeometryBuilder : virtual Interface { - virtual bool IsValid() = 0; +struct IGeometryBuilder : virtual Interface, virtual IEndable<IGeometry*> { virtual void BeginFigure(const ui::Point& point) = 0; virtual void LineTo(const ui::Point& point) = 0; virtual void QuadraticBezierTo(const ui::Point& control_point, const ui::Point& end_point) = 0; virtual void CloseFigure(bool close) = 0; - virtual Geometry* Build() = 0; - - Geometry* BuildAndDeleteThis() { - Build(); - delete this; - } }; -} // namespace cru::platform +} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/graph_factory.hpp b/include/cru/platform/graph/graph_factory.hpp index 51cf6f15..b2619e8d 100644 --- a/include/cru/platform/graph/graph_factory.hpp +++ b/include/cru/platform/graph/graph_factory.hpp @@ -1,26 +1,35 @@ #pragma once #include "cru/common/base.hpp" +#include "cru/common/auto_delete.hpp" #include "cru/common/ui_base.hpp" +#include "brush.hpp" +#include "font.hpp" +#include "geometry.hpp" +#include "text_layout.hpp" + #include <memory> #include <string> #include <string_view> namespace cru::platform::graph { -struct SolidColorBrush; -struct GeometryBuilder; -struct FontDescriptor; -struct TextLayout; +// Entry point of the graph module. +struct IGraphFactory : virtual Interface, virtual IAutoDelete { + // Create a platform-specific instance and save it as the global instance. + // Do not create the instance twice. Implements should assert for that. + // After the + // After creating, get the instance by GetInstance. + static IGraphFactory* CreateInstance(); -struct GraphFactory : virtual Interface { - static GraphFactory* GetInstance(); + // Get the global instance. If it is not created, then return nullptr. + static IGraphFactory* GetInstance(); - virtual SolidColorBrush* CreateSolidColorBrush(const ui::Color& color) = 0; - virtual GeometryBuilder* CreateGeometryBuilder() = 0; - virtual FontDescriptor* CreateFontDescriptor( + virtual ISolidColorBrush* CreateSolidColorBrush(const ui::Color& color) = 0; + virtual IGeometryBuilder* CreateGeometryBuilder() = 0; + virtual IFontDescriptor* CreateFontDescriptor( const std::wstring_view& font_family, float font_size) = 0; - virtual TextLayout* CreateTextLayout(std::shared_ptr<FontDescriptor> font, - std::wstring text) = 0; + virtual ITextLayout* CreateTextLayout(std::shared_ptr<IFontDescriptor> font, + std::wstring text) = 0; }; -} // namespace cru::platform +} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/painter.hpp b/include/cru/platform/graph/painter.hpp index 199ebbad..34c895d4 100644 --- a/include/cru/platform/graph/painter.hpp +++ b/include/cru/platform/graph/painter.hpp @@ -1,26 +1,26 @@ #pragma once #include "cru/common/base.hpp" -#include "cru/common/ui_base.hpp" #include "../matrix.hpp" +#include "cru/common/endable.hpp" +#include "cru/common/ui_base.hpp" namespace cru::platform::graph { -struct Brush; -struct Geometry; -struct TextLayout; +struct IBrush; +struct IGeometry; +struct ITextLayout; -struct Painter : virtual Interface { +struct IPainter : virtual Interface, virtual IEndable<void> { virtual Matrix GetTransform() = 0; virtual void SetTransform(const Matrix& matrix) = 0; virtual void Clear(const ui::Color& color) = 0; - virtual void StrokeRectangle(const ui::Rect& rectangle, Brush* brush, + virtual void StrokeRectangle(const ui::Rect& rectangle, IBrush* brush, float width) = 0; - virtual void FillRectangle(const ui::Rect& rectangle, Brush* brush) = 0; - virtual void StrokeGeometry(Geometry* geometry, Brush* brush, + virtual void FillRectangle(const ui::Rect& rectangle, IBrush* brush) = 0; + virtual void StrokeGeometry(IGeometry* geometry, IBrush* brush, float width) = 0; - virtual void FillGeometry(Geometry* geometry, Brush* brush) = 0; - virtual void DrawText(const ui::Point& offset, TextLayout* text_layout, Brush* brush) = 0; - virtual void EndDraw() = 0; - virtual bool IsDisposed() = 0; + virtual void FillGeometry(IGeometry* geometry, IBrush* brush) = 0; + virtual void DrawText(const ui::Point& offset, ITextLayout* text_layout, + IBrush* brush) = 0; }; -} // namespace cru::platform +} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/text_layout.hpp b/include/cru/platform/graph/text_layout.hpp index eb13c115..894c1408 100644 --- a/include/cru/platform/graph/text_layout.hpp +++ b/include/cru/platform/graph/text_layout.hpp @@ -8,13 +8,13 @@ #include <vector> namespace cru::platform::graph { -struct FontDescriptor; +struct IFontDescriptor; -struct TextLayout : virtual Interface { +struct ITextLayout : virtual Interface { virtual std::wstring GetText() = 0; virtual void SetText(std::wstring new_text) = 0; - virtual std::shared_ptr<FontDescriptor> GetFont() = 0; - virtual void SetFont(std::shared_ptr<FontDescriptor> font) = 0; + virtual std::shared_ptr<IFontDescriptor> GetFont() = 0; + virtual void SetFont(std::shared_ptr<IFontDescriptor> font) = 0; virtual void SetMaxWidth(float max_width) = 0; virtual void SetMaxHeight(float max_height) = 0; virtual ui::Rect GetTextBounds() = 0; diff --git a/include/cru/platform/graph/painter_util.hpp b/include/cru/platform/graph/util/painter_util.hpp index c137e8cb..71e125c3 100644 --- a/include/cru/platform/graph/painter_util.hpp +++ b/include/cru/platform/graph/util/painter_util.hpp @@ -1,14 +1,14 @@ #pragma once -#include "painter.hpp" +#include "../painter.hpp" #include <functional> #include <type_traits> namespace cru::platform::graph::util { template <typename Fn> -inline void WithTransform(Painter* painter, const Matrix& matrix, +inline void WithTransform(IPainter* painter, const Matrix& matrix, const Fn& action) { - static_assert(std::is_invocable_v<decltype(action), Painter*>, + static_assert(std::is_invocable_v<decltype(action), IPainter*>, "Action must can be be invoked with painter."); const auto old = painter->GetTransform(); painter->SetTransform(old * matrix); diff --git a/include/cru/platform/native/native_window.hpp b/include/cru/platform/native/native_window.hpp index a82e7a23..0e076348 100644 --- a/include/cru/platform/native/native_window.hpp +++ b/include/cru/platform/native/native_window.hpp @@ -6,7 +6,7 @@ #include "cru/common/ui_base.hpp" namespace cru::platform::graph { -struct Painter; +struct IPainter; } namespace cru::platform::native { @@ -34,7 +34,7 @@ struct NativeWindow : public virtual Interface { // The lefttop of the rect is relative to screen lefttop. virtual void SetWindowRect(const ui::Rect& rect) = 0; - virtual graph::Painter* BeginPaint() = 0; + virtual graph::IPainter* BeginPaint() = 0; virtual Event<>* DestroyEvent() = 0; virtual Event<const ui::Size&>* ResizeEvent() = 0; diff --git a/include/cru/platform/native/ui_applicaition.hpp b/include/cru/platform/native/ui_applicaition.hpp index 17ff703d..0188393e 100644 --- a/include/cru/platform/native/ui_applicaition.hpp +++ b/include/cru/platform/native/ui_applicaition.hpp @@ -5,14 +5,11 @@ #include <functional> #include <vector> -namespace cru::platform::graph { -struct GraphFactory; -} - namespace cru::platform::native { struct NativeWindow; struct UiApplication : public virtual Interface { + static UiApplication* CreateInstance(); static UiApplication* GetInstance(); virtual int Run() = 0; diff --git a/include/cru/ui/event/ui_event.hpp b/include/cru/ui/event/ui_event.hpp index c84116b9..2704cc4f 100644 --- a/include/cru/ui/event/ui_event.hpp +++ b/include/cru/ui/event/ui_event.hpp @@ -8,7 +8,7 @@ #include <optional> namespace cru::platform::graph { -struct Painter; +struct IPainter; } namespace cru::ui { @@ -113,7 +113,7 @@ class MouseWheelEventArgs : public MouseEventArgs { class PaintEventArgs : public UiEventArgs { public: PaintEventArgs(Object* sender, Object* original_sender, - platform::graph::Painter* painter) + platform::graph::IPainter* painter) : UiEventArgs(sender, original_sender), painter_(painter) {} PaintEventArgs(const PaintEventArgs& other) = default; PaintEventArgs(PaintEventArgs&& other) = default; @@ -121,10 +121,10 @@ class PaintEventArgs : public UiEventArgs { PaintEventArgs& operator=(PaintEventArgs&& other) = default; ~PaintEventArgs() = default; - platform::graph::Painter* GetPainter() const { return painter_; } + platform::graph::IPainter* GetPainter() const { return painter_; } private: - platform::graph::Painter* painter_; + platform::graph::IPainter* painter_; }; class FocusChangeEventArgs : public UiEventArgs { diff --git a/include/cru/ui/render/border_render_object.hpp b/include/cru/ui/render/border_render_object.hpp index 44382c63..407edbb3 100644 --- a/include/cru/ui/render/border_render_object.hpp +++ b/include/cru/ui/render/border_render_object.hpp @@ -4,8 +4,8 @@ #include <memory> namespace cru::platform::graph { -struct Brush; -struct Geometry; +struct IBrush; +struct IGeometry; } // namespace cru::platform namespace cru::ui::render { @@ -32,7 +32,7 @@ struct CornerRadius { class BorderRenderObject : public RenderObject { public: - explicit BorderRenderObject(std::shared_ptr<platform::graph::Brush> brush); + explicit BorderRenderObject(std::shared_ptr<platform::graph::IBrush> brush); BorderRenderObject(const BorderRenderObject& other) = delete; BorderRenderObject(BorderRenderObject&& other) = delete; BorderRenderObject& operator=(const BorderRenderObject& other) = delete; @@ -42,8 +42,8 @@ class BorderRenderObject : public RenderObject { bool IsEnabled() const { return is_enabled_; } void SetEnabled(bool enabled) { is_enabled_ = enabled; } - std::shared_ptr<platform::graph::Brush> GetBrush() const { return border_brush_; } - void SetBrush(std::shared_ptr<platform::graph::Brush> new_brush) { + std::shared_ptr<platform::graph::IBrush> GetBrush() const { return border_brush_; } + void SetBrush(std::shared_ptr<platform::graph::IBrush> new_brush) { border_brush_ = std::move(new_brush); } @@ -59,7 +59,7 @@ class BorderRenderObject : public RenderObject { void Refresh() { RecreateGeometry(); } - void Draw(platform::graph::Painter* painter) override; + void Draw(platform::graph::IPainter* painter) override; RenderObject* HitTest(const Point& point) override; @@ -83,11 +83,11 @@ class BorderRenderObject : public RenderObject { private: bool is_enabled_ = false; - std::shared_ptr<platform::graph::Brush> border_brush_ = nullptr; + std::shared_ptr<platform::graph::IBrush> border_brush_ = nullptr; Thickness border_thickness_{}; CornerRadius corner_radius_{}; - std::shared_ptr<platform::graph::Geometry> geometry_ = nullptr; - std::shared_ptr<platform::graph::Geometry> border_outer_geometry_ = nullptr; + std::shared_ptr<platform::graph::IGeometry> geometry_ = nullptr; + std::shared_ptr<platform::graph::IGeometry> border_outer_geometry_ = nullptr; }; } // namespace cru::ui::render diff --git a/include/cru/ui/render/flex_layout_render_object.hpp b/include/cru/ui/render/flex_layout_render_object.hpp index d225e679..99ec1247 100644 --- a/include/cru/ui/render/flex_layout_render_object.hpp +++ b/include/cru/ui/render/flex_layout_render_object.hpp @@ -38,7 +38,7 @@ class FlexLayoutRenderObject : public RenderObject { FlexChildLayoutData* GetChildLayoutData(int position); - void Draw(platform::graph::Painter* painter) override; + void Draw(platform::graph::IPainter* painter) override; RenderObject* HitTest(const Point& point) override; diff --git a/include/cru/ui/render/render_object.hpp b/include/cru/ui/render/render_object.hpp index 7f5f7ac6..b6614317 100644 --- a/include/cru/ui/render/render_object.hpp +++ b/include/cru/ui/render/render_object.hpp @@ -11,7 +11,7 @@ class Control; } namespace cru::platform::graph { -struct Painter; +struct IPainter; } namespace cru::ui::render { @@ -58,7 +58,7 @@ class RenderObject : public Object { void Measure(const Size& available_size); void Layout(const Rect& rect); - virtual void Draw(platform::graph::Painter* painter) = 0; + virtual void Draw(platform::graph::IPainter* painter) = 0; virtual RenderObject* HitTest(const Point& point) = 0; diff --git a/include/cru/ui/render/text_render_object.hpp b/include/cru/ui/render/text_render_object.hpp index 9f03551d..ecad6bb8 100644 --- a/include/cru/ui/render/text_render_object.hpp +++ b/include/cru/ui/render/text_render_object.hpp @@ -6,17 +6,17 @@ // forward declarations namespace cru::platform::graph { -struct Brush; -struct FontDescriptor; -struct TextLayout; +struct IBrush; +struct IFontDescriptor; +struct ITextLayout; } // namespace cru::platform::graph namespace cru::ui::render { class TextRenderObject : public RenderObject { public: - TextRenderObject(std::shared_ptr<platform::graph::Brush> brush, - std::shared_ptr<platform::graph::FontDescriptor> font, - std::shared_ptr<platform::graph::Brush> selection_brush); + TextRenderObject(std::shared_ptr<platform::graph::IBrush> brush, + std::shared_ptr<platform::graph::IFontDescriptor> font, + std::shared_ptr<platform::graph::IBrush> selection_brush); TextRenderObject(const TextRenderObject& other) = delete; TextRenderObject(TextRenderObject&& other) = delete; TextRenderObject& operator=(const TextRenderObject& other) = delete; @@ -26,13 +26,13 @@ class TextRenderObject : public RenderObject { std::wstring GetText() const; void SetText(std::wstring new_text); - std::shared_ptr<platform::graph::Brush> GetBrush() const { return brush_; } - void SetBrush(std::shared_ptr<platform::graph::Brush> new_brush) { + std::shared_ptr<platform::graph::IBrush> GetBrush() const { return brush_; } + void SetBrush(std::shared_ptr<platform::graph::IBrush> new_brush) { new_brush.swap(brush_); } - std::shared_ptr<platform::graph::FontDescriptor> GetFont() const; - void SetFont(std::shared_ptr<platform::graph::FontDescriptor> font); + std::shared_ptr<platform::graph::IFontDescriptor> GetFont() const; + void SetFont(std::shared_ptr<platform::graph::IFontDescriptor> font); std::optional<TextRange> GetSelectionRange() const { return selection_range_; @@ -41,14 +41,14 @@ class TextRenderObject : public RenderObject { selection_range_ = std::move(new_range); } - std::shared_ptr<platform::graph::Brush> GetSelectionBrush() const { + std::shared_ptr<platform::graph::IBrush> GetSelectionBrush() const { return selection_brush_; } - void SetSelectionBrush(std::shared_ptr<platform::graph::Brush> new_brush) { + void SetSelectionBrush(std::shared_ptr<platform::graph::IBrush> new_brush) { new_brush.swap(selection_brush_); } - void Draw(platform::graph::Painter* painter) override; + void Draw(platform::graph::IPainter* painter) override; RenderObject* HitTest(const Point& point) override; @@ -59,11 +59,11 @@ class TextRenderObject : public RenderObject { void OnLayoutContent(const Rect& content_rect) override; private: - std::shared_ptr<platform::graph::Brush> brush_; - std::shared_ptr<platform::graph::FontDescriptor> font_; - std::shared_ptr<platform::graph::TextLayout> text_layout_; + std::shared_ptr<platform::graph::IBrush> brush_; + std::shared_ptr<platform::graph::IFontDescriptor> font_; + std::shared_ptr<platform::graph::ITextLayout> text_layout_; std::optional<TextRange> selection_range_ = std::nullopt; - std::shared_ptr<platform::graph::Brush> selection_brush_; + std::shared_ptr<platform::graph::IBrush> selection_brush_; }; } // namespace cru::ui::render diff --git a/include/cru/ui/render/window_render_object.hpp b/include/cru/ui/render/window_render_object.hpp index dfeae487..b95acbce 100644 --- a/include/cru/ui/render/window_render_object.hpp +++ b/include/cru/ui/render/window_render_object.hpp @@ -17,7 +17,7 @@ class WindowRenderObject : public RenderObject { void MeasureAndLayout(); - void Draw(platform::graph::Painter* painter) override; + void Draw(platform::graph::IPainter* painter) override; RenderObject* HitTest(const Point& point) override; diff --git a/include/cru/ui/ui_manager.hpp b/include/cru/ui/ui_manager.hpp index 5317f579..2dc9cf6b 100644 --- a/include/cru/ui/ui_manager.hpp +++ b/include/cru/ui/ui_manager.hpp @@ -4,8 +4,8 @@ #include <memory> namespace cru::platform::graph { -struct Brush; -struct FontDescriptor; +struct IBrush; +struct IFontDescriptor; } // namespace cru::platform namespace cru::ui { @@ -19,12 +19,12 @@ class PredefineResources : public Object { ~PredefineResources() override = default; // region Button - std::shared_ptr<platform::graph::Brush> button_normal_border_brush; + std::shared_ptr<platform::graph::IBrush> button_normal_border_brush; // region TextBlock - std::shared_ptr<platform::graph::Brush> text_block_selection_brush; - std::shared_ptr<platform::graph::Brush> text_block_text_brush; - std::shared_ptr<platform::graph::FontDescriptor> text_block_font; + std::shared_ptr<platform::graph::IBrush> text_block_selection_brush; + std::shared_ptr<platform::graph::IBrush> text_block_text_brush; + std::shared_ptr<platform::graph::IFontDescriptor> text_block_font; }; class UiManager : public Object { diff --git a/include/cru/win/graph/d2d_painter.hpp b/include/cru/win/graph/d2d_painter.hpp deleted file mode 100644 index d33dced4..00000000 --- a/include/cru/win/graph/d2d_painter.hpp +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once -#include "../win_pre_config.hpp" - -#include "cru/platform/graph/painter.hpp" - -namespace cru::win::graph { -class GraphManager; - -class D2DPainter : public Object, public virtual platform::graph::Painter { - public: - explicit D2DPainter(ID2D1RenderTarget* render_target); - D2DPainter(const D2DPainter& other) = delete; - D2DPainter(D2DPainter&& other) = delete; - D2DPainter& operator=(const D2DPainter& other) = delete; - D2DPainter& operator=(D2DPainter&& other) = delete; - ~D2DPainter() override = default; - - platform::Matrix GetTransform() override; - void SetTransform(const platform::Matrix& matrix) override; - void Clear(const ui::Color& color) override; - void StrokeRectangle(const ui::Rect& rectangle, platform::graph::Brush* brush, - float width) override; - void FillRectangle(const ui::Rect& rectangle, - platform::graph::Brush* brush) override; - void StrokeGeometry(platform::graph::Geometry* geometry, - platform::graph::Brush* brush, float width) override; - void FillGeometry(platform::graph::Geometry* geometry, - platform::graph::Brush* brush) override; - void DrawText(const ui::Point& offset, - platform::graph::TextLayout* text_layout, - platform::graph::Brush* brush) override; - void EndDraw() override final; - bool IsDisposed() override final { return is_disposed_; } - - void EndDrawAndDeleteThis() { - EndDraw(); - delete this; - } - - protected: - virtual void DoEndDraw() = 0; - - private: - ID2D1RenderTarget* render_target_; - - bool is_disposed_ = false; -}; -} // namespace cru::win::graph diff --git a/include/cru/win/graph/graph_manager.hpp b/include/cru/win/graph/graph_manager.hpp deleted file mode 100644 index bf14b802..00000000 --- a/include/cru/win/graph/graph_manager.hpp +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once -#include "../win_pre_config.hpp" - -#include "cru/common/base.hpp" -#include "cru/win/graph/win_graph_factory.hpp" - -#include <memory> - -namespace cru::win::graph { -class GraphManager final : public Object { - public: - static GraphManager* GetInstance(); - - public: - GraphManager(); - GraphManager(const GraphManager& other) = delete; - GraphManager(GraphManager&& other) = delete; - GraphManager& operator=(const GraphManager& other) = delete; - GraphManager& operator=(GraphManager&& other) = delete; - ~GraphManager() override = default; - - public: - ID2D1Factory1* GetD2D1Factory() const { return d2d1_factory_.Get(); } - - ID2D1DeviceContext* GetD2D1DeviceContext() const { - return d2d1_device_context_.Get(); - } - - ID3D11Device* GetD3D11Device() const { return d3d11_device_.Get(); } - - IDXGIFactory2* GetDxgiFactory() const { return dxgi_factory_.Get(); } - - IDWriteFactory* GetDWriteFactory() const { return dwrite_factory_.Get(); } - - IDWriteFontCollection* GetSystemFontCollection() const { - return dwrite_system_font_collection_.Get(); - } - - WinGraphFactory* GetGraphFactory() const { return graph_factory_.get(); } - - private: - Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device_; - Microsoft::WRL::ComPtr<ID2D1Factory1> d2d1_factory_; - Microsoft::WRL::ComPtr<ID2D1DeviceContext> d2d1_device_context_; - Microsoft::WRL::ComPtr<IDXGIFactory2> dxgi_factory_; - Microsoft::WRL::ComPtr<IDWriteFactory> dwrite_factory_; - Microsoft::WRL::ComPtr<IDWriteFontCollection> dwrite_system_font_collection_; - - std::unique_ptr<WinGraphFactory> graph_factory_; -}; -} // namespace cru::win::graph diff --git a/include/cru/win/graph/d2d_util.hpp b/include/cru/win/graph/util/convert_util.hpp index 7a2da91f..4b5368b1 100644 --- a/include/cru/win/graph/d2d_util.hpp +++ b/include/cru/win/graph/util/convert_util.hpp @@ -1,5 +1,5 @@ #pragma once -#include "../win_pre_config.hpp" +#include "../../win_pre_config.hpp" #include "cru/common/ui_base.hpp" #include "cru/platform/matrix.hpp" diff --git a/include/cru/win/graph/win_brush.hpp b/include/cru/win/graph/win_brush.hpp index 69cd952d..c81019c0 100644 --- a/include/cru/win/graph/win_brush.hpp +++ b/include/cru/win/graph/win_brush.hpp @@ -4,18 +4,17 @@ #include "cru/platform/graph/brush.hpp" namespace cru::win::graph { -class GraphManager; +struct IWinNativeFactory; -struct WinBrush : virtual platform::graph::Brush { +struct IWinBrush : virtual platform::graph::IBrush { virtual ID2D1Brush* GetD2DBrush() = 0; }; class WinSolidColorBrush : public Object, - public virtual platform::graph::SolidColorBrush, - public virtual WinBrush { + public virtual platform::graph::ISolidColorBrush, + public virtual IWinBrush { public: - explicit WinSolidColorBrush(GraphManager* graph_manager, - const ui::Color& color); + WinSolidColorBrush(IWinNativeFactory* factory, const ui::Color& color); WinSolidColorBrush(const WinSolidColorBrush& other) = delete; WinSolidColorBrush(WinSolidColorBrush&& other) = delete; WinSolidColorBrush& operator=(const WinSolidColorBrush& other) = delete; diff --git a/include/cru/win/graph/win_font.hpp b/include/cru/win/graph/win_font.hpp index 3301e8da..44fa7edd 100644 --- a/include/cru/win/graph/win_font.hpp +++ b/include/cru/win/graph/win_font.hpp @@ -6,12 +6,12 @@ #include <string_view> namespace cru::win::graph { -class GraphManager; +struct IWinNativeFactory; class WinFontDescriptor : public Object, - public virtual platform::graph::FontDescriptor { + public virtual platform::graph::IFontDescriptor { public: - explicit WinFontDescriptor(GraphManager* graph_manager, + explicit WinFontDescriptor(IWinNativeFactory* factory, const std::wstring_view& font_family, float font_size); WinFontDescriptor(const WinFontDescriptor& other) = delete; diff --git a/include/cru/win/graph/win_geometry.hpp b/include/cru/win/graph/win_geometry.hpp index 005a4384..e312f13c 100644 --- a/include/cru/win/graph/win_geometry.hpp +++ b/include/cru/win/graph/win_geometry.hpp @@ -4,32 +4,32 @@ #include "cru/platform/graph/geometry.hpp" namespace cru::win::graph { -class GraphManager; +struct IWinNativeFactory; class WinGeometryBuilder : public Object, - public virtual platform::graph::GeometryBuilder { + public virtual platform::graph::IGeometryBuilder { public: - explicit WinGeometryBuilder(GraphManager* graph_manger); + explicit WinGeometryBuilder(IWinNativeFactory* factory); WinGeometryBuilder(const WinGeometryBuilder& other) = delete; WinGeometryBuilder(WinGeometryBuilder&& other) = delete; WinGeometryBuilder& operator=(const WinGeometryBuilder& other) = delete; WinGeometryBuilder& operator=(WinGeometryBuilder&& other) = delete; ~WinGeometryBuilder() override; - bool IsValid() override { return geometry_ != nullptr; } void BeginFigure(const ui::Point& point) override; void LineTo(const ui::Point& point) override; void QuadraticBezierTo(const ui::Point& control_point, const ui::Point& end_point) override; void CloseFigure(bool close) override; - platform::graph::Geometry* Build() override; + platform::graph::IGeometry* End() override; + bool IsEnded() const override { return geometry_ != nullptr; } private: Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry_; Microsoft::WRL::ComPtr<ID2D1GeometrySink> geometry_sink_; }; -class WinGeometry : public Object, public virtual platform::graph::Geometry { +class WinGeometry : public Object, public virtual platform::graph::IGeometry { public: explicit WinGeometry(Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry); WinGeometry(const WinGeometry& other) = delete; diff --git a/include/cru/win/graph/win_graph_factory.hpp b/include/cru/win/graph/win_graph_factory.hpp index f8a07819..02f1a0fc 100644 --- a/include/cru/win/graph/win_graph_factory.hpp +++ b/include/cru/win/graph/win_graph_factory.hpp @@ -2,29 +2,60 @@ #include "../win_pre_config.hpp" #include "cru/platform/graph/graph_factory.hpp" +#include "win_native_factory.hpp" namespace cru::win::graph { -class GraphManager; - class WinGraphFactory : public Object, - public virtual platform::graph::GraphFactory { + public virtual platform::graph::IGraphFactory, + public virtual IWinNativeFactory { + friend IGraphFactory* ::cru::platform::graph::IGraphFactory::CreateInstance(); + + public: + static WinGraphFactory* GetInstance(); + + private: + explicit WinGraphFactory(); + public: - explicit WinGraphFactory(GraphManager* graph_manager); WinGraphFactory(const WinGraphFactory& other) = delete; WinGraphFactory(WinGraphFactory&& other) = delete; WinGraphFactory& operator=(const WinGraphFactory& other) = delete; WinGraphFactory& operator=(WinGraphFactory&& other) = delete; - ~WinGraphFactory() override = default; + ~WinGraphFactory() override; + + ID2D1Factory1* GetD2D1Factory() const override { return d2d1_factory_.Get(); } + ID2D1DeviceContext* GetD2D1DeviceContext() const override { + return d2d1_device_context_.Get(); + } + ID3D11Device* GetD3D11Device() const override { return d3d11_device_.Get(); } + IDXGIFactory2* GetDxgiFactory() const override { return dxgi_factory_.Get(); } + IDWriteFactory* GetDWriteFactory() const override { + return dwrite_factory_.Get(); + } + IDWriteFontCollection* GetSystemFontCollection() const override { + return dwrite_system_font_collection_.Get(); + } - platform::graph::SolidColorBrush* CreateSolidColorBrush( + platform::graph::ISolidColorBrush* CreateSolidColorBrush( const ui::Color& color) override; - platform::graph::GeometryBuilder* CreateGeometryBuilder() override; - platform::graph::FontDescriptor* CreateFontDescriptor( + platform::graph::IGeometryBuilder* CreateGeometryBuilder() override; + platform::graph::IFontDescriptor* CreateFontDescriptor( const std::wstring_view& font_family, float font_size); - platform::graph::TextLayout* CreateTextLayout( - std::shared_ptr<platform::graph::FontDescriptor> font, std::wstring text); + platform::graph::ITextLayout* CreateTextLayout( + std::shared_ptr<platform::graph::IFontDescriptor> font, + std::wstring text); + + bool IsAutoDelete() const override { return auto_delete_; } + void SetAutoDelete(bool value) override { auto_delete_ = value; } private: - GraphManager* graph_manager_; + bool auto_delete_ = false; + + Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device_; + Microsoft::WRL::ComPtr<ID2D1Factory1> d2d1_factory_; + Microsoft::WRL::ComPtr<ID2D1DeviceContext> d2d1_device_context_; + Microsoft::WRL::ComPtr<IDXGIFactory2> dxgi_factory_; + Microsoft::WRL::ComPtr<IDWriteFactory> dwrite_factory_; + Microsoft::WRL::ComPtr<IDWriteFontCollection> dwrite_system_font_collection_; }; } // namespace cru::win::graph diff --git a/include/cru/win/graph/win_native_factory.hpp b/include/cru/win/graph/win_native_factory.hpp new file mode 100644 index 00000000..f5ad033c --- /dev/null +++ b/include/cru/win/graph/win_native_factory.hpp @@ -0,0 +1,16 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/common/base.hpp" + +namespace cru::win::graph { +// Interface provides access to root d2d resources. +struct IWinNativeFactory : virtual Interface { + virtual ID2D1Factory1* GetD2D1Factory() const = 0; + virtual ID2D1DeviceContext* GetD2D1DeviceContext() const = 0; + virtual ID3D11Device* GetD3D11Device() const = 0; + virtual IDXGIFactory2* GetDxgiFactory() const = 0; + virtual IDWriteFactory* GetDWriteFactory() const = 0; + virtual IDWriteFontCollection* GetSystemFontCollection() const = 0; +}; +} diff --git a/include/cru/win/graph/win_painter.hpp b/include/cru/win/graph/win_painter.hpp new file mode 100644 index 00000000..f218488c --- /dev/null +++ b/include/cru/win/graph/win_painter.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/platform/graph/painter.hpp" + +namespace cru::win::graph { +class GraphManager; + +class WinPainter : public Object, public virtual platform::graph::IPainter { + public: + explicit WinPainter(ID2D1RenderTarget* render_target); + WinPainter(const WinPainter& other) = delete; + WinPainter(WinPainter&& other) = delete; + WinPainter& operator=(const WinPainter& other) = delete; + WinPainter& operator=(WinPainter&& other) = delete; + ~WinPainter() override = default; + + platform::Matrix GetTransform() override; + void SetTransform(const platform::Matrix& matrix) override; + void Clear(const ui::Color& color) override; + void StrokeRectangle(const ui::Rect& rectangle, platform::graph::IBrush* brush, + float width) override; + void FillRectangle(const ui::Rect& rectangle, + platform::graph::IBrush* brush) override; + void StrokeGeometry(platform::graph::IGeometry* geometry, + platform::graph::IBrush* brush, float width) override; + void FillGeometry(platform::graph::IGeometry* geometry, + platform::graph::IBrush* brush) override; + void DrawText(const ui::Point& offset, + platform::graph::ITextLayout* text_layout, + platform::graph::IBrush* brush) override; + void End() override final; + bool IsEnded() const override final { return is_draw_ended_; } + + protected: + virtual void DoEndDraw() = 0; + + private: + ID2D1RenderTarget* render_target_; + + bool is_draw_ended_ = false; +}; +} // namespace cru::win::graph diff --git a/include/cru/win/graph/win_text_layout.hpp b/include/cru/win/graph/win_text_layout.hpp index b861fda7..7339eff9 100644 --- a/include/cru/win/graph/win_text_layout.hpp +++ b/include/cru/win/graph/win_text_layout.hpp @@ -6,12 +6,12 @@ #include <memory> namespace cru::win::graph { -class GraphManager; +struct IWinNativeFactory; class WinFontDescriptor; -class WinTextLayout : public Object, public virtual platform::graph::TextLayout { +class WinTextLayout : public Object, public virtual platform::graph::ITextLayout { public: - explicit WinTextLayout(GraphManager* graph_manager, + explicit WinTextLayout(IWinNativeFactory* factory, std::shared_ptr<WinFontDescriptor> font, std::wstring text); WinTextLayout(const WinTextLayout& other) = delete; WinTextLayout(WinTextLayout&& other) = delete; @@ -21,8 +21,8 @@ class WinTextLayout : public Object, public virtual platform::graph::TextLayout std::wstring GetText() override; void SetText(std::wstring new_text) override; - std::shared_ptr<platform::graph::FontDescriptor> GetFont(); - void SetFont(std::shared_ptr<platform::graph::FontDescriptor> font); + std::shared_ptr<platform::graph::IFontDescriptor> GetFont(); + void SetFont(std::shared_ptr<platform::graph::IFontDescriptor> font); void SetMaxWidth(float max_width) override; void SetMaxHeight(float max_height) override; ui::Rect GetTextBounds() override; @@ -32,7 +32,7 @@ class WinTextLayout : public Object, public virtual platform::graph::TextLayout IDWriteTextLayout* GetDWriteTextLayout() const { return text_layout_.Get(); } private: - GraphManager* graph_manager_; + IWinNativeFactory* factory_; std::wstring text_; std::shared_ptr<WinFontDescriptor> font_descriptor_; float max_width_ = 0.0f; diff --git a/include/cru/win/native/win_native_window.hpp b/include/cru/win/native/win_native_window.hpp index 2b9be25d..272ca14b 100644 --- a/include/cru/win/native/win_native_window.hpp +++ b/include/cru/win/native/win_native_window.hpp @@ -45,7 +45,7 @@ class WinNativeWindow : public Object, // The lefttop of the rect is relative to screen lefttop. void SetWindowRect(const ui::Rect& rect) override; - platform::graph::Painter* BeginPaint() override; + platform::graph::IPainter* BeginPaint() override; Event<>* DestroyEvent() override { return &destroy_event_; } Event<const ui::Size&>* ResizeEvent() override { return &resize_event_; } diff --git a/include/cru/win/native/window_render_target.hpp b/include/cru/win/native/window_render_target.hpp index c55055c6..5ff8ec87 100644 --- a/include/cru/win/native/window_render_target.hpp +++ b/include/cru/win/native/window_render_target.hpp @@ -4,14 +4,14 @@ #include "cru/common/base.hpp" namespace cru::win::graph { -class GraphManager; +struct IWinNativeFactory; } namespace cru::win::native { // Represents a window render target. class WindowRenderTarget : public Object { public: - WindowRenderTarget(graph::GraphManager* graph_manager, HWND hwnd); + WindowRenderTarget(graph::IWinNativeFactory* factory, HWND hwnd); WindowRenderTarget(const WindowRenderTarget& other) = delete; WindowRenderTarget(WindowRenderTarget&& other) = delete; WindowRenderTarget& operator=(const WindowRenderTarget& other) = delete; @@ -19,8 +19,7 @@ class WindowRenderTarget : public Object { ~WindowRenderTarget() override = default; public: - // Get the graph manager that created the render target. - graph::GraphManager* GetGraphManager() const { return graph_manager_; } + graph::IWinNativeFactory* GetWinNativeFactory() const { return factory_; } // Get the target bitmap which can be set as the ID2D1DeviceContext's target. ID2D1Bitmap1* GetTargetBitmap() const { return target_bitmap_.Get(); } @@ -38,7 +37,7 @@ class WindowRenderTarget : public Object { void CreateTargetBitmap(); private: - graph::GraphManager* graph_manager_; + graph::IWinNativeFactory* factory_; Microsoft::WRL::ComPtr<IDXGISwapChain1> dxgi_swap_chain_; Microsoft::WRL::ComPtr<ID2D1Bitmap1> target_bitmap_; }; diff --git a/src/ui/render/border_render_object.cpp b/src/ui/render/border_render_object.cpp index 541be473..99c2cb4c 100644 --- a/src/ui/render/border_render_object.cpp +++ b/src/ui/render/border_render_object.cpp @@ -3,20 +3,20 @@ #include "cru/platform/debug.hpp" #include "cru/platform/graph/geometry.hpp" #include "cru/platform/graph/graph_factory.hpp" -#include "cru/platform/graph/painter_util.hpp" +#include "cru/platform/graph/util/painter_util.hpp" #include <algorithm> #include <cassert> namespace cru::ui::render { BorderRenderObject::BorderRenderObject( - std::shared_ptr<platform::graph::Brush> brush) { + std::shared_ptr<platform::graph::IBrush> brush) { assert(brush); this->border_brush_ = std::move(brush); RecreateGeometry(); } -void BorderRenderObject::Draw(platform::graph::Painter* painter) { +void BorderRenderObject::Draw(platform::graph::IPainter* painter) { painter->FillGeometry(geometry_.get(), border_brush_.get()); if (const auto child = GetChild()) { auto offset = child->GetOffset(); @@ -154,7 +154,7 @@ void BorderRenderObject::RecreateGeometry() { geometry_.reset(); border_outer_geometry_.reset(); - auto f = [](platform::graph::GeometryBuilder* builder, const Rect& rect, + auto f = [](platform::graph::IGeometryBuilder* builder, const Rect& rect, const CornerRadius& corner) { builder->BeginFigure(Point(rect.left + corner.left_top.x, rect.top)); builder->LineTo(Point(rect.GetRight() - corner.right_top.x, rect.top)); @@ -181,18 +181,18 @@ void BorderRenderObject::RecreateGeometry() { const Rect outer_rect{margin.left, margin.top, size.width - margin.GetHorizontalTotal(), size.height - margin.GetVerticalTotal()}; - const auto graph_factory = platform::graph::GraphFactory::GetInstance(); - std::unique_ptr<platform::graph::GeometryBuilder> builder{ + const auto graph_factory = platform::graph::IGraphFactory::GetInstance(); + std::unique_ptr<platform::graph::IGeometryBuilder> builder{ graph_factory->CreateGeometryBuilder()}; f(builder.get(), outer_rect, corner_radius_); - border_outer_geometry_.reset(builder->Build()); + border_outer_geometry_.reset(builder->End()); builder.reset(); const Rect inner_rect = outer_rect.Shrink(border_thickness_); builder.reset(graph_factory->CreateGeometryBuilder()); f(builder.get(), outer_rect, corner_radius_); f(builder.get(), inner_rect, corner_radius_); - geometry_.reset(builder->Build()); + geometry_.reset(builder->End()); builder.reset(); } } // namespace cru::ui::render diff --git a/src/ui/render/flex_layout_render_object.cpp b/src/ui/render/flex_layout_render_object.cpp index 7528439f..0093f1ad 100644 --- a/src/ui/render/flex_layout_render_object.cpp +++ b/src/ui/render/flex_layout_render_object.cpp @@ -1,7 +1,7 @@ #include "cru/ui/render/flex_layout_render_object.hpp" #include "cru/platform/debug.hpp" -#include "cru/platform/graph/painter_util.hpp" +#include "cru/platform/graph/util/painter_util.hpp" #include <algorithm> #include <cassert> @@ -15,7 +15,7 @@ FlexChildLayoutData* FlexLayoutRenderObject::GetChildLayoutData(int position) { return &child_layout_data_[position]; } -void FlexLayoutRenderObject::Draw(platform::graph::Painter* painter) { +void FlexLayoutRenderObject::Draw(platform::graph::IPainter* painter) { for (const auto child : GetChildren()) { auto offset = child->GetOffset(); platform::graph::util::WithTransform( diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp index c886ee7a..849bff11 100644 --- a/src/ui/render/text_render_object.cpp +++ b/src/ui/render/text_render_object.cpp @@ -1,7 +1,7 @@ #include "cru/ui/render/text_render_object.hpp" #include "cru/platform/graph/graph_factory.hpp" -#include "cru/platform/graph/painter_util.hpp" +#include "cru/platform/graph/util/painter_util.hpp" #include "cru/platform/graph/text_layout.hpp" #include <algorithm> @@ -9,9 +9,9 @@ namespace cru::ui::render { TextRenderObject::TextRenderObject( - std::shared_ptr<platform::graph::Brush> brush, - std::shared_ptr<platform::graph::FontDescriptor> font, - std::shared_ptr<platform::graph::Brush> selection_brush) { + std::shared_ptr<platform::graph::IBrush> brush, + std::shared_ptr<platform::graph::IFontDescriptor> font, + std::shared_ptr<platform::graph::IBrush> selection_brush) { assert(brush); assert(font); assert(selection_brush); @@ -20,8 +20,7 @@ TextRenderObject::TextRenderObject( font.swap(font_); selection_brush.swap(selection_brush_); - const auto graph_factory = platform::graph::GraphFactory::GetInstance(); - + const auto graph_factory = platform::graph::IGraphFactory::GetInstance(); text_layout_.reset(graph_factory->CreateTextLayout(font_, L"")); } @@ -33,22 +32,22 @@ void TextRenderObject::SetText(std::wstring new_text) { text_layout_->SetText(std::move(new_text)); } -std::shared_ptr<platform::graph::FontDescriptor> TextRenderObject::GetFont() +std::shared_ptr<platform::graph::IFontDescriptor> TextRenderObject::GetFont() const { return text_layout_->GetFont(); } void TextRenderObject::SetFont( - std::shared_ptr<platform::graph::FontDescriptor> font) { + std::shared_ptr<platform::graph::IFontDescriptor> font) { text_layout_->SetFont(std::move(font)); } -void TextRenderObject::Draw(platform::graph::Painter* painter) { +void TextRenderObject::Draw(platform::graph::IPainter* painter) { platform::graph::util::WithTransform( painter, platform::Matrix::Translation(GetMargin().left + GetPadding().left, GetMargin().top + GetPadding().top), - [this](platform::graph::Painter* p) { + [this](platform::graph::IPainter* p) { if (this->selection_range_.has_value()) { const auto&& rects = text_layout_->TextRangeRect(this->selection_range_.value()); diff --git a/src/ui/render/window_render_object.cpp b/src/ui/render/window_render_object.cpp index 4fc57ad1..f2e29603 100644 --- a/src/ui/render/window_render_object.cpp +++ b/src/ui/render/window_render_object.cpp @@ -1,6 +1,6 @@ #include "cru/ui/render/window_render_object.hpp" -#include "cru/platform/graph/painter_util.hpp" +#include "cru/platform/graph/util/painter_util.hpp" #include "cru/platform/native/native_window.hpp" #include "cru/ui/window.hpp" @@ -13,13 +13,13 @@ void WindowRenderObject::MeasureAndLayout() { Layout(Rect{Point{}, client_size}); } -void WindowRenderObject::Draw(platform::graph::Painter* painter) { +void WindowRenderObject::Draw(platform::graph::IPainter* painter) { painter->Clear(colors::white); if (const auto child = GetChild()) { auto offset = child->GetOffset(); platform::graph::util::WithTransform( painter, platform::Matrix::Translation(offset.x, offset.y), - [child](auto rt) { child->Draw(rt); }); + [child](platform::graph::IPainter* p) { child->Draw(p); }); } } diff --git a/src/ui/ui_manager.cpp b/src/ui/ui_manager.cpp index 0c3a8f49..82b859c8 100644 --- a/src/ui/ui_manager.cpp +++ b/src/ui/ui_manager.cpp @@ -7,13 +7,13 @@ namespace cru::ui { PredefineResources::PredefineResources() { - const auto graph_factory = platform::graph::GraphFactory::GetInstance(); + const auto graph_factory = platform::graph::IGraphFactory::GetInstance(); - button_normal_border_brush.reset(static_cast<platform::graph::Brush*>( + button_normal_border_brush.reset(static_cast<platform::graph::IBrush*>( graph_factory->CreateSolidColorBrush(colors::black))); - text_block_selection_brush.reset(static_cast<platform::graph::Brush*>( + text_block_selection_brush.reset(static_cast<platform::graph::IBrush*>( graph_factory->CreateSolidColorBrush(colors::skyblue))); - text_block_text_brush.reset(static_cast<platform::graph::Brush*>( + text_block_text_brush.reset(static_cast<platform::graph::IBrush*>( graph_factory->CreateSolidColorBrush(colors::black))); text_block_font.reset(graph_factory->CreateFontDescriptor(L"等线", 24.0f)); } diff --git a/src/ui/window.cpp b/src/ui/window.cpp index 291f6fb6..1c035081 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -178,9 +178,9 @@ void Window::OnNativeDestroy() { delete this; } void Window::OnNativePaint() { const auto painter = - std::unique_ptr<platform::graph::Painter>(native_window_->BeginPaint()); + std::unique_ptr<platform::graph::IPainter>(native_window_->BeginPaint()); render_object_->Draw(painter.get()); - painter->EndDraw(); + painter->End(); } void Window::OnNativeResize(const Size& size) { diff --git a/src/win/graph/CMakeLists.txt b/src/win/graph/CMakeLists.txt index 61749a6a..d326eb2b 100644 --- a/src/win/graph/CMakeLists.txt +++ b/src/win/graph/CMakeLists.txt @@ -1,9 +1,8 @@ add_library(cru_win_graph STATIC - d2d_painter.cpp - graph_manager.cpp win_brush.cpp win_font.cpp win_geometry.cpp win_graph_factory.cpp + win_painter.cpp win_text_layout.cpp) target_link_libraries(cru_win_graph PUBLIC D3D11 D2d1 DWrite cru_win_base) diff --git a/src/win/graph/d2d_painter.cpp b/src/win/graph/d2d_painter.cpp deleted file mode 100644 index 26f6bef0..00000000 --- a/src/win/graph/d2d_painter.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include "cru/win/graph/d2d_painter.hpp" - -#include "cru/win/exception.hpp" -#include "cru/win/graph/d2d_util.hpp" -#include "cru/win/graph/graph_manager.hpp" -#include "cru/win/graph/win_brush.hpp" -#include "cru/win/graph/win_geometry.hpp" -#include "cru/win/graph/win_text_layout.hpp" - -#include <cassert> - -namespace cru::win::graph { -D2DPainter::D2DPainter(ID2D1RenderTarget* render_target) { - assert(render_target); - render_target_ = render_target; -} - -platform::Matrix D2DPainter::GetTransform() { - assert(!IsDisposed()); - D2D1_MATRIX_3X2_F m; - render_target_->GetTransform(&m); - return util::Convert(m); -} - -void D2DPainter::SetTransform(const platform::Matrix& matrix) { - assert(!IsDisposed()); - render_target_->SetTransform(util::Convert(matrix)); -} - -void D2DPainter::Clear(const ui::Color& color) { - assert(!IsDisposed()); - render_target_->Clear(util::Convert(color)); -} - -void D2DPainter::StrokeRectangle(const ui::Rect& rectangle, - platform::graph::Brush* brush, float width) { - assert(!IsDisposed()); - const auto b = dynamic_cast<WinBrush*>(brush); - assert(b); - render_target_->DrawRectangle(util::Convert(rectangle), b->GetD2DBrush(), - width); -} - -void D2DPainter::FillRectangle(const ui::Rect& rectangle, - platform::graph::Brush* brush) { - assert(!IsDisposed()); - const auto b = dynamic_cast<WinBrush*>(brush); - assert(b); - render_target_->FillRectangle(util::Convert(rectangle), b->GetD2DBrush()); -} - -void D2DPainter::StrokeGeometry(platform::graph::Geometry* geometry, - platform::graph::Brush* brush, float width) { - assert(!IsDisposed()); - const auto g = dynamic_cast<WinGeometry*>(geometry); - assert(g); - const auto b = dynamic_cast<WinBrush*>(brush); - assert(b); - - render_target_->DrawGeometry(g->GetNative(), b->GetD2DBrush(), width); -} - -void D2DPainter::FillGeometry(platform::graph::Geometry* geometry, - platform::graph::Brush* brush) { - assert(!IsDisposed()); - const auto g = dynamic_cast<WinGeometry*>(geometry); - assert(g); - const auto b = dynamic_cast<WinBrush*>(brush); - assert(b); - - render_target_->FillGeometry(g->GetNative(), b->GetD2DBrush()); -} - -void D2DPainter::DrawText(const ui::Point& offset, - platform::graph::TextLayout* text_layout, - platform::graph::Brush* brush) { - assert(!IsDisposed()); - const auto t = dynamic_cast<WinTextLayout*>(text_layout); - assert(t); - const auto b = dynamic_cast<WinBrush*>(brush); - assert(b); - - render_target_->DrawTextLayout(util::Convert(offset), - t->GetDWriteTextLayout(), b->GetD2DBrush()); -} - -void D2DPainter::EndDraw() { - if (!is_disposed_) { - is_disposed_ = true; - DoEndDraw(); - } -} -} // namespace cru::win::graph diff --git a/src/win/graph/graph_manager.cpp b/src/win/graph/graph_manager.cpp deleted file mode 100644 index 30e1a14e..00000000 --- a/src/win/graph/graph_manager.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include "cru/win/graph/graph_manager.hpp" - -#include "cru/win/exception.hpp" - -#include <cassert> - -namespace cru::win::graph { -GraphManager* GraphManager::GetInstance() { - static GraphManager* instance = new GraphManager(); - return instance; -} - -GraphManager::GraphManager() { - UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; - -#ifdef CRU_DEBUG - creation_flags |= D3D11_CREATE_DEVICE_DEBUG; -#endif - - const D3D_FEATURE_LEVEL feature_levels[] = { - D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, - D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2, - D3D_FEATURE_LEVEL_9_1}; - - Microsoft::WRL::ComPtr<ID3D11DeviceContext> d3d11_device_context; - - ThrowIfFailed(D3D11CreateDevice( - nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, creation_flags, - feature_levels, ARRAYSIZE(feature_levels), D3D11_SDK_VERSION, - &d3d11_device_, nullptr, &d3d11_device_context)); - - Microsoft::WRL::ComPtr<IDXGIDevice> dxgi_device; - ThrowIfFailed(d3d11_device_->QueryInterface(dxgi_device.GetAddressOf())); - - ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, - IID_PPV_ARGS(&d2d1_factory_))); - - Microsoft::WRL::ComPtr<ID2D1Device> d2d1_device; - - ThrowIfFailed(d2d1_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device)); - - ThrowIfFailed(d2d1_device->CreateDeviceContext( - D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context_)); - - // Identify the physical adapter (GPU or card) this device is runs on. - Microsoft::WRL::ComPtr<IDXGIAdapter> dxgi_adapter; - ThrowIfFailed(dxgi_device->GetAdapter(&dxgi_adapter)); - - // Get the factory object that created the DXGI device. - ThrowIfFailed(dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory_))); - - ThrowIfFailed(DWriteCreateFactory( - DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), - reinterpret_cast<IUnknown**>(dwrite_factory_.GetAddressOf()))); - - ThrowIfFailed(dwrite_factory_->GetSystemFontCollection( - &dwrite_system_font_collection_)); - - graph_factory_.reset(new WinGraphFactory(this)); -} -} // namespace cru::win::graph diff --git a/src/win/graph/win_brush.cpp b/src/win/graph/win_brush.cpp index 262c3302..0c783366 100644 --- a/src/win/graph/win_brush.cpp +++ b/src/win/graph/win_brush.cpp @@ -1,16 +1,16 @@ #include "cru/win/graph/win_brush.hpp" #include "cru/win/exception.hpp" -#include "cru/win/graph/d2d_util.hpp" -#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/graph/win_native_factory.hpp" +#include "cru/win/graph/util/convert_util.hpp" #include <cassert> namespace cru::win::graph { -WinSolidColorBrush::WinSolidColorBrush(GraphManager* graph_manager, +WinSolidColorBrush::WinSolidColorBrush(IWinNativeFactory* factory, const ui::Color& color) { - assert(graph_manager); - ThrowIfFailed(graph_manager->GetD2D1DeviceContext()->CreateSolidColorBrush( + assert(factory); + ThrowIfFailed(factory->GetD2D1DeviceContext()->CreateSolidColorBrush( util::Convert(color), &brush_)); } diff --git a/src/win/graph/win_font.cpp b/src/win/graph/win_font.cpp index 96983d3e..a359d73e 100644 --- a/src/win/graph/win_font.cpp +++ b/src/win/graph/win_font.cpp @@ -1,22 +1,23 @@ #include "cru/win/graph/win_font.hpp" #include "cru/win/exception.hpp" -#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/graph/win_native_factory.hpp" #include <array> #include <cassert> #include <utility> namespace cru::win::graph { -WinFontDescriptor::WinFontDescriptor(GraphManager* graph_manager, +WinFontDescriptor::WinFontDescriptor(IWinNativeFactory* factory, const std::wstring_view& font_family, float font_size) { - assert(graph_manager); + assert(factory); std::array<wchar_t, LOCALE_NAME_MAX_LENGTH> buffer; - if (!::GetUserDefaultLocaleName(buffer.data(), static_cast<int>(buffer.size()))) + if (!::GetUserDefaultLocaleName(buffer.data(), + static_cast<int>(buffer.size()))) throw Win32Error(::GetLastError(), "Failed to get locale."); - ThrowIfFailed(graph_manager->GetDWriteFactory()->CreateTextFormat( + ThrowIfFailed(factory->GetDWriteFactory()->CreateTextFormat( font_family.data(), nullptr, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, font_size, buffer.data(), &text_format_)); diff --git a/src/win/graph/win_geometry.cpp b/src/win/graph/win_geometry.cpp index 22c4b8a7..a725eff6 100644 --- a/src/win/graph/win_geometry.cpp +++ b/src/win/graph/win_geometry.cpp @@ -1,16 +1,15 @@ #include "cru/win/graph/win_geometry.hpp" #include "cru/win/exception.hpp" -#include "cru/win/graph/d2d_util.hpp" -#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/graph/util/convert_util.hpp" +#include "cru/win/graph/win_native_factory.hpp" #include <cassert> namespace cru::win::graph { -WinGeometryBuilder::WinGeometryBuilder(GraphManager* graph_manager) { - assert(graph_manager); - ThrowIfFailed( - graph_manager->GetD2D1Factory()->CreatePathGeometry(&geometry_)); +WinGeometryBuilder::WinGeometryBuilder(IWinNativeFactory* factory) { + assert(factory); + ThrowIfFailed(factory->GetD2D1Factory()->CreatePathGeometry(&geometry_)); ThrowIfFailed(geometry_->Open(&geometry_sink_)); } @@ -21,33 +20,33 @@ WinGeometryBuilder::~WinGeometryBuilder() { } void WinGeometryBuilder::BeginFigure(const ui::Point& point) { - assert(IsValid()); + assert(IsEnded()); geometry_sink_->BeginFigure(util::Convert(point), D2D1_FIGURE_BEGIN_FILLED); } void WinGeometryBuilder::LineTo(const ui::Point& point) { - assert(IsValid()); + assert(IsEnded()); geometry_sink_->AddLine(util::Convert(point)); } void WinGeometryBuilder::QuadraticBezierTo(const ui::Point& control_point, const ui::Point& end_point) { - assert(IsValid()); + assert(IsEnded()); geometry_sink_->AddQuadraticBezier(D2D1::QuadraticBezierSegment( util::Convert(control_point), util::Convert(end_point))); } void WinGeometryBuilder::CloseFigure(bool close) { - assert(IsValid()); + assert(IsEnded()); geometry_sink_->EndFigure(close ? D2D1_FIGURE_END_CLOSED : D2D1_FIGURE_END_OPEN); } -platform::graph::Geometry* WinGeometryBuilder::Build() { - assert(IsValid()); +platform::graph::IGeometry* WinGeometryBuilder::End() { + assert(IsEnded()); ThrowIfFailed(geometry_sink_->Close()); geometry_sink_ = nullptr; - const auto geometry = new WinGeometry(geometry_); + const auto geometry = new WinGeometry(std::move(geometry_)); geometry_ = nullptr; return geometry; } diff --git a/src/win/graph/win_graph_factory.cpp b/src/win/graph/win_graph_factory.cpp index 5ab905b6..de1d28f9 100644 --- a/src/win/graph/win_graph_factory.cpp +++ b/src/win/graph/win_graph_factory.cpp @@ -1,46 +1,111 @@ #include "cru/win/graph/win_graph_factory.hpp" #include "cru/win/exception.hpp" -#include "cru/win/graph/d2d_util.hpp" -#include "cru/win/graph/graph_manager.hpp" #include "cru/win/graph/win_brush.hpp" #include "cru/win/graph/win_font.hpp" #include "cru/win/graph/win_geometry.hpp" #include "cru/win/graph/win_text_layout.hpp" #include <cassert> +#include <cstdlib> #include <utility> +namespace cru::win::graph { +namespace { +WinGraphFactory* instance = nullptr; +} +} // namespace cru::win::graph + namespace cru::platform::graph { -GraphFactory* GraphFactory::GetInstance() { - return win::graph::GraphManager::GetInstance()->GetGraphFactory(); +void GraphFactoryAutoDeleteExitHandler() { + const auto i = ::cru::win::graph::instance; // avoid long namespace prefix + if (i == nullptr) return; + if (i->IsAutoDelete()) delete i; } + +IGraphFactory* IGraphFactory::CreateInstance() { + auto& i = ::cru::win::graph::instance; // avoid long namespace prefix + assert(i == nullptr); + i = new cru::win::graph::WinGraphFactory(); + std::atexit(&GraphFactoryAutoDeleteExitHandler); + return i; } +IGraphFactory* IGraphFactory::GetInstance() { + return ::cru::win::graph::instance; +} +} // namespace cru::platform::graph + namespace cru::win::graph { -WinGraphFactory::WinGraphFactory(GraphManager* graph_manager) { - assert(graph_manager); - graph_manager_ = graph_manager; +WinGraphFactory* WinGraphFactory::GetInstance() { return instance; } + +WinGraphFactory::WinGraphFactory() { + UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; + +#ifdef CRU_DEBUG + creation_flags |= D3D11_CREATE_DEVICE_DEBUG; +#endif + + const D3D_FEATURE_LEVEL feature_levels[] = { + D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, + D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2, + D3D_FEATURE_LEVEL_9_1}; + + Microsoft::WRL::ComPtr<ID3D11DeviceContext> d3d11_device_context; + + ThrowIfFailed(D3D11CreateDevice( + nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, creation_flags, + feature_levels, ARRAYSIZE(feature_levels), D3D11_SDK_VERSION, + &d3d11_device_, nullptr, &d3d11_device_context)); + + Microsoft::WRL::ComPtr<IDXGIDevice> dxgi_device; + ThrowIfFailed(d3d11_device_->QueryInterface(dxgi_device.GetAddressOf())); + + ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, + IID_PPV_ARGS(&d2d1_factory_))); + + Microsoft::WRL::ComPtr<ID2D1Device> d2d1_device; + + ThrowIfFailed(d2d1_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device)); + + ThrowIfFailed(d2d1_device->CreateDeviceContext( + D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context_)); + + // Identify the physical adapter (GPU or card) this device is runs on. + Microsoft::WRL::ComPtr<IDXGIAdapter> dxgi_adapter; + ThrowIfFailed(dxgi_device->GetAdapter(&dxgi_adapter)); + + // Get the factory object that created the DXGI device. + ThrowIfFailed(dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory_))); + + ThrowIfFailed(DWriteCreateFactory( + DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), + reinterpret_cast<IUnknown**>(dwrite_factory_.GetAddressOf()))); + + ThrowIfFailed(dwrite_factory_->GetSystemFontCollection( + &dwrite_system_font_collection_)); } -platform::graph::SolidColorBrush* WinGraphFactory::CreateSolidColorBrush( +WinGraphFactory::~WinGraphFactory() { instance = nullptr; } + +platform::graph::ISolidColorBrush* WinGraphFactory::CreateSolidColorBrush( const ui::Color& color) { - return new WinSolidColorBrush(graph_manager_, color); + return new WinSolidColorBrush(this, color); } -platform::graph::GeometryBuilder* WinGraphFactory::CreateGeometryBuilder() { - return new WinGeometryBuilder(graph_manager_); +platform::graph::IGeometryBuilder* WinGraphFactory::CreateGeometryBuilder() { + return new WinGeometryBuilder(this); } -platform::graph::FontDescriptor* WinGraphFactory::CreateFontDescriptor( +platform::graph::IFontDescriptor* WinGraphFactory::CreateFontDescriptor( const std::wstring_view& font_family, float font_size) { - return new WinFontDescriptor(graph_manager_, font_family, font_size); + return new WinFontDescriptor(this, font_family, font_size); } -platform::graph::TextLayout* WinGraphFactory::CreateTextLayout( - std::shared_ptr<platform::graph::FontDescriptor> font, std::wstring text) { +platform::graph::ITextLayout* WinGraphFactory::CreateTextLayout( + std::shared_ptr<platform::graph::IFontDescriptor> font, std::wstring text) { const auto f = std::dynamic_pointer_cast<WinFontDescriptor>(font); assert(f); - return new WinTextLayout(graph_manager_, std::move(f), std::move(text)); + return new WinTextLayout(this, std::move(f), std::move(text)); } } // namespace cru::win::graph diff --git a/src/win/graph/win_painter.cpp b/src/win/graph/win_painter.cpp new file mode 100644 index 00000000..f75cf4e0 --- /dev/null +++ b/src/win/graph/win_painter.cpp @@ -0,0 +1,93 @@ +#include "cru/win/graph/win_painter.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/win_native_factory.hpp" +#include "cru/win/graph/util/convert_util.hpp" +#include "cru/win/graph/win_brush.hpp" +#include "cru/win/graph/win_geometry.hpp" +#include "cru/win/graph/win_text_layout.hpp" + +#include <cassert> + +namespace cru::win::graph { +WinPainter::WinPainter(ID2D1RenderTarget* render_target) { + assert(render_target); + render_target_ = render_target; +} + +platform::Matrix WinPainter::GetTransform() { + assert(!IsEnded()); + D2D1_MATRIX_3X2_F m; + render_target_->GetTransform(&m); + return util::Convert(m); +} + +void WinPainter::SetTransform(const platform::Matrix& matrix) { + assert(!IsEnded()); + render_target_->SetTransform(util::Convert(matrix)); +} + +void WinPainter::Clear(const ui::Color& color) { + assert(!IsEnded()); + render_target_->Clear(util::Convert(color)); +} + +void WinPainter::StrokeRectangle(const ui::Rect& rectangle, + platform::graph::IBrush* brush, float width) { + assert(!IsEnded()); + const auto b = dynamic_cast<IWinBrush*>(brush); + assert(b); + render_target_->DrawRectangle(util::Convert(rectangle), b->GetD2DBrush(), + width); +} + +void WinPainter::FillRectangle(const ui::Rect& rectangle, + platform::graph::IBrush* brush) { + assert(!IsEnded()); + const auto b = dynamic_cast<IWinBrush*>(brush); + assert(b); + render_target_->FillRectangle(util::Convert(rectangle), b->GetD2DBrush()); +} + +void WinPainter::StrokeGeometry(platform::graph::IGeometry* geometry, + platform::graph::IBrush* brush, float width) { + assert(!IsEnded()); + const auto g = dynamic_cast<WinGeometry*>(geometry); + assert(g); + const auto b = dynamic_cast<IWinBrush*>(brush); + assert(b); + + render_target_->DrawGeometry(g->GetNative(), b->GetD2DBrush(), width); +} + +void WinPainter::FillGeometry(platform::graph::IGeometry* geometry, + platform::graph::IBrush* brush) { + assert(!IsEnded()); + const auto g = dynamic_cast<WinGeometry*>(geometry); + assert(g); + const auto b = dynamic_cast<IWinBrush*>(brush); + assert(b); + + render_target_->FillGeometry(g->GetNative(), b->GetD2DBrush()); +} + +void WinPainter::DrawText(const ui::Point& offset, + platform::graph::ITextLayout* text_layout, + platform::graph::IBrush* brush) { + assert(!IsEnded()); + const auto t = dynamic_cast<WinTextLayout*>(text_layout); + assert(t); + const auto b = dynamic_cast<IWinBrush*>(brush); + assert(b); + + render_target_->DrawTextLayout(util::Convert(offset), + t->GetDWriteTextLayout(), b->GetD2DBrush()); +} + +void WinPainter::End() { + if (!is_draw_ended_) { + is_draw_ended_ = true; + DoEndDraw(); + } +} +} // namespace cru::win::graph diff --git a/src/win/graph/win_text_layout.cpp b/src/win/graph/win_text_layout.cpp index 0506320a..997309ad 100644 --- a/src/win/graph/win_text_layout.cpp +++ b/src/win/graph/win_text_layout.cpp @@ -1,23 +1,23 @@ #include "cru/win/graph/win_text_layout.hpp" #include "cru/win/exception.hpp" -#include "cru/win/graph/graph_manager.hpp" #include "cru/win/graph/win_font.hpp" +#include "cru/win/graph/win_native_factory.hpp" #include <cassert> #include <utility> namespace cru::win::graph { -WinTextLayout::WinTextLayout(GraphManager* graph_manager, +WinTextLayout::WinTextLayout(IWinNativeFactory* factory, std::shared_ptr<WinFontDescriptor> font, std::wstring text) { - assert(graph_manager); + assert(factory); assert(font); - graph_manager_ = graph_manager; + factory_ = factory; text_.swap(text); font_descriptor_.swap(font); - ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout( + ThrowIfFailed(factory->GetDWriteFactory()->CreateTextLayout( text_.c_str(), static_cast<UINT32>(text_.size()), font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, &text_layout_)); @@ -27,22 +27,22 @@ std::wstring WinTextLayout::GetText() { return text_; } void WinTextLayout::SetText(std::wstring new_text) { text_.swap(new_text); - ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout( + ThrowIfFailed(factory_->GetDWriteFactory()->CreateTextLayout( text_.c_str(), static_cast<UINT32>(text_.size()), font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, &text_layout_)); } -std::shared_ptr<platform::graph::FontDescriptor> WinTextLayout::GetFont() { +std::shared_ptr<platform::graph::IFontDescriptor> WinTextLayout::GetFont() { return font_descriptor_; } void WinTextLayout::SetFont( - std::shared_ptr<platform::graph::FontDescriptor> font) { + std::shared_ptr<platform::graph::IFontDescriptor> font) { auto f = std::dynamic_pointer_cast<WinFontDescriptor>(font); assert(f); f.swap(font_descriptor_); - ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout( + ThrowIfFailed(factory_->GetDWriteFactory()->CreateTextLayout( text_.c_str(), static_cast<UINT32>(text_.size()), font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, &text_layout_)); diff --git a/src/win/native/win_application.cpp b/src/win/native/win_application.cpp index 6c39453f..00a16ff1 100644 --- a/src/win/native/win_application.cpp +++ b/src/win/native/win_application.cpp @@ -1,7 +1,7 @@ #include "cru/win/native/win_application.hpp" #include "cru/win/exception.hpp" -#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/graph/win_graph_factory.hpp" #include "cru/win/native/god_window.hpp" #include "cru/win/native/win_native_window.hpp" #include "god_window_message.hpp" @@ -38,13 +38,14 @@ WinApplication::WinApplication(HINSTANCE h_instance) : h_instance_(h_instance) { if (!::IsWindows8OrGreater()) throw std::runtime_error("Must run on Windows 8 or later."); + graph::WinGraphFactory::CreateInstance(); + god_window_ = std::make_shared<GodWindow>(this); timer_manager_ = std::make_shared<TimerManager>(god_window_.get()); window_manager_ = std::make_shared<WindowManager>(this); } -WinApplication::~WinApplication() { - instance = nullptr; } +WinApplication::~WinApplication() { instance = nullptr; } int WinApplication::Run() { MSG msg; @@ -55,7 +56,7 @@ int WinApplication::Run() { for (const auto& handler : quit_handlers_) handler(); - delete graph::GraphManager::GetInstance(); + delete graph::WinGraphFactory::GetInstance(); delete this; return static_cast<int>(msg.wParam); diff --git a/src/win/native/win_native_window.cpp b/src/win/native/win_native_window.cpp index 1d3b15ba..a2f23a2c 100644 --- a/src/win/native/win_native_window.cpp +++ b/src/win/native/win_native_window.cpp @@ -1,7 +1,7 @@ #include "cru/win/native/win_native_window.hpp" #include "cru/win/exception.hpp" -#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/graph/win_graph_factory.hpp" #include "cru/win/native/win_application.hpp" #include "cru/win/native/window_class.hpp" #include "cru/win/native/window_render_target.hpp" @@ -37,7 +37,7 @@ WinNativeWindow::WinNativeWindow(WinApplication* application, window_manager->RegisterWindow(hwnd_, this); window_render_target_.reset( - new WindowRenderTarget(graph::GraphManager::GetInstance(), hwnd_)); + new WindowRenderTarget(graph::WinGraphFactory::GetInstance(), hwnd_)); } WinNativeWindow::~WinNativeWindow() { @@ -117,7 +117,7 @@ void WinNativeWindow::SetWindowRect(const ui::Rect& rect) { } } -platform::graph::Painter* WinNativeWindow::BeginPaint() { +platform::graph::IPainter* WinNativeWindow::BeginPaint() { return new WindowPainter(this); } diff --git a/src/win/native/window_painter.cpp b/src/win/native/window_painter.cpp index 4e98a7fd..463be128 100644 --- a/src/win/native/window_painter.cpp +++ b/src/win/native/window_painter.cpp @@ -1,29 +1,29 @@ #include "window_painter.hpp" #include "cru/win/exception.hpp" -#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/graph/win_native_factory.hpp" #include "cru/win/native/window_render_target.hpp" #include <cassert> namespace cru::win::native { WindowPainter::WindowPainter(WinNativeWindow* window) - : D2DPainter(window->GetWindowRenderTarget() - ->GetGraphManager() + : WinPainter(window->GetWindowRenderTarget() + ->GetWinNativeFactory() ->GetD2D1DeviceContext()), window_(window) { window->GetWindowRenderTarget()->SetAsTarget(); window->GetWindowRenderTarget() - ->GetGraphManager() + ->GetWinNativeFactory() ->GetD2D1DeviceContext() ->BeginDraw(); } -WindowPainter::~WindowPainter() { EndDraw(); } +WindowPainter::~WindowPainter() { End(); } void WindowPainter::DoEndDraw() { ThrowIfFailed(window_->GetWindowRenderTarget() - ->GetGraphManager() + ->GetWinNativeFactory() ->GetD2D1DeviceContext() ->EndDraw()); window_->GetWindowRenderTarget()->Present(); diff --git a/src/win/native/window_painter.hpp b/src/win/native/window_painter.hpp index 78c0717c..78731ef3 100644 --- a/src/win/native/window_painter.hpp +++ b/src/win/native/window_painter.hpp @@ -1,9 +1,9 @@ #pragma once -#include "cru/win/graph/d2d_painter.hpp" +#include "cru/win/graph/win_painter.hpp" #include "cru/win/native/win_native_window.hpp" namespace cru::win::native { -class WindowPainter : public graph::D2DPainter { +class WindowPainter : public graph::WinPainter { public: explicit WindowPainter(WinNativeWindow* window); WindowPainter(const WindowPainter& other) = delete; diff --git a/src/win/native/window_render_target.cpp b/src/win/native/window_render_target.cpp index c49a920e..606b51f8 100644 --- a/src/win/native/window_render_target.cpp +++ b/src/win/native/window_render_target.cpp @@ -1,18 +1,18 @@ #include "cru/win/native/window_render_target.hpp" #include "cru/win/exception.hpp" -#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/graph/win_native_factory.hpp" #include "dpi_util.hpp" #include <cassert> namespace cru::win::native { -WindowRenderTarget::WindowRenderTarget(graph::GraphManager* graph_manager, +WindowRenderTarget::WindowRenderTarget(graph::IWinNativeFactory* factory, HWND hwnd) { - this->graph_manager_ = graph_manager; + this->factory_ = factory; - const auto d3d11_device = graph_manager->GetD3D11Device(); - const auto dxgi_factory = graph_manager->GetDxgiFactory(); + const auto d3d11_device = factory->GetD3D11Device(); + const auto dxgi_factory = factory->GetDxgiFactory(); // Allocate a descriptor. DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {0}; @@ -39,8 +39,8 @@ WindowRenderTarget::WindowRenderTarget(graph::GraphManager* graph_manager, } void WindowRenderTarget::ResizeBuffer(const int width, const int height) { - const auto graph_manager = graph_manager_; - const auto d2d1_device_context = graph_manager->GetD2D1DeviceContext(); + const auto factory = factory_; + const auto d2d1_device_context = factory->GetD2D1DeviceContext(); Microsoft::WRL::ComPtr<ID2D1Image> old_target; d2d1_device_context->GetTarget(&old_target); @@ -59,7 +59,7 @@ void WindowRenderTarget::ResizeBuffer(const int width, const int height) { } void WindowRenderTarget::SetAsTarget() { - graph_manager_->GetD2D1DeviceContext()->SetTarget(target_bitmap_.Get()); + factory_->GetD2D1DeviceContext()->SetTarget(target_bitmap_.Get()); } void WindowRenderTarget::Present() { @@ -83,8 +83,7 @@ void WindowRenderTarget::CreateTargetBitmap() { // Get a D2D surface from the DXGI back buffer to use as the D2D render // target. - ThrowIfFailed( - graph_manager_->GetD2D1DeviceContext()->CreateBitmapFromDxgiSurface( - dxgi_back_buffer.Get(), &bitmap_properties, &target_bitmap_)); + ThrowIfFailed(factory_->GetD2D1DeviceContext()->CreateBitmapFromDxgiSurface( + dxgi_back_buffer.Get(), &bitmap_properties, &target_bitmap_)); } } // namespace cru::win::native |