diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ui/controls/text_control.cpp | 56 | ||||
-rw-r--r-- | src/ui/controls/text_control.hpp | 5 | ||||
-rw-r--r-- | src/ui/render/text_render_object.cpp | 91 | ||||
-rw-r--r-- | src/ui/render/text_render_object.hpp | 71 |
4 files changed, 164 insertions, 59 deletions
diff --git a/src/ui/controls/text_control.cpp b/src/ui/controls/text_control.cpp index e53d3c69..fcfcb90c 100644 --- a/src/ui/controls/text_control.cpp +++ b/src/ui/controls/text_control.cpp @@ -19,28 +19,7 @@ namespace cru::ui::controls return is_trailing == 0 ? metrics.textPosition : metrics.textPosition + 1; } - void DrawSelectionRect(ID2D1DeviceContext* device_context, IDWriteTextLayout* layout, ID2D1Brush* brush, const std::optional<TextRange> range) - { - if (range.has_value()) - { - DWRITE_TEXT_METRICS text_metrics{}; - ThrowIfFailed(layout->GetMetrics(&text_metrics)); - const auto metrics_count = text_metrics.lineCount * text_metrics.maxBidiReorderingDepth; - - std::vector<DWRITE_HIT_TEST_METRICS> hit_test_metrics(metrics_count); - UINT32 actual_count; - layout->HitTestTextRange( - range.value().position, range.value().count, - 0, 0, - hit_test_metrics.data(), metrics_count, &actual_count - ); - - hit_test_metrics.erase(hit_test_metrics.cbegin() + actual_count, hit_test_metrics.cend()); - - for (const auto& metrics : hit_test_metrics) - device_context->FillRoundedRectangle(D2D1::RoundedRect(D2D1::RectF(metrics.left, metrics.top, metrics.left + metrics.width, metrics.top + metrics.height), 3, 3), brush); - } - } + } TextControl::TextControl(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format, @@ -58,9 +37,6 @@ namespace cru::ui::controls draw_content_event.AddHandler([this](events::DrawEventArgs& args) { - const auto device_context = args.GetDeviceContext(); - DrawSelectionRect(device_context, text_layout_.Get(), selection_brush_.Get(), selected_range_); - device_context->DrawTextLayout(D2D1::Point2F(), text_layout_.Get(), brush_.Get()); }); mouse_down_event.bubble.AddHandler([this](events::MouseButtonEventArgs& args) @@ -169,19 +145,7 @@ namespace cru::ui::controls } } - Size TextControl::OnMeasureContent(const Size& available_size, const AdditionalMeasureInfo&) - { - ThrowIfFailed(text_layout_->SetMaxWidth(available_size.width)); - ThrowIfFailed(text_layout_->SetMaxHeight(available_size.height)); - - DWRITE_TEXT_METRICS metrics{}; - ThrowIfFailed(text_layout_->GetMetrics(&metrics)); - - const Size measure_result(metrics.width, metrics.height); - - return measure_result; - } void TextControl::RequestChangeCaretPosition(unsigned position) { @@ -202,24 +166,6 @@ namespace cru::ui::controls InvalidateDraw(); } - void TextControl::RecreateTextLayout() - { - assert(text_format_ != nullptr); - - text_layout_ = nullptr; - - const auto dwrite_factory = graph::GraphManager::GetInstance()->GetDWriteFactory(); - - const auto&& size = GetSize(); - - ThrowIfFailed(dwrite_factory->CreateTextLayout( - text_.c_str(), static_cast<UINT32>(text_.size()), - text_format_.Get(), - size.width, size.height, - &text_layout_ - )); - } - void TextControl::UpdateCursor(const std::optional<Point>& point) { if (!is_selectable_) diff --git a/src/ui/controls/text_control.hpp b/src/ui/controls/text_control.hpp index 58d48c13..83d4753f 100644 --- a/src/ui/controls/text_control.hpp +++ b/src/ui/controls/text_control.hpp @@ -62,7 +62,6 @@ namespace cru::ui::controls protected: void SetSelectable(bool is_selectable); - Size OnMeasureContent(const Size& available_size, const AdditionalMeasureInfo&) override final; virtual void RequestChangeCaretPosition(unsigned position); @@ -82,14 +81,12 @@ namespace cru::ui::controls Microsoft::WRL::ComPtr<ID2D1Brush> brush_; Microsoft::WRL::ComPtr<ID2D1Brush> selection_brush_; Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_; - protected: Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_; - private: bool is_selectable_ = false; + std::optional<TextRange> selected_range_ = std::nullopt; bool is_selecting_ = false; unsigned mouse_down_position_ = 0; - std::optional<TextRange> selected_range_ = std::nullopt; }; } diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp new file mode 100644 index 00000000..43724e9f --- /dev/null +++ b/src/ui/render/text_render_object.cpp @@ -0,0 +1,91 @@ +#include "text_render_object.hpp" + +#include <cassert> + +#include "exception.hpp" +#include "graph/graph.hpp" + +namespace cru::ui::render { +TextRenderObject::TextRenderObject( + Microsoft::WRL::ComPtr<ID2D1Brush> brush, + Microsoft::WRL::ComPtr<IDWriteTextFormat> format, + Microsoft::WRL::ComPtr<ID2D1Brush> selection_brush) + : brush_(std::move(brush)), + text_format_(std::move(format)), + selection_brush_(std::move(selection_brush)) { + RecreateTextLayout(); +} + +namespace { +void DrawSelectionRect(ID2D1RenderTarget* render_target, + IDWriteTextLayout* layout, ID2D1Brush* brush, + const std::optional<TextRange> range) { + if (range.has_value()) { + DWRITE_TEXT_METRICS text_metrics{}; + ThrowIfFailed(layout->GetMetrics(&text_metrics)); + const auto metrics_count = + text_metrics.lineCount * text_metrics.maxBidiReorderingDepth; + + std::vector<DWRITE_HIT_TEST_METRICS> hit_test_metrics(metrics_count); + UINT32 actual_count; + layout->HitTestTextRange(range.value().position, range.value().count, 0, 0, + hit_test_metrics.data(), metrics_count, + &actual_count); + + hit_test_metrics.erase(hit_test_metrics.cbegin() + actual_count, + hit_test_metrics.cend()); + + for (const auto& metrics : hit_test_metrics) + render_target->FillRoundedRectangle( + D2D1::RoundedRect(D2D1::RectF(metrics.left, metrics.top, + metrics.left + metrics.width, + metrics.top + metrics.height), + 3, 3), + brush); + } +} +} // namespace + +void TextRenderObject::Draw(ID2D1RenderTarget* render_target) { + graph::WithTransform( + render_target, + D2D1::Matrix3x2F::Translation(GetMargin().left + GetPadding().left, + GetMargin().top + GetPadding().top), + [this](auto rt) { + DrawSelectionRect(rt, text_layout_.Get(), selection_brush_.Get(), + selection_range_); + rt->DrawTextLayout(D2D1::Point2F(), text_layout_.Get(), brush_.Get()); + }); +} + +RenderObject* TextRenderObject::HitTest(const Point& point) { + return Rect{Point::Zero(), GetSize()}.IsPointInside(point) ? this : nullptr; +} + +Size TextRenderObject::OnMeasureContent(const Size& available_size) { + ThrowIfFailed(text_layout_->SetMaxWidth(available_size.width)); + ThrowIfFailed(text_layout_->SetMaxHeight(available_size.height)); + + DWRITE_TEXT_METRICS metrics; + ThrowIfFailed(text_layout_->GetMetrics(&metrics)); + + return Size(metrics.width, metrics.height); +} + +void TextRenderObject::OnLayoutContent(const Rect& content_rect) {} + +void TextRenderObject::RecreateTextLayout() { + assert(text_format_ != nullptr); + + text_layout_ = nullptr; // release last one + + const auto dwrite_factory = + graph::GraphManager::GetInstance()->GetDWriteFactory(); + + const auto&& size = GetSize(); + + ThrowIfFailed(dwrite_factory->CreateTextLayout( + text_.c_str(), static_cast<UINT32>(text_.size()), text_format_.Get(), + size.width, size.height, &text_layout_)); +} +} // namespace cru::ui::render diff --git a/src/ui/render/text_render_object.hpp b/src/ui/render/text_render_object.hpp new file mode 100644 index 00000000..b868796d --- /dev/null +++ b/src/ui/render/text_render_object.hpp @@ -0,0 +1,71 @@ +#pragma once +#include "pre.hpp" + +#include "system_headers.hpp" + +#include "render_object.hpp" + +namespace cru::ui::render { +class TextRenderObject : public RenderObject { + public: + TextRenderObject(Microsoft::WRL::ComPtr<ID2D1Brush> brush, + Microsoft::WRL::ComPtr<IDWriteTextFormat> format, + Microsoft::WRL::ComPtr<ID2D1Brush> selection_brush); + TextRenderObject(const TextRenderObject& other) = delete; + TextRenderObject(TextRenderObject&& other) = delete; + TextRenderObject& operator=(const TextRenderObject& other) = delete; + TextRenderObject& operator=(TextRenderObject&& other) = delete; + ~TextRenderObject() override = default; + + String GetText() const { return text_; } + void SetText(String new_text) { text_ = std::move(new_text); } + + Microsoft::WRL::ComPtr<ID2D1Brush> GetBrush() const { return brush_; } + void SetBrush(Microsoft::WRL::ComPtr<ID2D1Brush> new_brush) { + brush_ = std::move(new_brush); + } + + Microsoft::WRL::ComPtr<IDWriteTextFormat> GetTextFormat() const { + return text_format_; + } + void SetTextFormat( + Microsoft::WRL::ComPtr<IDWriteTextFormat> new_text_format) { + text_format_ = std::move(new_text_format); + } + + std::optional<TextRange> GetSelectionRange() const { + return selection_range_; + } + void SetSelectionRange(std::optional<TextRange> new_range) { + selection_range_ = std::move(new_range); + } + + Microsoft::WRL::ComPtr<ID2D1Brush> GetSelectionBrush() const { + return selection_brush_; + } + void SetSelectionBrush(Microsoft::WRL::ComPtr<ID2D1Brush> new_brush) { + selection_brush_ = std::move(new_brush); + } + + void Draw(ID2D1RenderTarget* render_target) override; + + RenderObject* HitTest(const Point& point) override; + + protected: + Size OnMeasureContent(const Size& available_size) override; + void OnLayoutContent(const Rect& content_rect) override; + + private: + void RecreateTextLayout(); + + private: + String text_; + + Microsoft::WRL::ComPtr<ID2D1Brush> brush_; + Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_; + Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_; + + std::optional<TextRange> selection_range_ = std::nullopt; + Microsoft::WRL::ComPtr<ID2D1Brush> selection_brush_; +}; +} // namespace cru::ui::render |