diff --git a/src/kiwano/event/Event.h b/src/kiwano/event/Event.h index 16242d27..80fa0b76 100644 --- a/src/kiwano/event/Event.h +++ b/src/kiwano/event/Event.h @@ -54,7 +54,7 @@ public: /// \~chinese /// @brief 判断事件类型 - /// @return 是否是指定事件类型 + /// @return 事件类型相同返回true,否则返回false template bool IsType() const; @@ -75,19 +75,20 @@ private: }; /// \~chinese -/// @brief 事件特性:判断指定类型是否是事件 +/// @brief 事件特性:判断是否是事件 template -struct IsEvent : public std::bool_constant::value || std::is_same::value> +struct IsBaseOfEvent : public std::bool_constant::value || std::is_same::value> { }; /// \~chinese -/// @brief 事件特性:判断一个事件能否安全转换到另一事件类型 -template ::value, int>::type> -struct IsEventType +/// @brief 事件特性:判断事件类型是否相同 +template +struct IsSameEventType { inline bool operator()(const Event* evt) const { + static_assert(kiwano::IsBaseOfEvent<_Ty>::value, "_Ty is not an event type."); return evt->GetType() == KGE_EVENT(_Ty); } }; @@ -102,8 +103,8 @@ inline const EventType& Event::GetType() const template inline bool Event::IsType() const { - static_assert(kiwano::IsEvent<_Ty>::value, "_Ty is not an event type."); - return kiwano::IsEventType<_Ty>()(this); + static_assert(kiwano::IsBaseOfEvent<_Ty>::value, "_Ty is not an event type."); + return IsSameEventType<_Ty>()(this); } template diff --git a/src/kiwano/event/EventDispatcher.h b/src/kiwano/event/EventDispatcher.h index 6e5a0861..6e46a9a4 100644 --- a/src/kiwano/event/EventDispatcher.h +++ b/src/kiwano/event/EventDispatcher.h @@ -59,7 +59,7 @@ public: template EventListener* AddListener(EventListener::Callback callback) { - static_assert(kiwano::IsEvent<_EventTy>::value, "_EventTy is not an event type."); + static_assert(kiwano::IsBaseOfEvent<_EventTy>::value, "_EventTy is not an event type."); return AddListener(KGE_EVENT(_EventTy), callback); } @@ -71,7 +71,7 @@ public: template EventListener* AddListener(const String& name, EventListener::Callback callback) { - static_assert(kiwano::IsEvent<_EventTy>::value, "_EventTy is not an event type."); + static_assert(kiwano::IsBaseOfEvent<_EventTy>::value, "_EventTy is not an event type."); return AddListener(name, KGE_EVENT(_EventTy), callback); } diff --git a/src/kiwano/event/EventListener.cpp b/src/kiwano/event/EventListener.cpp index 1549d5d3..060ba62d 100644 --- a/src/kiwano/event/EventListener.cpp +++ b/src/kiwano/event/EventListener.cpp @@ -58,4 +58,12 @@ EventListener::EventListener() EventListener::~EventListener() {} +void EventListener::Receive(Event* evt) +{ + if (ShouldHandle(evt) && callback_) + { + callback_(evt); + } +} + } // namespace kiwano diff --git a/src/kiwano/event/EventListener.h b/src/kiwano/event/EventListener.h index d179ba11..4baed41b 100644 --- a/src/kiwano/event/EventListener.h +++ b/src/kiwano/event/EventListener.h @@ -67,7 +67,7 @@ public: template static inline EventListenerPtr Create(const Callback& callback) { - static_assert(kiwano::IsEvent<_EventTy>::value, "_EventTy is not an event type."); + static_assert(kiwano::IsBaseOfEvent<_EventTy>::value, "_EventTy is not an event type."); return EventListener::Create(KGE_EVENT(_EventTy), callback); } @@ -79,7 +79,7 @@ public: template static inline EventListenerPtr Create(const String& name, const Callback& callback) { - static_assert(kiwano::IsEvent<_EventTy>::value, "_EventTy is not an event type."); + static_assert(kiwano::IsBaseOfEvent<_EventTy>::value, "_EventTy is not an event type."); return EventListener::Create(name, KGE_EVENT(_EventTy), callback); } @@ -132,12 +132,17 @@ public: /// @brief 设置监听的事件类型 void SetEventType(const EventType& type); + /// \~chinese + /// @brief 判断是否处理事件 + virtual bool ShouldHandle(Event* evt) const; + /// \~chinese /// @brief 设置监听的事件类型 /// @tparam _EventTy 事件类型 - template ::value, int>::type> + template inline void SetEventType() { + static_assert(kiwano::IsBaseOfEvent<_EventTy>::value, "_EventTy is not an event type."); SetEventType(KGE_EVENT(_EventTy)); } @@ -208,13 +213,13 @@ inline void EventListener::SetEventType(const EventType& type) type_ = type; } -inline void EventListener::Receive(Event* evt) +inline bool EventListener::ShouldHandle(Event* evt) const { - KGE_ASSERT(evt != nullptr); - - if (type_ == evt->GetType() && callback_) + if (evt) { - callback_(evt); + return evt->GetType() == type_; } + return false; } + } // namespace kiwano diff --git a/src/kiwano/event/KeyEvent.h b/src/kiwano/event/KeyEvent.h index a56ed119..390cd01d 100644 --- a/src/kiwano/event/KeyEvent.h +++ b/src/kiwano/event/KeyEvent.h @@ -72,10 +72,8 @@ public: KeyCharEvent(); }; -/** @} */ - template <> -struct IsEventType +struct IsSameEventType { inline bool operator()(const Event* evt) const { @@ -84,4 +82,6 @@ struct IsEventType } }; +/** @} */ + } // namespace kiwano diff --git a/src/kiwano/event/MouseEvent.h b/src/kiwano/event/MouseEvent.h index 29731d39..6a308d93 100644 --- a/src/kiwano/event/MouseEvent.h +++ b/src/kiwano/event/MouseEvent.h @@ -113,10 +113,8 @@ public: MouseWheelEvent(); }; -/** @} */ - template <> -struct IsEventType +struct IsSameEventType { inline bool operator()(const Event* evt) const { @@ -127,4 +125,6 @@ struct IsEventType } }; +/** @} */ + } // namespace kiwano diff --git a/src/kiwano/event/WindowEvent.h b/src/kiwano/event/WindowEvent.h index 4370422d..d0c5da83 100644 --- a/src/kiwano/event/WindowEvent.h +++ b/src/kiwano/event/WindowEvent.h @@ -93,10 +93,8 @@ public: WindowClosedEvent(); }; -/** @} */ - template <> -struct IsEventType +struct IsSameEventType { inline bool operator()(const Event* evt) const { @@ -107,4 +105,6 @@ struct IsEventType } }; +/** @} */ + } // namespace kiwano