-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventManager.cpp
106 lines (97 loc) · 3.4 KB
/
EventManager.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "EventManager.hpp"
EventManager::EventManager():
m_hasFocus(true) {
LoadBindings();
}
EventManager::~EventManager() {
for (auto &itr : m_bindings) {
delete itr.second;
itr.second = nullptr;
}
}
bool EventManager::AddBinding(Binding* l_binding) {
if (m_bindings.find(l_binding->m_name) != m_bindings.end())
return false;
return
m_bindings.emplace(
l_binding->m_name,
l_binding
).second;
}
bool EventManager::RemoveBinding(std::string l_name) {
auto itr = m_bindings.find(l_name);
if (itr == m_bindings.end()) {
return false;
}
delete itr->second;
m_bindings.erase(itr);
return true;
}
void EventManager::HandleEvent(sf::Event& l_event) {
for (auto &b_itr : m_bindings) {
Binding* bind = b_itr.second;
for (auto &e_itr : bind->m_events) {
EventType sfmlEvent = (EventType)l_event.type;
if (e_itr.first !=sfmlEvent) {
continue;
}
if (sfmlEvent == EventType::KeyDown ||
sfmlEvent == EventType::KeyUp) {
if (e_itr.second.m_code == l_event.key.code) {
if (bind->m_details.m_keyCode != -1) {
bind->m_details.m_keyCode = e_itr.second.m_code;
}
++(bind->c);
break;
}
} else if (sfmlEvent == EventType::MButtonDown ||
sfmlEvent == EventType::MButtonUp) {
if (e_itr.second.m_code == l_event.mouseButton.button) {
bind->m_details.m_mouse.x = l_event.mouseButton.x;
bind->m_details.m_mouse.y = l_event.mouseButton.y;
if (bind->m_details.m_keyCode != -1) {
bind->m_details.m_keyCode = e_itr.second.m_code;
}
++(bind->c);
break;
}
} else {
if (sfmlEvent == EventType::MouseWheel) {
bind->m_details.m_mouseWheelDelta = l_event.mouseWheel.delta;
} else if (sfmlEvent == EventType::WindowResized) {
bind->m_details.m_size.x = l_event.size.width;
bind->m_details.m_size.y = l_event.size.height;
}
}
}
}
}
void EventManager::LoadBindings(){
std::string delimiter = ":";
std::ifstream bindings;
bindings.open("keys.cfg");
if (!bindings.is_open()){ std::cout << "! Failed loading keys.cfg." << std::endl; return; }
std::string line;
while (std::getline(bindings, line)){
std::stringstream keystream(line);
std::string callbackName;
keystream >> callbackName;
Binding* bind = new Binding(callbackName);
while (!keystream.eof()){
std::string keyval;
keystream >> keyval;
int start = 0;
std::string::size_type end = keyval.find(delimiter);
if (end == std::string::npos){ delete bind; bind = nullptr; break; }
EventType type = EventType(stoi(keyval.substr(start, end - start)));
int code = stoi(keyval.substr(end + delimiter.length(),
keyval.find(delimiter, end + delimiter.length())));
EventInfo eventInfo;
eventInfo.m_code = code;
bind->BindEvent(type, eventInfo);
}
if (!AddBinding(bind)){ delete bind; }
bind = nullptr;
}
bindings.close();
}