diff options
author | crupest <crupest@outlook.com> | 2019-05-24 23:45:58 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-05-24 23:45:58 +0800 |
commit | b6db663269201fa14a6a4aa1b9042645a9e8f859 (patch) | |
tree | 1984e2c2784fb9623d4c20fbdd6fc650792e133c /include | |
parent | b9df1bcaea0c19b2e29479cdb1ad5a39e23c4ee7 (diff) | |
download | cru-b6db663269201fa14a6a4aa1b9042645a9e8f859.tar.gz cru-b6db663269201fa14a6a4aa1b9042645a9e8f859.tar.bz2 cru-b6db663269201fa14a6a4aa1b9042645a9e8f859.zip |
...
Diffstat (limited to 'include')
30 files changed, 253 insertions, 229 deletions
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_; }; |