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
12#include "krpch.h"
13
14namespace Karma
15{
19 enum class EventType
20 {
21 None = 0,
22 WindowClose, WindowResize, WindowFocus, WindowLostFocus, WindowMoved,
23 AppTick, AppUpdate, AppRender,
24 KeyPressed, KeyReleased, KeyTyped,
25 MouseButtonPressed, MouseButtonReleased, MouseMoved, MouseScrolled,
26 GameControllerConnected, GameControllerDisconnected
27 };
28
69
75#define EVENT_CLASS_TYPE(type) static EventType GetStaticType() { return EventType::type; }\
76 virtual EventType GetEventType() const override { return GetStaticType(); }\
77 virtual const char* GetName() const override { return #type; }
78
84#define EVENT_CLASS_CATEGORY(category) virtual int GetCategoryFlags() const override { return category; }
85
90 {
96 friend class EventDispatcher;
97 public:
103 virtual EventType GetEventType() const = 0;
104
111 virtual const char* GetName() const = 0;
112
118 virtual int GetCategoryFlags() const = 0;
119
125 virtual std::string ToString() const { return GetName(); }
126
132 inline bool IsInCategory(EventCategory category)
133 {
134 return GetCategoryFlags() & category;
135 }
136
142 inline bool IsHandled() const
143 {
144 return m_Handled;
145 }
146
152 inline Event* GetObjPointer() { return this; }
153
154 protected:
155 // If an event has been handled or not. To implement blocking
156 bool m_Handled = false;
157 };
158
163 {
169 template<typename T>
170 using EventFn = std::function<bool(T&)>;
171
172 public:
179 EventDispatcher(Event& event) : m_Event(event)
180 {
181 }
182
209 template<typename T>
210 bool Dispatch(EventFn<T> func)
211 {
212 if (m_Event.GetEventType() == T::GetStaticType())
213 {
214 m_Event.m_Handled = func(*(T*)&m_Event);
215 return true;
216 }
217 return false;
218 }
219
220 private:
221 Event& m_Event;
222 };
223
224 /*
225 inline std::ostream& operator<<(std::ostream& os, const Event& e)
226 {
227 return os << e.ToString();
228 }
229 */
230}
#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:33
@ EventCategoryInput
Events belonging to game inputs. Input device examples include keyboard, mouse, and all that.
Definition Event.h:47
@ EventCategoryKeyboard
Events belonging to game inputs from keyboard (keyboard buttons pressing for instance)
Definition Event.h:52
@ None
Events of the unknown category.
Definition Event.h:37
@ EventCategoryMouse
Events belonging to game movement inputs from mouse.
Definition Event.h:57
@ EventCategoryApplication
Events belonging to application specific processes (resizing and movement for instance)
Definition Event.h:42
@ EventCategoryMouseButton
Events belonging to game button inputs from mouse (mouse buttons pressing for instance)
Definition Event.h:62
@ EventCategoryGameControllerDevice
Events belonging to game controller devices' input.
Definition Event.h:67
EventType
Collection of events used by the Engine.
Definition Event.h:20
bool Dispatch(EventFn< T > func)
Routine for dispatching Events.
Definition Event.h:210
EventDispatcher(Event &event)
A constructor.
Definition Event.h:179
The base class of all the events for Karma.
Definition Event.h:90
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:132
virtual std::string ToString() const
String representation of the event.
Definition Event.h:125
virtual int GetCategoryFlags() const =0
Getter for category flags (enum EventCategory)
Event * GetObjPointer()
Getter for the pointer to the object.
Definition Event.h:152
friend class EventDispatcher
An event dispatching class.
Definition Event.h:96
bool IsHandled() const
Getter for the m_Handled.
Definition Event.h:142
virtual const char * GetName() const =0
Getter for event type.