diff options
Diffstat (limited to 'src')
-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 |
7 files changed, 146 insertions, 142 deletions
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 |