Foray Library
rapid prototyping framework for crossplatform development of vulkan hardware ray tracing applications
Loading...
Searching...
No Matches
foray_callbackdispatcher.hpp
Go to the documentation of this file.
1#pragma once
2#include "../base/foray_framerenderinfo.hpp"
3#include "../foray_logger.hpp"
4#include "../osi/foray_osi_declares.hpp"
5#include "foray_component.hpp"
6#include <map>
7#include <vector>
8
9namespace foray::scene {
12 {
13 public:
14 friend Registry;
15
16 virtual void InvokeUpdate(SceneUpdateInfo& updateInfo);
17 virtual void InvokeDraw(SceneDrawInfo& renderInfo);
18 virtual void InvokeOnEvent(const osi::Event* event);
19 virtual void InvokeOnResized(VkExtent2D event);
20
21 protected:
22 template <typename TCallback, bool Ordered = TCallback::ORDERED_EXECUTION>
24 {
25 std::vector<TCallback*> Listeners = {};
26
27 inline void Invoke(typename TCallback::TArg arg);
28 inline void Add(TCallback* callback);
29 inline bool Remove(TCallback* callback);
30 };
31
32 template <typename TCallback>
33 struct CallbackVector<TCallback, true>
34 {
35 std::multimap<int32_t, TCallback*> Listeners = {};
36
37 inline void Invoke(typename TCallback::TArg arg);
38 inline void Add(TCallback* callback);
39 inline bool Remove(TCallback* callback);
40 };
41
46 };
47
48 template <typename TCallback, bool Ordered>
50 {
51 for(auto callback : Listeners)
52 {
53 callback->Invoke(arg);
54 }
55 }
56
57 template <typename TCallback, bool Ordered>
59 {
60 Listeners.push_back(callback);
61 }
62
63 template <typename TCallback, bool Ordered>
65 {
66 auto iter = std::find(Listeners.cbegin(), Listeners.cend(), callback);
67 if(iter != Listeners.cend())
68 {
69 Listeners.erase(iter);
70 return true;
71 }
72 return false;
73 }
74
75 template <typename TCallback>
77 {
78 for(auto callback : Listeners)
79 {
80 callback.second->Invoke(arg);
81 }
82 }
83
84 template <typename TCallback>
86 {
87 Listeners.emplace(callback->GetOrder(), callback);
88 }
89
90 template <typename TCallback>
92 {
93 auto range = Listeners.equal_range(callback->GetOrder());
94 for(auto iter = range.first; iter != range.second; ++iter)
95 {
96 if(iter->second == callback)
97 {
98 Listeners.erase(iter);
99 return true;
100 }
101 }
102 return false;
103 }
104
105} // namespace foray::scene
Base class for operating system events.
Definition foray_event.hpp:13
Type maintaining callback lists for event distribution.
Definition foray_callbackdispatcher.hpp:12
virtual void InvokeDraw(SceneDrawInfo &renderInfo)
friend Registry
Definition foray_callbackdispatcher.hpp:14
CallbackVector< Component::DrawCallback > mDraw
Definition foray_callbackdispatcher.hpp:44
CallbackVector< Component::OnResizedCallback > mOnResized
Definition foray_callbackdispatcher.hpp:45
virtual void InvokeUpdate(SceneUpdateInfo &updateInfo)
CallbackVector< Component::UpdateCallback > mUpdate
Definition foray_callbackdispatcher.hpp:43
CallbackVector< Component::OnEventCallback > mOnEvent
Definition foray_callbackdispatcher.hpp:42
virtual void InvokeOnEvent(const osi::Event *event)
virtual void InvokeOnResized(VkExtent2D event)
Definition foray_animation.hpp:8
Definition foray_callbackdispatcher.hpp:24
std::vector< TCallback * > Listeners
Definition foray_callbackdispatcher.hpp:25
bool Remove(TCallback *callback)
Definition foray_callbackdispatcher.hpp:64
void Add(TCallback *callback)
Definition foray_callbackdispatcher.hpp:58
void Invoke(typename TCallback::TArg arg)
Definition foray_callbackdispatcher.hpp:49
Temporary type passed to components when drawing the scene.
Definition foray_scenedrawing.hpp:63
Temporary type passed to components when updating the scene.
Definition foray_scenedrawing.hpp:49