-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.hpp
More file actions
198 lines (162 loc) · 6.11 KB
/
event.hpp
File metadata and controls
198 lines (162 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#ifndef INPUT_EVENT_H
#define INPUT_EVENT_H
#include <Nostalgia/fwd/events.hpp>
#include <Nostalgia/events/bindings.hpp>
#define NEW_EVENT(NAME) inline static constinit const char* NAME{#NAME};
#define EVENT_TYPE(TYPE) constexpr EventType Type() const noexcept final { return TYPE; }
#define EVENT_LOG std::string DebugLog() const noexcept
/**
* `Highest, Lowest`: What they say on the tins.
*
* `P0, P1, P2, ...`: The higher the number, the lower the priority (`P0` is one step below `Highest`).
**/
enum class EventPriority : unsigned int
{
Highest = 0,
P0, P1, P2,
Lowest
};
enum class EventType : unsigned int
{
InputEvent,
WindowEvent,
};
class IEvent
{
public:
virtual ~IEvent() noexcept = default;
static constexpr EventPriority Priority() noexcept { return EventPriority::Lowest; }
virtual constexpr EventType Type() const noexcept = 0;
virtual constexpr bool IsEvent(Sarg inEventName) const noexcept { return false; }
virtual std::string DebugLog() const noexcept { return "[NO LOG]"; }
};
template<EventPriority _Priority = EventPriority::Lowest>
class CEvent : public IEvent
{
public:
static constexpr EventPriority Priority() noexcept { return _Priority; }
};
class WindowEvent : public CEvent<EventPriority::Highest>
{
public:
NEW_EVENT(WindowClose)
NEW_EVENT(WindowResize)
NEW_EVENT(WindowMoved)
EVENT_TYPE(EventType::WindowEvent)
EVENT_LOG final { return "WindowEvent: " + mName; }
constexpr WindowEvent(Sarg inName):
mName{inName} {}
constexpr bool IsEvent(Sarg inEventName) const noexcept final
{ return mName == inEventName; }
constexpr Sarg Name() const noexcept
{ return mName; }
protected:
std::string mName{};
};
class InputEvent : public CEvent<EventPriority::P0>
{
public:
EVENT_TYPE(EventType::InputEvent)
EVENT_LOG override { return GetDebugLog(); }
// All
virtual size_t GetHash() const;
virtual std::string GetDebugLog() const;
// InputEventMouseScroll
virtual bool IsMouseScroll() const;
virtual Farg<Position2D> ScrollOffset() const;
// InputEventMouseMotion
virtual bool IsMouseMotion() const;
virtual bool IsStoppedMouseMotion() const;
virtual Farg<Position2D> MousePosition() const;
virtual Farg<Position2D> LastMousePosition() const;
virtual Farg<Motion2D> MouseMotion() const;
// InputEventAction
virtual bool IsInputAction() const;
virtual bool IsAction(Farg<std::string>) const;
virtual bool IsActive(Farg<std::string>) const;
virtual bool IsJustChanged(Farg<std::string>) const;
// InputEventBinding
virtual bool IsInputBinding() const;
virtual bool IsBinding(KeyID) const;
virtual bool IsRepeated(KeyID) const;
virtual bool IsPressed(KeyID) const;
virtual bool IsReleased(KeyID) const;
virtual bool IsJustPressed(KeyID) const;
virtual bool IsJustReleased(KeyID) const;
virtual bool IsModifierActive(Key::Modifier) const;
virtual Key::Modifiers GetModifiers() const;
protected:
static void sPrintMouseWarning(const char* inFunction);
static Position2D empty_position;
static Motion2D empty_motion;
};
class InputEventMouseScroll final : public InputEvent
{
public:
InputEventMouseScroll();
InputEventMouseScroll(Farg<Position2D> inScrollOffset);
std::string GetDebugLog() const final { return std::format("InputEventMouseScroll - scroll offset: [{}, {}]", mScrollOffset.x(), mScrollOffset.y()); }
Farg<Position2D> ScrollOffset() const final;
private:
Position2D mScrollOffset{};
};
class InputEventMouseMotion final : public InputEvent
{
public:
InputEventMouseMotion();
InputEventMouseMotion(Farg<Position2D> inCurrentPos, Farg<Position2D> inLastPos);
size_t GetHash() const final;
std::string GetDebugLog() const final { return std::format("InputEventMouseMotion - mouse pos: [{}, {}], last pos: [{}, {}], motion: [{}, {}]", mMousePosition.x(), mMousePosition.y(), mLastMousePosition.x(), mLastMousePosition.y(), mMouseMotion.x(), mMouseMotion.y()); }
bool IsMouseMotion() const final;
bool IsStoppedMouseMotion() const final;
Farg<Position2D> MousePosition() const final;
Farg<Position2D> LastMousePosition() const final;
Farg<Motion2D> MouseMotion() const final;
private:
Position2D mMousePosition{};
Position2D mLastMousePosition{};
Motion2D mMouseMotion{};
};
class InputEventAction final : public InputEvent
{
public:
InputEventAction(Farg<InputAction> inAction);
size_t GetHash() const final;
std::string GetDebugLog() const final { return std::format("InputEventAction - action: {}, active: {}, changed: {}", mAction, mActive, mJustChanged); }
bool IsInputAction() const final { return true; }
bool IsAction(Farg<std::string>) const final;
bool IsActive(Farg<std::string>) const final;
bool IsJustChanged(Farg<std::string>) const final;
private:
std::string mAction{""};
bool mActive{true};
bool mJustChanged{false};
};
class InputEventBinding final : public InputEvent
{
public:
InputEventBinding(KeyID inBindingID, Key::Modifiers inModifiers, bool isPressed, bool isRepeated = false, bool isJustChanged = false);
size_t GetHash() const final;
std::string GetDebugLog() const final { return std::format("InputEventBinding - key: {}, pressed: {}, changed: {}", debug_GetKeyName(mID), mPressed, mJustChanged); }
bool IsInputBinding() const final { return true; }
bool IsBinding(KeyID) const final;
bool IsPressed(KeyID) const final;
bool IsRepeated(KeyID) const final;
bool IsReleased(KeyID) const final;
bool IsJustPressed(KeyID) const final;
bool IsJustReleased(KeyID) const final;
bool IsModifierActive(Key::Modifier) const final;
virtual Key::Modifiers GetModifiers() const final;
private:
KeyID mID{};
Key::Modifiers mModifiers{};
bool mPressed{true};
bool mRepeated{false};
bool mJustChanged{false};
};
template<typename T>
concept is_event = std::derived_from<T,IEvent> && !(std::is_same_v<T,IEvent>);
#undef NEW_EVENT
#undef EVENT_TYPE
#undef EVENT_LOG
#endif // INPUT_EVENT_H