From 4d650160a388b0192e04c454ba1a3de21b0d4cc3 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 4 Apr 2019 17:52:15 +0800 Subject: ... --- include/cru/common/event.hpp | 56 +++++++++++++++++++++----- include/cru/platform/native_window.hpp | 8 ++-- include/cru/platform/painter.hpp | 1 + include/cru/platform/win/win_native_window.hpp | 16 ++++---- include/cru/platform/win/win_painter.hpp | 1 + include/cru/ui/window.hpp | 23 +++++------ 6 files changed, 70 insertions(+), 35 deletions(-) (limited to 'include') diff --git a/include/cru/common/event.hpp b/include/cru/common/event.hpp index ce014fb8..52d75a7b 100644 --- a/include/cru/common/event.hpp +++ b/include/cru/common/event.hpp @@ -3,33 +3,65 @@ #include #include +#include #include namespace cru { +using EventHandlerRevoker = std::function; + // A non-copyable non-movable Event class. // It stores a list of event handlers. template class Event { + private: + using EventResolver = std::function; + class EventHandlerRevokerImpl { + public: + EventHandlerRevokerImpl(const std::shared_ptr& resolver, + long token) + : resolver_(resolver), token_(token) {} + EventHandlerRevokerImpl(const EventHandlerRevokerImpl& other) = default; + EventHandlerRevokerImpl(EventHandlerRevokerImpl&& other) = default; + EventHandlerRevokerImpl& operator=(EventHandlerRevokerImpl&& other) = + default; + EventHandlerRevokerImpl& operator=(EventHandlerRevokerImpl&& other) = + default; + ~EventHandlerRevokerImpl() = default; + + void operator()() { + const auto true_resolver = resolver_.lock(); + if (true_resolver) { + true_resolver()->RemoveHandler(token_); + } + } + + private: + std::weak_ptr resolver_; + long token_; + }; + public: using EventHandler = std::function; - using EventHandlerToken = long; - Event() = default; + Event() + : event_resolver_(new std::function([this] { return this; })) {} Event(const Event&) = delete; Event& operator=(const Event&) = delete; Event(Event&&) = delete; Event& operator=(Event&&) = delete; ~Event() = default; - EventHandlerToken AddHandler(const EventHandler& handler) { + EventHandlerRevoker AddHandler(const EventHandler& handler) { const auto token = current_token_++; handlers_.emplace(token, handler); - return token; + return EventHandlerRevoker(EventHandlerRevokerImpl(event_resolver_, token)); } - void RemoveHandler(const EventHandlerToken token) { - auto find_result = handlers_.find(token); - if (find_result != handlers_.cend()) handlers_.erase(find_result); + template + EventHandlerRevoker AddHandler(Args&& args...) { + const auto token = current_token_++; + handlers_.emplace(token, EventHandler(std::forward(args)...)); + return EventHandlerRevoker(EventHandlerRevokerImpl(event_resolver_, token)); } template @@ -39,8 +71,14 @@ class Event { } private: - std::map handlers_; + void RemoveHandler(const long token) { + auto find_result = handlers_.find(token); + if (find_result != handlers_.cend()) handlers_.erase(find_result); + } - EventHandlerToken current_token_ = 0; + private: + std::map handlers_{}; + long current_token_ = 0; + std::shared_ptr event_resolver_; }; } // namespace cru diff --git a/include/cru/platform/native_window.hpp b/include/cru/platform/native_window.hpp index 2daf4870..f68fd3a4 100644 --- a/include/cru/platform/native_window.hpp +++ b/include/cru/platform/native_window.hpp @@ -35,13 +35,13 @@ struct NativeWindow : public virtual Interface { virtual Painter* BeginPaint() = 0; virtual Event<>* DestroyEvent() = 0; - virtual Event* ResizeEvent() = 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* MouseMoveEvent() = 0; + virtual Event* MouseDownEvent() = 0; + virtual Event* MouseUpEvent() = 0; virtual Event* KeyDownEvent() = 0; virtual Event* KeyUpEvent() = 0; }; diff --git a/include/cru/platform/painter.hpp b/include/cru/platform/painter.hpp index 7310bc5c..eaaf61f9 100644 --- a/include/cru/platform/painter.hpp +++ b/include/cru/platform/painter.hpp @@ -12,6 +12,7 @@ struct TextLayout; struct Painter : virtual Interface { virtual Matrix GetTransform() = 0; virtual void SetTransform(const Matrix& matrix) = 0; + virtual void Clear(const ui::Color& color) = 0; virtual void StrokeRectangle(const ui::Rect& rectangle, Brush* brush, float width) = 0; virtual void FillRectangle(const ui::Rect& rectangle, Brush* brush) = 0; diff --git a/include/cru/platform/win/win_native_window.hpp b/include/cru/platform/win/win_native_window.hpp index ae19c9f3..7b93fd5c 100644 --- a/include/cru/platform/win/win_native_window.hpp +++ b/include/cru/platform/win/win_native_window.hpp @@ -47,17 +47,17 @@ class WinNativeWindow : public Object, public virtual NativeWindow { Painter* BeginPaint() override; Event<>* DestroyEvent() override { return &destroy_event_; } - Event* ResizeEvent() override { return &resize_event_; } + Event* ResizeEvent() override { return &resize_event_; } Event<>* PaintEvent() override { return &paint_event_; } Event* FocusEvent() override { return &focus_event_; } Event* MouseEnterLeaveEvent() override { return &mouse_enter_leave_event_; } - Event* MouseMoveEvent() override { return &mouse_move_event_; } - Event* MouseDownEvent() override { + Event* MouseMoveEvent() override { return &mouse_move_event_; } + Event* MouseDownEvent() override { return &mouse_down_event_; } - Event* MouseUpEvent() override { + Event* MouseUpEvent() override { return &mouse_up_event_; } Event* KeyDownEvent() override { return &key_down_event_; } @@ -117,13 +117,13 @@ class WinNativeWindow : public Object, public virtual NativeWindow { std::shared_ptr window_render_target_; Event<> destroy_event_; - Event resize_event_; + Event resize_event_; Event<> paint_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_; diff --git a/include/cru/platform/win/win_painter.hpp b/include/cru/platform/win/win_painter.hpp index dfb981d5..3c37ccb2 100644 --- a/include/cru/platform/win/win_painter.hpp +++ b/include/cru/platform/win/win_painter.hpp @@ -17,6 +17,7 @@ class WinPainter : public Object, public virtual Painter { 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; diff --git a/include/cru/ui/window.hpp b/include/cru/ui/window.hpp index 043aae35..f0c790be 100644 --- a/include/cru/ui/window.hpp +++ b/include/cru/ui/window.hpp @@ -60,23 +60,18 @@ class Window final : public ContentControl { void OnNativeDestroy(); void OnNativePaint(); - void OnNativeResize(float new_width, float new_height); + void OnNativeResize(const Size& size); - void OnSetFocusInternal(); - void OnKillFocusInternal(); + void OnNativeFocus(bool focus); - void OnMouseMoveInternal(POINT point); - void OnMouseLeaveInternal(); - void OnMouseDownInternal(MouseButton button, POINT point); - void OnMouseUpInternal(MouseButton button, POINT point); + void OnNativeMouseMove(const Point& point); + void OnNativeMouseLeave(); + void OnNativeMouseDown(platform::MouseButton button, const Point& point); + void OnNativeMouseUp(platform::MouseButton button, const Point& point); - void OnMouseWheelInternal(short delta, POINT point); - void OnKeyDownInternal(int virtual_code); - void OnKeyUpInternal(int virtual_code); - void OnCharInternal(wchar_t c); + void OnNativeKeyDown(int virtual_code); + void OnNativeKeyUp(int virtual_code); - void OnActivatedInternal(); - void OnDeactivatedInternal(); //*************** region: event dispatcher helper *************** @@ -86,7 +81,7 @@ class Window final : public ContentControl { private: platform::NativeWindow* native_window_; - std::vector revoke_tokens_; + std::vector event_revokers_; std::shared_ptr render_object_; -- cgit v1.2.3