QuasarEngine
Cross-platform 3D engine and editor
Loading...
Searching...
No Matches
EventDispatcher.h
Go to the documentation of this file.
1#pragma once
2
7
9
10#include <concepts>
11#include <functional>
12#include <utility>
13
14namespace Quasar
15{
19 template <typename T>
20 concept EventClass = std::derived_from<T, Event> && requires {
21 { T::GetStaticType() } -> std::same_as<EventType>;
22 };
23
38 {
39 public:
41 explicit EventDispatcher(Event& event) : m_Event(event) {}
42
50 template <EventClass T, std::predicate<T&> Handler>
51 bool Dispatch(Handler&& handler)
52 {
53 if (m_Event.GetType() != T::GetStaticType())
54 {
55 return false;
56 }
57
58 if (std::invoke(std::forward<Handler>(handler), static_cast<T&>(m_Event)))
59 {
60 m_Event.SetHandled(true);
61 }
62 return true;
63 }
64
65 private:
66 // The dispatcher only lives while one event is dispatched, so it can keep a reference to it
67 // NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
68 Event& m_Event;
69 };
70}
Base class of all events, with their types and categories.
EventDispatcher(Event &event)
Creates a dispatcher for an event. The event must stay alive while the dispatcher is used.
Definition EventDispatcher.h:41
bool Dispatch(Handler &&handler)
Calls the handler if the event is of type T.
Definition EventDispatcher.h:51
Base class of all events.
Definition Event.h:95
An event class: a class derived from Event that gives its type with GetStaticType().
Definition EventDispatcher.h:20
Contains all the engine code.
@ T
T key.
Definition KeyCodes.h:63