QuasarEngine
Cross-platform 3D engine and editor
Toggle main menu visibility
Loading...
Searching...
No Matches
EventDispatcher.h
Go to the documentation of this file.
1
#pragma once
2
7
8
#include <
quasar/events/Event.h
>
9
10
#include <concepts>
11
#include <functional>
12
#include <utility>
13
14
namespace
Quasar
15
{
19
template
<
typename
T>
20
concept
EventClass
= std::derived_from<T, Event> &&
requires
{
21
{ T::GetStaticType() } -> std::same_as<EventType>;
22
};
23
37
class
EventDispatcher
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
}
Event.h
Base class of all events, with their types and categories.
Quasar::EventDispatcher::EventDispatcher
EventDispatcher(Event &event)
Creates a dispatcher for an event. The event must stay alive while the dispatcher is used.
Definition
EventDispatcher.h:41
Quasar::EventDispatcher::Dispatch
bool Dispatch(Handler &&handler)
Calls the handler if the event is of type T.
Definition
EventDispatcher.h:51
Quasar::Event
Base class of all events.
Definition
Event.h:95
Quasar::EventClass
An event class: a class derived from Event that gives its type with GetStaticType().
Definition
EventDispatcher.h:20
Quasar
Contains all the engine code.
Quasar::Key::T
@ T
T key.
Definition
KeyCodes.h:63
quasar
events
EventDispatcher.h
Generated by
1.18.0