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