update EventListener

This commit is contained in:
Nomango 2020-10-10 15:34:00 +08:00
parent 8f6fd5d1f6
commit f19d52b662
6 changed files with 133 additions and 152 deletions

1
.gitignore vendored
View File

@ -13,6 +13,7 @@ Release/
# Application folders
.vs
.idea
._Kiwano.sln
# vs2010
ipch/

View File

@ -34,7 +34,7 @@ bool EventDispatcher::DispatchEvent(Event* evt)
next = listener->GetNext();
if (listener->IsRunning())
listener->Receive(evt);
listener->Handle(evt);
if (listener->IsRemoveable())
listeners_.Remove(listener);
@ -58,13 +58,13 @@ EventListener* EventDispatcher::AddListener(EventListenerPtr listener)
EventListener* EventDispatcher::AddListener(const String& name, EventType type, EventListener::Callback callback)
{
auto lis = MakePtr<EventListener>(name, type, callback);
auto lis = EventListener::Create(name, type, callback);
return AddListener(lis);
}
EventListener* EventDispatcher::AddListener(EventType type, EventListener::Callback callback)
{
auto lis = MakePtr<EventListener>(type, callback);
auto lis = EventListener::Create(type, callback);
return AddListener(lis);
}
@ -101,39 +101,6 @@ void EventDispatcher::RemoveListeners(const String& name)
}
}
void EventDispatcher::StartListeners(const EventType& type)
{
for (auto& listener : listeners_)
{
if (listener->GetEventType() == type)
{
listener->Start();
}
}
}
void EventDispatcher::StopListeners(const EventType& type)
{
for (auto& listener : listeners_)
{
if (listener->GetEventType() == type)
{
listener->Stop();
}
}
}
void EventDispatcher::RemoveListeners(const EventType& type)
{
for (auto& listener : listeners_)
{
if (listener->GetEventType() == type)
{
listener->Remove();
}
}
}
void EventDispatcher::StartAllListeners()
{
for (auto& listener : listeners_)

View File

@ -90,21 +90,6 @@ public:
/// @param name 监听器名称
void RemoveListeners(const String& name);
/// \~chinese
/// @brief 启动监听器
/// @param type 监听的事件类型
void StartListeners(const EventType& type);
/// \~chinese
/// @brief 停止监听器
/// @param type 监听的事件类型
void StopListeners(const EventType& type);
/// \~chinese
/// @brief 移除监听器
/// @param type 监听的事件类型
void RemoveListeners(const EventType& type);
/// \~chinese
/// @brief 启动所有监听器
void StartAllListeners();

View File

@ -25,35 +25,63 @@ namespace kiwano
{
EventListener::EventListener()
: type_()
, callback_()
, running_(true)
: running_(true)
, removeable_(false)
, swallow_(false)
{
}
EventListener::EventListener(EventType type, const Callback& callback)
: EventListener()
{
this->SetEventType(type);
this->SetCallback(callback);
}
EventListener::EventListener(const String& name, EventType type, const Callback& callback)
: EventListener(type, callback)
{
this->SetName(name);
}
EventListener::~EventListener() {}
void EventListener::Receive(Event* evt)
class CallbackEventListener : public EventListener
{
if (ShouldHandle(evt) && callback_)
public:
CallbackEventListener(EventType type, const Callback& cb)
: type_(type)
, cb_(cb)
{
callback_(evt);
}
void Handle(Event* evt) override
{
if (type_.IsNull() || type_ == evt->GetType())
{
if (cb_)
{
cb_(evt);
}
}
}
private:
EventType type_;
Callback cb_;
};
EventListenerPtr EventListener::Create(const Callback& callback)
{
EventListenerPtr ptr = new CallbackEventListener(EventType(), callback);
return ptr;
}
EventListenerPtr EventListener::Create(const String& name, const Callback& callback)
{
EventListenerPtr ptr = new CallbackEventListener(EventType(), callback);
ptr->SetName(name);
return ptr;
}
EventListenerPtr EventListener::Create(EventType type, const Callback& callback)
{
EventListenerPtr ptr = new CallbackEventListener(type, callback);
return ptr;
}
EventListenerPtr EventListener::Create(const String& name, EventType type, const Callback& callback)
{
EventListenerPtr ptr = new CallbackEventListener(type, callback);
ptr->SetName(name);
return ptr;
}
} // namespace kiwano

View File

@ -48,20 +48,32 @@ public:
EventListener();
virtual ~EventListener();
/// \~chinese
/// @brief 创建监听器
/// @param type 监听的事件类型
/// @param callback 回调函数
EventListener(EventType type, const Callback& callback);
static EventListenerPtr Create(const Callback& callback);
/// \~chinese
/// @brief 创建监听器
/// @param name 监听器名称
/// @param type 监听的事件类型
/// @param callback 回调函数
EventListener(const String& name, EventType type, const Callback& callback);
static EventListenerPtr Create(const String& name, const Callback& callback);
virtual ~EventListener();
/// \~chinese
/// @brief 创建监听器
/// @param type 监听的事件类型
/// @param callback 回调函数
static EventListenerPtr Create(EventType type, const Callback& callback);
/// \~chinese
/// @brief 创建监听器
/// @param name 监听器名称
/// @param type 监听的事件类型
/// @param callback 回调函数
static EventListenerPtr Create(const String& name, EventType type, const Callback& callback);
/// \~chinese
/// @brief 启动监听器
@ -93,45 +105,13 @@ public:
void SetSwallowEnabled(bool enabled);
/// \~chinese
/// @brief 获取回调函数
const Callback& GetCallback() const;
/// \~chinese
/// @brief 设置回调函数
void SetCallback(const Callback& cb);
/// \~chinese
/// @brief 获取监听的事件类型
EventType GetEventType() const;
/// \~chinese
/// @brief 设置监听的事件类型
void SetEventType(const EventType& type);
/// \~chinese
/// @brief 判断是否处理事件
virtual bool ShouldHandle(Event* evt) const;
/// \~chinese
/// @brief 设置监听的事件类型
/// @tparam _EventTy 事件类型
template <typename _EventTy>
inline void SetEventType()
{
static_assert(std::is_base_of<Event, _EventTy>::value, "_EventTy is not an event type.");
SetEventType(KGE_EVENT(_EventTy));
}
/// \~chinese
/// @brief 接收消息
void Receive(Event* evt);
/// @brief 处理消息
virtual void Handle(Event* evt) = 0;
private:
bool running_;
bool removeable_;
bool swallow_;
EventType type_;
Callback callback_;
};
inline void EventListener::Start()
@ -169,33 +149,4 @@ inline void EventListener::SetSwallowEnabled(bool enabled)
swallow_ = enabled;
}
inline const EventListener::Callback& EventListener::GetCallback() const
{
return callback_;
}
inline void EventListener::SetCallback(const Callback& cb)
{
callback_ = cb;
}
inline EventType EventListener::GetEventType() const
{
return type_;
}
inline void EventListener::SetEventType(const EventType& type)
{
type_ = type;
}
inline bool EventListener::ShouldHandle(Event* evt) const
{
if (evt)
{
return evt->GetType() == type_;
}
return false;
}
} // namespace kiwano

