From 939fadcb9723510e3b1cda8bae59705aa1d05e0f Mon Sep 17 00:00:00 2001 From: Nomango Date: Sun, 22 Dec 2019 11:04:49 +0800 Subject: [PATCH] Update Events --- Doxyfile | 11 ++- src/kiwano-imgui/kiwano-imgui.h | 2 +- src/kiwano-physics/ContactEvent.cpp | 11 +-- src/kiwano-physics/ContactEvent.h | 24 +++--- src/kiwano/2d/DebugActor.cpp | 4 +- src/kiwano/2d/Layer.cpp | 14 ++-- src/kiwano/core/Event.cpp | 52 ++++--------- src/kiwano/core/Event.h | 109 ++++++++++++++++------------ src/kiwano/core/EventDispatcher.h | 20 ++++- src/kiwano/core/time.h | 4 +- src/kiwano/platform/FileSystem.h | 2 +- src/kiwano/ui/Button.cpp | 8 +- 12 files changed, 134 insertions(+), 127 deletions(-) diff --git a/Doxyfile b/Doxyfile index c9a4f675..561ccec7 100644 --- a/Doxyfile +++ b/Doxyfile @@ -42,7 +42,7 @@ FILE_PATTERNS = *.c \ *.hh \ *.hxx \ *.hpp \ - *.h++ \ + *.h++ RECURSIVE = YES EXCLUDE = src/3rd-party @@ -60,8 +60,13 @@ PREDEFINED = KGE_API= # preprocessor. # This tag requires that the tag SEARCH_INCLUDES is set to YES. -INCLUDE_PATH = -INCLUDE_FILE_PATTERNS = +SEARCH_INCLUDES = YES +INCLUDE_PATH = src/ +INCLUDE_FILE_PATTERNS = *.h \ + *.hh \ + *.hxx \ + *.hpp \ + *.h++ #--------------------------------------------------------------------------- # Configuration options related to the HTML output diff --git a/src/kiwano-imgui/kiwano-imgui.h b/src/kiwano-imgui/kiwano-imgui.h index 68899fd5..453a1a0c 100644 --- a/src/kiwano-imgui/kiwano-imgui.h +++ b/src/kiwano-imgui/kiwano-imgui.h @@ -24,4 +24,4 @@ #include // ImGui -#include <3rd-party/imgui/imgui.h> +#include diff --git a/src/kiwano-physics/ContactEvent.cpp b/src/kiwano-physics/ContactEvent.cpp index 02fa63c9..9bfbad99 100644 --- a/src/kiwano-physics/ContactEvent.cpp +++ b/src/kiwano-physics/ContactEvent.cpp @@ -22,17 +22,10 @@ namespace kiwano { - namespace physics { - namespace events - { - KGE_IMPLEMENT_EVENT_TYPE(ContactBegin, physics::ContactBeginEvent); - KGE_IMPLEMENT_EVENT_TYPE(ContactEnd, physics::ContactEndEvent); - } - ContactBeginEvent::ContactBeginEvent() - : Event(events::ContactBegin) + : Event(KGE_EVENT(ContactBeginEvent)) , body_a(nullptr) , body_b(nullptr) { @@ -47,7 +40,7 @@ namespace kiwano } ContactEndEvent::ContactEndEvent() - : Event(events::ContactEnd) + : Event(KGE_EVENT(ContactEndEvent)) , body_a(nullptr) , body_b(nullptr) { diff --git a/src/kiwano-physics/ContactEvent.h b/src/kiwano-physics/ContactEvent.h index d154993d..0010bb35 100644 --- a/src/kiwano-physics/ContactEvent.h +++ b/src/kiwano-physics/ContactEvent.h @@ -26,19 +26,6 @@ namespace kiwano { namespace physics { - namespace events - { - /** - * \addtogroup EventTypes - * @{ - */ - - KGE_DECLARE_EVENT_TYPE(ContactBegin); ///< 接触开始 - KGE_DECLARE_EVENT_TYPE(ContactEnd); ///< 接触结束 - - /** @} */ - } - /// \~chinese /// @brief 接触开始事件 class KGE_API ContactBeginEvent @@ -67,5 +54,16 @@ namespace kiwano ContactEndEvent(Contact const& contact); }; + /** + * \addtogroup EventTypes + * @{ + */ + + KGE_EVENT_BEGIN(PhysicEvents); + KGE_DEFINE_EVENT(ContactBegin, ContactBeginEvent); ///< 接触开始 @see kiwano::physics::ContactBeginEvent + KGE_DEFINE_EVENT(ContactEnd, ContactEndEvent); ///< 接触结束 @see kiwano::physics::ContactEndEvent + KGE_EVENT_END; + + /** @} */ } } diff --git a/src/kiwano/2d/DebugActor.cpp b/src/kiwano/2d/DebugActor.cpp index 07c41061..cbb1e3d7 100644 --- a/src/kiwano/2d/DebugActor.cpp +++ b/src/kiwano/2d/DebugActor.cpp @@ -66,8 +66,8 @@ namespace kiwano style.line_spacing = 20.f; debug_text_->SetStyle(style); - AddListener(events::MouseHover, [=](Event&) { SetOpacity(0.4f); }); - AddListener(events::MouseOut, [=](Event&) { SetOpacity(1.f); }); + AddListener(MouseEvents::Hover(), [=](Event&) { SetOpacity(0.4f); }); + AddListener(MouseEvents::Out(), [=](Event&) { SetOpacity(1.f); }); } DebugActor::~DebugActor() diff --git a/src/kiwano/2d/Layer.cpp b/src/kiwano/2d/Layer.cpp index 8b4a0640..abf5e776 100644 --- a/src/kiwano/2d/Layer.cpp +++ b/src/kiwano/2d/Layer.cpp @@ -29,14 +29,14 @@ namespace kiwano { auto handler = Closure(this, &Layer::HandleMessages); - AddListener(events::MouseDown, handler); - AddListener(events::MouseUp, handler); - AddListener(events::MouseMove, handler); - AddListener(events::MouseWheel, handler); + AddListener(MouseEvents::Down(), handler); + AddListener(MouseEvents::Up(), handler); + AddListener(MouseEvents::Move(), handler); + AddListener(MouseEvents::Wheel(), handler); - AddListener(events::KeyDown, handler); - AddListener(events::KeyUp, handler); - AddListener(events::KeyChar, handler); + AddListener(KeyEvents::Down(), handler); + AddListener(KeyEvents::Up(), handler); + AddListener(KeyEvents::Char(), handler); } Layer::~Layer() diff --git a/src/kiwano/core/Event.cpp b/src/kiwano/core/Event.cpp index f907a518..0bf249b6 100644 --- a/src/kiwano/core/Event.cpp +++ b/src/kiwano/core/Event.cpp @@ -2,27 +2,6 @@ namespace kiwano { - namespace events - { - KGE_IMPLEMENT_EVENT_TYPE(MouseMove, MouseMoveEvent); - KGE_IMPLEMENT_EVENT_TYPE(MouseDown, MouseDownEvent); - KGE_IMPLEMENT_EVENT_TYPE(MouseUp, MouseUpEvent); - KGE_IMPLEMENT_EVENT_TYPE(MouseWheel, MouseWheelEvent); - KGE_IMPLEMENT_EVENT_TYPE(MouseHover, MouseHoverEvent); - KGE_IMPLEMENT_EVENT_TYPE(MouseOut, MouseOutEvent); - KGE_IMPLEMENT_EVENT_TYPE(MouseClick, MouseClickEvent); - - KGE_IMPLEMENT_EVENT_TYPE(KeyDown, KeyDownEvent); - KGE_IMPLEMENT_EVENT_TYPE(KeyUp, KeyUpEvent); - KGE_IMPLEMENT_EVENT_TYPE(KeyChar, KeyCharEvent); - - KGE_IMPLEMENT_EVENT_TYPE(WindowMoved, WindowMovedEvent); - KGE_IMPLEMENT_EVENT_TYPE(WindowResized, WindowResizedEvent); - KGE_IMPLEMENT_EVENT_TYPE(WindowFocusChanged, WindowFocusChangedEvent); - KGE_IMPLEMENT_EVENT_TYPE(WindowTitleChanged, WindowTitleChangedEvent); - KGE_IMPLEMENT_EVENT_TYPE(WindowClosed, WindowClosedEvent); - } - Event::Event(EventType const& type) : type_(type) { @@ -42,91 +21,90 @@ namespace kiwano } MouseMoveEvent::MouseMoveEvent() - : MouseEvent(events::MouseMove) - , button(0) + : MouseEvent(KGE_EVENT(MouseMoveEvent)) { } MouseDownEvent::MouseDownEvent() - : MouseEvent(events::MouseDown) + : MouseEvent(KGE_EVENT(MouseDownEvent)) , button(0) { } MouseUpEvent::MouseUpEvent() - : MouseEvent(events::MouseUp) + : MouseEvent(KGE_EVENT(MouseUpEvent)) , button(0) { } MouseClickEvent::MouseClickEvent() - : MouseEvent(events::MouseClick) + : MouseEvent(KGE_EVENT(MouseClickEvent)) , button(0) { } MouseHoverEvent::MouseHoverEvent() - : MouseEvent(events::MouseHover) + : MouseEvent(KGE_EVENT(MouseHoverEvent)) { } MouseOutEvent::MouseOutEvent() - : MouseEvent(events::MouseOut) + : MouseEvent(KGE_EVENT(MouseOutEvent)) { } MouseWheelEvent::MouseWheelEvent() - : MouseEvent(events::MouseWheel) + : MouseEvent(KGE_EVENT(MouseWheelEvent)) , wheel(0.f) { } KeyDownEvent::KeyDownEvent() - : Event(events::KeyDown) + : Event(KGE_EVENT(KeyDownEvent)) , code(0) { } KeyUpEvent::KeyUpEvent() - : Event(events::KeyUp) + : Event(KGE_EVENT(KeyUpEvent)) , code(0) { } KeyCharEvent::KeyCharEvent() - : Event(events::KeyChar) + : Event(KGE_EVENT(KeyCharEvent)) , value() { } WindowMovedEvent::WindowMovedEvent() - : Event(events::WindowMoved) + : Event(KGE_EVENT(WindowMovedEvent)) , x(0) , y(0) { } WindowResizedEvent::WindowResizedEvent() - : Event(events::WindowResized) + : Event(KGE_EVENT(WindowResizedEvent)) , width(0) , height(0) { } WindowFocusChangedEvent::WindowFocusChangedEvent() - : Event(events::WindowFocusChanged) + : Event(KGE_EVENT(WindowFocusChangedEvent)) , focus(false) { } WindowTitleChangedEvent::WindowTitleChangedEvent() - : Event(events::WindowTitleChanged) + : Event(KGE_EVENT(WindowTitleChangedEvent)) , title() { } WindowClosedEvent::WindowClosedEvent() - : Event(events::WindowClosed) + : Event(KGE_EVENT(WindowClosedEvent)) { } diff --git a/src/kiwano/core/Event.h b/src/kiwano/core/Event.h index 5a7d000e..c8388297 100644 --- a/src/kiwano/core/Event.h +++ b/src/kiwano/core/Event.h @@ -19,13 +19,12 @@ // THE SOFTWARE. #pragma once +#include +#include #include #include #include -#include -#include - namespace kiwano { class Actor; @@ -53,49 +52,8 @@ namespace kiwano EventType(const std::type_index& index) : std::type_index(index) {} }; +#define KGE_EVENT(EVENT_TYPE) ::kiwano::EventType(typeid(EVENT_TYPE)) -#define KGE_EVENT(EVENT_TYPE) ::kiwano::EventType(typeid(EVENT_TYPE)) - -#define KGE_DECLARE_EVENT_TYPE(EVENT_NAME) extern ::kiwano::EventType EVENT_NAME; - -#define KGE_IMPLEMENT_EVENT_TYPE(EVENT_NAME, EVENT_TYPE) ::kiwano::EventType EVENT_NAME = KGE_EVENT(EVENT_TYPE); - - - namespace events - { - /** - * \~chinese - * \defgroup EventTypes 事件类型 - */ - - /** - * \addtogroup EventTypes - * @{ - */ - - // 鼠标事件 - KGE_DECLARE_EVENT_TYPE(MouseMove); ///< 鼠标移动 - KGE_DECLARE_EVENT_TYPE(MouseDown); ///< 鼠标按下 - KGE_DECLARE_EVENT_TYPE(MouseUp); ///< 鼠标抬起 - KGE_DECLARE_EVENT_TYPE(MouseWheel); ///< 滚轮滚动 - KGE_DECLARE_EVENT_TYPE(MouseHover); ///< 鼠标移入 - KGE_DECLARE_EVENT_TYPE(MouseOut); ///< 鼠标移出 - KGE_DECLARE_EVENT_TYPE(MouseClick); ///< 鼠标点击 - - // 按键事件 - KGE_DECLARE_EVENT_TYPE(KeyDown); ///< 按键按下 - KGE_DECLARE_EVENT_TYPE(KeyUp); ///< 按键抬起 - KGE_DECLARE_EVENT_TYPE(KeyChar); ///< 输出字符 - - // 窗口消息 - KGE_DECLARE_EVENT_TYPE(WindowMoved); ///< 窗口移动 - KGE_DECLARE_EVENT_TYPE(WindowResized); ///< 窗口大小变化 - KGE_DECLARE_EVENT_TYPE(WindowFocusChanged); ///< 获得或失去焦点 - KGE_DECLARE_EVENT_TYPE(WindowTitleChanged); ///< 标题变化 - KGE_DECLARE_EVENT_TYPE(WindowClosed); ///< 窗口被关闭 - - /** @} */ - } /** * \~chinese @@ -166,6 +124,12 @@ namespace kiwano const EventType type_; }; + /// \~chinese + /// @brief 事件特性:判断是否是事件类型 + template + class IsEvent : public std::bool_constant::value || std::is_same::value> {}; + + /// \~chinese /// @brief 鼠标事件 class KGE_API MouseEvent @@ -186,8 +150,6 @@ namespace kiwano : public MouseEvent { public: - MouseButton::Value button; ///< 鼠标键值 - MouseMoveEvent(); }; @@ -343,4 +305,57 @@ namespace kiwano /** @} */ + + /** + * \~chinese + * \defgroup EventTypes 事件类型 + * + */ + + /** + * \addtogroup EventTypes + * @{ + */ + + +#define KGE_EVENT_BEGIN(NAME) struct NAME { +#define KGE_EVENT_END }; +#define KGE_DEFINE_EVENT(EVENT_NAME, EVENT_TYPE) \ + static inline const EventType& EVENT_NAME() \ + { \ + static EventType event_type = KGE_EVENT(EVENT_TYPE); \ + return event_type; \ + } + + /// \~chinese + /// @brief 鼠标事件 + KGE_EVENT_BEGIN(MouseEvents); + KGE_DEFINE_EVENT(Move, MouseMoveEvent); ///< 鼠标移动 + KGE_DEFINE_EVENT(Down, MouseDownEvent); ///< 鼠标按下 + KGE_DEFINE_EVENT(Up, MouseUpEvent); ///< 鼠标抬起 + KGE_DEFINE_EVENT(Wheel, MouseWheelEvent); ///< 滚轮滚动 + KGE_DEFINE_EVENT(Hover, MouseHoverEvent); ///< 鼠标移入 + KGE_DEFINE_EVENT(Out, MouseOutEvent); ///< 鼠标移出 + KGE_DEFINE_EVENT(Click, MouseClickEvent); ///< 鼠标点击 + KGE_EVENT_END; + + /// \~chinese + /// @brief 键盘按键事件 + KGE_EVENT_BEGIN(KeyEvents); + KGE_DEFINE_EVENT(Down, KeyDownEvent); ///< 按键按下 + KGE_DEFINE_EVENT(Up, KeyUpEvent); ///< 按键抬起 + KGE_DEFINE_EVENT(Char, KeyCharEvent); ///< 输出字符 + KGE_EVENT_END; + + /// \~chinese + /// @brief 窗口事件 + KGE_EVENT_BEGIN(WindowEvents); + KGE_DEFINE_EVENT(Moved, WindowMovedEvent); ///< 窗口移动 + KGE_DEFINE_EVENT(Resized, WindowResizedEvent); ///< 窗口大小变化 + KGE_DEFINE_EVENT(FocusChanged, WindowFocusChangedEvent); ///< 获得或失去焦点 + KGE_DEFINE_EVENT(TitleChanged, WindowTitleChangedEvent); ///< 标题变化 + KGE_DEFINE_EVENT(Closed, WindowClosedEvent); ///< 窗口被关闭 + KGE_EVENT_END; + + /** @} */ } diff --git a/src/kiwano/core/EventDispatcher.h b/src/kiwano/core/EventDispatcher.h index f91f70eb..ed511ea3 100644 --- a/src/kiwano/core/EventDispatcher.h +++ b/src/kiwano/core/EventDispatcher.h @@ -40,17 +40,35 @@ namespace kiwano // 添加监听器 EventListener* AddListener( - String const& name, EventType type, EventListener::Callback callback ); // 添加监听器 EventListener* AddListener( + String const& name, EventType type, EventListener::Callback callback ); + template < + typename _EventTy, + typename = typename std::enable_if::value, int>::type + > + EventListener* AddListener(EventListener::Callback callback) + { + return AddListener(KGE_EVENT(_EventTy), callback); + } + + template < + typename _EventTy, + typename = typename std::enable_if::value, int> + > + EventListener* AddListener(String const& name, EventListener::Callback callback) + { + return AddListener(name, KGE_EVENT(_EventTy), callback); + } + // 启动监听器 void StartListeners( String const& listener_name diff --git a/src/kiwano/core/time.h b/src/kiwano/core/time.h index d063179f..284a8a56 100644 --- a/src/kiwano/core/time.h +++ b/src/kiwano/core/time.h @@ -106,8 +106,8 @@ namespace kiwano String ToString() const; /// \~chinese - /// @brief 时间段格式化 - /// @param format 格式 + /// @brief 解析时间段字符串 + /// @param str 时间段字符串 /// @details /// 时间段字符串允许是有符号的浮点数, 并且带有时间单位后缀 /// 例如: "300ms", "-1.5h", "2h45m" diff --git a/src/kiwano/platform/FileSystem.h b/src/kiwano/platform/FileSystem.h index 7ac2089c..48245ea3 100644 --- a/src/kiwano/platform/FileSystem.h +++ b/src/kiwano/platform/FileSystem.h @@ -89,7 +89,7 @@ namespace kiwano /** * \~chinese * @brief 删除文件 - * @param path 文件路径 + * @param file_path 文件路径 * @return 删除是否成功 */ bool RemoveFile(String const& file_path) const; diff --git a/src/kiwano/ui/Button.cpp b/src/kiwano/ui/Button.cpp index b905e0a5..10a74a77 100644 --- a/src/kiwano/ui/Button.cpp +++ b/src/kiwano/ui/Button.cpp @@ -32,10 +32,10 @@ namespace kiwano { SetResponsible(true); - AddListener(events::MouseHover, Closure(this, &Button::UpdateStatus)); - AddListener(events::MouseOut, Closure(this, &Button::UpdateStatus)); - AddListener(events::MouseDown, Closure(this, &Button::UpdateStatus)); - AddListener(events::MouseUp, Closure(this, &Button::UpdateStatus)); + AddListener(MouseEvents::Hover(), Closure(this, &Button::UpdateStatus)); + AddListener(MouseEvents::Out(), Closure(this, &Button::UpdateStatus)); + AddListener(MouseEvents::Down(), Closure(this, &Button::UpdateStatus)); + AddListener(MouseEvents::Up(), Closure(this, &Button::UpdateStatus)); } Button::Button(const Callback& click)