Unravel Engine C++ Reference
Loading...
Searching...
No Matches
keyboard_action_map.cpp
Go to the documentation of this file.
2
3namespace input
4{
5// ----------------------------------------------------------------------------
6auto keyboard_action_map::get_analog_value(const action_id_t& action, const keyboard& device) const -> float
7{
8 const auto& find = entries_by_action_id_.find(action);
9
10 if(find == entries_by_action_id_.end())
11 {
12 return 0.0f;
13 }
14
15 const auto& entries = find->second;
16
17 for(const auto& entry : entries)
18 {
19 if(device.is_down(entry.key))
20 {
21 bool modifiers_down = true;
22 for(auto modifier : entry.modifiers)
23 {
24 if(!device.is_down(modifier))
25 {
26 modifiers_down = false;
27 break;
28 }
29 }
30
31 if(modifiers_down)
32 {
33 return entry.analog_value;
34 }
35 }
36 }
37
38 return 0.0f;
39}
40
41// ----------------------------------------------------------------------------
42auto keyboard_action_map::get_digital_value(const action_id_t& action, const keyboard& device) const -> bool
43{
44 const auto& find = entries_by_action_id_.find(action);
45
46 if(find == entries_by_action_id_.end())
47 {
48 return false;
49 }
50
51 const auto& entries = find->second;
52
53 for(const auto& entry : entries)
54 {
55 if(device.is_down(entry.key))
56 {
57 bool modifiers_down = true;
58 for(auto modifier : entry.modifiers)
59 {
60 if(!device.is_down(modifier))
61 {
62 modifiers_down = false;
63 break;
64 }
65 }
66
67 if(modifiers_down)
68 {
69 return true;
70 }
71 }
72 }
73
74 return false;
75}
76
77// ----------------------------------------------------------------------------
78auto keyboard_action_map::is_pressed(const action_id_t& action, const keyboard& device) const -> bool
79{
80 const auto& find = entries_by_action_id_.find(action);
81
82 if(find == entries_by_action_id_.end())
83 {
84 return false;
85 }
86
87 const auto& entries = find->second;
88
89 for(const auto& entry : entries)
90 {
91 if(device.is_pressed(entry.key))
92 {
93 bool modifiers_down = true;
94 for(auto modifier : entry.modifiers)
95 {
96 if(!device.is_down(modifier))
97 {
98 modifiers_down = false;
99 break;
100 }
101 }
102
103 if(modifiers_down)
104 {
105 return true;
106 }
107 }
108 }
109
110 return false;
111}
112
113auto keyboard_action_map::is_released(const action_id_t& action, const keyboard& device) const -> bool
114{
115 const auto& find = entries_by_action_id_.find(action);
116
117 if(find == entries_by_action_id_.end())
118 {
119 return false;
120 }
121
122 const auto& entries = find->second;
123
124 for(const auto& entry : entries)
125 {
126 if(device.is_released(entry.key))
127 {
128 bool modifiers_down = true;
129 for(auto modifier : entry.modifiers)
130 {
131 if(!device.is_down(modifier))
132 {
133 modifiers_down = false;
134 break;
135 }
136 }
137
138 if(modifiers_down)
139 {
140 return true;
141 }
142 }
143 }
144
145 return false;
146}
147
148auto keyboard_action_map::is_down(const action_id_t& action, const keyboard& device) const -> bool
149{
150 const auto& find = entries_by_action_id_.find(action);
151
152 if(find == entries_by_action_id_.end())
153 {
154 return false;
155 }
156
157 const auto& entries = find->second;
158
159 for(const auto& entry : entries)
160 {
161 if(device.is_down(entry.key))
162 {
163 bool modifiers_down = true;
164 for(auto modifier : entry.modifiers)
165 {
166 if(!device.is_down(modifier))
167 {
168 modifiers_down = false;
169 break;
170 }
171 }
172
173 if(modifiers_down)
174 {
175 return true;
176 }
177 }
178 }
179
180 return false;
181}
182
183// ----------------------------------------------------------------------------
184void keyboard_action_map::map(const action_id_t& action, key_code key, float analog_value)
185{
186 map(action, key, {}, analog_value);
187}
188
189void keyboard_action_map::map(const action_id_t& action, key_code key, const std::vector<key_code>& modifiers, float analog_value)
190{
191 key_entry entry = {};
192 entry.key = key;
193 entry.modifiers = modifiers;
194 entry.analog_value = analog_value;
195
196 entries_by_action_id_[action].push_back(entry);
197}
198
199} // namespace input
auto get_analog_value(const action_id_t &action, const keyboard &device) const -> float
auto is_pressed(const action_id_t &action, const keyboard &device) const -> bool
std::map< action_id_t, std::vector< key_entry > > entries_by_action_id_
auto is_down(const action_id_t &action, const keyboard &device) const -> bool
auto get_digital_value(const action_id_t &action, const keyboard &device) const -> bool
auto is_released(const action_id_t &action, const keyboard &device) const -> bool
void map(const action_id_t &action, key_code key, float analog_value=1.0f)
key_code
Definition key.hpp:6
std::string action_id_t
Definition action_id.hpp:6