add EventListener::ShouldHandle function

This commit is contained in:
Nomango 2020-06-21 16:10:45 +08:00
parent 5bebd422fb
commit 9fa8639d95
7 changed files with 41 additions and 27 deletions

View File

@ -54,7 +54,7 @@ public:
/// \~chinese /// \~chinese
/// @brief 判断事件类型 /// @brief 判断事件类型
/// @return 是否是指定事件类型 /// @return 事件类型相同返回true否则返回false
template <typename _Ty> template <typename _Ty>
bool IsType() const; bool IsType() const;
@ -75,19 +75,20 @@ private:
}; };
/// \~chinese /// \~chinese
/// @brief 事件特性:判断指定类型是否是事件 /// @brief 事件特性:判断是否是事件
template <typename _Ty> template <typename _Ty>
struct IsEvent : public std::bool_constant<std::is_base_of<Event, _Ty>::value || std::is_same<Event, _Ty>::value> struct IsBaseOfEvent : public std::bool_constant<std::is_base_of<Event, _Ty>::value || std::is_same<Event, _Ty>::value>
{ {
}; };
/// \~chinese /// \~chinese
/// @brief 事件特性:判断一个事件能否安全转换到另一事件类型 /// @brief 事件特性:判断事件类型是否相同
template <typename _Ty, typename = typename std::enable_if<IsEvent<_Ty>::value, int>::type> template <typename _Ty>
struct IsEventType struct IsSameEventType
{ {
inline bool operator()(const Event* evt) const 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); return evt->GetType() == KGE_EVENT(_Ty);
} }
}; };
@ -102,8 +103,8 @@ inline const EventType& Event::GetType() const
template <typename _Ty> template <typename _Ty>
inline bool Event::IsType() const inline bool Event::IsType() const
{ {
static_assert(kiwano::IsEvent<_Ty>::value, "_Ty is not an event type."); static_assert(kiwano::IsBaseOfEvent<_Ty>::value, "_Ty is not an event type.");
return kiwano::IsEventType<_Ty>()(this); return IsSameEventType<_Ty>()(this);
} }
template <typename _Ty> template <typename _Ty>

View File

@ -59,7 +59,7 @@ public:
template <typename _EventTy> template <typename _EventTy>
EventListener* AddListener(EventListener::Callback callback) 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); return AddListener(KGE_EVENT(_EventTy), callback);
} }
@ -71,7 +71,7 @@ public:
template <typename _EventTy> template <typename _EventTy>
EventListener* AddListener(const String& name, EventListener::Callback callback) 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); return AddListener(name, KGE_EVENT(_EventTy), callback);
} }

View File

@ -58,4 +58,12 @@ EventListener::EventListener()
EventListener::~EventListener() {} EventListener::~EventListener() {}
void EventListener::Receive(Event* evt)
{
if (ShouldHandle(evt) && callback_)
{
callback_(evt);
}
}
} // namespace kiwano } // namespace kiwano

View File

@ -67,7 +67,7 @@ public:
template <typename _EventTy> template <typename _EventTy>
static inline EventListenerPtr Create(const Callback& callback) 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); return EventListener::Create(KGE_EVENT(_EventTy), callback);
} }
@ -79,7 +79,7 @@ public:
template <typename _EventTy> template <typename _EventTy>
static inline EventListenerPtr Create(const String& name, const Callback& callback) 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); return EventListener::Create(name, KGE_EVENT(_EventTy), callback);
} }
@ -132,12 +132,17 @@ public:
/// @brief 设置监听的事件类型 /// @brief 设置监听的事件类型
void SetEventType(const EventType& type); void SetEventType(const EventType& type);
/// \~chinese
/// @brief ÅжÏÊÇ·ñ´¦Àíʼþ
virtual bool ShouldHandle(Event* evt) const;
/// \~chinese /// \~chinese
/// @brief 设置监听的事件类型 /// @brief 设置监听的事件类型
/// @tparam _EventTy 事件类型 /// @tparam _EventTy 事件类型
template <typename _EventTy, typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type> template <typename _EventTy>
inline void SetEventType() inline void SetEventType()
{ {
static_assert(kiwano::IsBaseOfEvent<_EventTy>::value, "_EventTy is not an event type.");
SetEventType(KGE_EVENT(_EventTy)); SetEventType(KGE_EVENT(_EventTy));
} }
@ -208,13 +213,13 @@ inline void EventListener::SetEventType(const EventType& type)
type_ = type; type_ = type;
} }
inline void EventListener::Receive(Event* evt) inline bool EventListener::ShouldHandle(Event* evt) const
{ {
KGE_ASSERT(evt != nullptr); if (evt)
{
return evt->GetType() == type_;
}
return false;
}
if (type_ == evt->GetType() && callback_)
{
callback_(evt);
}
}
} // namespace kiwano } // namespace kiwano

View File

@ -72,10 +72,8 @@ public:
KeyCharEvent(); KeyCharEvent();
}; };
/** @} */
template <> template <>
struct IsEventType<KeyEvent> struct IsSameEventType<KeyEvent>
{ {
inline bool operator()(const Event* evt) const inline bool operator()(const Event* evt) const
{ {
@ -84,4 +82,6 @@ struct IsEventType<KeyEvent>
} }
}; };
/** @} */
} // namespace kiwano } // namespace kiwano

View File

@ -113,10 +113,8 @@ public:
MouseWheelEvent(); MouseWheelEvent();
}; };
/** @} */
template <> template <>
struct IsEventType<MouseEvent> struct IsSameEventType<MouseEvent>
{ {
inline bool operator()(const Event* evt) const inline bool operator()(const Event* evt) const
{ {
@ -127,4 +125,6 @@ struct IsEventType<MouseEvent>
} }
}; };
/** @} */
} // namespace kiwano } // namespace kiwano

View File

@ -93,10 +93,8 @@ public:
WindowClosedEvent(); WindowClosedEvent();
}; };
/** @} */
template <> template <>
struct IsEventType<WindowEvent> struct IsSameEventType<WindowEvent>
{ {
inline bool operator()(const Event* evt) const inline bool operator()(const Event* evt) const
{ {
@ -107,4 +105,6 @@ struct IsEventType<WindowEvent>
} }
}; };
/** @} */
} // namespace kiwano } // namespace kiwano