-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventHandler.cpp
More file actions
151 lines (121 loc) · 5.03 KB
/
Copy pathEventHandler.cpp
File metadata and controls
151 lines (121 loc) · 5.03 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include "EventHandler.h"
#include "PreprocessorDirectves.h"
#if defined(_DEBUG) && IS_PRINT_TRIGGERED_ACTIONS_TO_CONSOLE == 1
#include "Engine.h"
#endif
////////////////////////////////////////////////////////////
Binding::Binding(std::unique_ptr<Action> t_action, std::unique_ptr<BoundKeys> t_keys) : m_action{ std::move(t_action) }, m_keys{ std::move(t_keys) }{}
////////////////////////////////////////////////////////////
void Binding::changeKeys(std::unique_ptr<BoundKeys> t_keys) { m_keys = std::move(t_keys); }
////////////////////////////////////////////////////////////
bool Binding::hasKey(const sf::Keyboard::Key& t_key)const { return m_keys->hasKey(t_key); }
////////////////////////////////////////////////////////////
void Binding::addKey(const sf::Keyboard::Key& t_key) { m_keys->addKey(t_key); }
////////////////////////////////////////////////////////////
void Binding::removeKey(const sf::Keyboard::Key& t_key) { m_keys->removeKey(t_key); }
////////////////////////////////////////////////////////////
bool Binding::checkMatch(const KeySet& t_pressedKeys)const { return m_keys->checkMatch(t_pressedKeys); }
////////////////////////////////////////////////////////////
void Binding::execute(const EventInfo& t_info) { m_action->execute(t_info); }
////////////////////////////////////////////////////////////
const ActionTrigger& Binding::getTrigger()const { return m_action->getTrigger(); }
////////////////////////////////////////////////////////////
void Binding::replace(Binding&& t_bind) {
m_action.reset();
m_action = std::move(t_bind.m_action);
m_keys.reset();
m_keys = std::move(t_bind.m_keys);
}
////////////////////////////////////////////////////////////
EventHandler::EventHandler() : m_active{}, m_bindings{} {}
////////////////////////////////////////////////////////////
void EventHandler::deleteAction(const ActionId& t_id) {
for (auto& state_it : m_bindings) {
auto action_it{ state_it.second.find(t_id) };
if (action_it == state_it.second.end()) { continue; }
state_it.second.erase(action_it);
break;
}
}
////////////////////////////////////////////////////////////
bool EventHandler::executeAction(const ActionId& t_id, const EventInfo& t_info) {
auto state_it{ m_bindings.find(t_info.m_engineState) };
if (state_it == m_bindings.cend()) { return false; }
auto action_it{ state_it->second.find(t_id) };
if (action_it == state_it->second.cend()) { return false; }
action_it->second.execute(t_info);
return true;
}
////////////////////////////////////////////////////////////
bool EventHandler::hasKey(const ActionId& t_id, const sf::Keyboard::Key& t_key)const {
for (const auto& state_it : m_bindings) {
auto action_it{ state_it.second.find(t_id) };
if (action_it == state_it.second.end()) { continue; }
if (action_it->second.hasKey(t_key)) { return true; }
}
return false;
}
////////////////////////////////////////////////////////////
bool EventHandler::addKey(const ActionId& t_id, const sf::Keyboard::Key& t_key) {
for (auto& state_it : m_bindings) {
auto action_it{ state_it.second.find(t_id) };
if (action_it == state_it.second.end()) { continue; }
action_it->second.addKey(t_key);
return true;
}
return false;
}
////////////////////////////////////////////////////////////
bool EventHandler::removeKey(const ActionId& t_id, const sf::Keyboard::Key& t_key) {
for (auto& state_it : m_bindings) {
auto action_it{ state_it.second.find(t_id) };
if (action_it == state_it.second.end()) { continue; }
action_it->second.removeKey(t_key);
return true;
}
return false;
}
////////////////////////////////////////////////////////////
void EventHandler::addAction(std::unique_ptr<Action> t_action) {
const auto& state{ t_action->getEngineState() };
const auto& id{ t_action->getActionId() };
Binding temp_binding(std::move(t_action), std::make_unique<BoundKeys>());
auto state_it{ m_bindings.find(state) };
if (state_it == m_bindings.end()) {
// Add state, id, and binding
std::unordered_map<ActionId, Binding> temp_map;
temp_map.emplace(id, std::move(temp_binding));
m_bindings.emplace(state, std::move(temp_map));
return;
}
auto action_it{ state_it->second.find(id) };
if (action_it == state_it->second.end()) {
// Add id and binding
state_it->second.emplace(id, std::move(temp_binding));
return;
}
// Replace binding
action_it->second.replace(std::move(temp_binding));
}
////////////////////////////////////////////////////////////
void EventHandler::handleEvent(const EventInfo& t_info) {
const auto& state{ t_info.m_engineState };
const auto& pkeys{ t_info.m_keysCurrentlyPresssed };
const auto& rkeys{ t_info.m_keysCurrentlyBeingReleased };
auto state_it{ m_bindings.find(state) };
if (state_it == m_bindings.end()) { return; }
for (auto& it : state_it->second) {
auto& bind{ it.second };
bool isExecute{ false };
switch (bind.getTrigger()) {
case ActionTrigger::ContinousKeyPress:
isExecute = bind.checkMatch(pkeys); break;
case ActionTrigger::SingleKeyRelease:
isExecute = bind.checkMatch(rkeys); break;
}
if (isExecute) {
bind.execute(t_info);
}
}
}