From 616ebd78b543876388cb3d64f108abea041d4983 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 20 Mar 2019 19:26:55 +0800 Subject: ... --- src/ui/controls/button.cpp | 34 ---------------------------------- 1 file changed, 34 deletions(-) delete mode 100644 src/ui/controls/button.cpp (limited to 'src/ui/controls/button.cpp') diff --git a/src/ui/controls/button.cpp b/src/ui/controls/button.cpp deleted file mode 100644 index d4537f54..00000000 --- a/src/ui/controls/button.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "button.hpp" - -#include "graph/graph.hpp" -#include "ui/ui_manager.hpp" - -namespace cru::ui::controls -{ - Button::Button() : - normal_border_{UiManager::GetInstance()->GetPredefineResources()->button_normal_border}, - pressed_border_{UiManager::GetInstance()->GetPredefineResources()->button_press_border} - { - SetBordered(true); - GetBorderProperty() = normal_border_; - - SetCursor(cursors::hand); - } - - StringView Button::GetControlType() const - { - return control_type; - } - - void Button::OnMouseClickBegin(MouseButton button) - { - GetBorderProperty() = pressed_border_; - UpdateBorder(); - } - - void Button::OnMouseClickEnd(MouseButton button) - { - GetBorderProperty() = normal_border_; - UpdateBorder(); - } -} -- 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/controls/button.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/controls/button.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 b76e435faca204f830644047077ab08930dc8f9c Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 24 Mar 2019 19:44:31 +0800 Subject: ... --- src/graph/window_render_target.cpp | 2 -- src/main.cpp | 7 +++++-- src/ui/controls/button.cpp | 17 ++++++++++++++--- src/ui/controls/button.hpp | 10 +++++++--- src/ui/controls/flex_layout.cpp | 6 ++---- src/ui/controls/flex_layout.hpp | 6 ++++-- src/ui/controls/text_block.cpp | 8 +++----- src/ui/controls/text_block.hpp | 6 ++++-- src/ui/render/border_render_object.cpp | 10 ++++++++-- src/ui/render/border_render_object.hpp | 11 ++++++++++- src/ui/render/text_render_object.hpp | 8 ++++---- src/ui/ui_manager.cpp | 5 +++++ src/ui/ui_manager.hpp | 8 +++++--- 13 files changed, 71 insertions(+), 33 deletions(-) (limited to 'src/ui/controls/button.cpp') diff --git a/src/graph/window_render_target.cpp b/src/graph/window_render_target.cpp index d110101a..a36e0faf 100644 --- a/src/graph/window_render_target.cpp +++ b/src/graph/window_render_target.cpp @@ -93,7 +93,5 @@ void WindowRenderTarget::CreateTargetBitmap() { 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/main.cpp b/src/main.cpp index 906528c4..c35213f7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include "pre.hpp" #include "application.hpp" +#include "ui/controls/button.hpp" #include "ui/controls/flex_layout.hpp" #include "ui/controls/text_block.hpp" #include "ui/window.hpp" @@ -11,6 +12,7 @@ using cru::StringView; using cru::ui::Rect; using cru::ui::Thickness; using cru::ui::Window; +using cru::ui::controls::Button; using cru::ui::controls::FlexLayout; using cru::ui::controls::TextBlock; @@ -28,15 +30,16 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, window->SetChild(flex_layout); + const auto button = Button::Create(); const auto text_block1 = TextBlock::Create(); text_block1->SetText(L"Hello World!"); - flex_layout->AddChild(text_block1, 0); + button->SetChild(text_block1); + flex_layout->AddChild(button, 0); const auto text_block2 = TextBlock::Create(); text_block2->SetText(L"Hello World!"); flex_layout->AddChild(text_block2, 1); - window->Show(); return application.Run(); diff --git a/src/ui/controls/button.cpp b/src/ui/controls/button.cpp index 5d4e15cc..a6578251 100644 --- a/src/ui/controls/button.cpp +++ b/src/ui/controls/button.cpp @@ -1,13 +1,24 @@ #include "button.hpp" -#include "ui/ui_manager.hpp" #include "ui/render/border_render_object.hpp" +#include "ui/ui_manager.hpp" namespace cru::ui::controls { Button::Button() { const auto predefined_resource = UiManager::GetInstance()->GetPredefineResources(); - render_object_ = new render::BorderRenderObject(); } + render_object_.reset(new render::BorderRenderObject( + predefined_resource->button_normal_border_brush)); + render_object_->SetEnabled(true); + render_object_->SetBorderWidth(Thickness{3}); + render_object_->SetCornerRadius(render::CornerRadius{Point{10, 5}}); +} + +render::RenderObject* Button::GetRenderObject() const { return render_object_.get(); } -void Button::OnChildChanged(Control* old_child, Control* new_child) {} +void Button::OnChildChanged(Control* old_child, Control* new_child) { + if (old_child != nullptr) render_object_->RemoveChild(0); + if (new_child != nullptr) + render_object_->AddChild(new_child->GetRenderObject(), 0); +} } // namespace cru::ui::controls diff --git a/src/ui/controls/button.hpp b/src/ui/controls/button.hpp index 27e7fc7b..3f313dfc 100644 --- a/src/ui/controls/button.hpp +++ b/src/ui/controls/button.hpp @@ -1,6 +1,8 @@ #pragma once #include "pre.hpp" +#include + #include "ui/content_control.hpp" namespace cru::ui::render { @@ -12,7 +14,7 @@ class Button : public ContentControl { public: static constexpr auto control_type = L"Button"; - static Button* Create(); + static Button* Create() { return new Button(); } protected: Button(); @@ -22,7 +24,9 @@ class Button : public ContentControl { Button(Button&& other) = delete; Button& operator=(const Button& other) = delete; Button& operator=(Button&& other) = delete; - ~Button(); + ~Button() override = default; + + StringView GetControlType() const override final { return control_type; } render::RenderObject* GetRenderObject() const override; @@ -30,6 +34,6 @@ class Button : public ContentControl { void OnChildChanged(Control* old_child, Control* new_child) override; private: - render::BorderRenderObject* render_object_; + std::shared_ptr render_object_{}; }; } // namespace cru::ui::controls diff --git a/src/ui/controls/flex_layout.cpp b/src/ui/controls/flex_layout.cpp index 289df1f1..bdbe2d73 100644 --- a/src/ui/controls/flex_layout.cpp +++ b/src/ui/controls/flex_layout.cpp @@ -5,12 +5,10 @@ namespace cru::ui::controls { using render::FlexLayoutRenderObject; -FlexLayout::FlexLayout() { render_object_ = new FlexLayoutRenderObject(); } - -FlexLayout::~FlexLayout() { delete render_object_; } +FlexLayout::FlexLayout() { render_object_.reset(new FlexLayoutRenderObject()); } render::RenderObject* FlexLayout::GetRenderObject() const { - return render_object_; + return render_object_.get(); } void FlexLayout::OnAddChild(Control* child, int position) { diff --git a/src/ui/controls/flex_layout.hpp b/src/ui/controls/flex_layout.hpp index 2ab3e259..9ceef1f6 100644 --- a/src/ui/controls/flex_layout.hpp +++ b/src/ui/controls/flex_layout.hpp @@ -1,6 +1,8 @@ #pragma once #include "pre.hpp" +#include + #include "ui/layout_control.hpp" namespace cru::ui::render { @@ -23,7 +25,7 @@ class FlexLayout : public LayoutControl { FlexLayout(FlexLayout&& other) = delete; FlexLayout& operator=(const FlexLayout& other) = delete; FlexLayout& operator=(FlexLayout&& other) = delete; - ~FlexLayout() override; + ~FlexLayout() override = default; StringView GetControlType() const override final { return control_type; } @@ -34,6 +36,6 @@ class FlexLayout : public LayoutControl { void OnRemoveChild(Control* child, int position) override; private: - render::FlexLayoutRenderObject* render_object_; + std::shared_ptr render_object_; }; } // namespace cru::ui::controls diff --git a/src/ui/controls/text_block.cpp b/src/ui/controls/text_block.cpp index c891b832..c2f8cd8e 100644 --- a/src/ui/controls/text_block.cpp +++ b/src/ui/controls/text_block.cpp @@ -9,16 +9,14 @@ using render::TextRenderObject; TextBlock::TextBlock() { const auto predefined_resources = UiManager::GetInstance()->GetPredefineResources(); - render_object_ = + render_object_.reset( new TextRenderObject(predefined_resources->text_block_text_brush, predefined_resources->text_block_text_format, - predefined_resources->text_block_selection_brush); + predefined_resources->text_block_selection_brush)); } -TextBlock::~TextBlock() { delete render_object_; } - render::RenderObject* TextBlock::GetRenderObject() const { - return render_object_; + return render_object_.get(); } String TextBlock::GetText() const { return render_object_->GetText(); } diff --git a/src/ui/controls/text_block.hpp b/src/ui/controls/text_block.hpp index 4c443020..0d65dd67 100644 --- a/src/ui/controls/text_block.hpp +++ b/src/ui/controls/text_block.hpp @@ -1,6 +1,8 @@ #pragma once #include "pre.hpp" +#include + #include "ui/no_child_control.hpp" namespace cru::ui::render { @@ -22,7 +24,7 @@ class TextBlock : public NoChildControl { TextBlock(TextBlock&& other) = delete; TextBlock& operator=(const TextBlock& other) = delete; TextBlock& operator=(TextBlock&& other) = delete; - ~TextBlock() override; + ~TextBlock() override = default; StringView GetControlType() const override final { return control_type; } @@ -32,6 +34,6 @@ class TextBlock : public NoChildControl { void SetText(const String& text); private: - render::TextRenderObject* render_object_; + std::shared_ptr render_object_; }; } // namespace cru::ui::controls diff --git a/src/ui/render/border_render_object.cpp b/src/ui/render/border_render_object.cpp index e1550665..49700869 100644 --- a/src/ui/render/border_render_object.cpp +++ b/src/ui/render/border_render_object.cpp @@ -77,8 +77,9 @@ void BorderRenderObject::RecreateGeometry() { const auto size = GetSize(); const auto margin = GetMargin(); - const Rect outer_rect{margin.left, margin.top, size.width - margin.right, - size.height - size.height}; + 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()); @@ -137,6 +138,11 @@ void BorderRenderObject::OnAddChild(RenderObject* new_child, int position) { assert(GetChildren().size() == 1); } +void BorderRenderObject::OnSizeChanged(const Size& old_size, + const Size& new_size) { + RecreateGeometry(); +} + void BorderRenderObject::OnMeasureCore(const Size& available_size) { const auto margin = GetMargin(); const auto padding = GetPadding(); diff --git a/src/ui/render/border_render_object.hpp b/src/ui/render/border_render_object.hpp index eccb1219..6f9a8c11 100644 --- a/src/ui/render/border_render_object.hpp +++ b/src/ui/render/border_render_object.hpp @@ -13,6 +13,11 @@ namespace cru::ui::render { struct CornerRadius { constexpr CornerRadius() : left_top(), right_top(), left_bottom(), right_bottom() {} + constexpr CornerRadius(const Point& value) + : left_top(value), + right_top(value), + left_bottom(value), + right_bottom(value) {} constexpr CornerRadius(Point left_top, Point right_top, Point left_bottom, Point right_bottom) : left_top(left_top), @@ -42,7 +47,9 @@ class BorderRenderObject : public RenderObject { void SetBrush(ID2D1Brush* new_brush); Thickness GetBorderWidth() const { return border_thickness_; } - void SetBorderWidth(const Thickness& thickness) { border_thickness_ = thickness; } + void SetBorderWidth(const Thickness& thickness) { + border_thickness_ = thickness; + } CornerRadius GetCornerRadius() const { return corner_radius_; } void SetCornerRadius(const CornerRadius& new_corner_radius) { @@ -58,6 +65,8 @@ class BorderRenderObject : public RenderObject { protected: void OnAddChild(RenderObject* new_child, int position) override; + void OnSizeChanged(const Size& old_size, const Size& new_size) override; + void OnMeasureCore(const Size& available_size) override; void OnLayoutCore(const Rect& rect) override; Size OnMeasureContent(const Size& available_size) override; diff --git a/src/ui/render/text_render_object.hpp b/src/ui/render/text_render_object.hpp index 30d78736..db102b04 100644 --- a/src/ui/render/text_render_object.hpp +++ b/src/ui/render/text_render_object.hpp @@ -59,11 +59,11 @@ class TextRenderObject : public RenderObject { private: String text_; - ID2D1Brush* brush_; - IDWriteTextFormat* text_format_; - IDWriteTextLayout* text_layout_; + ID2D1Brush* brush_ = nullptr; + IDWriteTextFormat* text_format_ = nullptr; + IDWriteTextLayout* text_layout_ = nullptr; std::optional selection_range_ = std::nullopt; - ID2D1Brush* selection_brush_; + ID2D1Brush* selection_brush_ = nullptr; }; } // namespace cru::ui::render diff --git a/src/ui/ui_manager.cpp b/src/ui/ui_manager.cpp index add77516..4d14575b 100644 --- a/src/ui/ui_manager.cpp +++ b/src/ui/ui_manager.cpp @@ -40,12 +40,16 @@ IDWriteTextFormat* CreateDefaultTextFormat() { PredefineResources::PredefineResources() { try { + button_normal_border_brush = + graph::CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black)); + 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(button_normal_border_brush); util::SafeRelease(text_block_selection_brush); util::SafeRelease(text_block_text_brush); util::SafeRelease(text_block_text_format); @@ -53,6 +57,7 @@ PredefineResources::PredefineResources() { } PredefineResources::~PredefineResources() { + util::SafeRelease(button_normal_border_brush); util::SafeRelease(text_block_selection_brush); util::SafeRelease(text_block_text_brush); util::SafeRelease(text_block_text_format); diff --git a/src/ui/ui_manager.hpp b/src/ui/ui_manager.hpp index b736381d..107b536c 100644 --- a/src/ui/ui_manager.hpp +++ b/src/ui/ui_manager.hpp @@ -1,11 +1,10 @@ #pragma once #include "pre.hpp" -#include -#include - #include "base.hpp" +struct ID2D1Brush; +struct IDWriteTextFormat; namespace cru::graph { class GraphManager; } @@ -25,6 +24,9 @@ class PredefineResources : public Object { PredefineResources& operator=(PredefineResources&& other) = delete; ~PredefineResources() override; + // region Button + ID2D1Brush* button_normal_border_brush = nullptr; + // region TextBlock ID2D1Brush* text_block_selection_brush = nullptr; ID2D1Brush* text_block_text_brush = nullptr; -- cgit v1.2.3