From 5ec370e5f91ad8b5ed7717eb93f4e3240ea4e784 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 14 Jun 2019 00:44:14 +0800 Subject: ... --- include/cru/common/base.hpp | 5 +++ include/cru/common/event.hpp | 57 ++++++++++++++++++--------- include/cru/platform/native/native_event.hpp | 12 ++++++ include/cru/platform/native/native_window.hpp | 23 +++++------ include/cru/ui/window.hpp | 21 +++++----- include/cru/win/native/win_native_window.hpp | 36 ++++++++--------- 6 files changed, 97 insertions(+), 57 deletions(-) create mode 100644 include/cru/platform/native/native_event.hpp (limited to 'include') diff --git a/include/cru/common/base.hpp b/include/cru/common/base.hpp index 7dfe8240..d72b97f2 100644 --- a/include/cru/common/base.hpp +++ b/include/cru/common/base.hpp @@ -13,6 +13,11 @@ class Object { }; struct Interface { + Interface() = default; + Interface(const Interface& other) = delete; + Interface(Interface&& other) = delete; + Interface& operator=(const Interface& other) = delete; + Interface& operator=(Interface&& other) = delete; virtual ~Interface() = default; }; } // namespace cru diff --git a/include/cru/common/event.hpp b/include/cru/common/event.hpp index d8a24330..af282c30 100644 --- a/include/cru/common/event.hpp +++ b/include/cru/common/event.hpp @@ -79,13 +79,44 @@ inline EventRevoker details::EventBase::CreateRevoker(EventHandlerToken token) { return EventRevoker(resolver_, token); } +template +using DeducedEventArgs = std::conditional_t< + std::is_lvalue_reference_v, TRaw, + std::conditional_t, TRaw, const TRaw&>>; + +// Provides an interface of event. +// +// Note that this class does not inherit Interface because Interface is +// public destructable, but I want to make this class not public +// destructable to prevent user from destructing it. +// +// IEvent only allow to add handler but not to raise the event. You may +// want to create an Event object and expose IEvent only so users won't +// be able to emit the event. +template +struct IEvent { + public: + using EventArgs = DeducedEventArgs; + using EventHandler = std::function; + + protected: + IEvent() = default; + IEvent(const IEvent& other) = delete; + IEvent(IEvent&& other) = delete; + IEvent& operator=(const IEvent& other) = delete; + IEvent& operator=(IEvent&& other) = delete; + ~IEvent() = default; + + public: + virtual EventRevoker AddHandler(const EventHandler& handler) = 0; + virtual EventRevoker AddHandler(EventHandler&& handler) = 0; +}; + // A non-copyable non-movable Event class. // It stores a list of event handlers. -template -class Event : public details::EventBase { +template +class Event : public details::EventBase, public IEvent { public: - using EventHandler = std::function; - Event() = default; Event(const Event&) = delete; Event& operator=(const Event&) = delete; @@ -93,36 +124,26 @@ class Event : public details::EventBase { Event& operator=(Event&&) = delete; ~Event() = default; - EventRevoker AddHandler(const EventHandler& handler) { + EventRevoker AddHandler(const EventHandler& handler) override { const auto token = current_token_++; handlers_.emplace(token, handler); return CreateRevoker(token); } - EventRevoker AddHandler(EventHandler&& handler) { + EventRevoker AddHandler(EventHandler&& handler) override { const auto token = current_token_++; handlers_.emplace(token, std::move(handler)); return CreateRevoker(token); } - template - EventRevoker AddHandler(FArg&& handler) { - static_assert(std::is_invocable_v, - "Handler not invocable."); - const auto token = current_token_++; - handlers_.emplace(token, EventHandler(std::forward(handler))); - return CreateRevoker(token); - } - - template - void Raise(FArg&&... args) { + void Raise(EventArgs args) { // copy the handlers to a list, because the handler might be removed // during executing, and the handler with its data will be destroyed. // if the handler is a lambda with member data, then the member data // will be destroyed and result in seg fault. std::list handlers; for (const auto& [key, handler] : handlers_) handlers.push_back(handler); - for (const auto& handler : handlers) handler(std::forward(args)...); + for (const auto& handler : handlers) handler(args); } protected: diff --git a/include/cru/platform/native/native_event.hpp b/include/cru/platform/native/native_event.hpp new file mode 100644 index 00000000..21db5f90 --- /dev/null +++ b/include/cru/platform/native/native_event.hpp @@ -0,0 +1,12 @@ +#pragma once +#include "cru/common/base.hpp" + +#include "basic_types.hpp" +#include "cru/common/ui_base.hpp" + +namespace cru::platform::native { +struct NativeMouseButtonEventArgs { + MouseButton button; + ui::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 3a0fd3e1..3e83a531 100644 --- a/include/cru/platform/native/native_window.hpp +++ b/include/cru/platform/native/native_window.hpp @@ -4,6 +4,7 @@ #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; @@ -36,15 +37,15 @@ struct INativeWindow : public virtual Interface { virtual graph::IPainter* BeginPaint() = 0; - virtual Event<>* DestroyEvent() = 0; - virtual Event* ResizeEvent() = 0; - virtual Event<>* PaintEvent() = 0; - virtual Event* FocusEvent() = 0; - virtual Event* MouseEnterLeaveEvent() = 0; - virtual Event* MouseMoveEvent() = 0; - virtual Event* MouseDownEvent() = 0; - virtual Event* MouseUpEvent() = 0; - virtual Event* KeyDownEvent() = 0; - virtual Event* KeyUpEvent() = 0; + virtual IEvent* DestroyEvent() = 0; + virtual IEvent* PaintEvent() = 0; + virtual IEvent* ResizeEvent() = 0; + virtual IEvent* FocusEvent() = 0; + virtual IEvent* MouseEnterLeaveEvent() = 0; + virtual IEvent* MouseMoveEvent() = 0; + virtual IEvent* MouseDownEvent() = 0; + virtual IEvent* MouseUpEvent() = 0; + virtual IEvent* KeyDownEvent() = 0; + virtual IEvent* KeyUpEvent() = 0; }; -} // namespace cru::platform::ui +} // namespace cru::platform::native diff --git a/include/cru/ui/window.hpp b/include/cru/ui/window.hpp index 2ed05192..4bc0e41d 100644 --- a/include/cru/ui/window.hpp +++ b/include/cru/ui/window.hpp @@ -1,6 +1,7 @@ #pragma once #include "content_control.hpp" +#include "cru/platform/native/native_event.hpp" #include "event/ui_event.hpp" #include @@ -39,7 +40,9 @@ class Window final : public ContentControl { render::RenderObject* GetRenderObject() const override; - platform::native::INativeWindow* GetNativeWindow() const { return native_window_; } + platform::native::INativeWindow* GetNativeWindow() const { + return native_window_; + } Control* GetMouseHoverControl() const { return mouse_hover_control_; } @@ -59,18 +62,18 @@ class Window final : public ContentControl { //*************** region: native messages *************** - void OnNativeDestroy(); - void OnNativePaint(); + void OnNativeDestroy(std::nullptr_t); + void OnNativePaint(std::nullptr_t); void OnNativeResize(const Size& size); void OnNativeFocus(bool focus); void OnNativeMouseEnterLeave(bool enter); void OnNativeMouseMove(const Point& point); - void OnNativeMouseDown(platform::native::MouseButton button, - const Point& point); - void OnNativeMouseUp(platform::native::MouseButton button, - const Point& point); + void OnNativeMouseDown( + const platform::native::NativeMouseButtonEventArgs& args); + void OnNativeMouseUp( + const platform::native::NativeMouseButtonEventArgs& args); void OnNativeKeyDown(int virtual_code); void OnNativeKeyUp(int virtual_code); @@ -87,8 +90,8 @@ class Window final : public ContentControl { std::shared_ptr render_object_; - Control* mouse_hover_control_ = nullptr; + Control* mouse_hover_control_; - Control* focus_control_ = this; // "focus_control_" can't be nullptr + Control* focus_control_; // "focus_control_" can't be nullptr }; } // namespace cru::ui diff --git a/include/cru/win/native/win_native_window.hpp b/include/cru/win/native/win_native_window.hpp index 0fafc7fd..18de4f5d 100644 --- a/include/cru/win/native/win_native_window.hpp +++ b/include/cru/win/native/win_native_window.hpp @@ -47,28 +47,26 @@ class WinNativeWindow : public Object, platform::graph::IPainter* BeginPaint() override; - Event<>* DestroyEvent() override { return &destroy_event_; } - Event* ResizeEvent() override { return &resize_event_; } - Event<>* PaintEvent() override { return &paint_event_; } - Event* FocusEvent() override { return &focus_event_; } - Event* MouseEnterLeaveEvent() 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_; } - Event* MouseMoveEvent() override { - return &mouse_move_event_; - } - Event* MouseDownEvent() + IEvent* MouseMoveEvent() override { return &mouse_move_event_; } + IEvent* MouseDownEvent() override { return &mouse_down_event_; } - Event* MouseUpEvent() + IEvent* MouseUpEvent() override { return &mouse_up_event_; } - Event* KeyDownEvent() override { return &key_down_event_; } - Event* KeyUpEvent() override { return &key_up_event_; } + IEvent* KeyDownEvent() override { return &key_down_event_; } + IEvent* KeyUpEvent() override { return &key_up_event_; } - Event* NativeMessageEvent() { + IEvent* NativeMessageEvent() { return &native_message_event_; } @@ -121,14 +119,14 @@ class WinNativeWindow : public Object, std::shared_ptr window_render_target_; - Event<> destroy_event_; - Event resize_event_; - Event<> paint_event_; + 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 mouse_move_event_; + Event mouse_down_event_; + Event mouse_up_event_; Event key_down_event_; Event key_up_event_; -- cgit v1.2.3