KarmaEngine
Game Engine for practical learning and research purposes
Loading...
Searching...
No Matches
Event.h
Go to the documentation of this file.
1
10#pragma once
11
12namespace Karma
13{
17 enum class EventType
18 {
19 None = 0,
20 WindowClose, WindowResize, WindowFocus, WindowLostFocus, WindowMoved,
21 AppTick, AppUpdate, AppRender,
22 KeyPressed, KeyReleased, KeyTyped,
23 MouseButtonPressed, MouseButtonReleased, MouseMoved, MouseScrolled,
24 GameControllerConnected, GameControllerDisconnected
25 };
26
67
73#define EVENT_CLASS_TYPE(type) static EventType GetStaticType() { return EventType::type; }\
74 virtual EventType GetEventType() const override { return GetStaticType(); }\
75 virtual const char* GetName() const override { return #type; }
76
82#define EVENT_CLASS_CATEGORY(category) virtual int GetCategoryFlags() const override { return category; }
83
88 {
94 friend class EventDispatcher;
95 public:
101 virtual EventType GetEventType() const = 0;
102
109 virtual const char* GetName() const = 0;
110
116 virtual int GetCategoryFlags() const = 0;
117
123 virtual std::string ToString() const { return GetName(); }
124
130 inline bool IsInCategory(EventCategory category)
131 {
132 return GetCategoryFlags() & category;
133 }
134
140 inline bool IsHandled() const
141 {
142 return m_Handled;
143 }
144
150 inline Event* GetObjPointer() { return this; }
151
152 protected:
153 // If an event has been handled or not. To implement blocking
154 bool m_Handled = false;
155 };
156
161 {
167 template<typename T>
168 using EventFn = std::function<bool(T&)>;
169
170 public:
177 EventDispatcher(Event& event) : m_Event(event)
178 {
179 }
180
207 template<typename T>
208 bool Dispatch(EventFn<T> func)
209 {
210 if (m_Event.GetEventType() == T::GetStaticType())
211 {
212 m_Event.m_Handled = func(*(T*)&m_Event);
213 return true;
214 }
215 return false;
216 }
217
218 private:
219 Event& m_Event;
220 };
221
222 /*
223 inline std::ostream& operator<<(std::ostream& os, const Event& e)
224 {
225 return os << e.ToString();
226 }
227 */
228}
#define KARMA_API
Defining Karma's API macro for storage class information.
Definition Core.h:41
#define BIT(x)
Macro for bit-shift operation (left).
Definition Core.h:52
EventCategory
Classification of the above events.
Definition Event.h:31
@ EventCategoryInput
Events belonging to game inputs. Input device examples include keyboard, mouse, and all that.
Definition Event.h:45
@ EventCategoryKeyboard
Events belonging to game inputs from keyboard (keyboard buttons pressing for instance).
Definition Event.h:50
@ None
Events of the unknown category.
Definition Event.h:35
@ EventCategoryMouse
Events belonging to game movement inputs from mouse.
Definition Event.h:55
@ EventCategoryApplication
Events belonging to application specific processes (resizing and movement for instance).
Definition Event.h:40
@ EventCategoryMouseButton
Events belonging to game button inputs from mouse (mouse buttons pressing for instance).
Definition Event.h:60
@ EventCategoryGameControllerDevice
Events belonging to game controller devices' input.
Definition Event.h:65
EventType
Collection of events used by the Engine.
Definition Event.h:18
bool Dispatch(EventFn< T > func)
Routine for dispatching Events.
Definition Event.h:208
EventDispatcher(Event &event)
A constructor.
Definition Event.h:177
The base class of all the events for Karma.
Definition Event.h:88
virtual EventType GetEventType() const =0
Getter for event name (type).
bool IsInCategory(EventCategory category)
Routine to see if the category of the Event is (or a part of) given category.
Definition Event.h:130
virtual std::string ToString() const
String representation of the event.
Definition Event.h:123
virtual int GetCategoryFlags() const =0
Getter for category flags (enum EventCategory).
Event * GetObjPointer()
Getter for the pointer to the object.
Definition Event.h:150
friend class EventDispatcher
An event dispatching class.
Definition Event.h:94
bool IsHandled() const
Getter for the m_Handled.
Definition Event.h:140
virtual const char * GetName() const =0
Getter for event type.