QuasarEngine
Cross-platform 3D engine and editor
Loading...
Searching...
No Matches
Event.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <quasar/core/Base.h>
9
10#include <cstdint>
11#include <string>
12#include <type_traits>
13
14namespace Quasar
15{
31
33 enum class EventCategory : std::uint8_t
34 {
35 None = 0,
36 Window = Bit(0),
37 Input = Bit(1),
39 Mouse = Bit(3)
40 };
41
44 {
45 using Underlying = std::underlying_type_t<EventCategory>;
46 return static_cast<EventCategory>(static_cast<Underlying>(left) | static_cast<Underlying>(right));
47 }
48
51 {
52 using Underlying = std::underlying_type_t<EventCategory>;
53 return static_cast<EventCategory>(static_cast<Underlying>(left) & static_cast<Underlying>(right));
54 }
55
57 constexpr const char* GetEventTypeName(EventType type)
58 {
59 switch (type)
60 {
62 return "WindowClose";
64 return "WindowResize";
66 return "WindowFocus";
68 return "WindowMinimize";
70 return "KeyPressed";
72 return "KeyReleased";
74 return "KeyTyped";
76 return "MouseButtonPressed";
78 return "MouseButtonReleased";
80 return "MouseMoved";
82 return "MouseScrolled";
83 }
84 return "Unknown";
85 }
86
94 class Event
95 {
96 public:
97 virtual ~Event() = default;
98
100 virtual EventType GetType() const = 0;
101
103 virtual EventCategory GetCategories() const = 0;
104
109 virtual std::string ToString() const { return GetName(); }
110
112 const char* GetName() const { return GetEventTypeName(GetType()); }
113
115 bool IsInCategory(EventCategory category) const { return (GetCategories() & category) != EventCategory::None; }
116
118 bool IsHandled() const { return m_Handled; }
119
121 void SetHandled(bool handled) { m_Handled = handled; }
122
123 protected:
128 Event() = default;
129
131 Event(const Event&) = default;
132 Event& operator=(const Event&) = default;
133 Event(Event&&) = default;
134 Event& operator=(Event&&) = default;
136
137 private:
138 bool m_Handled = false;
139 };
140
146 template <EventType Type, EventCategory Categories>
147 class EventOf : public Event
148 {
149 public:
151 static constexpr EventType GetStaticType() { return Type; }
152
154 EventType GetType() const override { return Type; }
155
157 EventCategory GetCategories() const override { return Categories; }
158 };
159
161 inline std::string format_as(const Event& event)
162 {
163 return event.ToString();
164 }
165}
Contains the base definitions and utilities for the Quasar engine.
Base class of each event class, which gives the event its type and its categories.
Definition Event.h:148
static constexpr EventType GetStaticType()
Returns the type of this event class.
Definition Event.h:151
EventType GetType() const override
Returns the type of the event.
Definition Event.h:154
EventCategory GetCategories() const override
Returns all the categories of the event.
Definition Event.h:157
Base class of all events.
Definition Event.h:95
virtual EventType GetType() const =0
Returns the type of the event.
void SetHandled(bool handled)
Marks the event as handled, or not handled.
Definition Event.h:121
virtual std::string ToString() const
Returns a readable description of the event, for debugging.
Definition Event.h:109
const char * GetName() const
Returns the name of the type of the event, for example "KeyPressed".
Definition Event.h:112
virtual EventCategory GetCategories() const =0
Returns all the categories of the event.
bool IsInCategory(EventCategory category) const
Returns true if the event belongs to the given category.
Definition Event.h:115
bool IsHandled() const
Returns true if the event was handled.
Definition Event.h:118
Event()=default
Creates an event that is not handled.
Application window.
Definition Window.h:42
Functions that read the current state of the keyboard and the mouse, as an alternative to events.
Definition Input.h:22
Contains all the engine code.
constexpr std::uint32_t Bit(std::uint32_t index)
Returns a value where only the bit at the given index is set, for example Bit(3) is 8.
Definition Base.h:100
constexpr EventCategory operator|(EventCategory left, EventCategory right)
Combines two sets of categories.
Definition Event.h:43
std::string format_as(UUID uuid)
Lets the log macros print a UUID with {}, using UUID::ToString().
Definition UUID.h:66
EventCategory
Categories of an event. An event can belong to several categories, combined with the | operator.
Definition Event.h:34
@ None
No category.
Definition Event.h:35
@ Keyboard
Events from the keyboard.
Definition Event.h:38
@ Mouse
Events from the mouse.
Definition Event.h:39
constexpr const char * GetEventTypeName(EventType type)
Returns the name of an event type, for example "KeyPressed".
Definition Event.h:57
constexpr EventCategory operator&(EventCategory left, EventCategory right)
Keeps only the categories found in both sets.
Definition Event.h:50
EventType
Type of an event. Each event class has exactly one type.
Definition Event.h:18
@ WindowFocus
The window gained or lost the focus.
Definition Event.h:21
@ KeyReleased
A key was released.
Definition Event.h:24
@ MouseScrolled
The mouse wheel or the touchpad scrolled.
Definition Event.h:29
@ MouseButtonPressed
A mouse button was pressed.
Definition Event.h:26
@ MouseButtonReleased
A mouse button was released.
Definition Event.h:27
@ KeyTyped
A character was typed, for text input.
Definition Event.h:25
@ WindowResize
The size of the window changed.
Definition Event.h:20
@ KeyPressed
A key was pressed, or is repeating while held down.
Definition Event.h:23
@ MouseMoved
The cursor moved.
Definition Event.h:28
@ WindowClose
The user asked to close the window.
Definition Event.h:19
@ WindowMinimize
The window was minimized or restored.
Definition Event.h:22