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