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