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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
#pragma once
#include <functional>
#include <unordered_map>
#include <vector>
#include <string>
#include <sstream>
namespace Donut
{
class Event;
class EventDispatcher;
enum class EventType
{
None = 0,
WindowClose,
WindowResize,
WindowFocus,
WindowLostFocus,
WindowMoved,
KeyPressed,
KeyReleased,
KeyTyped,
MouseButtonPressed,
MouseButtonReleased,
MouseMoved,
MouseScrolled
};
enum EventCategory
{
None = 0,
EventCategoryApplication = 1 << 0,
EventCategoryInput = 1 << 1,
EventCategoryKeyboard = 1 << 2,
EventCategoryMouse = 1 << 3,
EventCategoryMouseButton = 1 << 4
};
class Event
{
public:
virtual ~Event() = default;
bool Handled = false;
virtual EventType GetEventType() const = 0;
virtual const char* GetName() const = 0;
virtual int GetCategoryFlags() const = 0;
virtual std::string ToString() const { return GetName(); }
bool IsInCategory(EventCategory category) { return GetCategoryFlags() & category; }
};
#define EVENT_CLASS_TYPE(type) \
static EventType GetStaticType() { return EventType::type; } \
virtual EventType GetEventType() const override { return GetStaticType(); } \
virtual const char* GetName() const override { return #type; }
#define APPLICATION_EVENT_CLASS_TYPE(type) \
EVENT_CLASS_TYPE(type) \
virtual int GetCategoryFlags() const override { return EventCategoryApplication; }
#define MOUSE_EVENT_CLASS_TYPE(type) \
EVENT_CLASS_TYPE(type) \
virtual int GetCategoryFlags() const override { return EventCategoryMouse | \
EventCategoryInput; }
#define MOUSE_BUTTON_EVENT_CLASS_TYPE(type) \
EVENT_CLASS_TYPE(type) \
virtual int GetCategoryFlags() const override { return EventCategoryMouseButton | \
EventCategoryMouse | \
EventCategoryInput; }
#define KEYBOARD_EVENT_CLASS_TYPE(type) \
EVENT_CLASS_TYPE(type) \
virtual int GetCategoryFlags() const override { return EventCategoryKeyboard | \
EventCategoryInput; }
class WindowResizeEvent
: public Event
{
public:
WindowResizeEvent(unsigned int width, unsigned int height)
: m_Width(width), m_Height(height) { }
unsigned int GetWidth() const { return m_Width; }
unsigned int GetHeight() const { return m_Height; }
std::string ToString() const override
{
return "WindowResizeEvent: " + std::to_string(m_Width) + ", " + std::to_string(m_Height);
}
APPLICATION_EVENT_CLASS_TYPE(WindowResize)
private:
unsigned int m_Width, m_Height;
};
class WindowCloseEvent
: public Event
{
public:
WindowCloseEvent() = default;
APPLICATION_EVENT_CLASS_TYPE(WindowClose)
};
class WindowFocusEvent
: public Event
{
public:
WindowFocusEvent() = default;
APPLICATION_EVENT_CLASS_TYPE(WindowFocus)
};
class WindowLostFocusEvent
: public Event
{
public:
WindowLostFocusEvent() = default;
APPLICATION_EVENT_CLASS_TYPE(WindowLostFocus)
};
class WindowMovedEvent
: public Event
{
public:
WindowMovedEvent(int x, int y)
: m_X(x), m_Y(y) { }
int GetX() const { return m_X; }
int GetY() const { return m_Y; }
std::string ToString() const override
{
return "WindowMovedEvent: " + std::to_string(m_X) + ", " + std::to_string(m_Y);
}
APPLICATION_EVENT_CLASS_TYPE(WindowMoved)
private:
int m_X, m_Y;
};
class KeyEvent
: public Event
{
public:
int GetKeyCode() const { return m_KeyCode; }
virtual int GetCategoryFlags() const override { return EventCategoryKeyboard |
EventCategoryInput; }
protected:
KeyEvent(int keycode)
: m_KeyCode(keycode) { }
int m_KeyCode;
};
class KeyPressedEvent
: public KeyEvent
{
public:
KeyPressedEvent(int keycode, bool isRepeat = false)
: KeyEvent(keycode), m_IsRepeat(isRepeat) { }
bool IsRepeat() const { return m_IsRepeat; }
std::string ToString() const override
{
return "KeyPressedEvent: " + std::to_string(m_KeyCode) +
" (repeat = " + std::to_string(m_IsRepeat) + ")";
}
KEYBOARD_EVENT_CLASS_TYPE(KeyPressed)
private:
bool m_IsRepeat;
};
class KeyReleasedEvent
: public KeyEvent
{
public:
KeyReleasedEvent(int keycode)
: KeyEvent(keycode) { }
std::string ToString() const override
{
return "KeyReleasedEvent: " + std::to_string(m_KeyCode);
}
KEYBOARD_EVENT_CLASS_TYPE(KeyReleased)
};
class KeyTypedEvent
: public KeyEvent
{
public:
KeyTypedEvent(int keycode)
: KeyEvent(keycode) { }
std::string ToString() const override
{
return "KeyTypedEvent: " + std::to_string(m_KeyCode);
}
KEYBOARD_EVENT_CLASS_TYPE(KeyTyped)
};
class MouseMovedEvent
: public Event
{
public:
MouseMovedEvent(float x, float y)
: m_MouseX(x), m_MouseY(y) { }
float GetX() const { return m_MouseX; }
float GetY() const { return m_MouseY; }
std::string ToString() const override
{
return "MouseMovedEvent: " + std::to_string(m_MouseX) + ", " + std::to_string(m_MouseY);
}
MOUSE_EVENT_CLASS_TYPE(MouseMoved)
private:
float m_MouseX, m_MouseY;
};
class MouseScrolledEvent
: public Event
{
public:
MouseScrolledEvent(float xOffset, float yOffset)
: m_XOffset(xOffset), m_YOffset(yOffset) { }
float GetXOffset() const { return m_XOffset; }
float GetYOffset() const { return m_YOffset; }
std::string ToString() const override
{
return "MouseScrolledEvent: " + std::to_string(m_XOffset) +
", " + std::to_string(m_YOffset);
}
MOUSE_EVENT_CLASS_TYPE(MouseScrolled)
private:
float m_XOffset, m_YOffset;
};
class MouseButtonEvent
: public Event
{
public:
int GetMouseButton() const { return m_Button; }
virtual int GetCategoryFlags() const override { return EventCategoryMouseButton |
EventCategoryMouse |
EventCategoryInput; }
protected:
MouseButtonEvent(int button)
: m_Button(button) { }
int m_Button;
};
class MouseButtonPressedEvent
: public MouseButtonEvent
{
public:
MouseButtonPressedEvent(int button)
: MouseButtonEvent(button) { }
std::string ToString() const override
{
return "MouseButtonPressedEvent: " + std::to_string(m_Button);
}
MOUSE_BUTTON_EVENT_CLASS_TYPE(MouseButtonPressed)
};
class MouseButtonReleasedEvent
: public MouseButtonEvent
{
public:
MouseButtonReleasedEvent(int button)
: MouseButtonEvent(button) { }
std::string ToString() const override
{
return "MouseButtonReleasedEvent: " + std::to_string(m_Button);
}
MOUSE_BUTTON_EVENT_CLASS_TYPE(MouseButtonReleased)
};
class EventDispatcher
{
public:
EventDispatcher(Event& event)
: m_Event(event) { }
template<typename T, typename F>
bool Dispatch(const F& func)
{
if (m_Event.GetEventType() == T::GetStaticType())
{
m_Event.Handled = func(static_cast<T&>(m_Event));
return true;
}
return false;
}
private:
Event& m_Event;
};
class EventHandler
{
public:
using EventCallbackFn = std::function<void(Event&)>;
EventHandler() = default;
~EventHandler() = default;
void SetEventCallback(const EventCallbackFn& callback)
{
m_EventCallback = callback;
}
void OnEvent(Event& event)
{
if (m_EventCallback)
m_EventCallback(event);
}
template<typename T>
void BindEvent(const std::function<void(T&)>& callback)
{
m_EventCallback = [callback](Event& event)
{
if (event.GetEventType() == T::GetStaticType())
callback(static_cast<T&>(event));
};
}
private:
EventCallbackFn m_EventCallback;
};
#undef EVENT_CLASS_TYPE
#undef APPLICATION_EVENT_CLASS_TYPE
#undef MOUSE_EVENT_CLASS_TYPE
#undef MOUSE_BUTTON_EVENT_CLASS_TYPE
#undef KEYBOARD_EVENT_CLASS_TYPE
}
|