-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyInput.cpp
More file actions
46 lines (39 loc) · 1.26 KB
/
keyInput.cpp
File metadata and controls
46 lines (39 loc) · 1.26 KB
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
#include "KeyInput.hpp"
#include <algorithm>
std::vector<KeyInput*> KeyInput::_instances;
KeyInput::KeyInput(std::vector<int> keysToMonitor) : _isEnabled(true) {
for (int key : keysToMonitor) {
_keys[key] = false;
}
// Add this instance to the list of instances
KeyInput::_instances.push_back(this);
}
KeyInput::~KeyInput() {
// Remove this instance from the list of instances
_instances.erase(std::remove(_instances.begin(), _instances.end(), this), _instances.end());
}
bool KeyInput::getIsKeyDown(int key) {
bool result = false;
if (_isEnabled) {
std::map<int, bool>::iterator it = _keys.find(key);
if (it != _keys.end()) {
result = _keys[key];
}
}
return result;
}
void KeyInput::setIsKeyDown(int key, bool isDown) {
std::map<int, bool>::iterator it = _keys.find(key);
if (it != _keys.end()) {
_keys[key] = isDown;
}
}
void KeyInput::setupKeyInputs(GLFWwindow* window) {
glfwSetKeyCallback(window, KeyInput::callback);
}
void KeyInput::callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
// Send key event to all KeyInput instances
for (KeyInput* keyInput : _instances) {
keyInput->setIsKeyDown(key, action != GLFW_RELEASE);
}
}