Kiwano Engine v1.3.x
EventDispatcher.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/event/listener/EventListener.h>
23
24namespace kiwano
25{
26
29typedef IntrusiveList<RefPtr<EventListener>> ListenerList;
30
35class KGE_API EventDispatcher
36{
37public:
40 EventListener* AddListener(RefPtr<EventListener> listener);
41
46 EventListener* AddListener(EventType type, EventListener::Callback callback);
47
53 EventListener* AddListener(StringView name, EventType type, EventListener::Callback callback);
54
59 template <typename _EventTy>
61 {
62 static_assert(std::is_base_of<Event, _EventTy>::value, "_EventTy is not an event type.");
63 return AddListener(KGE_EVENT(_EventTy), callback);
64 }
65
71 template <typename _EventTy>
73 {
74 static_assert(std::is_base_of<Event, _EventTy>::value, "_EventTy is not an event type.");
75 return AddListener(name, KGE_EVENT(_EventTy), callback);
76 }
77
81 void StartListeners(StringView name);
82
86 void StopListeners(StringView name);
87
91 void RemoveListeners(StringView name);
92
95 void StartAllListeners();
96
99 void StopAllListeners();
100
103 void RemoveAllListeners();
104
107 const ListenerList& GetAllListeners() const;
108
113 bool DispatchEvent(Event* evt);
114
115private:
116 ListenerList listeners_;
117};
118} // namespace kiwano
事件分发器
Definition: EventDispatcher.h:36
EventListener * AddListener(StringView name, EventListener::Callback callback)
添加监听器
Definition: EventDispatcher.h:72
EventListener * AddListener(EventListener::Callback callback)
添加监听器
Definition: EventDispatcher.h:60
事件监听器
Definition: EventListener.h:48
事件类型
Definition: EventType.h:36
事件
Definition: Event.h:43
Definition: Function.h:228
引用计数智能指针
Definition: RefBasePtr.hpp:35