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.hpp>
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
70 static RefPtr<EventListener> Create(StringView name, const Callback& callback);
71
76 static RefPtr<EventListener> Create(EventType type, const Callback& callback);
77
83 static RefPtr<EventListener> Create(StringView name, EventType type, const Callback& callback);
84
87 void Start();
88
91 void Stop();
92
95 void Remove();
96
99 bool IsRunning() const;
100
103 bool IsRemoveable() const;
104
107 bool IsSwallowEnabled() const;
108
112 void SetSwallowEnabled(bool enabled);
113
116 virtual void Handle(Event* evt) = 0;
117
118private:
119 bool running_;
120 bool removeable_;
121 bool swallow_;
122};
123
127{
128 running_ = true;
129}
130
132{
133 running_ = false;
134}
135
137{
138 removeable_ = true;
139}
140
141inline bool EventListener::IsRunning() const
142{
143 return running_;
144}
145
147{
148 return removeable_;
149}
150
152{
153 return swallow_;
154}
155
156inline void EventListener::SetSwallowEnabled(bool enabled)
157{
158 swallow_ = enabled;
159}
160
161} // namespace kiwano
事件分发器
Definition: EventDispatcher.h:36
事件监听器
Definition: EventListener.h:48
bool IsRemoveable() const
是否可移除
Definition: EventListener.h:146
bool IsRunning() const
是否正在运行
Definition: EventListener.h:141
void Stop()
停止监听器
Definition: EventListener.h:131
void Start()
启动监听器
Definition: EventListener.h:126
void Remove()
移除监听器
Definition: EventListener.h:136
bool IsSwallowEnabled() const
是否开启消息吞没
Definition: EventListener.h:151
virtual void Handle(Event *evt)=0
处理消息
void SetSwallowEnabled(bool enabled)
设置消息吞没功能
Definition: EventListener.h:156
事件类型
Definition: EventType.h:36
事件
Definition: Event.h:43
Definition: Function.h:228
侵入式链表元素
Definition: IntrusiveList.hpp:434
侵入式链表
Definition: IntrusiveList.hpp:34
基础对象
Definition: ObjectBase.h:138
引用计数智能指针
Definition: RefBasePtr.hpp:35