SDEngine
Game Engine
Loading...
Searching...
No Matches
Event.hpp
Go to the documentation of this file.
1// TODO(docs): Add file-level Doxygen header
2// - @file Event.hpp
3// - @brief Event system base types and macros
4// - Event type/category system design
5// - Event macro usage (EVENT_CLASS_TYPE, EVENT_CLASS_CATEGORY)
6#pragma once
7#include <cstddef>
8#include <string>
9#include <type_traits>
10
11#include "SD/core/types.hpp"
12
13namespace sd {
14// TODO(docs): Document BIT() helper
15// - Purpose: Compile-time bit shifting for event categories
16static consteval usize bit(usize idx) {
17 return 1ULL << idx;
18}
19
20
21// TODO(docs): Document EventType enum
22// - All event types and when they're fired
23// - Category relationships
24enum class EventType {
25 // clang-format off
26 NONE = 0,
27 /* Engine */
28 /* Application*/ APP_TICK, APP_UPDATE, APP_RENDER, APP_TERMINATE,
30 WINDOW_LOST_FOCUS, SWAPCHAIN_OUT_OF_DATE, // TODO: swapchain could just be windowresize
31 /* Input */
32 /* Keyboard */ KEY_PRESSED, KEY_RELEASED, KEY_TYPED,
33 /* mouse */ MOUSE_MOVED, MOUSE_SCROLLED,
34 /* mousebutton*/ MOUSE_PRESSED, MOUSE_RELEASED,
35 // clang-format on
36};
49 return static_cast<EventCategory>(static_cast<u16>(a) | static_cast<u16>(b));
50}
51
52inline bool operator&(int lhs, EventCategory rhs) {
53 return (static_cast<u16>(lhs) & static_cast<u16>(rhs)) != 0;
54}
55
56class Event {
57public:
58 virtual ~Event() = default;
59 virtual const char* get_name() const = 0;
60 [[nodiscard]] virtual EventType get_event_type() const = 0;
61 [[nodiscard]] virtual int get_category_flags() const = 0;
62
63 [[nodiscard]] bool is_in_category(EventCategory category) const {
64 return get_category_flags() & category;
65 }
66
67private:
68 bool m_handled = false;
69
70 friend class EventDispatcher;
71 friend class LayerList;
72};
73
74#define EVENT_CLASS_TYPE(type) \
75 static EventType get_static_type() { \
76 return EventType::type; \
77 } \
78 virtual EventType get_event_type() const override { \
79 return get_static_type(); \
80 } \
81 virtual const char* get_name() const override { \
82 return #type; \
83 }
84#define EVENT_CLASS_CATEGORY(category) \
85 virtual int get_category_flags() const override { \
86 return static_cast<int>(category); \
87 }
88} // namespace sd
Definition EventManager.hpp:72
Definition Event.hpp:56
virtual ~Event()=default
bool m_handled
Definition Event.hpp:68
virtual EventType get_event_type() const =0
virtual const char * get_name() const =0
bool is_in_category(EventCategory category) const
Definition Event.hpp:63
virtual int get_category_flags() const =0
Definition LayerList.hpp:21
Definition Application.hpp:28
EventCategory operator|(const EventCategory a, const EventCategory b)
Definition Event.hpp:48
EventType
Definition Event.hpp:24
EventCategory
Definition Event.hpp:37
bool operator&(int lhs, EventCategory rhs)
Definition Event.hpp:52
static consteval usize bit(usize idx)
Definition Event.hpp:16
constexpr T g_type_max
Definition types.hpp:21
std::uint16_t u16
Definition types.hpp:14
std::size_t usize
Definition types.hpp:18