From b53527fbe50a953ad0e3225cc812eb76b8a1f82d Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 27 Jun 2019 17:02:58 +0800 Subject: ... --- include/cru/platform/graph/graph_factory.hpp | 3 + include/cru/platform/graph/util/painter_util.hpp | 4 +- include/cru/platform/matrix.hpp | 4 +- include/cru/platform/native/native_event.hpp | 5 +- include/cru/platform/native/native_window.hpp | 37 ++++-- include/cru/platform/native/ui_applicaition.hpp | 32 +++-- include/cru/platform/native_resource.hpp | 5 +- include/cru/ui/base.hpp | 21 ++++ include/cru/ui/control.hpp | 1 + include/cru/ui/event/ui_event.hpp | 17 ++- include/cru/ui/render/border_render_object.hpp | 22 ++-- .../cru/ui/render/flex_layout_render_object.hpp | 2 +- include/cru/ui/render/render_object.hpp | 6 +- include/cru/ui/render/text_render_object.hpp | 34 ++--- include/cru/ui/render/window_render_object.hpp | 2 +- include/cru/ui/ui_manager.hpp | 17 +-- include/cru/ui/window.hpp | 6 +- include/cru/win/graph/direct/exception.hpp | 35 +----- include/cru/win/graph/direct/graph_factory.hpp | 2 +- include/cru/win/graph/direct/platform_id.hpp | 13 +- include/cru/win/native/exception.hpp | 6 + include/cru/win/native/god_window.hpp | 8 +- include/cru/win/native/native_window.hpp | 138 +++++++++++++++++++++ include/cru/win/native/platform_id.hpp | 19 +++ include/cru/win/native/ui_application.hpp | 69 +++++++++++ include/cru/win/native/win_application.hpp | 66 ---------- include/cru/win/native/win_native_window.hpp | 135 -------------------- include/cru/win/native/window_class.hpp | 2 +- .../native/window_native_message_event_args.hpp | 2 +- include/cru/win/native/window_render_target.hpp | 16 +-- 30 files changed, 392 insertions(+), 337 deletions(-) create mode 100644 include/cru/ui/base.hpp create mode 100644 include/cru/win/native/exception.hpp create mode 100644 include/cru/win/native/native_window.hpp create mode 100644 include/cru/win/native/platform_id.hpp create mode 100644 include/cru/win/native/ui_application.hpp delete mode 100644 include/cru/win/native/win_application.hpp delete mode 100644 include/cru/win/native/win_native_window.hpp (limited to 'include') diff --git a/include/cru/platform/graph/graph_factory.hpp b/include/cru/platform/graph/graph_factory.hpp index 69afc7b3..0b1034cc 100644 --- a/include/cru/platform/graph/graph_factory.hpp +++ b/include/cru/platform/graph/graph_factory.hpp @@ -55,5 +55,8 @@ class GraphFactory : public NativeResource { virtual TextLayout* CreateTextLayout(std::shared_ptr font, std::wstring text) = 0; + + virtual bool IsAutoDelete() const = 0; + virtual void SetAutoDelete(bool value) = 0; }; } // namespace cru::platform::graph diff --git a/include/cru/platform/graph/util/painter_util.hpp b/include/cru/platform/graph/util/painter_util.hpp index 71e125c3..7a655a34 100644 --- a/include/cru/platform/graph/util/painter_util.hpp +++ b/include/cru/platform/graph/util/painter_util.hpp @@ -6,9 +6,9 @@ namespace cru::platform::graph::util { template -inline void WithTransform(IPainter* painter, const Matrix& matrix, +inline void WithTransform(Painter* painter, const Matrix& matrix, const Fn& action) { - static_assert(std::is_invocable_v, + static_assert(std::is_invocable_v, "Action must can be be invoked with painter."); const auto old = painter->GetTransform(); painter->SetTransform(old * matrix); diff --git a/include/cru/platform/matrix.hpp b/include/cru/platform/matrix.hpp index b0165a88..cbb55c78 100644 --- a/include/cru/platform/matrix.hpp +++ b/include/cru/platform/matrix.hpp @@ -37,8 +37,8 @@ struct Matrix { return Product(*this, matrix); } - ui::Point TransformPoint(const ui::Point& point) const { - return ui::Point{point.x * m11 + point.y * m21 + m31, + Point TransformPoint(const Point& point) const { + return Point{point.x * m11 + point.y * m21 + m31, point.x * m12 + point.y * m22 + m32}; } diff --git a/include/cru/platform/native/native_event.hpp b/include/cru/platform/native/native_event.hpp index 21db5f90..54bab00c 100644 --- a/include/cru/platform/native/native_event.hpp +++ b/include/cru/platform/native/native_event.hpp @@ -1,12 +1,11 @@ #pragma once -#include "cru/common/base.hpp" +#include "../graphic_base.hpp" #include "basic_types.hpp" -#include "cru/common/ui_base.hpp" namespace cru::platform::native { struct NativeMouseButtonEventArgs { MouseButton button; - ui::Point point; + Point point; }; } // namespace cru::platform::native diff --git a/include/cru/platform/native/native_window.hpp b/include/cru/platform/native/native_window.hpp index bb97ef21..22a2dce0 100644 --- a/include/cru/platform/native/native_window.hpp +++ b/include/cru/platform/native/native_window.hpp @@ -1,13 +1,13 @@ #pragma once -#include "cru/common/base.hpp" +#include "../native_resource.hpp" +#include "../graphic_base.hpp" #include "basic_types.hpp" #include "cru/common/event.hpp" -#include "cru/common/ui_base.hpp" #include "native_event.hpp" namespace cru::platform::graph { -struct IPainter; +class Painter; } namespace cru::platform::native { @@ -19,7 +19,20 @@ namespace cru::platform::native { // Close or closed by the user, which leads to an invalid instance. You can // check the validity by IsValid. When you call perform native operations on the // invalid instance, there is no effect. -struct INativeWindow : public virtual Interface { +class NativeWindow : public NativeResource { + protected: + NativeWindow() = default; + + public: + NativeWindow(const NativeWindow& other) = delete; + NativeWindow& operator=(const NativeWindow& other) = delete; + + NativeWindow(NativeWindow&& other) = delete; + NativeWindow& operator=(NativeWindow&& other) = delete; + + ~NativeWindow() override = default; + + public: // Return if the window is still valid, that is, hasn't been closed or // destroyed. virtual bool IsValid() = 0; @@ -27,30 +40,30 @@ struct INativeWindow : public virtual Interface { virtual void Close() = 0; - virtual INativeWindow* GetParent() = 0; + virtual NativeWindow* GetParent() = 0; virtual bool IsVisible() = 0; virtual void SetVisible(bool is_visible) = 0; - virtual ui::Size GetClientSize() = 0; - virtual void SetClientSize(const ui::Size& size) = 0; + virtual Size GetClientSize() = 0; + virtual void SetClientSize(const Size& size) = 0; // Get the rect of the window containing frame. // The lefttop of the rect is relative to screen lefttop. - virtual ui::Rect GetWindowRect() = 0; + virtual Rect GetWindowRect() = 0; // Set the rect of the window containing frame. // The lefttop of the rect is relative to screen lefttop. - virtual void SetWindowRect(const ui::Rect& rect) = 0; + virtual void SetWindowRect(const Rect& rect) = 0; - virtual graph::IPainter* BeginPaint() = 0; + virtual graph::Painter* BeginPaint() = 0; virtual IEvent* DestroyEvent() = 0; virtual IEvent* PaintEvent() = 0; - virtual IEvent* ResizeEvent() = 0; + virtual IEvent* ResizeEvent() = 0; virtual IEvent* FocusEvent() = 0; virtual IEvent* MouseEnterLeaveEvent() = 0; - virtual IEvent* MouseMoveEvent() = 0; + virtual IEvent* MouseMoveEvent() = 0; virtual IEvent* MouseDownEvent() = 0; virtual IEvent* MouseUpEvent() = 0; virtual IEvent* KeyDownEvent() = 0; diff --git a/include/cru/platform/native/ui_applicaition.hpp b/include/cru/platform/native/ui_applicaition.hpp index 655b8ea0..aa8d98da 100644 --- a/include/cru/platform/native/ui_applicaition.hpp +++ b/include/cru/platform/native/ui_applicaition.hpp @@ -1,29 +1,41 @@ #pragma once -#include "cru/common/base.hpp" - -#include "cru/common/auto_delete.hpp" +#include "../native_resource.hpp" #include #include #include namespace cru::platform::native { -struct INativeWindow; +class NativeWindow; // The entry point of a ui application. // It will call IGraphFactory::CreateInstance during its creation // and set graph factory to be auto deleted. If you want to keep // the graph factory then you should manually set it to false after // creating the ui application. -struct IUiApplication : virtual Interface, virtual IAutoDelete { +class UiApplication : public NativeResource { + public: // Create a platform-specific instance and save it as the global instance. // Do not create the instance twice. Implements should assert for that. // After creating, get the instance by GetInstance. - static IUiApplication* CreateInstance(); + static UiApplication* CreateInstance(); // Get the global instance. If it is not created, then return nullptr. - static IUiApplication* GetInstance(); + static UiApplication* GetInstance(); + + protected: + UiApplication() = default; + + public: + UiApplication(const UiApplication& other) = delete; + UiApplication& operator=(const UiApplication& other) = delete; + + UiApplication(UiApplication&& other) = delete; + UiApplication& operator=(UiApplication&& other) = delete; + + ~UiApplication() override = default; + public: virtual int Run() = 0; virtual void Quit(int quite_code) = 0; @@ -36,7 +48,7 @@ struct IUiApplication : virtual Interface, virtual IAutoDelete { const std::function& action) = 0; virtual void CancelTimer(unsigned long id) = 0; - virtual std::vector GetAllWindow() = 0; - virtual INativeWindow* CreateWindow(INativeWindow* parent) = 0; + virtual std::vector GetAllWindow() = 0; + virtual NativeWindow* CreateWindow(NativeWindow* parent) = 0; }; -} // namespace cru::platform::ui +} // namespace cru::platform::native diff --git a/include/cru/platform/native_resource.hpp b/include/cru/platform/native_resource.hpp index 787c46a5..ec7f01b6 100644 --- a/include/cru/platform/native_resource.hpp +++ b/include/cru/platform/native_resource.hpp @@ -9,8 +9,9 @@ class NativeResource : public Object { NativeResource() = default; public: - NativeResource(NativeResource&& other) = delete; - NativeResource& operator=(NativeResource&& other) = delete; + NativeResource(const NativeResource& other) = delete; + NativeResource& operator=(const NativeResource& other) = delete; + NativeResource(NativeResource&& other) = delete; NativeResource& operator=(NativeResource&& other) = delete; diff --git a/include/cru/ui/base.hpp b/include/cru/ui/base.hpp new file mode 100644 index 00000000..427615b9 --- /dev/null +++ b/include/cru/ui/base.hpp @@ -0,0 +1,21 @@ +#pragma once +#include "cru/platform/graphic_base.hpp" +#include "cru/platform/matrix.hpp" +#include "cru/platform/native/basic_types.hpp" + +namespace cru::ui { +using cru::platform::Color; +using cru::platform::Ellipse; +using cru::platform::Matrix; +using cru::platform::Point; +using cru::platform::Rect; +using cru::platform::RoundedRect; +using cru::platform::Size; +using cru::platform::TextRange; +using cru::platform::Thickness; +using cru::platform::native::MouseButton; + +namespace colors { +using namespace cru::platform::colors; +} +} // namespace cru::ui \ No newline at end of file diff --git a/include/cru/ui/control.hpp b/include/cru/ui/control.hpp index 652efe89..15eeb6fa 100644 --- a/include/cru/ui/control.hpp +++ b/include/cru/ui/control.hpp @@ -1,5 +1,6 @@ #pragma once #include "cru/common/base.hpp" +#include "base.hpp" #include "cru/platform/native/basic_types.hpp" #include "event/ui_event.hpp" diff --git a/include/cru/ui/event/ui_event.hpp b/include/cru/ui/event/ui_event.hpp index 2704cc4f..62045808 100644 --- a/include/cru/ui/event/ui_event.hpp +++ b/include/cru/ui/event/ui_event.hpp @@ -1,14 +1,13 @@ #pragma once #include "cru/common/base.hpp" +#include "../base.hpp" #include "cru/common/event.hpp" -#include "cru/common/ui_base.hpp" -#include "cru/platform/native/basic_types.hpp" #include namespace cru::platform::graph { -struct IPainter; +class Painter; } namespace cru::ui { @@ -79,7 +78,7 @@ class MouseButtonEventArgs : public MouseEventArgs { public: MouseButtonEventArgs(Object* sender, Object* original_sender, const Point& point, - const platform::native::MouseButton button) + const MouseButton button) : MouseEventArgs(sender, original_sender, point), button_(button) {} MouseButtonEventArgs(const MouseButtonEventArgs& other) = default; MouseButtonEventArgs(MouseButtonEventArgs&& other) = default; @@ -87,10 +86,10 @@ class MouseButtonEventArgs : public MouseEventArgs { MouseButtonEventArgs& operator=(MouseButtonEventArgs&& other) = default; ~MouseButtonEventArgs() override = default; - platform::native::MouseButton GetMouseButton() const { return button_; } + MouseButton GetMouseButton() const { return button_; } private: - platform::native::MouseButton button_; + MouseButton button_; }; class MouseWheelEventArgs : public MouseEventArgs { @@ -113,7 +112,7 @@ class MouseWheelEventArgs : public MouseEventArgs { class PaintEventArgs : public UiEventArgs { public: PaintEventArgs(Object* sender, Object* original_sender, - platform::graph::IPainter* painter) + platform::graph::Painter* painter) : UiEventArgs(sender, original_sender), painter_(painter) {} PaintEventArgs(const PaintEventArgs& other) = default; PaintEventArgs(PaintEventArgs&& other) = default; @@ -121,10 +120,10 @@ class PaintEventArgs : public UiEventArgs { PaintEventArgs& operator=(PaintEventArgs&& other) = default; ~PaintEventArgs() = default; - platform::graph::IPainter* GetPainter() const { return painter_; } + platform::graph::Painter* GetPainter() const { return painter_; } private: - platform::graph::IPainter* painter_; + platform::graph::Painter* painter_; }; class FocusChangeEventArgs : public UiEventArgs { diff --git a/include/cru/ui/render/border_render_object.hpp b/include/cru/ui/render/border_render_object.hpp index 407edbb3..ab424e60 100644 --- a/include/cru/ui/render/border_render_object.hpp +++ b/include/cru/ui/render/border_render_object.hpp @@ -4,9 +4,9 @@ #include namespace cru::platform::graph { -struct IBrush; -struct IGeometry; -} // namespace cru::platform +class Brush; +class Geometry; +} // namespace cru::platform::graph namespace cru::ui::render { struct CornerRadius { @@ -32,7 +32,7 @@ struct CornerRadius { class BorderRenderObject : public RenderObject { public: - explicit BorderRenderObject(std::shared_ptr brush); + explicit BorderRenderObject(std::shared_ptr brush); BorderRenderObject(const BorderRenderObject& other) = delete; BorderRenderObject(BorderRenderObject&& other) = delete; BorderRenderObject& operator=(const BorderRenderObject& other) = delete; @@ -42,8 +42,10 @@ class BorderRenderObject : public RenderObject { bool IsEnabled() const { return is_enabled_; } void SetEnabled(bool enabled) { is_enabled_ = enabled; } - std::shared_ptr GetBrush() const { return border_brush_; } - void SetBrush(std::shared_ptr new_brush) { + std::shared_ptr GetBrush() const { + return border_brush_; + } + void SetBrush(std::shared_ptr new_brush) { border_brush_ = std::move(new_brush); } @@ -59,7 +61,7 @@ class BorderRenderObject : public RenderObject { void Refresh() { RecreateGeometry(); } - void Draw(platform::graph::IPainter* painter) override; + void Draw(platform::graph::Painter* painter) override; RenderObject* HitTest(const Point& point) override; @@ -83,11 +85,11 @@ class BorderRenderObject : public RenderObject { private: bool is_enabled_ = false; - std::shared_ptr border_brush_ = nullptr; + std::shared_ptr border_brush_ = nullptr; Thickness border_thickness_{}; CornerRadius corner_radius_{}; - std::shared_ptr geometry_ = nullptr; - std::shared_ptr border_outer_geometry_ = nullptr; + std::shared_ptr geometry_ = nullptr; + std::shared_ptr 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 8d881239..1234b920 100644 --- a/include/cru/ui/render/flex_layout_render_object.hpp +++ b/include/cru/ui/render/flex_layout_render_object.hpp @@ -64,7 +64,7 @@ class FlexLayoutRenderObject : public RenderObject { FlexChildLayoutData* GetChildLayoutData(int position); - void Draw(platform::graph::IPainter* 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 b6614317..dca254cc 100644 --- a/include/cru/ui/render/render_object.hpp +++ b/include/cru/ui/render/render_object.hpp @@ -1,7 +1,7 @@ #pragma once #include "cru/common/base.hpp" -#include "cru/common/ui_base.hpp" +#include "../base.hpp" #include @@ -11,7 +11,7 @@ class Control; } namespace cru::platform::graph { -struct IPainter; +class Painter; } namespace cru::ui::render { @@ -58,7 +58,7 @@ class RenderObject : public Object { void Measure(const Size& available_size); void Layout(const Rect& rect); - virtual void Draw(platform::graph::IPainter* painter) = 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 ecad6bb8..054d9b47 100644 --- a/include/cru/ui/render/text_render_object.hpp +++ b/include/cru/ui/render/text_render_object.hpp @@ -6,17 +6,17 @@ // forward declarations namespace cru::platform::graph { -struct IBrush; -struct IFontDescriptor; -struct ITextLayout; +class Brush; +class Font; +class TextLayout; } // namespace cru::platform::graph namespace cru::ui::render { class TextRenderObject : public RenderObject { public: - TextRenderObject(std::shared_ptr brush, - std::shared_ptr font, - std::shared_ptr selection_brush); + TextRenderObject(std::shared_ptr brush, + std::shared_ptr font, + std::shared_ptr 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 GetBrush() const { return brush_; } - void SetBrush(std::shared_ptr new_brush) { + std::shared_ptr GetBrush() const { return brush_; } + void SetBrush(std::shared_ptr new_brush) { new_brush.swap(brush_); } - std::shared_ptr GetFont() const; - void SetFont(std::shared_ptr font); + std::shared_ptr GetFont() const; + void SetFont(std::shared_ptr font); std::optional GetSelectionRange() const { return selection_range_; @@ -41,14 +41,14 @@ class TextRenderObject : public RenderObject { selection_range_ = std::move(new_range); } - std::shared_ptr GetSelectionBrush() const { + std::shared_ptr GetSelectionBrush() const { return selection_brush_; } - void SetSelectionBrush(std::shared_ptr new_brush) { + void SetSelectionBrush(std::shared_ptr new_brush) { new_brush.swap(selection_brush_); } - void Draw(platform::graph::IPainter* 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 brush_; - std::shared_ptr font_; - std::shared_ptr text_layout_; + std::shared_ptr brush_; + std::shared_ptr font_; + std::shared_ptr text_layout_; std::optional selection_range_ = std::nullopt; - std::shared_ptr selection_brush_; + std::shared_ptr 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 b95acbce..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::graph::IPainter* 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 f3f78722..d2b8ad53 100644 --- a/include/cru/ui/ui_manager.hpp +++ b/include/cru/ui/ui_manager.hpp @@ -1,15 +1,16 @@ #pragma once +#include "base.hpp" #include "cru/common/base.hpp" #include namespace cru::platform::graph { -struct IBrush; -struct IFontDescriptor; -} // namespace cru::platform +class Brush; +class Font; +} // namespace cru::platform::graph namespace cru::ui { -//TODO: Make this theme resource. +// TODO: Make this theme resource. class PredefineResources : public Object { public: PredefineResources(); @@ -20,12 +21,12 @@ class PredefineResources : public Object { ~PredefineResources() override = default; // region Button - std::shared_ptr button_normal_border_brush; + std::shared_ptr button_normal_border_brush; // region TextBlock - std::shared_ptr text_block_selection_brush; - std::shared_ptr text_block_text_brush; - std::shared_ptr text_block_font; + std::shared_ptr text_block_selection_brush; + std::shared_ptr text_block_text_brush; + std::shared_ptr text_block_font; }; class UiManager : public Object { diff --git a/include/cru/ui/window.hpp b/include/cru/ui/window.hpp index e175b234..311f1487 100644 --- a/include/cru/ui/window.hpp +++ b/include/cru/ui/window.hpp @@ -9,7 +9,7 @@ #include namespace cru::platform::native { -struct INativeWindow; +class NativeWindow; } namespace cru::ui { @@ -41,7 +41,7 @@ class Window final : public ContentControl, public SelfResovable { render::RenderObject* GetRenderObject() const override; - platform::native::INativeWindow* GetNativeWindow() const { + platform::native::NativeWindow* GetNativeWindow() const { return native_window_; } @@ -91,7 +91,7 @@ class Window final : public ContentControl, public SelfResovable { const Point& point); private: - platform::native::INativeWindow* native_window_; + platform::native::NativeWindow* native_window_; std::vector event_revoker_guards_; std::shared_ptr render_object_; diff --git a/include/cru/win/graph/direct/exception.hpp b/include/cru/win/graph/direct/exception.hpp index bfa14aaf..8e955825 100644 --- a/include/cru/win/graph/direct/exception.hpp +++ b/include/cru/win/graph/direct/exception.hpp @@ -1,36 +1,7 @@ #pragma once -#include "../../win_pre_config.hpp" - -#include "cru/platform/exception.hpp" - -#include -#include +#include "../../exception.hpp" namespace cru::platform::graph::win::direct { - -class HResultError : public PlatformException { - public: - explicit HResultError(HRESULT h_result); - explicit HResultError(HRESULT h_result, - const std::string_view& additional_message); - HResultError(const HResultError& other) = default; - HResultError(HResultError&& other) = default; - HResultError& operator=(const HResultError& other) = default; - HResultError& operator=(HResultError&& other) = default; - ~HResultError() override = default; - - HRESULT GetHResult() const { return h_result_; } - - private: - HRESULT h_result_; -}; - -inline void ThrowIfFailed(const HRESULT h_result) { - if (FAILED(h_result)) throw HResultError(h_result); -} - -inline void ThrowIfFailed(const HRESULT h_result, - const std::string_view& message) { - if (FAILED(h_result)) throw HResultError(h_result, message); -} +using platform::win::HResultError; +using platform::win::ThrowIfFailed; } // namespace cru::platform::graph::win::direct \ No newline at end of file diff --git a/include/cru/win/graph/direct/graph_factory.hpp b/include/cru/win/graph/direct/graph_factory.hpp index 841dd104..fb26a7c5 100644 --- a/include/cru/win/graph/direct/graph_factory.hpp +++ b/include/cru/win/graph/direct/graph_factory.hpp @@ -10,7 +10,7 @@ #include "cru/platform/graph/graph_factory.hpp" namespace cru::platform::graph::win::direct { -class DirectGraphFactory : public GraphFactory, IDirectFactory { +class DirectGraphFactory : public GraphFactory, public IDirectFactory { friend GraphFactory* GraphFactory::CreateInstance(); public: diff --git a/include/cru/win/graph/direct/platform_id.hpp b/include/cru/win/graph/direct/platform_id.hpp index ff02eb27..edac38f1 100644 --- a/include/cru/win/graph/direct/platform_id.hpp +++ b/include/cru/win/graph/direct/platform_id.hpp @@ -1,19 +1,18 @@ #pragma once #include -#include #include namespace cru::platform::graph::win::direct { -constexpr std::wstring_view platform_id = L"Windows Direct"; +constexpr std::wstring_view win_direct_platform_id = L"Windows Direct"; -bool IsDirectResource(NativeResource* resource) { - return resource->GetPlatformId() == platform_id; +inline bool IsDirectResource(NativeResource* resource) { + return resource->GetPlatformId() == win_direct_platform_id; } } // namespace cru::platform::graph::win::direct -#define CRU_PLATFORMID_IMPLEMENT_DIRECT \ - std::wstring_view GetPlatformId() const override { \ - return ::cru::platform::graph::win::direct::platform_id; \ +#define CRU_PLATFORMID_IMPLEMENT_DIRECT \ + std::wstring_view GetPlatformId() const override { \ + return ::cru::platform::graph::win::direct::win_direct_platform_id; \ } diff --git a/include/cru/win/native/exception.hpp b/include/cru/win/native/exception.hpp new file mode 100644 index 00000000..637f021d --- /dev/null +++ b/include/cru/win/native/exception.hpp @@ -0,0 +1,6 @@ +#pragma once +#include "../exception.hpp" + +namespace cru::platform::native::win { +using platform::win::Win32Error; +} // namespace cru::platform::native::win diff --git a/include/cru/win/native/god_window.hpp b/include/cru/win/native/god_window.hpp index 9ac49858..9fd20caa 100644 --- a/include/cru/win/native/god_window.hpp +++ b/include/cru/win/native/god_window.hpp @@ -5,13 +5,13 @@ #include -namespace cru::win::native { -class WinApplication; +namespace cru::platform::native::win { +class WinUiApplication; class WindowClass; class GodWindow : public Object { public: - explicit GodWindow(WinApplication* application); + explicit GodWindow(WinUiApplication* application); GodWindow(const GodWindow& other) = delete; GodWindow(GodWindow&& other) = delete; GodWindow& operator=(const GodWindow& other) = delete; @@ -24,7 +24,7 @@ class GodWindow : public Object { LPARAM l_param, LRESULT* result); private: - WinApplication* application_; + WinUiApplication* application_; std::shared_ptr god_window_class_; HWND hwnd_; diff --git a/include/cru/win/native/native_window.hpp b/include/cru/win/native/native_window.hpp new file mode 100644 index 00000000..ed678591 --- /dev/null +++ b/include/cru/win/native/native_window.hpp @@ -0,0 +1,138 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/platform/native/native_window.hpp" +#include "platform_id.hpp" +#include "window_native_message_event_args.hpp" + +#include + +namespace cru::platform::native::win { +class WinUiApplication; +class WindowClass; +class WindowManager; +class WindowRenderTarget; + +class WinNativeWindow : public NativeWindow { + public: + WinNativeWindow(WinUiApplication* application, + std::shared_ptr window_class, DWORD window_style, + WinNativeWindow* parent); + WinNativeWindow(const WinNativeWindow& other) = delete; + WinNativeWindow(WinNativeWindow&& other) = delete; + WinNativeWindow& operator=(const WinNativeWindow& other) = delete; + WinNativeWindow& operator=(WinNativeWindow&& other) = delete; + ~WinNativeWindow() override; + + CRU_PLATFORMID_IMPLEMENT_WIN + + public: + bool IsValid() override; + void SetDeleteThisOnDestroy(bool value) override; + + void Close() override; + + WinNativeWindow* GetParent() override { return parent_window_; } + + bool IsVisible() override; + void SetVisible(bool is_visible) override; + + Size GetClientSize() override; + void SetClientSize(const Size& size) override; + + // Get the rect of the window containing frame. + // The lefttop of the rect is relative to screen lefttop. + Rect GetWindowRect() override; + + // Set the rect of the window containing frame. + // The lefttop of the rect is relative to screen lefttop. + void SetWindowRect(const Rect& rect) override; + + graph::Painter* BeginPaint() override; + + IEvent* DestroyEvent() override { return &destroy_event_; } + IEvent* PaintEvent() override { return &paint_event_; } + IEvent* ResizeEvent() override { return &resize_event_; } + IEvent* FocusEvent() override { return &focus_event_; } + IEvent* MouseEnterLeaveEvent() override { + return &mouse_enter_leave_event_; + } + IEvent* MouseMoveEvent() override { return &mouse_move_event_; } + IEvent* MouseDownEvent() + override { + return &mouse_down_event_; + } + IEvent* MouseUpEvent() + override { + return &mouse_up_event_; + } + IEvent* KeyDownEvent() override { return &key_down_event_; } + IEvent* KeyUpEvent() override { return &key_up_event_; } + + IEvent* NativeMessageEvent() { + return &native_message_event_; + } + + // Get the handle of the window. Return null if window is invalid. + HWND GetWindowHandle() const { return hwnd_; } + + bool HandleNativeWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, + LPARAM l_param, LRESULT* result); + + WindowRenderTarget* GetWindowRenderTarget() const { + return window_render_target_.get(); + } + + private: + // Get the client rect in pixel. + RECT GetClientRectPixel(); + + //*************** region: native messages *************** + + void OnDestroyInternal(); + void OnPaintInternal(); + void OnResizeInternal(int new_width, int new_height); + + void OnSetFocusInternal(); + void OnKillFocusInternal(); + + void OnMouseMoveInternal(POINT point); + void OnMouseLeaveInternal(); + 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); + void OnKeyUpInternal(int virtual_code); + void OnCharInternal(wchar_t c); + + void OnActivatedInternal(); + void OnDeactivatedInternal(); + + private: + WinUiApplication* application_; + + bool delete_this_on_destroy_ = true; + + HWND hwnd_; + WinNativeWindow* parent_window_; + + bool has_focus_ = false; + bool is_mouse_in_ = false; + + std::shared_ptr window_render_target_; + + Event destroy_event_; + Event paint_event_; + Event resize_event_; + Event focus_event_; + Event mouse_enter_leave_event_; + Event mouse_move_event_; + Event mouse_down_event_; + Event mouse_up_event_; + Event key_down_event_; + Event key_up_event_; + + Event native_message_event_; +}; +} // namespace cru::platform::native::win diff --git a/include/cru/win/native/platform_id.hpp b/include/cru/win/native/platform_id.hpp new file mode 100644 index 00000000..4b018d51 --- /dev/null +++ b/include/cru/win/native/platform_id.hpp @@ -0,0 +1,19 @@ +#pragma once +#include + +#include +#include + +namespace cru::platform::native::win { +constexpr std::wstring_view win_platform_id = L"Windows"; + +inline bool IsWindowsResource(NativeResource* resource) { + return resource->GetPlatformId() == win_platform_id; +} + +} // namespace cru::platform::native::win + +#define CRU_PLATFORMID_IMPLEMENT_WIN \ + std::wstring_view GetPlatformId() const override { \ + return ::cru::platform::native::win::win_platform_id; \ + } diff --git a/include/cru/win/native/ui_application.hpp b/include/cru/win/native/ui_application.hpp new file mode 100644 index 00000000..08a9c3ed --- /dev/null +++ b/include/cru/win/native/ui_application.hpp @@ -0,0 +1,69 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "platform_id.hpp" + +#include "cru/platform/native/ui_applicaition.hpp" + +#include + +namespace cru::platform::native::win { +class GodWindow; +class TimerManager; +class WindowManager; + +class WinUiApplication : public UiApplication { + friend UiApplication* UiApplication::CreateInstance(); + + public: + static WinUiApplication* GetInstance(); + + private: + explicit WinUiApplication(HINSTANCE h_instance); + + public: + WinUiApplication(const WinUiApplication&) = delete; + WinUiApplication(WinUiApplication&&) = delete; + WinUiApplication& operator=(const WinUiApplication&) = delete; + WinUiApplication& operator=(WinUiApplication&&) = delete; + ~WinUiApplication() override; + + CRU_PLATFORMID_IMPLEMENT_WIN + + public: + int Run() override; + void Quit(int quit_code) override; + + void AddOnQuitHandler(const std::function& handler) override; + + void InvokeLater(const std::function& action) override; + unsigned long SetTimeout(std::chrono::milliseconds milliseconds, + const std::function& action) override; + unsigned long SetInterval(std::chrono::milliseconds milliseconds, + const std::function& action) override; + void CancelTimer(unsigned long id) override; + + std::vector GetAllWindow() override; + NativeWindow* CreateWindow(NativeWindow* parent) override; + + bool IsAutoDelete() const { return auto_delete_; } + void SetAutoDelete(bool value) { auto_delete_ = value; } + + 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(); } + + private: + bool auto_delete_ = false; + + HINSTANCE h_instance_; + + std::shared_ptr god_window_; + std::shared_ptr timer_manager_; + std::shared_ptr window_manager_; + + std::vector> quit_handlers_; +}; +} // namespace cru::platform::native::win diff --git a/include/cru/win/native/win_application.hpp b/include/cru/win/native/win_application.hpp deleted file mode 100644 index d3e705e1..00000000 --- a/include/cru/win/native/win_application.hpp +++ /dev/null @@ -1,66 +0,0 @@ -#pragma once -#include "../win_pre_config.hpp" - -#include "cru/platform/native/ui_applicaition.hpp" - -#include - -namespace cru::win::native { -class GodWindow; -class TimerManager; -class WindowManager; - -class WinApplication : public Object, - public virtual platform::native::IUiApplication { - friend IUiApplication* IUiApplication::CreateInstance(); - public: - static WinApplication* GetInstance(); - - private: - explicit WinApplication(HINSTANCE h_instance); - - public: - WinApplication(const WinApplication&) = delete; - WinApplication(WinApplication&&) = delete; - WinApplication& operator=(const WinApplication&) = delete; - WinApplication& operator=(WinApplication&&) = delete; - ~WinApplication() override; - - public: - int Run() override; - void Quit(int quit_code) override; - - void AddOnQuitHandler(const std::function& handler) override; - - void InvokeLater(const std::function& action) override; - unsigned long SetTimeout(std::chrono::milliseconds milliseconds, - const std::function& action) override; - unsigned long SetInterval(std::chrono::milliseconds milliseconds, - const std::function& action) override; - void CancelTimer(unsigned long id) override; - - std::vector GetAllWindow() override; - platform::native::INativeWindow* CreateWindow( - platform::native::INativeWindow* parent) override; - - bool IsAutoDelete() const override { return auto_delete_; } - void SetAutoDelete(bool value) override { auto_delete_ = value; } - - 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(); } - - private: - bool auto_delete_ = false; - - HINSTANCE h_instance_; - - std::shared_ptr god_window_; - std::shared_ptr timer_manager_; - std::shared_ptr window_manager_; - - std::vector> quit_handlers_; -}; -} // namespace cru::win::native diff --git a/include/cru/win/native/win_native_window.hpp b/include/cru/win/native/win_native_window.hpp deleted file mode 100644 index 18de4f5d..00000000 --- a/include/cru/win/native/win_native_window.hpp +++ /dev/null @@ -1,135 +0,0 @@ -#pragma once -#include "../win_pre_config.hpp" - -#include "cru/platform/native/native_window.hpp" -#include "window_native_message_event_args.hpp" - -#include - -namespace cru::win::native { -class WinApplication; -class WindowClass; -class WindowManager; -class WindowRenderTarget; - -class WinNativeWindow : public Object, - public virtual platform::native::INativeWindow { - public: - WinNativeWindow(WinApplication* application, - std::shared_ptr window_class, DWORD window_style, - WinNativeWindow* parent); - WinNativeWindow(const WinNativeWindow& other) = delete; - WinNativeWindow(WinNativeWindow&& other) = delete; - WinNativeWindow& operator=(const WinNativeWindow& other) = delete; - WinNativeWindow& operator=(WinNativeWindow&& other) = delete; - ~WinNativeWindow() override; - - bool IsValid() override; - void SetDeleteThisOnDestroy(bool value) override; - - void Close() override; - - INativeWindow* GetParent() override { return parent_window_; } - - bool IsVisible() override; - void SetVisible(bool is_visible) override; - - ui::Size GetClientSize() override; - void SetClientSize(const ui::Size& size) override; - - // Get the rect of the window containing frame. - // The lefttop of the rect is relative to screen lefttop. - ui::Rect GetWindowRect() override; - - // Set the rect of the window containing frame. - // The lefttop of the rect is relative to screen lefttop. - void SetWindowRect(const ui::Rect& rect) override; - - platform::graph::IPainter* BeginPaint() override; - - IEvent* DestroyEvent() override { return &destroy_event_; } - IEvent* PaintEvent() override { return &paint_event_; } - IEvent* ResizeEvent() override { return &resize_event_; } - IEvent* FocusEvent() override { return &focus_event_; } - IEvent* MouseEnterLeaveEvent() override { - return &mouse_enter_leave_event_; - } - IEvent* MouseMoveEvent() override { return &mouse_move_event_; } - IEvent* MouseDownEvent() - override { - return &mouse_down_event_; - } - IEvent* MouseUpEvent() - override { - return &mouse_up_event_; - } - IEvent* KeyDownEvent() override { return &key_down_event_; } - IEvent* KeyUpEvent() override { return &key_up_event_; } - - IEvent* NativeMessageEvent() { - return &native_message_event_; - } - - // Get the handle of the window. Return null if window is invalid. - HWND GetWindowHandle() const { return hwnd_; } - - bool HandleNativeWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, - LPARAM l_param, LRESULT* result); - - WindowRenderTarget* GetWindowRenderTarget() const { - return window_render_target_.get(); - } - - private: - // Get the client rect in pixel. - RECT GetClientRectPixel(); - - //*************** region: native messages *************** - - void OnDestroyInternal(); - void OnPaintInternal(); - void OnResizeInternal(int new_width, int new_height); - - void OnSetFocusInternal(); - void OnKillFocusInternal(); - - void OnMouseMoveInternal(POINT point); - void OnMouseLeaveInternal(); - 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); - void OnKeyUpInternal(int virtual_code); - void OnCharInternal(wchar_t c); - - void OnActivatedInternal(); - void OnDeactivatedInternal(); - - private: - WinApplication* application_; - - bool delete_this_on_destroy_ = true; - - HWND hwnd_; - WinNativeWindow* parent_window_; - - bool has_focus_ = false; - bool is_mouse_in_ = false; - - std::shared_ptr window_render_target_; - - Event destroy_event_; - Event paint_event_; - Event resize_event_; - Event focus_event_; - Event mouse_enter_leave_event_; - Event mouse_move_event_; - Event mouse_down_event_; - Event mouse_up_event_; - Event key_down_event_; - Event key_up_event_; - - Event native_message_event_; -}; -} // namespace cru::win::native diff --git a/include/cru/win/native/window_class.hpp b/include/cru/win/native/window_class.hpp index 17712958..fec3b32e 100644 --- a/include/cru/win/native/window_class.hpp +++ b/include/cru/win/native/window_class.hpp @@ -5,7 +5,7 @@ #include -namespace cru::win::native { +namespace cru::platform::native::win { class WindowClass : public Object { public: WindowClass(const std::wstring& name, WNDPROC window_proc, diff --git a/include/cru/win/native/window_native_message_event_args.hpp b/include/cru/win/native/window_native_message_event_args.hpp index 37149f36..4cf744f2 100644 --- a/include/cru/win/native/window_native_message_event_args.hpp +++ b/include/cru/win/native/window_native_message_event_args.hpp @@ -3,7 +3,7 @@ #include "cru/common/base.hpp" -namespace cru::win::native { +namespace cru::platform::native::win { struct WindowNativeMessage { HWND hwnd; UINT msg; diff --git a/include/cru/win/native/window_render_target.hpp b/include/cru/win/native/window_render_target.hpp index 5ff8ec87..bde47f4f 100644 --- a/include/cru/win/native/window_render_target.hpp +++ b/include/cru/win/native/window_render_target.hpp @@ -3,15 +3,15 @@ #include "cru/common/base.hpp" -namespace cru::win::graph { -struct IWinNativeFactory; +namespace cru::platform::graph::win::direct { +struct IDirectFactory; } -namespace cru::win::native { +namespace cru::platform::native::win { // Represents a window render target. class WindowRenderTarget : public Object { public: - WindowRenderTarget(graph::IWinNativeFactory* factory, HWND hwnd); + WindowRenderTarget(graph::win::direct::IDirectFactory* factory, HWND hwnd); WindowRenderTarget(const WindowRenderTarget& other) = delete; WindowRenderTarget(WindowRenderTarget&& other) = delete; WindowRenderTarget& operator=(const WindowRenderTarget& other) = delete; @@ -19,7 +19,9 @@ class WindowRenderTarget : public Object { ~WindowRenderTarget() override = default; public: - graph::IWinNativeFactory* GetWinNativeFactory() const { return factory_; } + graph::win::direct::IDirectFactory* GetWinNativeFactory() const { + return factory_; + } // Get the target bitmap which can be set as the ID2D1DeviceContext's target. ID2D1Bitmap1* GetTargetBitmap() const { return target_bitmap_.Get(); } @@ -37,8 +39,8 @@ class WindowRenderTarget : public Object { void CreateTargetBitmap(); private: - graph::IWinNativeFactory* factory_; + graph::win::direct::IDirectFactory* factory_; Microsoft::WRL::ComPtr dxgi_swap_chain_; Microsoft::WRL::ComPtr target_bitmap_; }; -} // namespace cru::win::native +} // namespace cru::platform::native::win -- cgit v1.2.3