wren
Vulkan-based game engine
Loading...
Searching...
No Matches
event.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <string_view>
5#include <unordered_map>
6#include <vector>
8
9#include "keycode.hpp"
10
11namespace wren::event {
12
13DESCRIBED_ENUM(Category, WINDOW, KEYBOARD, MOUSE)
14
15// NOLINTNEXTLINE
16#define APPEND_EVENT_INFO(name, cat) \
17 inline static const std::string_view debug_name = name; \
18 inline static const Category category = cat;
19
21 APPEND_EVENT_INFO("WindowClose", Category::WINDOW);
22};
23
25 WindowResized() = default;
26 explicit WindowResized(const std::pair<float, float> &size)
27 : width(size.first), height(size.second) {}
28
29 float width = 0;
30 float height = 0;
31
32 APPEND_EVENT_INFO("WindowResized", Category::WINDOW)
33};
34
35struct MouseMoved {
36 float x = 0;
37 float y = 0;
38 bool relative = false;
39
40 APPEND_EVENT_INFO("MouseMoved", Category::MOUSE)
41};
42
43struct MouseWheel {
44 float x = 0;
45 float y = 0;
46
47 APPEND_EVENT_INFO("MouseWheel", Category::MOUSE)
48};
49
52 APPEND_EVENT_INFO("MouseButtonDown", Category::MOUSE)
53};
54
57 APPEND_EVENT_INFO("MouseButtonUp", Category::MOUSE)
58};
59
60struct KeyDown {
62 APPEND_EVENT_INFO("KeyDown", Category::KEYBOARD)
63};
64
65struct KeyUp {
67 APPEND_EVENT_INFO("KeyUp", Category::KEYBOARD)
68};
69
70struct KeyTyped {
71 std::string_view text;
72 APPEND_EVENT_INFO("KeyTyped", Category::KEYBOARD);
73};
74
76 public:
77 template <typename T>
78 void dispatch(T &&value) const;
79 template <typename T>
80 void on(std::function<void(const T &t)> cb);
81
82 private:
83 std::unordered_map<size_t, std::vector<std::function<void(void *)>>>
85};
86
87template <typename Type>
88// NOLINTNEXTLINE
89void Dispatcher::dispatch(Type &&value) const {
90 const auto id = typeid(Type{}).hash_code();
91 if (handlers_.contains(id)) {
92 for (auto &cb : handlers_.at(id)) {
93 if (cb) cb(&value);
94 }
95 }
96}
97
98template <typename Type>
99void Dispatcher::on(std::function<void(const Type &)> func) {
100 const auto id = typeid(Type{}).hash_code();
101 handlers_[id].push_back([func = std::move(func)](void *value) {
102 func(*static_cast<Type *>(value));
103 });
104}
105
106} // namespace wren::event
Definition event.hpp:75
void dispatch(T &&value) const
std::unordered_map< size_t, std::vector< std::function< void(void *)> > handlers_)
Definition event.hpp:84
void on(std::function< void(const T &t)> cb)
#define DESCRIBED_ENUM(E,...)
Definition enums.hpp:7
#define APPEND_EVENT_INFO(name, cat)
Definition event.hpp:16
Definition event.cpp:3
MouseCode
Definition keycode.hpp:5
KeyCode
Definition keycode.hpp:7
Definition event.hpp:60
KeyCode key
Definition event.hpp:61
Definition event.hpp:70
std::string_view text
Definition event.hpp:71
APPEND_EVENT_INFO("KeyTyped", Category::KEYBOARD)
Definition event.hpp:65
KeyCode key
Definition event.hpp:66
Definition event.hpp:50
MouseCode button
Definition event.hpp:51
Definition event.hpp:55
MouseCode button
Definition event.hpp:56
Definition event.hpp:35
float x
Definition event.hpp:36
bool relative
Definition event.hpp:38
float y
Definition event.hpp:37
Definition event.hpp:43
float y
Definition event.hpp:45
float x
Definition event.hpp:44
Definition event.hpp:20
APPEND_EVENT_INFO("WindowClose", Category::WINDOW)
Definition event.hpp:24
float height
Definition event.hpp:30
WindowResized(const std::pair< float, float > &size)
Definition event.hpp:26
float width
Definition event.hpp:29