-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInputManager.cpp
More file actions
61 lines (52 loc) · 1.93 KB
/
Copy pathInputManager.cpp
File metadata and controls
61 lines (52 loc) · 1.93 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "InputManager.h"
namespace Eng {
struct InputManagerImp {
std::function<input_event_t(const input_event_t &)> input_converters[int(eInputEvent::_Count)];
std::deque<input_event_t> input_buffer;
std::vector<bool> keys_state;
};
} // namespace Eng
Eng::InputManager::InputManager() {
imp_ = std::make_unique<InputManagerImp>();
imp_->keys_state.resize(256, false);
}
Eng::InputManager::~InputManager() = default;
const std::vector<bool> &Eng::InputManager::keys_state() const { return imp_->keys_state; }
const std::deque<Eng::input_event_t> &Eng::InputManager::peek_events() const { return imp_->input_buffer; }
void Eng::InputManager::SetConverter(const eInputEvent evt_type,
const std::function<input_event_t(const input_event_t &)> &conv) {
imp_->input_converters[int(evt_type)] = conv;
}
void Eng::InputManager::AddRawInputEvent(input_event_t evt) {
if (imp_->input_buffer.size() > 100) {
return;
}
auto conv = imp_->input_converters[int(evt.type)];
if (conv) {
evt = conv(evt);
}
imp_->input_buffer.push_back(evt);
}
bool Eng::InputManager::PollEvent(const uint64_t time_us, input_event_t &evt) {
if (imp_->input_buffer.empty()) {
return false;
} else {
evt = imp_->input_buffer.front();
if (evt.time_stamp <= time_us) {
imp_->input_buffer.pop_front();
if (evt.type == eInputEvent::KeyDown) {
if (evt.key_code < imp_->keys_state.size()) {
imp_->keys_state[evt.key_code] = true;
}
} else if (evt.type == eInputEvent::KeyUp) {
if (evt.key_code < imp_->keys_state.size()) {
imp_->keys_state[evt.key_code] = false;
}
}
return true;
} else {
return false;
}
}
}
void Eng::InputManager::ClearBuffer() { imp_->input_buffer.clear(); }