• R/O
  • HTTP
  • SSH
  • HTTPS

コミット

タグ
未設定

よく使われているワード(クリックで追加)

javaandroidc++linuxc#objective-ccocoa誰得qtrubybathyscaphegamephpguicwindows翻訳pythonomegattwitterframeworkbtronarduinovb.net計画中(planning stage)directxpreviewertestゲームエンジンdom

Simple C++ UI framework and more...


コミットメタ情報

リビジョン18abe6014ccfc7209ade8b1bdb7862ae1e54e443 (tree)
日時2018-06-11 01:11:35
作者Starg <starg@user...>
コミッターStarg

ログメッセージ

Event handlers now return bool to indicate if an event was handled

変更サマリ

差分

--- a/include/sirius/event.hpp
+++ b/include/sirius/event.hpp
@@ -41,8 +41,16 @@ public:
4141 EventListener& operator=(const EventListener&) = delete;
4242 virtual ~EventListener() = default;
4343
44- virtual void OnEvent(const std::shared_ptr<EventHandler>& /* pTarget */, EventID /* id */, EventArgs* /* pArgs */)
44+ // return true to suppress event
45+ virtual bool OnInvokeEvent(const std::shared_ptr<EventHandler>& /* pTarget */, EventID /* id */, EventArgs* /* pArgs */)
4546 {
47+ return false;
48+ }
49+
50+ // return true if event was handled
51+ virtual bool OnInvokeEventReturn(const std::shared_ptr<EventHandler>& /* pTarget */, EventID /* id */, EventArgs* /* pArgs */, bool /* handled */)
52+ {
53+ return false;
4654 }
4755 };
4856
@@ -61,9 +69,13 @@ public:
6169 void RemoveEventListener(EventListener* pListener);
6270 void RemoveAllEventListeners();
6371
64- void InvokeEventListeners(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs* pArgs);
72+ bool CallOnInvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs* pArgs);
73+ bool CallOnInvokeEventReturn(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs* pArgs, bool handled);
6574
6675 private:
76+ template<typename T>
77+ bool InvokeEventListeners(T&& invoker);
78+
6779 std::vector<std::variant<EventListener*, std::unique_ptr<EventListener>, std::shared_ptr<EventListener>, std::weak_ptr<EventListener>>> m_Listeners;
6880 };
6981
@@ -75,8 +87,10 @@ public:
7587 EventHandler& operator=(const EventHandler&) = delete;
7688 virtual ~EventHandler() = default;
7789
78- virtual void OnEvent(EventID /* id */, EventArgs* /* pArgs */)
90+ // return true if event was handled
91+ virtual bool OnEvent(EventID /* id */, EventArgs* /* pArgs */)
7992 {
93+ return false;
8094 }
8195 };
8296
@@ -94,10 +108,10 @@ public:
94108 EventManager& operator=(const EventManager&) = delete;
95109 ~EventManager() = default;
96110
97- void InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs* pArgs = nullptr);
98- void InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs& args);
99- void InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs&& args);
100- void InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, const std::unique_ptr<EventArgs>& pArgs);
111+ bool InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs* pArgs = nullptr);
112+ bool InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs& args);
113+ bool InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs&& args);
114+ bool InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, const std::unique_ptr<EventArgs>& pArgs);
101115
102116 // Not thread safe
103117 void PostEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, std::unique_ptr<EventArgs> pArgs = nullptr);
@@ -154,11 +168,12 @@ public:
154168 );
155169
156170 // Thread safe
157- void SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs* pArgs = nullptr);
158- void SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs& args);
159- void SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs&& args);
160- void SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, const std::unique_ptr<EventArgs>& pArgs);
161- std::future<std::unique_ptr<EventArgs>> SendEventAsync(
171+ bool SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs* pArgs = nullptr);
172+ bool SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs& args);
173+ bool SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs&& args);
174+ bool SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, const std::unique_ptr<EventArgs>& pArgs);
175+
176+ std::future<std::pair<bool, std::unique_ptr<EventArgs>>> SendEventAsync(
162177 const std::shared_ptr<EventHandler>& pTarget,
163178 EventID id,
164179 std::unique_ptr<EventArgs> pArgs = nullptr
@@ -239,7 +254,7 @@ private:
239254 std::shared_ptr<EventHandler> pHandler;
240255 EventID ID;
241256 std::variant<EventArgs*, std::unique_ptr<EventArgs>> pArgs;
242- std::promise<std::unique_ptr<EventArgs>> Promise;
257+ std::promise<std::pair<bool, std::unique_ptr<EventArgs>>> Promise;
243258 };
244259
245260 void TryInvokeQueuedEvent(QueuedEvent& ev);
@@ -274,24 +289,24 @@ protected:
274289 ~EventInvokeUtility() = default;
275290
276291 public:
277- void InvokeEvent(EventID id, EventArgs* pArgs = nullptr)
292+ bool InvokeEvent(EventID id, EventArgs* pArgs = nullptr)
278293 {
279- CallGetEventManager().InvokeEvent(CallGetEventHandler(), id, pArgs);
294+ return CallGetEventManager().InvokeEvent(CallGetEventHandler(), id, pArgs);
280295 }
281296
282- void InvokeEvent(EventID id, EventArgs& args)
297+ bool InvokeEvent(EventID id, EventArgs& args)
283298 {
284- CallGetEventManager().InvokeEvent(CallGetEventHandler(), id, args);
299+ return CallGetEventManager().InvokeEvent(CallGetEventHandler(), id, args);
285300 }
286301
287- void InvokeEvent(EventID id, EventArgs&& args)
302+ bool InvokeEvent(EventID id, EventArgs&& args)
288303 {
289- CallGetEventManager().InvokeEvent(CallGetEventHandler(), id, args);
304+ return CallGetEventManager().InvokeEvent(CallGetEventHandler(), id, args);
290305 }
291306
292- void InvokeEvent(EventID id, const std::unique_ptr<EventArgs>& pArgs)
307+ bool InvokeEvent(EventID id, const std::unique_ptr<EventArgs>& pArgs)
293308 {
294- CallGetEventManager().InvokeEvent(CallGetEventHandler(), id, pArgs);
309+ return CallGetEventManager().InvokeEvent(CallGetEventHandler(), id, pArgs);
295310 }
296311
297312 void PostEvent(EventID id, std::unique_ptr<EventArgs> pArgs = nullptr)
@@ -339,27 +354,27 @@ public:
339354 CallGetEventManager().PostEventOnFutureReady(CallGetEventHandler(), id, std::move(f));
340355 }
341356
342- void SendEvent(EventID id, EventArgs* pArgs = nullptr)
357+ bool SendEvent(EventID id, EventArgs* pArgs = nullptr)
343358 {
344- CallGetEventManager().SendEvent(CallGetEventHandler(), id, pArgs);
359+ return CallGetEventManager().SendEvent(CallGetEventHandler(), id, pArgs);
345360 }
346361
347- void SendEvent(EventID id, EventArgs& args)
362+ bool SendEvent(EventID id, EventArgs& args)
348363 {
349- CallGetEventManager().SendEvent(CallGetEventHandler(), id, args);
364+ return CallGetEventManager().SendEvent(CallGetEventHandler(), id, args);
350365 }
351366
352- void SendEvent(EventID id, EventArgs&& args)
367+ bool SendEvent(EventID id, EventArgs&& args)
353368 {
354- CallGetEventManager().SendEvent(CallGetEventHandler(), id, args);
369+ return CallGetEventManager().SendEvent(CallGetEventHandler(), id, args);
355370 }
356371
357- void SendEvent(EventID id, const std::unique_ptr<EventArgs>& pArgs)
372+ bool SendEvent(EventID id, const std::unique_ptr<EventArgs>& pArgs)
358373 {
359- CallGetEventManager().SendEvent(CallGetEventHandler(), id, pArgs);
374+ return CallGetEventManager().SendEvent(CallGetEventHandler(), id, pArgs);
360375 }
361376
362- std::future<std::unique_ptr<EventArgs>> SendEventAsync(EventID id, std::unique_ptr<EventArgs> pArgs = nullptr)
377+ std::future<std::pair<bool, std::unique_ptr<EventArgs>>> SendEventAsync(EventID id, std::unique_ptr<EventArgs> pArgs = nullptr)
363378 {
364379 return CallGetEventManager().SendEventAsync(CallGetEventHandler(), id, std::move(pArgs));
365380 }
--- a/include/sirius/platform_window.hpp
+++ b/include/sirius/platform_window.hpp
@@ -42,7 +42,7 @@ public:
4242 PlatformWindow& operator=(const PlatformWindow&) = delete;
4343 virtual ~PlatformWindow() = default;
4444
45- virtual void OnEvent(EventID id, EventArgs* pArgs) override;
45+ virtual bool OnEvent(EventID id, EventArgs* pArgs) override;
4646
4747 private:
4848 std::unique_ptr<PlatformWindowBackEnd> m_pBackEnd;
--- a/src/event.cpp
+++ b/src/event.cpp
@@ -12,37 +12,56 @@
1212 namespace Sirius
1313 {
1414
15-void EventManager::InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs* pArgs /* = nullptr */)
15+bool EventManager::InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs* pArgs /* = nullptr */)
1616 {
17+ bool handled = false;
18+
1719 // First, invoke local listeners
1820 if (pTarget)
1921 {
20- pTarget->InvokeEventListeners(pTarget, id, pArgs);
22+ handled = pTarget->CallOnInvokeEvent(pTarget, id, pArgs);
2123 }
2224
2325 // Next, invoke global listeners
24- InvokeEventListeners(pTarget, id, pArgs);
26+ if (!handled)
27+ {
28+ handled = CallOnInvokeEvent(pTarget, id, pArgs);
29+ }
30+
31+ // Then, invoke the handler
32+ if (!handled && pTarget)
33+ {
34+ handled = pTarget->OnEvent(id, pArgs);
35+ }
36+
37+ bool retHandled = false;
2538
26- // Finally, invoke the handler
2739 if (pTarget)
2840 {
29- pTarget->OnEvent(id, pArgs);
41+ retHandled = pTarget->CallOnInvokeEventReturn(pTarget, id, pArgs, handled);
42+ }
43+
44+ if (!retHandled)
45+ {
46+ retHandled = CallOnInvokeEventReturn(pTarget, id, pArgs, handled);
3047 }
48+
49+ return handled || retHandled;
3150 }
3251
33-void EventManager::InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs& args)
52+bool EventManager::InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs& args)
3453 {
35- InvokeEvent(pTarget, id, std::addressof(args));
54+ return InvokeEvent(pTarget, id, std::addressof(args));
3655 }
3756
38-void EventManager::InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs&& args)
57+bool EventManager::InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs&& args)
3958 {
40- InvokeEvent(pTarget, id, std::addressof(args));
59+ return InvokeEvent(pTarget, id, std::addressof(args));
4160 }
4261
43-void EventManager::InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, const std::unique_ptr<EventArgs>& pArgs)
62+bool EventManager::InvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, const std::unique_ptr<EventArgs>& pArgs)
4463 {
45- InvokeEvent(pTarget, id, pArgs.get());
64+ return InvokeEvent(pTarget, id, pArgs.get());
4665 }
4766
4867 void EventManager::PostEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, std::unique_ptr<EventArgs> pArgs /* = nullptr */)
@@ -129,34 +148,34 @@ void EventManager::PostEventOnFutureReady(
129148 m_EventQueueWithFuture.emplace_back(pTarget, id, std::move(f));
130149 }
131150
132-void EventManager::SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs* pArgs /* = nullptr */)
151+bool EventManager::SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs* pArgs /* = nullptr */)
133152 {
134- std::future<std::unique_ptr<EventArgs>> f;
153+ std::future<std::pair<bool, std::unique_ptr<EventArgs>>> f;
135154
136155 {
137156 std::scoped_lock lock(m_MutexForThreadSafeEventQueues);
138157 f = m_ThreadSafeSentEventQueue.emplace_back(pTarget, id, pArgs).Promise.get_future();
139158 }
140159
141- f.get();
160+ return f.get().first;
142161 }
143162
144-void EventManager::SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs& args)
163+bool EventManager::SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs& args)
145164 {
146- SendEvent(pTarget, id, std::addressof(args));
165+ return SendEvent(pTarget, id, std::addressof(args));
147166 }
148167
149-void EventManager::SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs&& args)
168+bool EventManager::SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs&& args)
150169 {
151- SendEvent(pTarget, id, std::addressof(args));
170+ return SendEvent(pTarget, id, std::addressof(args));
152171 }
153172
154-void EventManager::SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, const std::unique_ptr<EventArgs>& pArgs)
173+bool EventManager::SendEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, const std::unique_ptr<EventArgs>& pArgs)
155174 {
156- SendEvent(pTarget, id, pArgs.get());
175+ return SendEvent(pTarget, id, pArgs.get());
157176 }
158177
159-std::future<std::unique_ptr<EventArgs>> EventManager::SendEventAsync(
178+std::future<std::pair<bool, std::unique_ptr<EventArgs>>> EventManager::SendEventAsync(
160179 const std::shared_ptr<EventHandler>& pTarget,
161180 EventID id,
162181 std::unique_ptr<EventArgs> pArgs /* = nullptr */
@@ -230,12 +249,33 @@ void EventListenerManager::RemoveAllEventListeners()
230249 m_Listeners.clear();
231250 }
232251
233-void EventListenerManager::InvokeEventListeners(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs* pArgs)
252+bool EventListenerManager::CallOnInvokeEvent(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs* pArgs)
253+{
254+ return InvokeEventListeners(
255+ [&pTarget, id, pArgs] (auto&& pListener)
256+ {
257+ return pListener->OnInvokeEvent(pTarget, id, pArgs);
258+ }
259+ );
260+}
261+
262+bool EventListenerManager::CallOnInvokeEventReturn(const std::shared_ptr<EventHandler>& pTarget, EventID id, EventArgs* pArgs, bool handled)
263+{
264+ return InvokeEventListeners(
265+ [&pTarget, id, pArgs, handled] (auto&& pListener)
266+ {
267+ return pListener->OnInvokeEventReturn(pTarget, id, pArgs, handled);
268+ }
269+ );
270+}
271+
272+template<typename T>
273+bool EventListenerManager::InvokeEventListeners(T&& invoker)
234274 {
235275 for (auto it = m_Listeners.begin(); it != m_Listeners.end(); ++it)
236276 {
237- bool expired = std::visit(
238- [&pTarget, id, pArgs] (auto&& p)
277+ auto [handled, expired] = std::visit(
278+ [invoker = std::forward<T>(invoker)] (auto&& p)
239279 {
240280 using Type = std::decay_t<decltype(p)>;
241281
@@ -243,18 +283,16 @@ void EventListenerManager::InvokeEventListeners(const std::shared_ptr<EventHandl
243283 {
244284 if (auto pLocked = p.lock())
245285 {
246- pLocked->OnEvent(pTarget, id, pArgs);
247- return false;
286+ return std::pair(invoker(pLocked), false);
248287 }
249288 else
250289 {
251- return true;
290+ return std::pair(false, true);
252291 }
253292 }
254293 else
255294 {
256- p->OnEvent(pTarget, id, pArgs);
257- return false;
295+ return std::pair(invoker(p), false);
258296 }
259297 },
260298 *it
@@ -264,7 +302,14 @@ void EventListenerManager::InvokeEventListeners(const std::shared_ptr<EventHandl
264302 {
265303 it = m_Listeners.erase(it);
266304 }
305+
306+ if (handled)
307+ {
308+ return true;
309+ }
267310 }
311+
312+ return false;
268313 }
269314
270315 void EventManager::RemoveAllEvents()
@@ -434,7 +479,7 @@ void EventManager::ProcessSentEvents()
434479
435480 try
436481 {
437- InvokeEvent(
482+ bool handled = InvokeEvent(
438483 ev.pHandler,
439484 ev.ID,
440485 std::visit(
@@ -454,21 +499,21 @@ void EventManager::ProcessSentEvents()
454499 ev.pArgs
455500 )
456501 );
502+
503+ if (auto ppArgs = std::get_if<std::unique_ptr<EventArgs>>(std::addressof(ev.pArgs)))
504+ {
505+ ev.Promise.set_value({handled, std::move(*ppArgs)});
506+ }
507+ else
508+ {
509+ ev.Promise.set_value({handled, nullptr});
510+ }
457511 }
458512 catch (...)
459513 {
460514 ev.Promise.set_exception(std::current_exception());
461515 continue;
462516 }
463-
464- if (auto ppArgs = std::get_if<std::unique_ptr<EventArgs>>(std::addressof(ev.pArgs)))
465- {
466- ev.Promise.set_value(std::move(*ppArgs));
467- }
468- else
469- {
470- ev.Promise.set_value(nullptr);
471- }
472517 }
473518 }
474519
--- a/src/platform_window.cpp
+++ b/src/platform_window.cpp
@@ -19,9 +19,9 @@ PlatformWindow::PlatformWindow(UIManager& uiManager)
1919 m_pBackEnd->Initialize(this);
2020 }
2121
22-void PlatformWindow::OnEvent(EventID id, EventArgs* pArgs)
22+bool PlatformWindow::OnEvent(EventID id, EventArgs* pArgs)
2323 {
24- Widget::OnEvent(id, pArgs);
24+ return Widget::OnEvent(id, pArgs);
2525 }
2626
2727 } // namespace Sirius