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