From b53527fbe50a953ad0e3225cc812eb76b8a1f82d Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 27 Jun 2019 17:02:58 +0800 Subject: ... --- 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 +-- 13 files changed, 257 insertions(+), 254 deletions(-) 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/cru/win') 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