CoriEngine
Loading...
Searching...
No Matches
Event.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace Cori {
4 namespace Core {
14
18 class Event {
19 friend class EventDispatcher;
20 public:
21 virtual ~Event() = default;
22
28 [[nodiscard]] virtual constexpr std::type_index GetEventType() const = 0;
29
30
36 [[nodiscard]] virtual const char* GetName() const = 0;
37
43 [[nodiscard]] virtual uint32_t GetCategoryFlags() const = 0;
44
45
51 [[nodiscard]] virtual std::string ToString() const { return GetName(); }
52
53
59 [[nodiscard]] bool IsInCategory(const EventCategory category) const {
60 return GetCategoryFlags() & category;
61 }
62
63 bool m_Handled = false;
64 };
65
70 template<typename T>
71 using EventFn = std::function<bool(T&)>;
72 public:
73 explicit EventDispatcher(Event& event)
74 : m_Event(event) {
75 }
76
84 template<typename T>
85 bool Dispatch(EventFn<T> func) {
86 if (m_Event.GetEventType() == std::type_index(typeid(T))) {
87 m_Event.m_Handled = func(*static_cast<T*>(&m_Event));
88 return true;
89 }
90
91
92 return false;
93 }
94 private:
95 Event& m_Event;
96 };
97
101 inline std::ostream& operator<<(std::ostream& os, const Event& e) {
102 return os << e.ToString();
103 }
104
108 inline std::string format_as(const Event& e) {
109 return std::string(e.ToString());
110 }
111
112 using EventCallbackFn = std::function<void(Event&)>;
113 }
114}
115
116
124#define EVENT_CLASS_TYPE(type) constexpr std::type_index GetEventType() const override { return std::type_index(typeid(type)); }\
125 const char* GetName() const override { return CORI_CLEAN_TYPE_NAME(type); }
126
132#define EVENT_CLASS_CATEGORY(category) uint32_t GetCategoryFlags() const override { return category; }
bool Dispatch(EventFn< T > func)
Binds dispatch function that will handle a specific Event type.
Definition Event.hpp:85
EventDispatcher(Event &event)
Definition Event.hpp:73
An abstract class that is ment to be used as a template for defining events.
Definition Event.hpp:18
virtual std::string ToString() const
Returns the name of the Event.
Definition Event.hpp:51
virtual const char * GetName() const =0
This will give you the string version of EventType.
bool IsInCategory(const EventCategory category) const
Checks if the Event is in specific category.
Definition Event.hpp:59
virtual constexpr std::type_index GetEventType() const =0
Gives the return type of the Event.
virtual ~Event()=default
virtual uint32_t GetCategoryFlags() const =0
Gives the flags of particular Event.
friend class EventDispatcher
Definition Event.hpp:19
Core systems of the engine are here.
std::function< void(Event &)> EventCallbackFn
Definition Event.hpp:112
EventCategory
Definition Event.hpp:5
@ EventCategoryMouse
Definition Event.hpp:11
@ EventCategoryKeyboard
Definition Event.hpp:10
@ EventCategoryInput
Definition Event.hpp:9
@ EventCategoryGameplay
Definition Event.hpp:8
@ EventCategoryApplication
Definition Event.hpp:7
@ EventCategoryMouseButton
Definition Event.hpp:12
std::ostream & operator<<(std::ostream &os, const Event &e)
Needed for CoriError.
Definition Event.hpp:101
std::string format_as(const Event &e)
Needed for fmt/spadlog.
Definition Event.hpp:108
Global engine namespace.