Karma Engine
Loading...
Searching...
No Matches
Event.h
1#pragma once
2
3#include "krpch.h"
4
5namespace Karma
6{
7 enum class EventType
8 {
9 None = 0,
10 WindowClose, WindowResize, WindowFocus, WindowLostFocus, WindowMoved,
11 AppTick, AppUpdate, AppRender,
12 KeyPressed, KeyReleased, KeyTyped,
13 MouseButtonPressed, MouseButtonReleased, MouseMoved, MouseScrolled,
14 GameControllerConnected, GameControllerDisconnected
15 };
16
17 enum EventCategory
18 {
19 None = 0,
20 EventCategoryApplication = BIT(0),
21 EventCategoryInput = BIT(1),
22 EventCategoryKeyboard = BIT(2),
23 EventCategoryMouse = BIT(3),
24 EventCategoryMouseButton = BIT(4),
25 EventCategoryGameControllerDevice = BIT(5)
26 };
27
28#define EVENT_CLASS_TYPE(type) static EventType GetStaticType() { return EventType::type; }\
29 virtual EventType GetEventType() const override { return GetStaticType(); }\
30 virtual const char* GetName() const override { return #type; }
31
32#define EVENT_CLASS_CATEGORY(category) virtual int GetCategoryFlags() const override { return category; }
33
34 class KARMA_API Event
35 {
36 friend class EventDispatcher;
37 public:
38 virtual EventType GetEventType() const = 0;
39 virtual const char* GetName() const = 0;
40 virtual int GetCategoryFlags() const = 0;
41 virtual std::string ToString() const { return GetName(); }
42
43 inline bool IsInCategory(EventCategory category)
44 {
45 return GetCategoryFlags() & category;
46 }
47
48 inline bool IsHandled() const
49 {
50 return m_Handled;
51 }
52
53 inline Event* GetObjPointer() { return this; }
54
55 protected:
56 // If an event has been handled or not. To implement blocking
57 bool m_Handled = false;
58 };
59
61 {
62 template<typename T>
63 using EventFn = std::function<bool(T&)>;
64 public:
65 EventDispatcher(Event& event) : m_Event(event)
66 {
67 }
68
69 template<typename T>
70 bool Dispatch(EventFn<T> func)
71 {
72 if (m_Event.GetEventType() == T::GetStaticType())
73 {
74 m_Event.m_Handled = func(*(T*)&m_Event);
75 return true;
76 }
77 return false;
78 }
79
80 private:
81 Event& m_Event;
82 };
83
84 /*
85 inline std::ostream& operator<<(std::ostream& os, const Event& e)
86 {
87 return os << e.ToString();
88 }
89 */
90}
Definition Event.h:61
Definition Event.h:35
@ None
Definition KarmaTypes.h:33