GlistEngine
Loading...
Searching...
No Matches
gEvent.h
Go to the documentation of this file.
1/*
2* gEvent.h
3*
4* Created on: June 24, 2023
5* Author: Metehan Gezer
6*/
7
8#ifndef GEVENT_H_
9#define GEVENT_H_
10
11#include <functional>
12
13#define BIT(x) (1 << x)
25#undef BIT
26
27/*
28 * Events are more future-proof, in case anything new is added to an event
29 * developers doesn't have to update their function parameters.
30 */
31
32class gEvent {
33public:
34 virtual ~gEvent() = default;
35
36 bool ishandled = false;
37
38 virtual const char* getEventName() const = 0;
39 virtual std::size_t getEventType() const = 0;
40 virtual int getCategoryFlags() const = 0;
41
42 bool isInCategory(EventCategory category) {
43 return getCategoryFlags() & category;
44 }
45};
46
47typedef std::function<void(gEvent&)> EventHandlerFn;
48
67public:
69 : event(event) {
70 }
71
72 // F will be deduced by the compiler
73 template<typename T, typename F>
74 bool dispatch(const F& func) {
75 if (event.getEventType() == T::getStaticType()) {
76 event.ishandled |= func(*static_cast<T*>(&event));
77 return true;
78 }
79 return false;
80 }
81
82private:
83 gEvent& event;
84};
85
86#define G_EVENT_CLASS_TYPE(type) \
87 static size_t getStaticType() { return typeid(type).hash_code(); } \
88 virtual size_t getEventType() const override { return getStaticType(); } \
89 virtual const char* getEventName() const override { return #type; }
90
91#define G_EVENT_CLASS_CATEGORY(category) \
92 virtual int getCategoryFlags() const override { return category; }
93
94
95// This macro can be used instead of std::bind function call.
96// G_BIND_FUNCTION can be used to bind class methods. While G_BIND_GLOBAL_FUNCTION can be used to bind global functions without a class.
97#define G_BIND_FUNCTION(fn) [this](auto&&... args) -> decltype(auto) { return this->fn(std::forward<decltype(args)>(args)...); }
98#define G_BIND_GLOBAL_FUNCTION(fn) [](auto&&... args) -> decltype(auto) { return fn(std::forward<decltype(args)>(args)...); }
99
100#endif /* GEVENT_H_ */
Definition gEvent.h:66
gEventDispatcher(gEvent &event)
Definition gEvent.h:68
bool dispatch(const F &func)
Definition gEvent.h:74
Definition gEvent.h:32
virtual int getCategoryFlags() const =0
bool isInCategory(EventCategory category)
Definition gEvent.h:42
bool ishandled
Definition gEvent.h:36
virtual ~gEvent()=default
virtual std::size_t getEventType() const =0
virtual const char * getEventName() const =0
std::function< void(gEvent &)> EventHandlerFn
Definition gEvent.h:47
EventCategory
Definition gEvent.h:14
@ EVENTCATEGORY_KEYBOARD
Definition gEvent.h:17
@ EVENTCATEGORY_CUSTOM
Definition gEvent.h:23
@ EVENTCATEGORY_JOYSTICK
Definition gEvent.h:20
@ EVENTCATEGORY_TOUCHSCREEN
Definition gEvent.h:21
@ EVENTCATEGORY_APP
Definition gEvent.h:15
@ EVENTCATEGORY_MOUSE
Definition gEvent.h:18
@ EVENTCATEGORY_RENDERER
Definition gEvent.h:22
@ EVENTCATEGORY_MOUSE_BUTTON
Definition gEvent.h:19
@ EVENTCATEGORY_INPUT
Definition gEvent.h:16
#define BIT(x)
Definition gEvent.h:13