Elevate Engine 1
Loading...
Searching...
No Matches
Event.h
Go to the documentation of this file.
1#pragma once
2
4#include <string>
5#include <cstdint>
6#include <ostream>
7#include <functional>
8
9namespace Elevate {
19
30
31#define EVENT_CLASS_TYPE(type) static EventType GetStaticType() { return EventType::type; }\
32 virtual EventType GetEventType() const override { return GetStaticType(); }\
33 virtual const char* GetName() const override { return #type; }
34
35#define EVENT_CLASS_CATEGORY(category) virtual int GetCategoryFlags() const override { return category; }
36
37 class Event
38 {
39 friend class EventDispatcher;
40 public:
41 bool Handled = false;
42
43 virtual EventType GetEventType() const = 0;
44 virtual const char* GetName() const = 0;
45 virtual int GetCategoryFlags() const = 0;
46 virtual std::string ToString() const { return GetName(); }
47
48 inline bool IsInCategory(EventCategory category)
49 {
50 return GetCategoryFlags() & category;
51 }
52 protected:
53 bool m_Handled = false;
54 };
55
57 {
58 template<typename T>
59 using EventFn = std::function<bool(T&)>;
60 public:
62 : m_Event(event)
63 {
64 }
65
66 template<typename T>
67 bool Dispatch(EventFn<T> func)
68 {
69 if (m_Event.GetEventType() == T::GetStaticType())
70 {
71 m_Event.m_Handled = func(*(T*)&m_Event);
72 return true;
73 }
74 return false;
75 }
76 private:
77 Event& m_Event;
78 };
79
80 inline std::ostream& operator<<(std::ostream& os, const Event& e)
81 {
82 return os << e.ToString();
83 }
84}
#define BIT(x)
Definition Core.h:43
bool Dispatch(EventFn< T > func)
Definition Event.h:67
EventDispatcher(Event &event)
Definition Event.h:61
bool IsInCategory(EventCategory category)
Definition Event.h:48
virtual std::string ToString() const
Definition Event.h:46
virtual EventType GetEventType() const =0
virtual const char * GetName() const =0
bool Handled
Definition Event.h:41
virtual int GetCategoryFlags() const =0
bool m_Handled
Definition Event.h:53
EventType
Definition Event.h:11
std::ostream & operator<<(std::ostream &os, const Event &e)
Definition Event.h:80
EventCategory
Definition Event.h:21
@ EventCategoryApplication
Definition Event.h:23
@ EventCategoryGameContext
Definition Event.h:28
@ EventCategoryMouse
Definition Event.h:26
@ EventCategoryInput
Definition Event.h:24
@ EventCategoryKeyboard
Definition Event.h:25
@ EventCategoryMouseButton
Definition Event.h:27
@ None
Definition Event.h:22