Brenta Engine 1.2
Loading...
Searching...
No Matches
input.hpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#pragma once
7
8#include <brenta/subsystem.hpp>
9#include <brenta/key.hpp>
10
11#include <functional>
12#include <string>
13#include <unordered_map>
14#include <expected>
15
16namespace brenta
17{
18
19//
20// Input subsystem
21// ----------------
22//
23// This subsystem is used to add and remove callbacks for keyboard and
24// mouse events. You can define your own callbacks and register them
25// using `add_keyboard_callback(..)` and `add_mouse_callback(..)`.
26//
27class Input : public Subsystem
28{
29public:
30
31 using MouseCallbackId = std::string;
32 using MouseCallback = std::function<void(double, double)>;
33 using KeyboardCallback = std::function<void()>;
34
35 class Builder;
36
37 // Subsystem interface
38 static const std::string subsystem_name;
39 std::expected<void, Subsystem::Error> initialize() override;
40 std::expected<void, Subsystem::Error> terminate() override;
41 std::string name() override;
42 bool is_initialized() override;
43
44 // Member functions
45
46 static Input &instance();
47
48 static void add_keyboard_callback(Key key, KeyboardCallback callback);
49 static void remove_keyboard_callback(Key key);
50
51 static void add_mouse_callback(MouseCallbackId name, MouseCallback callback);
52 static void remove_mouse_callback(MouseCallbackId name);
53
54 // These functions will be called when a key or mouse event is
55 // received. They are used to call the other registered callbacks.
56 static void key_callback(Key key, KeyAction action, KeyMods mods);
57 static void mouse_callback(double xpos, double ypos);
58
59protected:
60
61 static bool initialized;
62 static std::unordered_map<brenta::Key, KeyboardCallback> keyboard_callbacks;
63 static std::unordered_map<MouseCallbackId, MouseCallback> mouse_callbacks;
64
65 // Private constructors / destructors for singleton
66 Input() = default;
67 ~Input() = default;
68
69};
70
72{
73public:
74
75 Builder() = default;
76 ~Builder() = default;
77
78 brenta::Subsystem &build() override;
79};
80
81} // namespace brenta