Update EventListener & EventDispatcher

This commit is contained in:
Nomango 2019-12-25 23:37:36 +08:00
parent 11c43a0731
commit bd20606041
9 changed files with 241 additions and 88 deletions

View File

@ -58,12 +58,12 @@ namespace kiwano
return action;
}
Action* ActionManager::GetAction(String const & name)
ActionPtr ActionManager::GetAction(String const & name)
{
if (actions_.empty())
return nullptr;
for (auto action = actions_.first_item().get(); action; action = action->next_item().get())
for (auto& action : actions_)
if (action->IsName(name))
return action;
@ -75,7 +75,7 @@ namespace kiwano
if (actions_.empty())
return;
for (auto action = actions_.first_item().get(); action; action = action->next_item().get())
for (auto& action : actions_)
{
action->Resume();
}
@ -86,7 +86,7 @@ namespace kiwano
if (actions_.empty())
return;
for (auto action = actions_.first_item().get(); action; action = action->next_item().get())
for (auto& action : actions_)
{
action->Pause();
}
@ -97,7 +97,7 @@ namespace kiwano
if (actions_.empty())
return;
for (auto action = actions_.first_item().get(); action; action = action->next_item().get())
for (auto& action : actions_)
{
action->Stop();
}

View File

@ -48,7 +48,7 @@ namespace kiwano
/// \~chinese
/// @brief 获取指定名称的动画
/// @param name 动画名称
Action* GetAction(String const& name);
ActionPtr GetAction(String const& name);
/// \~chinese
/// @brief 继续所有暂停动画

View File

@ -37,6 +37,11 @@ namespace kiwano
{
listener->callback_(evt);
}
if (listener->IsRemoveable())
{
listeners_.remove(listener);
}
}
}
@ -70,7 +75,7 @@ namespace kiwano
void EventDispatcher::StartListeners(String const & listener_name)
{
for (auto listener = listeners_.first_item(); listener; listener = listener->next_item())
for (auto& listener : listeners_)
{
if (listener->IsName(listener_name))
{
@ -81,7 +86,7 @@ namespace kiwano
void EventDispatcher::StopListeners(String const & listener_name)
{
for (auto listener = listeners_.first_item(); listener; listener = listener->next_item())
for (auto& listener : listeners_)
{
if (listener->IsName(listener_name))
{
@ -92,21 +97,18 @@ namespace kiwano
void EventDispatcher::RemoveListeners(String const & listener_name)
{
EventListenerPtr next;
for (auto listener = listeners_.first_item(); listener; listener = next)
for (auto& listener : listeners_)
{
next = listener->next_item();
if (listener->IsName(listener_name))
{
listeners_.remove(listener);
listener->Remove();
}
}
}
void EventDispatcher::StartListeners(const EventType& type)
{
for (auto listener = listeners_.first_item(); listener; listener = listener->next_item())
for (auto& listener : listeners_)
{
if (listener->type_ == type)
{
@ -117,7 +119,7 @@ namespace kiwano
void EventDispatcher::StopListeners(const EventType& type)
{
for (auto listener = listeners_.first_item(); listener; listener = listener->next_item())
for (auto& listener : listeners_)
{
if (listener->type_ == type)
{
@ -128,16 +130,42 @@ namespace kiwano
void EventDispatcher::RemoveListeners(const EventType& type)
{
EventListenerPtr next;
for (auto listener = listeners_.first_item(); listener; listener = next)
for (auto& listener : listeners_)
{
next = listener->next_item();
if (listener->type_ == type)
{
listeners_.remove(listener);
listener->Remove();
}
}
}
void EventDispatcher::StartAllListeners()
{
for (auto& listener : listeners_)
{
listener->Start();
}
}
void EventDispatcher::StopAllListeners()
{
for (auto& listener : listeners_)
{
listener->Stop();
}
}
void EventDispatcher::RemoveAllListeners()
{
for (auto& listener : listeners_)
{
listener->Remove();
}
}
const EventDispatcher::Listeners& EventDispatcher::GetAllListeners() const
{
return listeners_;
}
}

View File

@ -23,34 +23,40 @@
namespace kiwano
{
/**
* \~chinese
* @brief
*/
class KGE_API EventDispatcher
{
public:
using Listeners = IntrusiveList<EventListenerPtr>;
public:
// 添加监听器
EventListener* AddListener(
EventListenerPtr listener
);
/// \~chinese
/// @brief 添加监听器
EventListener* AddListener(EventListenerPtr listener);
// 添加监听器
EventListener* AddListener(
EventListener* listener
);
/// \~chinese
/// @brief 添加监听器
EventListener* AddListener(EventListener* listener);
// 添加监听器
EventListener* AddListener(
EventType type,
EventListener::Callback callback
);
/// \~chinese
/// @brief 添加监听器
/// @param type 监听的事件类型
/// @param callback 回调函数
EventListener* AddListener(EventType type, EventListener::Callback callback);
// 添加监听器
EventListener* AddListener(
String const& name,
EventType type,
EventListener::Callback callback
);
/// \~chinese
/// @brief 添加监听器
/// @param name 监听器名称
/// @param type 监听的事件类型
/// @param callback 回调函数
EventListener* AddListener(String const& name, EventType type, EventListener::Callback callback);
/// \~chinese
/// @brief 添加监听器
/// @tparam _EventTy 事件类型
/// @param callback 回调函数
template <
typename _EventTy,
typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type
@ -60,6 +66,11 @@ namespace kiwano
return AddListener(KGE_EVENT(_EventTy), callback);
}
/// \~chinese
/// @brief 添加监听器
/// @tparam _EventTy 事件类型
/// @param name 监听器名称
/// @param callback 回调函数
template <
typename _EventTy,
typename = typename std::enable_if<IsEvent<_EventTy>::value, int>
@ -69,36 +80,55 @@ namespace kiwano
return AddListener(name, KGE_EVENT(_EventTy), callback);
}
// 启动监听器
void StartListeners(
String const& listener_name
);
/// \~chinese
/// @brief 启动监听器
/// @param name 监听器名称
void StartListeners(String const& name);
// 停止监听器
void StopListeners(
String const& listener_name
);
/// \~chinese
/// @brief 停止监听器
/// @param name 监听器名称
void StopListeners(String const& name);
// 移除监听器
void RemoveListeners(
String const& listener_name
);
/// \~chinese
/// @brief 移除监听器
/// @param name 监听器名称
void RemoveListeners(String const& name);
// 启动监听器
void StartListeners(
const EventType& type
);
/// \~chinese
/// @brief 启动监听器
/// @param type 监听的事件类型
void StartListeners(const EventType& type);
// 停止监听器
void StopListeners(
const EventType& type
);
/// \~chinese
/// @brief 停止监听器
/// @param type 监听的事件类型
void StopListeners(const EventType& type);
// 移除监听器
void RemoveListeners(
const EventType& type
);
/// \~chinese
/// @brief 移除监听器
/// @param type 监听的事件类型
void RemoveListeners(const EventType& type);
/// \~chinese
/// @brief 启动所有监听器
void StartAllListeners();
/// \~chinese
/// @brief 停止所有监听器
void StopAllListeners();
/// \~chinese
/// @brief 移除所有监听器
void RemoveAllListeners();
/// \~chinese
/// @brief 获取所有监听器
const Listeners& GetAllListeners() const;
/// \~chinese
/// @brief 分发事件
/// @param evt 事件
virtual void Dispatch(Event& evt);
private:

View File

@ -27,6 +27,7 @@ namespace kiwano
: type_()
, callback_()
, running_(true)
, removeable_(false)
{
}

View File

@ -30,7 +30,10 @@ namespace kiwano
KGE_DECLARE_SMART_PTR(EventListener);
// ʼþ¼àÌýÆ÷
/**
* \~chinese
* @brief
*/
class KGE_API EventListener
: public ObjectBase
, protected IntrusiveListItem<EventListenerPtr>
@ -39,57 +42,107 @@ namespace kiwano
friend IntrusiveList<EventListenerPtr>;
public:
/// \~chinese
/// @brief 监听器回调函数
using Callback = Function<void(Event&)>;
/// \~chinese
/// @brief 构造空监听器
EventListener();
EventListener(
EventType type,
Callback const& callback
);
/// \~chinese
/// @brief 构造监听器
/// @param type 监听的事件类型
/// @param callback 回调函数
EventListener(EventType type, Callback const& callback);
EventListener(
String const& name,
EventType type,
Callback const& callback
);
/// \~chinese
/// @brief 构造监听器
/// @param name 监听器名称
/// @param type 监听的事件类型
/// @param callback 回调函数
EventListener(String const& name, EventType type, Callback const& callback);
/// \~chinese
/// @brief 构造监听器
/// @tparam _EventTy 事件类型
/// @param callback 回调函数
template <
typename _EventTy,
typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type
>
EventListener(Callback const& callback)
inline EventListener(Callback const& callback)
: EventListener(KGE_EVENT(_EventTy), callback)
{
}
/// \~chinese
/// @brief 构造监听器
/// @tparam _EventTy 事件类型
/// @param name 监听器名称
/// @param callback 回调函数
template <
typename _EventTy,
typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type
>
EventListener(String const& name, Callback const& callback)
inline EventListener(String const& name, Callback const& callback)
: EventListener(name, KGE_EVENT(_EventTy), callback)
{
}
virtual ~EventListener();
/// \~chinese
/// @brief 启动监听器
void Start();
/// \~chinese
/// @brief 停止监听器
void Stop();
/// \~chinese
/// @brief 移除监听器
void Remove();
/// \~chinese
/// @brief 是否正在运行
bool IsRunning() const;
Callback GetCallback() const;
/// \~chinese
/// @brief 是否可移除
bool IsRemoveable() const;
/// \~chinese
/// @brief 获取回调函数
const Callback& GetCallback() const;
/// \~chinese
/// @brief 设置回调函数
void SetCallback(Callback const& cb);
EventType const& GetEventType() const;
/// \~chinese
/// @brief 获取监听的事件类型
EventType GetEventType() const;
/// \~chinese
/// @brief 设置监听的事件类型
void SetEventType(EventType const& type);
/// \~chinese
/// @brief 设置监听的事件类型
/// @tparam _EventTy 事件类型
template <
typename _EventTy,
typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type
>
inline void SetEventType()
{
SetEventType(KGE_EVENT(_EventTy));
}
private:
bool running_;
bool removeable_;
EventType type_;
Callback callback_;
};
@ -105,12 +158,22 @@ namespace kiwano
running_ = false;
}
inline void EventListener::Remove()
{
removeable_ = true;
}
inline bool EventListener::IsRunning() const
{
return running_;
}
inline EventListener::Callback EventListener::GetCallback() const
inline bool EventListener::IsRemoveable() const
{
return removeable_;
}
inline const EventListener::Callback& EventListener::GetCallback() const
{
return callback_;
}
@ -120,7 +183,7 @@ namespace kiwano
callback_ = cb;
}
inline EventType const& EventListener::GetEventType() const
inline EventType EventListener::GetEventType() const
{
return type_;
}

View File

@ -24,22 +24,45 @@
namespace kiwano
{
// DLL ¿â
/**
* \~chinese
* @brief DLL库
*/
class KGE_API Library
{
public:
/// \~chinese
/// @brief 构造DLL库
Library();
/// \~chinese
/// @brief 构造DLL库
/// @param lib DLL文件路径
Library(String const& lib);
virtual ~Library();
/// \~chinese
/// @brief 加载DLL
/// @param lib DLL文件路径
bool Load(String const& lib);
/// \~chinese
/// @brief 是否有效
bool IsValid() const;
/// \~chinese
/// @brief 释放DLL
void Free();
/// \~chinese
/// @brief 检索指定的DLL中的输出库函数地址
/// @param proc_name 函数名
FARPROC GetProcess(String const& proc_name);
/// \~chinese
/// @brief 检索指定的DLL中的输出库函数地址
/// @param proc_name 函数名
template <typename _Proc>
inline _Proc GetProcess(String const& proc_name)
{

View File

@ -72,14 +72,24 @@ namespace kiwano
String DumpObject();
public:
/// \~chinese
/// @brief 是否启用了内存泄漏追踪
static bool IsTracingLeaks();
/// \~chinese
/// @brief 开始追踪内存泄漏
static void StartTracingLeaks();
/// \~chinese
/// @brief 停止追踪内存泄漏
static void StopTracingLeaks();
/// \~chinese
/// @brief 打印所有追踪中的对象信息
static void DumpTracingObjects();
/// \~chinese
/// @brief 获取所有追踪中的对象
static Vector<ObjectBase*>& GetTracingObjects();
private:

View File

@ -69,7 +69,7 @@ namespace kiwano
if (timers_.empty())
return;
for (auto timer = timers_.first_item().get(); timer; timer = timer->next_item().get())
for (auto& timer : timers_)
{
if (timer->IsName(name))
{
@ -83,7 +83,7 @@ namespace kiwano
if (timers_.empty())
return;
for (auto timer = timers_.first_item().get(); timer; timer = timer->next_item().get())
for (auto& timer : timers_)
{
if (timer->IsName(name))
{
@ -97,10 +97,8 @@ namespace kiwano
if (timers_.empty())
return;
TimerPtr next;
for (auto timer = timers_.first_item(); timer; timer = next)
for (auto& timer : timers_)
{
next = timer->next_item();
if (timer->IsName(name))
{
timer->Remove();
@ -113,7 +111,7 @@ namespace kiwano
if (timers_.empty())
return;
for (auto timer = timers_.first_item().get(); timer; timer = timer->next_item().get())
for (auto& timer : timers_)
{
timer->Stop();
}
@ -124,7 +122,7 @@ namespace kiwano
if (timers_.empty())
return;
for (auto timer = timers_.first_item().get(); timer; timer = timer->next_item().get())
for (auto& timer : timers_)
{
timer->Start();
}