Add listener swallowable

This commit is contained in:
Nomango 2020-01-10 18:08:54 +08:00
parent b102f85096
commit 8660ee0b59
10 changed files with 62 additions and 37 deletions

View File

@ -68,13 +68,13 @@ namespace kiwano
void BeginContact(b2Contact* contact) override void BeginContact(b2Contact* contact) override
{ {
ContactBeginEvent evt(contact); ContactBeginEvent evt(contact);
world_->Dispatch(evt); world_->DispatchEvent(evt);
} }
void EndContact(b2Contact* contact) override void EndContact(b2Contact* contact) override
{ {
ContactEndEvent evt(contact); ContactEndEvent evt(contact);
world_->Dispatch(evt); world_->DispatchEvent(evt);
} }
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold) override { KGE_NOT_USED(contact); KGE_NOT_USED(oldManifold); } void PreSolve(b2Contact* contact, const b2Manifold* oldManifold) override { KGE_NOT_USED(contact); KGE_NOT_USED(oldManifold); }

View File

@ -175,10 +175,10 @@ namespace kiwano
return visible_in_rt_; return visible_in_rt_;
} }
void Actor::Dispatch(Event& evt) bool Actor::DispatchEvent(Event& evt)
{ {
if (!visible_) if (!visible_)
return; return true;
// Dispatch to children those are greater than 0 in Z-Order // Dispatch to children those are greater than 0 in Z-Order
Actor* child = children_.last_item().get(); Actor* child = children_.last_item().get();
@ -187,23 +187,29 @@ namespace kiwano
if (child->GetZOrder() < 0) if (child->GetZOrder() < 0)
break; break;
child->Dispatch(evt); if (!child->DispatchEvent(evt))
return false;
child = child->prev_item().get(); child = child->prev_item().get();
} }
if (!EventDispatcher::DispatchEvent(evt))
return false;
HandleEvent(evt); HandleEvent(evt);
while (child) while (child)
{ {
child->Dispatch(evt); if (!child->DispatchEvent(evt))
return false;
child = child->prev_item().get(); child = child->prev_item().get();
} }
return true;
} }
void Actor::HandleEvent(Event& evt) void Actor::HandleEvent(Event& evt)
{ {
EventDispatcher::Dispatch(evt);
if (responsible_) if (responsible_)
{ {
if (evt.IsType<MouseMoveEvent>()) if (evt.IsType<MouseMoveEvent>())
@ -218,7 +224,7 @@ namespace kiwano
hover.pos = mouse_evt.pos; hover.pos = mouse_evt.pos;
hover.left_btn_down = mouse_evt.left_btn_down; hover.left_btn_down = mouse_evt.left_btn_down;
hover.right_btn_down = mouse_evt.right_btn_down; hover.right_btn_down = mouse_evt.right_btn_down;
EventDispatcher::Dispatch(hover); EventDispatcher::DispatchEvent(hover);
} }
else if (hover_ && !contains) else if (hover_ && !contains)
{ {
@ -229,7 +235,7 @@ namespace kiwano
out.pos = mouse_evt.pos; out.pos = mouse_evt.pos;
out.left_btn_down = mouse_evt.left_btn_down; out.left_btn_down = mouse_evt.left_btn_down;
out.right_btn_down = mouse_evt.right_btn_down; out.right_btn_down = mouse_evt.right_btn_down;
EventDispatcher::Dispatch(out); EventDispatcher::DispatchEvent(out);
} }
} }
@ -249,7 +255,7 @@ namespace kiwano
click.left_btn_down = mouse_up_evt.left_btn_down; click.left_btn_down = mouse_up_evt.left_btn_down;
click.right_btn_down = mouse_up_evt.right_btn_down; click.right_btn_down = mouse_up_evt.right_btn_down;
click.button = mouse_up_evt.button; click.button = mouse_up_evt.button;
EventDispatcher::Dispatch(click); EventDispatcher::DispatchEvent(click);
} }
} }
} }

View File

@ -393,7 +393,9 @@ namespace kiwano
/// \~chinese /// \~chinese
/// @brief 分发事件 /// @brief 分发事件
void Dispatch(Event& evt) override; /// @param evt 事件
/// @return 是否继续分发该事件
virtual bool DispatchEvent(Event& evt);
/// \~chinese /// \~chinese
/// @brief 设置默认锚点 /// @brief 设置默认锚点

View File

@ -54,20 +54,16 @@ namespace kiwano
area_.SetMaskTransform(transform); area_.SetMaskTransform(transform);
} }
void Layer::Dispatch(Event& evt) bool Layer::DispatchEvent(Event& evt)
{ {
if (!IsVisible()) if (!IsVisible())
return; return true;
if (!swallow_) if (swallow_)
{ {
for (auto child = GetAllChildren().rbegin(); child != GetAllChildren().rend(); ++child) return EventDispatcher::DispatchEvent(evt);
{
child->Dispatch(evt);
}
} }
return Actor::DispatchEvent(evt);
EventDispatcher::Dispatch(evt);
} }
void Layer::Render(RenderContext& ctx) void Layer::Render(RenderContext& ctx)

