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