OpenShaderDesigner 0.0.1
Loading...
Searching...
No Matches
EventSystem.h
1// =====================================================================================================================
2// OpenShaderDesigner, an open source software utility to create materials and shaders.
3// Copyright (C) 2024 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
19#ifndef ENGINE_EVENTSYSTEM_H
20#define ENGINE_EVENTSYSTEM_H
21
22#include <open-cpp-utils/unique_id.h>
23
24#include <cstdint>
25#include <list>
26#include <mutex>
27
28
29#define MAX_EVENT_TYPES 256
30
31namespace ocu = open_cpp_utils;
32
33namespace OpenShaderDesigner
34{
35
39struct Event
40{
41 template<typename T>
42 static uint8_t TypeOf() { return static_cast<uint8_t>(ocu::unique_id<uint8_t, T>()); }
43
48 virtual inline uint8_t GetID() const = 0;
49};
50
51
56{
57 virtual bool _HandleEvent(const Event* event) = 0;
58
59 friend class EventSystem;
60};
61
66template<typename EventType>
68{
69public:
70 using HandledType = EventType;
71
76 virtual bool HandleEvent(const HandledType* event) = 0;
77private:
78
83 bool _HandleEvent(const Event* event) override;
84};
85
90{
91public:
95 static void PostEvent(const Event*);
96
101 template<typename T>
102 static void RegisterHandler(EventHandler<T>*);
103
108 template<typename T>
110
111private:
112 inline static std::list<_ImplEventHandler*> HandlerMap_[MAX_EVENT_TYPES];
113 inline static std::mutex Lock_;
114
115 EventSystem(const EventSystem&) = delete;
116 EventSystem(EventSystem&&) = delete;
117};
118
119template<typename T>
121{
122 // Thread safe
123 std::lock_guard guard(Lock_);
124 const uint8_t index = T::ID;
125 std::erase(HandlerMap_[index], reinterpret_cast<_ImplEventHandler*>(handler));
126}
127
128template<typename T>
130{
131 // Thread safe
132 std::lock_guard guard(Lock_);
133 const uint8_t index = T::ID;
134 HandlerMap_[index].push_back(reinterpret_cast<_ImplEventHandler*>(handler));
135}
136
137template<typename EventType>
139{
140 if(EventType::ID != event->GetID()) return false;
141 return HandleEvent(reinterpret_cast<const EventType*>(event));
142}
143
144}
145
146#define BeginEvent(EVENT) struct EVENT : OpenShaderDesigner::Event \
147 { \
148 static inline const uint8_t ID = Event::TypeOf<EVENT>(); \
149 inline uint8_t GetID() const override { return ID; }
150
151#define EndEvent };
152
153#endif //ENGINE_EVENTSYSTEM_H
Base EventHandler for abstraction.
Definition EventSystem.h:56
EventHandler interface for creating custom EventHandlers.
Definition EventSystem.h:68
virtual bool HandleEvent(const HandledType *event)=0
Virtual function for custom EventHandler implementations.
EventType HandledType
The type handled by the EventHandler.
Definition EventSystem.h:70
EventSystem for posting Events to be handled.
Definition EventSystem.h:90
static void UnregisterHandler(EventHandler< T > *)
Unregister an EventHandler with the EventSystem.
Definition EventSystem.h:120
static void PostEvent(const Event *)
Post an Event to be Handled.
Definition EventSystem.cpp:26
static void RegisterHandler(EventHandler< T > *)
Register an EventHandler with the EventSystem.
Definition EventSystem.h:129
Base Event class for sending events to the Engine.
Definition EventSystem.h:40
virtual uint8_t GetID() const =0
Get the Event's type ID.