View File

@ -82,7 +82,7 @@ namespace kiwano
/// @brief 获取图层区域 /// @brief 获取图层区域
LayerArea const& GetArea() const; LayerArea const& GetArea() const;
void Dispatch(Event& evt) override; bool DispatchEvent(Event& evt) override;
protected: protected:
void Render(RenderContext& ctx) override; void Render(RenderContext& ctx) override;

View File

@ -23,10 +23,10 @@
namespace kiwano namespace kiwano
{ {
void EventDispatcher::Dispatch(Event& evt) bool EventDispatcher::DispatchEvent(Event& evt)
{ {
if (listeners_.empty()) if (listeners_.empty())
return; return true;
EventListenerPtr next; EventListenerPtr next;
for (auto listener = listeners_.first_item(); listener; listener = next) for (auto listener = listeners_.first_item(); listener; listener = next)
@ -34,15 +34,15 @@ namespace kiwano
next = listener->next_item(); next = listener->next_item();
if (listener->IsRunning()) if (listener->IsRunning())
{
listener->Receive(evt); listener->Receive(evt);
}
if (listener->IsRemoveable()) if (listener->IsRemoveable())
{
listeners_.remove(listener); listeners_.remove(listener);
}
if (listener->IsSwallowEnabled())
return false;
} }
return true;
} }
EventListener* EventDispatcher::AddListener(EventListenerPtr listener) EventListener* EventDispatcher::AddListener(EventListenerPtr listener)

View File

@ -131,7 +131,8 @@ namespace kiwano
/// \~chinese /// \~chinese
/// @brief 分发事件 /// @brief 分发事件
/// @param evt 事件 /// @param evt 事件
virtual void Dispatch(Event& evt); /// @return 是否继续分发该事件
bool DispatchEvent(Event& evt);
private: private:
Listeners listeners_; Listeners listeners_;

View File

@ -28,21 +28,21 @@ namespace kiwano
, callback_() , callback_()
, running_(true) , running_(true)
, removeable_(false) , removeable_(false)
, swallow_(false)
{ {
} }
EventListener::EventListener(EventType type, Callback const& callback) EventListener::EventListener(EventType type, Callback const& callback)
: type_(type) : EventListener(String(), type, callback)
, callback_(callback)
, running_(true)
, removeable_(false)
{ {
} }
EventListener::EventListener(String const& name, EventType type, Callback const& callback) EventListener::EventListener(String const& name, EventType type, Callback const& callback)
: EventListener(type, callback) : EventListener()
{ {
SetName(name); SetName(name);
SetEventType(type);
SetCallback(callback);
} }
EventListener::~EventListener() EventListener::~EventListener()

View File

@ -115,6 +115,15 @@ namespace kiwano
/// @brief 是否可移除 /// @brief 是否可移除
bool IsRemoveable() const; bool IsRemoveable() const;
/// \~chinese
/// @brief 是否开启消息吞没
bool IsSwallowEnabled() const;
/// \~chinese
/// @brief 设置消息吞没功能
/// @param enabled 是否启用
void SetSwallowEnabled(bool enabled);
/// \~chinese /// \~chinese
/// @brief 获取回调函数 /// @brief 获取回调函数
const Callback& GetCallback() const; const Callback& GetCallback() const;
@ -150,6 +159,7 @@ namespace kiwano
private: private:
bool running_; bool running_;
bool removeable_; bool removeable_;
bool swallow_;
EventType type_; EventType type_;
Callback callback_; Callback callback_;
}; };
@ -180,6 +190,16 @@ namespace kiwano
return removeable_; return removeable_;
} }
inline bool EventListener::IsSwallowEnabled() const
{
return swallow_;
}
inline void EventListener::SetSwallowEnabled(bool enabled)
{
swallow_ = enabled;
}
inline const EventListener::Callback& EventListener::GetCallback() const inline const EventListener::Callback& EventListener::GetCallback() const
{ {
return callback_; return callback_;

View File

@ -182,13 +182,13 @@ namespace kiwano
void Director::HandleEvent(Event& evt) void Director::HandleEvent(Event& evt)
{ {
if (current_stage_) if (current_stage_)
current_stage_->Dispatch(evt); current_stage_->DispatchEvent(evt);
if (next_stage_) if (next_stage_)
next_stage_->Dispatch(evt); next_stage_->DispatchEvent(evt);
if (debug_actor_) if (debug_actor_)
debug_actor_->Dispatch(evt); debug_actor_->DispatchEvent(evt);
} }
} }