PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
event.h
1#pragma once
2
3#include "pixelbullet/core/bits.h"
4
5#include <functional>
6#include <ostream>
7#include <string>
8#include <utility>
9
10#define PB_BIND_EVENT_FN(fn) [this](auto&&... args) -> decltype(auto) { return this->fn(std::forward<decltype(args)>(args)...); }
11
12namespace pixelbullet
13{
14enum 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
34enum 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
44enum class EventResult
45{
46 Ignored,
47 Handled,
48 Consumed
49};
50
51#define EVENT_CLASS_TYPE(type) \
52 static EventType GetStaticType() \
53 { \
54 return EventType::type; \
55 } \
56 virtual EventType GetEventType() const override \
57 { \
58 return GetStaticType(); \
59 } \
60 virtual const char* GetName() const override \
61 { \
62 return #type; \
63 }
64
65#define EVENT_CLASS_CATEGORY(category) \
66 virtual int GetCategoryFlags() const override \
67 { \
68 return category; \
69 }
70
71class Event
72{
73public:
74 virtual ~Event() = default;
75
76 virtual EventType GetEventType() const = 0;
77 virtual const char* GetName() const = 0;
78 virtual int GetCategoryFlags() const = 0;
79 virtual std::string ToString() const
80 {
81 return GetName();
82 }
83
84 bool IsInCategory(EventCategory category) const;
85};
86
88{
89public:
90 explicit EventDispatcher(Event& event);
91
92 template <typename T, typename F>
93 EventResult Dispatch(const F& func)
94 {
95 if (event_.GetEventType() == T::GetStaticType())
96 {
97 return func(static_cast<T&>(event_));
98 }
99 return EventResult::Ignored;
100 }
101
102private:
103 Event& event_;
104};
105
106std::ostream& operator<<(std::ostream& os, const Event& e);
107
108} // namespace pixelbullet
Definition event.h:88
Definition event.h:72