diff options
author | crupest <crupest@outlook.com> | 2019-04-10 19:42:46 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-04-10 19:42:46 +0800 |
commit | 7351020a582d70a1495249fba87d342c8a1fb634 (patch) | |
tree | e80f225041dc3816b3dce21c7e15aadbb211602e | |
parent | a94a806f69586e08a30fff0cdb3e52b0ce7acfa5 (diff) | |
download | cru-7351020a582d70a1495249fba87d342c8a1fb634.tar.gz cru-7351020a582d70a1495249fba87d342c8a1fb634.tar.bz2 cru-7351020a582d70a1495249fba87d342c8a1fb634.zip |
Refactor.
-rw-r--r-- | art/project_structure.png | bin | 0 -> 81434 bytes | |||
-rw-r--r-- | include/cru/platform/debug.hpp | 2 | ||||
-rw-r--r-- | include/cru/platform/exception.hpp | 11 | ||||
-rw-r--r-- | include/cru/platform/graph/brush.hpp (renamed from include/cru/platform/brush.hpp) | 2 | ||||
-rw-r--r-- | include/cru/platform/graph/font.hpp (renamed from include/cru/platform/font.hpp) | 2 | ||||
-rw-r--r-- | include/cru/platform/graph/geometry.hpp (renamed from include/cru/platform/geometry.hpp) | 3 | ||||
-rw-r--r-- | include/cru/platform/graph/graph_factory.hpp (renamed from include/cru/platform/graph_factory.hpp) | 4 | ||||
-rw-r--r-- | include/cru/platform/graph/painter.hpp (renamed from include/cru/platform/painter.hpp) | 4 | ||||
-rw-r--r-- | include/cru/platform/graph/painter_util.hpp (renamed from include/cru/platform/painter_util.hpp) | 2 | ||||
-rw-r--r-- | include/cru/platform/graph/text_layout.hpp (renamed from include/cru/platform/text_layout.hpp) | 2 | ||||
-rw-r--r-- | include/cru/platform/native/basic_types.hpp (renamed from include/cru/platform/basic_types.hpp) | 2 | ||||
-rw-r--r-- | include/cru/platform/native/native_window.hpp (renamed from include/cru/platform/native_window.hpp) | 8 | ||||
-rw-r--r-- | include/cru/platform/native/ui_applicaition.hpp (renamed from include/cru/platform/ui_applicaition.hpp) | 13 | ||||
-rw-r--r-- | include/cru/platform/string_util.hpp | 2 | ||||
-rw-r--r-- | include/cru/platform/win/win_graph_factory.hpp | 28 | ||||
-rw-r--r-- | include/cru/platform/win/win_painter.hpp | 42 | ||||
-rw-r--r-- | include/cru/ui/control.hpp | 6 | ||||
-rw-r--r-- | include/cru/ui/event/ui_event.hpp | 23 | ||||
-rw-r--r-- | include/cru/ui/render/border_render_object.hpp | 16 | ||||
-rw-r--r-- | include/cru/ui/render/flex_layout_render_object.hpp | 2 | ||||
-rw-r--r-- | include/cru/ui/render/render_object.hpp | 4 | ||||
-rw-r--r-- | include/cru/ui/render/text_render_object.hpp | 32 | ||||
-rw-r--r-- | include/cru/ui/render/window_render_object.hpp | 2 | ||||
-rw-r--r-- | include/cru/ui/ui_manager.hpp | 10 | ||||
-rw-r--r-- | include/cru/ui/window.hpp | 13 | ||||
-rw-r--r-- | include/cru/win/exception.hpp (renamed from include/cru/platform/win/exception.hpp) | 10 | ||||
-rw-r--r-- | include/cru/win/graph/d2d_util.hpp (renamed from include/cru/platform/win/d2d_util.hpp) | 16 | ||||
-rw-r--r-- | include/cru/win/graph/graph_manager.hpp (renamed from include/cru/platform/win/graph_manager.hpp) | 17 | ||||
-rw-r--r-- | include/cru/win/graph/win_brush.hpp (renamed from include/cru/platform/win/win_brush.hpp) | 18 | ||||
-rw-r--r-- | include/cru/win/graph/win_font.hpp (renamed from include/cru/platform/win/win_font.hpp) | 14 | ||||
-rw-r--r-- | include/cru/win/graph/win_geometry.hpp (renamed from include/cru/platform/win/win_geometry.hpp) | 19 | ||||
-rw-r--r-- | include/cru/win/graph/win_graph_factory.hpp | 30 | ||||
-rw-r--r-- | include/cru/win/graph/win_painter.hpp | 48 | ||||
-rw-r--r-- | include/cru/win/graph/win_text_layout.hpp (renamed from include/cru/platform/win/win_text_layout.hpp) | 14 | ||||
-rw-r--r-- | include/cru/win/native/god_window.hpp (renamed from include/cru/platform/win/god_window.hpp) | 10 | ||||
-rw-r--r-- | include/cru/win/native/win_application.hpp (renamed from include/cru/platform/win/win_application.hpp) | 26 | ||||
-rw-r--r-- | include/cru/win/native/win_native_window.hpp (renamed from include/cru/platform/win/win_native_window.hpp) | 31 | ||||
-rw-r--r-- | include/cru/win/native/window_class.hpp (renamed from include/cru/platform/win/window_class.hpp) | 9 | ||||
-rw-r--r-- | include/cru/win/native/window_native_message_event_args.hpp (renamed from include/cru/platform/win/window_native_message_event_args.hpp) | 6 | ||||
-rw-r--r-- | include/cru/win/native/window_render_target.hpp (renamed from include/cru/platform/win/window_render_target.hpp) | 15 | ||||
-rw-r--r-- | include/cru/win/win_pre_config.hpp (renamed from include/cru/platform/win/win_pre_config.hpp) | 0 | ||||
-rw-r--r-- | src/CMakeLists.txt | 22 | ||||
-rw-r--r-- | src/main.cpp | 6 | ||||
-rw-r--r-- | src/platform_win/CMakeLists.txt | 21 | ||||
-rw-r--r-- | src/platform_win/god_window_message.hpp | 6 | ||||
-rw-r--r-- | src/platform_win/win_brush.cpp | 21 | ||||
-rw-r--r-- | src/platform_win/win_graph_factory.cpp | 46 | ||||
-rw-r--r-- | src/platform_win/win_painter.cpp | 104 | ||||
-rw-r--r-- | src/ui/CMakeLists.txt | 22 | ||||
-rw-r--r-- | src/ui/control.cpp | 4 | ||||
-rw-r--r-- | src/ui/render/border_render_object.cpp | 32 | ||||
-rw-r--r-- | src/ui/render/flex_layout_render_object.cpp | 11 | ||||
-rw-r--r-- | src/ui/render/render_object.cpp | 8 | ||||
-rw-r--r-- | src/ui/render/text_render_object.cpp | 28 | ||||
-rw-r--r-- | src/ui/render/window_render_object.cpp | 8 | ||||
-rw-r--r-- | src/ui/ui_manager.cpp | 17 | ||||
-rw-r--r-- | src/ui/window.cpp | 16 | ||||
-rw-r--r-- | src/win/CMakeLists.txt | 10 | ||||
-rw-r--r-- | src/win/debug.cpp (renamed from src/platform_win/debug.cpp) | 7 | ||||
-rw-r--r-- | src/win/exception.cpp (renamed from src/platform_win/exception.cpp) | 14 | ||||
-rw-r--r-- | src/win/graph/CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/win/graph/graph_manager.cpp (renamed from src/platform_win/graph_manager.cpp) | 24 | ||||
-rw-r--r-- | src/win/graph/win_brush.cpp | 23 | ||||
-rw-r--r-- | src/win/graph/win_font.cpp (renamed from src/platform_win/win_font.cpp) | 15 | ||||
-rw-r--r-- | src/win/graph/win_geometry.cpp (renamed from src/platform_win/win_geometry.cpp) | 23 | ||||
-rw-r--r-- | src/win/graph/win_graph_factory.cpp | 46 | ||||
-rw-r--r-- | src/win/graph/win_painter.cpp | 99 | ||||
-rw-r--r-- | src/win/graph/win_text_layout.cpp (renamed from src/platform_win/win_text_layout.cpp) | 16 | ||||
-rw-r--r-- | src/win/native/CMakeLists.txt | 10 | ||||
-rw-r--r-- | src/win/native/dpi_util.hpp (renamed from src/platform_win/dpi_util.hpp) | 10 | ||||
-rw-r--r-- | src/win/native/god_window.cpp (renamed from src/platform_win/god_window.cpp) | 19 | ||||
-rw-r--r-- | src/win/native/god_window_message.hpp | 6 | ||||
-rw-r--r-- | src/win/native/timer.cpp (renamed from src/platform_win/timer.cpp) | 4 | ||||
-rw-r--r-- | src/win/native/timer.hpp (renamed from src/platform_win/timer.hpp) | 12 | ||||
-rw-r--r-- | src/win/native/win_application.cpp (renamed from src/platform_win/win_application.cpp) | 33 | ||||
-rw-r--r-- | src/win/native/win_native_window.cpp (renamed from src/platform_win/win_native_window.cpp) | 42 | ||||
-rw-r--r-- | src/win/native/window_class.cpp (renamed from src/platform_win/window_class.cpp) | 12 | ||||
-rw-r--r-- | src/win/native/window_manager.cpp (renamed from src/platform_win/window_manager.cpp) | 10 | ||||
-rw-r--r-- | src/win/native/window_manager.hpp (renamed from src/platform_win/window_manager.hpp) | 6 | ||||
-rw-r--r-- | src/win/native/window_painter.cpp | 24 | ||||
-rw-r--r-- | src/win/native/window_painter.hpp | 21 | ||||
-rw-r--r-- | src/win/native/window_render_target.cpp (renamed from src/platform_win/window_render_target.cpp) | 13 | ||||
-rw-r--r-- | src/win/string_util.cpp (renamed from src/platform_win/string_util.cpp) | 6 |
83 files changed, 755 insertions, 649 deletions
diff --git a/art/project_structure.png b/art/project_structure.png Binary files differnew file mode 100644 index 00000000..51e15d95 --- /dev/null +++ b/art/project_structure.png diff --git a/include/cru/platform/debug.hpp b/include/cru/platform/debug.hpp index 21c2b646..18472201 100644 --- a/include/cru/platform/debug.hpp +++ b/include/cru/platform/debug.hpp @@ -3,6 +3,6 @@ #include <string_view> -namespace cru::platform::debug { +namespace cru::platform { void DebugMessage(const std::wstring_view& message); } diff --git a/include/cru/platform/exception.hpp b/include/cru/platform/exception.hpp new file mode 100644 index 00000000..6dae08c5 --- /dev/null +++ b/include/cru/platform/exception.hpp @@ -0,0 +1,11 @@ +#pragma once +#include "cru/common/pre_config.hpp" + +#include <stdexcept> + +namespace cru::platform { +class PlatformException : public std::runtime_error { + public: + using runtime_error::runtime_error; // inherent constructors +}; +} // namespace cru::platform diff --git a/include/cru/platform/brush.hpp b/include/cru/platform/graph/brush.hpp index 713d302d..8ffbfc97 100644 --- a/include/cru/platform/brush.hpp +++ b/include/cru/platform/graph/brush.hpp @@ -3,7 +3,7 @@ #include "cru/common/ui_base.hpp" -namespace cru::platform { +namespace cru::platform::graph { struct Brush : public virtual Interface {}; struct SolidColorBrush : public virtual Brush { diff --git a/include/cru/platform/font.hpp b/include/cru/platform/graph/font.hpp index 750aaae1..5af369e5 100644 --- a/include/cru/platform/font.hpp +++ b/include/cru/platform/graph/font.hpp @@ -1,7 +1,7 @@ #pragma once #include "cru/common/base.hpp" -namespace cru::platform { +namespace cru::platform::graph { struct FontDescriptor : virtual Interface { }; diff --git a/include/cru/platform/geometry.hpp b/include/cru/platform/graph/geometry.hpp index aa93fe37..7a7ad8b2 100644 --- a/include/cru/platform/geometry.hpp +++ b/include/cru/platform/graph/geometry.hpp @@ -1,10 +1,9 @@ #pragma once #include "cru/common/base.hpp" -#include "basic_types.hpp" #include "cru/common/ui_base.hpp" -namespace cru::platform { +namespace cru::platform::graph { struct Geometry : virtual Interface { virtual bool FillContains(const ui::Point& point) = 0; }; diff --git a/include/cru/platform/graph_factory.hpp b/include/cru/platform/graph/graph_factory.hpp index f6122241..51cf6f15 100644 --- a/include/cru/platform/graph_factory.hpp +++ b/include/cru/platform/graph/graph_factory.hpp @@ -7,13 +7,15 @@ #include <string> #include <string_view> -namespace cru::platform { +namespace cru::platform::graph { struct SolidColorBrush; struct GeometryBuilder; struct FontDescriptor; struct TextLayout; struct GraphFactory : virtual Interface { + static GraphFactory* GetInstance(); + virtual SolidColorBrush* CreateSolidColorBrush(const ui::Color& color) = 0; virtual GeometryBuilder* CreateGeometryBuilder() = 0; virtual FontDescriptor* CreateFontDescriptor( diff --git a/include/cru/platform/painter.hpp b/include/cru/platform/graph/painter.hpp index b7b89fc9..199ebbad 100644 --- a/include/cru/platform/painter.hpp +++ b/include/cru/platform/graph/painter.hpp @@ -2,9 +2,9 @@ #include "cru/common/base.hpp" #include "cru/common/ui_base.hpp" -#include "matrix.hpp" +#include "../matrix.hpp" -namespace cru::platform { +namespace cru::platform::graph { struct Brush; struct Geometry; struct TextLayout; diff --git a/include/cru/platform/painter_util.hpp b/include/cru/platform/graph/painter_util.hpp index 5711d099..c137e8cb 100644 --- a/include/cru/platform/painter_util.hpp +++ b/include/cru/platform/graph/painter_util.hpp @@ -4,7 +4,7 @@ #include <functional> #include <type_traits> -namespace cru::platform::util { +namespace cru::platform::graph::util { template <typename Fn> inline void WithTransform(Painter* painter, const Matrix& matrix, const Fn& action) { diff --git a/include/cru/platform/text_layout.hpp b/include/cru/platform/graph/text_layout.hpp index e43adfde..eb13c115 100644 --- a/include/cru/platform/text_layout.hpp +++ b/include/cru/platform/graph/text_layout.hpp @@ -7,7 +7,7 @@ #include <string> #include <vector> -namespace cru::platform { +namespace cru::platform::graph { struct FontDescriptor; struct TextLayout : virtual Interface { diff --git a/include/cru/platform/basic_types.hpp b/include/cru/platform/native/basic_types.hpp index e6ab4b59..573a9e76 100644 --- a/include/cru/platform/basic_types.hpp +++ b/include/cru/platform/native/basic_types.hpp @@ -1,7 +1,7 @@ #pragma once #include "cru/common/pre_config.hpp" -namespace cru::platform { +namespace cru::platform::native { struct Dpi { float x; float y; diff --git a/include/cru/platform/native_window.hpp b/include/cru/platform/native/native_window.hpp index f68fd3a4..a82e7a23 100644 --- a/include/cru/platform/native_window.hpp +++ b/include/cru/platform/native/native_window.hpp @@ -5,9 +5,11 @@ #include "cru/common/event.hpp" #include "cru/common/ui_base.hpp" -namespace cru::platform { +namespace cru::platform::graph { struct Painter; +} +namespace cru::platform::native { struct NativeWindow : public virtual Interface { // Return if the window is still valid, that is, hasn't been closed or // destroyed. @@ -32,7 +34,7 @@ struct NativeWindow : public virtual Interface { // The lefttop of the rect is relative to screen lefttop. virtual void SetWindowRect(const ui::Rect& rect) = 0; - virtual Painter* BeginPaint() = 0; + virtual graph::Painter* BeginPaint() = 0; virtual Event<>* DestroyEvent() = 0; virtual Event<const ui::Size&>* ResizeEvent() = 0; @@ -45,4 +47,4 @@ struct NativeWindow : public virtual Interface { virtual Event<int>* KeyDownEvent() = 0; virtual Event<int>* KeyUpEvent() = 0; }; -} // namespace cru::platform +} // namespace cru::platform::ui diff --git a/include/cru/platform/ui_applicaition.hpp b/include/cru/platform/native/ui_applicaition.hpp index 16739f9d..8d42d813 100644 --- a/include/cru/platform/ui_applicaition.hpp +++ b/include/cru/platform/native/ui_applicaition.hpp @@ -1,15 +1,16 @@ #pragma once #include "cru/common/base.hpp" -#include "basic_types.hpp" - #include <chrono> #include <functional> #include <vector> -namespace cru::platform { -struct NativeWindow; +namespace cru::platform::graph { struct GraphFactory; +} + +namespace cru::platform::native { +struct NativeWindow; struct UiApplication : public virtual Interface { static UiApplication* GetInstance(); @@ -26,7 +27,5 @@ struct UiApplication : public virtual Interface { virtual std::vector<NativeWindow*> GetAllWindow() = 0; virtual NativeWindow* CreateWindow(NativeWindow* parent) = 0; - - virtual GraphFactory* GetGraphFactory() = 0; }; -} // namespace cru::platform +} // namespace cru::platform::ui diff --git a/include/cru/platform/string_util.hpp b/include/cru/platform/string_util.hpp index c3b9f93b..822c0c95 100644 --- a/include/cru/platform/string_util.hpp +++ b/include/cru/platform/string_util.hpp @@ -4,6 +4,6 @@ #include <string> #include <string_view> -namespace cru::platform::util { +namespace cru::platform { std::string ToUtf8String(const std::wstring_view& string); } diff --git a/include/cru/platform/win/win_graph_factory.hpp b/include/cru/platform/win/win_graph_factory.hpp deleted file mode 100644 index ca83be86..00000000 --- a/include/cru/platform/win/win_graph_factory.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include "win_pre_config.hpp" - -#include "../graph_factory.hpp" - -namespace cru::platform::win { -class GraphManager; - -class WinGraphFactory : public Object, public virtual GraphFactory { - public: - explicit WinGraphFactory(GraphManager* graph_manager); - WinGraphFactory(const WinGraphFactory& other) = delete; - WinGraphFactory(WinGraphFactory&& other) = delete; - WinGraphFactory& operator=(const WinGraphFactory& other) = delete; - WinGraphFactory& operator=(WinGraphFactory&& other) = delete; - ~WinGraphFactory() override = default; - - SolidColorBrush* CreateSolidColorBrush(const ui::Color& color) override; - GeometryBuilder* CreateGeometryBuilder() override; - FontDescriptor* CreateFontDescriptor(const std::wstring_view& font_family, - float font_size); - TextLayout* CreateTextLayout(std::shared_ptr<FontDescriptor> font, - std::wstring text); - - private: - GraphManager* graph_manager_; -}; -} // namespace cru::platform::win diff --git a/include/cru/platform/win/win_painter.hpp b/include/cru/platform/win/win_painter.hpp deleted file mode 100644 index 3c37ccb2..00000000 --- a/include/cru/platform/win/win_painter.hpp +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once -#include "win_pre_config.hpp" - -#include "../painter.hpp" - -namespace cru::platform::win { -class WinNativeWindow; - -class WinPainter : public Object, public virtual Painter { - public: - explicit WinPainter(WinNativeWindow* window); - WinPainter(const WinPainter& other) = delete; - WinPainter(WinPainter&& other) = delete; - WinPainter& operator=(const WinPainter& other) = delete; - WinPainter& operator=(WinPainter&& other) = delete; - ~WinPainter() override; - - Matrix GetTransform() override; - void SetTransform(const Matrix& matrix) override; - void Clear(const ui::Color& color) override; - void StrokeRectangle(const ui::Rect& rectangle, Brush* brush, - float width) override; - void FillRectangle(const ui::Rect& rectangle, Brush* brush) override; - void StrokeGeometry(Geometry* geometry, Brush* brush, float width) override; - void FillGeometry(Geometry* geometry, Brush* brush) override; - void DrawText(const ui::Point& offset, TextLayout* text_layout, - Brush* brush) override; - void EndDraw() override; - bool IsDisposed() override { return is_disposed; } - - void EndDrawAndDeleteThis() { - EndDraw(); - delete this; - } - - private: - WinNativeWindow* window_; - ID2D1RenderTarget* render_target_; - - bool is_disposed = false; -}; -} // namespace cru::platform::win diff --git a/include/cru/ui/control.hpp b/include/cru/ui/control.hpp index 684af2f0..652efe89 100644 --- a/include/cru/ui/control.hpp +++ b/include/cru/ui/control.hpp @@ -1,7 +1,7 @@ #pragma once #include "cru/common/base.hpp" -#include "cru/platform/basic_types.hpp" +#include "cru/platform/native/basic_types.hpp" #include "event/ui_event.hpp" #include <string_view> @@ -127,8 +127,8 @@ class Control : public Object { //*************** region: additional mouse event *************** protected: - virtual void OnMouseClickBegin(platform::MouseButton button); - virtual void OnMouseClickEnd(platform::MouseButton button); + virtual void OnMouseClickBegin(platform::native::MouseButton button); + virtual void OnMouseClickEnd(platform::native::MouseButton button); private: Window* window_ = nullptr; diff --git a/include/cru/ui/event/ui_event.hpp b/include/cru/ui/event/ui_event.hpp index ba8940cb..c84116b9 100644 --- a/include/cru/ui/event/ui_event.hpp +++ b/include/cru/ui/event/ui_event.hpp @@ -3,11 +3,11 @@ #include "cru/common/event.hpp" #include "cru/common/ui_base.hpp" -#include "cru/platform/basic_types.hpp" +#include "cru/platform/native/basic_types.hpp" #include <optional> -namespace cru::platform { +namespace cru::platform::graph { struct Painter; } @@ -19,9 +19,7 @@ namespace cru::ui::event { class UiEventArgs : public Object { public: UiEventArgs(Object* sender, Object* original_sender) - : sender_(sender), - original_sender_(original_sender), - handled_(false) {} + : sender_(sender), original_sender_(original_sender), handled_(false) {} UiEventArgs(const UiEventArgs& other) = default; UiEventArgs(UiEventArgs&& other) = default; @@ -80,7 +78,8 @@ class MouseEventArgs : public UiEventArgs { class MouseButtonEventArgs : public MouseEventArgs { public: MouseButtonEventArgs(Object* sender, Object* original_sender, - const Point& point, const platform::MouseButton button) + const Point& point, + const platform::native::MouseButton button) : MouseEventArgs(sender, original_sender, point), button_(button) {} MouseButtonEventArgs(const MouseButtonEventArgs& other) = default; MouseButtonEventArgs(MouseButtonEventArgs&& other) = default; @@ -88,10 +87,10 @@ class MouseButtonEventArgs : public MouseEventArgs { MouseButtonEventArgs& operator=(MouseButtonEventArgs&& other) = default; ~MouseButtonEventArgs() override = default; - platform::MouseButton GetMouseButton() const { return button_; } + platform::native::MouseButton GetMouseButton() const { return button_; } private: - platform::MouseButton button_; + platform::native::MouseButton button_; }; class MouseWheelEventArgs : public MouseEventArgs { @@ -114,7 +113,7 @@ class MouseWheelEventArgs : public MouseEventArgs { class PaintEventArgs : public UiEventArgs { public: PaintEventArgs(Object* sender, Object* original_sender, - platform::Painter* painter) + platform::graph::Painter* painter) : UiEventArgs(sender, original_sender), painter_(painter) {} PaintEventArgs(const PaintEventArgs& other) = default; PaintEventArgs(PaintEventArgs&& other) = default; @@ -122,10 +121,10 @@ class PaintEventArgs : public UiEventArgs { PaintEventArgs& operator=(PaintEventArgs&& other) = default; ~PaintEventArgs() = default; - platform::Painter* GetPainter() const { return painter_; } + platform::graph::Painter* GetPainter() const { return painter_; } private: - platform::Painter* painter_; + platform::graph::Painter* painter_; }; class FocusChangeEventArgs : public UiEventArgs { @@ -197,4 +196,4 @@ class CharEventArgs : public UiEventArgs { wchar_t c_; }; */ -} // namespace cru::ui::events +} // namespace cru::ui::event diff --git a/include/cru/ui/render/border_render_object.hpp b/include/cru/ui/render/border_render_object.hpp index 35b98948..44382c63 100644 --- a/include/cru/ui/render/border_render_object.hpp +++ b/include/cru/ui/render/border_render_object.hpp @@ -3,7 +3,7 @@ #include <memory> -namespace cru::platform { +namespace cru::platform::graph { struct Brush; struct Geometry; } // namespace cru::platform @@ -32,7 +32,7 @@ struct CornerRadius { class BorderRenderObject : public RenderObject { public: - explicit BorderRenderObject(std::shared_ptr<platform::Brush> brush); + explicit BorderRenderObject(std::shared_ptr<platform::graph::Brush> brush); BorderRenderObject(const BorderRenderObject& other) = delete; BorderRenderObject(BorderRenderObject&& other) = delete; BorderRenderObject& operator=(const BorderRenderObject& other) = delete; @@ -42,8 +42,8 @@ class BorderRenderObject : public RenderObject { bool IsEnabled() const { return is_enabled_; } void SetEnabled(bool enabled) { is_enabled_ = enabled; } - std::shared_ptr<platform::Brush> GetBrush() const { return border_brush_; } - void SetBrush(std::shared_ptr<platform::Brush> new_brush) { + std::shared_ptr<platform::graph::Brush> GetBrush() const { return border_brush_; } + void SetBrush(std::shared_ptr<platform::graph::Brush> new_brush) { border_brush_ = std::move(new_brush); } @@ -59,7 +59,7 @@ class BorderRenderObject : public RenderObject { void Refresh() { RecreateGeometry(); } - void Draw(platform::Painter* painter) override; + void Draw(platform::graph::Painter* painter) override; RenderObject* HitTest(const Point& point) override; @@ -83,11 +83,11 @@ class BorderRenderObject : public RenderObject { private: bool is_enabled_ = false; - std::shared_ptr<platform::Brush> border_brush_ = nullptr; + std::shared_ptr<platform::graph::Brush> border_brush_ = nullptr; Thickness border_thickness_{}; CornerRadius corner_radius_{}; - std::shared_ptr<platform::Geometry> geometry_ = nullptr; - std::shared_ptr<platform::Geometry> border_outer_geometry_ = nullptr; + std::shared_ptr<platform::graph::Geometry> geometry_ = nullptr; + std::shared_ptr<platform::graph::Geometry> border_outer_geometry_ = nullptr; }; } // namespace cru::ui::render diff --git a/include/cru/ui/render/flex_layout_render_object.hpp b/include/cru/ui/render/flex_layout_render_object.hpp index 278bf8c2..d225e679 100644 --- a/include/cru/ui/render/flex_layout_render_object.hpp +++ b/include/cru/ui/render/flex_layout_render_object.hpp @@ -38,7 +38,7 @@ class FlexLayoutRenderObject : public RenderObject { FlexChildLayoutData* GetChildLayoutData(int position); - void Draw(platform::Painter* painter) override; + void Draw(platform::graph::Painter* painter) override; RenderObject* HitTest(const Point& point) override; diff --git a/include/cru/ui/render/render_object.hpp b/include/cru/ui/render/render_object.hpp index 3cd14a6a..7f5f7ac6 100644 --- a/include/cru/ui/render/render_object.hpp +++ b/include/cru/ui/render/render_object.hpp @@ -10,7 +10,7 @@ namespace cru::ui { class Control; } -namespace cru::platform { +namespace cru::platform::graph { struct Painter; } @@ -58,7 +58,7 @@ class RenderObject : public Object { void Measure(const Size& available_size); void Layout(const Rect& rect); - virtual void Draw(platform::Painter* render_target) = 0; + virtual void Draw(platform::graph::Painter* painter) = 0; virtual RenderObject* HitTest(const Point& point) = 0; diff --git a/include/cru/ui/render/text_render_object.hpp b/include/cru/ui/render/text_render_object.hpp index 527fcd71..9f03551d 100644 --- a/include/cru/ui/render/text_render_object.hpp +++ b/include/cru/ui/render/text_render_object.hpp @@ -5,18 +5,18 @@ #include <string> // forward declarations -namespace cru::platform { +namespace cru::platform::graph { struct Brush; struct FontDescriptor; struct TextLayout; -} // namespace cru::platform +} // namespace cru::platform::graph namespace cru::ui::render { class TextRenderObject : public RenderObject { public: - TextRenderObject(std::shared_ptr<platform::Brush> brush, - std::shared_ptr<platform::FontDescriptor> font, - std::shared_ptr<platform::Brush> selection_brush); + TextRenderObject(std::shared_ptr<platform::graph::Brush> brush, + std::shared_ptr<platform::graph::FontDescriptor> font, + std::shared_ptr<platform::graph::Brush> selection_brush); TextRenderObject(const TextRenderObject& other) = delete; TextRenderObject(TextRenderObject&& other) = delete; TextRenderObject& operator=(const TextRenderObject& other) = delete; @@ -26,13 +26,13 @@ class TextRenderObject : public RenderObject { std::wstring GetText() const; void SetText(std::wstring new_text); - std::shared_ptr<platform::Brush> GetBrush() const { return brush_; } - void SetBrush(std::shared_ptr<platform::Brush> new_brush) { + std::shared_ptr<platform::graph::Brush> GetBrush() const { return brush_; } + void SetBrush(std::shared_ptr<platform::graph::Brush> new_brush) { new_brush.swap(brush_); } - std::shared_ptr<platform::FontDescriptor> GetFont() const; - void SetFont(std::shared_ptr<platform::FontDescriptor> font); + std::shared_ptr<platform::graph::FontDescriptor> GetFont() const; + void SetFont(std::shared_ptr<platform::graph::FontDescriptor> font); std::optional<TextRange> GetSelectionRange() const { return selection_range_; @@ -41,14 +41,14 @@ class TextRenderObject : public RenderObject { selection_range_ = std::move(new_range); } - std::shared_ptr<platform::Brush> GetSelectionBrush() const { + std::shared_ptr<platform::graph::Brush> GetSelectionBrush() const { return selection_brush_; } - void SetSelectionBrush(std::shared_ptr<platform::Brush> new_brush) { + void SetSelectionBrush(std::shared_ptr<platform::graph::Brush> new_brush) { new_brush.swap(selection_brush_); } - void Draw(platform::Painter* painter) override; + void Draw(platform::graph::Painter* painter) override; RenderObject* HitTest(const Point& point) override; @@ -59,11 +59,11 @@ class TextRenderObject : public RenderObject { void OnLayoutContent(const Rect& content_rect) override; private: - std::shared_ptr<platform::Brush> brush_; - std::shared_ptr<platform::FontDescriptor> font_; - std::shared_ptr<platform::TextLayout> text_layout_; + std::shared_ptr<platform::graph::Brush> brush_; + std::shared_ptr<platform::graph::FontDescriptor> font_; + std::shared_ptr<platform::graph::TextLayout> text_layout_; std::optional<TextRange> selection_range_ = std::nullopt; - std::shared_ptr<platform::Brush> selection_brush_; + std::shared_ptr<platform::graph::Brush> selection_brush_; }; } // namespace cru::ui::render diff --git a/include/cru/ui/render/window_render_object.hpp b/include/cru/ui/render/window_render_object.hpp index e0a7c74e..dfeae487 100644 --- a/include/cru/ui/render/window_render_object.hpp +++ b/include/cru/ui/render/window_render_object.hpp @@ -17,7 +17,7 @@ class WindowRenderObject : public RenderObject { void MeasureAndLayout(); - void Draw(platform::Painter* painter) override; + void Draw(platform::graph::Painter* painter) override; RenderObject* HitTest(const Point& point) override; diff --git a/include/cru/ui/ui_manager.hpp b/include/cru/ui/ui_manager.hpp index 6f4a3bc0..5317f579 100644 --- a/include/cru/ui/ui_manager.hpp +++ b/include/cru/ui/ui_manager.hpp @@ -3,7 +3,7 @@ #include <memory> -namespace cru::platform { +namespace cru::platform::graph { struct Brush; struct FontDescriptor; } // namespace cru::platform @@ -19,12 +19,12 @@ class PredefineResources : public Object { ~PredefineResources() override = default; // region Button - std::shared_ptr<platform::Brush> button_normal_border_brush; + std::shared_ptr<platform::graph::Brush> button_normal_border_brush; // region TextBlock - std::shared_ptr<platform::Brush> text_block_selection_brush; - std::shared_ptr<platform::Brush> text_block_text_brush; - std::shared_ptr<platform::FontDescriptor> text_block_font; + std::shared_ptr<platform::graph::Brush> text_block_selection_brush; + std::shared_ptr<platform::graph::Brush> text_block_text_brush; + std::shared_ptr<platform::graph::FontDescriptor> text_block_font; }; class UiManager : public Object { diff --git a/include/cru/ui/window.hpp b/include/cru/ui/window.hpp index 9a76f30f..0a81e2e1 100644 --- a/include/cru/ui/window.hpp +++ b/include/cru/ui/window.hpp @@ -5,7 +5,7 @@ #include <memory> -namespace cru::platform { +namespace cru::platform::native { struct NativeWindow; } @@ -38,7 +38,7 @@ class Window final : public ContentControl { render::RenderObject* GetRenderObject() const override; - platform::NativeWindow* GetNativeWindow() const { return native_window_; } + platform::native::NativeWindow* GetNativeWindow() const { return native_window_; } Control* GetMouseHoverControl() const { return mouse_hover_control_; } @@ -66,13 +66,14 @@ class Window final : public ContentControl { void OnNativeMouseEnterLeave(bool enter); void OnNativeMouseMove(const Point& point); - void OnNativeMouseDown(platform::MouseButton button, const Point& point); - void OnNativeMouseUp(platform::MouseButton button, const Point& point); + void OnNativeMouseDown(platform::native::MouseButton button, + const Point& point); + void OnNativeMouseUp(platform::native::MouseButton button, + const Point& point); void OnNativeKeyDown(int virtual_code); void OnNativeKeyUp(int virtual_code); - //*************** region: event dispatcher helper *************** void DispatchMouseHoverControlChangeEvent(Control* old_control, @@ -80,7 +81,7 @@ class Window final : public ContentControl { const Point& point); private: - platform::NativeWindow* native_window_; + platform::native::NativeWindow* native_window_; EventRevokerGuard event_revoker_guard_; std::shared_ptr<render::WindowRenderObject> render_object_; diff --git a/include/cru/platform/win/exception.hpp b/include/cru/win/exception.hpp index 01b139b4..39c52c45 100644 --- a/include/cru/platform/win/exception.hpp +++ b/include/cru/win/exception.hpp @@ -1,11 +1,13 @@ #pragma once #include "win_pre_config.hpp" +#include "cru/platform/exception.hpp" + #include <stdexcept> #include <string_view> -namespace cru::platform::win { -class HResultError : public std::runtime_error { +namespace cru::win { +class HResultError : public platform::PlatformException { public: explicit HResultError(HRESULT h_result); explicit HResultError(HRESULT h_result, @@ -31,7 +33,7 @@ inline void ThrowIfFailed(const HRESULT h_result, if (FAILED(h_result)) throw HResultError(h_result, message); } -class Win32Error : public std::runtime_error { +class Win32Error : public platform::PlatformException { public: explicit Win32Error(DWORD error_code); Win32Error(DWORD error_code, const std::string_view& additional_message); @@ -46,4 +48,4 @@ class Win32Error : public std::runtime_error { private: DWORD error_code_; }; -} // namespace cru::platform::win +} // namespace cru::win diff --git a/include/cru/platform/win/d2d_util.hpp b/include/cru/win/graph/d2d_util.hpp index 51aea4f8..7a2da91f 100644 --- a/include/cru/platform/win/d2d_util.hpp +++ b/include/cru/win/graph/d2d_util.hpp @@ -1,11 +1,11 @@ #pragma once -#include "win_pre_config.hpp" +#include "../win_pre_config.hpp" -#include "../matrix.hpp" #include "cru/common/ui_base.hpp" +#include "cru/platform/matrix.hpp" -namespace cru::platform::win::util { -inline D2D1_MATRIX_3X2_F Convert(const Matrix& matrix) { +namespace cru::win::graph::util { +inline D2D1_MATRIX_3X2_F Convert(const platform::Matrix& matrix) { D2D1_MATRIX_3X2_F m; m._11 = matrix.m11; m._12 = matrix.m12; @@ -40,9 +40,9 @@ inline D2D1_ELLIPSE Convert(const ui::Ellipse& ellipse) { ellipse.radius_y); } -inline Matrix Convert(const D2D1_MATRIX_3X2_F& matrix) { - return Matrix{matrix._11, matrix._12, matrix._21, - matrix._22, matrix._31, matrix._32}; +inline platform::Matrix Convert(const D2D1_MATRIX_3X2_F& matrix) { + return platform::Matrix{matrix._11, matrix._12, matrix._21, + matrix._22, matrix._31, matrix._32}; } inline ui::Color Convert(const D2D1_COLOR_F& color) { @@ -105,4 +105,4 @@ inline bool operator==(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { inline bool operator!=(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { return !(left == right); } -} // namespace cru::platform::win::util +} // namespace cru::win::graph::util diff --git a/include/cru/platform/win/graph_manager.hpp b/include/cru/win/graph/graph_manager.hpp index f2e29c22..bf14b802 100644 --- a/include/cru/platform/win/graph_manager.hpp +++ b/include/cru/win/graph/graph_manager.hpp @@ -1,14 +1,17 @@ #pragma once -#include "win_pre_config.hpp" +#include "../win_pre_config.hpp" -#include "../basic_types.hpp" #include "cru/common/base.hpp" +#include "cru/win/graph/win_graph_factory.hpp" -namespace cru::platform::win { -class WindowRenderTarget; +#include <memory> +namespace cru::win::graph { class GraphManager final : public Object { public: + static GraphManager* GetInstance(); + + public: GraphManager(); GraphManager(const GraphManager& other) = delete; GraphManager(GraphManager&& other) = delete; @@ -33,6 +36,8 @@ class GraphManager final : public Object { return dwrite_system_font_collection_.Get(); } + WinGraphFactory* GetGraphFactory() const { return graph_factory_.get(); } + private: Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device_; Microsoft::WRL::ComPtr<ID2D1Factory1> d2d1_factory_; @@ -40,5 +45,7 @@ class GraphManager final : public Object { Microsoft::WRL::ComPtr<IDXGIFactory2> dxgi_factory_; Microsoft::WRL::ComPtr<IDWriteFactory> dwrite_factory_; Microsoft::WRL::ComPtr<IDWriteFontCollection> dwrite_system_font_collection_; + + std::unique_ptr<WinGraphFactory> graph_factory_; }; -} // namespace cru::platform::win +} // namespace cru::win::graph diff --git a/include/cru/platform/win/win_brush.hpp b/include/cru/win/graph/win_brush.hpp index d32f6bbc..69cd952d 100644 --- a/include/cru/platform/win/win_brush.hpp +++ b/include/cru/win/graph/win_brush.hpp @@ -1,19 +1,21 @@ #pragma once -#include "win_pre_config.hpp" +#include "../win_pre_config.hpp" -#include "../brush.hpp" +#include "cru/platform/graph/brush.hpp" -namespace cru::platform::win { -struct WinBrush : virtual Brush { +namespace cru::win::graph { +class GraphManager; + +struct WinBrush : virtual platform::graph::Brush { virtual ID2D1Brush* GetD2DBrush() = 0; }; class WinSolidColorBrush : public Object, - public virtual SolidColorBrush, + public virtual platform::graph::SolidColorBrush, public virtual WinBrush { public: - explicit WinSolidColorBrush( - Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> brush); + explicit WinSolidColorBrush(GraphManager* graph_manager, + const ui::Color& color); WinSolidColorBrush(const WinSolidColorBrush& other) = delete; WinSolidColorBrush(WinSolidColorBrush&& other) = delete; WinSolidColorBrush& operator=(const WinSolidColorBrush& other) = delete; @@ -28,4 +30,4 @@ class WinSolidColorBrush : public Object, private: Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> brush_; }; -} // namespace cru::platform::win +} // namespace cru::win::graph diff --git a/include/cru/platform/win/win_font.hpp b/include/cru/win/graph/win_font.hpp index f03ff3c8..3301e8da 100644 --- a/include/cru/platform/win/win_font.hpp +++ b/include/cru/win/graph/win_font.hpp @@ -1,17 +1,19 @@ #pragma once -#include "win_pre_config.hpp" +#include "../win_pre_config.hpp" -#include "../font.hpp" +#include "cru/platform/graph/font.hpp" #include <string_view> -namespace cru::platform::win { +namespace cru::win::graph { class GraphManager; -class WinFontDescriptor : public Object, public virtual FontDescriptor { +class WinFontDescriptor : public Object, + public virtual platform::graph::FontDescriptor { public: explicit WinFontDescriptor(GraphManager* graph_manager, - const std::wstring_view& font_family, float font_size); + const std::wstring_view& font_family, + float font_size); WinFontDescriptor(const WinFontDescriptor& other) = delete; WinFontDescriptor(WinFontDescriptor&& other) = delete; WinFontDescriptor& operator=(const WinFontDescriptor& other) = delete; @@ -23,4 +25,4 @@ class WinFontDescriptor : public Object, public virtual FontDescriptor { private: Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_; }; -} // namespace cru::platform::win +} // namespace cru::win::graph diff --git a/include/cru/platform/win/win_geometry.hpp b/include/cru/win/graph/win_geometry.hpp index e8ab7796..005a4384 100644 --- a/include/cru/platform/win/win_geometry.hpp +++ b/include/cru/win/graph/win_geometry.hpp @@ -1,12 +1,15 @@ #pragma once -#include "win_pre_config.hpp" +#include "../win_pre_config.hpp" -#include "../geometry.hpp" +#include "cru/platform/graph/geometry.hpp" -namespace cru::platform::win { -class WinGeometryBuilder : public Object, public virtual GeometryBuilder { +namespace cru::win::graph { +class GraphManager; + +class WinGeometryBuilder : public Object, + public virtual platform::graph::GeometryBuilder { public: - explicit WinGeometryBuilder(Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry); + explicit WinGeometryBuilder(GraphManager* graph_manger); WinGeometryBuilder(const WinGeometryBuilder& other) = delete; WinGeometryBuilder(WinGeometryBuilder&& other) = delete; WinGeometryBuilder& operator=(const WinGeometryBuilder& other) = delete; @@ -19,14 +22,14 @@ class WinGeometryBuilder : public Object, public virtual GeometryBuilder { void QuadraticBezierTo(const ui::Point& control_point, const ui::Point& end_point) override; void CloseFigure(bool close) override; - Geometry* Build() override; + platform::graph::Geometry* Build() override; private: Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry_; Microsoft::WRL::ComPtr<ID2D1GeometrySink> geometry_sink_; }; -class WinGeometry : public Object, public virtual Geometry { +class WinGeometry : public Object, public virtual platform::graph::Geometry { public: explicit WinGeometry(Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry); WinGeometry(const WinGeometry& other) = delete; @@ -42,4 +45,4 @@ class WinGeometry : public Object, public virtual Geometry { private: Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry_; }; -} // namespace cru::platform::win +} // namespace cru::win::graph diff --git a/include/cru/win/graph/win_graph_factory.hpp b/include/cru/win/graph/win_graph_factory.hpp new file mode 100644 index 00000000..f8a07819 --- /dev/null +++ b/include/cru/win/graph/win_graph_factory.hpp @@ -0,0 +1,30 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/platform/graph/graph_factory.hpp" + +namespace cru::win::graph { +class GraphManager; + +class WinGraphFactory : public Object, + public virtual platform::graph::GraphFactory { + public: + explicit WinGraphFactory(GraphManager* graph_manager); + WinGraphFactory(const WinGraphFactory& other) = delete; + WinGraphFactory(WinGraphFactory&& other) = delete; + WinGraphFactory& operator=(const WinGraphFactory& other) = delete; + WinGraphFactory& operator=(WinGraphFactory&& other) = delete; + ~WinGraphFactory() override = default; + + platform::graph::SolidColorBrush* CreateSolidColorBrush( + const ui::Color& color) override; + platform::graph::GeometryBuilder* CreateGeometryBuilder() override; + platform::graph::FontDescriptor* CreateFontDescriptor( + const std::wstring_view& font_family, float font_size); + platform::graph::TextLayout* CreateTextLayout( + std::shared_ptr<platform::graph::FontDescriptor> font, std::wstring text); + + private: + GraphManager* graph_manager_; +}; +} // namespace cru::win::graph diff --git a/include/cru/win/graph/win_painter.hpp b/include/cru/win/graph/win_painter.hpp new file mode 100644 index 00000000..8351cb7b --- /dev/null +++ b/include/cru/win/graph/win_painter.hpp @@ -0,0 +1,48 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/platform/graph/painter.hpp" + +namespace cru::win::graph { +class GraphManager; + +class WinPainter : public Object, public virtual platform::graph::Painter { + public: + explicit WinPainter(ID2D1RenderTarget* render_target); + WinPainter(const WinPainter& other) = delete; + WinPainter(WinPainter&& other) = delete; + WinPainter& operator=(const WinPainter& other) = delete; + WinPainter& operator=(WinPainter&& other) = delete; + ~WinPainter() override; + + platform::Matrix GetTransform() override; + void SetTransform(const platform::Matrix& matrix) override; + void Clear(const ui::Color& color) override; + void StrokeRectangle(const ui::Rect& rectangle, platform::graph::Brush* brush, + float width) override; + void FillRectangle(const ui::Rect& rectangle, + platform::graph::Brush* brush) override; + void StrokeGeometry(platform::graph::Geometry* geometry, + platform::graph::Brush* brush, float width) override; + void FillGeometry(platform::graph::Geometry* geometry, + platform::graph::Brush* brush) override; + void DrawText(const ui::Point& offset, + platform::graph::TextLayout* text_layout, + platform::graph::Brush* brush) override; + void EndDraw() override; + bool IsDisposed() override { return is_disposed_; } + + void EndDrawAndDeleteThis() { + EndDraw(); + delete this; + } + + protected: + virtual void DoEndDraw(); + + private: + ID2D1RenderTarget* render_target_; + + bool is_disposed_ = false; +}; +} // namespace cru::win::graph diff --git a/include/cru/platform/win/win_text_layout.hpp b/include/cru/win/graph/win_text_layout.hpp index dfb54264..b861fda7 100644 --- a/include/cru/platform/win/win_text_layout.hpp +++ b/include/cru/win/graph/win_text_layout.hpp @@ -1,15 +1,15 @@ #pragma once -#include "win_pre_config.hpp" +#include "../win_pre_config.hpp" -#include "../text_layout.hpp" -#include "win_font.hpp" +#include "cru/platform/graph/text_layout.hpp" #include <memory> -namespace cru::platform::win { +namespace cru::win::graph { class GraphManager; +class WinFontDescriptor; -class WinTextLayout : public Object, public virtual TextLayout { +class WinTextLayout : public Object, public virtual platform::graph::TextLayout { public: explicit WinTextLayout(GraphManager* graph_manager, std::shared_ptr<WinFontDescriptor> font, std::wstring text); @@ -21,8 +21,8 @@ class WinTextLayout : public Object, public virtual TextLayout { std::wstring GetText() override; void SetText(std::wstring new_text) override; - std::shared_ptr<FontDescriptor> GetFont(); - void SetFont(std::shared_ptr<FontDescriptor> font); + std::shared_ptr<platform::graph::FontDescriptor> GetFont(); + void SetFont(std::shared_ptr<platform::graph::FontDescriptor> font); void SetMaxWidth(float max_width) override; void SetMaxHeight(float max_height) override; ui::Rect GetTextBounds() override; diff --git a/include/cru/platform/win/god_window.hpp b/include/cru/win/native/god_window.hpp index 95a253e9..9ac49858 100644 --- a/include/cru/platform/win/god_window.hpp +++ b/include/cru/win/native/god_window.hpp @@ -1,11 +1,11 @@ #pragma once -#include "win_pre_config.hpp" - -#include <memory> +#include "../win_pre_config.hpp" #include "cru/common/base.hpp" -namespace cru::platform::win { +#include <memory> + +namespace cru::win::native { class WinApplication; class WindowClass; @@ -29,4 +29,4 @@ class GodWindow : public Object { std::shared_ptr<WindowClass> god_window_class_; HWND hwnd_; }; -} // namespace cru::platform::win
\ No newline at end of file +} // namespace cru::win::native
\ No newline at end of file diff --git a/include/cru/platform/win/win_application.hpp b/include/cru/win/native/win_application.hpp index f8150aed..458b10ae 100644 --- a/include/cru/platform/win/win_application.hpp +++ b/include/cru/win/native/win_application.hpp @@ -1,19 +1,17 @@ #pragma once -#include "win_pre_config.hpp" +#include "../win_pre_config.hpp" -#include "../ui_applicaition.hpp" -#include "cru/common/base.hpp" +#include "cru/platform/native/ui_applicaition.hpp" #include <memory> -namespace cru::platform::win { +namespace cru::win::native { class GodWindow; class TimerManager; class WindowManager; -class GraphManager; -class WinGraphFactory; -class WinApplication : public Object, public virtual UiApplication { +class WinApplication : public Object, + public virtual platform::native::UiApplication { public: static WinApplication* GetInstance(); @@ -41,19 +39,15 @@ class WinApplication : public Object, public virtual UiApplication { const std::function<void()>& action) override; void CancelTimer(unsigned long id) override; - std::vector<NativeWindow*> GetAllWindow() override; - NativeWindow* CreateWindow(NativeWindow* parent) override; - - GraphFactory* GetGraphFactory() override; - + std::vector<platform::native::NativeWindow*> GetAllWindow() override; + platform::native::NativeWindow* CreateWindow( + platform::native::NativeWindow* parent) override; HINSTANCE GetInstanceHandle() const { return h_instance_; } GodWindow* GetGodWindow() const { return god_window_.get(); } TimerManager* GetTimerManager() const { return timer_manager_.get(); } WindowManager* GetWindowManager() const { return window_manager_.get(); } - GraphManager* GetGraphManager() const { return graph_manager_.get(); } - WinGraphFactory* GetWinGraphFactory() const { return graph_factory_.get(); } private: HINSTANCE h_instance_; @@ -61,7 +55,5 @@ class WinApplication : public Object, public virtual UiApplication { std::shared_ptr<GodWindow> god_window_; std::shared_ptr<TimerManager> timer_manager_; std::shared_ptr<WindowManager> window_manager_; - std::shared_ptr<GraphManager> graph_manager_; - std::shared_ptr<WinGraphFactory> graph_factory_; }; -} // namespace cru::platform::win +} // namespace cru::win::native diff --git a/include/cru/platform/win/win_native_window.hpp b/include/cru/win/native/win_native_window.hpp index 7b93fd5c..2b9be25d 100644 --- a/include/cru/platform/win/win_native_window.hpp +++ b/include/cru/win/native/win_native_window.hpp @@ -1,18 +1,19 @@ #pragma once -#include "win_pre_config.hpp" +#include "../win_pre_config.hpp" -#include "../native_window.hpp" +#include "cru/platform/native/native_window.hpp" #include "window_native_message_event_args.hpp" #include <memory> -namespace cru::platform::win { +namespace cru::win::native { class WinApplication; class WindowClass; class WindowManager; class WindowRenderTarget; -class WinNativeWindow : public Object, public virtual NativeWindow { +class WinNativeWindow : public Object, + public virtual platform::native::NativeWindow { public: WinNativeWindow(WinApplication* application, std::shared_ptr<WindowClass> window_class, DWORD window_style, @@ -44,7 +45,7 @@ class WinNativeWindow : public Object, public virtual NativeWindow { // The lefttop of the rect is relative to screen lefttop. void SetWindowRect(const ui::Rect& rect) override; - Painter* BeginPaint() override; + platform::graph::Painter* BeginPaint() override; Event<>* DestroyEvent() override { return &destroy_event_; } Event<const ui::Size&>* ResizeEvent() override { return &resize_event_; } @@ -53,11 +54,15 @@ class WinNativeWindow : public Object, public virtual NativeWindow { Event<bool>* MouseEnterLeaveEvent() override { return &mouse_enter_leave_event_; } - Event<const ui::Point&>* MouseMoveEvent() override { return &mouse_move_event_; } - Event<MouseButton, const ui::Point&>* MouseDownEvent() override { + Event<const ui::Point&>* MouseMoveEvent() override { + return &mouse_move_event_; + } + Event<platform::native::MouseButton, const ui::Point&>* MouseDownEvent() + override { return &mouse_down_event_; } - Event<MouseButton, const ui::Point&>* MouseUpEvent() override { + Event<platform::native::MouseButton, const ui::Point&>* MouseUpEvent() + override { return &mouse_up_event_; } Event<int>* KeyDownEvent() override { return &key_down_event_; } @@ -92,8 +97,8 @@ class WinNativeWindow : public Object, public virtual NativeWindow { void OnMouseMoveInternal(POINT point); void OnMouseLeaveInternal(); - void OnMouseDownInternal(MouseButton button, POINT point); - void OnMouseUpInternal(MouseButton button, POINT point); + void OnMouseDownInternal(platform::native::MouseButton button, POINT point); + void OnMouseUpInternal(platform::native::MouseButton button, POINT point); void OnMouseWheelInternal(short delta, POINT point); void OnKeyDownInternal(int virtual_code); @@ -122,11 +127,11 @@ class WinNativeWindow : public Object, public virtual NativeWindow { Event<bool> focus_event_; Event<bool> mouse_enter_leave_event_; Event<const ui::Point&> mouse_move_event_; - Event<MouseButton, const ui::Point&> mouse_down_event_; - Event<MouseButton, const ui::Point&> mouse_up_event_; + Event<platform::native::MouseButton, const ui::Point&> mouse_down_event_; + Event<platform::native::MouseButton, const ui::Point&> mouse_up_event_; Event<int> key_down_event_; Event<int> key_up_event_; Event<WindowNativeMessageEventArgs&> native_message_event_; }; -} // namespace cru::platform::win +} // namespace cru::win::native diff --git a/include/cru/platform/win/window_class.hpp b/include/cru/win/native/window_class.hpp index be79af13..17712958 100644 --- a/include/cru/platform/win/window_class.hpp +++ b/include/cru/win/native/window_class.hpp @@ -1,14 +1,15 @@ #pragma once -#include "win_pre_config.hpp" +#include "../win_pre_config.hpp" #include "cru/common/base.hpp" #include <string> -namespace cru::platform::win { +namespace cru::win::native { class WindowClass : public Object { public: - WindowClass(const std::wstring& name, WNDPROC window_proc, HINSTANCE h_instance); + WindowClass(const std::wstring& name, WNDPROC window_proc, + HINSTANCE h_instance); WindowClass(const WindowClass& other) = delete; WindowClass(WindowClass&& other) = delete; WindowClass& operator=(const WindowClass& other) = delete; @@ -23,4 +24,4 @@ class WindowClass : public Object { std::wstring name_; ATOM atom_; }; -} // namespace cru::ui +} // namespace cru::win::native diff --git a/include/cru/platform/win/window_native_message_event_args.hpp b/include/cru/win/native/window_native_message_event_args.hpp index b3419c24..37149f36 100644 --- a/include/cru/platform/win/window_native_message_event_args.hpp +++ b/include/cru/win/native/window_native_message_event_args.hpp @@ -1,9 +1,9 @@ #pragma once -#include "win_pre_config.hpp" +#include "../win_pre_config.hpp" #include "cru/common/base.hpp" -namespace cru::platform::win { +namespace cru::win::native { struct WindowNativeMessage { HWND hwnd; UINT msg; @@ -42,4 +42,4 @@ class WindowNativeMessageEventArgs : public Object { LRESULT result_; bool handled_ = false; }; -} // namespace cru::platform::win +} // namespace cru::win::native diff --git a/include/cru/platform/win/window_render_target.hpp b/include/cru/win/native/window_render_target.hpp index f328a84a..c55055c6 100644 --- a/include/cru/platform/win/window_render_target.hpp +++ b/include/cru/win/native/window_render_target.hpp @@ -1,16 +1,17 @@ #pragma once -#include "win_pre_config.hpp" +#include "../win_pre_config.hpp" #include "cru/common/base.hpp" - -namespace cru::platform::win { +namespace cru::win::graph { class GraphManager; +} +namespace cru::win::native { // Represents a window render target. class WindowRenderTarget : public Object { public: - WindowRenderTarget(GraphManager* graph_manager, HWND hwnd); + WindowRenderTarget(graph::GraphManager* graph_manager, HWND hwnd); WindowRenderTarget(const WindowRenderTarget& other) = delete; WindowRenderTarget(WindowRenderTarget&& other) = delete; WindowRenderTarget& operator=(const WindowRenderTarget& other) = delete; @@ -19,7 +20,7 @@ class WindowRenderTarget : public Object { public: // Get the graph manager that created the render target. - GraphManager* GetGraphManager() const { return graph_manager_; } + graph::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(); } @@ -37,8 +38,8 @@ class WindowRenderTarget : public Object { void CreateTargetBitmap(); private: - GraphManager* graph_manager_; + graph::GraphManager* graph_manager_; Microsoft::WRL::ComPtr<IDXGISwapChain1> dxgi_swap_chain_; Microsoft::WRL::ComPtr<ID2D1Bitmap1> target_bitmap_; }; -} // namespace cru::graph +} // namespace cru::win::native diff --git a/include/cru/platform/win/win_pre_config.hpp b/include/cru/win/win_pre_config.hpp index 6962eb7b..6962eb7b 100644 --- a/include/cru/platform/win/win_pre_config.hpp +++ b/include/cru/win/win_pre_config.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8f29ddc9..b2209bb3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,27 +1,9 @@ -add_library(cru_ui STATIC - ui/content_control.cpp - ui/control.cpp - ui/layout_control.cpp - ui/no_child_control.cpp - ui/ui_manager.cpp - ui/window.cpp - ui/controls/button.cpp - ui/controls/flex_layout.cpp - ui/controls/text_block.cpp - ui/render/border_render_object.cpp - ui/render/flex_layout_render_object.cpp - ui/render/render_object.cpp - ui/render/text_render_object.cpp - ui/render/window_render_object.cpp) - -target_include_directories(cru_ui PUBLIC ${PROJECT_SOURCE_DIR}/include .) +add_subdirectory(ui) if(WIN32) -add_subdirectory(platform_win) -target_link_libraries(cru_ui PUBLIC cru_platform_win) +add_subdirectory(win) endif() - add_executable(demo main.cpp) target_link_libraries(demo PRIVATE cru_ui) diff --git a/src/main.cpp b/src/main.cpp index c0c90002..ecaebda5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,11 @@ -#include "cru/platform/native_window.hpp" -#include "cru/platform/ui_applicaition.hpp" +#include "cru/platform/native/native_window.hpp" +#include "cru/platform/native/ui_applicaition.hpp" #include "cru/ui/controls/button.hpp" #include "cru/ui/controls/flex_layout.hpp" #include "cru/ui/controls/text_block.hpp" #include "cru/ui/window.hpp" -using cru::platform::UiApplication; +using cru::platform::native::UiApplication; using cru::ui::Rect; using cru::ui::Thickness; using cru::ui::Window; diff --git a/src/platform_win/CMakeLists.txt b/src/platform_win/CMakeLists.txt deleted file mode 100644 index 27e93091..00000000 --- a/src/platform_win/CMakeLists.txt +++ /dev/null @@ -1,21 +0,0 @@ -add_library(cru_platform_win STATIC - debug.cpp - exception.cpp - god_window.cpp - graph_manager.cpp - string_util.cpp - timer.cpp - win_application.cpp - win_brush.cpp - win_font.cpp - win_geometry.cpp - win_graph_factory.cpp - win_native_window.cpp - win_painter.cpp - win_text_layout.cpp - window_class.cpp - window_manager.cpp - window_render_target.cpp) -target_include_directories(cru_platform_win PUBLIC ${PROJECT_SOURCE_DIR}/include .) -target_link_libraries(cru_platform_win PRIVATE D3D11 D2d1 DWrite) -target_compile_definitions(cru_platform_win PUBLIC UNICODE _UNICODE) # use unicode diff --git a/src/platform_win/god_window_message.hpp b/src/platform_win/god_window_message.hpp deleted file mode 100644 index a906a3b7..00000000 --- a/src/platform_win/god_window_message.hpp +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once -#include "cru/platform/win/win_pre_config.hpp" - -namespace cru::platform::win { -constexpr int invoke_later_message_id = WM_USER + 2000; -} diff --git a/src/platform_win/win_brush.cpp b/src/platform_win/win_brush.cpp deleted file mode 100644 index 189dfd57..00000000 --- a/src/platform_win/win_brush.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "cru/platform/win/win_brush.hpp" - -#include "cru/platform/win/d2d_util.hpp" -#include "cru/platform/win/exception.hpp" - -#include <cassert> - -namespace cru::platform::win { -WinSolidColorBrush::WinSolidColorBrush( - Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> brush) { - assert(brush); - brush_ = std::move(brush); -} - -ui::Color cru::platform::win::WinSolidColorBrush::GetColor() { - return util::Convert(brush_->GetColor()); -} -void WinSolidColorBrush::SetColor(const ui::Color& color) { - brush_->SetColor(util::Convert(color)); -} -} // namespace cru::platform::win diff --git a/src/platform_win/win_graph_factory.cpp b/src/platform_win/win_graph_factory.cpp deleted file mode 100644 index 03031b32..00000000 --- a/src/platform_win/win_graph_factory.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "cru/platform/win/win_graph_factory.hpp" - -#include "cru/platform/win/d2d_util.hpp" -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/graph_manager.hpp" -#include "cru/platform/win/win_brush.hpp" -#include "cru/platform/win/win_font.hpp" -#include "cru/platform/win/win_geometry.hpp" -#include "cru/platform/win/win_text_layout.hpp" - -#include <cassert> -#include <utility> - -namespace cru::platform::win { -WinGraphFactory::WinGraphFactory(GraphManager* graph_manager) { - assert(graph_manager); - graph_manager_ = graph_manager; -} - -SolidColorBrush* WinGraphFactory::CreateSolidColorBrush( - const ui::Color& color) { - Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> brush; - ThrowIfFailed(graph_manager_->GetD2D1DeviceContext()->CreateSolidColorBrush( - util::Convert(color), &brush)); - return new WinSolidColorBrush(std::move(brush)); -} - -GeometryBuilder* WinGraphFactory::CreateGeometryBuilder() { - Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry; - ThrowIfFailed( - graph_manager_->GetD2D1Factory()->CreatePathGeometry(&geometry)); - return new WinGeometryBuilder(std::move(geometry)); -} - -FontDescriptor* WinGraphFactory::CreateFontDescriptor( - const std::wstring_view& font_family, float font_size) { - return new WinFontDescriptor(graph_manager_, font_family, font_size); -} - -TextLayout* WinGraphFactory::CreateTextLayout( - std::shared_ptr<FontDescriptor> font, std::wstring text) { - const auto f = std::dynamic_pointer_cast<WinFontDescriptor>(font); - assert(f); - return new WinTextLayout(graph_manager_, std::move(f), std::move(text)); -} -} // namespace cru::platform::win diff --git a/src/platform_win/win_painter.cpp b/src/platform_win/win_painter.cpp deleted file mode 100644 index 7c99146a..00000000 --- a/src/platform_win/win_painter.cpp +++ /dev/null @@ -1,104 +0,0 @@ -#include "cru/platform/win/win_painter.hpp" - -#include "cru/platform/win/d2d_util.hpp" -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/graph_manager.hpp" -#include "cru/platform/win/win_brush.hpp" -#include "cru/platform/win/win_geometry.hpp" -#include "cru/platform/win/win_native_window.hpp" -#include "cru/platform/win/win_text_layout.hpp" -#include "cru/platform/win/window_render_target.hpp" - -#include <cassert> - -namespace cru::platform::win { -WinPainter::WinPainter(WinNativeWindow* window) { - assert(window); - window_ = window; - const auto window_render_target = window_->GetWindowRenderTarget(); - render_target_ = - window_render_target->GetGraphManager()->GetD2D1DeviceContext(); - window_render_target->SetAsTarget(); - render_target_->BeginDraw(); -} - -WinPainter::~WinPainter() { - if (!IsDisposed()) { - ThrowIfFailed(render_target_->EndDraw()); - } -} - -Matrix WinPainter::GetTransform() { - assert(!IsDisposed()); - D2D1_MATRIX_3X2_F m; - render_target_->GetTransform(&m); - return util::Convert(m); -} - -void WinPainter::SetTransform(const Matrix& matrix) { - assert(!IsDisposed()); - render_target_->SetTransform(util::Convert(matrix)); -} - -void WinPainter::Clear(const ui::Color& color) { - assert(!IsDisposed()); - render_target_->Clear(util::Convert(color)); -} - -void WinPainter::StrokeRectangle(const ui::Rect& rectangle, Brush* brush, - float width) { - assert(!IsDisposed()); - const auto b = dynamic_cast<WinBrush*>(brush); - assert(b); - render_target_->DrawRectangle(util::Convert(rectangle), b->GetD2DBrush(), - width); -} - -void WinPainter::FillRectangle(const ui::Rect& rectangle, Brush* brush) { - assert(!IsDisposed()); - const auto b = dynamic_cast<WinBrush*>(brush); - assert(b); - render_target_->FillRectangle(util::Convert(rectangle), b->GetD2DBrush()); -} - -void WinPainter::StrokeGeometry(Geometry* geometry, Brush* brush, float width) { - assert(!IsDisposed()); - const auto g = dynamic_cast<WinGeometry*>(geometry); - assert(g); - const auto b = dynamic_cast<WinBrush*>(brush); - assert(b); - - render_target_->DrawGeometry(g->GetNative(), b->GetD2DBrush(), width); -} - -void WinPainter::FillGeometry(Geometry* geometry, Brush* brush) { - assert(!IsDisposed()); - const auto g = dynamic_cast<WinGeometry*>(geometry); - assert(g); - const auto b = dynamic_cast<WinBrush*>(brush); - assert(b); - - render_target_->FillGeometry(g->GetNative(), b->GetD2DBrush()); -} - -void WinPainter::DrawText(const ui::Point& offset, TextLayout* text_layout, - Brush* brush) { - assert(!IsDisposed()); - const auto t = dynamic_cast<WinTextLayout*>(text_layout); - assert(t); - const auto b = dynamic_cast<WinBrush*>(brush); - assert(b); - - render_target_->DrawTextLayout(util::Convert(offset), - t->GetDWriteTextLayout(), b->GetD2DBrush()); -} - -void WinPainter::EndDraw() { - if (!IsDisposed()) { - ThrowIfFailed(render_target_->EndDraw()); - render_target_ = nullptr; - window_->GetWindowRenderTarget()->Present(); - is_disposed = true; - } -} -} // namespace cru::platform::win diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt new file mode 100644 index 00000000..900948ca --- /dev/null +++ b/src/ui/CMakeLists.txt @@ -0,0 +1,22 @@ +add_library(cru_ui STATIC + content_control.cpp + control.cpp + layout_control.cpp + no_child_control.cpp + ui_manager.cpp + window.cpp + controls/button.cpp + controls/flex_layout.cpp + controls/text_block.cpp + render/border_render_object.cpp + render/flex_layout_render_object.cpp + render/render_object.cpp + render/text_render_object.cpp + render/window_render_object.cpp) + +target_include_directories(cru_ui PUBLIC ${PROJECT_SOURCE_DIR}/include .) + +if(WIN32) +target_link_libraries(cru_ui PUBLIC cru_win_native) +endif() + diff --git a/src/ui/control.cpp b/src/ui/control.cpp index f915af07..7563dc91 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -63,7 +63,7 @@ void Control::OnAttachToWindow(Window* window) {} void Control::OnDetachToWindow(Window* window) {} -void Control::OnMouseClickBegin(platform::MouseButton button) {} +void Control::OnMouseClickBegin(platform::native::MouseButton button) {} -void Control::OnMouseClickEnd(platform::MouseButton button) {} +void Control::OnMouseClickEnd(platform::native::MouseButton button) {} } // namespace cru::ui diff --git a/src/ui/render/border_render_object.cpp b/src/ui/render/border_render_object.cpp index 97e386dd..541be473 100644 --- a/src/ui/render/border_render_object.cpp +++ b/src/ui/render/border_render_object.cpp @@ -1,26 +1,26 @@ - #include "cru/ui/render/border_render_object.hpp" +#include "cru/ui/render/border_render_object.hpp" #include "cru/platform/debug.hpp" -#include "cru/platform/geometry.hpp" -#include "cru/platform/graph_factory.hpp" -#include "cru/platform/painter_util.hpp" -#include "cru/platform/ui_applicaition.hpp" +#include "cru/platform/graph/geometry.hpp" +#include "cru/platform/graph/graph_factory.hpp" +#include "cru/platform/graph/painter_util.hpp" #include <algorithm> #include <cassert> namespace cru::ui::render { -BorderRenderObject::BorderRenderObject(std::shared_ptr<platform::Brush> brush) { +BorderRenderObject::BorderRenderObject( + std::shared_ptr<platform::graph::Brush> brush) { assert(brush); this->border_brush_ = std::move(brush); RecreateGeometry(); } -void BorderRenderObject::Draw(platform::Painter* painter) { +void BorderRenderObject::Draw(platform::graph::Painter* painter) { painter->FillGeometry(geometry_.get(), border_brush_.get()); if (const auto child = GetChild()) { auto offset = child->GetOffset(); - platform::util::WithTransform( + platform::graph::util::WithTransform( painter, platform::Matrix::Translation(offset.x, offset.y), [child](auto p) { child->Draw(p); }); } @@ -75,13 +75,13 @@ void BorderRenderObject::OnMeasureCore(const Size& available_size) { auto coerced_margin_border_padding_size = margin_border_padding_size; if (coerced_margin_border_padding_size.width > available_size.width) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Measure: horizontal length of padding, border and margin is bigger " L"than available length."); coerced_margin_border_padding_size.width = available_size.width; } if (coerced_margin_border_padding_size.height > available_size.height) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Measure: vertical length of padding, border and margin is bigger " L"than available length."); coerced_margin_border_padding_size.height = available_size.height; @@ -113,13 +113,13 @@ void BorderRenderObject::OnLayoutCore(const Rect& rect) { auto coerced_content_available_size = content_available_size; if (coerced_content_available_size.width < 0) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Layout: horizontal length of padding, border and margin is bigger " L"than available length."); coerced_content_available_size.width = 0; } if (coerced_content_available_size.height < 0) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Layout: vertical length of padding, border and margin is bigger " L"than " L"available length."); @@ -154,7 +154,7 @@ void BorderRenderObject::RecreateGeometry() { geometry_.reset(); border_outer_geometry_.reset(); - auto f = [](platform::GeometryBuilder* builder, const Rect& rect, + auto f = [](platform::graph::GeometryBuilder* builder, const Rect& rect, const CornerRadius& corner) { builder->BeginFigure(Point(rect.left + corner.left_top.x, rect.top)); builder->LineTo(Point(rect.GetRight() - corner.right_top.x, rect.top)); @@ -181,9 +181,9 @@ void BorderRenderObject::RecreateGeometry() { const Rect outer_rect{margin.left, margin.top, size.width - margin.GetHorizontalTotal(), size.height - margin.GetVerticalTotal()}; - const auto graph_factory = - platform::UiApplication::GetInstance()->GetGraphFactory(); - std::unique_ptr<platform::GeometryBuilder> builder{graph_factory->CreateGeometryBuilder()}; + const auto graph_factory = platform::graph::GraphFactory::GetInstance(); + std::unique_ptr<platform::graph::GeometryBuilder> builder{ + graph_factory->CreateGeometryBuilder()}; f(builder.get(), outer_rect, corner_radius_); border_outer_geometry_.reset(builder->Build()); builder.reset(); diff --git a/src/ui/render/flex_layout_render_object.cpp b/src/ui/render/flex_layout_render_object.cpp index e7840b7e..7528439f 100644 --- a/src/ui/render/flex_layout_render_object.cpp +++ b/src/ui/render/flex_layout_render_object.cpp @@ -1,15 +1,13 @@ #include "cru/ui/render/flex_layout_render_object.hpp" #include "cru/platform/debug.hpp" -#include "cru/platform/painter_util.hpp" +#include "cru/platform/graph/painter_util.hpp" #include <algorithm> #include <cassert> #include <functional> namespace cru::ui::render { -using namespace platform; - FlexChildLayoutData* FlexLayoutRenderObject::GetChildLayoutData(int position) { assert(position >= 0 && position < child_layout_data_.size()); // Position out of bound. @@ -17,11 +15,12 @@ FlexChildLayoutData* FlexLayoutRenderObject::GetChildLayoutData(int position) { return &child_layout_data_[position]; } -void FlexLayoutRenderObject::Draw(platform::Painter* painter) { +void FlexLayoutRenderObject::Draw(platform::graph::Painter* painter) { for (const auto child : GetChildren()) { auto offset = child->GetOffset(); - util::WithTransform(painter, Matrix::Translation(offset.x, offset.y), - [child](auto p) { child->Draw(p); }); + platform::graph::util::WithTransform( + painter, platform::Matrix::Translation(offset.x, offset.y), + [child](auto p) { child->Draw(p); }); } } diff --git a/src/ui/render/render_object.cpp b/src/ui/render/render_object.cpp index 111128b2..3cb7e4df 100644 --- a/src/ui/render/render_object.cpp +++ b/src/ui/render/render_object.cpp @@ -54,13 +54,13 @@ void RenderObject::OnMeasureCore(const Size& available_size) { auto coerced_margin_padding_size = margin_padding_size; if (coerced_margin_padding_size.width > available_size.width) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Measure: horizontal length of padding and margin is bigger than " L"available length."); coerced_margin_padding_size.width = available_size.width; } if (coerced_margin_padding_size.height > available_size.height) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Measure: vertical length of padding and margin is bigger than " L"available length."); coerced_margin_padding_size.height = available_size.height; @@ -82,13 +82,13 @@ void RenderObject::OnLayoutCore(const Rect& rect) { auto coerced_content_available_size = content_available_size; if (coerced_content_available_size.width < 0) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Layout: horizontal length of padding and margin is bigger than " L"available length."); coerced_content_available_size.width = 0; } if (coerced_content_available_size.height < 0) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Layout: vertical length of padding and margin is bigger than " L"available length."); coerced_content_available_size.height = 0; diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp index bdf48c9a..c886ee7a 100644 --- a/src/ui/render/text_render_object.cpp +++ b/src/ui/render/text_render_object.cpp @@ -1,18 +1,17 @@ #include "cru/ui/render/text_render_object.hpp" -#include "cru/platform/graph_factory.hpp" -#include "cru/platform/painter_util.hpp" -#include "cru/platform/text_layout.hpp" -#include "cru/platform/ui_applicaition.hpp" +#include "cru/platform/graph/graph_factory.hpp" +#include "cru/platform/graph/painter_util.hpp" +#include "cru/platform/graph/text_layout.hpp" #include <algorithm> #include <cassert> namespace cru::ui::render { TextRenderObject::TextRenderObject( - std::shared_ptr<platform::Brush> brush, - std::shared_ptr<platform::FontDescriptor> font, - std::shared_ptr<platform::Brush> selection_brush) { + std::shared_ptr<platform::graph::Brush> brush, + std::shared_ptr<platform::graph::FontDescriptor> font, + std::shared_ptr<platform::graph::Brush> selection_brush) { assert(brush); assert(font); assert(selection_brush); @@ -21,8 +20,7 @@ TextRenderObject::TextRenderObject( font.swap(font_); selection_brush.swap(selection_brush_); - const auto graph_factory = - platform::UiApplication::GetInstance()->GetGraphFactory(); + const auto graph_factory = platform::graph::GraphFactory::GetInstance(); text_layout_.reset(graph_factory->CreateTextLayout(font_, L"")); } @@ -35,20 +33,22 @@ void TextRenderObject::SetText(std::wstring new_text) { text_layout_->SetText(std::move(new_text)); } -std::shared_ptr<platform::FontDescriptor> TextRenderObject::GetFont() const { +std::shared_ptr<platform::graph::FontDescriptor> TextRenderObject::GetFont() + const { return text_layout_->GetFont(); } -void TextRenderObject::SetFont(std::shared_ptr<platform::FontDescriptor> font) { +void TextRenderObject::SetFont( + std::shared_ptr<platform::graph::FontDescriptor> font) { text_layout_->SetFont(std::move(font)); } -void TextRenderObject::Draw(platform::Painter* painter) { - platform::util::WithTransform( +void TextRenderObject::Draw(platform::graph::Painter* painter) { + platform::graph::util::WithTransform( painter, platform::Matrix::Translation(GetMargin().left + GetPadding().left, GetMargin().top + GetPadding().top), - [this](platform::Painter* p) { + [this](platform::graph::Painter* p) { if (this->selection_range_.has_value()) { const auto&& rects = text_layout_->TextRangeRect(this->selection_range_.value()); diff --git a/src/ui/render/window_render_object.cpp b/src/ui/render/window_render_object.cpp index 781087aa..4fc57ad1 100644 --- a/src/ui/render/window_render_object.cpp +++ b/src/ui/render/window_render_object.cpp @@ -1,7 +1,7 @@ #include "cru/ui/render/window_render_object.hpp" -#include "cru/platform/native_window.hpp" -#include "cru/platform/painter_util.hpp" +#include "cru/platform/graph/painter_util.hpp" +#include "cru/platform/native/native_window.hpp" #include "cru/ui/window.hpp" #include <cassert> @@ -13,11 +13,11 @@ void WindowRenderObject::MeasureAndLayout() { Layout(Rect{Point{}, client_size}); } -void WindowRenderObject::Draw(platform::Painter* painter) { +void WindowRenderObject::Draw(platform::graph::Painter* painter) { painter->Clear(colors::white); if (const auto child = GetChild()) { auto offset = child->GetOffset(); - platform::util::WithTransform( + platform::graph::util::WithTransform( painter, platform::Matrix::Translation(offset.x, offset.y), [child](auto rt) { child->Draw(rt); }); } diff --git a/src/ui/ui_manager.cpp b/src/ui/ui_manager.cpp index b1132ea8..2ac1c8e7 100644 --- a/src/ui/ui_manager.cpp +++ b/src/ui/ui_manager.cpp @@ -1,20 +1,19 @@ #include "cru/ui/ui_manager.hpp" -#include "cru/platform/brush.hpp" -#include "cru/platform/font.hpp" -#include "cru/platform/graph_factory.hpp" -#include "cru/platform/ui_applicaition.hpp" +#include "cru/platform/graph/brush.hpp" +#include "cru/platform/graph/font.hpp" +#include "cru/platform/graph/graph_factory.hpp" +#include "cru/platform/native/ui_applicaition.hpp" namespace cru::ui { PredefineResources::PredefineResources() { - const auto graph_factory = - platform::UiApplication::GetInstance()->GetGraphFactory(); + const auto graph_factory = platform::graph::GraphFactory::GetInstance(); - button_normal_border_brush.reset(static_cast<platform::Brush*>( + button_normal_border_brush.reset(static_cast<platform::graph::Brush*>( graph_factory->CreateSolidColorBrush(colors::black))); - text_block_selection_brush.reset(static_cast<platform::Brush*>( + text_block_selection_brush.reset(static_cast<platform::graph::Brush*>( graph_factory->CreateSolidColorBrush(colors::skyblue))); - text_block_text_brush.reset(static_cast<platform::Brush*>( + text_block_text_brush.reset(static_cast<platform::graph::Brush*>( graph_factory->CreateSolidColorBrush(colors::black))); text_block_font.reset(graph_factory->CreateFontDescriptor(L"等线", 24.0f)); } diff --git a/src/ui/window.cpp b/src/ui/window.cpp index f909e0f9..347a7a49 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -1,8 +1,8 @@ #include "cru/ui/window.hpp" -#include "cru/platform/native_window.hpp" -#include "cru/platform/painter.hpp" -#include "cru/platform/ui_applicaition.hpp" +#include "cru/platform/graph/painter.hpp" +#include "cru/platform/native/native_window.hpp" +#include "cru/platform/native/ui_applicaition.hpp" #include "cru/ui/render/window_render_object.hpp" #include <cassert> @@ -102,7 +102,7 @@ Window::Window(tag_overlapped_constructor) { using namespace std::placeholders; native_window_ = - platform::UiApplication::GetInstance()->CreateWindow(nullptr); + platform::native::UiApplication::GetInstance()->CreateWindow(nullptr); render_object_.reset(new render::WindowRenderObject(this)); event_revoker_guard_.Add(native_window_->DestroyEvent()->AddHandler( @@ -124,7 +124,7 @@ Window::Window(tag_overlapped_constructor) { event_revoker_guard_.Add(native_window_->KeyDownEvent()->AddHandler( std::bind(&Window::OnNativeKeyDown, this, _1))); event_revoker_guard_.Add(native_window_->KeyUpEvent()->AddHandler( - std::bind(&Window::OnNativeKeyUp, this, _1))); + std::bind(&Window::OnNativeKeyUp, this, _1))); } Window::~Window() { @@ -168,7 +168,7 @@ void Window::OnNativeDestroy() { delete this; } void Window::OnNativePaint() { const auto painter = - std::unique_ptr<platform::Painter>(native_window_->BeginPaint()); + std::unique_ptr<platform::graph::Painter>(native_window_->BeginPaint()); render_object_->Draw(painter.get()); painter->EndDraw(); } @@ -202,13 +202,13 @@ void Window::OnNativeMouseMove(const Point& point) { point); } -void Window::OnNativeMouseDown(platform::MouseButton button, +void Window::OnNativeMouseDown(platform::native::MouseButton button, const Point& point) { Control* control = HitTest(point); DispatchEvent(control, &Control::MouseDownEvent, nullptr, point, button); } -void Window::OnNativeMouseUp(platform::MouseButton button, const Point& point) { +void Window::OnNativeMouseUp(platform::native::MouseButton button, const Point& point) { Control* control = HitTest(point); DispatchEvent(control, &Control::MouseUpEvent, nullptr, point, button); } diff --git a/src/win/CMakeLists.txt b/src/win/CMakeLists.txt new file mode 100644 index 00000000..1ca1f544 --- /dev/null +++ b/src/win/CMakeLists.txt @@ -0,0 +1,10 @@ +add_library(cru_win_base STATIC + debug.cpp + exception.cpp + string_util.cpp +) +target_compile_definitions(cru_win_base PUBLIC UNICODE _UNICODE) # use unicode +target_include_directories(cru_win_base PUBLIC ${PROJECT_SOURCE_DIR}/include .) + +add_subdirectory(graph) +add_subdirectory(native) diff --git a/src/platform_win/debug.cpp b/src/win/debug.cpp index f52d41bd..e15b1f6c 100644 --- a/src/platform_win/debug.cpp +++ b/src/win/debug.cpp @@ -1,10 +1,9 @@ -#include "cru/platform/win/win_pre_config.hpp" +#include "cru/win/win_pre_config.hpp" #include "cru/platform/debug.hpp" - -namespace cru::platform::debug { +namespace cru::platform { void DebugMessage(const std::wstring_view& message) { ::OutputDebugStringW(message.data()); } -} // namespace cru::debug +} // namespace cru::platform diff --git a/src/platform_win/exception.cpp b/src/win/exception.cpp index 3db88b8b..62305329 100644 --- a/src/platform_win/exception.cpp +++ b/src/win/exception.cpp @@ -1,8 +1,8 @@ -#include "cru/platform/win/exception.hpp" +#include "cru/win/exception.hpp" #include "cru/common/format.hpp" -namespace cru::platform::win { +namespace cru::win { using util::Format; inline std::string HResultMakeMessage(HRESULT h_result, @@ -19,12 +19,12 @@ inline std::string HResultMakeMessage(HRESULT h_result, } HResultError::HResultError(HRESULT h_result) - : runtime_error(HResultMakeMessage(h_result, nullptr)), + : PlatformException(HResultMakeMessage(h_result, nullptr)), h_result_(h_result) {} HResultError::HResultError(HRESULT h_result, const std::string_view& additional_message) - : runtime_error(HResultMakeMessage(h_result, &additional_message)), + : PlatformException(HResultMakeMessage(h_result, &additional_message)), h_result_(h_result) {} inline std::string Win32MakeMessage(DWORD error_code, @@ -40,11 +40,11 @@ inline std::string Win32MakeMessage(DWORD error_code, } Win32Error::Win32Error(DWORD error_code) - : runtime_error(Win32MakeMessage(error_code, nullptr)), + : PlatformException(Win32MakeMessage(error_code, nullptr)), error_code_(error_code) {} Win32Error::Win32Error(DWORD error_code, const std::string_view& additional_message) - : runtime_error(Win32MakeMessage(error_code, &additional_message)), + : PlatformException(Win32MakeMessage(error_code, &additional_message)), error_code_(error_code) {} -} // namespace cru::platform::win +} // namespace cru::win diff --git a/src/win/graph/CMakeLists.txt b/src/win/graph/CMakeLists.txt new file mode 100644 index 00000000..7781e09a --- /dev/null +++ b/src/win/graph/CMakeLists.txt @@ -0,0 +1,9 @@ +add_library(cru_win_graph STATIC + graph_manager.cpp + win_brush.cpp + win_font.cpp + win_geometry.cpp + win_graph_factory.cpp + win_painter.cpp + win_text_layout.cpp) +target_link_libraries(cru_win_graph PUBLIC D3D11 D2d1 DWrite cru_win_base) diff --git a/src/platform_win/graph_manager.cpp b/src/win/graph/graph_manager.cpp index 4961867b..a4306b1b 100644 --- a/src/platform_win/graph_manager.cpp +++ b/src/win/graph/graph_manager.cpp @@ -1,8 +1,13 @@ -#include "cru/platform/win/graph_manager.hpp" +#include "cru/win/graph/graph_manager.hpp" -#include "cru/platform/win/exception.hpp" +#include "cru/win/exception.hpp" + +namespace cru::win::graph { +GraphManager* GraphManager::GetInstance() { + static GraphManager instance; + return &instance; +} -namespace cru::platform::win { GraphManager::GraphManager() { UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; @@ -42,10 +47,13 @@ GraphManager::GraphManager() { // 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<IUnknown**>(dwrite_factory_.GetAddressOf()))); + ThrowIfFailed(DWriteCreateFactory( + DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), + reinterpret_cast<IUnknown**>(dwrite_factory_.GetAddressOf()))); + + ThrowIfFailed(dwrite_factory_->GetSystemFontCollection( + &dwrite_system_font_collection_)); - ThrowIfFailed(dwrite_factory_->GetSystemFontCollection(&dwrite_system_font_collection_)); + graph_factory_.reset(new WinGraphFactory(this)); } -} // namespace cru::graph +} // namespace cru::win::graph diff --git a/src/win/graph/win_brush.cpp b/src/win/graph/win_brush.cpp new file mode 100644 index 00000000..262c3302 --- /dev/null +++ b/src/win/graph/win_brush.cpp @@ -0,0 +1,23 @@ +#include "cru/win/graph/win_brush.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/d2d_util.hpp" +#include "cru/win/graph/graph_manager.hpp" + +#include <cassert> + +namespace cru::win::graph { +WinSolidColorBrush::WinSolidColorBrush(GraphManager* graph_manager, + const ui::Color& color) { + assert(graph_manager); + ThrowIfFailed(graph_manager->GetD2D1DeviceContext()->CreateSolidColorBrush( + util::Convert(color), &brush_)); +} + +ui::Color WinSolidColorBrush::GetColor() { + return util::Convert(brush_->GetColor()); +} +void WinSolidColorBrush::SetColor(const ui::Color& color) { + brush_->SetColor(util::Convert(color)); +} +} // namespace cru::win::graph diff --git a/src/platform_win/win_font.cpp b/src/win/graph/win_font.cpp index bca70b9f..0eb5e6b2 100644 --- a/src/platform_win/win_font.cpp +++ b/src/win/graph/win_font.cpp @@ -1,15 +1,17 @@ -#include "cru/platform/win/win_font.hpp" +#include "cru/win/graph/win_font.hpp" -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/graph_manager.hpp" +#include "cru/win/exception.hpp" +#include "cru/win/graph/graph_manager.hpp" #include <array> +#include <cassert> #include <utility> -namespace cru::platform::win { +namespace cru::win::graph { WinFontDescriptor::WinFontDescriptor(GraphManager* graph_manager, const std::wstring_view& font_family, float font_size) { + assert(graph_manager); std::array<wchar_t, LOCALE_NAME_MAX_LENGTH> buffer; if (!::GetUserDefaultLocaleName(buffer.data(), buffer.size())) throw Win32Error(::GetLastError(), "Failed to get locale."); @@ -20,6 +22,7 @@ WinFontDescriptor::WinFontDescriptor(GraphManager* graph_manager, buffer.data(), &text_format_)); ThrowIfFailed(text_format_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER)); - ThrowIfFailed(text_format_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER)); + ThrowIfFailed( + text_format_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER)); } -} // namespace cru::platform::win +} // namespace cru::win::graph diff --git a/src/platform_win/win_geometry.cpp b/src/win/graph/win_geometry.cpp index 1078700b..22c4b8a7 100644 --- a/src/platform_win/win_geometry.cpp +++ b/src/win/graph/win_geometry.cpp @@ -1,16 +1,17 @@ -#include "cru/platform/win/win_geometry.hpp" +#include "cru/win/graph/win_geometry.hpp" -#include "cru/platform/win/d2d_util.hpp" -#include "cru/platform/win/exception.hpp" +#include "cru/win/exception.hpp" +#include "cru/win/graph/d2d_util.hpp" +#include "cru/win/graph/graph_manager.hpp" #include <cassert> -namespace cru::platform::win { -WinGeometryBuilder::WinGeometryBuilder( - Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry) { - assert(geometry); - ThrowIfFailed(geometry->Open(&geometry_sink_)); - geometry_ = std::move(geometry); +namespace cru::win::graph { +WinGeometryBuilder::WinGeometryBuilder(GraphManager* graph_manager) { + assert(graph_manager); + ThrowIfFailed( + graph_manager->GetD2D1Factory()->CreatePathGeometry(&geometry_)); + ThrowIfFailed(geometry_->Open(&geometry_sink_)); } WinGeometryBuilder::~WinGeometryBuilder() { @@ -42,7 +43,7 @@ void WinGeometryBuilder::CloseFigure(bool close) { : D2D1_FIGURE_END_OPEN); } -Geometry* WinGeometryBuilder::Build() { +platform::graph::Geometry* WinGeometryBuilder::Build() { assert(IsValid()); ThrowIfFailed(geometry_sink_->Close()); geometry_sink_ = nullptr; @@ -62,4 +63,4 @@ bool WinGeometry::FillContains(const ui::Point& point) { util::Convert(point), D2D1::Matrix3x2F::Identity(), &result)); return result != 0; } -} // namespace cru::platform::win +} // namespace cru::win::graph diff --git a/src/win/graph/win_graph_factory.cpp b/src/win/graph/win_graph_factory.cpp new file mode 100644 index 00000000..5ab905b6 --- /dev/null +++ b/src/win/graph/win_graph_factory.cpp @@ -0,0 +1,46 @@ +#include "cru/win/graph/win_graph_factory.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/d2d_util.hpp" +#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/graph/win_brush.hpp" +#include "cru/win/graph/win_font.hpp" +#include "cru/win/graph/win_geometry.hpp" +#include "cru/win/graph/win_text_layout.hpp" + +#include <cassert> +#include <utility> + +namespace cru::platform::graph { +GraphFactory* GraphFactory::GetInstance() { + return win::graph::GraphManager::GetInstance()->GetGraphFactory(); +} +} + +namespace cru::win::graph { +WinGraphFactory::WinGraphFactory(GraphManager* graph_manager) { + assert(graph_manager); + graph_manager_ = graph_manager; +} + +platform::graph::SolidColorBrush* WinGraphFactory::CreateSolidColorBrush( + const ui::Color& color) { + return new WinSolidColorBrush(graph_manager_, color); +} + +platform::graph::GeometryBuilder* WinGraphFactory::CreateGeometryBuilder() { + return new WinGeometryBuilder(graph_manager_); +} + +platform::graph::FontDescriptor* WinGraphFactory::CreateFontDescriptor( + const std::wstring_view& font_family, float font_size) { + return new WinFontDescriptor(graph_manager_, font_family, font_size); +} + +platform::graph::TextLayout* WinGraphFactory::CreateTextLayout( + std::shared_ptr<platform::graph::FontDescriptor> font, std::wstring text) { + const auto f = std::dynamic_pointer_cast<WinFontDescriptor>(font); + assert(f); + return new WinTextLayout(graph_manager_, std::move(f), std::move(text)); +} +} // namespace cru::win::graph diff --git a/src/win/graph/win_painter.cpp b/src/win/graph/win_painter.cpp new file mode 100644 index 00000000..69ce1141 --- /dev/null +++ b/src/win/graph/win_painter.cpp @@ -0,0 +1,99 @@ +#include "cru/win/graph/win_painter.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/d2d_util.hpp" +#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/graph/win_brush.hpp" +#include "cru/win/graph/win_geometry.hpp" +#include "cru/win/graph/win_text_layout.hpp" + +#include <cassert> + +namespace cru::win::graph { +WinPainter::WinPainter(ID2D1RenderTarget* render_target) { + assert(render_target); + render_target_ = render_target; +} + +WinPainter::~WinPainter() { EndDraw(); } + +platform::Matrix WinPainter::GetTransform() { + assert(!IsDisposed()); + D2D1_MATRIX_3X2_F m; + render_target_->GetTransform(&m); + return util::Convert(m); +} + +void WinPainter::SetTransform(const platform::Matrix& matrix) { + assert(!IsDisposed()); + render_target_->SetTransform(util::Convert(matrix)); +} + +void WinPainter::Clear(const ui::Color& color) { + assert(!IsDisposed()); + render_target_->Clear(util::Convert(color)); +} + +void WinPainter::StrokeRectangle(const ui::Rect& rectangle, + platform::graph::Brush* brush, float width) { + assert(!IsDisposed()); + const auto b = dynamic_cast<WinBrush*>(brush); + assert(b); + render_target_->DrawRectangle(util::Convert(rectangle), b->GetD2DBrush(), + width); +} + +void WinPainter::FillRectangle(const ui::Rect& rectangle, + platform::graph::Brush* brush) { + assert(!IsDisposed()); + const auto b = dynamic_cast<WinBrush*>(brush); + assert(b); + render_target_->FillRectangle(util::Convert(rectangle), b->GetD2DBrush()); +} + +void WinPainter::StrokeGeometry(platform::graph::Geometry* geometry, + platform::graph::Brush* brush, float width) { + assert(!IsDisposed()); + const auto g = dynamic_cast<WinGeometry*>(geometry); + assert(g); + const auto b = dynamic_cast<WinBrush*>(brush); + assert(b); + + render_target_->DrawGeometry(g->GetNative(), b->GetD2DBrush(), width); +} + +void WinPainter::FillGeometry(platform::graph::Geometry* geometry, + platform::graph::Brush* brush) { + assert(!IsDisposed()); + const auto g = dynamic_cast<WinGeometry*>(geometry); + assert(g); + const auto b = dynamic_cast<WinBrush*>(brush); + assert(b); + + render_target_->FillGeometry(g->GetNative(), b->GetD2DBrush()); +} + +void WinPainter::DrawText(const ui::Point& offset, + platform::graph::TextLayout* text_layout, + platform::graph::Brush* brush) { + assert(!IsDisposed()); + const auto t = dynamic_cast<WinTextLayout*>(text_layout); + assert(t); + const auto b = dynamic_cast<WinBrush*>(brush); + assert(b); + + render_target_->DrawTextLayout(util::Convert(offset), + t->GetDWriteTextLayout(), b->GetD2DBrush()); +} + +void WinPainter::EndDraw() { + if (!IsDisposed()) { + DoEndDraw(); + } +} + +void WinPainter::DoEndDraw() { + ThrowIfFailed(render_target_->EndDraw()); + is_disposed_ = true; +} +} // namespace cru::win::graph diff --git a/src/platform_win/win_text_layout.cpp b/src/win/graph/win_text_layout.cpp index 04b5e928..0506320a 100644 --- a/src/platform_win/win_text_layout.cpp +++ b/src/win/graph/win_text_layout.cpp @@ -1,12 +1,13 @@ -#include "cru/platform/win/win_text_layout.hpp" +#include "cru/win/graph/win_text_layout.hpp" -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/graph_manager.hpp" +#include "cru/win/exception.hpp" +#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/graph/win_font.hpp" #include <cassert> #include <utility> -namespace cru::platform::win { +namespace cru::win::graph { WinTextLayout::WinTextLayout(GraphManager* graph_manager, std::shared_ptr<WinFontDescriptor> font, std::wstring text) { @@ -32,11 +33,12 @@ void WinTextLayout::SetText(std::wstring new_text) { &text_layout_)); } -std::shared_ptr<FontDescriptor> WinTextLayout::GetFont() { +std::shared_ptr<platform::graph::FontDescriptor> WinTextLayout::GetFont() { return font_descriptor_; } -void WinTextLayout::SetFont(std::shared_ptr<FontDescriptor> font) { +void WinTextLayout::SetFont( + std::shared_ptr<platform::graph::FontDescriptor> font) { auto f = std::dynamic_pointer_cast<WinFontDescriptor>(font); assert(f); f.swap(font_descriptor_); @@ -89,4 +91,4 @@ std::vector<ui::Rect> WinTextLayout::TextRangeRect( return result; } -} // namespace cru::platform::win +} // namespace cru::win::graph diff --git a/src/win/native/CMakeLists.txt b/src/win/native/CMakeLists.txt new file mode 100644 index 00000000..421f7515 --- /dev/null +++ b/src/win/native/CMakeLists.txt @@ -0,0 +1,10 @@ +add_library(cru_win_native STATIC + god_window.cpp + timer.cpp + win_application.cpp + win_native_window.cpp + window_class.cpp + window_manager.cpp + window_painter.cpp + window_render_target.cpp) +target_link_libraries(cru_win_native PUBLIC cru_win_graph) diff --git a/src/platform_win/dpi_util.hpp b/src/win/native/dpi_util.hpp index 92819e0f..d459e8c6 100644 --- a/src/platform_win/dpi_util.hpp +++ b/src/win/native/dpi_util.hpp @@ -1,10 +1,14 @@ #pragma once +#include "cru/platform/native/basic_types.hpp" + // The dpi awareness needs to be implemented in the future. Currently we use 96 // as default. -namespace cru::platform { -inline Dpi GetDpi() { return Dpi{96.0f, 96.0f}; } +namespace cru::win::native { +inline platform::native::Dpi GetDpi() { + return platform::native::Dpi{96.0f, 96.0f}; +} inline int DipToPixelInternal(const float dip, const float dpi) { return static_cast<int>(dip * dpi / 96.0f); @@ -29,4 +33,4 @@ inline float PixelToDipX(const int pixel_x) { inline float PixelToDipY(const int pixel_y) { return DipToPixelInternal(pixel_y, GetDpi().y); } -} // namespace cru::platform +} // namespace cru::win::native diff --git a/src/platform_win/god_window.cpp b/src/win/native/god_window.cpp index 0cb1a0e4..58fb663c 100644 --- a/src/platform_win/god_window.cpp +++ b/src/win/native/god_window.cpp @@ -1,12 +1,12 @@ -#include "cru/platform/win/god_window.hpp" +#include "cru/win/native/god_window.hpp" -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/win_application.hpp" -#include "cru/platform/win/window_class.hpp" +#include "cru/win/exception.hpp" +#include "cru/win/native/win_application.hpp" +#include "cru/win/native/window_class.hpp" #include "god_window_message.hpp" #include "timer.hpp" -namespace cru::platform::win { +namespace cru::win::native { constexpr auto god_window_class_name = L"GodWindowClass"; LRESULT CALLBACK GodWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, @@ -15,8 +15,8 @@ LRESULT CALLBACK GodWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, if (app) { LRESULT result; - const auto handled = - app->GetGodWindow()->HandleGodWindowMessage(hWnd, uMsg, wParam, lParam, &result); + const auto handled = app->GetGodWindow()->HandleGodWindowMessage( + hWnd, uMsg, wParam, lParam, &result); if (handled) return result; else @@ -37,7 +37,8 @@ GodWindow::GodWindow(WinApplication* application) { CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_MESSAGE, nullptr, h_instance, nullptr); - if (hwnd_ == nullptr) throw Win32Error(::GetLastError(), "Failed to create god window."); + if (hwnd_ == nullptr) + throw Win32Error(::GetLastError(), "Failed to create god window."); } GodWindow::~GodWindow() { ::DestroyWindow(hwnd_); } @@ -69,4 +70,4 @@ bool GodWindow::HandleGodWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, } return false; } -} // namespace cru::platform::win +} // namespace cru::win::native diff --git a/src/win/native/god_window_message.hpp b/src/win/native/god_window_message.hpp new file mode 100644 index 00000000..10268625 --- /dev/null +++ b/src/win/native/god_window_message.hpp @@ -0,0 +1,6 @@ +#pragma once +#include "cru/win/win_pre_config.hpp" + +namespace cru::win::native { +constexpr int invoke_later_message_id = WM_USER + 2000; +} diff --git a/src/platform_win/timer.cpp b/src/win/native/timer.cpp index 280d1aed..9e3bbde6 100644 --- a/src/platform_win/timer.cpp +++ b/src/win/native/timer.cpp @@ -1,6 +1,6 @@ #include "timer.hpp" -namespace cru::platform::win { +namespace cru::win::native { TimerManager::TimerManager(GodWindow* god_window) { god_window_ = god_window; } UINT_PTR TimerManager::CreateTimer(const UINT milliseconds, const bool loop, @@ -25,4 +25,4 @@ std::optional<std::pair<bool, TimerAction>> TimerManager::GetAction( if (find_result == map_.cend()) return std::nullopt; return find_result->second; } -} // namespace cru::platform::win +} // namespace cru::win::native diff --git a/src/platform_win/timer.hpp b/src/win/native/timer.hpp index 95468b8d..f30d9b91 100644 --- a/src/platform_win/timer.hpp +++ b/src/win/native/timer.hpp @@ -1,15 +1,15 @@ #pragma once -#include "cru/platform/win/win_pre_config.hpp" +#include "cru/win/win_pre_config.hpp" + +#include "cru/common/base.hpp" +#include "cru/win/native/god_window.hpp" #include <chrono> #include <functional> #include <map> #include <optional> -#include "cru/common/base.hpp" -#include "cru/platform/win/god_window.hpp" - -namespace cru::platform::win { +namespace cru::win::native { using TimerAction = std::function<void()>; class TimerManager : public Object { @@ -31,4 +31,4 @@ class TimerManager : public Object { std::map<UINT_PTR, std::pair<bool, TimerAction>> map_{}; UINT_PTR current_count_ = 0; }; -} // namespace cru::platform::win +} // namespace cru::win::native diff --git a/src/platform_win/win_application.cpp b/src/win/native/win_application.cpp index ce187136..72e12fa0 100644 --- a/src/platform_win/win_application.cpp +++ b/src/win/native/win_application.cpp @@ -1,10 +1,8 @@ -#include "cru/platform/win/win_application.hpp" +#include "cru/win/native/win_application.hpp" -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/god_window.hpp" -#include "cru/platform/win/graph_manager.hpp" -#include "cru/platform/win/win_graph_factory.hpp" -#include "cru/platform/win/win_native_window.hpp" +#include "cru/win/exception.hpp" +#include "cru/win/native/god_window.hpp" +#include "cru/win/native/win_native_window.hpp" #include "god_window_message.hpp" #include "timer.hpp" #include "window_manager.hpp" @@ -12,13 +10,13 @@ #include <VersionHelpers.h> #include <cassert> -namespace cru::platform { +namespace cru::platform::native { UiApplication* UiApplication::GetInstance() { - return win::WinApplication::GetInstance(); + return win::native::WinApplication::GetInstance(); } -} // namespace cru::platform +} // namespace cru::platform::native -namespace cru::platform::win { +namespace cru::win::native { WinApplication* WinApplication::instance_ = nullptr; WinApplication* WinApplication::GetInstance() { @@ -39,8 +37,6 @@ WinApplication::WinApplication(HINSTANCE h_instance) : h_instance_(h_instance) { god_window_ = std::make_shared<GodWindow>(this); timer_manager_ = std::make_shared<TimerManager>(god_window_.get()); window_manager_ = std::make_shared<WindowManager>(this); - graph_manager_ = std::make_shared<GraphManager>(); - graph_factory_ = std::make_shared<WinGraphFactory>(graph_manager_.get()); } WinApplication::~WinApplication() { instance_ = nullptr; } @@ -82,16 +78,17 @@ void WinApplication::CancelTimer(unsigned long id) { timer_manager_->KillTimer(static_cast<UINT_PTR>(id)); } -std::vector<NativeWindow*> WinApplication::GetAllWindow() { +std::vector<platform::native::NativeWindow*> WinApplication::GetAllWindow() { const auto&& windows = window_manager_->GetAllWindows(); - std::vector<NativeWindow*> result; + std::vector<platform::native::NativeWindow*> result; for (const auto w : windows) { - result.push_back(static_cast<NativeWindow*>(w)); + result.push_back(static_cast<platform::native::NativeWindow*>(w)); } return result; } -NativeWindow* WinApplication::CreateWindow(NativeWindow* parent) { +platform::native::NativeWindow* WinApplication::CreateWindow( + platform::native::NativeWindow* parent) { WinNativeWindow* p = nullptr; if (parent != nullptr) { p = dynamic_cast<WinNativeWindow*>(parent); @@ -100,6 +97,4 @@ NativeWindow* WinApplication::CreateWindow(NativeWindow* parent) { return new WinNativeWindow(this, window_manager_->GetGeneralWindowClass(), WS_OVERLAPPEDWINDOW, p); } - -GraphFactory* WinApplication::GetGraphFactory() { return graph_factory_.get(); } -} // namespace cru::platform::win +} // namespace cru::win::native diff --git a/src/platform_win/win_native_window.cpp b/src/win/native/win_native_window.cpp index 3f34717f..0afad4e6 100644 --- a/src/platform_win/win_native_window.cpp +++ b/src/win/native/win_native_window.cpp @@ -1,17 +1,18 @@ -#include "cru/platform/win/win_native_window.hpp" +#include "cru/win/native/win_native_window.hpp" -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/win_application.hpp" -#include "cru/platform/win/win_painter.hpp" -#include "cru/platform/win/window_class.hpp" -#include "cru/platform/win/window_render_target.hpp" +#include "cru/win/exception.hpp" +#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/native/win_application.hpp" +#include "cru/win/native/window_class.hpp" +#include "cru/win/native/window_render_target.hpp" #include "dpi_util.hpp" #include "window_manager.hpp" +#include "window_painter.hpp" #include <assert.h> #include <windowsx.h> -namespace cru::platform::win { +namespace cru::win::native { WinNativeWindow::WinNativeWindow(WinApplication* application, std::shared_ptr<WindowClass> window_class, DWORD window_style, WinNativeWindow* parent) { @@ -36,7 +37,7 @@ WinNativeWindow::WinNativeWindow(WinApplication* application, window_manager->RegisterWindow(hwnd_, this); window_render_target_.reset( - new WindowRenderTarget(application->GetGraphManager(), hwnd_)); + new WindowRenderTarget(graph::GraphManager::GetInstance(), hwnd_)); } WinNativeWindow::~WinNativeWindow() { @@ -116,8 +117,9 @@ void WinNativeWindow::SetWindowRect(const ui::Rect& rect) { } } -Painter* WinNativeWindow::BeginPaint() { - return new WinPainter(this); } +platform::graph::Painter* WinNativeWindow::BeginPaint() { + return new WindowPainter(this); +} bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param, @@ -158,7 +160,7 @@ bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg, POINT point; point.x = GET_X_LPARAM(l_param); point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(MouseButton::Left, point); + OnMouseDownInternal(platform::native::MouseButton::Left, point); *result = 0; return true; } @@ -166,7 +168,7 @@ bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg, POINT point; point.x = GET_X_LPARAM(l_param); point.y = GET_Y_LPARAM(l_param); - OnMouseUpInternal(MouseButton::Left, point); + OnMouseUpInternal(platform::native::MouseButton::Left, point); *result = 0; return true; } @@ -174,7 +176,7 @@ bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg, POINT point; point.x = GET_X_LPARAM(l_param); point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(MouseButton::Right, point); + OnMouseDownInternal(platform::native::MouseButton::Right, point); *result = 0; return true; } @@ -182,7 +184,7 @@ bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg, POINT point; point.x = GET_X_LPARAM(l_param); point.y = GET_Y_LPARAM(l_param); - OnMouseUpInternal(MouseButton::Right, point); + OnMouseUpInternal(platform::native::MouseButton::Right, point); *result = 0; return true; } @@ -190,7 +192,7 @@ bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg, POINT point; point.x = GET_X_LPARAM(l_param); point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(MouseButton::Middle, point); + OnMouseDownInternal(platform::native::MouseButton::Middle, point); *result = 0; return true; } @@ -198,7 +200,7 @@ bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg, POINT point; point.x = GET_X_LPARAM(l_param); point.y = GET_Y_LPARAM(l_param); - OnMouseUpInternal(MouseButton::Middle, point); + OnMouseUpInternal(platform::native::MouseButton::Middle, point); *result = 0; return true; } @@ -308,12 +310,14 @@ void WinNativeWindow::OnMouseLeaveInternal() { mouse_enter_leave_event_.Raise(false); } -void WinNativeWindow::OnMouseDownInternal(MouseButton button, POINT point) { +void WinNativeWindow::OnMouseDownInternal(platform::native::MouseButton button, + POINT point) { const auto dip_point = PiToDip(point); mouse_down_event_.Raise(button, dip_point); } -void WinNativeWindow::OnMouseUpInternal(MouseButton button, POINT point) { +void WinNativeWindow::OnMouseUpInternal(platform::native::MouseButton button, + POINT point) { const auto dip_point = PiToDip(point); mouse_up_event_.Raise(button, dip_point); } @@ -333,4 +337,4 @@ void WinNativeWindow::OnCharInternal(wchar_t c) {} void WinNativeWindow::OnActivatedInternal() {} void WinNativeWindow::OnDeactivatedInternal() {} -} // namespace cru::platform::win +} // namespace cru::win::native diff --git a/src/platform_win/window_class.cpp b/src/win/native/window_class.cpp index b58f53b2..6c7d0d3d 100644 --- a/src/platform_win/window_class.cpp +++ b/src/win/native/window_class.cpp @@ -1,13 +1,13 @@ -#include "cru/platform/win/window_class.hpp" +#include "cru/win/native/window_class.hpp" -#include "cru/platform/win/exception.hpp" +#include "cru/win/exception.hpp" -namespace cru::platform::win { +namespace cru::win::native { WindowClass::WindowClass(const std::wstring& name, WNDPROC window_proc, HINSTANCE h_instance) : name_(name) { - WNDCLASSEX window_class; - window_class.cbSize = sizeof(WNDCLASSEX); + WNDCLASSEXW window_class; + window_class.cbSize = sizeof(WNDCLASSEXW); window_class.style = CS_HREDRAW | CS_VREDRAW; window_class.lpfnWndProc = window_proc; @@ -25,4 +25,4 @@ WindowClass::WindowClass(const std::wstring& name, WNDPROC window_proc, if (atom_ == 0) throw Win32Error(::GetLastError(), "Failed to create window class."); } -} // namespace cru::ui +} // namespace cru::win::native diff --git a/src/platform_win/window_manager.cpp b/src/win/native/window_manager.cpp index e6c255c3..a2fcdb54 100644 --- a/src/platform_win/window_manager.cpp +++ b/src/win/native/window_manager.cpp @@ -1,12 +1,12 @@ #include "window_manager.hpp" -#include "cru/platform/win/win_application.hpp" -#include "cru/platform/win/win_native_window.hpp" -#include "cru/platform/win/window_class.hpp" +#include "cru/win/native/win_application.hpp" +#include "cru/win/native/win_native_window.hpp" +#include "cru/win/native/window_class.hpp" #include <assert.h> -namespace cru::platform::win { +namespace cru::win::native { LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { auto window = @@ -51,4 +51,4 @@ std::vector<WinNativeWindow*> WindowManager::GetAllWindows() const { for (auto [key, value] : window_map_) windows.push_back(value); return windows; } -} // namespace cru::platform::win +} // namespace cru::win::native diff --git a/src/platform_win/window_manager.hpp b/src/win/native/window_manager.hpp index 6e36ead1..fa5bbe9d 100644 --- a/src/platform_win/window_manager.hpp +++ b/src/win/native/window_manager.hpp @@ -1,5 +1,5 @@ #pragma once -#include "cru/platform/win/win_pre_config.hpp" +#include "cru/win/win_pre_config.hpp" #include "cru/common/base.hpp" @@ -7,7 +7,7 @@ #include <memory> #include <vector> -namespace cru::platform::win { +namespace cru::win::native { class WinApplication; class WinNativeWindow; class WindowClass; @@ -49,4 +49,4 @@ class WindowManager : public Object { std::map<HWND, WinNativeWindow*> window_map_; }; -} // namespace cru::platform::win +} // namespace cru::win::native diff --git a/src/win/native/window_painter.cpp b/src/win/native/window_painter.cpp new file mode 100644 index 00000000..46364cdd --- /dev/null +++ b/src/win/native/window_painter.cpp @@ -0,0 +1,24 @@ +#include "window_painter.hpp" + +#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/native/window_render_target.hpp" + +#include <cassert> + +namespace cru::win::native { +WindowPainter::WindowPainter(WinNativeWindow* window) + : WinPainter(window->GetWindowRenderTarget() + ->GetGraphManager() + ->GetD2D1DeviceContext()), + window_(window) { + window->GetWindowRenderTarget()->SetAsTarget(); + window->GetWindowRenderTarget() + ->GetGraphManager() + ->GetD2D1DeviceContext() + ->BeginDraw(); +} + +void WindowPainter::DoEndDraw() { + WinPainter::DoEndDraw(); + window_->GetWindowRenderTarget()->Present(); } +} // namespace cru::win::native diff --git a/src/win/native/window_painter.hpp b/src/win/native/window_painter.hpp new file mode 100644 index 00000000..0e6ab2cb --- /dev/null +++ b/src/win/native/window_painter.hpp @@ -0,0 +1,21 @@ +#pragma once +#include "cru/win/graph/win_painter.hpp" +#include "cru/win/native/win_native_window.hpp" + +namespace cru::win::native { +class WindowPainter : public graph::WinPainter { + public: + explicit WindowPainter(WinNativeWindow* window); + WindowPainter(const WindowPainter& other) = delete; + WindowPainter& operator=(const WindowPainter& other) = delete; + WindowPainter(WindowPainter&& other) = delete; + WindowPainter& operator=(WindowPainter&& other) = delete; + ~WindowPainter() override = default; + + protected: + void DoEndDraw() override; + + private: + WinNativeWindow* window_; +}; +} // namespace cru::win::native diff --git a/src/platform_win/window_render_target.cpp b/src/win/native/window_render_target.cpp index f4836d17..c49a920e 100644 --- a/src/platform_win/window_render_target.cpp +++ b/src/win/native/window_render_target.cpp @@ -1,13 +1,14 @@ -#include "cru/platform/win/window_render_target.hpp" +#include "cru/win/native/window_render_target.hpp" -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/graph_manager.hpp" +#include "cru/win/exception.hpp" +#include "cru/win/graph/graph_manager.hpp" #include "dpi_util.hpp" #include <cassert> -namespace cru::platform::win { -WindowRenderTarget::WindowRenderTarget(GraphManager* graph_manager, HWND hwnd) { +namespace cru::win::native { +WindowRenderTarget::WindowRenderTarget(graph::GraphManager* graph_manager, + HWND hwnd) { this->graph_manager_ = graph_manager; const auto d3d11_device = graph_manager->GetD3D11Device(); @@ -86,4 +87,4 @@ void WindowRenderTarget::CreateTargetBitmap() { graph_manager_->GetD2D1DeviceContext()->CreateBitmapFromDxgiSurface( dxgi_back_buffer.Get(), &bitmap_properties, &target_bitmap_)); } -} // namespace cru::platform::win +} // namespace cru::win::native diff --git a/src/platform_win/string_util.cpp b/src/win/string_util.cpp index 8ae069c0..dd513b12 100644 --- a/src/platform_win/string_util.cpp +++ b/src/win/string_util.cpp @@ -1,8 +1,8 @@ #include "cru/platform/string_util.hpp" -#include "cru/platform/win/exception.hpp" +#include "cru/win/exception.hpp" -namespace cru::platform::util { +namespace cru::platform { std::string ToUtf8String(const std::wstring_view& string) { if (string.empty()) return std::string(); @@ -17,4 +17,4 @@ std::string ToUtf8String(const std::wstring_view& string) { "Failed to convert wide string to UTF-8."); return result; } -} // namespace cru::platform::util +} // namespace cru::platform |