Brenta Engine 1.2
Loading...
Searching...
No Matches
input.cpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#include <brenta/input.hpp>
7#include <brenta/logger.hpp>
8
9using namespace brenta;
10
11//
12// Static variables
13//
14
15std::unordered_map<brenta::Key, Input::KeyboardCallback> Input::keyboard_callbacks;
16std::unordered_map<Input::MouseCallbackId, Input::MouseCallback>
17 Input::mouse_callbacks;
18const std::string Input::subsystem_name = "input";
19bool Input::initialized = false;
20
21//
22// Subsystem interface
23//
24
25std::expected<void, Subsystem::Error> Input::initialize()
26{
27 if (this->is_initialized()) return {};
28
29 Input::initialized = true;
30 INFO("{} initialized", Input::subsystem_name);
31 return {};
32}
33
34std::expected<void, Subsystem::Error> Input::terminate()
35{
36 if (!this->is_initialized()) return {};
37
38 Input::initialized = false;
39 INFO("{}: terminated", Input::subsystem_name);
40 return {};
41}
42
43std::string Input::name()
44{
45 return Input::subsystem_name;
46}
47
48bool Input::is_initialized()
49{
50 return Input::initialized;
51}
52
53//
54// Member functions
55//
56
57Input &Input::instance()
58{
59 static Input _input;
60 return _input;
61}
62
63void Input::add_keyboard_callback(Key key, KeyboardCallback callback)
64{
65 Input::keyboard_callbacks[key] = callback;
66 DEBUG("{}: aded callback for key: {}",
67 Input::subsystem_name, (int) key);
68 return;
69}
70
71void Input::remove_keyboard_callback(Key key)
72{
73 if (Input::keyboard_callbacks.find(key) == Input::keyboard_callbacks.end())
74 {
75 ERROR("{}: no callback found for key: {}", Input::subsystem_name, (int) key);
76 return;
77 }
78
79 Input::keyboard_callbacks.erase(key);
80 DEBUG("{}: removed callback for key: {}", Input::subsystem_name, (int) key);
81 return;
82}
83
84void Input::key_callback(Key key, KeyAction action,
85 [[maybe_unused]] KeyMods mods)
86{
87 EVENT(Logger::Event::Callback, "{}: received key callback",
88 Input::subsystem_name);
89
90 if (action == KeyAction::Press)
91 {
92 if (Input::keyboard_callbacks.find(key) != Input::keyboard_callbacks.end())
93 {
94 Input::keyboard_callbacks.at(key)();
95 }
96 }
97 return;
98}
99
100void Input::add_mouse_callback(MouseCallbackId callback_name,
101 MouseCallback callback)
102{
103 Input::mouse_callbacks[callback_name] = callback;
104 DEBUG("{}: added callback for mouse: {}",
105 Input::subsystem_name, callback_name);
106 return;
107}
108
109void Input::remove_mouse_callback(MouseCallbackId callback_name)
110{
111 if (Input::mouse_callbacks.find(callback_name)
112 == Input::mouse_callbacks.end())
113 {
114 ERROR("{}: no callback found for mouse: {}",
115 Input::subsystem_name, callback_name);
116 return;
117 }
118
119 Input::mouse_callbacks.erase(callback_name);
120 DEBUG("{}: removed callback for mouse: {}",
121 Input::subsystem_name, callback_name);
122 return;
123}
124
125void Input::mouse_callback(double xpos,
126 double ypos)
127{
128 for (auto &callback : Input::mouse_callbacks)
129 {
130 callback.second(xpos, ypos);
131 }
132 return;
133}
134
135//
136// Builder
137//
138
139Subsystem &Input::Builder::build()
140{
141 return Input::instance();
142}