From 7f1acd3ab4bddf6c61551ebc867667fbdd3ad7b8 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 20 Mar 2019 18:14:07 +0800 Subject: ... --- src/ui/render/text_render_object.cpp | 91 ++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/ui/render/text_render_object.cpp (limited to 'src/ui/render/text_render_object.cpp') 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 + +#include "exception.hpp" +#include "graph/graph.hpp" + +namespace cru::ui::render { +TextRenderObject::TextRenderObject( + Microsoft::WRL::ComPtr brush, + Microsoft::WRL::ComPtr format, + Microsoft::WRL::ComPtr 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 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 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(text_.size()), text_format_.Get(), + size.width, size.height, &text_layout_)); +} +} // namespace cru::ui::render -- cgit v1.2.3 From fdbb02b2cbdd4e4069005d0535a343229f7c4d32 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 21 Mar 2019 21:42:28 +0800 Subject: ... --- CruUI.vcxproj | 2 + CruUI.vcxproj.filters | 6 + src/main.cpp | 239 +----- src/pre.hpp | 4 - src/ui/control.hpp | 2 +- src/ui/controls/flex_layout.cpp | 4 + src/ui/controls/flex_layout.hpp | 10 +- src/ui/controls/text_block.cpp | 4 + src/ui/controls/text_block.hpp | 10 +- src/ui/d2d_util.hpp | 158 ++-- src/ui/events/ui_event.cpp | 6 - src/ui/events/ui_event.hpp | 507 +++++------- src/ui/input_util.cpp | 29 +- src/ui/input_util.hpp | 20 +- src/ui/render/flex_layout_render_object.cpp | 2 + src/ui/render/render_object.cpp | 5 +- src/ui/render/render_object.hpp | 8 +- src/ui/render/text_render_object.cpp | 6 + src/ui/render/text_render_object.hpp | 2 + src/ui/render/window_render_object.cpp | 48 ++ src/ui/render/window_render_object.hpp | 40 + src/ui/window.cpp | 1194 ++++++++++++--------------- src/ui/window.hpp | 33 +- 23 files changed, 1018 insertions(+), 1321 deletions(-) create mode 100644 src/ui/render/window_render_object.cpp create mode 100644 src/ui/render/window_render_object.hpp (limited to 'src/ui/render/text_render_object.cpp') diff --git a/CruUI.vcxproj b/CruUI.vcxproj index 2954ecc8..2e1359f9 100644 --- a/CruUI.vcxproj +++ b/CruUI.vcxproj @@ -118,6 +118,7 @@ + @@ -130,6 +131,7 @@ + diff --git a/CruUI.vcxproj.filters b/CruUI.vcxproj.filters index 35a97194..3bd5d30a 100644 --- a/CruUI.vcxproj.filters +++ b/CruUI.vcxproj.filters @@ -78,6 +78,9 @@ Source Files + + Source Files + @@ -164,6 +167,9 @@ Header Files + + Header Files + diff --git a/src/main.cpp b/src/main.cpp index 1773ce84..906528c4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,242 +1,43 @@ #include "pre.hpp" #include "application.hpp" -#include "ui/window.hpp" -#include "ui/controls/linear_layout.hpp" +#include "ui/controls/flex_layout.hpp" #include "ui/controls/text_block.hpp" -#include "ui/controls/toggle_button.hpp" -#include "ui/controls/button.hpp" -#include "ui/controls/text_box.hpp" -#include "ui/controls/list_item.hpp" -#include "ui/controls/popup_menu.hpp" -#include "ui/controls/frame_layout.hpp" -#include "ui/controls/scroll_control.hpp" -#include "graph/graph.hpp" +#include "ui/window.hpp" +using cru::Application; using cru::String; using cru::StringView; -using cru::Application; using cru::ui::Rect; -using cru::ui::Window; -using cru::ui::Alignment; -using cru::ui::LayoutSideParams; using cru::ui::Thickness; -using cru::ui::ControlList; -using cru::ui::CreateWithLayout; -using cru::ui::controls::LinearLayout; +using cru::ui::Window; +using cru::ui::controls::FlexLayout; using cru::ui::controls::TextBlock; -using cru::ui::controls::ToggleButton; -using cru::ui::controls::Button; -using cru::ui::controls::TextBox; -using cru::ui::controls::ListItem; -using cru::ui::controls::FrameLayout; -using cru::ui::controls::ScrollControl; - -int APIENTRY wWinMain( - HINSTANCE hInstance, - HINSTANCE hPrevInstance, - LPWSTR lpCmdLine, - int nCmdShow) { +int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, + LPWSTR lpCmdLine, int nCmdShow) { #ifdef CRU_DEBUG - _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); -#endif - - Application application(hInstance); - - const auto window = Window::CreateOverlapped(); - /* - window.native_message_event.AddHandler([](cru::ui::events::WindowNativeMessageEventArgs& args) - { - if (args.GetWindowMessage().msg == WM_PAINT) - { - OutputDebugStringW(L"Paint!\n"); - //args.SetResult(0); - } - }); - */ - /* - // test1 - cru::ui::controls::TextBlock text_block; - text_block.SetText(L"Hello world!"); - text_block.SetSize(cru::ui::Size(200, 30)); - window.AddChild(&text_block); - - std::array colors = - { - D2D1::ColorF(D2D1::ColorF::Blue), - D2D1::ColorF(D2D1::ColorF::Yellow), - D2D1::ColorF(D2D1::ColorF::Green), - D2D1::ColorF(D2D1::ColorF::Red) - }; - - std::random_device rd; // only used once to initialise (seed) engine - std::mt19937 rng(rd()); // random-number engine used (Mersenne-Twister in this case) - std::uniform_int_distribution uni(0, colors.size() - 1); // guaranteed unbiased - - - window.draw_event.AddHandler([&](cru::ui::events::DrawEventArgs& args) { - auto device_context = args.GetDeviceContext(); - - ID2D1SolidColorBrush* brush; - device_context->CreateSolidColorBrush(colors[uni(rng)], &brush); - - device_context->FillRectangle(D2D1::RectF(100.0f, 100.0f, 300.0f, 200.0f), brush); - - brush->Release(); - }); - - cru::SetTimeout(2.0, [&window]() { - window.InvalidateDraw(); - - auto task = cru::SetInterval(0.5, [&window]() { - window.InvalidateDraw(); - }); - - cru::SetTimeout(4, [task]() { - task->Cancel(); - task->Cancel(); // test for idempotency. - }); - }); - */ - - - //test 2 - const auto layout = CreateWithLayout(LayoutSideParams::Exactly(500), LayoutSideParams::Content()); - - layout->mouse_click_event.bubble.AddHandler([layout](cru::ui::events::MouseButtonEventArgs& args) - { - if (args.GetSender() == args.GetOriginalSender()) - layout->AddChild(TextBlock::Create(L"Layout is clicked!")); - }); - - { - const auto inner_layout = CreateWithLayout(LayoutSideParams::Content(Alignment::End), LayoutSideParams::Content(), LinearLayout::Orientation::Horizontal); - - inner_layout->AddChild(TextBlock::Create(L"Toggle debug border")); - - const auto l = FrameLayout::Create(); - l->GetLayoutParams()->padding.SetLeftRight(20.0f); - const auto toggle_button = ToggleButton::Create(); -#ifdef CRU_DEBUG_LAYOUT - toggle_button->toggle_event.AddHandler([&window](cru::ui::events::ToggleEventArgs& args) - { - window->SetDebugLayout(args.GetNewState()); - }); + _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); #endif - l->AddChild(toggle_button); - inner_layout->AddChild(l); - layout->AddChild(inner_layout); - } - - { - const auto button = Button::Create(); - button->GetLayoutParams()->padding = Thickness(20, 5); - button->SetChild(TextBlock::Create(L"Show popup window parenting this.")); - button->mouse_click_event.bubble.AddHandler([window, button](auto) - { - std::vector items; - items.emplace_back(L"Hello world!", []{}); - items.emplace_back(L"Item 2", []{}); - items.emplace_back(L"Close parent window.", [window]{ window->Close(); }); - - cru::ui::controls::CreatePopupMenu(window->PointToScreen(button->GetPositionAbsolute()), items, window)->Show(); - }); - layout->AddChild(button); - } - - { - const auto button = Button::Create(); - button->GetLayoutParams()->padding = Thickness(20, 5); - button->SetChild(TextBlock::Create(L"Show popup window parenting null.")); - button->SetBackgroundBrush(cru::graph::CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Gold))); - button->mouse_click_event.bubble.AddHandler([](auto) - { - auto popup = Window::CreatePopup(nullptr); - popup->SetWindowRect(Rect(100, 100, 300, 300)); - popup->Show(); - }); - layout->AddChild(button); - } - - { - const auto button = Button::Create(); - button->GetLayoutParams()->padding = Thickness(20, 5); - button->SetChild(TextBlock::Create(L"Show popup window with caption.")); - button->mouse_click_event.bubble.AddHandler([](auto) - { - auto popup = Window::CreatePopup(nullptr, true); - popup->SetWindowRect(Rect(100, 100, 300, 300)); - popup->Show(); - }); - layout->AddChild(button); - } - - { - const auto text_block = CreateWithLayout(LayoutSideParams::Exactly(200), LayoutSideParams::Exactly(80), L"Hello World!!!"); - - text_block->mouse_click_event.bubble.AddHandler([layout](cru::ui::events::MouseButtonEventArgs& args) - { - layout->AddChild(TextBlock::Create(L"Hello world is clicked!")); - }); - - layout->AddChild(text_block); - } - - { - const auto text_box = TextBox::Create(); - text_box->GetLayoutParams()->width.min = 50.0f; - text_box->GetLayoutParams()->width.max = 100.0f; - text_box->char_event.tunnel.AddHandler([](cru::ui::events::CharEventArgs& args) - { - if (args.GetChar() == L'1') - args.SetHandled(); - }); - layout->AddChild(text_box); - } - - { - const auto scroll_view = CreateWithLayout(LayoutSideParams::Stretch(), LayoutSideParams::Stretch()); - - scroll_view->SetVerticalScrollBarVisibility(ScrollControl::ScrollBarVisibility::Always); - - const auto text_block = TextBlock::Create( - L"Love myself I do. Not everything, but I love the good as well as the bad. I love my crazy lifestyle, and I love my hard discipline. I love my freedom of speech and the way my eyes get dark when I'm tired. I love that I have learned to trust people with my heart, even if it will get broken. I am proud of everything that I am and will become."); - text_block->SetSelectable(true); - - scroll_view->SetChild(text_block); - layout->AddChild(scroll_view); - } - layout->AddChild(CreateWithLayout(LayoutSideParams::Content(Alignment::Start), LayoutSideParams::Content(), L"This is a little short sentence!!!")); - layout->AddChild(CreateWithLayout(LayoutSideParams::Content(Alignment::End), LayoutSideParams::Stretch(), L"By crupest!!!")); + Application application(hInstance); + const auto window = Window::CreateOverlapped(); - window->SetChild(layout); + const auto flex_layout = FlexLayout::Create(); - /* - window.AddChild( - CreateWithLayout(LayoutSideParams::Exactly(200), LayoutSideParams::Content(), - std::initializer_list{ - CreateWithLayout(LayoutSideParams::Stretch(), LayoutSideParams::Content()) - } - )); - */ + window->SetChild(flex_layout); - /* test 3 - const auto linear_layout = CreateWithLayout(Thickness(50, 50), Thickness(50, 50), LinearLayout::Orientation::Vertical, ControlList{ - Button::Create({ - TextBlock::Create(L"Button") - }), - CreateWithLayout(Thickness(30), Thickness(20)) - }); + const auto text_block1 = TextBlock::Create(); + text_block1->SetText(L"Hello World!"); + flex_layout->AddChild(text_block1, 0); - linear_layout->SetBordered(true); + const auto text_block2 = TextBlock::Create(); + text_block2->SetText(L"Hello World!"); + flex_layout->AddChild(text_block2, 1); - window.AddChild(linear_layout); - */ - window->Show(); + window->Show(); - return application.Run(); + return application.Run(); } diff --git a/src/pre.hpp b/src/pre.hpp index ba47dc6c..dfa0666c 100644 --- a/src/pre.hpp +++ b/src/pre.hpp @@ -4,10 +4,6 @@ #define CRU_DEBUG #endif -#ifdef CRU_DEBUG -#define CRU_DEBUG_LAYOUT -#endif - #ifdef CRU_DEBUG #define _CRTDBG_MAP_ALLOC #include diff --git a/src/ui/control.hpp b/src/ui/control.hpp index 5f8ac02a..9ff407b0 100644 --- a/src/ui/control.hpp +++ b/src/ui/control.hpp @@ -207,7 +207,7 @@ void DispatchEvent(Control* const original_sender, auto parent = original_sender; while (parent != last_receiver) { receive_list.push_back(parent); - parent = parent->GetInternalParent(); + parent = parent->GetParent(); } auto handled = false; diff --git a/src/ui/controls/flex_layout.cpp b/src/ui/controls/flex_layout.cpp index ebe61a6d..289df1f1 100644 --- a/src/ui/controls/flex_layout.cpp +++ b/src/ui/controls/flex_layout.cpp @@ -9,6 +9,10 @@ FlexLayout::FlexLayout() { render_object_ = new FlexLayoutRenderObject(); } FlexLayout::~FlexLayout() { delete render_object_; } +render::RenderObject* FlexLayout::GetRenderObject() const { + return render_object_; +} + void FlexLayout::OnAddChild(Control* child, int position) { render_object_->AddChild(child->GetRenderObject(), position); } diff --git a/src/ui/controls/flex_layout.hpp b/src/ui/controls/flex_layout.hpp index 6acd25dc..682ed8dc 100644 --- a/src/ui/controls/flex_layout.hpp +++ b/src/ui/controls/flex_layout.hpp @@ -13,8 +13,12 @@ class FlexLayout : public Layout { public: static constexpr auto control_type = L"FlexLayout"; - public: + static FlexLayout* Create() { return new FlexLayout(); } + + protected: FlexLayout(); + + public: FlexLayout(const FlexLayout& other) = delete; FlexLayout(FlexLayout&& other) = delete; FlexLayout& operator=(const FlexLayout& other) = delete; @@ -23,9 +27,7 @@ class FlexLayout : public Layout { StringView GetControlType() const override final { return control_type; } - render::RenderObject* GetRenderObject() const override { - return render_object_; - } + render::RenderObject* GetRenderObject() const override; protected: void OnAddChild(Control* child, int position) override; diff --git a/src/ui/controls/text_block.cpp b/src/ui/controls/text_block.cpp index 123dbc86..c891b832 100644 --- a/src/ui/controls/text_block.cpp +++ b/src/ui/controls/text_block.cpp @@ -17,6 +17,10 @@ TextBlock::TextBlock() { TextBlock::~TextBlock() { delete render_object_; } +render::RenderObject* TextBlock::GetRenderObject() const { + return render_object_; +} + String TextBlock::GetText() const { return render_object_->GetText(); } void TextBlock::SetText(const String& text) { render_object_->SetText(text); } diff --git a/src/ui/controls/text_block.hpp b/src/ui/controls/text_block.hpp index ce8977a5..c345c5ab 100644 --- a/src/ui/controls/text_block.hpp +++ b/src/ui/controls/text_block.hpp @@ -12,8 +12,12 @@ class TextBlock : public NoChildControl { public: static constexpr auto control_type = L"TextBlock"; - public: + static TextBlock* Create() { return new TextBlock(); } + + protected: TextBlock(); + + public: TextBlock(const TextBlock& other) = delete; TextBlock(TextBlock&& other) = delete; TextBlock& operator=(const TextBlock& other) = delete; @@ -22,9 +26,7 @@ class TextBlock : public NoChildControl { StringView GetControlType() const override final { return control_type; } - render::RenderObject* GetRenderObject() const override { - return render_object_; - } + render::RenderObject* GetRenderObject() const override; String GetText() const; void SetText(const String& text); diff --git a/src/ui/d2d_util.hpp b/src/ui/d2d_util.hpp index 94ef6223..96a017dc 100644 --- a/src/ui/d2d_util.hpp +++ b/src/ui/d2d_util.hpp @@ -5,88 +5,78 @@ #include "ui_base.hpp" -namespace cru::ui -{ - inline D2D1_POINT_2F Convert(const Point& point) - { - return D2D1::Point2F(point.x, point.y); - } - - inline D2D1_RECT_F Convert(const Rect& rect) - { - return D2D1::RectF(rect.left, rect.top, rect.left + rect.width, rect.top + rect.height); - } - - inline D2D1_ROUNDED_RECT Convert(const RoundedRect& rounded_rect) - { - return D2D1::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radius_x, rounded_rect.radius_y); - } - - inline D2D1_ELLIPSE Convert(const Ellipse& ellipse) - { - return D2D1::Ellipse(Convert(ellipse.center), ellipse.radius_x, ellipse.radius_y); - } - - inline Point Convert(const D2D1_POINT_2F& point) - { - return Point(point.x, point.y); - } - - inline Rect Convert(const D2D1_RECT_F& rect) - { - return Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top); - } - - inline RoundedRect Convert(const D2D1_ROUNDED_RECT& rounded_rect) - { - return RoundedRect(Convert(rounded_rect.rect), rounded_rect.radiusX, rounded_rect.radiusY); - } - - inline Ellipse Convert(const D2D1_ELLIPSE& ellipse) - { - return Ellipse(Convert(ellipse.point), ellipse.radiusX, ellipse.radiusY); - } - - inline bool operator==(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) - { - return left.x == right.x && left.y == right.y; - } - - inline bool operator!=(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) - { - return !(left == right); - } - - inline bool operator==(const D2D1_RECT_F& left, const D2D1_RECT_F& right) - { - return left.left == right.left && - left.top == right.top && - left.right == right.right && - left.bottom == right.bottom; - } - - inline bool operator!=(const D2D1_RECT_F& left, const D2D1_RECT_F& right) - { - return !(left == right); - } - - inline bool operator==(const D2D1_ROUNDED_RECT& left, const D2D1_ROUNDED_RECT& right) - { - return left.rect == right.rect && left.radiusX == right.radiusX && left.radiusY == right.radiusY; - } - - inline bool operator!=(const D2D1_ROUNDED_RECT& left, const D2D1_ROUNDED_RECT& right) - { - return !(left == right); - } - - inline bool operator == (const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) - { - return left.point == right.point && left.radiusX == right.radiusX && left.radiusY == right.radiusY; - } - - inline bool operator!=(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) - { - return !(left == right); - } +namespace cru::ui { +inline D2D1_POINT_2F Convert(const Point& point) { + return D2D1::Point2F(point.x, point.y); } + +inline D2D1_RECT_F Convert(const Rect& rect) { + return D2D1::RectF(rect.left, rect.top, rect.left + rect.width, + rect.top + rect.height); +} + +inline D2D1_ROUNDED_RECT Convert(const RoundedRect& rounded_rect) { + return D2D1::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radius_x, + rounded_rect.radius_y); +} + +inline D2D1_ELLIPSE Convert(const Ellipse& ellipse) { + return D2D1::Ellipse(Convert(ellipse.center), ellipse.radius_x, + ellipse.radius_y); +} + +inline Point Convert(const D2D1_POINT_2F& point) { + return Point(point.x, point.y); +} + +inline Rect Convert(const D2D1_RECT_F& rect) { + return Rect(rect.left, rect.top, rect.right - rect.left, + rect.bottom - rect.top); +} + +inline RoundedRect Convert(const D2D1_ROUNDED_RECT& rounded_rect) { + return RoundedRect(Convert(rounded_rect.rect), rounded_rect.radiusX, + rounded_rect.radiusY); +} + +inline Ellipse Convert(const D2D1_ELLIPSE& ellipse) { + return Ellipse(Convert(ellipse.point), ellipse.radiusX, ellipse.radiusY); +} + +inline bool operator==(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { + return left.x == right.x && left.y == right.y; +} + +inline bool operator!=(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { + return left.left == right.left && left.top == right.top && + left.right == right.right && left.bottom == right.bottom; +} + +inline bool operator!=(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_ROUNDED_RECT& left, + const D2D1_ROUNDED_RECT& right) { + return left.rect == right.rect && left.radiusX == right.radiusX && + left.radiusY == right.radiusY; +} + +inline bool operator!=(const D2D1_ROUNDED_RECT& left, + const D2D1_ROUNDED_RECT& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { + return left.point == right.point && left.radiusX == right.radiusX && + left.radiusY == right.radiusY; +} + +inline bool operator!=(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { + return !(left == right); +} +} // namespace cru::ui diff --git a/src/ui/events/ui_event.cpp b/src/ui/events/ui_event.cpp index a1fc3d82..ee3a68dc 100644 --- a/src/ui/events/ui_event.cpp +++ b/src/ui/events/ui_event.cpp @@ -4,10 +4,4 @@ namespace cru::ui::events { - Point MouseEventArgs::GetPoint(Control* control, const RectRange range) const - { - if (point_.has_value()) - return control->TransformPoint(control->WindowToControl(point_.value()), RectRange::Margin, range); - return Point(); - } } diff --git a/src/ui/events/ui_event.hpp b/src/ui/events/ui_event.hpp index e0040942..572cf8d6 100644 --- a/src/ui/events/ui_event.hpp +++ b/src/ui/events/ui_event.hpp @@ -1,301 +1,228 @@ #pragma once - -// ReSharper disable once CppUnusedIncludeDirective #include "pre.hpp" -#include "system_headers.hpp" #include +#include "system_headers.hpp" #include "base.hpp" #include "cru_event.hpp" #include "ui/ui_base.hpp" -#include "ui/layout_base.hpp" +#include "ui/input_util.hpp" -namespace cru::ui -{ - class Control; +namespace cru::ui { +class Control; } -namespace cru::ui::events -{ - class UiEventArgs : public BasicEventArgs - { - public: - UiEventArgs(Object* sender, Object* original_sender) - : BasicEventArgs(sender), original_sender_(original_sender), handled_(false) - { - - } - - UiEventArgs(const UiEventArgs& other) = default; - UiEventArgs(UiEventArgs&& other) = default; - UiEventArgs& operator=(const UiEventArgs& other) = default; - UiEventArgs& operator=(UiEventArgs&& other) = default; - ~UiEventArgs() override = default; - - Object* GetOriginalSender() const - { - return original_sender_; - } - - bool IsHandled() const - { - return handled_; - } - - void SetHandled(const bool handled = true) - { - handled_ = handled; - } - - private: - Object* original_sender_; - bool handled_; - }; - - template - class RoutedEvent - { - public: - static_assert(std::is_base_of_v, "TEventArgs must be subclass of UiEventArgs."); - - using EventArgs = TEventArgs; - - RoutedEvent() = default; - RoutedEvent(const RoutedEvent& other) = delete; - RoutedEvent(RoutedEvent&& other) = delete; - RoutedEvent& operator=(const RoutedEvent& other) = delete; - RoutedEvent& operator=(RoutedEvent&& other) = delete; - ~RoutedEvent() = default; - - Event direct; - Event bubble; - Event tunnel; - }; - - class MouseEventArgs : public UiEventArgs - { - public: - MouseEventArgs(Object* sender, Object* original_sender, const std::optional& point = std::nullopt) - : UiEventArgs(sender, original_sender), point_(point) - { - - } - MouseEventArgs(const MouseEventArgs& other) = default; - MouseEventArgs(MouseEventArgs&& other) = default; - MouseEventArgs& operator=(const MouseEventArgs& other) = default; - MouseEventArgs& operator=(MouseEventArgs&& other) = default; - ~MouseEventArgs() override = default; - - Point GetPoint(Control* control, RectRange range = RectRange::Content) const; - - private: - std::optional point_; - }; - - - class MouseButtonEventArgs : public MouseEventArgs - { - public: - MouseButtonEventArgs(Object* sender, Object* original_sender, const Point& point, const MouseButton button) - : MouseEventArgs(sender, original_sender, point), button_(button) - { - - } - MouseButtonEventArgs(const MouseButtonEventArgs& other) = default; - MouseButtonEventArgs(MouseButtonEventArgs&& other) = default; - MouseButtonEventArgs& operator=(const MouseButtonEventArgs& other) = default; - MouseButtonEventArgs& operator=(MouseButtonEventArgs&& other) = default; - ~MouseButtonEventArgs() override = default; - - MouseButton GetMouseButton() const - { - return button_; - } - - private: - MouseButton button_; - }; - - - class MouseWheelEventArgs : public MouseEventArgs - { - public: - MouseWheelEventArgs(Object* sender, Object* original_sender, const Point& point, const float delta) - : MouseEventArgs(sender, original_sender, point), delta_(delta) - { - - } - MouseWheelEventArgs(const MouseWheelEventArgs& other) = default; - MouseWheelEventArgs(MouseWheelEventArgs&& other) = default; - MouseWheelEventArgs& operator=(const MouseWheelEventArgs& other) = default; - MouseWheelEventArgs& operator=(MouseWheelEventArgs&& other) = default; - ~MouseWheelEventArgs() override = default; - - float GetDelta() const - { - return delta_; - } - - private: - float delta_; - }; - - - class DrawEventArgs : public UiEventArgs - { - public: - DrawEventArgs(Object* sender, Object* original_sender, ID2D1DeviceContext* device_context) - : UiEventArgs(sender, original_sender), device_context_(device_context) - { - - } - DrawEventArgs(const DrawEventArgs& other) = default; - DrawEventArgs(DrawEventArgs&& other) = default; - DrawEventArgs& operator=(const DrawEventArgs& other) = default; - DrawEventArgs& operator=(DrawEventArgs&& other) = default; - ~DrawEventArgs() = default; - - ID2D1DeviceContext* GetDeviceContext() const - { - return device_context_; - } - - private: - ID2D1DeviceContext * device_context_; - }; - - - class FocusChangeEventArgs : public UiEventArgs - { - public: - FocusChangeEventArgs(Object* sender, Object* original_sender, const bool is_window = false) - : UiEventArgs(sender, original_sender), is_window_(is_window) - { - - } - FocusChangeEventArgs(const FocusChangeEventArgs& other) = default; - FocusChangeEventArgs(FocusChangeEventArgs&& other) = default; - FocusChangeEventArgs& operator=(const FocusChangeEventArgs& other) = default; - FocusChangeEventArgs& operator=(FocusChangeEventArgs&& other) = default; - ~FocusChangeEventArgs() override = default; - - // Return whether the focus change is caused by the window-wide focus change. - bool IsWindow() const - { - return is_window_; - } - - private: - bool is_window_; - }; - - class ToggleEventArgs : public UiEventArgs - { - public: - ToggleEventArgs(Object* sender, Object* original_sender, bool new_state) - : UiEventArgs(sender, original_sender), new_state_(new_state) - { - - } - ToggleEventArgs(const ToggleEventArgs& other) = default; - ToggleEventArgs(ToggleEventArgs&& other) = default; - ToggleEventArgs& operator=(const ToggleEventArgs& other) = default; - ToggleEventArgs& operator=(ToggleEventArgs&& other) = default; - ~ToggleEventArgs() override = default; - - bool GetNewState() const - { - return new_state_; - } - - private: - bool new_state_; - }; - - struct WindowNativeMessage - { - HWND hwnd; - int msg; - WPARAM w_param; - LPARAM l_param; - }; - - class WindowNativeMessageEventArgs : public UiEventArgs - { - public: - WindowNativeMessageEventArgs(Object* sender, Object* original_sender, const WindowNativeMessage& message) - : UiEventArgs(sender, original_sender), message_(message), result_(std::nullopt) - { - - } - WindowNativeMessageEventArgs(const WindowNativeMessageEventArgs& other) = default; - WindowNativeMessageEventArgs(WindowNativeMessageEventArgs&& other) = default; - WindowNativeMessageEventArgs& operator=(const WindowNativeMessageEventArgs& other) = default; - WindowNativeMessageEventArgs& operator=(WindowNativeMessageEventArgs&& other) = default; - ~WindowNativeMessageEventArgs() override = default; - - WindowNativeMessage GetWindowMessage() const - { - return message_; - } - - std::optional GetResult() const - { - return result_; - } - - void SetResult(const std::optional result) - { - result_ = result; - } - - private: - WindowNativeMessage message_; - std::optional result_; - }; - - class KeyEventArgs : public UiEventArgs - { - public: - KeyEventArgs(Object* sender, Object* original_sender, int virtual_code) - : UiEventArgs(sender, original_sender), virtual_code_(virtual_code) - { - } - KeyEventArgs(const KeyEventArgs& other) = default; - KeyEventArgs(KeyEventArgs&& other) = default; - KeyEventArgs& operator=(const KeyEventArgs& other) = default; - KeyEventArgs& operator=(KeyEventArgs&& other) = default; - ~KeyEventArgs() override = default; - - int GetVirtualCode() const - { - return virtual_code_; - } - - private: - int virtual_code_; - }; - - class CharEventArgs : public UiEventArgs - { - public: - CharEventArgs(Object* sender, Object* original_sender, wchar_t c) - : UiEventArgs(sender, original_sender), c_(c) - { - } - CharEventArgs(const CharEventArgs& other) = default; - CharEventArgs(CharEventArgs&& other) = default; - CharEventArgs& operator=(const CharEventArgs& other) = default; - CharEventArgs& operator=(CharEventArgs&& other) = default; - ~CharEventArgs() override = default; - - wchar_t GetChar() const - { - return c_; - } - - private: - wchar_t c_; - }; -} +namespace cru::ui::events { +class UiEventArgs : public BasicEventArgs { + public: + UiEventArgs(Object* sender, Object* original_sender) + : BasicEventArgs(sender), + original_sender_(original_sender), + handled_(false) {} + + UiEventArgs(const UiEventArgs& other) = default; + UiEventArgs(UiEventArgs&& other) = default; + UiEventArgs& operator=(const UiEventArgs& other) = default; + UiEventArgs& operator=(UiEventArgs&& other) = default; + ~UiEventArgs() override = default; + + Object* GetOriginalSender() const { return original_sender_; } + + bool IsHandled() const { return handled_; } + + void SetHandled(const bool handled = true) { handled_ = handled; } + + private: + Object* original_sender_; + bool handled_; +}; + +template +class RoutedEvent { + public: + static_assert(std::is_base_of_v, + "TEventArgs must be subclass of UiEventArgs."); + + using EventArgs = TEventArgs; + + RoutedEvent() = default; + RoutedEvent(const RoutedEvent& other) = delete; + RoutedEvent(RoutedEvent&& other) = delete; + RoutedEvent& operator=(const RoutedEvent& other) = delete; + RoutedEvent& operator=(RoutedEvent&& other) = delete; + ~RoutedEvent() = default; + + Event direct; + Event bubble; + Event tunnel; +}; + +class MouseEventArgs : public UiEventArgs { + public: + MouseEventArgs(Object* sender, Object* original_sender, + const std::optional& point = std::nullopt) + : UiEventArgs(sender, original_sender), point_(point) {} + MouseEventArgs(const MouseEventArgs& other) = default; + MouseEventArgs(MouseEventArgs&& other) = default; + MouseEventArgs& operator=(const MouseEventArgs& other) = default; + MouseEventArgs& operator=(MouseEventArgs&& other) = default; + ~MouseEventArgs() override = default; + + Point GetPoint() const { return point_.value_or(Point::Zero()); } + + private: + std::optional point_; +}; + +class MouseButtonEventArgs : public MouseEventArgs { + public: + MouseButtonEventArgs(Object* sender, Object* original_sender, + const Point& point, const MouseButton button) + : MouseEventArgs(sender, original_sender, point), button_(button) {} + MouseButtonEventArgs(const MouseButtonEventArgs& other) = default; + MouseButtonEventArgs(MouseButtonEventArgs&& other) = default; + MouseButtonEventArgs& operator=(const MouseButtonEventArgs& other) = default; + MouseButtonEventArgs& operator=(MouseButtonEventArgs&& other) = default; + ~MouseButtonEventArgs() override = default; + + MouseButton GetMouseButton() const { return button_; } + + private: + MouseButton button_; +}; + +class MouseWheelEventArgs : public MouseEventArgs { + public: + MouseWheelEventArgs(Object* sender, Object* original_sender, + const Point& point, const float delta) + : MouseEventArgs(sender, original_sender, point), delta_(delta) {} + MouseWheelEventArgs(const MouseWheelEventArgs& other) = default; + MouseWheelEventArgs(MouseWheelEventArgs&& other) = default; + MouseWheelEventArgs& operator=(const MouseWheelEventArgs& other) = default; + MouseWheelEventArgs& operator=(MouseWheelEventArgs&& other) = default; + ~MouseWheelEventArgs() override = default; + + float GetDelta() const { return delta_; } + + private: + float delta_; +}; + +class DrawEventArgs : public UiEventArgs { + public: + DrawEventArgs(Object* sender, Object* original_sender, + ID2D1DeviceContext* device_context) + : UiEventArgs(sender, original_sender), device_context_(device_context) {} + DrawEventArgs(const DrawEventArgs& other) = default; + DrawEventArgs(DrawEventArgs&& other) = default; + DrawEventArgs& operator=(const DrawEventArgs& other) = default; + DrawEventArgs& operator=(DrawEventArgs&& other) = default; + ~DrawEventArgs() = default; + + ID2D1DeviceContext* GetDeviceContext() const { return device_context_; } + + private: + ID2D1DeviceContext* device_context_; +}; + +class FocusChangeEventArgs : public UiEventArgs { + public: + FocusChangeEventArgs(Object* sender, Object* original_sender, + const bool is_window = false) + : UiEventArgs(sender, original_sender), is_window_(is_window) {} + FocusChangeEventArgs(const FocusChangeEventArgs& other) = default; + FocusChangeEventArgs(FocusChangeEventArgs&& other) = default; + FocusChangeEventArgs& operator=(const FocusChangeEventArgs& other) = default; + FocusChangeEventArgs& operator=(FocusChangeEventArgs&& other) = default; + ~FocusChangeEventArgs() override = default; + + // Return whether the focus change is caused by the window-wide focus change. + bool IsWindow() const { return is_window_; } + + private: + bool is_window_; +}; + +class ToggleEventArgs : public UiEventArgs { + public: + ToggleEventArgs(Object* sender, Object* original_sender, bool new_state) + : UiEventArgs(sender, original_sender), new_state_(new_state) {} + ToggleEventArgs(const ToggleEventArgs& other) = default; + ToggleEventArgs(ToggleEventArgs&& other) = default; + ToggleEventArgs& operator=(const ToggleEventArgs& other) = default; + ToggleEventArgs& operator=(ToggleEventArgs&& other) = default; + ~ToggleEventArgs() override = default; + + bool GetNewState() const { return new_state_; } + + private: + bool new_state_; +}; + +struct WindowNativeMessage { + HWND hwnd; + int msg; + WPARAM w_param; + LPARAM l_param; +}; + +class WindowNativeMessageEventArgs : public UiEventArgs { + public: + WindowNativeMessageEventArgs(Object* sender, Object* original_sender, + const WindowNativeMessage& message) + : UiEventArgs(sender, original_sender), + message_(message), + result_(std::nullopt) {} + WindowNativeMessageEventArgs(const WindowNativeMessageEventArgs& other) = + default; + WindowNativeMessageEventArgs(WindowNativeMessageEventArgs&& other) = default; + WindowNativeMessageEventArgs& operator=( + const WindowNativeMessageEventArgs& other) = default; + WindowNativeMessageEventArgs& operator=( + WindowNativeMessageEventArgs&& other) = default; + ~WindowNativeMessageEventArgs() override = default; + + WindowNativeMessage GetWindowMessage() const { return message_; } + + std::optional GetResult() const { return result_; } + + void SetResult(const std::optional result) { result_ = result; } + + private: + WindowNativeMessage message_; + std::optional result_; +}; + +class KeyEventArgs : public UiEventArgs { + public: + KeyEventArgs(Object* sender, Object* original_sender, int virtual_code) + : UiEventArgs(sender, original_sender), virtual_code_(virtual_code) {} + KeyEventArgs(const KeyEventArgs& other) = default; + KeyEventArgs(KeyEventArgs&& other) = default; + KeyEventArgs& operator=(const KeyEventArgs& other) = default; + KeyEventArgs& operator=(KeyEventArgs&& other) = default; + ~KeyEventArgs() override = default; + + int GetVirtualCode() const { return virtual_code_; } + + private: + int virtual_code_; +}; + +class CharEventArgs : public UiEventArgs { + public: + CharEventArgs(Object* sender, Object* original_sender, wchar_t c) + : UiEventArgs(sender, original_sender), c_(c) {} + CharEventArgs(const CharEventArgs& other) = default; + CharEventArgs(CharEventArgs&& other) = default; + CharEventArgs& operator=(const CharEventArgs& other) = default; + CharEventArgs& operator=(CharEventArgs&& other) = default; + ~CharEventArgs() override = default; + + wchar_t GetChar() const { return c_; } + + private: + wchar_t c_; +}; +} // namespace cru::ui::events diff --git a/src/ui/input_util.cpp b/src/ui/input_util.cpp index 6cf2d695..3fe34f10 100644 --- a/src/ui/input_util.cpp +++ b/src/ui/input_util.cpp @@ -2,22 +2,19 @@ #include "system_headers.hpp" -namespace cru::ui -{ - bool IsKeyDown(const int virtual_code) - { - const auto result = ::GetKeyState(virtual_code); - return (static_cast(result) & 0x8000) != 0; - } +namespace cru::ui { +bool IsKeyDown(const int virtual_code) { + const auto result = ::GetKeyState(virtual_code); + return (static_cast(result) & 0x8000) != 0; +} - bool IsKeyToggled(const int virtual_code) - { - const auto result = ::GetKeyState(virtual_code); - return (static_cast(result) & 1) != 0; - } +bool IsKeyToggled(const int virtual_code) { + const auto result = ::GetKeyState(virtual_code); + return (static_cast(result) & 1) != 0; +} - bool IsAnyMouseButtonDown() - { - return IsKeyDown(VK_LBUTTON) || IsKeyDown(VK_RBUTTON) || IsKeyDown(VK_MBUTTON); - } +bool IsAnyMouseButtonDown() { + return IsKeyDown(VK_LBUTTON) || IsKeyDown(VK_RBUTTON) || + IsKeyDown(VK_MBUTTON); } +} // namespace cru::ui diff --git a/src/ui/input_util.hpp b/src/ui/input_util.hpp index 13f568b3..2d01f725 100644 --- a/src/ui/input_util.hpp +++ b/src/ui/input_util.hpp @@ -1,18 +1,10 @@ #pragma once - -// ReSharper disable once CppUnusedIncludeDirective #include "pre.hpp" -namespace cru::ui -{ - enum class MouseButton - { - Left, - Right, - Middle - }; +namespace cru::ui { +enum class MouseButton { Left, Right, Middle }; - bool IsKeyDown(int virtual_code); - bool IsKeyToggled(int virtual_code); - bool IsAnyMouseButtonDown(); -} +bool IsKeyDown(int virtual_code); +bool IsKeyToggled(int virtual_code); +bool IsAnyMouseButtonDown(); +} // namespace cru::ui diff --git a/src/ui/render/flex_layout_render_object.cpp b/src/ui/render/flex_layout_render_object.cpp index 58e7e338..7891906d 100644 --- a/src/ui/render/flex_layout_render_object.cpp +++ b/src/ui/render/flex_layout_render_object.cpp @@ -162,6 +162,8 @@ void FlexLayoutRenderObject::OnLayoutContent(const Rect& content_rect) { return start_point + (total_length - content_length) / 2.0f; case Alignment::End: return start_point + total_length - content_length; + default: + UnreachableCode(); } }; diff --git a/src/ui/render/render_object.cpp b/src/ui/render/render_object.cpp index 0a0e693c..0be44c1e 100644 --- a/src/ui/render/render_object.cpp +++ b/src/ui/render/render_object.cpp @@ -51,6 +51,8 @@ void RenderObject::OnAddChild(RenderObject* new_child, int position) {} void RenderObject::OnRemoveChild(RenderObject* removed_child, int position) {} +void RenderObject::OnSizeChanged(const Size& old_size, const Size& new_size) {} + void RenderObject::SetParent(RenderObject* new_parent) { const auto old_parent = parent_; parent_ = new_parent; @@ -80,8 +82,7 @@ void RenderObject::OnMeasureCore(const Size& available_size) { const auto actual_content_size = OnMeasureContent(coerced_content_available_size); - SetPreferredSize(margin_padding_size + content_available_size + - actual_content_size); + SetPreferredSize(margin_padding_size + actual_content_size); } void RenderObject::OnLayoutCore(const Rect& rect) { diff --git a/src/ui/render/render_object.hpp b/src/ui/render/render_object.hpp index 34f5dcee..a9950198 100644 --- a/src/ui/render/render_object.hpp +++ b/src/ui/render/render_object.hpp @@ -37,7 +37,11 @@ class RenderObject : public Object { Point GetOffset() const { return offset_; } void SetOffset(const Point& offset) { offset_ = offset; } Size GetSize() const { return size_; } - void SetSize(const Size& size) { size_ = size; } + void SetSize(const Size& size) { + const auto old_size = size_; + size_ = size; + OnSizeChanged(old_size, size); + } Thickness GetMargin() const { return margin_; } void SetMargin(const Thickness& margin) { margin_ = margin; } @@ -64,6 +68,8 @@ class RenderObject : public Object { virtual void OnAddChild(RenderObject* new_child, int position); virtual void OnRemoveChild(RenderObject* removed_child, int position); + virtual void OnSizeChanged(const Size& old_size, const Size& new_size); + virtual Size OnMeasureContent(const Size& available_size) = 0; virtual void OnLayoutContent(const Rect& content_rect) = 0; diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp index 43724e9f..e8032f78 100644 --- a/src/ui/render/text_render_object.cpp +++ b/src/ui/render/text_render_object.cpp @@ -62,6 +62,12 @@ RenderObject* TextRenderObject::HitTest(const Point& point) { return Rect{Point::Zero(), GetSize()}.IsPointInside(point) ? this : nullptr; } +void TextRenderObject::OnSizeChanged(const Size& old_size, + const Size& new_size) { + ThrowIfFailed(text_layout_->SetMaxWidth(new_size.width)); + ThrowIfFailed(text_layout_->SetMaxHeight(new_size.height)); +} + Size TextRenderObject::OnMeasureContent(const Size& available_size) { ThrowIfFailed(text_layout_->SetMaxWidth(available_size.width)); ThrowIfFailed(text_layout_->SetMaxHeight(available_size.height)); diff --git a/src/ui/render/text_render_object.hpp b/src/ui/render/text_render_object.hpp index d1d91034..4361b9c0 100644 --- a/src/ui/render/text_render_object.hpp +++ b/src/ui/render/text_render_object.hpp @@ -56,6 +56,8 @@ class TextRenderObject : public RenderObject { RenderObject* HitTest(const Point& point) override; protected: + void OnSizeChanged(const Size& old_size, const Size& new_size) override; + Size OnMeasureContent(const Size& available_size) override; void OnLayoutContent(const Rect& content_rect) override; diff --git a/src/ui/render/window_render_object.cpp b/src/ui/render/window_render_object.cpp new file mode 100644 index 00000000..52452bd4 --- /dev/null +++ b/src/ui/render/window_render_object.cpp @@ -0,0 +1,48 @@ +#include "window_render_object.hpp" + +#include + +#include "graph/graph.hpp" +#include "ui/window.hpp" + +namespace cru::ui::render { +void WindowRenderObject::MeasureAndLayout() { + const auto client_size = window_->GetClientSize(); + Measure(client_size); + Layout(Rect{Point::Zero(), client_size}); +} + +void WindowRenderObject::Draw(ID2D1RenderTarget* render_target) { + if (const auto child = GetChild()) { + auto offset = child->GetOffset(); + graph::WithTransform(render_target, + D2D1::Matrix3x2F::Translation(offset.x, offset.y), + [child](auto rt) { child->Draw(rt); }); + } +} + +RenderObject* WindowRenderObject::HitTest(const Point& point) { + if (const auto child = GetChild()) { + auto offset = child->GetOffset(); + Point p{point.x - offset.x, point.y - offset.y}; + const auto result = child->HitTest(point); + if (result != nullptr) { + return result; + } + } + return Rect{Point::Zero(), GetSize()}.IsPointInside(point) ? this : nullptr; +} + +void WindowRenderObject::OnAddChild(RenderObject* new_child, int position) { + assert(GetChildren().size() == 1); +} + +Size WindowRenderObject::OnMeasureContent(const Size& available_size) { + if (const auto child = GetChild()) child->Measure(available_size); + return available_size; +} + +void WindowRenderObject::OnLayoutContent(const Rect& content_rect) { + if (const auto child = GetChild()) child->Layout(content_rect); +} +} // namespace cru::ui::render diff --git a/src/ui/render/window_render_object.hpp b/src/ui/render/window_render_object.hpp new file mode 100644 index 00000000..63eb8588 --- /dev/null +++ b/src/ui/render/window_render_object.hpp @@ -0,0 +1,40 @@ +#pragma once +#include "pre.hpp" + +#include "render_object.hpp" + +namespace cru::ui { +class Window; +} + +namespace cru::ui::render { +class WindowRenderObject : public RenderObject { + public: + WindowRenderObject(Window* window) : window_(window) {} + WindowRenderObject(const WindowRenderObject& other) = delete; + WindowRenderObject(WindowRenderObject&& other) = delete; + WindowRenderObject& operator=(const WindowRenderObject& other) = delete; + WindowRenderObject& operator=(WindowRenderObject&& other) = delete; + ~WindowRenderObject() override = default; + + void MeasureAndLayout(); + + void Draw(ID2D1RenderTarget* render_target) override; + + RenderObject* HitTest(const Point& point) override; + + protected: + void OnAddChild(RenderObject* new_child, int position) override; + + Size OnMeasureContent(const Size& available_size) override; + void OnLayoutContent(const Rect& content_rect) override; + + private: + RenderObject* GetChild() const { + return GetChildren().empty() ? nullptr : GetChildren()[0]; + } + + private: + Window* window_; +}; +} // namespace cru::ui::render diff --git a/src/ui/window.cpp b/src/ui/window.cpp index 51b3f628..f92811a7 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -1,767 +1,645 @@ #include "window.hpp" #include "application.hpp" -#include "graph/graph.hpp" -#include "exception.hpp" #include "cursor.hpp" +#include "exception.hpp" +#include "graph/graph.hpp" +#include "render/window_render_object.hpp" -namespace cru::ui -{ - LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { - auto window = WindowManager::GetInstance()->FromHandle(hWnd); - - LRESULT result; - if (window != nullptr && window->HandleWindowMessage(hWnd, Msg, wParam, lParam, result)) - return result; - - return DefWindowProc(hWnd, Msg, wParam, lParam); - } - - WindowManager* WindowManager::GetInstance() - { - return Application::GetInstance()->ResolveSingleton([](auto) - { - return new WindowManager{}; - }); - } - - WindowManager::WindowManager() { - general_window_class_ = std::make_unique( - L"CruUIWindowClass", - GeneralWndProc, - Application::GetInstance()->GetInstanceHandle() - ); - } - - void WindowManager::RegisterWindow(HWND hwnd, Window * window) { - const auto find_result = window_map_.find(hwnd); - if (find_result != window_map_.end()) - throw std::runtime_error("The hwnd is already in the map."); - - window_map_.emplace(hwnd, window); - } - - void WindowManager::UnregisterWindow(HWND hwnd) { - const auto find_result = window_map_.find(hwnd); - if (find_result == window_map_.end()) - throw std::runtime_error("The hwnd is not in the map."); - window_map_.erase(find_result); - - if (window_map_.empty()) - Application::GetInstance()->Quit(0); - } - - Window* WindowManager::FromHandle(HWND hwnd) { - const auto find_result = window_map_.find(hwnd); - if (find_result == window_map_.end()) - return nullptr; - else - return find_result->second; - } +namespace cru::ui { +LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam, + LPARAM lParam) { + auto window = WindowManager::GetInstance()->FromHandle(hWnd); - std::vector WindowManager::GetAllWindows() const - { - std::vector windows; - for (auto [key, value] : window_map_) - windows.push_back(value); - return windows; - } + LRESULT result; + if (window != nullptr && + window->HandleWindowMessage(hWnd, Msg, wParam, lParam, result)) + return result; - inline Point PiToDip(const POINT& pi_point) - { - return Point( - graph::PixelToDipX(pi_point.x), - graph::PixelToDipY(pi_point.y) - ); - } + return DefWindowProc(hWnd, Msg, wParam, lParam); +} - inline POINT DipToPi(const Point& dip_point) - { - POINT result; - result.x = graph::DipToPixelX(dip_point.x); - result.y = graph::DipToPixelY(dip_point.y); - return result; - } +WindowManager* WindowManager::GetInstance() { + return Application::GetInstance()->ResolveSingleton( + [](auto) { return new WindowManager{}; }); +} +WindowManager::WindowManager() { + general_window_class_ = std::make_unique( + L"CruUIWindowClass", GeneralWndProc, + Application::GetInstance()->GetInstanceHandle()); +} - namespace - { - Cursor::Ptr GetCursorInherit(Control* control) - { - while (control != nullptr) - { - const auto cursor = control->GetCursor(); - if (cursor != nullptr) - return cursor; - control = control->GetInternalParent(); - } - return cursors::arrow; - } - } +void WindowManager::RegisterWindow(HWND hwnd, Window* window) { + const auto find_result = window_map_.find(hwnd); + if (find_result != window_map_.end()) + throw std::runtime_error("The hwnd is already in the map."); - Window* Window::CreateOverlapped() - { - return new Window(tag_overlapped_constructor{}); - } + window_map_.emplace(hwnd, window); +} - Window* Window::CreatePopup(Window* parent, const bool caption) - { - return new Window(tag_popup_constructor{}, parent, caption); - } +void WindowManager::UnregisterWindow(HWND hwnd) { + const auto find_result = window_map_.find(hwnd); + if (find_result == window_map_.end()) + throw std::runtime_error("The hwnd is not in the map."); + window_map_.erase(find_result); + if (window_map_.empty()) Application::GetInstance()->Quit(0); +} - Window::Window(tag_overlapped_constructor) - { - BeforeCreateHwnd(); +Window* WindowManager::FromHandle(HWND hwnd) { + const auto find_result = window_map_.find(hwnd); + if (find_result == window_map_.end()) + return nullptr; + else + return find_result->second; +} - const auto window_manager = WindowManager::GetInstance(); +std::vector WindowManager::GetAllWindows() const { + std::vector windows; + for (auto [key, value] : window_map_) windows.push_back(value); + return windows; +} - hwnd_ = CreateWindowEx(0, - window_manager->GetGeneralWindowClass()->GetName(), - L"", WS_OVERLAPPEDWINDOW, - CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, - nullptr, nullptr, Application::GetInstance()->GetInstanceHandle(), nullptr - ); +inline Point PiToDip(const POINT& pi_point) { + return Point(graph::PixelToDipX(pi_point.x), graph::PixelToDipY(pi_point.y)); +} - if (hwnd_ == nullptr) - throw std::runtime_error("Failed to create window."); +inline POINT DipToPi(const Point& dip_point) { + POINT result; + result.x = graph::DipToPixelX(dip_point.x); + result.y = graph::DipToPixelY(dip_point.y); + return result; +} - AfterCreateHwnd(window_manager); - } +namespace { +Cursor::Ptr GetCursorInherit(Control* control) { + while (control != nullptr) { + const auto cursor = control->GetCursor(); + if (cursor != nullptr) return cursor; + control = control->GetParent(); + } + return cursors::arrow; +} +} // namespace - Window::Window(tag_popup_constructor, Window* parent, const bool caption) - { - if (parent != nullptr && !parent->IsWindowValid()) - throw std::runtime_error("Parent window is not valid."); +Window* Window::CreateOverlapped() { + return new Window(tag_overlapped_constructor{}); +} - BeforeCreateHwnd(); +Window* Window::CreatePopup(Window* parent, const bool caption) { + return new Window(tag_popup_constructor{}, parent, caption); +} - parent_window_ = parent; +Window::Window(tag_overlapped_constructor) { + BeforeCreateHwnd(); - const auto window_manager = WindowManager::GetInstance(); + const auto window_manager = WindowManager::GetInstance(); - hwnd_ = CreateWindowEx(0, - window_manager->GetGeneralWindowClass()->GetName(), - L"", caption ? (WS_POPUPWINDOW | WS_CAPTION) : WS_POPUP, - CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, - parent == nullptr ? nullptr : parent->GetWindowHandle(), - nullptr, Application::GetInstance()->GetInstanceHandle(), nullptr - ); + hwnd_ = + CreateWindowEx(0, window_manager->GetGeneralWindowClass()->GetName(), L"", + WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, + Application::GetInstance()->GetInstanceHandle(), nullptr); - if (hwnd_ == nullptr) - throw std::runtime_error("Failed to create window."); + if (hwnd_ == nullptr) throw std::runtime_error("Failed to create window."); - AfterCreateHwnd(window_manager); - } + AfterCreateHwnd(window_manager); +} - void Window::BeforeCreateHwnd() - { - window_ = this; - } +Window::Window(tag_popup_constructor, Window* parent, const bool caption) { + if (parent != nullptr && !parent->IsWindowValid()) + throw std::runtime_error("Parent window is not valid."); - void Window::AfterCreateHwnd(WindowManager* window_manager) - { - window_manager->RegisterWindow(hwnd_, this); + BeforeCreateHwnd(); - render_target_ = graph::GraphManager::GetInstance()->CreateWindowRenderTarget(hwnd_); + parent_window_ = parent; - SetCursor(cursors::arrow); - } + const auto window_manager = WindowManager::GetInstance(); - Window::~Window() { - if (IsWindowValid()) - { - SetDeleteThisOnDestroy(false); // avoid double delete. - Close(); - } - TraverseDescendants([this](Control* control) { - control->OnDetachToWindow(this); - }); - } + hwnd_ = CreateWindowEx( + 0, window_manager->GetGeneralWindowClass()->GetName(), L"", + caption ? (WS_POPUPWINDOW | WS_CAPTION) : WS_POPUP, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, + parent == nullptr ? nullptr : parent->GetWindowHandle(), nullptr, + Application::GetInstance()->GetInstanceHandle(), nullptr); - StringView Window::GetControlType() const - { - return control_type; - } + if (hwnd_ == nullptr) throw std::runtime_error("Failed to create window."); - void Window::SetDeleteThisOnDestroy(bool value) - { - delete_this_on_destroy_ = value; - } + AfterCreateHwnd(window_manager); +} - void Window::Close() { - if (IsWindowValid()) - DestroyWindow(hwnd_); - } +void Window::BeforeCreateHwnd() { window_ = this; } - void Window::InvalidateDraw() { - if (IsWindowValid()) { - InvalidateRect(hwnd_, nullptr, false); - } - } +void Window::AfterCreateHwnd(WindowManager* window_manager) { + window_manager->RegisterWindow(hwnd_, this); - void Window::Show() { - if (IsWindowValid()) { - ShowWindow(hwnd_, SW_SHOWNORMAL); - } - } + render_target_ = + graph::GraphManager::GetInstance()->CreateWindowRenderTarget(hwnd_); - void Window::Hide() { - if (IsWindowValid()) { - ShowWindow(hwnd_, SW_HIDE); - } - } + SetCursor(cursors::arrow); - Size Window::GetClientSize() { - if (!IsWindowValid()) - return Size(); + render_object_ = new render::WindowRenderObject(this); +} - const auto pixel_rect = GetClientRectPixel(); - return Size( - graph::PixelToDipX(pixel_rect.right), - graph::PixelToDipY(pixel_rect.bottom) - ); - } +Window::~Window() { + if (IsWindowValid()) { + SetDeleteThisOnDestroy(false); // avoid double delete. + Close(); + } + TraverseDescendants( + [this](Control* control) { control->OnDetachToWindow(this); }); + delete render_object_; +} - void Window::SetClientSize(const Size & size) { - if (IsWindowValid()) { - const auto window_style = static_cast(GetWindowLongPtr(hwnd_, GWL_STYLE)); - const auto window_ex_style = static_cast(GetWindowLongPtr(hwnd_, GWL_EXSTYLE)); - - RECT rect; - rect.left = 0; - rect.top = 0; - rect.right = graph::DipToPixelX(size.width); - rect.bottom = graph::DipToPixelY(size.height); - AdjustWindowRectEx(&rect, window_style, FALSE, window_ex_style); - - SetWindowPos( - hwnd_, nullptr, 0, 0, - rect.right - rect.left, - rect.bottom - rect.top, - SWP_NOZORDER | SWP_NOMOVE - ); - } - } +StringView Window::GetControlType() const { return control_type; } - Rect Window::GetWindowRect() { - if (!IsWindowValid()) - return Rect(); +render::RenderObject* Window::GetRenderObject() const { return render_object_; } - RECT rect; - ::GetWindowRect(hwnd_, &rect); +void Window::SetDeleteThisOnDestroy(bool value) { + delete_this_on_destroy_ = value; +} - return Rect::FromVertices( - graph::PixelToDipX(rect.left), - graph::PixelToDipY(rect.top), - graph::PixelToDipX(rect.right), - graph::PixelToDipY(rect.bottom) - ); - } +void Window::Close() { + if (IsWindowValid()) DestroyWindow(hwnd_); +} - void Window::SetWindowRect(const Rect & rect) { - if (IsWindowValid()) { - SetWindowPos( - hwnd_, nullptr, - graph::DipToPixelX(rect.left), - graph::DipToPixelY(rect.top), - graph::DipToPixelX(rect.GetRight()), - graph::DipToPixelY(rect.GetBottom()), - SWP_NOZORDER - ); - } - } +void Window::InvalidateDraw() { + if (IsWindowValid()) { + InvalidateRect(hwnd_, nullptr, false); + } +} - void Window::SetWindowPosition(const Point& position) - { - if (IsWindowValid()) { - SetWindowPos( - hwnd_, nullptr, - graph::DipToPixelX(position.x), - graph::DipToPixelY(position.y), - 0, 0, - SWP_NOZORDER | SWP_NOSIZE - ); - } - } +void Window::Show() { + if (IsWindowValid()) { + ShowWindow(hwnd_, SW_SHOWNORMAL); + } +} - Point Window::PointToScreen(const Point& point) - { - if (!IsWindowValid()) - return Point::Zero(); +void Window::Hide() { + if (IsWindowValid()) { + ShowWindow(hwnd_, SW_HIDE); + } +} - auto p = DipToPi(point); - if (::ClientToScreen(GetWindowHandle(), &p) == 0) - throw Win32Error(::GetLastError(), "Failed transform point from window to screen."); - return PiToDip(p); - } +Size Window::GetClientSize() { + if (!IsWindowValid()) return Size(); - Point Window::PointFromScreen(const Point& point) - { - if (!IsWindowValid()) - return Point::Zero(); + const auto pixel_rect = GetClientRectPixel(); + return Size(graph::PixelToDipX(pixel_rect.right), + graph::PixelToDipY(pixel_rect.bottom)); +} - auto p = DipToPi(point); - if (::ScreenToClient(GetWindowHandle(), &p) == 0) - throw Win32Error(::GetLastError(), "Failed transform point from screen to window."); - return PiToDip(p); - } +void Window::SetClientSize(const Size& size) { + if (IsWindowValid()) { + const auto window_style = + static_cast(GetWindowLongPtr(hwnd_, GWL_STYLE)); + const auto window_ex_style = + static_cast(GetWindowLongPtr(hwnd_, GWL_EXSTYLE)); + + RECT rect; + rect.left = 0; + rect.top = 0; + rect.right = graph::DipToPixelX(size.width); + rect.bottom = graph::DipToPixelY(size.height); + AdjustWindowRectEx(&rect, window_style, FALSE, window_ex_style); + + SetWindowPos(hwnd_, nullptr, 0, 0, rect.right - rect.left, + rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE); + } +} - bool Window::HandleWindowMessage(HWND hwnd, int msg, WPARAM w_param, LPARAM l_param, LRESULT & result) { - - events::WindowNativeMessageEventArgs args(this, this, {hwnd, msg, w_param, l_param}); - native_message_event.Raise(args); - if (args.GetResult().has_value()) - { - result = args.GetResult().value(); - return true; - } - - switch (msg) { - case WM_PAINT: - OnPaintInternal(); - result = 0; - return true; - case WM_ERASEBKGND: - result = 1; - return true; - case WM_SETFOCUS: - OnSetFocusInternal(); - result = 0; - return true; - case WM_KILLFOCUS: - OnKillFocusInternal(); - result = 0; - return true; - case WM_MOUSEMOVE: - { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseMoveInternal(point); - result = 0; - return true; - } - case WM_LBUTTONDOWN: - { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(MouseButton::Left, point); - result = 0; - return true; - } - case WM_LBUTTONUP: - { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseUpInternal(MouseButton::Left, point); - result = 0; - return true; - } - case WM_RBUTTONDOWN: - { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(MouseButton::Right, point); - result = 0; - return true; - } - case WM_RBUTTONUP: - { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseUpInternal(MouseButton::Right, point); - result = 0; - return true; - } - case WM_MBUTTONDOWN: - { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(MouseButton::Middle, point); - result = 0; - return true; - } - case WM_MBUTTONUP: - { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseUpInternal(MouseButton::Middle, point); - result = 0; - return true; - } - case WM_MOUSEWHEEL: - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - ScreenToClient(hwnd, &point); - OnMouseWheelInternal(GET_WHEEL_DELTA_WPARAM(w_param), point); - result = 0; - return true; - case WM_KEYDOWN: - OnKeyDownInternal(static_cast(w_param)); - result = 0; - return true; - case WM_KEYUP: - OnKeyUpInternal(static_cast(w_param)); - result = 0; - return true; - case WM_CHAR: - OnCharInternal(static_cast(w_param)); - result = 0; - return true; - case WM_SIZE: - OnResizeInternal(LOWORD(l_param), HIWORD(l_param)); - result = 0; - return true; - case WM_ACTIVATE: - if (w_param == WA_ACTIVE || w_param == WA_CLICKACTIVE) - OnActivatedInternal(); - else if (w_param == WA_INACTIVE) - OnDeactivatedInternal(); - result = 0; - return true; - case WM_DESTROY: - OnDestroyInternal(); - result = 0; - return true; - default: - return false; - } - } +Rect Window::GetWindowRect() { + if (!IsWindowValid()) return Rect(); - Point Window::GetMousePosition() - { - if (!IsWindowValid()) - return Point::Zero(); - POINT point; - ::GetCursorPos(&point); - ::ScreenToClient(hwnd_, &point); - return PiToDip(point); - } + RECT rect; + ::GetWindowRect(hwnd_, &rect); - Point Window::GetOffset() - { - return Point(); - } + return Rect::FromVertices( + graph::PixelToDipX(rect.left), graph::PixelToDipY(rect.top), + graph::PixelToDipX(rect.right), graph::PixelToDipY(rect.bottom)); +} - Size Window::GetSize() - { - return GetClientSize(); - } +void Window::SetWindowRect(const Rect& rect) { + if (IsWindowValid()) { + SetWindowPos(hwnd_, nullptr, graph::DipToPixelX(rect.left), + graph::DipToPixelY(rect.top), + graph::DipToPixelX(rect.GetRight()), + graph::DipToPixelY(rect.GetBottom()), SWP_NOZORDER); + } +} - void Window::SetRect(const Rect& size) - { +void Window::SetWindowPosition(const Point& position) { + if (IsWindowValid()) { + SetWindowPos(hwnd_, nullptr, graph::DipToPixelX(position.x), + graph::DipToPixelY(position.y), 0, 0, + SWP_NOZORDER | SWP_NOSIZE); + } +} - } +Point Window::PointToScreen(const Point& point) { + if (!IsWindowValid()) return Point::Zero(); - bool Window::IsPointInside(const Point& point) - { - return Rect(Point::Zero(), GetClientSize()).IsPointInside(point); - } + auto p = DipToPi(point); + if (::ClientToScreen(GetWindowHandle(), &p) == 0) + throw Win32Error(::GetLastError(), + "Failed transform point from window to screen."); + return PiToDip(p); +} - void Window::WindowInvalidateLayout() - { - if (is_layout_invalid_) - return; - - is_layout_invalid_ = true; - InvokeLater([this] - { - if (is_layout_invalid_) - Relayout(); - }); - } +Point Window::PointFromScreen(const Point& point) { + if (!IsWindowValid()) return Point::Zero(); - void Window::Relayout() - { - Measure(GetSize(), AdditionalMeasureInfo{}); - OnLayoutCore(Rect(Point::Zero(), GetSize()), AdditionalLayoutInfo{}); - is_layout_invalid_ = false; - } + auto p = DipToPi(point); + if (::ScreenToClient(GetWindowHandle(), &p) == 0) + throw Win32Error(::GetLastError(), + "Failed transform point from screen to window."); + return PiToDip(p); +} - void Window::SetSizeFitContent(const Size& max_size) - { - Measure(max_size, AdditionalMeasureInfo{}); - SetClientSize(GetDesiredSize()); - OnLayoutCore(Rect(Point::Zero(), GetSize()), AdditionalLayoutInfo{}); - is_layout_invalid_ = false; - } +bool Window::HandleWindowMessage(HWND hwnd, int msg, WPARAM w_param, + LPARAM l_param, LRESULT& result) { + events::WindowNativeMessageEventArgs args(this, this, + {hwnd, msg, w_param, l_param}); + native_message_event.Raise(args); + if (args.GetResult().has_value()) { + result = args.GetResult().value(); + return true; + } + + switch (msg) { + case WM_PAINT: + OnPaintInternal(); + result = 0; + return true; + case WM_ERASEBKGND: + result = 1; + return true; + case WM_SETFOCUS: + OnSetFocusInternal(); + result = 0; + return true; + case WM_KILLFOCUS: + OnKillFocusInternal(); + result = 0; + return true; + case WM_MOUSEMOVE: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseMoveInternal(point); + result = 0; + return true; + } + case WM_LBUTTONDOWN: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseDownInternal(MouseButton::Left, point); + result = 0; + return true; + } + case WM_LBUTTONUP: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseUpInternal(MouseButton::Left, point); + result = 0; + return true; + } + case WM_RBUTTONDOWN: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseDownInternal(MouseButton::Right, point); + result = 0; + return true; + } + case WM_RBUTTONUP: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseUpInternal(MouseButton::Right, point); + result = 0; + return true; + } + case WM_MBUTTONDOWN: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseDownInternal(MouseButton::Middle, point); + result = 0; + return true; + } + case WM_MBUTTONUP: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseUpInternal(MouseButton::Middle, point); + result = 0; + return true; + } + case WM_MOUSEWHEEL: + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + ScreenToClient(hwnd, &point); + OnMouseWheelInternal(GET_WHEEL_DELTA_WPARAM(w_param), point); + result = 0; + return true; + case WM_KEYDOWN: + OnKeyDownInternal(static_cast(w_param)); + result = 0; + return true; + case WM_KEYUP: + OnKeyUpInternal(static_cast(w_param)); + result = 0; + return true; + case WM_CHAR: + OnCharInternal(static_cast(w_param)); + result = 0; + return true; + case WM_SIZE: + OnResizeInternal(LOWORD(l_param), HIWORD(l_param)); + result = 0; + return true; + case WM_ACTIVATE: + if (w_param == WA_ACTIVE || w_param == WA_CLICKACTIVE) + OnActivatedInternal(); + else if (w_param == WA_INACTIVE) + OnDeactivatedInternal(); + result = 0; + return true; + case WM_DESTROY: + OnDestroyInternal(); + result = 0; + return true; + default: + return false; + } +} - bool Window::RequestFocusFor(Control * control) - { - if (control == nullptr) - throw std::invalid_argument("The control to request focus can't be null. You can set it as the window."); +Point Window::GetMousePosition() { + if (!IsWindowValid()) return Point::Zero(); + POINT point; + ::GetCursorPos(&point); + ::ScreenToClient(hwnd_, &point); + return PiToDip(point); +} - if (!IsWindowValid()) - return false; +bool Window::RequestFocusFor(Control* control) { + if (control == nullptr) + throw std::invalid_argument( + "The control to request focus can't be null. You can set it as the " + "window."); - if (!window_focus_) - { - focus_control_ = control; - ::SetFocus(hwnd_); - return true; // event dispatch will be done in window message handling function "OnSetFocusInternal". - } + if (!IsWindowValid()) return false; - if (focus_control_ == control) - return true; + if (!window_focus_) { + focus_control_ = control; + ::SetFocus(hwnd_); + return true; // event dispatch will be done in window message handling + // function "OnSetFocusInternal". + } - DispatchEvent(focus_control_, &Control::lose_focus_event, nullptr, false); + if (focus_control_ == control) return true; - focus_control_ = control; + DispatchEvent(focus_control_, &Control::lose_focus_event, nullptr, false); - DispatchEvent(control, &Control::get_focus_event, nullptr, false); + focus_control_ = control; - return true; - } + DispatchEvent(control, &Control::get_focus_event, nullptr, false); - Control* Window::GetFocusControl() - { - return focus_control_; - } + return true; +} - Control* Window::CaptureMouseFor(Control* control) - { - if (control != nullptr) - { - ::SetCapture(hwnd_); - std::swap(mouse_capture_control_, control); - DispatchMouseHoverControlChangeEvent(control ? control : mouse_hover_control_, mouse_capture_control_, GetMousePosition()); - return control; - } - else - { - return ReleaseCurrentMouseCapture(); - } - } +Control* Window::GetFocusControl() { return focus_control_; } + +Control* Window::CaptureMouseFor(Control* control) { + if (control != nullptr) { + ::SetCapture(hwnd_); + std::swap(mouse_capture_control_, control); + DispatchMouseHoverControlChangeEvent( + control ? control : mouse_hover_control_, mouse_capture_control_, + GetMousePosition()); + return control; + } else { + return ReleaseCurrentMouseCapture(); + } +} - Control* Window::ReleaseCurrentMouseCapture() - { - if (mouse_capture_control_) - { - const auto previous = mouse_capture_control_; - mouse_capture_control_ = nullptr; - ::ReleaseCapture(); - DispatchMouseHoverControlChangeEvent(previous, mouse_hover_control_, GetMousePosition()); - return previous; - } - else - { - return nullptr; - } - } +Control* Window::ReleaseCurrentMouseCapture() { + if (mouse_capture_control_) { + const auto previous = mouse_capture_control_; + mouse_capture_control_ = nullptr; + ::ReleaseCapture(); + DispatchMouseHoverControlChangeEvent(previous, mouse_hover_control_, + GetMousePosition()); + return previous; + } else { + return nullptr; + } +} - void Window::UpdateCursor() - { - if (IsWindowValid() && mouse_hover_control_ != nullptr) - { - SetCursorInternal(GetCursorInherit(mouse_hover_control_)->GetHandle()); - } - } +void Window::UpdateCursor() { + if (IsWindowValid() && mouse_hover_control_ != nullptr) { + SetCursorInternal(GetCursorInherit(mouse_hover_control_)->GetHandle()); + } +} #ifdef CRU_DEBUG_LAYOUT - void Window::SetDebugLayout(const bool value) - { - if (debug_layout_ != value) - { - debug_layout_ = value; - InvalidateDraw(); - } - } +void Window::SetDebugLayout(const bool value) { + if (debug_layout_ != value) { + debug_layout_ = value; + InvalidateDraw(); + } +} #endif - RECT Window::GetClientRectPixel() { - RECT rect{ }; - GetClientRect(hwnd_, &rect); - return rect; - } +void Window::OnChildChanged(Control* old_child, Control* new_child) { + if (old_child) render_object_->RemoveChild(0); + if (new_child) render_object_->AddChild(new_child->GetRenderObject(), 0); +} - bool Window::IsMessageInQueue(UINT message) - { - MSG msg; - return ::PeekMessageW(&msg, hwnd_, message, message, PM_NOREMOVE) != 0; - } +Control* Window::HitTest(const Point& point) { + return render_object_->HitTest(point)->GetAttachedControl(); +} - void Window::SetCursorInternal(HCURSOR cursor) - { - if (IsWindowValid()) - { - ::SetClassLongPtrW(GetWindowHandle(), GCLP_HCURSOR, reinterpret_cast(cursor)); - if (mouse_hover_control_ != nullptr) - ::SetCursor(cursor); - } - } +RECT Window::GetClientRectPixel() { + RECT rect{}; + GetClientRect(hwnd_, &rect); + return rect; +} - void Window::OnDestroyInternal() { - WindowManager::GetInstance()->UnregisterWindow(hwnd_); - hwnd_ = nullptr; - if (delete_this_on_destroy_) - InvokeLater([this]{ delete this; }); - } +bool Window::IsMessageInQueue(UINT message) { + MSG msg; + return ::PeekMessageW(&msg, hwnd_, message, message, PM_NOREMOVE) != 0; +} - void Window::OnPaintInternal() { - render_target_->SetAsTarget(); +void Window::SetCursorInternal(HCURSOR cursor) { + if (IsWindowValid()) { + ::SetClassLongPtrW(GetWindowHandle(), GCLP_HCURSOR, + reinterpret_cast(cursor)); + if (mouse_hover_control_ != nullptr) ::SetCursor(cursor); + } +} - auto device_context = render_target_->GetD2DDeviceContext(); +void Window::OnDestroyInternal() { + WindowManager::GetInstance()->UnregisterWindow(hwnd_); + hwnd_ = nullptr; + if (delete_this_on_destroy_) InvokeLater([this] { delete this; }); +} - device_context->BeginDraw(); +void Window::OnPaintInternal() { + render_target_->SetAsTarget(); - //Clear the background. - device_context->Clear(D2D1::ColorF(D2D1::ColorF::White)); + auto device_context = render_target_->GetD2DDeviceContext(); - Draw(device_context.Get()); + device_context->BeginDraw(); - ThrowIfFailed( - device_context->EndDraw(), "Failed to draw window." - ); + // Clear the background. + device_context->Clear(D2D1::ColorF(D2D1::ColorF::White)); - render_target_->Present(); + render_object_->Draw(device_context.Get()); - ValidateRect(hwnd_, nullptr); - } + ThrowIfFailed(device_context->EndDraw(), "Failed to draw window."); - void Window::OnResizeInternal(const int new_width, const int new_height) { - render_target_->ResizeBuffer(new_width, new_height); - if (!(new_width == 0 && new_height == 0)) - WindowInvalidateLayout(); - } + render_target_->Present(); - void Window::OnSetFocusInternal() - { - window_focus_ = true; - DispatchEvent(focus_control_, &Control::get_focus_event, nullptr, true); - } + ValidateRect(hwnd_, nullptr); +} - void Window::OnKillFocusInternal() - { - window_focus_ = false; - DispatchEvent(focus_control_, &Control::lose_focus_event, nullptr, true); - } +void Window::OnResizeInternal(const int new_width, const int new_height) { + render_target_->ResizeBuffer(new_width, new_height); + if (!(new_width == 0 && new_height == 0)) render_object_->MeasureAndLayout(); +} - void Window::OnMouseMoveInternal(const POINT point) - { - const auto dip_point = PiToDip(point); - - //when mouse was previous outside the window - if (mouse_hover_control_ == nullptr) { - //invoke TrackMouseEvent to have WM_MOUSELEAVE sent. - TRACKMOUSEEVENT tme; - tme.cbSize = sizeof tme; - tme.dwFlags = TME_LEAVE; - tme.hwndTrack = hwnd_; - - TrackMouseEvent(&tme); - } - - //Find the first control that hit test succeed. - const auto new_control_mouse_hover = HitTest(dip_point); - const auto old_control_mouse_hover = mouse_hover_control_; - mouse_hover_control_ = new_control_mouse_hover; - - if (mouse_capture_control_) // if mouse is captured - { - DispatchEvent(mouse_capture_control_, &Control::mouse_move_event, nullptr, dip_point); - } - else - { - DispatchMouseHoverControlChangeEvent(old_control_mouse_hover, new_control_mouse_hover, dip_point); - DispatchEvent(new_control_mouse_hover, &Control::mouse_move_event, nullptr, dip_point); - } - } +void Window::OnSetFocusInternal() { + window_focus_ = true; + DispatchEvent(focus_control_, &Control::get_focus_event, nullptr, true); +} - void Window::OnMouseLeaveInternal() - { - DispatchEvent(mouse_hover_control_, &Control::mouse_leave_event, nullptr); - mouse_hover_control_ = nullptr; - } +void Window::OnKillFocusInternal() { + window_focus_ = false; + DispatchEvent(focus_control_, &Control::lose_focus_event, nullptr, true); +} - void Window::OnMouseDownInternal(MouseButton button, POINT point) - { - const auto dip_point = PiToDip(point); +void Window::OnMouseMoveInternal(const POINT point) { + const auto dip_point = PiToDip(point); + + // when mouse was previous outside the window + if (mouse_hover_control_ == nullptr) { + // invoke TrackMouseEvent to have WM_MOUSELEAVE sent. + TRACKMOUSEEVENT tme; + tme.cbSize = sizeof tme; + tme.dwFlags = TME_LEAVE; + tme.hwndTrack = hwnd_; + + TrackMouseEvent(&tme); + } + + // Find the first control that hit test succeed. + const auto new_control_mouse_hover = HitTest(dip_point); + const auto old_control_mouse_hover = mouse_hover_control_; + mouse_hover_control_ = new_control_mouse_hover; + + if (mouse_capture_control_) // if mouse is captured + { + DispatchEvent(mouse_capture_control_, &Control::mouse_move_event, nullptr, + dip_point); + } else { + DispatchMouseHoverControlChangeEvent(old_control_mouse_hover, + new_control_mouse_hover, dip_point); + DispatchEvent(new_control_mouse_hover, &Control::mouse_move_event, nullptr, + dip_point); + } +} - Control* control; +void Window::OnMouseLeaveInternal() { + DispatchEvent(mouse_hover_control_, &Control::mouse_leave_event, nullptr); + mouse_hover_control_ = nullptr; +} - if (mouse_capture_control_) - control = mouse_capture_control_; - else - control = HitTest(dip_point); +void Window::OnMouseDownInternal(MouseButton button, POINT point) { + const auto dip_point = PiToDip(point); - DispatchEvent(control, &Control::mouse_down_event, nullptr, dip_point, button); - } + Control* control; - void Window::OnMouseUpInternal(MouseButton button, POINT point) - { - const auto dip_point = PiToDip(point); + if (mouse_capture_control_) + control = mouse_capture_control_; + else + control = HitTest(dip_point); - Control* control; + DispatchEvent(control, &Control::mouse_down_event, nullptr, dip_point, + button); +} - if (mouse_capture_control_) - control = mouse_capture_control_; - else - control = HitTest(dip_point); +void Window::OnMouseUpInternal(MouseButton button, POINT point) { + const auto dip_point = PiToDip(point); - DispatchEvent(control, &Control::mouse_up_event, nullptr, dip_point, button); - } + Control* control; - void Window::OnMouseWheelInternal(short delta, POINT point) - { - const auto dip_point = PiToDip(point); + if (mouse_capture_control_) + control = mouse_capture_control_; + else + control = HitTest(dip_point); - Control* control; + DispatchEvent(control, &Control::mouse_up_event, nullptr, dip_point, button); +} - if (mouse_capture_control_) - control = mouse_capture_control_; - else - control = HitTest(dip_point); +void Window::OnMouseWheelInternal(short delta, POINT point) { + const auto dip_point = PiToDip(point); - DispatchEvent(control, &Control::mouse_wheel_event, nullptr, dip_point, static_cast(delta)); - } + Control* control; - void Window::OnKeyDownInternal(int virtual_code) - { - DispatchEvent(focus_control_, &Control::key_down_event, nullptr, virtual_code); - } + if (mouse_capture_control_) + control = mouse_capture_control_; + else + control = HitTest(dip_point); - void Window::OnKeyUpInternal(int virtual_code) - { - DispatchEvent(focus_control_, &Control::key_up_event, nullptr, virtual_code); - } + DispatchEvent(control, &Control::mouse_wheel_event, nullptr, dip_point, + static_cast(delta)); +} - void Window::OnCharInternal(wchar_t c) - { - DispatchEvent(focus_control_, &Control::char_event, nullptr, c); - } +void Window::OnKeyDownInternal(int virtual_code) { + DispatchEvent(focus_control_, &Control::key_down_event, nullptr, + virtual_code); +} - void Window::OnActivatedInternal() - { - events::UiEventArgs args(this, this); - activated_event.Raise(args); - } +void Window::OnKeyUpInternal(int virtual_code) { + DispatchEvent(focus_control_, &Control::key_up_event, nullptr, virtual_code); +} - void Window::OnDeactivatedInternal() - { - events::UiEventArgs args(this, this); - deactivated_event.Raise(args); - } +void Window::OnCharInternal(wchar_t c) { + DispatchEvent(focus_control_, &Control::char_event, nullptr, c); +} - void Window::DispatchMouseHoverControlChangeEvent(Control* old_control, Control* new_control, const Point& point) - { - if (new_control != old_control) //if the mouse-hover-on control changed - { - const auto lowest_common_ancestor = FindLowestCommonAncestor(old_control, new_control); - if (old_control != nullptr) // if last mouse-hover-on control exists - DispatchEvent(old_control, &Control::mouse_leave_event, lowest_common_ancestor); // dispatch mouse leave event. - if (new_control != nullptr) - { - DispatchEvent(new_control, &Control::mouse_enter_event, lowest_common_ancestor, point); // dispatch mouse enter event. - UpdateCursor(); - } - } - } +void Window::OnActivatedInternal() { + events::UiEventArgs args(this, this); + activated_event.Raise(args); +} + +void Window::OnDeactivatedInternal() { + events::UiEventArgs args(this, this); + deactivated_event.Raise(args); +} + +void Window::DispatchMouseHoverControlChangeEvent(Control* old_control, + Control* new_control, + const Point& point) { + if (new_control != old_control) // if the mouse-hover-on control changed + { + const auto lowest_common_ancestor = + FindLowestCommonAncestor(old_control, new_control); + if (old_control != nullptr) // if last mouse-hover-on control exists + DispatchEvent(old_control, &Control::mouse_leave_event, + lowest_common_ancestor); // dispatch mouse leave event. + if (new_control != nullptr) { + DispatchEvent(new_control, &Control::mouse_enter_event, + lowest_common_ancestor, + point); // dispatch mouse enter event. + UpdateCursor(); + } + } } +} // namespace cru::ui diff --git a/src/ui/window.hpp b/src/ui/window.hpp index ac26de22..7a28c257 100644 --- a/src/ui/window.hpp +++ b/src/ui/window.hpp @@ -1,6 +1,4 @@ #pragma once - -// ReSharper disable once CppUnusedIncludeDirective #include "pre.hpp" #include @@ -9,11 +7,16 @@ #include "control.hpp" #include "events/ui_event.hpp" +#include "window_class.hpp" namespace cru::graph { class WindowRenderTarget; } +namespace cru::ui::render { +class WindowRenderObject; +} + namespace cru::ui { class WindowManager : public Object { public: @@ -85,6 +88,8 @@ class Window final : public ContentControl { public: StringView GetControlType() const override final; + render::RenderObject* GetRenderObject() const override; + void SetDeleteThisOnDestroy(bool value); //*************** region: handle *************** @@ -105,7 +110,7 @@ class Window final : public ContentControl { // Send a repaint message to the window's message queue which may make the // window repaint. - void InvalidateDraw() override final; + void InvalidateDraw(); // Show the window. void Show(); @@ -146,14 +151,6 @@ class Window final : public ContentControl { Control* GetMouseHoverControl() const { return mouse_hover_control_; } - //*************** region: layout *************** - - void WindowInvalidateLayout(); - - void Relayout(); - - void SetSizeFitContent(const Size& max_size = Size(1000, 1000)); - //*************** region: focus *************** // Request focus for specified control. @@ -170,13 +167,6 @@ class Window final : public ContentControl { //*************** region: cursor *************** void UpdateCursor(); - //*************** region: debug *************** -#ifdef CRU_DEBUG_LAYOUT - bool IsDebugLayout() const { return debug_layout_; } - - void SetDebugLayout(bool value); -#endif - public: //*************** region: events *************** Event activated_event; @@ -184,7 +174,12 @@ class Window final : public ContentControl { Event native_message_event; + protected: + void OnChildChanged(Control* old_child, Control* new_child) override; + private: + Control* HitTest(const Point& point); + //*************** region: native operations *************** // Get the client rect in pixel. @@ -229,6 +224,8 @@ class Window final : public ContentControl { Window* parent_window_ = nullptr; std::shared_ptr render_target_{}; + render::WindowRenderObject* render_object_; + Control* mouse_hover_control_ = nullptr; bool window_focus_ = false; -- cgit v1.2.3 From afdac77a66143375b5bebd4ff128b0fba87c5d21 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 21 Mar 2019 22:44:31 +0800 Subject: Change all error handling. --- src/base.hpp | 1 + src/ui/content_control.cpp | 4 +--- src/ui/control.cpp | 3 --- src/ui/layout_control.cpp | 21 +++++---------------- src/ui/render/render_object.cpp | 21 ++++++--------------- src/ui/render/text_render_object.cpp | 2 -- src/ui/render/window_render_object.cpp | 2 -- src/ui/window.cpp | 25 ++++++++++--------------- src/ui/window_class.cpp | 5 ++++- 9 files changed, 27 insertions(+), 57 deletions(-) (limited to 'src/ui/render/text_render_object.cpp') diff --git a/src/base.hpp b/src/base.hpp index 2c511c4b..e3dfc1ee 100644 --- a/src/base.hpp +++ b/src/base.hpp @@ -1,6 +1,7 @@ #pragma once #include "pre.hpp" +#include #include #include #include diff --git a/src/ui/content_control.cpp b/src/ui/content_control.cpp index 960867b2..d5abca1c 100644 --- a/src/ui/content_control.cpp +++ b/src/ui/content_control.cpp @@ -9,9 +9,7 @@ ContentControl::ContentControl() ContentControl::~ContentControl() { delete child_; } void ContentControl::SetChild(Control* child) { - if (dynamic_cast(child)) - throw std::invalid_argument("Can't add a window as child."); - + assert(!dynamic_cast(child)); // Can't add a window as child. if (child == child_) return; const auto window = GetWindow(); diff --git a/src/ui/control.cpp b/src/ui/control.cpp index e19754dc..5c629fd6 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -1,11 +1,8 @@ #include "control.hpp" -#include - #include "window.hpp" namespace cru::ui { - void Control::_SetParent(Control* parent) { const auto old_parent = GetParent(); parent_ = parent; diff --git a/src/ui/layout_control.cpp b/src/ui/layout_control.cpp index d2b430dd..c0c4a7fe 100644 --- a/src/ui/layout_control.cpp +++ b/src/ui/layout_control.cpp @@ -3,24 +3,14 @@ #include "window.hpp" namespace cru::ui { -void ControlAddChildCheck(Control* control) { - if (control->GetParent() != nullptr) - throw std::invalid_argument("The control already has a parent."); - - if (dynamic_cast(control)) - throw std::invalid_argument("Can't add a window as child."); -} - LayoutControl::~LayoutControl() { for (const auto child : children_) delete child; } void LayoutControl::AddChild(Control* control, const int position) { - ControlAddChildCheck(control); - - if (position < 0 || static_cast(position) > - this->children_.size()) - throw std::invalid_argument("The position is out of range."); + assert(control->GetParent() == nullptr); // The control already has a parent. + assert(!dynamic_cast(control)); // Can't add a window as child. + assert(position >= 0 || position <= this->children_.size()); // The position is out of range. children_.insert(this->children_.cbegin() + position, control); @@ -31,9 +21,8 @@ void LayoutControl::AddChild(Control* control, const int position) { } void LayoutControl::RemoveChild(const int position) { - if (position < 0 || static_castchildren_.size())>(position) >= - this->children_.size()) - throw std::invalid_argument("The position is out of range."); + assert(position >= 0 && + position < this->children_.size()); // The position is out of range. const auto i = children_.cbegin() + position; const auto child = *i; diff --git a/src/ui/render/render_object.cpp b/src/ui/render/render_object.cpp index 0be44c1e..6380c2fe 100644 --- a/src/ui/render/render_object.cpp +++ b/src/ui/render/render_object.cpp @@ -4,15 +4,10 @@ namespace cru::ui::render { void RenderObject::AddChild(RenderObject* render_object, const int position) { - if (render_object->GetParent() != nullptr) - throw std::invalid_argument("Render object already has a parent."); - - if (position < 0) - throw std::invalid_argument("Position index is less than 0."); - - if (static_cast::size_type>(position) > - children_.size()) - throw std::invalid_argument("Position index is out of bound."); + assert(render_object->GetParent() == + nullptr); // Render object already has a parent. + assert(position >= 0); // Position index is less than 0. + assert(position <= children_.size()); // Position index is out of bound. children_.insert(children_.cbegin() + position, render_object); render_object->SetParent(this); @@ -20,12 +15,8 @@ void RenderObject::AddChild(RenderObject* render_object, const int position) { } void RenderObject::RemoveChild(const int position) { - if (position < 0) - throw std::invalid_argument("Position index is less than 0."); - - if (static_cast::size_type>(position) >= - children_.size()) - throw std::invalid_argument("Position index is out of bound."); + assert(position >= 0); // Position index is less than 0. + assert(position < children_.size()); // Position index is out of bound. const auto i = children_.cbegin() + position; const auto removed_child = *i; diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp index e8032f78..b90dae71 100644 --- a/src/ui/render/text_render_object.cpp +++ b/src/ui/render/text_render_object.cpp @@ -1,7 +1,5 @@ #include "text_render_object.hpp" -#include - #include "exception.hpp" #include "graph/graph.hpp" diff --git a/src/ui/render/window_render_object.cpp b/src/ui/render/window_render_object.cpp index 52452bd4..f198c2fa 100644 --- a/src/ui/render/window_render_object.cpp +++ b/src/ui/render/window_render_object.cpp @@ -1,7 +1,5 @@ #include "window_render_object.hpp" -#include - #include "graph/graph.hpp" #include "ui/window.hpp" diff --git a/src/ui/window.cpp b/src/ui/window.cpp index b976ca6a..ca3356ff 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -117,19 +117,14 @@ WindowManager::WindowManager() { } void WindowManager::RegisterWindow(HWND hwnd, Window* window) { - const auto find_result = window_map_.find(hwnd); - if (find_result != window_map_.end()) - throw std::runtime_error("The hwnd is already in the map."); - + assert(window_map_.count(hwnd) == 0); // The hwnd is already in the map. window_map_.emplace(hwnd, window); } void WindowManager::UnregisterWindow(HWND hwnd) { const auto find_result = window_map_.find(hwnd); - if (find_result == window_map_.end()) - throw std::runtime_error("The hwnd is not in the map."); + assert(find_result != window_map_.end()); // The hwnd is not in the map. window_map_.erase(find_result); - if (window_map_.empty()) Application::GetInstance()->Quit(0); } @@ -188,14 +183,15 @@ Window::Window(tag_overlapped_constructor) { CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, Application::GetInstance()->GetInstanceHandle(), nullptr); - if (hwnd_ == nullptr) throw std::runtime_error("Failed to create window."); + if (hwnd_ == nullptr) + throw Win32Error(::GetLastError(), "Failed to create window."); AfterCreateHwnd(window_manager); } Window::Window(tag_popup_constructor, Window* parent, const bool caption) { - if (parent != nullptr && !parent->IsWindowValid()) - throw std::runtime_error("Parent window is not valid."); + assert(parent == nullptr || + parent->IsWindowValid()); // Parent window is not valid. BeforeCreateHwnd(); @@ -210,7 +206,8 @@ Window::Window(tag_popup_constructor, Window* parent, const bool caption) { parent == nullptr ? nullptr : parent->GetWindowHandle(), nullptr, Application::GetInstance()->GetInstanceHandle(), nullptr); - if (hwnd_ == nullptr) throw std::runtime_error("Failed to create window."); + if (hwnd_ == nullptr) + throw Win32Error(::GetLastError(), "Failed to create window."); AfterCreateHwnd(window_manager); } @@ -474,10 +471,8 @@ Point Window::GetMousePosition() { } bool Window::RequestFocusFor(Control* control) { - if (control == nullptr) - throw std::invalid_argument( - "The control to request focus can't be null. You can set it as the " - "window."); + assert(control != nullptr); // The control to request focus can't be null. + // You can set it as the window. if (!IsWindowValid()) return false; diff --git a/src/ui/window_class.cpp b/src/ui/window_class.cpp index 456d9492..5e8b3454 100644 --- a/src/ui/window_class.cpp +++ b/src/ui/window_class.cpp @@ -1,5 +1,7 @@ #include "window_class.hpp" +#include "exception.hpp" + namespace cru::ui { WindowClass::WindowClass(const String& name, WNDPROC window_proc, HINSTANCE h_instance) @@ -20,6 +22,7 @@ WindowClass::WindowClass(const String& name, WNDPROC window_proc, window_class.hIconSm = NULL; atom_ = ::RegisterClassExW(&window_class); - if (atom_ == 0) throw std::runtime_error("Failed to create window class."); + if (atom_ == 0) + throw Win32Error(::GetLastError(), "Failed to create window class."); } } // namespace cru::ui -- cgit v1.2.3 From 3ee6e0bc3675689c5f24d36bbecb7ca1bc93d433 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 23 Mar 2019 18:01:24 +0800 Subject: ... --- src/pre.hpp | 3 +++ src/system_headers.hpp | 3 --- src/ui/render/flex_layout_render_object.cpp | 12 ++++++++++-- src/ui/render/text_render_object.cpp | 22 +++++++++++++++++++--- 4 files changed, 32 insertions(+), 8 deletions(-) (limited to 'src/ui/render/text_render_object.cpp') diff --git a/src/pre.hpp b/src/pre.hpp index dfa0666c..eefc828d 100644 --- a/src/pre.hpp +++ b/src/pre.hpp @@ -4,6 +4,9 @@ #define CRU_DEBUG #endif +#define NOMINMAX +#define WIN32_LEAN_AND_MEAN + #ifdef CRU_DEBUG #define _CRTDBG_MAP_ALLOC #include diff --git a/src/system_headers.hpp b/src/system_headers.hpp index 6cbad697..5517d71a 100644 --- a/src/system_headers.hpp +++ b/src/system_headers.hpp @@ -2,9 +2,6 @@ #include "pre.hpp" //include system headers - -#define NOMINMAX -#define WIN32_LEAN_AND_MEAN #include #include diff --git a/src/ui/render/flex_layout_render_object.cpp b/src/ui/render/flex_layout_render_object.cpp index 5383c462..4708d187 100644 --- a/src/ui/render/flex_layout_render_object.cpp +++ b/src/ui/render/flex_layout_render_object.cpp @@ -33,8 +33,16 @@ RenderObject* FlexLayoutRenderObject::HitTest(const Point& point) { return result; } } - return Rect{Point::Zero(), GetSize()}.IsPointInside(point) ? this : nullptr; -} + + const auto margin = GetMargin(); + const auto size = GetSize(); + return Rect{margin.left, margin.top, + std::max(size.width - margin.GetHorizontalTotal(), 0.0f), + std::max(size.height - margin.GetVerticalTotal(), 0.0f)} + .IsPointInside(point) + ? this + : nullptr; +} // namespace cru::ui::render void FlexLayoutRenderObject::OnAddChild(RenderObject* new_child, int position) { child_layout_data_.emplace(child_layout_data_.cbegin() + position); diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp index b90dae71..d57335ad 100644 --- a/src/ui/render/text_render_object.cpp +++ b/src/ui/render/text_render_object.cpp @@ -1,5 +1,7 @@ #include "text_render_object.hpp" +#include + #include "exception.hpp" #include "graph/graph.hpp" @@ -57,13 +59,27 @@ void TextRenderObject::Draw(ID2D1RenderTarget* render_target) { } RenderObject* TextRenderObject::HitTest(const Point& point) { - return Rect{Point::Zero(), GetSize()}.IsPointInside(point) ? this : nullptr; + const auto margin = GetMargin(); + const auto size = GetSize(); + return Rect{margin.left, margin.top, + std::max(size.width - margin.GetHorizontalTotal(), 0.0f), + std::max(size.height - margin.GetVerticalTotal(), 0.0f)} + .IsPointInside(point) + ? this + : nullptr; } void TextRenderObject::OnSizeChanged(const Size& old_size, const Size& new_size) { - ThrowIfFailed(text_layout_->SetMaxWidth(new_size.width)); - ThrowIfFailed(text_layout_->SetMaxHeight(new_size.height)); + const auto margin = GetMargin(); + const auto padding = GetPadding(); + ThrowIfFailed(text_layout_->SetMaxWidth( + std::max(new_size.width - margin.GetHorizontalTotal() - + padding.GetHorizontalTotal(), + 0.0f))); + ThrowIfFailed(text_layout_->SetMaxHeight(std::max( + new_size.height - margin.GetVerticalTotal() - padding.GetVerticalTotal(), + 0.0f))); } Size TextRenderObject::OnMeasureContent(const Size& available_size) { -- cgit v1.2.3 From e8be3841457853daefc26d0ca00256ad8c44f593 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 23 Mar 2019 23:52:07 +0800 Subject: ... --- CruUI.vcxproj | 15 +++++++++--- CruUI.vcxproj.filters | 21 +++++++++-------- src/application.cpp | 5 ++-- src/application.hpp | 2 +- src/cru_debug.cpp | 2 +- src/exception.hpp | 2 +- src/graph/graph.hpp | 8 +++++-- src/system_headers.hpp | 20 ---------------- src/timer.hpp | 2 +- src/ui/control.cpp | 9 -------- src/ui/control.hpp | 14 ----------- src/ui/controls/button.cpp | 0 src/ui/controls/button.hpp | 8 +++++++ src/ui/controls/text_block.cpp | 2 ++ src/ui/cursor.cpp | 24 ------------------- src/ui/cursor.hpp | 37 ----------------------------- src/ui/d2d_util.hpp | 2 +- src/ui/events/ui_event.cpp | 6 +---- src/ui/events/ui_event.hpp | 45 +++++------------------------------- src/ui/events/window_event.cpp | 3 +++ src/ui/events/window_event.hpp | 42 +++++++++++++++++++++++++++++++++ src/ui/input_util.cpp | 2 +- src/ui/render/text_render_object.cpp | 2 ++ src/ui/ui_manager.cpp | 2 ++ src/ui/ui_manager.hpp | 3 ++- src/ui/window.cpp | 23 ++---------------- src/ui/window.hpp | 6 ++--- src/ui/window_class.hpp | 2 +- src/util/string_util.cpp | 5 ++-- 29 files changed, 114 insertions(+), 200 deletions(-) delete mode 100644 src/system_headers.hpp create mode 100644 src/ui/controls/button.cpp create mode 100644 src/ui/controls/button.hpp delete mode 100644 src/ui/cursor.cpp delete mode 100644 src/ui/cursor.hpp create mode 100644 src/ui/events/window_event.cpp create mode 100644 src/ui/events/window_event.hpp (limited to 'src/ui/render/text_render_object.cpp') diff --git a/CruUI.vcxproj b/CruUI.vcxproj index 374a882c..4143bb43 100644 --- a/CruUI.vcxproj +++ b/CruUI.vcxproj @@ -83,6 +83,7 @@ MachineX86 true Windows + D3D11.lib;D2d1.lib;DWrite.lib;%(AdditionalDependencies) @@ -100,6 +101,7 @@ Windows true true + D3D11.lib;D2d1.lib;DWrite.lib;%(AdditionalDependencies) @@ -108,6 +110,9 @@ stdcpplatest CRU_X64;%(PreprocessorDefinitions) + + D3D11.lib;D2d1.lib;DWrite.lib;%(AdditionalDependencies) + @@ -115,9 +120,14 @@ $(ProjectDir)\src;%(AdditionalIncludeDirectories) CRU_X64;%(PreprocessorDefinitions) + + D3D11.lib;D2d1.lib;DWrite.lib;%(AdditionalDependencies) + + + @@ -135,6 +145,8 @@ + + @@ -144,7 +156,6 @@ - @@ -161,14 +172,12 @@ - - diff --git a/CruUI.vcxproj.filters b/CruUI.vcxproj.filters index 5554927b..01575ce2 100644 --- a/CruUI.vcxproj.filters +++ b/CruUI.vcxproj.filters @@ -42,9 +42,6 @@ Source Files - - Source Files - Source Files @@ -90,6 +87,12 @@ Source Files + + Source Files + + + Source Files + @@ -104,9 +107,6 @@ Header Files - - Header Files - Header Files @@ -128,9 +128,6 @@ Header Files - - Header Files - Header Files @@ -188,6 +185,12 @@ Header Files + + Header Files + + + Header Files + diff --git a/src/application.cpp b/src/application.cpp index e580b56b..aafca6fe 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -1,8 +1,9 @@ #include "application.hpp" +#include + #include "exception.hpp" #include "timer.hpp" -#include "ui/cursor.hpp" #include "ui/window_class.hpp" namespace cru { @@ -79,8 +80,6 @@ Application::Application(HINSTANCE h_instance) : h_instance_(h_instance) { throw std::runtime_error("Must run on Windows 8 or later."); god_window_ = std::make_unique(this); - - ui::cursors::LoadSystemCursors(); } Application::~Application() { diff --git a/src/application.hpp b/src/application.hpp index acf264c3..f5f69ea4 100644 --- a/src/application.hpp +++ b/src/application.hpp @@ -1,12 +1,12 @@ #pragma once #include "pre.hpp" +#include #include #include #include #include #include -#include "system_headers.hpp" #include "base.hpp" diff --git a/src/cru_debug.cpp b/src/cru_debug.cpp index 331d2bce..81945227 100644 --- a/src/cru_debug.cpp +++ b/src/cru_debug.cpp @@ -1,6 +1,6 @@ #include "cru_debug.hpp" -#include "system_headers.hpp" +#include namespace cru::debug { #ifdef CRU_DEBUG diff --git a/src/exception.hpp b/src/exception.hpp index db2572f1..ade51d54 100644 --- a/src/exception.hpp +++ b/src/exception.hpp @@ -1,8 +1,8 @@ #pragma once #include "pre.hpp" +#include #include -#include "system_headers.hpp" #include "base.hpp" diff --git a/src/graph/graph.hpp b/src/graph/graph.hpp index bad5b6d0..af14cc50 100644 --- a/src/graph/graph.hpp +++ b/src/graph/graph.hpp @@ -1,9 +1,13 @@ #pragma once #include "pre.hpp" +#include +#include +#include +#include +#include #include #include -#include "system_headers.hpp" #include "base.hpp" @@ -101,7 +105,7 @@ class GraphManager final : public Object { Microsoft::WRL::ComPtr GetSystemFontCollection() const { - return dwrite_system_font_collection_.Get(); + return dwrite_system_font_collection_; } private: diff --git a/src/system_headers.hpp b/src/system_headers.hpp deleted file mode 100644 index 5517d71a..00000000 --- a/src/system_headers.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "pre.hpp" - -//include system headers -#include -#include - -#pragma comment(lib, "D3D11.lib") -#include - -#pragma comment(lib, "D2d1.lib") -#include - -#pragma comment(lib, "DWrite.lib") -#include - -#include -#include - -#include diff --git a/src/timer.hpp b/src/timer.hpp index 685e83b9..7199adc2 100644 --- a/src/timer.hpp +++ b/src/timer.hpp @@ -1,11 +1,11 @@ #pragma once #include "pre.hpp" +#include #include #include #include #include -#include "system_headers.hpp" #include "base.hpp" diff --git a/src/ui/control.cpp b/src/ui/control.cpp index 5c629fd6..318d591a 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -55,15 +55,6 @@ bool Control::HasFocus() { return window->GetFocusControl() == this; } -void Control::SetCursor(const Cursor::Ptr& cursor) { - if (cursor != cursor_) { - cursor_ = cursor; - const auto window = GetWindow(); - if (window && window->GetMouseHoverControl() == this) - window->UpdateCursor(); - } -} - void Control::OnParentChanged(Control* old_parent, Control* new_parent) {} void Control::OnAttachToWindow(Window* window) {} diff --git a/src/ui/control.hpp b/src/ui/control.hpp index 8454e981..a44399bf 100644 --- a/src/ui/control.hpp +++ b/src/ui/control.hpp @@ -1,12 +1,8 @@ #pragma once #include "pre.hpp" -#include "system_headers.hpp" - #include "base.hpp" -#include "cursor.hpp" #include "events/ui_event.hpp" -#include "input_util.hpp" #include "ui_base.hpp" namespace cru::ui { @@ -60,14 +56,6 @@ class Control : public Object { bool HasFocus(); - //*************** region: cursor *************** - // If cursor is set to null, then it uses parent's cursor. - // Window's cursor can't be null. - public: - Cursor::Ptr GetCursor() const { return cursor_; } - - void SetCursor(const Cursor::Ptr& cursor); - //*************** region: events *************** public: // Raised when mouse enter the control. @@ -107,7 +95,5 @@ class Control : public Object { private: Window* window_ = nullptr; Control* parent_ = nullptr; - - Cursor::Ptr cursor_{}; }; } // namespace cru::ui diff --git a/src/ui/controls/button.cpp b/src/ui/controls/button.cpp new file mode 100644 index 00000000..e69de29b diff --git a/src/ui/controls/button.hpp b/src/ui/controls/button.hpp new file mode 100644 index 00000000..010c3f5b --- /dev/null +++ b/src/ui/controls/button.hpp @@ -0,0 +1,8 @@ +#pragma once +#include "pre.hpp" + +#include "ui/control.hpp" + +namespace cru::ui::controls { + +} diff --git a/src/ui/controls/text_block.cpp b/src/ui/controls/text_block.cpp index c891b832..85116910 100644 --- a/src/ui/controls/text_block.cpp +++ b/src/ui/controls/text_block.cpp @@ -1,5 +1,7 @@ #include "text_block.hpp" +#include + #include "ui/render/text_render_object.hpp" #include "ui/ui_manager.hpp" diff --git a/src/ui/cursor.cpp b/src/ui/cursor.cpp deleted file mode 100644 index d8c362ed..00000000 --- a/src/ui/cursor.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "cursor.hpp" - -#include "exception.hpp" - -namespace cru::ui { -Cursor::Cursor(HCURSOR handle, const bool auto_release) - : handle_(handle), auto_release_(auto_release) {} - -Cursor::~Cursor() { - if (auto_release_) ::DestroyCursor(handle_); -} - -namespace cursors { -Cursor::Ptr arrow{}; -Cursor::Ptr hand{}; -Cursor::Ptr i_beam{}; - -void LoadSystemCursors() { - arrow = std::make_shared(::LoadCursorW(nullptr, IDC_ARROW), false); - hand = std::make_shared(::LoadCursorW(nullptr, IDC_HAND), false); - i_beam = std::make_shared(::LoadCursorW(nullptr, IDC_IBEAM), false); -} -} // namespace cursors -} // namespace cru::ui diff --git a/src/ui/cursor.hpp b/src/ui/cursor.hpp deleted file mode 100644 index aec3fc40..00000000 --- a/src/ui/cursor.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -// ReSharper disable once CppUnusedIncludeDirective -#include "pre.hpp" - -#include -#include "system_headers.hpp" - -#include "base.hpp" - -namespace cru::ui { -class Cursor : public Object { - public: - using Ptr = std::shared_ptr; - - Cursor(HCURSOR handle, bool auto_release); - Cursor(const Cursor& other) = delete; - Cursor(Cursor&& other) = delete; - Cursor& operator=(const Cursor& other) = delete; - Cursor& operator=(Cursor&& other) = delete; - ~Cursor() override; - - HCURSOR GetHandle() const { return handle_; } - - private: - HCURSOR handle_; - bool auto_release_; -}; - -namespace cursors { -extern Cursor::Ptr arrow; -extern Cursor::Ptr hand; -extern Cursor::Ptr i_beam; - -void LoadSystemCursors(); -} // namespace cursors -} // namespace cru::ui diff --git a/src/ui/d2d_util.hpp b/src/ui/d2d_util.hpp index 96a017dc..2ec8ba98 100644 --- a/src/ui/d2d_util.hpp +++ b/src/ui/d2d_util.hpp @@ -1,7 +1,7 @@ #pragma once #include "pre.hpp" -#include "system_headers.hpp" +#include #include "ui_base.hpp" diff --git a/src/ui/events/ui_event.cpp b/src/ui/events/ui_event.cpp index ee3a68dc..78d56a83 100644 --- a/src/ui/events/ui_event.cpp +++ b/src/ui/events/ui_event.cpp @@ -1,7 +1,3 @@ #include "ui_event.hpp" -#include "ui/control.hpp" - -namespace cru::ui::events -{ -} +namespace cru::ui::events {} diff --git a/src/ui/events/ui_event.hpp b/src/ui/events/ui_event.hpp index 572cf8d6..7fe4e6eb 100644 --- a/src/ui/events/ui_event.hpp +++ b/src/ui/events/ui_event.hpp @@ -2,13 +2,14 @@ #include "pre.hpp" #include -#include "system_headers.hpp" #include "base.hpp" #include "cru_event.hpp" #include "ui/ui_base.hpp" #include "ui/input_util.hpp" +struct ID2D1RenderTarget; + namespace cru::ui { class Control; } @@ -112,18 +113,18 @@ class MouseWheelEventArgs : public MouseEventArgs { class DrawEventArgs : public UiEventArgs { public: DrawEventArgs(Object* sender, Object* original_sender, - ID2D1DeviceContext* device_context) - : UiEventArgs(sender, original_sender), device_context_(device_context) {} + ID2D1RenderTarget* render_target) + : UiEventArgs(sender, original_sender), render_target_(render_target) {} DrawEventArgs(const DrawEventArgs& other) = default; DrawEventArgs(DrawEventArgs&& other) = default; DrawEventArgs& operator=(const DrawEventArgs& other) = default; DrawEventArgs& operator=(DrawEventArgs&& other) = default; ~DrawEventArgs() = default; - ID2D1DeviceContext* GetDeviceContext() const { return device_context_; } + ID2D1RenderTarget* GetRenderTarget() const { return render_target_; } private: - ID2D1DeviceContext* device_context_; + ID2D1RenderTarget* render_target_; }; class FocusChangeEventArgs : public UiEventArgs { @@ -160,40 +161,6 @@ class ToggleEventArgs : public UiEventArgs { bool new_state_; }; -struct WindowNativeMessage { - HWND hwnd; - int msg; - WPARAM w_param; - LPARAM l_param; -}; - -class WindowNativeMessageEventArgs : public UiEventArgs { - public: - WindowNativeMessageEventArgs(Object* sender, Object* original_sender, - const WindowNativeMessage& message) - : UiEventArgs(sender, original_sender), - message_(message), - result_(std::nullopt) {} - WindowNativeMessageEventArgs(const WindowNativeMessageEventArgs& other) = - default; - WindowNativeMessageEventArgs(WindowNativeMessageEventArgs&& other) = default; - WindowNativeMessageEventArgs& operator=( - const WindowNativeMessageEventArgs& other) = default; - WindowNativeMessageEventArgs& operator=( - WindowNativeMessageEventArgs&& other) = default; - ~WindowNativeMessageEventArgs() override = default; - - WindowNativeMessage GetWindowMessage() const { return message_; } - - std::optional GetResult() const { return result_; } - - void SetResult(const std::optional result) { result_ = result; } - - private: - WindowNativeMessage message_; - std::optional result_; -}; - class KeyEventArgs : public UiEventArgs { public: KeyEventArgs(Object* sender, Object* original_sender, int virtual_code) diff --git a/src/ui/events/window_event.cpp b/src/ui/events/window_event.cpp new file mode 100644 index 00000000..a217136c --- /dev/null +++ b/src/ui/events/window_event.cpp @@ -0,0 +1,3 @@ +#include "window_event.hpp" + +namespace cru::ui::events {} diff --git a/src/ui/events/window_event.hpp b/src/ui/events/window_event.hpp new file mode 100644 index 00000000..21c644af --- /dev/null +++ b/src/ui/events/window_event.hpp @@ -0,0 +1,42 @@ +#pragma once +#include "pre.hpp" + +#include + +#include "ui_event.hpp" + +namespace cru::ui::events { +struct WindowNativeMessage { + HWND hwnd; + int msg; + WPARAM w_param; + LPARAM l_param; +}; + +class WindowNativeMessageEventArgs : public UiEventArgs { + public: + WindowNativeMessageEventArgs(Object* sender, Object* original_sender, + const WindowNativeMessage& message) + : UiEventArgs(sender, original_sender), + message_(message), + result_(std::nullopt) {} + WindowNativeMessageEventArgs(const WindowNativeMessageEventArgs& other) = + default; + WindowNativeMessageEventArgs(WindowNativeMessageEventArgs&& other) = default; + WindowNativeMessageEventArgs& operator=( + const WindowNativeMessageEventArgs& other) = default; + WindowNativeMessageEventArgs& operator=( + WindowNativeMessageEventArgs&& other) = default; + ~WindowNativeMessageEventArgs() override = default; + + WindowNativeMessage GetWindowMessage() const { return message_; } + + std::optional GetResult() const { return result_; } + + void SetResult(const std::optional result) { result_ = result; } + + private: + WindowNativeMessage message_; + std::optional result_; +}; +} diff --git a/src/ui/input_util.cpp b/src/ui/input_util.cpp index 3fe34f10..193cba4a 100644 --- a/src/ui/input_util.cpp +++ b/src/ui/input_util.cpp @@ -1,6 +1,6 @@ #include "input_util.hpp" -#include "system_headers.hpp" +#include namespace cru::ui { bool IsKeyDown(const int virtual_code) { diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp index d57335ad..e8967d48 100644 --- a/src/ui/render/text_render_object.cpp +++ b/src/ui/render/text_render_object.cpp @@ -1,5 +1,7 @@ #include "text_render_object.hpp" +#include +#include #include #include "exception.hpp" diff --git a/src/ui/ui_manager.cpp b/src/ui/ui_manager.cpp index bcda4133..26b1fe62 100644 --- a/src/ui/ui_manager.cpp +++ b/src/ui/ui_manager.cpp @@ -1,5 +1,7 @@ #include "ui_manager.hpp" +#include + #include "application.hpp" #include "exception.hpp" #include "graph/graph.hpp" diff --git a/src/ui/ui_manager.hpp b/src/ui/ui_manager.hpp index 3fd2adc9..c2331dd4 100644 --- a/src/ui/ui_manager.hpp +++ b/src/ui/ui_manager.hpp @@ -1,7 +1,8 @@ #pragma once #include "pre.hpp" -#include "system_headers.hpp" +#include +#include #include "base.hpp" diff --git a/src/ui/window.cpp b/src/ui/window.cpp index ca3356ff..7b00ca05 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -1,7 +1,8 @@ #include "window.hpp" +#include + #include "application.hpp" -#include "cursor.hpp" #include "exception.hpp" #include "graph/graph.hpp" #include "render/window_render_object.hpp" @@ -153,17 +154,6 @@ inline POINT DipToPi(const Point& dip_point) { return result; } -namespace { -Cursor::Ptr GetCursorInherit(Control* control) { - while (control != nullptr) { - const auto cursor = control->GetCursor(); - if (cursor != nullptr) return cursor; - control = control->GetParent(); - } - return cursors::arrow; -} -} // namespace - Window* Window::CreateOverlapped() { return new Window(tag_overlapped_constructor{}); } @@ -220,8 +210,6 @@ void Window::AfterCreateHwnd(WindowManager* window_manager) { render_target_ = graph::GraphManager::GetInstance()->CreateWindowRenderTarget(hwnd_); - SetCursor(cursors::arrow); - render_object_ = new render::WindowRenderObject(this); } @@ -522,12 +510,6 @@ Control* Window::ReleaseCurrentMouseCapture() { } } -void Window::UpdateCursor() { - if (IsWindowValid() && mouse_hover_control_ != nullptr) { - SetCursorInternal(GetCursorInherit(mouse_hover_control_)->GetHandle()); - } -} - #ifdef CRU_DEBUG_LAYOUT void Window::SetDebugLayout(const bool value) { if (debug_layout_ != value) { @@ -719,7 +701,6 @@ void Window::DispatchMouseHoverControlChangeEvent(Control* old_control, DispatchEvent(new_control, &Control::mouse_enter_event, lowest_common_ancestor, point); // dispatch mouse enter event. - UpdateCursor(); } } } diff --git a/src/ui/window.hpp b/src/ui/window.hpp index 3e0422b1..1c48bf43 100644 --- a/src/ui/window.hpp +++ b/src/ui/window.hpp @@ -1,12 +1,13 @@ #pragma once #include "pre.hpp" +#include #include #include -#include "system_headers.hpp" #include "content_control.hpp" #include "events/ui_event.hpp" +#include "events/window_event.hpp" #include "window_class.hpp" namespace cru::graph { @@ -164,9 +165,6 @@ class Window final : public ContentControl { Control* CaptureMouseFor(Control* control); Control* ReleaseCurrentMouseCapture(); - //*************** region: cursor *************** - void UpdateCursor(); - public: //*************** region: events *************** Event activated_event; diff --git a/src/ui/window_class.hpp b/src/ui/window_class.hpp index 66babd94..72a7c431 100644 --- a/src/ui/window_class.hpp +++ b/src/ui/window_class.hpp @@ -1,7 +1,7 @@ #pragma once #include "pre.hpp" -#include "system_headers.hpp" +#include #include "base.hpp" diff --git a/src/util/string_util.cpp b/src/util/string_util.cpp index 3c765259..c9391fc6 100644 --- a/src/util/string_util.cpp +++ b/src/util/string_util.cpp @@ -1,6 +1,7 @@ #include "string_util.hpp" -#include "system_headers.hpp" +#include + #include "exception.hpp" namespace cru::util { @@ -18,4 +19,4 @@ MultiByteString ToUtf8String(const StringView& string) { "Failed to convert wide string to UTF-8."); return result; } -} +} // namespace cru::util -- cgit v1.2.3 From 79d1d76509dbf6cf9c79f8eb55968535982975aa Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 24 Mar 2019 19:06:17 +0800 Subject: ... --- CruUI.vcxproj | 8 +- CruUI.vcxproj.filters | 24 +++- src/graph/graph.cpp | 161 --------------------------- src/graph/graph.hpp | 164 ---------------------------- src/graph/graph_manager.cpp | 79 ++++++++++++++ src/graph/graph_manager.hpp | 60 ++++++++++ src/graph/graph_util.hpp | 63 +++++++++++ src/graph/window_render_target.cpp | 99 +++++++++++++++++ src/graph/window_render_target.hpp | 49 +++++++++ src/ui/controls/button.cpp | 13 +++ src/ui/controls/button.hpp | 31 +++++- src/ui/controls/text_block.cpp | 2 - src/ui/render/border_render_object.cpp | 52 +++++++-- src/ui/render/border_render_object.hpp | 22 ++-- src/ui/render/flex_layout_render_object.cpp | 2 +- src/ui/render/render_object.cpp | 3 - src/ui/render/render_object.hpp | 2 - src/ui/render/text_render_object.cpp | 67 +++++++++--- src/ui/render/text_render_object.hpp | 39 +++---- src/ui/render/window_render_object.cpp | 2 +- src/ui/ui_manager.cpp | 57 +++++----- src/ui/ui_manager.hpp | 15 +-- src/ui/window.cpp | 22 ++-- src/ui/window.hpp | 3 +- src/util/com_util.hpp | 22 ++++ 25 files changed, 602 insertions(+), 459 deletions(-) delete mode 100644 src/graph/graph.cpp delete mode 100644 src/graph/graph.hpp create mode 100644 src/graph/graph_manager.cpp create mode 100644 src/graph/graph_manager.hpp create mode 100644 src/graph/graph_util.hpp create mode 100644 src/graph/window_render_target.cpp create mode 100644 src/graph/window_render_target.hpp create mode 100644 src/util/com_util.hpp (limited to 'src/ui/render/text_render_object.cpp') diff --git a/CruUI.vcxproj b/CruUI.vcxproj index 4143bb43..24b6957e 100644 --- a/CruUI.vcxproj +++ b/CruUI.vcxproj @@ -125,6 +125,7 @@ + @@ -137,13 +138,15 @@ - + + + @@ -153,6 +156,7 @@ + @@ -171,7 +175,7 @@ - + diff --git a/CruUI.vcxproj.filters b/CruUI.vcxproj.filters index 01575ce2..68dc97ba 100644 --- a/CruUI.vcxproj.filters +++ b/CruUI.vcxproj.filters @@ -27,9 +27,6 @@ Source Files - - Source Files - Source Files @@ -93,11 +90,14 @@ Source Files + + Source Files + + + Source Files + - - Header Files - Header Files @@ -191,6 +191,18 @@ Header Files + + Header Files + + + Header Files + + + Header Files + + + Header Files + diff --git a/src/graph/graph.cpp b/src/graph/graph.cpp deleted file mode 100644 index ed3fe5d5..00000000 --- a/src/graph/graph.cpp +++ /dev/null @@ -1,161 +0,0 @@ -#include "graph.hpp" - -#include "application.hpp" -#include "exception.hpp" - -namespace cru::graph { -using Microsoft::WRL::ComPtr; - -WindowRenderTarget::WindowRenderTarget(GraphManager* graph_manager, HWND hwnd) { - this->graph_manager_ = graph_manager; - - const auto d3d11_device = graph_manager->GetD3D11Device(); - const auto dxgi_factory = graph_manager->GetDxgiFactory(); - - // Allocate a descriptor. - DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {0}; - swap_chain_desc.Width = 0; // use automatic sizing - swap_chain_desc.Height = 0; - swap_chain_desc.Format = - DXGI_FORMAT_B8G8R8A8_UNORM; // this is the most common swapchain format - swap_chain_desc.Stereo = false; - swap_chain_desc.SampleDesc.Count = 1; // don't use multi-sampling - swap_chain_desc.SampleDesc.Quality = 0; - swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; - swap_chain_desc.BufferCount = 2; // use double buffering to enable flip - swap_chain_desc.Scaling = DXGI_SCALING_NONE; - swap_chain_desc.SwapEffect = - DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // all apps must use this SwapEffect - swap_chain_desc.Flags = 0; - - // Get the final swap chain for this window from the DXGI factory. - ThrowIfFailed(dxgi_factory->CreateSwapChainForHwnd( - d3d11_device.Get(), hwnd, &swap_chain_desc, nullptr, nullptr, - &dxgi_swap_chain_)); - - CreateTargetBitmap(); -} - -WindowRenderTarget::~WindowRenderTarget() {} - -void WindowRenderTarget::ResizeBuffer(const int width, const int height) { - const auto graph_manager = graph_manager_; - const auto d2d1_device_context = graph_manager->GetD2D1DeviceContext(); - - ComPtr old_target; - d2d1_device_context->GetTarget(&old_target); - const auto target_this = old_target == this->target_bitmap_; - if (target_this) d2d1_device_context->SetTarget(nullptr); - - old_target = nullptr; - target_bitmap_ = nullptr; - - ThrowIfFailed(dxgi_swap_chain_->ResizeBuffers(0, width, height, - DXGI_FORMAT_UNKNOWN, 0)); - - CreateTargetBitmap(); - - if (target_this) d2d1_device_context->SetTarget(target_bitmap_.Get()); -} - -void WindowRenderTarget::SetAsTarget() { - GetD2DDeviceContext()->SetTarget(target_bitmap_.Get()); -} - -void WindowRenderTarget::Present() { - ThrowIfFailed(dxgi_swap_chain_->Present(1, 0)); -} - -void WindowRenderTarget::CreateTargetBitmap() { - // Direct2D needs the dxgi version of the backbuffer surface pointer. - ComPtr dxgiBackBuffer; - ThrowIfFailed(dxgi_swap_chain_->GetBuffer(0, IID_PPV_ARGS(&dxgiBackBuffer))); - - const auto dpi = graph_manager_->GetDpi(); - - auto bitmap_properties = D2D1::BitmapProperties1( - D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW, - D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE), - dpi.x, dpi.y); - - // Get a D2D surface from the DXGI back buffer to use as the D2D render - // target. - ThrowIfFailed( - graph_manager_->GetD2D1DeviceContext()->CreateBitmapFromDxgiSurface( - dxgiBackBuffer.Get(), &bitmap_properties, &target_bitmap_)); -} - -GraphManager* GraphManager::GetInstance() { - return Application::GetInstance()->ResolveSingleton( - [](auto) { return new GraphManager{}; }); -} - -GraphManager::GraphManager() { - UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; - -#ifdef CRU_DEBUG - creation_flags |= D3D11_CREATE_DEVICE_DEBUG; -#endif - - const D3D_FEATURE_LEVEL feature_levels[] = { - D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, - D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2, - D3D_FEATURE_LEVEL_9_1}; - - ThrowIfFailed(D3D11CreateDevice( - nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, creation_flags, - feature_levels, ARRAYSIZE(feature_levels), D3D11_SDK_VERSION, - &d3d11_device_, nullptr, &d3d11_device_context_)); - - Microsoft::WRL::ComPtr dxgi_device; - - ThrowIfFailed(d3d11_device_.As(&dxgi_device)); - - ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, - __uuidof(ID2D1Factory1), &d2d1_factory_)); - - ThrowIfFailed(d2d1_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device_)); - - ThrowIfFailed(d2d1_device_->CreateDeviceContext( - D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context_)); - - // Identify the physical adapter (GPU or card) this device is runs on. - ComPtr dxgi_adapter; - ThrowIfFailed(dxgi_device->GetAdapter(&dxgi_adapter)); - - // Get the factory object that created the DXGI device. - ThrowIfFailed(dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory_))); - - ThrowIfFailed(DWriteCreateFactory( - DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), - reinterpret_cast(dwrite_factory_.GetAddressOf()))); - - dwrite_factory_->GetSystemFontCollection(&dwrite_system_font_collection_); -} - -GraphManager::~GraphManager() {} - -std::shared_ptr GraphManager::CreateWindowRenderTarget( - HWND hwnd) { - return std::make_shared(this, hwnd); -} - -Dpi GraphManager::GetDpi() const { - Dpi dpi; - d2d1_factory_->GetDesktopDpi(&dpi.x, &dpi.y); - return dpi; -} - -void GraphManager::ReloadSystemMetrics() { - ThrowIfFailed(d2d1_factory_->ReloadSystemMetrics()); -} - -Microsoft::WRL::ComPtr CreateSolidColorBrush( - const D2D1_COLOR_F& color) { - Microsoft::WRL::ComPtr brush; - ThrowIfFailed(GraphManager::GetInstance() - ->GetD2D1DeviceContext() - ->CreateSolidColorBrush(color, &brush)); - return brush; -} -} // namespace cru::graph diff --git a/src/graph/graph.hpp b/src/graph/graph.hpp deleted file mode 100644 index af14cc50..00000000 --- a/src/graph/graph.hpp +++ /dev/null @@ -1,164 +0,0 @@ -#pragma once -#include "pre.hpp" - -#include -#include -#include -#include -#include -#include -#include - -#include "base.hpp" - -namespace cru::graph { -class GraphManager; - -// Represents a window render target. -class WindowRenderTarget : public Object { - public: - WindowRenderTarget(GraphManager* graph_manager, HWND hwnd); - WindowRenderTarget(const WindowRenderTarget& other) = delete; - WindowRenderTarget(WindowRenderTarget&& other) = delete; - WindowRenderTarget& operator=(const WindowRenderTarget& other) = delete; - WindowRenderTarget& operator=(WindowRenderTarget&& other) = delete; - ~WindowRenderTarget() override; - - public: - // Get the graph manager that created the render target. - GraphManager* GetGraphManager() const { return graph_manager_; } - - // Get the d2d device context. - inline Microsoft::WRL::ComPtr GetD2DDeviceContext() const; - - // Get the target bitmap which can be set as the ID2D1DeviceContext's target. - Microsoft::WRL::ComPtr GetTargetBitmap() const { - return target_bitmap_; - } - - // Resize the underlying buffer. - void ResizeBuffer(int width, int height); - - // Set this render target as the d2d device context's target. - void SetAsTarget(); - - // Present the data of the underlying buffer to the window. - void Present(); - - private: - void CreateTargetBitmap(); - - private: - GraphManager* graph_manager_; - Microsoft::WRL::ComPtr dxgi_swap_chain_; - Microsoft::WRL::ComPtr target_bitmap_; -}; - -struct Dpi { - float x; - float y; -}; - -class GraphManager final : public Object { - public: - static GraphManager* GetInstance(); - - private: - GraphManager(); - - public: - GraphManager(const GraphManager& other) = delete; - GraphManager(GraphManager&& other) = delete; - GraphManager& operator=(const GraphManager& other) = delete; - GraphManager& operator=(GraphManager&& other) = delete; - ~GraphManager() override; - - public: - Microsoft::WRL::ComPtr GetD2D1Factory() const { - return d2d1_factory_; - } - - Microsoft::WRL::ComPtr GetD2D1DeviceContext() const { - return d2d1_device_context_; - } - - Microsoft::WRL::ComPtr GetD3D11Device() const { - return d3d11_device_; - } - - Microsoft::WRL::ComPtr GetDxgiFactory() const { - return dxgi_factory_; - } - - Microsoft::WRL::ComPtr GetDWriteFactory() const { - return dwrite_factory_; - } - - // Create a window render target with the HWND. - std::shared_ptr CreateWindowRenderTarget(HWND hwnd); - - // Get the desktop dpi. - Dpi GetDpi() const; - - // Reload system metrics including desktop dpi. - void ReloadSystemMetrics(); - - Microsoft::WRL::ComPtr GetSystemFontCollection() - const { - return dwrite_system_font_collection_; - } - - private: - Microsoft::WRL::ComPtr d3d11_device_; - Microsoft::WRL::ComPtr d3d11_device_context_; - Microsoft::WRL::ComPtr d2d1_factory_; - Microsoft::WRL::ComPtr d2d1_device_; - Microsoft::WRL::ComPtr d2d1_device_context_; - Microsoft::WRL::ComPtr dxgi_factory_; - - Microsoft::WRL::ComPtr dwrite_factory_; - Microsoft::WRL::ComPtr dwrite_system_font_collection_; -}; - -inline int DipToPixelInternal(const float dip, const float dpi) { - return static_cast(dip * dpi / 96.0f); -} - -inline int DipToPixelX(const float dip_x) { - return DipToPixelInternal(dip_x, GraphManager::GetInstance()->GetDpi().x); -} - -inline int DipToPixelY(const float dip_y) { - return DipToPixelInternal(dip_y, GraphManager::GetInstance()->GetDpi().y); -} - -inline float DipToPixelInternal(const int pixel, const float dpi) { - return static_cast(pixel) * 96.0f / dpi; -} - -inline float PixelToDipX(const int pixel_x) { - return DipToPixelInternal(pixel_x, GraphManager::GetInstance()->GetDpi().x); -} - -inline float PixelToDipY(const int pixel_y) { - return DipToPixelInternal(pixel_y, GraphManager::GetInstance()->GetDpi().y); -} - -Microsoft::WRL::ComPtr -WindowRenderTarget::GetD2DDeviceContext() const { - return graph_manager_->GetD2D1DeviceContext(); -} - -inline void WithTransform( - ID2D1RenderTarget* device_context, const D2D1_MATRIX_3X2_F matrix, - const std::function& action) { - D2D1_MATRIX_3X2_F old_transform; - device_context->GetTransform(&old_transform); - device_context->SetTransform(old_transform * matrix); - action(device_context); - device_context->SetTransform(old_transform); -} - -Microsoft::WRL::ComPtr CreateSolidColorBrush( - const D2D1_COLOR_F& color); -} // namespace cru::graph diff --git a/src/graph/graph_manager.cpp b/src/graph/graph_manager.cpp new file mode 100644 index 00000000..ecc60915 --- /dev/null +++ b/src/graph/graph_manager.cpp @@ -0,0 +1,79 @@ +#include "graph_manager.hpp" + +#include +#include +#include +#include +#include + +#include "application.hpp" +#include "exception.hpp" +#include "util/com_util.hpp" +#include "window_render_target.hpp" + +namespace cru::graph { + +GraphManager* GraphManager::GetInstance() { + return Application::GetInstance()->ResolveSingleton( + [](auto) { return new GraphManager{}; }); +} + +GraphManager::GraphManager() { + UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; + +#ifdef CRU_DEBUG + creation_flags |= D3D11_CREATE_DEVICE_DEBUG; +#endif + + const D3D_FEATURE_LEVEL feature_levels[] = { + D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, + D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2, + D3D_FEATURE_LEVEL_9_1}; + + Microsoft::WRL::ComPtr d3d11_device_context; + ID3D11Device* d3d11_device; + + ThrowIfFailed(D3D11CreateDevice( + nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, creation_flags, + feature_levels, ARRAYSIZE(feature_levels), D3D11_SDK_VERSION, + &d3d11_device, nullptr, &d3d11_device_context)); + this->d3d11_device_ = util::CreateComSharedPtr(d3d11_device); + + Microsoft::WRL::ComPtr dxgi_device; + ThrowIfFailed(d3d11_device_->QueryInterface(dxgi_device.GetAddressOf())); + + ID2D1Factory1* d2d1_factory; + ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, + IID_PPV_ARGS(&d2d1_factory))); + this->d2d1_factory_ = util::CreateComSharedPtr(d2d1_factory); + + Microsoft::WRL::ComPtr d2d1_device; + + ThrowIfFailed(d2d1_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device)); + + ID2D1DeviceContext* d2d1_device_context; + ThrowIfFailed(d2d1_device->CreateDeviceContext( + D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context)); + this->d2d1_device_context_ = util::CreateComSharedPtr(d2d1_device_context); + + // Identify the physical adapter (GPU or card) this device is runs on. + Microsoft::WRL::ComPtr dxgi_adapter; + ThrowIfFailed(dxgi_device->GetAdapter(&dxgi_adapter)); + + IDXGIFactory2* dxgi_factory; + // Get the factory object that created the DXGI device. + ThrowIfFailed(dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory))); + this->dxgi_factory_ = util::CreateComSharedPtr(dxgi_factory); + + IDWriteFactory* dwrite_factory; + ThrowIfFailed( + DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), + reinterpret_cast(&dwrite_factory))); + this->dwrite_factory_ = util::CreateComSharedPtr(dwrite_factory); + + IDWriteFontCollection* font_collection; + ThrowIfFailed(dwrite_factory_->GetSystemFontCollection(&font_collection)); + this->dwrite_system_font_collection_ = + util::CreateComSharedPtr(font_collection); +} +} // namespace cru::graph diff --git a/src/graph/graph_manager.hpp b/src/graph/graph_manager.hpp new file mode 100644 index 00000000..4a1e7153 --- /dev/null +++ b/src/graph/graph_manager.hpp @@ -0,0 +1,60 @@ +#pragma once +#include "pre.hpp" + +#include + +#include "base.hpp" + +struct ID3D11Device; +struct ID3D11DeviceContext; +struct ID2D1Factory1; +struct ID2D1DeviceContext; +struct IDXGIFactory2; +struct IDWriteFontCollection; +struct IDWriteFactory; + +struct ID2D1RenderTarget; + +namespace cru::graph { +class WindowRenderTarget; + +class GraphManager final : public Object { + public: + static GraphManager* GetInstance(); + + private: + GraphManager(); + + public: + 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(); + } + + private: + std::shared_ptr d3d11_device_; + std::shared_ptr d2d1_factory_; + std::shared_ptr d2d1_device_context_; + std::shared_ptr dxgi_factory_; + std::shared_ptr dwrite_factory_; + std::shared_ptr dwrite_system_font_collection_; +}; +} // namespace cru::graph diff --git a/src/graph/graph_util.hpp b/src/graph/graph_util.hpp new file mode 100644 index 00000000..2d5be5f3 --- /dev/null +++ b/src/graph/graph_util.hpp @@ -0,0 +1,63 @@ +#pragma once +#include "pre.hpp" + +#include +#include + +#include "exception.hpp" +#include "graph_manager.hpp" + +namespace cru::graph { +struct Dpi { + float x; + float y; +}; + +inline Dpi GetDpi() { + Dpi dpi; + GraphManager::GetInstance()->GetD2D1Factory()->GetDesktopDpi(&dpi.x, &dpi.y); + return dpi; +} + +inline int DipToPixelInternal(const float dip, const float dpi) { + return static_cast(dip * dpi / 96.0f); +} + +inline int DipToPixelX(const float dip_x) { + return DipToPixelInternal(dip_x, GetDpi().x); +} + +inline int DipToPixelY(const float dip_y) { + return DipToPixelInternal(dip_y, GetDpi().y); +} + +inline float DipToPixelInternal(const int pixel, const float dpi) { + return static_cast(pixel) * 96.0f / dpi; +} + +inline float PixelToDipX(const int pixel_x) { + return DipToPixelInternal(pixel_x, GetDpi().x); +} + +inline float PixelToDipY(const int pixel_y) { + return DipToPixelInternal(pixel_y, GetDpi().y); +} + +inline void WithTransform( + ID2D1RenderTarget* render_target, const D2D1_MATRIX_3X2_F matrix, + const std::function& action) { + D2D1_MATRIX_3X2_F old_transform; + render_target->GetTransform(&old_transform); + render_target->SetTransform(old_transform * matrix); + action(render_target); + render_target->SetTransform(old_transform); +} + +inline ID2D1SolidColorBrush* CreateSolidColorBrush(const D2D1_COLOR_F& color) { + ID2D1SolidColorBrush* brush; + ThrowIfFailed(GraphManager::GetInstance() + ->GetD2D1DeviceContext() + ->CreateSolidColorBrush(color, &brush)); + return brush; +} +} // namespace cru::graph diff --git a/src/graph/window_render_target.cpp b/src/graph/window_render_target.cpp new file mode 100644 index 00000000..d110101a --- /dev/null +++ b/src/graph/window_render_target.cpp @@ -0,0 +1,99 @@ +#include "window_render_target.hpp" + +#include +#include +#include +#include + +#include "exception.hpp" +#include "graph_manager.hpp" +#include "graph_util.hpp" +#include "util/com_util.hpp" + +namespace cru::graph { +WindowRenderTarget::WindowRenderTarget(GraphManager* graph_manager, HWND hwnd) { + this->graph_manager_ = graph_manager; + + const auto d3d11_device = graph_manager->GetD3D11Device(); + const auto dxgi_factory = graph_manager->GetDxgiFactory(); + + // Allocate a descriptor. + DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {0}; + swap_chain_desc.Width = 0; // use automatic sizing + swap_chain_desc.Height = 0; + swap_chain_desc.Format = + DXGI_FORMAT_B8G8R8A8_UNORM; // this is the most common swapchain format + swap_chain_desc.Stereo = false; + swap_chain_desc.SampleDesc.Count = 1; // don't use multi-sampling + swap_chain_desc.SampleDesc.Quality = 0; + swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + swap_chain_desc.BufferCount = 2; // use double buffering to enable flip + swap_chain_desc.Scaling = DXGI_SCALING_NONE; + swap_chain_desc.SwapEffect = + DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // all apps must use this SwapEffect + swap_chain_desc.Flags = 0; + + IDXGISwapChain1* dxgi_swap_chain; + // Get the final swap chain for this window from the DXGI factory. + ThrowIfFailed( + dxgi_factory->CreateSwapChainForHwnd(d3d11_device, hwnd, &swap_chain_desc, + nullptr, nullptr, &dxgi_swap_chain)); + this->dxgi_swap_chain_ = util::CreateComSharedPtr(dxgi_swap_chain); + + CreateTargetBitmap(); +} + +void WindowRenderTarget::ResizeBuffer(const int width, const int height) { + const auto graph_manager = graph_manager_; + const auto d2d1_device_context = graph_manager->GetD2D1DeviceContext(); + + ID2D1Image* old_target; + d2d1_device_context->GetTarget(&old_target); + const auto target_this = old_target == this->target_bitmap_.get(); + if (target_this) d2d1_device_context->SetTarget(nullptr); + + util::SafeRelease(old_target); + target_bitmap_.reset(); + + ThrowIfFailed(dxgi_swap_chain_->ResizeBuffers(0, width, height, + DXGI_FORMAT_UNKNOWN, 0)); + + CreateTargetBitmap(); + + if (target_this) d2d1_device_context->SetTarget(target_bitmap_.get()); +} + +void WindowRenderTarget::SetAsTarget() { + graph_manager_->GetD2D1DeviceContext()->SetTarget(target_bitmap_.get()); +} + +void WindowRenderTarget::Present() { + ThrowIfFailed(dxgi_swap_chain_->Present(1, 0)); +} + +void WindowRenderTarget::CreateTargetBitmap() { + assert(target_bitmap_ == nullptr); // target bitmap must not exist. + + // Direct2D needs the dxgi version of the backbuffer surface pointer. + Microsoft::WRL::ComPtr dxgi_back_buffer; + ThrowIfFailed( + dxgi_swap_chain_->GetBuffer(0, IID_PPV_ARGS(&dxgi_back_buffer))); + + const auto dpi = GetDpi(); + + auto bitmap_properties = D2D1::BitmapProperties1( + D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW, + D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE), + dpi.x, dpi.y); + + ID2D1Bitmap1* bitmap; + // Get a D2D surface from the DXGI back buffer to use as the D2D render + // target. + ThrowIfFailed( + graph_manager_->GetD2D1DeviceContext()->CreateBitmapFromDxgiSurface( + dxgi_back_buffer.Get(), &bitmap_properties, &bitmap)); + this->target_bitmap_ = util::CreateComSharedPtr(bitmap); + + dxgi_back_buffer->Release(); +} +} // namespace cru::graph diff --git a/src/graph/window_render_target.hpp b/src/graph/window_render_target.hpp new file mode 100644 index 00000000..9b93df19 --- /dev/null +++ b/src/graph/window_render_target.hpp @@ -0,0 +1,49 @@ +#pragma once +#include "pre.hpp" + +#include +#include + +#include "base.hpp" + +struct IDXGISwapChain1; +struct ID2D1Bitmap1; + +namespace cru::graph { +class GraphManager; + +// Represents a window render target. +class WindowRenderTarget : public Object { + public: + WindowRenderTarget(GraphManager* graph_manager, HWND hwnd); + WindowRenderTarget(const WindowRenderTarget& other) = delete; + WindowRenderTarget(WindowRenderTarget&& other) = delete; + WindowRenderTarget& operator=(const WindowRenderTarget& other) = delete; + WindowRenderTarget& operator=(WindowRenderTarget&& other) = delete; + ~WindowRenderTarget() override = default; + + public: + // Get the graph manager that created the render target. + GraphManager* GetGraphManager() const { return graph_manager_; } + + // Get the target bitmap which can be set as the ID2D1DeviceContext's target. + ID2D1Bitmap1* GetTargetBitmap() const { return target_bitmap_.get(); } + + // Resize the underlying buffer. + void ResizeBuffer(int width, int height); + + // Set this render target as the d2d device context's target. + void SetAsTarget(); + + // Present the data of the underlying buffer to the window. + void Present(); + + private: + void CreateTargetBitmap(); + + private: + GraphManager* graph_manager_; + std::shared_ptr dxgi_swap_chain_; + std::shared_ptr target_bitmap_; +}; +} // namespace cru::graph diff --git a/src/ui/controls/button.cpp b/src/ui/controls/button.cpp index e69de29b..5d4e15cc 100644 --- a/src/ui/controls/button.cpp +++ b/src/ui/controls/button.cpp @@ -0,0 +1,13 @@ +#include "button.hpp" + +#include "ui/ui_manager.hpp" +#include "ui/render/border_render_object.hpp" + +namespace cru::ui::controls { +Button::Button() { + const auto predefined_resource = + UiManager::GetInstance()->GetPredefineResources(); + render_object_ = new render::BorderRenderObject(); } + +void Button::OnChildChanged(Control* old_child, Control* new_child) {} +} // namespace cru::ui::controls diff --git a/src/ui/controls/button.hpp b/src/ui/controls/button.hpp index 010c3f5b..27e7fc7b 100644 --- a/src/ui/controls/button.hpp +++ b/src/ui/controls/button.hpp @@ -1,8 +1,35 @@ #pragma once #include "pre.hpp" -#include "ui/control.hpp" +#include "ui/content_control.hpp" + +namespace cru::ui::render { +class BorderRenderObject; +} namespace cru::ui::controls { +class Button : public ContentControl { + public: + static constexpr auto control_type = L"Button"; -} + static Button* Create(); + + protected: + Button(); + + public: + Button(const Button& other) = delete; + Button(Button&& other) = delete; + Button& operator=(const Button& other) = delete; + Button& operator=(Button&& other) = delete; + ~Button(); + + render::RenderObject* GetRenderObject() const override; + + protected: + void OnChildChanged(Control* old_child, Control* new_child) override; + + private: + render::BorderRenderObject* render_object_; +}; +} // namespace cru::ui::controls diff --git a/src/ui/controls/text_block.cpp b/src/ui/controls/text_block.cpp index 85116910..c891b832 100644 --- a/src/ui/controls/text_block.cpp +++ b/src/ui/controls/text_block.cpp @@ -1,7 +1,5 @@ #include "text_block.hpp" -#include - #include "ui/render/text_render_object.hpp" #include "ui/ui_manager.hpp" diff --git a/src/ui/render/border_render_object.cpp b/src/ui/render/border_render_object.cpp index 033f59d8..e1550665 100644 --- a/src/ui/render/border_render_object.cpp +++ b/src/ui/render/border_render_object.cpp @@ -1,16 +1,45 @@ #include "border_render_object.hpp" +#include +#include #include #include "cru_debug.hpp" #include "exception.hpp" -#include "graph/graph.hpp" +#include "graph/graph_manager.hpp" +#include "graph/graph_util.hpp" +#include "util/com_util.hpp" namespace cru::ui::render { -BorderRenderObject::BorderRenderObject(Microsoft::WRL::ComPtr brush) - : border_brush_(std::move(brush)) {} +BorderRenderObject::BorderRenderObject(ID2D1Brush* brush) { + assert(brush); + brush->AddRef(); + this->border_brush_ = brush; + try { + RecreateGeometry(); + } catch (...) { + brush->Release(); + throw; + } +} + +BorderRenderObject::~BorderRenderObject() { + util::SafeRelease(border_brush_); + util::SafeRelease(geometry_); + util::SafeRelease(border_outer_geometry_); +} + +void BorderRenderObject::SetBrush(ID2D1Brush* new_brush) { + assert(new_brush); + util::SafeRelease(border_brush_); + new_brush->AddRef(); + border_brush_ = new_brush; +} void BorderRenderObject::RecreateGeometry() { + util::SafeRelease(geometry_); + util::SafeRelease(border_outer_geometry_); + const auto d2d_factory = graph::GraphManager::GetInstance()->GetD2D1Factory(); Microsoft::WRL::ComPtr geometry; @@ -19,8 +48,8 @@ void BorderRenderObject::RecreateGeometry() { Microsoft::WRL::ComPtr border_outer_geometry; ThrowIfFailed(d2d_factory->CreatePathGeometry(&border_outer_geometry)); - ID2D1GeometrySink* sink; - auto f = [sink](const Rect& rect, const CornerRadius& corner) { + Microsoft::WRL::ComPtr sink; + auto f = [&sink](const Rect& rect, const CornerRadius& corner) { sink->BeginFigure(D2D1::Point2F(rect.left + corner.left_top.x, rect.top), D2D1_FIGURE_BEGIN_FILLED); sink->AddLine( @@ -53,21 +82,21 @@ void BorderRenderObject::RecreateGeometry() { ThrowIfFailed(border_outer_geometry->Open(&sink)); f(outer_rect, corner_radius_); ThrowIfFailed(sink->Close()); - sink->Release(); + sink = nullptr; const Rect inner_rect = outer_rect.Shrink(border_thickness_); ThrowIfFailed(geometry->Open(&sink)); f(outer_rect, corner_radius_); f(inner_rect, corner_radius_); ThrowIfFailed(sink->Close()); - sink->Release(); + sink = nullptr; - geometry_ = std::move(geometry); - border_outer_geometry_ = std::move(border_outer_geometry); + geometry_ = geometry.Detach(); + border_outer_geometry_ = border_outer_geometry.Detach(); } void BorderRenderObject::Draw(ID2D1RenderTarget* render_target) { - render_target->FillGeometry(geometry_.Get(), border_brush_.Get()); + render_target->FillGeometry(geometry_, border_brush_); if (const auto child = GetChild()) { auto offset = child->GetOffset(); graph::WithTransform(render_target, @@ -167,7 +196,8 @@ void BorderRenderObject::OnLayoutCore(const Rect& rect) { } if (coerced_content_available_size.height < 0) { debug::DebugMessage( - L"Layout: vertical length of padding, border and margin is bigger than " + L"Layout: vertical length of padding, border and margin is bigger " + L"than " L"available length."); coerced_content_available_size.height = 0; } diff --git a/src/ui/render/border_render_object.hpp b/src/ui/render/border_render_object.hpp index d6effc21..eccb1219 100644 --- a/src/ui/render/border_render_object.hpp +++ b/src/ui/render/border_render_object.hpp @@ -28,20 +28,18 @@ struct CornerRadius { class BorderRenderObject : public RenderObject { public: - explicit BorderRenderObject(Microsoft::WRL::ComPtr brush); + explicit BorderRenderObject(ID2D1Brush* brush); BorderRenderObject(const BorderRenderObject& other) = delete; BorderRenderObject(BorderRenderObject&& other) = delete; BorderRenderObject& operator=(const BorderRenderObject& other) = delete; BorderRenderObject& operator=(BorderRenderObject&& other) = delete; - ~BorderRenderObject() override = default; + ~BorderRenderObject() override; bool IsEnabled() const { return is_enabled_; } void SetEnabled(bool enabled) { is_enabled_ = enabled; } - Microsoft::WRL::ComPtr GetBrush() const { return border_brush_; } - void SetBrush(const Microsoft::WRL::ComPtr new_brush) { - border_brush_ = std::move(new_brush); - } + ID2D1Brush* GetBrush() const { return border_brush_; } + void SetBrush(ID2D1Brush* new_brush); Thickness GetBorderWidth() const { return border_thickness_; } void SetBorderWidth(const Thickness& thickness) { border_thickness_ = thickness; } @@ -51,11 +49,11 @@ class BorderRenderObject : public RenderObject { corner_radius_ = new_corner_radius; } - void RecreateGeometry(); // TODO + void RecreateGeometry(); - void Draw(ID2D1RenderTarget* render_target) override; // TODO + void Draw(ID2D1RenderTarget* render_target) override; - RenderObject* HitTest(const Point& point) override; // TODO + RenderObject* HitTest(const Point& point) override; protected: void OnAddChild(RenderObject* new_child, int position) override; @@ -73,11 +71,11 @@ class BorderRenderObject : public RenderObject { private: bool is_enabled_ = false; - Microsoft::WRL::ComPtr border_brush_; + ID2D1Brush* border_brush_ = nullptr; Thickness border_thickness_ = Thickness::Zero(); CornerRadius corner_radius_{}; - Microsoft::WRL::ComPtr geometry_{nullptr}; - Microsoft::WRL::ComPtr border_outer_geometry_{nullptr}; + ID2D1Geometry* geometry_ = nullptr; + ID2D1Geometry* border_outer_geometry_ = nullptr; }; } // namespace cru::ui::render diff --git a/src/ui/render/flex_layout_render_object.cpp b/src/ui/render/flex_layout_render_object.cpp index 4708d187..e4d327f1 100644 --- a/src/ui/render/flex_layout_render_object.cpp +++ b/src/ui/render/flex_layout_render_object.cpp @@ -4,7 +4,7 @@ #include #include "cru_debug.hpp" -#include "graph/graph.hpp" +#include "graph/graph_util.hpp" namespace cru::ui::render { FlexChildLayoutData* FlexLayoutRenderObject::GetChildLayoutData(int position) { diff --git a/src/ui/render/render_object.cpp b/src/ui/render/render_object.cpp index f56baa8f..232eda41 100644 --- a/src/ui/render/render_object.cpp +++ b/src/ui/render/render_object.cpp @@ -1,8 +1,5 @@ #include "render_object.hpp" -#include -#include - #include "cru_debug.hpp" namespace cru::ui::render { diff --git a/src/ui/render/render_object.hpp b/src/ui/render/render_object.hpp index 51b0c3ae..abdda605 100644 --- a/src/ui/render/render_object.hpp +++ b/src/ui/render/render_object.hpp @@ -1,7 +1,6 @@ #pragma once #include "pre.hpp" -#include #include #include "base.hpp" @@ -9,7 +8,6 @@ // forward declarations struct ID2D1RenderTarget; - namespace cru::ui { class Control; } diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp index e8967d48..b66fffa3 100644 --- a/src/ui/render/text_render_object.cpp +++ b/src/ui/render/text_render_object.cpp @@ -5,19 +5,61 @@ #include #include "exception.hpp" -#include "graph/graph.hpp" +#include "graph/graph_manager.hpp" +#include "graph/graph_util.hpp" +#include "util/com_util.hpp" namespace cru::ui::render { -TextRenderObject::TextRenderObject( - Microsoft::WRL::ComPtr brush, - Microsoft::WRL::ComPtr format, - Microsoft::WRL::ComPtr selection_brush) - : brush_(std::move(brush)), - text_format_(std::move(format)), - selection_brush_(std::move(selection_brush)) { +TextRenderObject::TextRenderObject(ID2D1Brush* brush, IDWriteTextFormat* format, + ID2D1Brush* selection_brush) { + assert(brush); + assert(format); + assert(selection_brush); + brush->AddRef(); + format->AddRef(); + selection_brush->AddRef(); + this->brush_ = brush; + this->text_format_ = format; + this->selection_brush_ = selection_brush; + try { + RecreateTextLayout(); + } catch (...) { + brush->Release(); + format->Release(); + selection_brush->Release(); + throw; + } +} + +TextRenderObject::~TextRenderObject() { + util::SafeRelease(brush_); + util::SafeRelease(text_format_); + util::SafeRelease(text_layout_); + util::SafeRelease(selection_brush_); +} + +void TextRenderObject::SetBrush(ID2D1Brush* new_brush) { + assert(new_brush); + util::SafeRelease(brush_); + new_brush->AddRef(); + brush_ = new_brush; +} + +void TextRenderObject::SetTextFormat(IDWriteTextFormat* new_text_format) { + assert(new_text_format); + util::SafeRelease(text_format_); + new_text_format->AddRef(); + text_format_ = new_text_format; RecreateTextLayout(); } +void TextRenderObject::SetSelectionBrush(ID2D1Brush* new_brush) { + assert(new_brush); + util::SafeRelease(selection_brush_); + new_brush->AddRef(); + selection_brush_ = new_brush; +} + namespace { void DrawSelectionRect(ID2D1RenderTarget* render_target, IDWriteTextLayout* layout, ID2D1Brush* brush, @@ -54,9 +96,8 @@ void TextRenderObject::Draw(ID2D1RenderTarget* 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()); + DrawSelectionRect(rt, text_layout_, selection_brush_, selection_range_); + rt->DrawTextLayout(D2D1::Point2F(), text_layout_, brush_); }); } @@ -99,7 +140,7 @@ void TextRenderObject::OnLayoutContent(const Rect& content_rect) {} void TextRenderObject::RecreateTextLayout() { assert(text_format_ != nullptr); - text_layout_ = nullptr; // release last one + util::SafeRelease(text_layout_); const auto dwrite_factory = graph::GraphManager::GetInstance()->GetDWriteFactory(); @@ -107,7 +148,7 @@ void TextRenderObject::RecreateTextLayout() { const auto&& size = GetSize(); ThrowIfFailed(dwrite_factory->CreateTextLayout( - text_.c_str(), static_cast(text_.size()), text_format_.Get(), + text_.c_str(), static_cast(text_.size()), text_format_, 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 index ac874b75..30d78736 100644 --- a/src/ui/render/text_render_object.hpp +++ b/src/ui/render/text_render_object.hpp @@ -1,8 +1,6 @@ #pragma once #include "pre.hpp" -#include // for ComPtr - #include "render_object.hpp" // forward declarations @@ -13,14 +11,13 @@ struct IDWriteTextLayout; namespace cru::ui::render { class TextRenderObject : public RenderObject { public: - TextRenderObject(Microsoft::WRL::ComPtr brush, - Microsoft::WRL::ComPtr format, - Microsoft::WRL::ComPtr selection_brush); + TextRenderObject(ID2D1Brush* brush, IDWriteTextFormat* format, + 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; + ~TextRenderObject() override; String GetText() const { return text_; } void SetText(String new_text) { @@ -28,19 +25,11 @@ class TextRenderObject : public RenderObject { RecreateTextLayout(); } - Microsoft::WRL::ComPtr GetBrush() const { return brush_; } - void SetBrush(Microsoft::WRL::ComPtr new_brush) { - brush_ = std::move(new_brush); - } + ID2D1Brush* GetBrush() const { return brush_; } + void SetBrush(ID2D1Brush* new_brush); - Microsoft::WRL::ComPtr GetTextFormat() const { - return text_format_; - } - void SetTextFormat( - Microsoft::WRL::ComPtr new_text_format) { - text_format_ = std::move(new_text_format); - RecreateTextLayout(); - } + IDWriteTextFormat* GetTextFormat() const { return text_format_; } + void SetTextFormat(IDWriteTextFormat* new_text_format); std::optional GetSelectionRange() const { return selection_range_; @@ -49,12 +38,10 @@ class TextRenderObject : public RenderObject { selection_range_ = std::move(new_range); } - Microsoft::WRL::ComPtr GetSelectionBrush() const { + ID2D1Brush* GetSelectionBrush() const { return selection_brush_; } - void SetSelectionBrush(Microsoft::WRL::ComPtr new_brush) { - selection_brush_ = std::move(new_brush); - } + void SetSelectionBrush(ID2D1Brush* new_brush); void Draw(ID2D1RenderTarget* render_target) override; @@ -72,11 +59,11 @@ class TextRenderObject : public RenderObject { private: String text_; - Microsoft::WRL::ComPtr brush_; - Microsoft::WRL::ComPtr text_format_; - Microsoft::WRL::ComPtr text_layout_; + ID2D1Brush* brush_; + IDWriteTextFormat* text_format_; + IDWriteTextLayout* text_layout_; std::optional selection_range_ = std::nullopt; - Microsoft::WRL::ComPtr selection_brush_; + ID2D1Brush* selection_brush_; }; } // namespace cru::ui::render diff --git a/src/ui/render/window_render_object.cpp b/src/ui/render/window_render_object.cpp index f198c2fa..44c3c426 100644 --- a/src/ui/render/window_render_object.cpp +++ b/src/ui/render/window_render_object.cpp @@ -1,6 +1,6 @@ #include "window_render_object.hpp" -#include "graph/graph.hpp" +#include "graph/graph_util.hpp" #include "ui/window.hpp" namespace cru::ui::render { diff --git a/src/ui/ui_manager.cpp b/src/ui/ui_manager.cpp index 26b1fe62..add77516 100644 --- a/src/ui/ui_manager.cpp +++ b/src/ui/ui_manager.cpp @@ -1,10 +1,14 @@ #include "ui_manager.hpp" #include +#include +#include #include "application.hpp" #include "exception.hpp" -#include "graph/graph.hpp" +#include "graph/graph_manager.hpp" +#include "graph/graph_util.hpp" +#include "util/com_util.hpp" namespace cru::ui { namespace { @@ -17,19 +21,10 @@ void GetSystemCaretInfo(CaretInfo* caret_info) { caret_info->half_caret_width = caret_width / 2.0f; } -Microsoft::WRL::ComPtr CreateSolidBrush( - graph::GraphManager* graph_manager, const D2D1_COLOR_F& color) { - const auto device_context = graph_manager->GetD2D1DeviceContext(); - Microsoft::WRL::ComPtr solid_color_brush; - device_context->CreateSolidColorBrush(color, &solid_color_brush); - return solid_color_brush; -} - -Microsoft::WRL::ComPtr CreateDefaultTextFormat( - graph::GraphManager* graph_manager) { - const auto dwrite_factory = graph_manager->GetDWriteFactory(); - - Microsoft::WRL::ComPtr text_format; +IDWriteTextFormat* CreateDefaultTextFormat() { + const auto dwrite_factory = + graph::GraphManager::GetInstance()->GetDWriteFactory(); + IDWriteTextFormat* text_format; ThrowIfFailed(dwrite_factory->CreateTextFormat( L"等线", nullptr, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, @@ -43,26 +38,32 @@ Microsoft::WRL::ComPtr CreateDefaultTextFormat( } } // namespace -PredefineResources::PredefineResources(graph::GraphManager* graph_manager) - : text_block_selection_brush{CreateSolidBrush( - graph_manager, D2D1::ColorF(D2D1::ColorF::LightSkyBlue))}, - text_block_text_brush{ - CreateSolidBrush(graph_manager, D2D1::ColorF(D2D1::ColorF::Black))}, - text_block_text_format{CreateDefaultTextFormat(graph_manager)}, - debug_layout_out_border_brush{ - CreateSolidBrush(graph_manager, D2D1::ColorF(D2D1::ColorF::Crimson))}, - debug_layout_margin_brush{CreateSolidBrush( - graph_manager, D2D1::ColorF(D2D1::ColorF::LightCoral, 0.25f))}, - debug_layout_padding_brush{CreateSolidBrush( - graph_manager, D2D1::ColorF(D2D1::ColorF::SkyBlue, 0.25f))} {} +PredefineResources::PredefineResources() { + try { + text_block_selection_brush = + graph::CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::LightSkyBlue)); + text_block_text_brush = + graph::CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black)); + text_block_text_format = CreateDefaultTextFormat(); + } catch (...) { + util::SafeRelease(text_block_selection_brush); + util::SafeRelease(text_block_text_brush); + util::SafeRelease(text_block_text_format); + } +} + +PredefineResources::~PredefineResources() { + util::SafeRelease(text_block_selection_brush); + util::SafeRelease(text_block_text_brush); + util::SafeRelease(text_block_text_format); +} UiManager* UiManager::GetInstance() { return Application::GetInstance()->ResolveSingleton( [](auto) { return new UiManager{}; }); } -UiManager::UiManager() - : predefine_resources_(graph::GraphManager::GetInstance()) { +UiManager::UiManager() : predefine_resources_() { GetSystemCaretInfo(&caret_info_); } } // namespace cru::ui diff --git a/src/ui/ui_manager.hpp b/src/ui/ui_manager.hpp index c2331dd4..b736381d 100644 --- a/src/ui/ui_manager.hpp +++ b/src/ui/ui_manager.hpp @@ -18,22 +18,17 @@ struct CaretInfo { class PredefineResources : public Object { public: - explicit PredefineResources(graph::GraphManager* graph_manager); + PredefineResources(); PredefineResources(const PredefineResources& other) = delete; PredefineResources(PredefineResources&& other) = delete; PredefineResources& operator=(const PredefineResources& other) = delete; PredefineResources& operator=(PredefineResources&& other) = delete; - ~PredefineResources() override = default; + ~PredefineResources() override; // region TextBlock - Microsoft::WRL::ComPtr text_block_selection_brush; - Microsoft::WRL::ComPtr text_block_text_brush; - Microsoft::WRL::ComPtr text_block_text_format; - - // region debug - Microsoft::WRL::ComPtr debug_layout_out_border_brush; - Microsoft::WRL::ComPtr debug_layout_margin_brush; - Microsoft::WRL::ComPtr debug_layout_padding_brush; + ID2D1Brush* text_block_selection_brush = nullptr; + ID2D1Brush* text_block_text_brush = nullptr; + IDWriteTextFormat* text_block_text_format = nullptr; }; class UiManager : public Object { diff --git a/src/ui/window.cpp b/src/ui/window.cpp index 7b00ca05..90add552 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -1,10 +1,13 @@ #include "window.hpp" #include +#include #include "application.hpp" #include "exception.hpp" -#include "graph/graph.hpp" +#include "graph/graph_manager.hpp" +#include "graph/graph_util.hpp" +#include "graph/window_render_target.hpp" #include "render/window_render_object.hpp" namespace cru::ui { @@ -207,10 +210,9 @@ void Window::BeforeCreateHwnd() { window_ = this; } void Window::AfterCreateHwnd(WindowManager* window_manager) { window_manager->RegisterWindow(hwnd_, this); - render_target_ = - graph::GraphManager::GetInstance()->CreateWindowRenderTarget(hwnd_); + render_target_.reset(new graph::WindowRenderTarget(graph::GraphManager::GetInstance(), hwnd_)); - render_object_ = new render::WindowRenderObject(this); + render_object_.reset(new render::WindowRenderObject(this)); } Window::~Window() { @@ -220,12 +222,11 @@ Window::~Window() { } TraverseDescendants( [this](Control* control) { control->OnDetachToWindow(this); }); - delete render_object_; } StringView Window::GetControlType() const { return control_type; } -render::RenderObject* Window::GetRenderObject() const { return render_object_; } +render::RenderObject* Window::GetRenderObject() const { return render_object_.get(); } void Window::SetDeleteThisOnDestroy(bool value) { delete_this_on_destroy_ = value; @@ -556,17 +557,12 @@ void Window::OnDestroyInternal() { void Window::OnPaintInternal() { render_target_->SetAsTarget(); - auto device_context = render_target_->GetD2DDeviceContext(); - + auto device_context = render_target_->GetGraphManager()->GetD2D1DeviceContext(); device_context->BeginDraw(); - // Clear the background. device_context->Clear(D2D1::ColorF(D2D1::ColorF::White)); - - render_object_->Draw(device_context.Get()); - + render_object_->Draw(device_context); ThrowIfFailed(device_context->EndDraw(), "Failed to draw window."); - render_target_->Present(); ValidateRect(hwnd_, nullptr); diff --git a/src/ui/window.hpp b/src/ui/window.hpp index 1c48bf43..dd7631d0 100644 --- a/src/ui/window.hpp +++ b/src/ui/window.hpp @@ -221,8 +221,7 @@ class Window final : public ContentControl { HWND hwnd_ = nullptr; Window* parent_window_ = nullptr; std::shared_ptr render_target_{}; - - render::WindowRenderObject* render_object_; + std::shared_ptr render_object_{}; Control* mouse_hover_control_ = nullptr; diff --git a/src/util/com_util.hpp b/src/util/com_util.hpp new file mode 100644 index 00000000..bbaf1c27 --- /dev/null +++ b/src/util/com_util.hpp @@ -0,0 +1,22 @@ +#pragma once +#include "pre.hpp" + +#include + +namespace cru::util { + +template +std::shared_ptr CreateComSharedPtr(TInterface* p) { + return std::shared_ptr(p, [](TInterface* ptr) { + if (ptr != nullptr) ptr->Release(); + }); +} + +template +void SafeRelease(TInterface*& p) { + if (p != nullptr) { + p->Release(); + p = nullptr; + } +} +} // namespace cru::util -- cgit v1.2.3 From b506c349b793f723df6da170ffa0f412200e26e9 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 24 Mar 2019 21:59:29 +0800 Subject: ... --- src/ui/render/border_render_object.cpp | 120 ++++++++++++++++----------------- src/ui/render/border_render_object.hpp | 4 +- src/ui/render/render_object.cpp | 11 +++ src/ui/render/render_object.hpp | 2 + src/ui/render/text_render_object.cpp | 14 ++-- src/ui/render/text_render_object.hpp | 6 +- 6 files changed, 83 insertions(+), 74 deletions(-) (limited to 'src/ui/render/text_render_object.cpp') diff --git a/src/ui/render/border_render_object.cpp b/src/ui/render/border_render_object.cpp index 49700869..72cea756 100644 --- a/src/ui/render/border_render_object.cpp +++ b/src/ui/render/border_render_object.cpp @@ -36,66 +36,6 @@ void BorderRenderObject::SetBrush(ID2D1Brush* new_brush) { border_brush_ = new_brush; } -void BorderRenderObject::RecreateGeometry() { - util::SafeRelease(geometry_); - util::SafeRelease(border_outer_geometry_); - - const auto d2d_factory = graph::GraphManager::GetInstance()->GetD2D1Factory(); - - Microsoft::WRL::ComPtr geometry; - ThrowIfFailed(d2d_factory->CreatePathGeometry(&geometry)); - - Microsoft::WRL::ComPtr border_outer_geometry; - ThrowIfFailed(d2d_factory->CreatePathGeometry(&border_outer_geometry)); - - Microsoft::WRL::ComPtr sink; - auto f = [&sink](const Rect& rect, const CornerRadius& corner) { - sink->BeginFigure(D2D1::Point2F(rect.left + corner.left_top.x, rect.top), - D2D1_FIGURE_BEGIN_FILLED); - sink->AddLine( - D2D1::Point2F(rect.GetRight() - corner.right_top.x, rect.top)); - sink->AddQuadraticBezier(D2D1::QuadraticBezierSegment( - D2D1::Point2F(rect.GetRight(), rect.top), - D2D1::Point2F(rect.GetRight(), rect.top + corner.right_top.y))); - sink->AddLine(D2D1::Point2F(rect.GetRight(), - rect.GetBottom() - corner.right_bottom.y)); - sink->AddQuadraticBezier(D2D1::QuadraticBezierSegment( - D2D1::Point2F(rect.GetRight(), rect.GetBottom()), - D2D1::Point2F(rect.GetRight() - corner.right_bottom.x, - rect.GetBottom()))); - sink->AddLine( - D2D1::Point2F(rect.left + corner.left_bottom.x, rect.GetBottom())); - sink->AddQuadraticBezier(D2D1::QuadraticBezierSegment( - D2D1::Point2F(rect.left, rect.GetBottom()), - D2D1::Point2F(rect.left, rect.GetBottom() - corner.left_bottom.y))); - sink->AddLine(D2D1::Point2F(rect.left, rect.top + corner.left_top.y)); - sink->AddQuadraticBezier(D2D1::QuadraticBezierSegment( - D2D1::Point2F(rect.left, rect.top), - D2D1::Point2F(rect.left + corner.left_top.x, rect.top))); - sink->EndFigure(D2D1_FIGURE_END_CLOSED); - }; - - const auto size = GetSize(); - const auto margin = GetMargin(); - const Rect outer_rect{margin.left, margin.top, - size.width - margin.GetHorizontalTotal(), - size.height - margin.GetVerticalTotal()}; - ThrowIfFailed(border_outer_geometry->Open(&sink)); - f(outer_rect, corner_radius_); - ThrowIfFailed(sink->Close()); - sink = nullptr; - - const Rect inner_rect = outer_rect.Shrink(border_thickness_); - ThrowIfFailed(geometry->Open(&sink)); - f(outer_rect, corner_radius_); - f(inner_rect, corner_radius_); - ThrowIfFailed(sink->Close()); - sink = nullptr; - - geometry_ = geometry.Detach(); - border_outer_geometry_ = border_outer_geometry.Detach(); -} - void BorderRenderObject::Draw(ID2D1RenderTarget* render_target) { render_target->FillGeometry(geometry_, border_brush_); if (const auto child = GetChild()) { @@ -231,4 +171,64 @@ void BorderRenderObject::OnLayoutContent(const Rect& content_rect) { child->Layout(content_rect); } } + +void BorderRenderObject::RecreateGeometry() { + util::SafeRelease(geometry_); + util::SafeRelease(border_outer_geometry_); + + const auto d2d_factory = graph::GraphManager::GetInstance()->GetD2D1Factory(); + + Microsoft::WRL::ComPtr geometry; + ThrowIfFailed(d2d_factory->CreatePathGeometry(&geometry)); + + Microsoft::WRL::ComPtr border_outer_geometry; + ThrowIfFailed(d2d_factory->CreatePathGeometry(&border_outer_geometry)); + + Microsoft::WRL::ComPtr sink; + auto f = [&sink](const Rect& rect, const CornerRadius& corner) { + sink->BeginFigure(D2D1::Point2F(rect.left + corner.left_top.x, rect.top), + D2D1_FIGURE_BEGIN_FILLED); + sink->AddLine( + D2D1::Point2F(rect.GetRight() - corner.right_top.x, rect.top)); + sink->AddQuadraticBezier(D2D1::QuadraticBezierSegment( + D2D1::Point2F(rect.GetRight(), rect.top), + D2D1::Point2F(rect.GetRight(), rect.top + corner.right_top.y))); + sink->AddLine(D2D1::Point2F(rect.GetRight(), + rect.GetBottom() - corner.right_bottom.y)); + sink->AddQuadraticBezier(D2D1::QuadraticBezierSegment( + D2D1::Point2F(rect.GetRight(), rect.GetBottom()), + D2D1::Point2F(rect.GetRight() - corner.right_bottom.x, + rect.GetBottom()))); + sink->AddLine( + D2D1::Point2F(rect.left + corner.left_bottom.x, rect.GetBottom())); + sink->AddQuadraticBezier(D2D1::QuadraticBezierSegment( + D2D1::Point2F(rect.left, rect.GetBottom()), + D2D1::Point2F(rect.left, rect.GetBottom() - corner.left_bottom.y))); + sink->AddLine(D2D1::Point2F(rect.left, rect.top + corner.left_top.y)); + sink->AddQuadraticBezier(D2D1::QuadraticBezierSegment( + D2D1::Point2F(rect.left, rect.top), + D2D1::Point2F(rect.left + corner.left_top.x, rect.top))); + sink->EndFigure(D2D1_FIGURE_END_CLOSED); + }; + + const auto size = GetSize(); + const auto margin = GetMargin(); + const Rect outer_rect{margin.left, margin.top, + size.width - margin.GetHorizontalTotal(), + size.height - margin.GetVerticalTotal()}; + ThrowIfFailed(border_outer_geometry->Open(&sink)); + f(outer_rect, corner_radius_); + ThrowIfFailed(sink->Close()); + sink = nullptr; + + const Rect inner_rect = outer_rect.Shrink(border_thickness_); + ThrowIfFailed(geometry->Open(&sink)); + f(outer_rect, corner_radius_); + f(inner_rect, corner_radius_); + ThrowIfFailed(sink->Close()); + sink = nullptr; + + geometry_ = geometry.Detach(); + border_outer_geometry_ = border_outer_geometry.Detach(); +} } // namespace cru::ui::render diff --git a/src/ui/render/border_render_object.hpp b/src/ui/render/border_render_object.hpp index 6f9a8c11..80db648a 100644 --- a/src/ui/render/border_render_object.hpp +++ b/src/ui/render/border_render_object.hpp @@ -56,7 +56,7 @@ class BorderRenderObject : public RenderObject { corner_radius_ = new_corner_radius; } - void RecreateGeometry(); + void Refresh() { RecreateGeometry(); } void Draw(ID2D1RenderTarget* render_target) override; @@ -77,6 +77,8 @@ class BorderRenderObject : public RenderObject { return GetChildren().empty() ? nullptr : GetChildren()[0]; } + void RecreateGeometry(); + private: bool is_enabled_ = false; diff --git a/src/ui/render/render_object.cpp b/src/ui/render/render_object.cpp index 232eda41..5c6af580 100644 --- a/src/ui/render/render_object.cpp +++ b/src/ui/render/render_object.cpp @@ -1,5 +1,7 @@ #include "render_object.hpp" +#include + #include "cru_debug.hpp" namespace cru::ui::render { @@ -96,6 +98,15 @@ void RenderObject::OnLayoutCore(const Rect& rect) { coerced_content_available_size.height}); } +Rect RenderObject::GetContentRect() const { + Rect rect{Point::Zero(), GetSize()}; + rect = rect.Shrink(GetMargin()); + rect = rect.Shrink(GetPadding()); + rect.width = std::max(rect.width, 0.0f); + rect.height = std::max(rect.height, 0.0f); + return rect; +} + void RenderObject::SetParent(RenderObject* new_parent) { const auto old_parent = parent_; parent_ = new_parent; diff --git a/src/ui/render/render_object.hpp b/src/ui/render/render_object.hpp index abdda605..824b88e6 100644 --- a/src/ui/render/render_object.hpp +++ b/src/ui/render/render_object.hpp @@ -75,6 +75,8 @@ class RenderObject : public Object { virtual Size OnMeasureContent(const Size& available_size) = 0; virtual void OnLayoutContent(const Rect& content_rect) = 0; + Rect GetContentRect() const; + private: void SetParent(RenderObject* new_parent); diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp index b66fffa3..69563ad7 100644 --- a/src/ui/render/text_render_object.cpp +++ b/src/ui/render/text_render_object.cpp @@ -114,15 +114,9 @@ RenderObject* TextRenderObject::HitTest(const Point& point) { void TextRenderObject::OnSizeChanged(const Size& old_size, const Size& new_size) { - const auto margin = GetMargin(); - const auto padding = GetPadding(); - ThrowIfFailed(text_layout_->SetMaxWidth( - std::max(new_size.width - margin.GetHorizontalTotal() - - padding.GetHorizontalTotal(), - 0.0f))); - ThrowIfFailed(text_layout_->SetMaxHeight(std::max( - new_size.height - margin.GetVerticalTotal() - padding.GetVerticalTotal(), - 0.0f))); + const auto&& size = GetContentRect().GetSize(); + ThrowIfFailed(text_layout_->SetMaxWidth(size.width)); + ThrowIfFailed(text_layout_->SetMaxHeight(size.height)); } Size TextRenderObject::OnMeasureContent(const Size& available_size) { @@ -145,7 +139,7 @@ void TextRenderObject::RecreateTextLayout() { const auto dwrite_factory = graph::GraphManager::GetInstance()->GetDWriteFactory(); - const auto&& size = GetSize(); + const auto&& size = GetContentRect().GetSize(); ThrowIfFailed(dwrite_factory->CreateTextLayout( text_.c_str(), static_cast(text_.size()), text_format_, diff --git a/src/ui/render/text_render_object.hpp b/src/ui/render/text_render_object.hpp index db102b04..7827f994 100644 --- a/src/ui/render/text_render_object.hpp +++ b/src/ui/render/text_render_object.hpp @@ -38,11 +38,11 @@ class TextRenderObject : public RenderObject { selection_range_ = std::move(new_range); } - ID2D1Brush* GetSelectionBrush() const { - return selection_brush_; - } + ID2D1Brush* GetSelectionBrush() const { return selection_brush_; } void SetSelectionBrush(ID2D1Brush* new_brush); + void Refresh() { RecreateTextLayout(); } + void Draw(ID2D1RenderTarget* render_target) override; RenderObject* HitTest(const Point& point) override; -- cgit v1.2.3