Unravel Engine C++ Reference
Loading...
Searching...
No Matches
gamepad_action_map.cpp
Go to the documentation of this file.
2
3namespace input
4{
5// ----------------------------------------------------------------------------
6namespace
7{
8auto get_axis_value(const float value,
9 const axis_range range,
10 const float min_analog_value,
11 const float max_analog_value) -> float
12{
13 float v{};
14
15 switch(range)
16 {
18 v = value;
19 break;
21 v = value > 0.0f ? value : 0.0f;
22 break;
24 v = value < 0.0f ? value : 0.0f;
25 break;
26 default:
27 v = value;
28 break;
29 }
30
31 const float in_start = -1.0f;
32 const float in_end = 1.0f;
33
34 return (v - in_start) / (in_end - in_start) * (max_analog_value - min_analog_value) + min_analog_value;
35}
36} // namespace
37// ----------------------------------------------------------------------------
38auto gamepad_action_map::get_analog_value(const action_id_t& action, const gamepad& device) const -> float
39{
40 const auto& find = entries_by_action_id_.find(action);
41
42 if(find == entries_by_action_id_.end())
43 {
44 return 0.0f;
45 }
46
47 const auto& entries = find->second;
48
49 for(const auto& entry : entries)
50 {
51 switch(entry.type)
52 {
54 {
55 const float value = get_axis_value(device.get_axis_value(entry.value),
56 entry.range,
57 entry.min_analog_value,
58 entry.max_analog_value);
59
60 if(epsilon_not_equal(value, 0.0f))
61 {
62 return value;
63 }
64 }
65 break;
66
68 {
69 const button_state state = device.get_button_state(entry.value);
70 const float value = button_state_to_analog_value(state);
71 if(epsilon_not_equal(value, 0.0f))
72 {
73 return value;
74 }
75 }
76 break;
77 default:
78 break;
79 }
80 }
81
82 return 0.0f;
83}
84
85// ----------------------------------------------------------------------------
86auto gamepad_action_map::get_digital_value(const action_id_t& action, const gamepad& device) const -> bool
87{
88 const auto& find = entries_by_action_id_.find(action);
89
90 if(find == entries_by_action_id_.end())
91 {
92 return false;
93 }
94
95 const auto& entries = find->second;
96
97 for(const auto& entry : entries)
98 {
99 switch(entry.type)
100 {
101 case input_type::axis:
102 {
103 const float value = get_axis_value(device.get_axis_value(entry.value),
104 entry.range,
105 entry.min_analog_value,
106 entry.max_analog_value);
107
108 if(epsilon_not_equal(value, 0.0f))
109 {
110 return true;
111 }
112 }
113 break;
114
116 {
117 const button_state state = device.get_button_state(entry.value);
118 const bool value = button_state_to_digital_value(state);
119 if(value)
120 {
121 return true;
122 }
123 }
124 break;
125 default:
126 break;
127 }
128 }
129
130 return false;
131}
132
133// ----------------------------------------------------------------------------
134auto gamepad_action_map::is_pressed(const action_id_t& action, const gamepad& device) const -> bool
135{
136 const auto& find = entries_by_action_id_.find(action);
137
138 if(find == entries_by_action_id_.end())
139 {
140 return false;
141 }
142
143 const auto& entries = find->second;
144
145 for(const auto& entry : entries)
146 {
147 switch(entry.type)
148 {
149 case input_type::axis:
150 {
151 }
152 break;
153
155 {
156 const button_state state = device.get_button_state(entry.value);
157 return state == button_state::pressed;
158 }
159 break;
160
161 default:
162 break;
163 }
164 }
165
166 return false;
167}
168
169auto gamepad_action_map::is_released(const action_id_t& action, const gamepad& device) const -> bool
170{
171 const auto& find = entries_by_action_id_.find(action);
172
173 if(find == entries_by_action_id_.end())
174 {
175 return false;
176 }
177
178 const auto& entries = find->second;
179
180 for(const auto& entry : entries)
181 {
182 switch(entry.type)
183 {
184 case input_type::axis:
185 {
186 }
187 break;
188
190 {
191 const button_state state = device.get_button_state(entry.value);
192 return state == button_state::released;
193 }
194 break;
195
196 default:
197 break;
198 }
199 }
200
201 return false;
202}
203
204auto gamepad_action_map::is_down(const action_id_t& action, const gamepad& device) const -> bool
205{
206 const auto& find = entries_by_action_id_.find(action);
207
208 if(find == entries_by_action_id_.end())
209 {
210 return false;
211 }
212
213 const auto& entries = find->second;
214
215 for(const auto& entry : entries)
216 {
217 switch(entry.type)
218 {
219 case input_type::axis:
220 {
221 return false;
222 }
223 break;
224
226 {
227 const button_state state = device.get_button_state(entry.value);
228 return state == button_state::down;
229 }
230 break;
231
232 default:
233 break;
234 }
235 }
236
237 return false;
238}
239
240// ----------------------------------------------------------------------------
244 float min_analog_value,
245 float max_analog_value)
246{
247 gamepad_entry entry = {};
248 entry.type = input_type::axis;
249 entry.range = range;
250 entry.min_analog_value = min_analog_value;
251 entry.max_analog_value = max_analog_value;
252 entry.value = static_cast<uint32_t>(axis);
253
254 entries_by_action_id_[action].push_back(entry);
255}
256
257// ----------------------------------------------------------------------------
259{
260 gamepad_entry entry = {};
262 entry.value = static_cast<uint32_t>(button);
263
264 entries_by_action_id_[action].push_back(entry);
265}
266} // namespace input
auto get_digital_value(const action_id_t &action, const gamepad &device) const -> bool
auto is_down(const action_id_t &action, const gamepad &device) const -> bool
auto is_released(const action_id_t &action, const gamepad &device) const -> bool
auto is_pressed(const action_id_t &action, const gamepad &device) const -> bool
std::map< action_id_t, std::vector< gamepad_entry > > entries_by_action_id_
auto get_analog_value(const action_id_t &action, const gamepad &device) const -> float
void map(const action_id_t &action, gamepad_axis axis, axis_range range=axis_range::full, float min_analog_value=-1.0f, float max_analog_value=1.0f)
auto button_state_to_digital_value(const button_state state) -> bool
auto button_state_to_analog_value(const button_state state) -> float
auto epsilon_not_equal(float x, float y) -> bool
std::string action_id_t
Definition action_id.hpp:6