1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Reto Grieder |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "KeyBinder.h" |
---|
30 | |
---|
31 | #include <algorithm> |
---|
32 | #include <sstream> |
---|
33 | #include "util/Convert.h" |
---|
34 | #include "util/Output.h" |
---|
35 | #include "util/Exception.h" |
---|
36 | #include "core/CoreIncludes.h" |
---|
37 | #include "core/config/ConfigValueIncludes.h" |
---|
38 | #include "core/config/ConfigFile.h" |
---|
39 | #include "core/ApplicationPaths.h" |
---|
40 | #include "core/ConfigurablePaths.h" |
---|
41 | #include "InputCommands.h" |
---|
42 | #include "JoyStick.h" |
---|
43 | |
---|
44 | namespace orxonox |
---|
45 | { |
---|
46 | RegisterAbstractClass(KeyBinder).inheritsFrom<JoyStickQuantityListener>(); |
---|
47 | |
---|
48 | /** |
---|
49 | @brief |
---|
50 | Constructor that does as little as necessary. |
---|
51 | */ |
---|
52 | KeyBinder::KeyBinder(const std::string& filename) |
---|
53 | : deriveTime_(0.0f) |
---|
54 | , filename_(filename) |
---|
55 | , configFile_(NULL) |
---|
56 | , fallbackConfigFile_(NULL) |
---|
57 | { |
---|
58 | mouseRelative_[0] = 0; |
---|
59 | mouseRelative_[1] = 0; |
---|
60 | mousePosition_[0] = 0.0; |
---|
61 | mousePosition_[1] = 0.0; |
---|
62 | |
---|
63 | RegisterObject(KeyBinder); |
---|
64 | |
---|
65 | // initialise all buttons and half axes to avoid creating everything with 'new' |
---|
66 | // keys |
---|
67 | for (unsigned int i = 0; i < KeyCode::numberOfKeys; i++) |
---|
68 | { |
---|
69 | const std::string& keyname = KeyCode::ByString[i]; |
---|
70 | if (!keyname.empty()) |
---|
71 | keys_[i].name_ = std::string("Key") + keyname; |
---|
72 | else |
---|
73 | keys_[i].name_.clear(); |
---|
74 | keys_[i].paramCommandBuffer_ = ¶mCommandBuffer_; |
---|
75 | keys_[i].groupName_ = "Keys"; |
---|
76 | } |
---|
77 | // mouse buttons plus 4 mouse wheel buttons only 'generated' by KeyBinder |
---|
78 | const char* const mouseWheelNames[] = { "Wheel1Down", "Wheel1Up", "Wheel2Down", "Wheel2Up" }; |
---|
79 | for (unsigned int i = 0; i < numberOfMouseButtons_; i++) |
---|
80 | { |
---|
81 | std::string nameSuffix; |
---|
82 | if (i < MouseButtonCode::numberOfButtons) |
---|
83 | nameSuffix = MouseButtonCode::ByString[i]; |
---|
84 | else |
---|
85 | nameSuffix = mouseWheelNames[i - MouseButtonCode::numberOfButtons]; |
---|
86 | mouseButtons_[i].name_ = nameSuffix; |
---|
87 | mouseButtons_[i].paramCommandBuffer_ = ¶mCommandBuffer_; |
---|
88 | mouseButtons_[i].groupName_ = "MouseButtons"; |
---|
89 | } |
---|
90 | // mouse axes |
---|
91 | for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++) |
---|
92 | { |
---|
93 | mouseAxes_[i].name_ = MouseAxisCode::ByString[i / 2]; |
---|
94 | if (i & 1) |
---|
95 | mouseAxes_[i].name_ += "Pos"; |
---|
96 | else |
---|
97 | mouseAxes_[i].name_ += "Neg"; |
---|
98 | mouseAxes_[i].paramCommandBuffer_ = ¶mCommandBuffer_; |
---|
99 | mouseAxes_[i].groupName_ = "MouseAxes"; |
---|
100 | } |
---|
101 | |
---|
102 | // initialise joy sticks separatly to allow for reloading |
---|
103 | this->JoyStickQuantityChanged(this->getJoyStickList()); |
---|
104 | |
---|
105 | // set them here to use allHalfAxes_ |
---|
106 | setConfigValues(); |
---|
107 | |
---|
108 | // Load the bindings if filename was given |
---|
109 | if (!this->filename_.empty()) |
---|
110 | this->loadBindings(); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | @brief |
---|
115 | Destructor |
---|
116 | */ |
---|
117 | KeyBinder::~KeyBinder() |
---|
118 | { |
---|
119 | // almost no destructors required because most of the arrays are static. |
---|
120 | clearBindings(); // does some destruction work |
---|
121 | if (this->configFile_) |
---|
122 | delete this->configFile_; |
---|
123 | if (this->fallbackConfigFile_) |
---|
124 | delete this->fallbackConfigFile_; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | @brief |
---|
129 | Loader for the key bindings, managed by config values. |
---|
130 | */ |
---|
131 | void KeyBinder::setConfigValues() |
---|
132 | { |
---|
133 | SetConfigValue(analogThreshold_, 0.05f) |
---|
134 | .description("Threshold for analog axes until which the state is 0."); |
---|
135 | SetConfigValue(bFilterAnalogNoise_, false) |
---|
136 | .description("Specifies whether to filter small analog values like joy stick fluctuations."); |
---|
137 | SetConfigValue(mouseSensitivity_, 3.0f) |
---|
138 | .description("Mouse sensitivity."); |
---|
139 | this->totalMouseSensitivity_ = this->mouseSensitivity_ / this->mouseClippingSize_; |
---|
140 | SetConfigValue(bDeriveMouseInput_, false) |
---|
141 | .description("Whether or not to derive moues movement for the absolute value."); |
---|
142 | SetConfigValue(derivePeriod_, 0.05f) |
---|
143 | .description("Accuracy of the mouse input deriver. The higher the more precise, but laggier."); |
---|
144 | SetConfigValue(mouseSensitivityDerived_, 1.0f) |
---|
145 | .description("Mouse sensitivity if mouse input is derived."); |
---|
146 | SetConfigValue(mouseWheelStepSize_, 120) |
---|
147 | .description("Equals one step of the mousewheel."); |
---|
148 | SetConfigValue(buttonThreshold_, 0.80f) |
---|
149 | .description("Threshold for analog axes until which the button is not pressed.") |
---|
150 | .callback(this, &KeyBinder::buttonThresholdChanged); |
---|
151 | } |
---|
152 | |
---|
153 | void KeyBinder::buttonThresholdChanged() |
---|
154 | { |
---|
155 | for (unsigned int i = 0; i < allHalfAxes_.size(); i++) |
---|
156 | if (!allHalfAxes_[i]->bButtonThresholdUser_) |
---|
157 | allHalfAxes_[i]->buttonThreshold_ = this->buttonThreshold_; |
---|
158 | } |
---|
159 | |
---|
160 | void KeyBinder::JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList) |
---|
161 | { |
---|
162 | unsigned int oldValue = joySticks_.size(); |
---|
163 | joySticks_ = joyStickList; |
---|
164 | |
---|
165 | // initialise joy stick bindings |
---|
166 | initialiseJoyStickBindings(); |
---|
167 | |
---|
168 | // collect all Buttons and HalfAxes again |
---|
169 | compilePointerLists(); |
---|
170 | |
---|
171 | // load the bindings if required |
---|
172 | if (configFile_ != NULL) |
---|
173 | { |
---|
174 | for (unsigned int iDev = oldValue; iDev < joySticks_.size(); ++iDev) |
---|
175 | { |
---|
176 | for (unsigned int i = 0; i < JoyStickButtonCode::numberOfButtons; ++i) |
---|
177 | (*joyStickButtons_[iDev])[i].readBinding(this->configFile_, this->fallbackConfigFile_); |
---|
178 | for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; ++i) |
---|
179 | (*joyStickAxes_[iDev])[i].readBinding(this->configFile_, this->fallbackConfigFile_); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | // Set the button threshold for potential new axes |
---|
184 | buttonThresholdChanged(); |
---|
185 | } |
---|
186 | |
---|
187 | void KeyBinder::initialiseJoyStickBindings() |
---|
188 | { |
---|
189 | while (joyStickAxes_.size() < joySticks_.size()) |
---|
190 | joyStickAxes_.push_back(shared_ptr<JoyStickAxisVector>(new JoyStickAxisVector())); |
---|
191 | while (joyStickButtons_.size() < joySticks_.size()) |
---|
192 | joyStickButtons_.push_back(shared_ptr<JoyStickButtonVector>(new JoyStickButtonVector())); |
---|
193 | // For the case the new size is smaller |
---|
194 | this->joyStickAxes_.resize(joySticks_.size()); |
---|
195 | this->joyStickButtons_.resize(joySticks_.size()); |
---|
196 | |
---|
197 | // reinitialise all joy stick bindings (doesn't overwrite the old ones) |
---|
198 | for (unsigned int iDev = 0; iDev < joySticks_.size(); iDev++) |
---|
199 | { |
---|
200 | const std::string& deviceName = joySticks_[iDev]->getDeviceName(); |
---|
201 | // joy stick buttons |
---|
202 | for (unsigned int i = 0; i < JoyStickButtonCode::numberOfButtons; i++) |
---|
203 | { |
---|
204 | (*joyStickButtons_[iDev])[i].name_ = JoyStickButtonCode::ByString[i]; |
---|
205 | (*joyStickButtons_[iDev])[i].paramCommandBuffer_ = ¶mCommandBuffer_; |
---|
206 | (*joyStickButtons_[iDev])[i].groupName_ = "JoyStickButtons_" + deviceName; |
---|
207 | } |
---|
208 | // joy stick axes |
---|
209 | for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; i++) |
---|
210 | { |
---|
211 | (*joyStickAxes_[iDev])[i].name_ = JoyStickAxisCode::ByString[i / 2]; |
---|
212 | if (i & 1) |
---|
213 | (*joyStickAxes_[iDev])[i].name_ += "Pos"; |
---|
214 | else |
---|
215 | (*joyStickAxes_[iDev])[i].name_ += "Neg"; |
---|
216 | (*joyStickAxes_[iDev])[i].paramCommandBuffer_ = ¶mCommandBuffer_; |
---|
217 | (*joyStickAxes_[iDev])[i].groupName_ = "JoyStickAxes_" + deviceName; |
---|
218 | } |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | void KeyBinder::compilePointerLists() |
---|
223 | { |
---|
224 | allButtons_.clear(); |
---|
225 | allHalfAxes_.clear(); |
---|
226 | |
---|
227 | // Note: Don't include the dummy keys which don't actually exist in OIS but have a number |
---|
228 | for (unsigned int i = 0; i < KeyCode::numberOfKeys; i++) |
---|
229 | if (!keys_[i].name_.empty()) |
---|
230 | allButtons_[keys_[i].groupName_ + '.' + keys_[i].name_] = keys_ + i; |
---|
231 | for (unsigned int i = 0; i < numberOfMouseButtons_; i++) |
---|
232 | allButtons_[mouseButtons_[i].groupName_ + '.' + mouseButtons_[i].name_] = mouseButtons_ + i; |
---|
233 | for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++) |
---|
234 | { |
---|
235 | allButtons_[mouseAxes_[i].groupName_ + '.' + mouseAxes_[i].name_] = mouseAxes_ + i; |
---|
236 | allHalfAxes_.push_back(mouseAxes_ + i); |
---|
237 | } |
---|
238 | for (unsigned int iDev = 0; iDev < joySticks_.size(); iDev++) |
---|
239 | { |
---|
240 | for (unsigned int i = 0; i < JoyStickButtonCode::numberOfButtons; i++) |
---|
241 | allButtons_[(*joyStickButtons_[iDev])[i].groupName_ + '.' + (*joyStickButtons_[iDev])[i].name_] = &((*joyStickButtons_[iDev])[i]); |
---|
242 | for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; i++) |
---|
243 | { |
---|
244 | allButtons_[(*joyStickAxes_[iDev])[i].groupName_ + '.' + (*joyStickAxes_[iDev])[i].name_] = &((*joyStickAxes_[iDev])[i]); |
---|
245 | allHalfAxes_.push_back(&((*joyStickAxes_[iDev])[i])); |
---|
246 | } |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | /** |
---|
251 | @brief |
---|
252 | Loads the key and button bindings. |
---|
253 | */ |
---|
254 | void KeyBinder::loadBindings() |
---|
255 | { |
---|
256 | orxout(internal_info, context::input) << "KeyBinder: Loading key bindings..." << endl; |
---|
257 | |
---|
258 | this->configFile_ = new ConfigFile(this->filename_, !ApplicationPaths::buildDirectoryRun()); |
---|
259 | this->configFile_->load(); |
---|
260 | |
---|
261 | if (ApplicationPaths::buildDirectoryRun()) |
---|
262 | { |
---|
263 | // Dev users should have combined key bindings files |
---|
264 | std::string defaultFilepath(ConfigurablePaths::getDataPathString() + ConfigFile::DEFAULT_CONFIG_FOLDER + '/' + this->filename_); |
---|
265 | std::ifstream file(defaultFilepath.c_str()); |
---|
266 | if (file.is_open()) |
---|
267 | { |
---|
268 | file.close(); |
---|
269 | // Open the default file for later use (use absolute path!) |
---|
270 | this->fallbackConfigFile_ = new ConfigFile(defaultFilepath, false); |
---|
271 | this->fallbackConfigFile_->load(); |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | // Parse bindings and create the ConfigValueContainers if necessary |
---|
276 | for (std::map<std::string, Button*>::const_iterator it = allButtons_.begin(); it != allButtons_.end(); ++it) |
---|
277 | { |
---|
278 | it->second->readBinding(this->configFile_, this->fallbackConfigFile_); |
---|
279 | addButtonToCommand(it->second->bindingString_, it->second); |
---|
280 | } |
---|
281 | |
---|
282 | orxout(internal_info, context::input) << "KeyBinder: Loading key bindings done." << endl; |
---|
283 | } |
---|
284 | |
---|
285 | bool KeyBinder::setBinding(const std::string& binding, const std::string& name, bool bTemporary) |
---|
286 | { |
---|
287 | std::map<std::string, Button*>::iterator it = allButtons_.find(name); |
---|
288 | if (it != allButtons_.end()) |
---|
289 | { |
---|
290 | addButtonToCommand(binding, it->second); |
---|
291 | std::string str = binding; |
---|
292 | if (ApplicationPaths::buildDirectoryRun() && binding.empty()) |
---|
293 | str = "NoBinding"; |
---|
294 | it->second->setBinding(this->configFile_, this->fallbackConfigFile_, binding, bTemporary); |
---|
295 | return true; |
---|
296 | } |
---|
297 | else |
---|
298 | { |
---|
299 | orxout(internal_warning, context::input) << "Could not find key/button/axis with name '" << name << "'." << endl; |
---|
300 | return false; |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | void KeyBinder::addButtonToCommand(const std::string& command, Button* button) |
---|
305 | { |
---|
306 | std::ostringstream stream; |
---|
307 | stream << button->groupName_ << '.' << button->name_; |
---|
308 | |
---|
309 | std::vector<std::string>& oldKeynames = this->allCommands_[button->bindingString_]; |
---|
310 | std::vector<std::string>::iterator it = std::find(oldKeynames.begin(), oldKeynames.end(), stream.str()); |
---|
311 | if (it != oldKeynames.end()) |
---|
312 | oldKeynames.erase(it); |
---|
313 | |
---|
314 | if (!command.empty()) |
---|
315 | { |
---|
316 | std::vector<std::string>& keynames = this->allCommands_[command]; |
---|
317 | if (std::find(keynames.begin(), keynames.end(), stream.str()) == keynames.end()) |
---|
318 | this->allCommands_[command].push_back(stream.str()); |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | /** |
---|
323 | @brief |
---|
324 | Return the first key name for a specific command |
---|
325 | */ |
---|
326 | const std::string& KeyBinder::getBinding(const std::string& commandName) |
---|
327 | { |
---|
328 | if (this->allCommands_.find(commandName) != this->allCommands_.end()) |
---|
329 | { |
---|
330 | std::vector<std::string>& keynames = this->allCommands_[commandName]; |
---|
331 | return keynames.front(); |
---|
332 | } |
---|
333 | |
---|
334 | return BLANKSTRING; |
---|
335 | } |
---|
336 | |
---|
337 | /** |
---|
338 | @brief |
---|
339 | Return the key name for a specific command at a given index. |
---|
340 | @param commandName |
---|
341 | The command name the key name is returned for. |
---|
342 | @param index |
---|
343 | The index at which the key name is returned for. |
---|
344 | */ |
---|
345 | const std::string& KeyBinder::getBinding(const std::string& commandName, unsigned int index) |
---|
346 | { |
---|
347 | if (this->allCommands_.find(commandName) != this->allCommands_.end()) |
---|
348 | { |
---|
349 | std::vector<std::string>& keynames = this->allCommands_[commandName]; |
---|
350 | if (index < keynames.size()) |
---|
351 | return keynames[index]; |
---|
352 | |
---|
353 | return BLANKSTRING; |
---|
354 | } |
---|
355 | |
---|
356 | return BLANKSTRING; |
---|
357 | } |
---|
358 | |
---|
359 | /** |
---|
360 | @brief |
---|
361 | Return the first key name for a specific command in a human readable form |
---|
362 | */ |
---|
363 | const std::string& KeyBinder::getBindingReadable(const std::string& commandName) |
---|
364 | { |
---|
365 | const std::string& binding = this->getBinding(commandName); |
---|
366 | |
---|
367 | SubString substring = SubString(binding, "."); |
---|
368 | std::string name; |
---|
369 | std::string group; |
---|
370 | switch(substring.size()) |
---|
371 | { |
---|
372 | case 0: |
---|
373 | return binding; |
---|
374 | case 1: |
---|
375 | return binding; |
---|
376 | case 2: |
---|
377 | group = substring[0]; |
---|
378 | default: |
---|
379 | name = substring.subSet(1).join("."); |
---|
380 | } |
---|
381 | |
---|
382 | std::stringstream stream; |
---|
383 | if(group.compare("Keys") == 0) |
---|
384 | stream << "Key " << name.substr(3); |
---|
385 | else if(group.compare("MouseButtons") == 0) |
---|
386 | stream << "Mouse " << name; |
---|
387 | else if(group.compare("JoyStickButtons") == 0) |
---|
388 | stream << "Joystick " << name; |
---|
389 | else if(group.compare("JoyStickAxes") == 0) |
---|
390 | stream << "Joystick Axis" << name.substr(5, 6) << name.substr(name.find("Axis")+6); |
---|
391 | else if(group.compare("MouseAxes") == 0) |
---|
392 | stream << "Mouse " << name.substr(1,3) << " " << name.substr(0, 1) << "-Axis"; |
---|
393 | else |
---|
394 | return binding; |
---|
395 | |
---|
396 | return *(new std::string(stream.str())); |
---|
397 | } |
---|
398 | |
---|
399 | /** |
---|
400 | @brief |
---|
401 | Get the number of different key bindings of a specific command. |
---|
402 | @param commandName |
---|
403 | The command. |
---|
404 | */ |
---|
405 | unsigned int KeyBinder::getNumberOfBindings(const std::string& commandName) |
---|
406 | { |
---|
407 | if (this->allCommands_.find(commandName) != this->allCommands_.end()) |
---|
408 | { |
---|
409 | std::vector<std::string>& keynames = this->allCommands_[commandName]; |
---|
410 | return keynames.size(); |
---|
411 | } |
---|
412 | |
---|
413 | return 0; |
---|
414 | } |
---|
415 | |
---|
416 | /** |
---|
417 | @brief |
---|
418 | Overwrites all bindings with "" |
---|
419 | */ |
---|
420 | void KeyBinder::clearBindings() |
---|
421 | { |
---|
422 | for (std::map<std::string, Button*>::const_iterator it = allButtons_.begin(); it != allButtons_.end(); ++it) |
---|
423 | it->second->clear(); |
---|
424 | |
---|
425 | for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++) |
---|
426 | delete paramCommandBuffer_[i]; |
---|
427 | paramCommandBuffer_.clear(); |
---|
428 | } |
---|
429 | |
---|
430 | /** |
---|
431 | @brief Changes the keybind mode of a given console command. |
---|
432 | */ |
---|
433 | void KeyBinder::changeMode(ConsoleCommand* command, KeybindMode::Value new_mode) |
---|
434 | { |
---|
435 | // iterate over all buttons |
---|
436 | for (std::map<std::string, Button*>::iterator it = this->allButtons_.begin(); it != this->allButtons_.end(); ++it) |
---|
437 | { |
---|
438 | Button* button = it->second; |
---|
439 | |
---|
440 | // iterate over all modes |
---|
441 | for (int mode_index = 0; mode_index < 3; ++mode_index) |
---|
442 | { |
---|
443 | if (mode_index == new_mode) // skip commands that are already in the desired mode |
---|
444 | continue; |
---|
445 | |
---|
446 | // iterate over all commands of the given mode at the given button |
---|
447 | for (size_t command_index = 0; command_index < button->nCommands_[mode_index]; ++command_index) |
---|
448 | { |
---|
449 | if (button->commands_[mode_index][command_index]->hasFixedKeybindMode()) |
---|
450 | continue; |
---|
451 | |
---|
452 | CommandEvaluation* evaluation = button->commands_[mode_index][command_index]->getEvaluation(); |
---|
453 | |
---|
454 | // compare the command |
---|
455 | if (evaluation && evaluation->getConsoleCommand() == command) |
---|
456 | { |
---|
457 | // increase array of new mode |
---|
458 | BaseCommand** array_new_mode = new BaseCommand*[button->nCommands_[new_mode] + 1]; |
---|
459 | // copy array content |
---|
460 | for (size_t c = 0; c < button->nCommands_[new_mode]; ++c) |
---|
461 | array_new_mode[c] = button->commands_[new_mode][c]; |
---|
462 | // insert changed command at the end |
---|
463 | array_new_mode[button->nCommands_[new_mode]] = button->commands_[mode_index][command_index]; |
---|
464 | // delete old array |
---|
465 | delete[] button->commands_[new_mode]; |
---|
466 | // assign new array |
---|
467 | button->commands_[new_mode] = array_new_mode; |
---|
468 | // increase counter |
---|
469 | button->nCommands_[new_mode]++; |
---|
470 | |
---|
471 | // erase command from old array |
---|
472 | for (size_t c = command_index; c < button->nCommands_[mode_index] - 1; ++c) |
---|
473 | button->commands_[mode_index][c] = button->commands_[mode_index][c + 1]; |
---|
474 | // decrease counter |
---|
475 | button->nCommands_[mode_index]--; |
---|
476 | // old array would not get deleted if nCommands_ is now 0 |
---|
477 | // otherwise: nobody cares about an array that is one element too large - nCommands_ defines the size |
---|
478 | if (button->nCommands_[mode_index] == 0) |
---|
479 | { |
---|
480 | delete[] button->commands_[mode_index]; |
---|
481 | button->commands_[mode_index] = 0; |
---|
482 | } |
---|
483 | |
---|
484 | // decrement the index since we shifted the array and continue searching for more occurrences of the command |
---|
485 | command_index--; |
---|
486 | } |
---|
487 | } |
---|
488 | } |
---|
489 | } |
---|
490 | } |
---|
491 | |
---|
492 | void KeyBinder::resetJoyStickAxes() |
---|
493 | { |
---|
494 | for (unsigned int iDev = 0; iDev < joySticks_.size(); ++iDev) |
---|
495 | for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; i++) |
---|
496 | (*joyStickAxes_[iDev])[i].reset(); |
---|
497 | } |
---|
498 | |
---|
499 | /** |
---|
500 | @brief Sets the position of the mouse back to 0/0. |
---|
501 | */ |
---|
502 | void KeyBinder::resetMouseAxes() |
---|
503 | { |
---|
504 | this->mousePosition_[0] = 0.0f; |
---|
505 | this->mousePosition_[1] = 0.0f; |
---|
506 | |
---|
507 | for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++) |
---|
508 | mouseAxes_[i].reset(); |
---|
509 | } |
---|
510 | |
---|
511 | void KeyBinder::mouseUpdated(float dt) |
---|
512 | { |
---|
513 | if (bDeriveMouseInput_) |
---|
514 | { |
---|
515 | // only update when derivation dt has passed |
---|
516 | if (deriveTime_ > derivePeriod_) |
---|
517 | { |
---|
518 | for (int i = 0; i < 2; i++) |
---|
519 | { |
---|
520 | if (mouseRelative_[i] < 0) |
---|
521 | { |
---|
522 | mouseAxes_[2*i + 0].absVal_ |
---|
523 | = -mouseRelative_[i] / deriveTime_ * 0.0005f * mouseSensitivityDerived_; |
---|
524 | mouseAxes_[2*i + 1].absVal_ = 0.0f; |
---|
525 | } |
---|
526 | else if (mouseRelative_[i] > 0) |
---|
527 | { |
---|
528 | mouseAxes_[2*i + 0].absVal_ = 0.0f; |
---|
529 | mouseAxes_[2*i + 1].absVal_ |
---|
530 | = mouseRelative_[i] / deriveTime_ * 0.0005f * mouseSensitivityDerived_; |
---|
531 | } |
---|
532 | else |
---|
533 | { |
---|
534 | mouseAxes_[2*i + 0].absVal_ = 0.0f; |
---|
535 | mouseAxes_[2*i + 1].absVal_ = 0.0f; |
---|
536 | } |
---|
537 | mouseRelative_[i] = 0; |
---|
538 | mouseAxes_[2*i + 0].hasChanged_ = true; |
---|
539 | mouseAxes_[2*i + 1].hasChanged_ = true; |
---|
540 | } |
---|
541 | deriveTime_ = 0.0f; |
---|
542 | } |
---|
543 | else |
---|
544 | deriveTime_ += dt; |
---|
545 | } |
---|
546 | |
---|
547 | for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++) |
---|
548 | { |
---|
549 | // Why dividing relative value by dt? The reason lies in the simple fact, that when you |
---|
550 | // press a button that has relative movement, that value has to be multiplied by dt to be |
---|
551 | // frame rate independent. This can easily (and only) be done in updateInput(float). |
---|
552 | // Hence we need to divide by dt here for the mouse to compensate, because the relative |
---|
553 | // move movements have nothing to do with dt. |
---|
554 | if (dt != 0.0f) |
---|
555 | { |
---|
556 | // just ignore if dt == 0.0 because we have multiplied by 0.0 anyway.. |
---|
557 | mouseAxes_[i].relVal_ /= dt; |
---|
558 | } |
---|
559 | |
---|
560 | tickHalfAxis(mouseAxes_[i]); |
---|
561 | } |
---|
562 | } |
---|
563 | |
---|
564 | void KeyBinder::joyStickUpdated(unsigned int joyStick, float dt) |
---|
565 | { |
---|
566 | for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; i++) |
---|
567 | { |
---|
568 | tickHalfAxis((*joyStickAxes_[joyStick])[i]); |
---|
569 | } |
---|
570 | } |
---|
571 | |
---|
572 | void KeyBinder::tickHalfAxis(HalfAxis& halfAxis) |
---|
573 | { |
---|
574 | // button mode |
---|
575 | // TODO: optimize out all the half axes that don't act as a button at the moment |
---|
576 | if (halfAxis.hasChanged_) |
---|
577 | { |
---|
578 | if (!halfAxis.pressed_ && halfAxis.absVal_ > halfAxis.buttonThreshold_) |
---|
579 | { |
---|
580 | // key pressed event |
---|
581 | halfAxis.pressed_ = true; |
---|
582 | if (halfAxis.nCommands_[KeybindMode::OnPress]) |
---|
583 | halfAxis.execute(KeybindMode::OnPress); |
---|
584 | } |
---|
585 | else if (halfAxis.pressed_ && halfAxis.absVal_ < halfAxis.buttonThreshold_) |
---|
586 | { |
---|
587 | // key released event |
---|
588 | halfAxis.pressed_ = false; |
---|
589 | if (halfAxis.nCommands_[KeybindMode::OnRelease]) |
---|
590 | halfAxis.execute(KeybindMode::OnRelease); |
---|
591 | } |
---|
592 | halfAxis.hasChanged_ = false; |
---|
593 | } |
---|
594 | |
---|
595 | if (halfAxis.pressed_) |
---|
596 | { |
---|
597 | // key held event |
---|
598 | if (halfAxis.nCommands_[KeybindMode::OnHold]) |
---|
599 | halfAxis.execute(KeybindMode::OnHold); |
---|
600 | } |
---|
601 | |
---|
602 | // these are the actually useful axis bindings for analog input |
---|
603 | halfAxis.execute(); |
---|
604 | } |
---|
605 | |
---|
606 | /** |
---|
607 | @brief |
---|
608 | Event handler for the mouseMoved Event. |
---|
609 | @param abs_ |
---|
610 | The absolute position of the mouse |
---|
611 | @param rel_ |
---|
612 | The relative movement of the mouse |
---|
613 | @param clippingSize |
---|
614 | Mouse screen area in pixels (usually 1024x1024) |
---|
615 | */ |
---|
616 | void KeyBinder::mouseMoved(IntVector2 abs_, IntVector2 rel_, IntVector2 clippingSize) |
---|
617 | { |
---|
618 | // y axis of mouse input is inverted |
---|
619 | int rel[] = { rel_.x, -rel_.y }; |
---|
620 | |
---|
621 | if (bDeriveMouseInput_) |
---|
622 | { |
---|
623 | mouseRelative_[0] += rel[0]; |
---|
624 | mouseRelative_[1] += rel[1]; |
---|
625 | } |
---|
626 | else |
---|
627 | { |
---|
628 | for (int i = 0; i < 2; i++) |
---|
629 | { |
---|
630 | if (rel[i]) // performance opt. for the case that rel[i] == 0 |
---|
631 | { |
---|
632 | // write absolute values |
---|
633 | mouseAxes_[2*i + 0].hasChanged_ = true; |
---|
634 | mouseAxes_[2*i + 1].hasChanged_ = true; |
---|
635 | mousePosition_[i] += rel[i] * totalMouseSensitivity_; |
---|
636 | |
---|
637 | // clip absolute position |
---|
638 | if (mousePosition_[i] > 1.0) |
---|
639 | mousePosition_[i] = 1.0; |
---|
640 | if (mousePosition_[i] < -1.0) |
---|
641 | mousePosition_[i] = -1.0; |
---|
642 | |
---|
643 | if (mousePosition_[i] < 0.0) |
---|
644 | { |
---|
645 | mouseAxes_[2*i + 0].absVal_ = -mousePosition_[i]; |
---|
646 | mouseAxes_[2*i + 1].absVal_ = 0.0f; |
---|
647 | } |
---|
648 | else |
---|
649 | { |
---|
650 | mouseAxes_[2*i + 0].absVal_ = 0.0f; |
---|
651 | mouseAxes_[2*i + 1].absVal_ = mousePosition_[i]; |
---|
652 | } |
---|
653 | } |
---|
654 | } |
---|
655 | } |
---|
656 | |
---|
657 | // relative |
---|
658 | for (int i = 0; i < 2; i++) |
---|
659 | { |
---|
660 | if (rel[i] < 0) |
---|
661 | mouseAxes_[0 + 2*i].relVal_ = -rel[i] * totalMouseSensitivity_; |
---|
662 | else |
---|
663 | mouseAxes_[1 + 2*i].relVal_ = rel[i] * totalMouseSensitivity_; |
---|
664 | } |
---|
665 | } |
---|
666 | |
---|
667 | /** |
---|
668 | @brief Event handler for the mouseScrolled Event. |
---|
669 | @param abs The absolute position of the scroll wheel |
---|
670 | @param rel The relative movement of the scroll wheel |
---|
671 | */ |
---|
672 | void KeyBinder::mouseScrolled(int abs, int rel) |
---|
673 | { |
---|
674 | if (rel < 0) |
---|
675 | for (int i = 0; i < -rel/mouseWheelStepSize_; i++) |
---|
676 | mouseButtons_[8].execute(KeybindMode::OnPress, static_cast<float>(abs)/mouseWheelStepSize_); |
---|
677 | else |
---|
678 | for (int i = 0; i < rel/mouseWheelStepSize_; i++) |
---|
679 | mouseButtons_[9].execute(KeybindMode::OnPress, static_cast<float>(abs)/mouseWheelStepSize_); |
---|
680 | } |
---|
681 | |
---|
682 | void KeyBinder::axisMoved(unsigned int device, unsigned int axisID, float value) |
---|
683 | { |
---|
684 | // Filter analog noise |
---|
685 | if (this->bFilterAnalogNoise_ && std::abs(value) < this->analogThreshold_) |
---|
686 | value = 0.0; |
---|
687 | int i = axisID * 2; |
---|
688 | JoyStickAxisVector& axis = *joyStickAxes_[device]; |
---|
689 | if (value < 0) |
---|
690 | { |
---|
691 | axis[i].absVal_ = -value; |
---|
692 | axis[i].relVal_ = -value; |
---|
693 | axis[i].hasChanged_ = true; |
---|
694 | if (axis[i + 1].absVal_ > 0.0f) |
---|
695 | { |
---|
696 | axis[i + 1].absVal_ = -0.0f; |
---|
697 | axis[i + 1].relVal_ = -0.0f; |
---|
698 | axis[i + 1].hasChanged_ = true; |
---|
699 | } |
---|
700 | } |
---|
701 | else |
---|
702 | { |
---|
703 | axis[i + 1].absVal_ = value; |
---|
704 | axis[i + 1].relVal_ = value; |
---|
705 | axis[i + 1].hasChanged_ = true; |
---|
706 | if (axis[i].absVal_ > 0.0f) |
---|
707 | { |
---|
708 | axis[i].absVal_ = -0.0f; |
---|
709 | axis[i].relVal_ = -0.0f; |
---|
710 | axis[i].hasChanged_ = true; |
---|
711 | } |
---|
712 | } |
---|
713 | } |
---|
714 | } |
---|