diff options
Diffstat (limited to 'include/cru/win/native')
-rw-r--r-- | include/cru/win/native/god_window.hpp | 32 | ||||
-rw-r--r-- | include/cru/win/native/win_application.hpp | 59 | ||||
-rw-r--r-- | include/cru/win/native/win_native_window.hpp | 137 | ||||
-rw-r--r-- | include/cru/win/native/window_class.hpp | 27 | ||||
-rw-r--r-- | include/cru/win/native/window_native_message_event_args.hpp | 45 | ||||
-rw-r--r-- | include/cru/win/native/window_render_target.hpp | 45 |
6 files changed, 345 insertions, 0 deletions
diff --git a/include/cru/win/native/god_window.hpp b/include/cru/win/native/god_window.hpp new file mode 100644 index 00000000..9ac49858 --- /dev/null +++ b/include/cru/win/native/god_window.hpp @@ -0,0 +1,32 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/common/base.hpp" + +#include <memory> + +namespace cru::win::native { +class WinApplication; +class WindowClass; + +class GodWindow : public Object { + public: + explicit GodWindow(WinApplication* application); + GodWindow(const GodWindow& other) = delete; + GodWindow(GodWindow&& other) = delete; + GodWindow& operator=(const GodWindow& other) = delete; + GodWindow& operator=(GodWindow&& other) = delete; + ~GodWindow() override; + + HWND GetHandle() const { return hwnd_; } + + bool HandleGodWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, + LPARAM l_param, LRESULT* result); + + private: + WinApplication* application_; + + std::shared_ptr<WindowClass> god_window_class_; + HWND hwnd_; +}; +} // namespace cru::win::native
\ No newline at end of file diff --git a/include/cru/win/native/win_application.hpp b/include/cru/win/native/win_application.hpp new file mode 100644 index 00000000..458b10ae --- /dev/null +++ b/include/cru/win/native/win_application.hpp @@ -0,0 +1,59 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/platform/native/ui_applicaition.hpp" + +#include <memory> + +namespace cru::win::native { +class GodWindow; +class TimerManager; +class WindowManager; + +class WinApplication : public Object, + public virtual platform::native::UiApplication { + public: + static WinApplication* GetInstance(); + + private: + static WinApplication* instance_; + + 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 InvokeLater(const std::function<void()>& action) override; + unsigned long SetTimeout(std::chrono::milliseconds milliseconds, + const std::function<void()>& action) override; + unsigned long SetInterval(std::chrono::milliseconds milliseconds, + const std::function<void()>& action) override; + void CancelTimer(unsigned long id) 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(); } + + private: + HINSTANCE h_instance_; + + std::shared_ptr<GodWindow> god_window_; + std::shared_ptr<TimerManager> timer_manager_; + std::shared_ptr<WindowManager> window_manager_; +}; +} // namespace cru::win::native diff --git a/include/cru/win/native/win_native_window.hpp b/include/cru/win/native/win_native_window.hpp new file mode 100644 index 00000000..2b9be25d --- /dev/null +++ b/include/cru/win/native/win_native_window.hpp @@ -0,0 +1,137 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/platform/native/native_window.hpp" +#include "window_native_message_event_args.hpp" + +#include <memory> + +namespace cru::win::native { +class WinApplication; +class WindowClass; +class WindowManager; +class WindowRenderTarget; + +class WinNativeWindow : public Object, + public virtual platform::native::NativeWindow { + public: + WinNativeWindow(WinApplication* application, + std::shared_ptr<WindowClass> 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; + + NativeWindow* 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::Painter* BeginPaint() override; + + Event<>* DestroyEvent() override { return &destroy_event_; } + Event<const ui::Size&>* ResizeEvent() override { return &resize_event_; } + Event<>* PaintEvent() override { return &paint_event_; } + Event<bool>* FocusEvent() override { return &focus_event_; } + Event<bool>* MouseEnterLeaveEvent() override { + return &mouse_enter_leave_event_; + } + Event<const ui::Point&>* MouseMoveEvent() override { + return &mouse_move_event_; + } + Event<platform::native::MouseButton, const ui::Point&>* MouseDownEvent() + override { + return &mouse_down_event_; + } + Event<platform::native::MouseButton, const ui::Point&>* MouseUpEvent() + override { + return &mouse_up_event_; + } + Event<int>* KeyDownEvent() override { return &key_down_event_; } + Event<int>* KeyUpEvent() override { return &key_up_event_; } + + Event<WindowNativeMessageEventArgs&>* 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<WindowRenderTarget> window_render_target_; + + Event<> destroy_event_; + Event<const ui::Size&> resize_event_; + Event<> paint_event_; + Event<bool> focus_event_; + Event<bool> mouse_enter_leave_event_; + Event<const ui::Point&> mouse_move_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::win::native diff --git a/include/cru/win/native/window_class.hpp b/include/cru/win/native/window_class.hpp new file mode 100644 index 00000000..17712958 --- /dev/null +++ b/include/cru/win/native/window_class.hpp @@ -0,0 +1,27 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/common/base.hpp" + +#include <string> + +namespace cru::win::native { +class WindowClass : public Object { + public: + 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; + WindowClass& operator=(WindowClass&& other) = delete; + ~WindowClass() override = default; + + const wchar_t* GetName() const { return name_.c_str(); } + + ATOM GetAtom() const { return atom_; } + + private: + std::wstring name_; + ATOM atom_; +}; +} // namespace cru::win::native diff --git a/include/cru/win/native/window_native_message_event_args.hpp b/include/cru/win/native/window_native_message_event_args.hpp new file mode 100644 index 00000000..37149f36 --- /dev/null +++ b/include/cru/win/native/window_native_message_event_args.hpp @@ -0,0 +1,45 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/common/base.hpp" + +namespace cru::win::native { +struct WindowNativeMessage { + HWND hwnd; + UINT msg; + WPARAM w_param; + LPARAM l_param; +}; + +class WindowNativeMessageEventArgs : public Object { + public: + WindowNativeMessageEventArgs(const WindowNativeMessage& message) + : message_(message) {} + WindowNativeMessageEventArgs(const WindowNativeMessageEventArgs& other) = + default; + WindowNativeMessageEventArgs(WindowNativeMessageEventArgs&& other) = default; + WindowNativeMessageEventArgs& operator=( + const WindowNativeMessageEventArgs& other) = default; + WindowNativeMessageEventArgs& operator=( + WindowNativeMessageEventArgs&& other) = default; + ~WindowNativeMessageEventArgs() override = default; + + WindowNativeMessage GetWindowMessage() const { return message_; } + + LRESULT GetResult() const { return result_; } + void SetResult(LRESULT result) { result_ = result; } + + bool IsHandled() const { return handled_; } + void SetHandled(bool handled) { handled_ = handled; } + + void HandledAndSetResult(LRESULT result) { + handled_ = true; + result_ = result; + } + + private: + WindowNativeMessage message_; + LRESULT result_; + bool handled_ = false; +}; +} // namespace cru::win::native diff --git a/include/cru/win/native/window_render_target.hpp b/include/cru/win/native/window_render_target.hpp new file mode 100644 index 00000000..c55055c6 --- /dev/null +++ b/include/cru/win/native/window_render_target.hpp @@ -0,0 +1,45 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/common/base.hpp" + +namespace cru::win::graph { +class GraphManager; +} + +namespace cru::win::native { +// Represents a window render target. +class WindowRenderTarget : public Object { + public: + WindowRenderTarget(graph::GraphManager* graph_manager, HWND hwnd); + WindowRenderTarget(const WindowRenderTarget& other) = delete; + WindowRenderTarget(WindowRenderTarget&& other) = delete; + WindowRenderTarget& operator=(const WindowRenderTarget& other) = delete; + WindowRenderTarget& operator=(WindowRenderTarget&& other) = delete; + ~WindowRenderTarget() override = default; + + public: + // Get the graph manager that created the render target. + 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(); } + + // Resize the underlying buffer. + void ResizeBuffer(int width, int height); + + // Set this render target as the d2d device context's target. + void SetAsTarget(); + + // Present the data of the underlying buffer to the window. + void Present(); + + private: + void CreateTargetBitmap(); + + private: + graph::GraphManager* graph_manager_; + Microsoft::WRL::ComPtr<IDXGISwapChain1> dxgi_swap_chain_; + Microsoft::WRL::ComPtr<ID2D1Bitmap1> target_bitmap_; +}; +} // namespace cru::win::native |