PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
Event.hpp
1#pragma once
2
3#include "PixelBullet/Math/Bitwise.hpp"
4
5#include <functional>
6#include <ostream>
7#include <string>
8
9#define PB_BIND_EVENT_FN(fn) \
10 [this](auto&&... args) -> decltype(auto) { return this->fn(std::forward<decltype(args)>(args)...); }
11
12namespace PixelBullet
13{
14 enum class EventType
15 {
16 None = 0,
17 WindowClose,
18 WindowResize,
19 WindowFocus,
20 WindowLostFocus,
21 WindowMoved,
22 AppTick,
23 AppUpdate,
24 AppRender,
25 KeyPressed,
26 KeyReleased,
27 KeyTyped,
28 MouseButtonPressed,
29 MouseButtonReleased,
30 MouseMoved,
31 MouseScrolled
32 };
33
34 enum EventCategory
35 {
36 None = 0,
37 EventCategoryApplication = BIT(0),
38 EventCategoryInput = BIT(1),
39 EventCategoryKeyboard = BIT(2),
40 EventCategoryMouse = BIT(3),
41 EventCategoryMouseButton = BIT(4)
42 };
43
44#define EVENT_CLASS_TYPE(type) \
45 static EventType GetStaticType() \
46 { \
47 return EventType::type; \
48 } \
49 virtual EventType GetEventType() const override \
50 { \
51 return GetStaticType(); \
52 } \
53 virtual const char* GetName() const override \
54 { \
55 return #type; \
56 }
57
58#define EVENT_CLASS_CATEGORY(category) \
59 virtual int GetCategoryFlags() const override \
60 { \
61 return category; \
62 }
63
64 class Event
65 {
66 public:
67 virtual ~Event() = default;
68
69 bool Handled = false;
70
71 virtual EventType GetEventType() const = 0;
72 virtual const char* GetName() const = 0;
73 virtual int GetCategoryFlags() const = 0;
74 virtual std::string ToString() const
75 {
76 return GetName();
77 }
78
79 bool IsInCategory(EventCategory category) const;
80 };
81
83 {
84 public:
85 explicit EventDispatcher(Event& event);
86
87 template <typename T, typename F>
88 bool Dispatch(const F& func)
89 {
90 if (m_Event.GetEventType() == T::GetStaticType())
91 {
92 m_Event.Handled |= func(static_cast<T&>(m_Event));
93 return true;
94 }
95 return false;
96 }
97
98 private:
99 Event& m_Event;
100 };
101
102 std::ostream& operator<<(std::ostream& os, const Event& e);
103
104} // namespace PixelBullet
Definition Event.hpp:83
Definition Event.hpp:65