diff options
-rw-r--r-- | include/cru/platform/graph/painter.hpp | 2 | ||||
-rw-r--r-- | include/cru/win/graph/direct/brush.hpp | 22 | ||||
-rw-r--r-- | include/cru/win/graph/direct/com_resource.hpp | 2 | ||||
-rw-r--r-- | include/cru/win/graph/direct/font.hpp | 35 | ||||
-rw-r--r-- | include/cru/win/graph/direct/geometry.hpp | 71 | ||||
-rw-r--r-- | include/cru/win/graph/direct/graph_factory.hpp | 25 | ||||
-rw-r--r-- | include/cru/win/graph/direct/painter.hpp | 65 | ||||
-rw-r--r-- | include/cru/win/graph/direct/platform_id.hpp | 19 | ||||
-rw-r--r-- | include/cru/win/graph/direct/text_layout.hpp | 55 | ||||
-rw-r--r-- | src/win/graph/direct/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/win/graph/direct/brush.cpp | 1 | ||||
-rw-r--r-- | src/win/graph/direct/font.cpp | 15 | ||||
-rw-r--r-- | src/win/graph/direct/geometry.cpp | 53 | ||||
-rw-r--r-- | src/win/graph/direct/graph_factory.cpp | 16 | ||||
-rw-r--r-- | src/win/graph/direct/painter.cpp | 135 | ||||
-rw-r--r-- | src/win/graph/direct/text_layout.cpp | 67 |
16 files changed, 341 insertions, 243 deletions
diff --git a/include/cru/platform/graph/painter.hpp b/include/cru/platform/graph/painter.hpp index 1096aa7c..97d4b4cf 100644 --- a/include/cru/platform/graph/painter.hpp +++ b/include/cru/platform/graph/painter.hpp @@ -37,5 +37,7 @@ class Painter : public NativeResource { virtual void DrawText(const Point& offset, TextLayout* text_layout, Brush* brush) = 0; + + virtual void EndDraw() = 0; }; } // namespace cru::platform::graph diff --git a/include/cru/win/graph/direct/brush.hpp b/include/cru/win/graph/direct/brush.hpp index 9775b5c1..1f1c319f 100644 --- a/include/cru/win/graph/direct/brush.hpp +++ b/include/cru/win/graph/direct/brush.hpp @@ -1,21 +1,39 @@ #pragma once #include "com_resource.hpp" #include "direct_factory.hpp" +#include "platform_id.hpp" #include "cru/platform/graph/brush.hpp" namespace cru::platform::graph::win::direct { +struct ID2DBrush { + virtual ~ID2DBrush() = default; + + virtual ID2D1Brush* GetD2DBrushInterface() const = 0; +}; + class D2DSolidColorBrush : public SolidColorBrush, + public ID2DBrush, public IComResource<ID2D1SolidColorBrush> { public: explicit D2DSolidColorBrush(IDirectFactory* factory); + D2DSolidColorBrush(const D2DSolidColorBrush& other) = delete; - D2DSolidColorBrush(D2DSolidColorBrush&& other) = delete; D2DSolidColorBrush& operator=(const D2DSolidColorBrush& other) = delete; + + D2DSolidColorBrush(D2DSolidColorBrush&& other) = delete; D2DSolidColorBrush& operator=(D2DSolidColorBrush&& other) = delete; + ~D2DSolidColorBrush() override = default; - ID2D1SolidColorBrush* GetComInterface() override { return brush_.Get(); } + CRU_PLATFORMID_IMPLEMENT_DIRECT + + public: + ID2D1Brush* GetD2DBrushInterface() const override { return brush_.Get(); } + + ID2D1SolidColorBrush* GetComInterface() const override { + return brush_.Get(); + } protected: void OnSetColor(const Color& color) override; diff --git a/include/cru/win/graph/direct/com_resource.hpp b/include/cru/win/graph/direct/com_resource.hpp index aa2c366b..22d1d6f0 100644 --- a/include/cru/win/graph/direct/com_resource.hpp +++ b/include/cru/win/graph/direct/com_resource.hpp @@ -6,6 +6,6 @@ template<typename TInterface> struct IComResource { virtual ~IComResource() = default; - virtual TInterface* GetComInterface() = 0; + virtual TInterface* GetComInterface() const = 0; }; } // namespace cru::platform::graph::win_direct diff --git a/include/cru/win/graph/direct/font.hpp b/include/cru/win/graph/direct/font.hpp index 44fa7edd..d3376503 100644 --- a/include/cru/win/graph/direct/font.hpp +++ b/include/cru/win/graph/direct/font.hpp @@ -1,28 +1,33 @@ #pragma once -#include "../win_pre_config.hpp" +#include "com_resource.hpp" +#include "direct_factory.hpp" +#include "platform_id.hpp" #include "cru/platform/graph/font.hpp" #include <string_view> -namespace cru::win::graph { -struct IWinNativeFactory; +namespace cru::platform::graph::win::direct { -class WinFontDescriptor : public Object, - public virtual platform::graph::IFontDescriptor { +class DWriteFont : public Font, public IComResource<IDWriteTextFormat> { public: - explicit WinFontDescriptor(IWinNativeFactory* factory, - const std::wstring_view& font_family, - float font_size); - WinFontDescriptor(const WinFontDescriptor& other) = delete; - WinFontDescriptor(WinFontDescriptor&& other) = delete; - WinFontDescriptor& operator=(const WinFontDescriptor& other) = delete; - WinFontDescriptor& operator=(WinFontDescriptor&& other) = delete; - ~WinFontDescriptor() override = default; + DWriteFont(IDirectFactory* factory, const std::wstring_view& font_family, + float font_size); - IDWriteTextFormat* GetDWriteTextFormat() const { return text_format_.Get(); } + DWriteFont(const DWriteFont& other) = delete; + DWriteFont& operator=(const DWriteFont& other) = delete; + + DWriteFont(DWriteFont&& other) = delete; + DWriteFont& operator=(DWriteFont&& other) = delete; + + ~DWriteFont() override = default; + + CRU_PLATFORMID_IMPLEMENT_DIRECT + + public: + IDWriteTextFormat* GetComInterface() const override { return text_format_.Get(); } private: Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_; }; -} // namespace cru::win::graph +} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/geometry.hpp b/include/cru/win/graph/direct/geometry.hpp index e312f13c..f40db7af 100644 --- a/include/cru/win/graph/direct/geometry.hpp +++ b/include/cru/win/graph/direct/geometry.hpp @@ -1,48 +1,63 @@ #pragma once -#include "../win_pre_config.hpp" +#include "com_resource.hpp" +#include "direct_factory.hpp" +#include "platform_id.hpp" #include "cru/platform/graph/geometry.hpp" -namespace cru::win::graph { -struct IWinNativeFactory; +namespace cru::platform::graph::win::direct { +class D2DGeometryBuilder : public GeometryBuilder { + public: + explicit D2DGeometryBuilder(IDirectFactory* factory); + + D2DGeometryBuilder(const D2DGeometryBuilder& other) = delete; + D2DGeometryBuilder& operator=(const D2DGeometryBuilder& other) = delete; + + D2DGeometryBuilder(D2DGeometryBuilder&& other) = delete; + D2DGeometryBuilder& operator=(D2DGeometryBuilder&& other) = delete; + + ~D2DGeometryBuilder() override; + + CRU_PLATFORMID_IMPLEMENT_DIRECT -class WinGeometryBuilder : public Object, - public virtual platform::graph::IGeometryBuilder { public: - 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; - - 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 BeginFigure(const Point& point) override; + void LineTo(const Point& point) override; + void QuadraticBezierTo(const Point& control_point, + const Point& end_point) override; void CloseFigure(bool close) override; - platform::graph::IGeometry* End() override; - bool IsEnded() const override { return geometry_ != nullptr; } + + Geometry* Build() override; + + private: + bool IsValid() { return geometry_ != nullptr; } private: Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry_; Microsoft::WRL::ComPtr<ID2D1GeometrySink> geometry_sink_; }; -class WinGeometry : public Object, public virtual platform::graph::IGeometry { +class D2DGeometry : public Geometry, public IComResource<ID2D1Geometry> { public: - explicit WinGeometry(Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry); - WinGeometry(const WinGeometry& other) = delete; - WinGeometry(WinGeometry&& other) = delete; - WinGeometry& operator=(const WinGeometry& other) = delete; - WinGeometry& operator=(WinGeometry&& other) = delete; - ~WinGeometry() override = default; + explicit D2DGeometry(Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry); + + D2DGeometry(const D2DGeometry& other) = delete; + D2DGeometry& operator=(const D2DGeometry& other) = delete; + + D2DGeometry(D2DGeometry&& other) = delete; + D2DGeometry& operator=(D2DGeometry&& other) = delete; + + ~D2DGeometry() override = default; - bool FillContains(const ui::Point& point) override; + CRU_PLATFORMID_IMPLEMENT_DIRECT - ID2D1PathGeometry* GetNative() const { return geometry_.Get(); } + public: + ID2D1Geometry* GetComInterface() const override { return geometry_.Get(); } + + public: + bool FillContains(const Point& point) override; private: Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry_; }; -} // namespace cru::win::graph +} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/graph_factory.hpp b/include/cru/win/graph/direct/graph_factory.hpp index b3c901be..841dd104 100644 --- a/include/cru/win/graph/direct/graph_factory.hpp +++ b/include/cru/win/graph/direct/graph_factory.hpp @@ -1,7 +1,11 @@ #pragma once #include "direct_factory.hpp" +#include "platform_id.hpp" #include "brush.hpp" +#include "font.hpp" +#include "geometry.hpp" +#include "text_layout.hpp" #include "cru/platform/graph/graph_factory.hpp" @@ -17,11 +21,16 @@ class DirectGraphFactory : public GraphFactory, IDirectFactory { public: DirectGraphFactory(const DirectGraphFactory& other) = delete; - DirectGraphFactory(DirectGraphFactory&& other) = delete; DirectGraphFactory& operator=(const DirectGraphFactory& other) = delete; + + DirectGraphFactory(DirectGraphFactory&& other) = delete; DirectGraphFactory& operator=(DirectGraphFactory&& other) = delete; + ~DirectGraphFactory() override; + CRU_PLATFORMID_IMPLEMENT_DIRECT + + public: ID2D1Factory1* GetD2D1Factory() const override { return d2d1_factory_.Get(); } ID2D1DeviceContext* GetD2D1DeviceContext() const override { return d2d1_device_context_.Get(); @@ -35,14 +44,16 @@ class DirectGraphFactory : public GraphFactory, IDirectFactory { return dwrite_system_font_collection_.Get(); } + public: D2DSolidColorBrush* CreateSolidColorBrush() override; D2DGeometryBuilder* CreateGeometryBuilder() override; - D2DFont* CreateFont( - const std::wstring_view& font_family, float font_size) override; - DWriteTextLayout* CreateTextLayout( - std::shared_ptr<Font> font, - std::wstring text) override; + + DWriteFont* CreateFont(const std::wstring_view& font_family, + float font_size) override; + + DWriteTextLayout* CreateTextLayout(std::shared_ptr<Font> font, + std::wstring text) override; bool IsAutoDelete() const override { return auto_delete_; } void SetAutoDelete(bool value) override { auto_delete_ = value; } @@ -57,4 +68,4 @@ class DirectGraphFactory : public GraphFactory, IDirectFactory { Microsoft::WRL::ComPtr<IDWriteFactory> dwrite_factory_; Microsoft::WRL::ComPtr<IDWriteFontCollection> dwrite_system_font_collection_; }; -} // namespace cru::win::graph +} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/painter.hpp b/include/cru/win/graph/direct/painter.hpp index f218488c..d8791c7f 100644 --- a/include/cru/win/graph/direct/painter.hpp +++ b/include/cru/win/graph/direct/painter.hpp @@ -1,43 +1,54 @@ #pragma once -#include "../win_pre_config.hpp" +#include "com_resource.hpp" +#include "platform_id.hpp" #include "cru/platform/graph/painter.hpp" -namespace cru::win::graph { -class GraphManager; +namespace cru::platform::graph::win::direct { +class D2DPainter : public Painter, public IComResource<ID2D1RenderTarget> { + public: + explicit D2DPainter(ID2D1RenderTarget* render_target); + + D2DPainter(const D2DPainter& other) = delete; + D2DPainter& operator=(const D2DPainter& other) = delete; + + D2DPainter(D2DPainter&& other) = delete; + D2DPainter& operator=(D2DPainter&& other) = delete; + + ~D2DPainter() override = default; + + CRU_PLATFORMID_IMPLEMENT_DIRECT + + public: + ID2D1RenderTarget* GetComInterface() const override { return render_target_; } -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; + 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, + + void Clear(const Color& color) override; + + void StrokeRectangle(const Rect& rectangle, Brush* 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_; } + void FillRectangle(const Rect& rectangle, Brush* brush) override; + + void StrokeGeometry(Geometry* geometry, Brush* brush, float width) override; + void FillGeometry(Geometry* geometry, Brush* brush) override; + + void DrawText(const Point& offset, TextLayout* text_layout, + Brush* brush) override; + + void EndDraw() override final; protected: virtual void DoEndDraw() = 0; private: + bool IsValid() { return is_drawing_; } + + private: ID2D1RenderTarget* render_target_; - bool is_draw_ended_ = false; + bool is_drawing_ = true; }; -} // namespace cru::win::graph +} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/platform_id.hpp b/include/cru/win/graph/direct/platform_id.hpp new file mode 100644 index 00000000..ff02eb27 --- /dev/null +++ b/include/cru/win/graph/direct/platform_id.hpp @@ -0,0 +1,19 @@ +#pragma once +#include <cru/platform/native_resource.hpp> + +#include <stdexcept> +#include <string_view> + +namespace cru::platform::graph::win::direct { +constexpr std::wstring_view platform_id = L"Windows Direct"; + +bool IsDirectResource(NativeResource* resource) { + return resource->GetPlatformId() == platform_id; +} + +} // namespace cru::platform::graph::win::direct + +#define CRU_PLATFORMID_IMPLEMENT_DIRECT \ + std::wstring_view GetPlatformId() const override { \ + return ::cru::platform::graph::win::direct::platform_id; \ + } diff --git a/include/cru/win/graph/direct/text_layout.hpp b/include/cru/win/graph/direct/text_layout.hpp index 7339eff9..c7657762 100644 --- a/include/cru/win/graph/direct/text_layout.hpp +++ b/include/cru/win/graph/direct/text_layout.hpp @@ -1,42 +1,55 @@ #pragma once -#include "../win_pre_config.hpp" +#include "com_resource.hpp" +#include "direct_factory.hpp" +#include "platform_id.hpp" #include "cru/platform/graph/text_layout.hpp" +#include "font.hpp" + #include <memory> -namespace cru::win::graph { -struct IWinNativeFactory; -class WinFontDescriptor; +namespace cru::platform::graph::win::direct { +class DWriteTextLayout : public TextLayout, + public IComResource<IDWriteTextLayout> { + public: + explicit DWriteTextLayout(IDirectFactory* factory, std::shared_ptr<Font> font, + std::wstring text); + + DWriteTextLayout(const DWriteTextLayout& other) = delete; + DWriteTextLayout& operator=(const DWriteTextLayout& other) = delete; + + DWriteTextLayout(DWriteTextLayout&& other) = delete; + DWriteTextLayout& operator=(DWriteTextLayout&& other) = delete; + + ~DWriteTextLayout() override = default; + + CRU_PLATFORMID_IMPLEMENT_DIRECT -class WinTextLayout : public Object, public virtual platform::graph::ITextLayout { public: - explicit WinTextLayout(IWinNativeFactory* factory, - std::shared_ptr<WinFontDescriptor> font, std::wstring text); - WinTextLayout(const WinTextLayout& other) = delete; - WinTextLayout(WinTextLayout&& other) = delete; - WinTextLayout& operator=(const WinTextLayout& other) = delete; - WinTextLayout& operator=(WinTextLayout&& other) = delete; - ~WinTextLayout() override = default; + IDWriteTextLayout* GetComInterface() const override { + return text_layout_.Get(); + } + public: std::wstring GetText() override; void SetText(std::wstring new_text) override; - std::shared_ptr<platform::graph::IFontDescriptor> GetFont(); - void SetFont(std::shared_ptr<platform::graph::IFontDescriptor> font); + + std::shared_ptr<Font> GetFont(); + void SetFont(std::shared_ptr<Font> font); + void SetMaxWidth(float max_width) override; void SetMaxHeight(float max_height) override; - ui::Rect GetTextBounds() override; - std::vector<ui::Rect> TextRangeRect( - const ui::TextRange& text_range) override; - IDWriteTextLayout* GetDWriteTextLayout() const { return text_layout_.Get(); } + Rect GetTextBounds() override; + std::vector<Rect> TextRangeRect(const TextRange& text_range) override; private: - IWinNativeFactory* factory_; + IDirectFactory* factory_; std::wstring text_; - std::shared_ptr<WinFontDescriptor> font_descriptor_; + std::shared_ptr<DWriteFont> font_; float max_width_ = 0.0f; float max_height_ = 0.0f; Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_; }; -} // namespace cru::platform::win +} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/CMakeLists.txt b/src/win/graph/direct/CMakeLists.txt index 7a04a778..7cbbb080 100644 --- a/src/win/graph/direct/CMakeLists.txt +++ b/src/win/graph/direct/CMakeLists.txt @@ -19,6 +19,7 @@ target_sources(cru_win_graph_direct PUBLIC ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/geometry.hpp ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/graph_factory.hpp ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/painter.hpp + ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/platform_id.hpp ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/text_layout.hpp ) target_link_libraries(cru_win_graph_direct PUBLIC D3D11 D2d1 DWrite cru_win_base) diff --git a/src/win/graph/direct/brush.cpp b/src/win/graph/direct/brush.cpp index 7ddd14b4..1f17cd03 100644 --- a/src/win/graph/direct/brush.cpp +++ b/src/win/graph/direct/brush.cpp @@ -1,7 +1,6 @@ #include "cru/win/graph/direct/brush.hpp" #include "cru/win/graph/direct/convert_util.hpp" -#include "cru/win/graph/direct/direct_factory.hpp" #include "cru/win/graph/direct/exception.hpp" #include <cassert> diff --git a/src/win/graph/direct/font.cpp b/src/win/graph/direct/font.cpp index a359d73e..5d7b4483 100644 --- a/src/win/graph/direct/font.cpp +++ b/src/win/graph/direct/font.cpp @@ -1,21 +1,20 @@ -#include "cru/win/graph/win_font.hpp" +#include "cru/win/graph/direct/font.hpp" #include "cru/win/exception.hpp" -#include "cru/win/graph/win_native_factory.hpp" +#include "cru/win/graph/direct/exception.hpp" #include <array> #include <cassert> #include <utility> -namespace cru::win::graph { -WinFontDescriptor::WinFontDescriptor(IWinNativeFactory* factory, - const std::wstring_view& font_family, - float font_size) { +namespace cru::platform::graph::win::direct { +DWriteFont::DWriteFont(IDirectFactory* factory, + const std::wstring_view& font_family, float font_size) { assert(factory); std::array<wchar_t, LOCALE_NAME_MAX_LENGTH> buffer; if (!::GetUserDefaultLocaleName(buffer.data(), static_cast<int>(buffer.size()))) - throw Win32Error(::GetLastError(), "Failed to get locale."); + throw platform::win::Win32Error(::GetLastError(), "Failed to get locale."); ThrowIfFailed(factory->GetDWriteFactory()->CreateTextFormat( font_family.data(), nullptr, DWRITE_FONT_WEIGHT_NORMAL, @@ -26,4 +25,4 @@ WinFontDescriptor::WinFontDescriptor(IWinNativeFactory* factory, ThrowIfFailed( text_format_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER)); } -} // namespace cru::win::graph +} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/geometry.cpp b/src/win/graph/direct/geometry.cpp index a725eff6..6b827906 100644 --- a/src/win/graph/direct/geometry.cpp +++ b/src/win/graph/direct/geometry.cpp @@ -1,65 +1,64 @@ -#include "cru/win/graph/win_geometry.hpp" +#include "cru/win/graph/direct/geometry.hpp" -#include "cru/win/exception.hpp" -#include "cru/win/graph/util/convert_util.hpp" -#include "cru/win/graph/win_native_factory.hpp" +#include "cru/win/graph/direct/convert_util.hpp" +#include "cru/win/graph/direct/exception.hpp" #include <cassert> -namespace cru::win::graph { -WinGeometryBuilder::WinGeometryBuilder(IWinNativeFactory* factory) { +namespace cru::platform::graph::win::direct { +D2DGeometryBuilder::D2DGeometryBuilder(IDirectFactory* factory) { assert(factory); ThrowIfFailed(factory->GetD2D1Factory()->CreatePathGeometry(&geometry_)); ThrowIfFailed(geometry_->Open(&geometry_sink_)); } -WinGeometryBuilder::~WinGeometryBuilder() { +D2DGeometryBuilder::~D2DGeometryBuilder() { if (geometry_sink_) { ThrowIfFailed(geometry_sink_->Close()); } } -void WinGeometryBuilder::BeginFigure(const ui::Point& point) { - assert(IsEnded()); - geometry_sink_->BeginFigure(util::Convert(point), D2D1_FIGURE_BEGIN_FILLED); +void D2DGeometryBuilder::BeginFigure(const Point& point) { + assert(IsValid()); + geometry_sink_->BeginFigure(Convert(point), D2D1_FIGURE_BEGIN_FILLED); } -void WinGeometryBuilder::LineTo(const ui::Point& point) { - assert(IsEnded()); - geometry_sink_->AddLine(util::Convert(point)); +void D2DGeometryBuilder::LineTo(const Point& point) { + assert(IsValid()); + geometry_sink_->AddLine(Convert(point)); } -void WinGeometryBuilder::QuadraticBezierTo(const ui::Point& control_point, - const ui::Point& end_point) { - assert(IsEnded()); - geometry_sink_->AddQuadraticBezier(D2D1::QuadraticBezierSegment( - util::Convert(control_point), util::Convert(end_point))); +void D2DGeometryBuilder::QuadraticBezierTo(const Point& control_point, + const Point& end_point) { + assert(IsValid()); + geometry_sink_->AddQuadraticBezier( + D2D1::QuadraticBezierSegment(Convert(control_point), Convert(end_point))); } -void WinGeometryBuilder::CloseFigure(bool close) { - assert(IsEnded()); +void D2DGeometryBuilder::CloseFigure(bool close) { + assert(IsValid()); geometry_sink_->EndFigure(close ? D2D1_FIGURE_END_CLOSED : D2D1_FIGURE_END_OPEN); } -platform::graph::IGeometry* WinGeometryBuilder::End() { - assert(IsEnded()); +Geometry* D2DGeometryBuilder::Build() { + assert(IsValid()); ThrowIfFailed(geometry_sink_->Close()); geometry_sink_ = nullptr; - const auto geometry = new WinGeometry(std::move(geometry_)); + const auto geometry = new D2DGeometry(std::move(geometry_)); geometry_ = nullptr; return geometry; } -WinGeometry::WinGeometry(Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry) { +D2DGeometry::D2DGeometry(Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry) { assert(geometry); geometry_ = std::move(geometry); } -bool WinGeometry::FillContains(const ui::Point& point) { +bool D2DGeometry::FillContains(const Point& point) { BOOL result; ThrowIfFailed(geometry_->FillContainsPoint( - util::Convert(point), D2D1::Matrix3x2F::Identity(), &result)); + Convert(point), D2D1::Matrix3x2F::Identity(), &result)); return result != 0; } -} // namespace cru::win::graph +} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/graph_factory.cpp b/src/win/graph/direct/graph_factory.cpp index 2f2bb7a5..49752d0b 100644 --- a/src/win/graph/direct/graph_factory.cpp +++ b/src/win/graph/direct/graph_factory.cpp @@ -95,19 +95,17 @@ D2DSolidColorBrush* DirectGraphFactory::CreateSolidColorBrush() { return new D2DSolidColorBrush(this); } -platform::graph::IGeometryBuilder* WinGraphFactory::CreateGeometryBuilder() { - return new WinGeometryBuilder(this); +D2DGeometryBuilder* DirectGraphFactory::CreateGeometryBuilder() { + return new D2DGeometryBuilder(this); } -platform::graph::IFontDescriptor* WinGraphFactory::CreateFontDescriptor( +DWriteFont* DirectGraphFactory::CreateFont( const std::wstring_view& font_family, float font_size) { - return new WinFontDescriptor(this, font_family, font_size); + return new DWriteFont(this, font_family, font_size); } -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(this, std::move(f), std::move(text)); +DWriteTextLayout* DirectGraphFactory::CreateTextLayout( + std::shared_ptr<Font> font, std::wstring text) { + return new DWriteTextLayout(this, std::move(font), std::move(text)); } } // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/painter.cpp b/src/win/graph/direct/painter.cpp index f75cf4e0..6fbbf957 100644 --- a/src/win/graph/direct/painter.cpp +++ b/src/win/graph/direct/painter.cpp @@ -1,93 +1,106 @@ -#include "cru/win/graph/win_painter.hpp" +#include "cru/win/graph/direct/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 "cru/win/graph/direct/brush.hpp" +#include "cru/win/graph/direct/convert_util.hpp" +#include "cru/win/graph/direct/exception.hpp" +#include "cru/win/graph/direct/geometry.hpp" +#include "cru/win/graph/direct/text_layout.hpp" #include <cassert> +#include <type_traits> -namespace cru::win::graph { -WinPainter::WinPainter(ID2D1RenderTarget* render_target) { +namespace cru::platform::graph::win::direct { + +namespace { +template <typename T, typename U, typename = void> +struct is_static_castable : std::false_type {}; + +template <typename T, typename U> +struct is_static_castable< + T, U, std::void_t<decltype(static_cast<U>(std::declval<T>()))>> + : std::true_type {}; + +template <typename TDes, typename TSrc> +TDes* CheckAndCast(TSrc* src) { + assert(src); + assert(IsDirectResource(src)); + if constexpr (is_static_castable<TSrc*, TDes*>::value) + return static_cast<TDes*>(src); + else { + TDes* d = dynamic_cast<TDes*>(src); + assert(d); + return d; + } +} +} // namespace + +D2DPainter::D2DPainter(ID2D1RenderTarget* render_target) { assert(render_target); render_target_ = render_target; } -platform::Matrix WinPainter::GetTransform() { - assert(!IsEnded()); +platform::Matrix D2DPainter::GetTransform() { + assert(IsValid()); D2D1_MATRIX_3X2_F m; render_target_->GetTransform(&m); - return util::Convert(m); + return Convert(m); } -void WinPainter::SetTransform(const platform::Matrix& matrix) { - assert(!IsEnded()); - render_target_->SetTransform(util::Convert(matrix)); +void D2DPainter::SetTransform(const platform::Matrix& matrix) { + assert(IsValid()); + render_target_->SetTransform(Convert(matrix)); } -void WinPainter::Clear(const ui::Color& color) { - assert(!IsEnded()); - render_target_->Clear(util::Convert(color)); +void D2DPainter::Clear(const Color& color) { + assert(IsValid()); + render_target_->Clear(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(), +void D2DPainter::StrokeRectangle(const Rect& rectangle, Brush* brush, + float width) { + assert(IsValid()); + const auto b = CheckAndCast<ID2DBrush>(brush); + render_target_->DrawRectangle(Convert(rectangle), b->GetD2DBrushInterface(), 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 D2DPainter::FillRectangle(const Rect& rectangle, Brush* brush) { + assert(IsValid()); + const auto b = CheckAndCast<ID2DBrush>(brush); + render_target_->FillRectangle(Convert(rectangle), b->GetD2DBrushInterface()); } -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); +void D2DPainter::StrokeGeometry(Geometry* geometry, Brush* brush, float width) { + assert(IsValid()); + const auto g = CheckAndCast<D2DGeometry>(geometry); + const auto b = CheckAndCast<ID2DBrush>(brush); - render_target_->DrawGeometry(g->GetNative(), b->GetD2DBrush(), width); + render_target_->DrawGeometry(g->GetComInterface(), b->GetD2DBrushInterface(), + 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); +void D2DPainter::FillGeometry(Geometry* geometry, Brush* brush) { + assert(IsValid()); + const auto g = CheckAndCast<D2DGeometry>(geometry); + const auto b = CheckAndCast<ID2DBrush>(brush); - render_target_->FillGeometry(g->GetNative(), b->GetD2DBrush()); + render_target_->FillGeometry(g->GetComInterface(), b->GetD2DBrushInterface()); } -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 D2DPainter::DrawText(const Point& offset, TextLayout* text_layout, + Brush* brush) { + assert(IsValid()); + const auto t = CheckAndCast<DWriteTextLayout>(text_layout); + const auto b = CheckAndCast<ID2DBrush>(brush); + + render_target_->DrawTextLayout(Convert(offset), t->GetComInterface(), + b->GetD2DBrushInterface()); } -void WinPainter::End() { - if (!is_draw_ended_) { - is_draw_ended_ = true; +void D2DPainter::EndDraw() { + if (is_drawing_) { + is_drawing_ = false; DoEndDraw(); } } -} // namespace cru::win::graph +} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/text_layout.cpp b/src/win/graph/direct/text_layout.cpp index 997309ad..23a14b2a 100644 --- a/src/win/graph/direct/text_layout.cpp +++ b/src/win/graph/direct/text_layout.cpp @@ -1,71 +1,66 @@ -#include "cru/win/graph/win_text_layout.hpp" +#include "cru/win/graph/direct/text_layout.hpp" -#include "cru/win/exception.hpp" -#include "cru/win/graph/win_font.hpp" -#include "cru/win/graph/win_native_factory.hpp" +#include "cru/win/graph/direct/exception.hpp" #include <cassert> #include <utility> -namespace cru::win::graph { -WinTextLayout::WinTextLayout(IWinNativeFactory* factory, - std::shared_ptr<WinFontDescriptor> font, - std::wstring text) { +namespace cru::platform::graph::win::direct { +DWriteTextLayout::DWriteTextLayout(IDirectFactory* factory, + std::shared_ptr<Font> font, + std::wstring text) + : text_(std::move(text)) { assert(factory); assert(font); + assert(IsDirectResource(font.get())); factory_ = factory; - text_.swap(text); - font_descriptor_.swap(font); + font_ = std::static_pointer_cast<DWriteFont>(font); ThrowIfFailed(factory->GetDWriteFactory()->CreateTextLayout( text_.c_str(), static_cast<UINT32>(text_.size()), - font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, - &text_layout_)); + font_->GetComInterface(), max_width_, max_height_, &text_layout_)); } -std::wstring WinTextLayout::GetText() { return text_; } +std::wstring DWriteTextLayout::GetText() { return text_; } -void WinTextLayout::SetText(std::wstring new_text) { +void DWriteTextLayout::SetText(std::wstring new_text) { text_.swap(new_text); ThrowIfFailed(factory_->GetDWriteFactory()->CreateTextLayout( text_.c_str(), static_cast<UINT32>(text_.size()), - font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, - &text_layout_)); + font_->GetComInterface(), max_width_, max_height_, &text_layout_)); } -std::shared_ptr<platform::graph::IFontDescriptor> WinTextLayout::GetFont() { - return font_descriptor_; +std::shared_ptr<Font> DWriteTextLayout::GetFont() { + return std::static_pointer_cast<Font>(font_); } -void WinTextLayout::SetFont( - std::shared_ptr<platform::graph::IFontDescriptor> font) { - auto f = std::dynamic_pointer_cast<WinFontDescriptor>(font); - assert(f); - f.swap(font_descriptor_); +void DWriteTextLayout::SetFont(std::shared_ptr<Font> font) { + assert(IsDirectResource(font.get())); + auto f = std::static_pointer_cast<DWriteFont>(font); + + f.swap(font_); ThrowIfFailed(factory_->GetDWriteFactory()->CreateTextLayout( text_.c_str(), static_cast<UINT32>(text_.size()), - font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, - &text_layout_)); + font_->GetComInterface(), max_width_, max_height_, &text_layout_)); } -void WinTextLayout::SetMaxWidth(float max_width) { +void DWriteTextLayout::SetMaxWidth(float max_width) { max_width_ = max_width; ThrowIfFailed(text_layout_->SetMaxWidth(max_width_)); } -void WinTextLayout::SetMaxHeight(float max_height) { +void DWriteTextLayout::SetMaxHeight(float max_height) { max_height_ = max_height; ThrowIfFailed(text_layout_->SetMaxHeight(max_height_)); } -ui::Rect WinTextLayout::GetTextBounds() { +Rect DWriteTextLayout::GetTextBounds() { DWRITE_TEXT_METRICS metrics; ThrowIfFailed(text_layout_->GetMetrics(&metrics)); - return ui::Rect{metrics.left, metrics.top, metrics.width, metrics.height}; + return Rect{metrics.left, metrics.top, metrics.width, metrics.height}; } -std::vector<ui::Rect> WinTextLayout::TextRangeRect( - const ui::TextRange& text_range) { +std::vector<Rect> DWriteTextLayout::TextRangeRect(const TextRange& text_range) { DWRITE_TEXT_METRICS text_metrics; ThrowIfFailed(text_layout_->GetMetrics(&text_metrics)); const auto metrics_count = @@ -80,15 +75,15 @@ std::vector<ui::Rect> WinTextLayout::TextRangeRect( hit_test_metrics.erase(hit_test_metrics.cbegin() + actual_count, hit_test_metrics.cend()); - std::vector<ui::Rect> result; + std::vector<Rect> result; result.reserve(actual_count); for (const auto& metrics : hit_test_metrics) { - result.push_back(ui::Rect{metrics.left, metrics.top, - metrics.left + metrics.width, - metrics.top + metrics.height}); + result.push_back(Rect{metrics.left, metrics.top, + metrics.left + metrics.width, + metrics.top + metrics.height}); } return result; } -} // namespace cru::win::graph +} // namespace cru::platform::graph::win::direct |