View File

@ -32,24 +32,28 @@ namespace kiwano
/// \~chinese
/// @brief 事件类型
class EventType : public std::type_index
class EventType
{
class Dummy
{
};
public:
/// \~chinese
/// @brief 构建事件类型
EventType();
using std::type_index::type_index;
using std::type_index::operator==;
using std::type_index::operator!=;
using std::type_index::operator<;
using std::type_index::operator>=;
using std::type_index::operator>;
using std::type_index::operator<=;
EventType(const std::type_index& type);
/// \~chinese
/// @brief ÊÇ·ñÊÇ¿ÕÀàÐÍ
bool IsNull() const;
const std::type_index& GetType() const;
bool operator==(const EventType& rhs) const;
bool operator!=(const EventType& rhs) const;
bool operator<(const EventType& rhs) const;
bool operator<=(const EventType& rhs) const;
bool operator>(const EventType& rhs) const;
bool operator>=(const EventType& rhs) const;
private:
std::type_index type_;
};
/** @} */
@ -57,8 +61,53 @@ public:
#define KGE_EVENT(EVENT_TYPE) ::kiwano::EventType(typeid(EVENT_TYPE))
inline EventType::EventType()
: std::type_index(typeid(EventType::Dummy))
: type_(typeid(void))
{
}
inline EventType::EventType(const std::type_index& type)
: type_(type)
{
}
inline bool EventType::IsNull() const
{
return type_ == typeid(void);
}
inline const std::type_index& EventType::GetType() const
{
return type_;
}
inline bool EventType::operator==(const EventType& rhs) const
{
return type_ == rhs.type_;
}
inline bool EventType::operator!=(const EventType& rhs) const
{
return type_ != rhs.type_;
}
inline bool EventType::operator<(const EventType& rhs) const
{
return type_ < rhs.type_;
}
inline bool EventType::operator<=(const EventType& rhs) const
{
return type_ <= rhs.type_;
}
inline bool EventType::operator>(const EventType& rhs) const
{
return type_ > rhs.type_;
}
inline bool EventType::operator>=(const EventType& rhs) const
{
return type_ >= rhs.type_;
}
} // namespace kiwano