aboutsummaryrefslogtreecommitdiff
path: root/src/core/event.h
blob: 189120635dfabe4773360139b4ecab93fd021c4f (plain)
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;

        virtual auto get_event_type() const -> EventType = 0;
        virtual auto get_name() const -> const char* = 0;
        virtual auto get_category_flags() const -> int = 0;
        virtual auto to_string() const -> std::string { return get_name(); }

        auto is_in_category(EventCategory category) -> bool { return get_category_flags() & category; }

        bool handled = false;
    };

    #define EVENT_CLASS_TYPE(type)                                                                    \
        static  auto get_static_type() -> EventType               { return EventType::type; }         \
        virtual auto get_event_type() const -> EventType override  { return get_static_type(); }       \
        virtual auto get_name() const -> const char* override      { return #type; }

    #define APPLICATION_EVENT_CLASS_TYPE(type) \
        EVENT_CLASS_TYPE(type)                 \
        virtual auto get_category_flags() const -> int override { return EventCategoryApplication; }

    #define MOUSE_EVENT_CLASS_TYPE(type) \
        EVENT_CLASS_TYPE(type)           \
        virtual auto get_category_flags() const -> int override { return EventCategoryMouse | \
                                                                         EventCategoryInput; }

    #define MOUSE_BUTTON_EVENT_CLASS_TYPE(type) \
        EVENT_CLASS_TYPE(type)                  \
        virtual auto get_category_flags() const -> int override { return EventCategoryMouseButton | \
                                                                         EventCategoryMouse       | \
                                                                         EventCategoryInput; }

    #define KEYBOARD_EVENT_CLASS_TYPE(type) \
        EVENT_CLASS_TYPE(type)              \
        virtual auto get_category_flags() const -> int override { return EventCategoryKeyboard | \
                                                                         EventCategoryInput; }

    class WindowResizeEvent
        : public Event
    {
    public:
        WindowResizeEvent(unsigned int width, unsigned int height)
            : m_width(width), m_height(height) { }

        auto get_width() const -> unsigned int { return m_width; }
        auto get_height() const -> unsigned int { return m_height; }

        auto to_string() const -> std::string 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) { }

        auto get_x() const -> int { return m_x; }
        auto get_y() const -> int { return m_y; }

        auto to_string() const -> std::string 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:
        auto get_key_code() const -> int { return m_key_code; }
        virtual auto get_category_flags() const -> int override { return EventCategoryKeyboard |
                                                                         EventCategoryInput; }

    protected:
        KeyEvent(int keycode)
            : m_key_code(keycode) { }
        int m_key_code;
    };

    class KeyPressedEvent
        : public KeyEvent
    {
    public:
        KeyPressedEvent(int keycode, bool is_repeat = false)
            : KeyEvent(keycode), m_is_repeat(is_repeat) { }

        auto is_repeat() const -> bool { return m_is_repeat; }

        auto to_string() const -> std::string override
        {
            return "KeyPressedEvent: " + std::to_string(m_key_code) +
                   " (repeat = " + std::to_string(m_is_repeat) + ")";
        }

        KEYBOARD_EVENT_CLASS_TYPE(KeyPressed)

    private:
        bool m_is_repeat;
    };

    class KeyReleasedEvent
        : public KeyEvent
    {
    public:
        KeyReleasedEvent(int keycode)
            : KeyEvent(keycode) { }

        auto to_string() const -> std::string override
        {
            return "KeyReleasedEvent: " + std::to_string(m_key_code);
        }

        KEYBOARD_EVENT_CLASS_TYPE(KeyReleased)
    };

    class KeyTypedEvent
        : public KeyEvent
    {
    public:
        KeyTypedEvent(int keycode)
            : KeyEvent(keycode) { }

        auto to_string() const -> std::string override
        {
            return "KeyTypedEvent: " + std::to_string(m_key_code);
        }

        KEYBOARD_EVENT_CLASS_TYPE(KeyTyped)
    };

    class MouseMovedEvent
        : public Event
    {
    public:
        MouseMovedEvent(float x, float y)
            : m_mouse_x(x), m_mouse_y(y) { }

        auto get_x() const -> float { return m_mouse_x; }
        auto get_y() const -> float { return m_mouse_y; }

        auto to_string() const -> std::string override
        {
            return "MouseMovedEvent: " + std::to_string(m_mouse_x) + ", " + std::to_string(m_mouse_y);
        }

        MOUSE_EVENT_CLASS_TYPE(MouseMoved)

    private:
        float m_mouse_x, m_mouse_y;
    };

    class MouseScrolledEvent
        : public Event
    {
    public:
        MouseScrolledEvent(float x_offset, float y_offset)
            : m_x_offset(x_offset), m_y_offset(y_offset) { }

        auto get_x_offset() const -> float { return m_x_offset; }
        auto get_y_offset() const -> float { return m_y_offset; }

        auto to_string() const -> std::string override
        {
            return "MouseScrolledEvent: " + std::to_string(m_x_offset) +
                   ", " + std::to_string(m_y_offset);
        }

        MOUSE_EVENT_CLASS_TYPE(MouseScrolled)

    private:
        float m_x_offset, m_y_offset;
    };

    class MouseButtonEvent
        : public Event
    {
    public:
        auto get_mouse_button() const -> int { return m_button; }
        virtual auto get_category_flags() const -> int 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) { }

        auto to_string() const -> std::string override
        {
            return "MouseButtonPressedEvent: " + std::to_string(m_button);
        }

        MOUSE_BUTTON_EVENT_CLASS_TYPE(MouseButtonPressed)
    };

    class MouseButtonReleasedEvent
        : public MouseButtonEvent
    {
    public:
        MouseButtonReleasedEvent(int button)
            : MouseButtonEvent(button) { }

        auto to_string() const -> std::string 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>
        auto dispatch(const F& func) -> bool
        {
            if (m_event.get_event_type() == T::get_static_type())
            {
                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;

        auto set_event_callback(const EventCallbackFn& callback) -> void
        {
            m_event_callback = callback;
        }

        auto on_event(Event& event) -> void
        {
            if (m_event_callback)
                m_event_callback(event);
        }

        template<typename T>
        auto bind_event(const std::function<void(T&)>& callback) -> void
        {
            m_event_callback = [callback](Event& event)
            {
                if (event.get_event_type() == T::get_static_type())
                    callback(static_cast<T&>(event));
            };
        }

    private:
        EventCallbackFn m_event_callback;
    };

    #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
}