Kiwano Engine v1.3.x
EventListener.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/core/Common.h>
23#include <kiwano/core/IntrusiveList.h>
24#include <kiwano/base/ObjectBase.h>
25#include <kiwano/event/Events.h>
26
27namespace kiwano
28{
29class EventDispatcher;
30
45class KGE_API EventListener
46 : public ObjectBase
47 , protected IntrusiveListValue<RefPtr<EventListener>>
48{
49 friend class EventDispatcher;
51
52public:
55 using Callback = Function<void(Event*)>;
56
58
59 virtual ~EventListener();
60
64 static RefPtr<EventListener> Create(const Callback& callback);
65
71 static RefPtr<EventListener> Create(StringView name, const Callback& callback);
72
77 static RefPtr<EventListener> Create(EventType type, const Callback& callback);
78
84 static RefPtr<EventListener> Create(StringView name, EventType type, const Callback& callback);
85
88 void Start();
89
92 void Stop();
93
96 void Remove();
97
100 bool IsRunning() const;
101
104 bool IsRemoveable() const;
105
108 bool IsSwallowEnabled() const;
109
113 void SetSwallowEnabled(bool enabled);
114
117 virtual void Handle(Event* evt) = 0;
118
119private:
120 bool running_;
121 bool removeable_;
122 bool swallow_;
123};
124
128{
129 running_ = true;
130}
131
133{
134 running_ = false;
135}
136
138{
139 removeable_ = true;
140}
141
142inline bool EventListener::IsRunning() const
143{
144 return running_;
145}
146
148{
149 return removeable_;
150}
151
153{
154 return swallow_;
155}
156
157inline void EventListener::SetSwallowEnabled(bool enabled)
158{
159 swallow_ = enabled;
160}
161
162} // namespace kiwano
事件分发器
Definition: EventDispatcher.h:36
事件监听器
Definition: EventListener.h:48
bool IsRemoveable() const
是否可移除
Definition: EventListener.h:147
bool IsRunning() const
是否正在运行
Definition: EventListener.h:142
void Stop()
停止监听器
Definition: EventListener.h:132
void Start()
启动监听器
Definition: EventListener.h:127
void Remove()
移除监听器
Definition: EventListener.h:137
bool IsSwallowEnabled() const
是否开启消息吞没
Definition: EventListener.h:152
virtual void Handle(Event *evt)=0
处理消息
void SetSwallowEnabled(bool enabled)
设置消息吞没功能
Definition: EventListener.h:157
事件类型
Definition: EventType.h:36
事件
Definition: Event.h:43
Definition: Function.h:228
侵入式链表元素
Definition: IntrusiveList.h:434
侵入式链表
Definition: IntrusiveList.h:34
基础对象
Definition: ObjectBase.h:138
引用计数智能指针
Definition: RefBasePtr.hpp:35