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