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