[918] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1502] | 3 | * > www.orxonox.net < |
---|
[918] | 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: |
---|
[934] | 25 | * ... |
---|
[918] | 26 | * |
---|
| 27 | */ |
---|
[973] | 28 | |
---|
[918] | 29 | /** |
---|
[1219] | 30 | @file |
---|
| 31 | @brief Implementation of the InputManager that captures all the input from OIS |
---|
[1293] | 32 | and redirects it to handlers. |
---|
[918] | 33 | */ |
---|
| 34 | |
---|
[1062] | 35 | #include "InputManager.h" |
---|
[1519] | 36 | |
---|
[1520] | 37 | #include <limits.h> |
---|
[1519] | 38 | #include "core/CoreIncludes.h" |
---|
| 39 | #include "core/ConfigValueIncludes.h" |
---|
| 40 | #include "core/Debug.h" |
---|
| 41 | #include "core/CommandExecutor.h" |
---|
| 42 | #include "core/ConsoleCommand.h" |
---|
[1520] | 43 | #include "core/Shell.h" // hack! |
---|
[1219] | 44 | #include "InputBuffer.h" |
---|
[1502] | 45 | #include "KeyBinder.h" |
---|
[1520] | 46 | #include "KeyDetector.h" |
---|
| 47 | #include "CalibratorCallback.h" |
---|
[918] | 48 | |
---|
| 49 | namespace orxonox |
---|
| 50 | { |
---|
[1502] | 51 | SetConsoleCommandShortcut(InputManager, keyBind); |
---|
| 52 | SetConsoleCommandShortcut(InputManager, storeKeyStroke); |
---|
| 53 | SetConsoleCommandShortcut(InputManager, calibrate); |
---|
| 54 | |
---|
[1219] | 55 | // ############################### |
---|
| 56 | // ### Internal Methods ### |
---|
| 57 | // ############################### |
---|
| 58 | // ############################### |
---|
[1084] | 59 | |
---|
[919] | 60 | /** |
---|
[1219] | 61 | @brief Constructor only sets member fields to initial zero values |
---|
| 62 | and registers the class in the class hierarchy. |
---|
[919] | 63 | */ |
---|
[973] | 64 | InputManager::InputManager() : |
---|
[1034] | 65 | inputSystem_(0), keyboard_(0), mouse_(0), |
---|
[1293] | 66 | joySticksSize_(0), |
---|
[1502] | 67 | keyBinder_(0), keyDetector_(0), buffer_(0), calibratorCallback_(0), |
---|
| 68 | state_(IS_UNINIT), stateRequest_(IS_UNINIT), savedState_(IS_UNINIT), |
---|
[1293] | 69 | keyboardModifiers_(0) |
---|
[918] | 70 | { |
---|
[1531] | 71 | RegisterRootObject(InputManager); |
---|
[929] | 72 | } |
---|
| 73 | |
---|
| 74 | /** |
---|
[973] | 75 | @brief The one instance of the InputManager is stored in this function. |
---|
[1035] | 76 | @return A reference to the only instance of the InputManager |
---|
[919] | 77 | */ |
---|
[1219] | 78 | InputManager& InputManager::_getSingleton() |
---|
[919] | 79 | { |
---|
[1035] | 80 | static InputManager theOnlyInstance; |
---|
| 81 | return theOnlyInstance; |
---|
[919] | 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
[1219] | 85 | @brief Destructor only called at the end of the program, after main. |
---|
| 86 | */ |
---|
| 87 | InputManager::~InputManager() |
---|
| 88 | { |
---|
| 89 | _destroy(); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | @brief Creates the OIS::InputMananger, the keyboard, the mouse and |
---|
| 94 | the joysticks and assigns the key bindings. |
---|
[919] | 95 | @param windowHnd The window handle of the render window |
---|
| 96 | @param windowWidth The width of the render window |
---|
| 97 | @param windowHeight The height of the render window |
---|
| 98 | */ |
---|
[1293] | 99 | bool InputManager::_initialise(const size_t windowHnd, int windowWidth, int windowHeight, |
---|
| 100 | bool createKeyboard, bool createMouse, bool createJoySticks) |
---|
[919] | 101 | { |
---|
[1219] | 102 | if (state_ == IS_UNINIT) |
---|
[918] | 103 | { |
---|
[1293] | 104 | CCOUT(3) << "Initialising Input System..." << std::endl; |
---|
[1219] | 105 | CCOUT(ORX_DEBUG) << "Initialising OIS components..." << std::endl; |
---|
| 106 | |
---|
[918] | 107 | OIS::ParamList paramList; |
---|
| 108 | std::ostringstream windowHndStr; |
---|
| 109 | |
---|
| 110 | // Fill parameter list |
---|
| 111 | windowHndStr << (unsigned int)windowHnd; |
---|
| 112 | paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
[1502] | 113 | //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE"))); |
---|
[1349] | 114 | //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND"))); |
---|
[1219] | 115 | //#if defined OIS_LINUX_PLATFORM |
---|
| 116 | // paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true"))); |
---|
| 117 | //#endif |
---|
[928] | 118 | |
---|
[934] | 119 | try |
---|
[918] | 120 | { |
---|
[934] | 121 | inputSystem_ = OIS::InputManager::createInputSystem(paramList); |
---|
[1219] | 122 | CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl; |
---|
[918] | 123 | } |
---|
[934] | 124 | catch (OIS::Exception ex) |
---|
| 125 | { |
---|
[1219] | 126 | CCOUT(ORX_ERROR) << "Error: Failed creating an OIS input system." |
---|
| 127 | << "OIS message: \"" << ex.eText << "\"" << std::endl; |
---|
| 128 | inputSystem_ = 0; |
---|
[934] | 129 | return false; |
---|
| 130 | } |
---|
[1219] | 131 | |
---|
| 132 | if (createKeyboard) |
---|
| 133 | _initialiseKeyboard(); |
---|
| 134 | |
---|
| 135 | if (createMouse) |
---|
| 136 | _initialiseMouse(); |
---|
| 137 | |
---|
| 138 | if (createJoySticks) |
---|
| 139 | _initialiseJoySticks(); |
---|
| 140 | |
---|
| 141 | // Set mouse/joystick region |
---|
[1349] | 142 | if (mouse_) |
---|
| 143 | { |
---|
| 144 | setWindowExtents(windowWidth, windowHeight); |
---|
| 145 | } |
---|
[1219] | 146 | |
---|
| 147 | state_ = IS_NONE; |
---|
| 148 | CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl; |
---|
[1502] | 149 | |
---|
| 150 | // InputManager holds the input buffer --> create one and add it. |
---|
| 151 | buffer_ = new InputBuffer(); |
---|
| 152 | addKeyHandler(buffer_, "buffer"); |
---|
[1505] | 153 | Shell::getInstance().setInputBuffer(buffer_); |
---|
[1502] | 154 | |
---|
| 155 | keyBinder_ = new KeyBinder(); |
---|
| 156 | keyBinder_->loadBindings(); |
---|
| 157 | addKeyHandler(keyBinder_, "keybinder"); |
---|
| 158 | addMouseHandler(keyBinder_, "keybinder"); |
---|
| 159 | addJoyStickHandler(keyBinder_, "keybinder"); |
---|
| 160 | |
---|
| 161 | keyDetector_ = new KeyDetector(); |
---|
| 162 | keyDetector_->loadBindings(); |
---|
| 163 | addKeyHandler(keyDetector_, "keydetector"); |
---|
| 164 | addMouseHandler(keyDetector_, "keydetector"); |
---|
| 165 | addJoyStickHandler(keyDetector_, "keydetector"); |
---|
| 166 | |
---|
| 167 | calibratorCallback_ = new CalibratorCallback(); |
---|
| 168 | addKeyHandler(calibratorCallback_, "calibratorcallback"); |
---|
| 169 | |
---|
| 170 | setConfigValues(); |
---|
| 171 | |
---|
| 172 | CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl; |
---|
[918] | 173 | } |
---|
[1219] | 174 | else |
---|
| 175 | { |
---|
| 176 | CCOUT(ORX_WARNING) << "Warning: OIS compoments already initialised, skipping" << std::endl; |
---|
| 177 | } |
---|
[934] | 178 | return true; |
---|
[918] | 179 | } |
---|
| 180 | |
---|
[919] | 181 | /** |
---|
[1219] | 182 | @brief Creates a keyboard and sets the event handler. |
---|
| 183 | @return False if keyboard stays uninitialised, true otherwise. |
---|
[928] | 184 | */ |
---|
[1219] | 185 | bool InputManager::_initialiseKeyboard() |
---|
[928] | 186 | { |
---|
[1219] | 187 | if (keyboard_ != 0) |
---|
| 188 | { |
---|
| 189 | CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl; |
---|
| 190 | return true; |
---|
| 191 | } |
---|
| 192 | try |
---|
| 193 | { |
---|
| 194 | if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0) |
---|
| 195 | { |
---|
| 196 | keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true); |
---|
| 197 | // register our listener in OIS. |
---|
| 198 | keyboard_->setEventCallback(this); |
---|
[1505] | 199 | // note: OIS will not detect keys that have already been down when the keyboard was created. |
---|
[1219] | 200 | CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl; |
---|
| 201 | return true; |
---|
| 202 | } |
---|
| 203 | else |
---|
| 204 | { |
---|
| 205 | CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl; |
---|
| 206 | return false; |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | catch (OIS::Exception ex) |
---|
| 210 | { |
---|
| 211 | // TODO: Test this output regarding formatting |
---|
| 212 | CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n" |
---|
| 213 | << "OIS error message: \"" << ex.eText << "\"" << std::endl; |
---|
| 214 | keyboard_ = 0; |
---|
| 215 | return false; |
---|
| 216 | } |
---|
| 217 | } |
---|
[928] | 218 | |
---|
[1219] | 219 | /** |
---|
| 220 | @brief Creates a mouse and sets the event handler. |
---|
| 221 | @return False if mouse stays uninitialised, true otherwise. |
---|
| 222 | */ |
---|
| 223 | bool InputManager::_initialiseMouse() |
---|
| 224 | { |
---|
| 225 | if (mouse_ != 0) |
---|
| 226 | { |
---|
| 227 | CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl; |
---|
| 228 | return true; |
---|
| 229 | } |
---|
| 230 | try |
---|
| 231 | { |
---|
| 232 | if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0) |
---|
| 233 | { |
---|
| 234 | mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); |
---|
| 235 | // register our listener in OIS. |
---|
| 236 | mouse_->setEventCallback(this); |
---|
[1502] | 237 | CCOUT(ORX_DEBUG) << "Created OIS mouse" << std::endl; |
---|
[1219] | 238 | return true; |
---|
| 239 | } |
---|
| 240 | else |
---|
| 241 | { |
---|
| 242 | CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl; |
---|
| 243 | return false; |
---|
| 244 | } |
---|
| 245 | } |
---|
| 246 | catch (OIS::Exception ex) |
---|
| 247 | { |
---|
| 248 | CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS mouse\n" |
---|
| 249 | << "OIS error message: \"" << ex.eText << "\"" << std::endl; |
---|
| 250 | mouse_ = 0; |
---|
| 251 | return false; |
---|
| 252 | } |
---|
| 253 | } |
---|
[1035] | 254 | |
---|
[1219] | 255 | /** |
---|
| 256 | @brief Creates all joy sticks and sets the event handler. |
---|
| 257 | @return False joy stick stay uninitialised, true otherwise. |
---|
| 258 | */ |
---|
| 259 | bool InputManager::_initialiseJoySticks() |
---|
| 260 | { |
---|
[1293] | 261 | if (joySticksSize_ > 0) |
---|
[1219] | 262 | { |
---|
| 263 | CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl; |
---|
| 264 | return true; |
---|
| 265 | } |
---|
| 266 | bool success = false; |
---|
| 267 | if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0) |
---|
| 268 | { |
---|
| 269 | for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++) |
---|
| 270 | { |
---|
| 271 | try |
---|
| 272 | { |
---|
| 273 | OIS::JoyStick* stig = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true)); |
---|
| 274 | joySticks_.push_back(stig); |
---|
| 275 | // register our listener in OIS. |
---|
| 276 | stig->setEventCallback(this); |
---|
| 277 | CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl; |
---|
| 278 | success = true; |
---|
| 279 | } |
---|
| 280 | catch (OIS::Exception ex) |
---|
| 281 | { |
---|
| 282 | CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n" |
---|
| 283 | << "OIS error message: \"" << ex.eText << "\"" << std::endl; |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | else |
---|
| 288 | { |
---|
| 289 | CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl; |
---|
| 290 | return false; |
---|
| 291 | } |
---|
[1293] | 292 | joySticksSize_ = joySticks_.size(); |
---|
| 293 | activeJoyStickHandlers_.resize(joySticksSize_); |
---|
| 294 | joyStickButtonsDown_.resize(joySticksSize_); |
---|
[1349] | 295 | povStates_.resize(joySticksSize_); |
---|
| 296 | sliderStates_.resize(joySticksSize_); |
---|
[1502] | 297 | joySticksCalibration_.resize(joySticksSize_); |
---|
| 298 | for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) |
---|
| 299 | { |
---|
| 300 | // reset the calibration with default values |
---|
| 301 | for (unsigned int i = 0; i < 24; i++) |
---|
| 302 | { |
---|
| 303 | joySticksCalibration_[iJoyStick].negativeCoeff[i] = 1.0f/32767.0f; |
---|
| 304 | joySticksCalibration_[iJoyStick].positiveCoeff[i] = 1.0f/32768.0f; |
---|
| 305 | joySticksCalibration_[iJoyStick].zeroStates[i] = 0; |
---|
| 306 | } |
---|
| 307 | } |
---|
[1219] | 308 | return success; |
---|
| 309 | } |
---|
[1035] | 310 | |
---|
[1219] | 311 | /** |
---|
[1502] | 312 | @brief Sets the configurable values. Use keybindings.ini as file.. |
---|
| 313 | */ |
---|
| 314 | void InputManager::setConfigValues() |
---|
| 315 | { |
---|
[1505] | 316 | if (joySticksSize_) |
---|
| 317 | { |
---|
[1502] | 318 | std::vector<MultiTypeMath> coeffPos; |
---|
| 319 | std::vector<MultiTypeMath> coeffNeg; |
---|
| 320 | std::vector<MultiTypeMath> zero; |
---|
| 321 | coeffPos.resize(24); |
---|
| 322 | coeffNeg.resize(24); |
---|
| 323 | zero.resize(24); |
---|
| 324 | for (unsigned int i = 0; i < 24; i++) |
---|
| 325 | { |
---|
| 326 | coeffPos[i] = 1.0f/32767.0f; |
---|
| 327 | coeffNeg[i] = 1.0f/32768.0f; |
---|
| 328 | zero[i] = 0; |
---|
| 329 | } |
---|
| 330 | |
---|
[1505] | 331 | ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer("CoeffPos"); |
---|
| 332 | if (!cont) |
---|
| 333 | { |
---|
| 334 | cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), "CoeffPos", coeffPos); |
---|
| 335 | getIdentifier()->addConfigValueContainer("CoeffPos", cont); |
---|
| 336 | } |
---|
| 337 | cont->getValue(&coeffPos); |
---|
| 338 | |
---|
| 339 | cont = getIdentifier()->getConfigValueContainer("CoeffNeg"); |
---|
| 340 | if (!cont) |
---|
| 341 | { |
---|
| 342 | cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), "CoeffNeg", coeffNeg); |
---|
| 343 | getIdentifier()->addConfigValueContainer("CoeffNeg", cont); |
---|
| 344 | } |
---|
| 345 | cont->getValue(&coeffNeg); |
---|
| 346 | |
---|
| 347 | cont = getIdentifier()->getConfigValueContainer("Zero"); |
---|
| 348 | if (!cont) |
---|
| 349 | { |
---|
| 350 | cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), "Zero", zero); |
---|
| 351 | getIdentifier()->addConfigValueContainer("Zero", cont); |
---|
| 352 | } |
---|
| 353 | cont->getValue(&zero); |
---|
| 354 | |
---|
| 355 | // copy values to our own variables |
---|
| 356 | for (unsigned int i = 0; i < 24; i++) |
---|
| 357 | { |
---|
| 358 | joySticksCalibration_[0].positiveCoeff[i] = coeffPos[i]; |
---|
| 359 | joySticksCalibration_[0].negativeCoeff[i] = coeffNeg[i]; |
---|
| 360 | joySticksCalibration_[0].zeroStates[i] = zero[i]; |
---|
| 361 | } |
---|
| 362 | } |
---|
[1502] | 363 | } |
---|
| 364 | |
---|
| 365 | /** |
---|
[1219] | 366 | @brief Destroys all the created input devices and sets the InputManager to construction state. |
---|
| 367 | */ |
---|
| 368 | void InputManager::_destroy() |
---|
| 369 | { |
---|
| 370 | if (state_ != IS_UNINIT) |
---|
| 371 | { |
---|
[1293] | 372 | CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl; |
---|
| 373 | |
---|
[1502] | 374 | if (buffer_) |
---|
| 375 | delete buffer_; |
---|
[1219] | 376 | |
---|
[1502] | 377 | if (keyBinder_) |
---|
| 378 | delete keyBinder_; |
---|
[1219] | 379 | |
---|
[1502] | 380 | if (keyDetector_) |
---|
| 381 | delete keyDetector_; |
---|
| 382 | |
---|
| 383 | if (calibratorCallback_) |
---|
| 384 | delete calibratorCallback_; |
---|
| 385 | |
---|
[1219] | 386 | keyHandlers_.clear(); |
---|
| 387 | mouseHandlers_.clear(); |
---|
| 388 | joyStickHandlers_.clear(); |
---|
| 389 | |
---|
| 390 | _destroyKeyboard(); |
---|
| 391 | _destroyMouse(); |
---|
| 392 | _destroyJoySticks(); |
---|
| 393 | |
---|
[1502] | 394 | activeHandlers_.clear(); |
---|
| 395 | |
---|
[1219] | 396 | // inputSystem_ can never be 0, or else the code is mistaken |
---|
| 397 | OIS::InputManager::destroyInputSystem(inputSystem_); |
---|
| 398 | inputSystem_ = 0; |
---|
| 399 | |
---|
| 400 | state_ = IS_UNINIT; |
---|
[1293] | 401 | CCOUT(ORX_DEBUG) << "Destroying done." << std::endl; |
---|
[1219] | 402 | } |
---|
[928] | 403 | } |
---|
| 404 | |
---|
| 405 | /** |
---|
[1219] | 406 | @brief Destroys the keyboard and sets it to 0. |
---|
| 407 | */ |
---|
| 408 | void InputManager::_destroyKeyboard() |
---|
| 409 | { |
---|
| 410 | if (keyboard_) |
---|
| 411 | // inputSystem_ can never be 0, or else the code is mistaken |
---|
| 412 | inputSystem_->destroyInputObject(keyboard_); |
---|
| 413 | keyboard_ = 0; |
---|
| 414 | activeKeyHandlers_.clear(); |
---|
| 415 | keysDown_.clear(); |
---|
| 416 | CCOUT(ORX_DEBUG) << "Keyboard destroyed." << std::endl; |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | /** |
---|
| 420 | @brief Destroys the mouse and sets it to 0. |
---|
| 421 | */ |
---|
| 422 | void InputManager::_destroyMouse() |
---|
| 423 | { |
---|
| 424 | if (mouse_) |
---|
| 425 | // inputSystem_ can never be 0, or else the code is mistaken |
---|
| 426 | inputSystem_->destroyInputObject(mouse_); |
---|
| 427 | mouse_ = 0; |
---|
| 428 | activeMouseHandlers_.clear(); |
---|
| 429 | mouseButtonsDown_.clear(); |
---|
| 430 | CCOUT(ORX_DEBUG) << "Mouse destroyed." << std::endl; |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | /** |
---|
| 434 | @brief Destroys all the joy sticks and resizes the lists to 0. |
---|
| 435 | */ |
---|
| 436 | void InputManager::_destroyJoySticks() |
---|
| 437 | { |
---|
[1293] | 438 | if (joySticksSize_ > 0) |
---|
[1219] | 439 | { |
---|
| 440 | // note: inputSystem_ can never be 0, or else the code is mistaken |
---|
[1293] | 441 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
[1219] | 442 | if (joySticks_[i] != 0) |
---|
| 443 | inputSystem_->destroyInputObject(joySticks_[i]); |
---|
| 444 | |
---|
| 445 | joySticks_.clear(); |
---|
[1293] | 446 | joySticksSize_ = 0; |
---|
[1219] | 447 | activeJoyStickHandlers_.clear(); |
---|
| 448 | joyStickButtonsDown_.clear(); |
---|
[1502] | 449 | povStates_.clear(); |
---|
| 450 | sliderStates_.clear(); |
---|
| 451 | joySticksCalibration_.clear(); |
---|
[1219] | 452 | } |
---|
| 453 | CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl; |
---|
| 454 | } |
---|
| 455 | |
---|
[1502] | 456 | void InputManager::_saveState() |
---|
| 457 | { |
---|
| 458 | savedHandlers_.activeHandlers_ = activeHandlers_; |
---|
| 459 | savedHandlers_.activeJoyStickHandlers_ = activeJoyStickHandlers_; |
---|
| 460 | savedHandlers_.activeKeyHandlers_ = activeKeyHandlers_; |
---|
| 461 | savedHandlers_.activeMouseHandlers_ = activeMouseHandlers_; |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | void InputManager::_restoreState() |
---|
| 465 | { |
---|
| 466 | activeHandlers_ = savedHandlers_.activeHandlers_; |
---|
| 467 | activeJoyStickHandlers_ = savedHandlers_.activeJoyStickHandlers_; |
---|
| 468 | activeKeyHandlers_ = savedHandlers_.activeKeyHandlers_; |
---|
| 469 | activeMouseHandlers_ = savedHandlers_.activeMouseHandlers_; |
---|
| 470 | } |
---|
| 471 | |
---|
[1349] | 472 | void InputManager::_updateTickables() |
---|
| 473 | { |
---|
[1502] | 474 | // we can use a map to have a list of unique pointers (an object can implement all 3 handlers) |
---|
| 475 | std::map<InputTickable*, HandlerState> tempSet; |
---|
[1349] | 476 | for (unsigned int iHandler = 0; iHandler < activeKeyHandlers_.size(); iHandler++) |
---|
[1502] | 477 | tempSet[activeKeyHandlers_[iHandler]].joyStick = true; |
---|
[1349] | 478 | for (unsigned int iHandler = 0; iHandler < activeMouseHandlers_.size(); iHandler++) |
---|
[1502] | 479 | tempSet[activeMouseHandlers_[iHandler]].mouse = true; |
---|
[1349] | 480 | for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) |
---|
| 481 | for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) |
---|
[1502] | 482 | tempSet[activeJoyStickHandlers_[iJoyStick][iHandler]].joyStick = true; |
---|
[1219] | 483 | |
---|
[1502] | 484 | // copy the content of the map back to the actual vector |
---|
[1349] | 485 | activeHandlers_.clear(); |
---|
[1502] | 486 | for (std::map<InputTickable*, HandlerState>::const_iterator itHandler = tempSet.begin(); |
---|
| 487 | itHandler != tempSet.end(); itHandler++) |
---|
| 488 | activeHandlers_.push_back(std::pair<InputTickable*, HandlerState>((*itHandler).first, (*itHandler).second)); |
---|
[1349] | 489 | } |
---|
| 490 | |
---|
| 491 | |
---|
[1219] | 492 | // ################################# |
---|
| 493 | // ### Private Interface Methods ### |
---|
| 494 | // ################################# |
---|
| 495 | // ################################# |
---|
| 496 | |
---|
| 497 | /** |
---|
| 498 | @brief Updates the InputManager. Tick is called by Orxonox. |
---|
[919] | 499 | @param dt Delta time |
---|
| 500 | */ |
---|
[1524] | 501 | void InputManager::_tick(float dt) |
---|
[918] | 502 | { |
---|
[1219] | 503 | if (state_ == IS_UNINIT) |
---|
| 504 | return; |
---|
| 505 | |
---|
| 506 | if (state_ != stateRequest_) |
---|
[1022] | 507 | { |
---|
[1502] | 508 | InputState sr = stateRequest_; |
---|
| 509 | switch (sr) |
---|
[1022] | 510 | { |
---|
[1502] | 511 | case IS_NORMAL: |
---|
[1219] | 512 | activeKeyHandlers_.clear(); |
---|
| 513 | activeMouseHandlers_.clear(); |
---|
[1293] | 514 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
| 515 | activeJoyStickHandlers_[i].clear(); |
---|
[1219] | 516 | |
---|
[1502] | 517 | // normal play mode |
---|
| 518 | // note: we assume that the handlers exist since otherwise, something's wrong anyway. |
---|
| 519 | enableKeyHandler("keybinder"); |
---|
| 520 | enableMouseHandler("keybinder"); |
---|
| 521 | enableJoyStickHandler("keybinder", 0); |
---|
| 522 | stateRequest_ = IS_NORMAL; |
---|
| 523 | state_ = IS_NORMAL; |
---|
| 524 | break; |
---|
| 525 | |
---|
| 526 | case IS_GUI: |
---|
| 527 | state_ = IS_GUI; |
---|
| 528 | break; |
---|
| 529 | |
---|
| 530 | case IS_CONSOLE: |
---|
| 531 | activeKeyHandlers_.clear(); |
---|
| 532 | activeMouseHandlers_.clear(); |
---|
| 533 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
| 534 | activeJoyStickHandlers_[i].clear(); |
---|
| 535 | |
---|
| 536 | enableMouseHandler("keybinder"); |
---|
| 537 | enableJoyStickHandler("keybinder", 0); |
---|
| 538 | enableKeyHandler("buffer"); |
---|
| 539 | stateRequest_ = IS_CONSOLE; |
---|
| 540 | state_ = IS_CONSOLE; |
---|
| 541 | break; |
---|
| 542 | |
---|
| 543 | case IS_DETECT: |
---|
| 544 | savedState_ = state_; |
---|
| 545 | _saveState(); |
---|
| 546 | |
---|
| 547 | activeKeyHandlers_.clear(); |
---|
| 548 | activeMouseHandlers_.clear(); |
---|
| 549 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
| 550 | activeJoyStickHandlers_[i].clear(); |
---|
| 551 | |
---|
| 552 | enableKeyHandler("keydetector"); |
---|
| 553 | enableMouseHandler("keydetector"); |
---|
| 554 | enableJoyStickHandler("keydetector", 0); |
---|
| 555 | |
---|
| 556 | stateRequest_ = IS_DETECT; |
---|
| 557 | state_ = IS_DETECT; |
---|
| 558 | break; |
---|
| 559 | |
---|
| 560 | case IS_NODETECT: |
---|
| 561 | _restoreState(); |
---|
| 562 | keysDown_.clear(); |
---|
| 563 | mouseButtonsDown_.clear(); |
---|
| 564 | joyStickButtonsDown_[0].clear(); |
---|
| 565 | state_ = IS_NODETECT; |
---|
| 566 | stateRequest_ = savedState_; |
---|
| 567 | break; |
---|
| 568 | |
---|
| 569 | case IS_CALIBRATE: |
---|
| 570 | if (joySticksSize_) |
---|
[1219] | 571 | { |
---|
[1502] | 572 | savedState_ = _getSingleton().state_; |
---|
| 573 | for (unsigned int i = 0; i < 24; i++) |
---|
| 574 | { |
---|
| 575 | marginalsMax_[i] = INT_MIN; |
---|
| 576 | marginalsMin_[i] = INT_MAX; |
---|
| 577 | } |
---|
| 578 | COUT(0) << "Move all joy stick axes in all directions a few times. " |
---|
| 579 | << "Then put all axes in zero state and hit enter." << std::endl; |
---|
[1219] | 580 | |
---|
[1502] | 581 | savedState_ = state_; |
---|
| 582 | _saveState(); |
---|
[1219] | 583 | |
---|
[1502] | 584 | activeKeyHandlers_.clear(); |
---|
| 585 | activeMouseHandlers_.clear(); |
---|
| 586 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
| 587 | activeJoyStickHandlers_[i].clear(); |
---|
[1219] | 588 | |
---|
[1502] | 589 | enableKeyHandler("calibratorcallback"); |
---|
| 590 | stateRequest_ = IS_CALIBRATE; |
---|
| 591 | state_ = IS_CALIBRATE; |
---|
[1219] | 592 | } |
---|
[1502] | 593 | else |
---|
| 594 | { |
---|
| 595 | COUT(3) << "Connot calibrate, no joy stick found!" << std::endl; |
---|
| 596 | stateRequest_ = state_; |
---|
| 597 | } |
---|
| 598 | break; |
---|
| 599 | |
---|
| 600 | case IS_NOCALIBRATE: |
---|
| 601 | _completeCalibration(); |
---|
| 602 | _restoreState(); |
---|
| 603 | keyBinder_->resetJoyStickAxes(); |
---|
| 604 | state_ = IS_NOCALIBRATE; |
---|
| 605 | stateRequest_ = savedState_; |
---|
| 606 | break; |
---|
| 607 | |
---|
| 608 | case IS_NONE: |
---|
| 609 | activeKeyHandlers_.clear(); |
---|
| 610 | activeMouseHandlers_.clear(); |
---|
| 611 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
| 612 | activeJoyStickHandlers_[i].clear(); |
---|
| 613 | state_ = IS_NONE; |
---|
| 614 | |
---|
| 615 | default: |
---|
| 616 | break; |
---|
[1219] | 617 | } |
---|
| 618 | } |
---|
| 619 | |
---|
| 620 | // Capture all the input. This calls the event handlers in InputManager. |
---|
| 621 | if (mouse_) |
---|
| 622 | mouse_->capture(); |
---|
| 623 | if (keyboard_) |
---|
| 624 | keyboard_->capture(); |
---|
[1293] | 625 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
| 626 | joySticks_[i]->capture(); |
---|
[1219] | 627 | |
---|
[1502] | 628 | if (state_ != IS_CALIBRATE) |
---|
| 629 | { |
---|
| 630 | // call all the handlers for the held key events |
---|
| 631 | for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++) |
---|
| 632 | for (unsigned int iHandler = 0; iHandler < activeKeyHandlers_.size(); iHandler++) |
---|
| 633 | activeKeyHandlers_[iHandler]->keyHeld(KeyEvent(keysDown_[iKey], keyboardModifiers_)); |
---|
[1219] | 634 | |
---|
[1502] | 635 | // call all the handlers for the held mouse button events |
---|
| 636 | for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++) |
---|
| 637 | for (unsigned int iHandler = 0; iHandler < activeMouseHandlers_.size(); iHandler++) |
---|
| 638 | activeMouseHandlers_[iHandler]->mouseButtonHeld(mouseButtonsDown_[iButton]); |
---|
[1219] | 639 | |
---|
[1502] | 640 | // call all the handlers for the held joy stick button events |
---|
| 641 | for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) |
---|
| 642 | for (unsigned int iButton = 0; iButton < joyStickButtonsDown_[iJoyStick].size(); iButton++) |
---|
| 643 | for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) |
---|
| 644 | activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonHeld(iJoyStick, joyStickButtonsDown_[iJoyStick][iButton]); |
---|
| 645 | } |
---|
[1219] | 646 | |
---|
[1502] | 647 | // call the ticks for the handlers (need to be treated specially) |
---|
| 648 | for (unsigned int iHandler = 0; iHandler < activeHandlers_.size(); iHandler++) |
---|
| 649 | activeHandlers_[iHandler].first->tickInput(dt, activeHandlers_[iHandler].second); |
---|
| 650 | } |
---|
[1524] | 651 | |
---|
[1502] | 652 | void InputManager::_completeCalibration() |
---|
| 653 | { |
---|
| 654 | for (unsigned int i = 0; i < 24; i++) |
---|
| 655 | { |
---|
| 656 | // positive coefficient |
---|
| 657 | if (marginalsMax_[i] == INT_MIN) |
---|
| 658 | marginalsMax_[i] = 32767; |
---|
| 659 | // coefficients |
---|
| 660 | if (marginalsMax_[i] - joySticksCalibration_[0].zeroStates[i]) |
---|
| 661 | joySticksCalibration_[0].positiveCoeff[i] = 1.0f/(marginalsMax_[i] - joySticksCalibration_[0].zeroStates[i]); |
---|
| 662 | else |
---|
[1505] | 663 | joySticksCalibration_[0].positiveCoeff[i] = 1.0f; |
---|
[1349] | 664 | |
---|
[1502] | 665 | // config value |
---|
[1505] | 666 | ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer("CoeffPos"); |
---|
| 667 | assert(cont); |
---|
| 668 | cont->set(i, joySticksCalibration_[0].positiveCoeff[i]); |
---|
| 669 | |
---|
[1502] | 670 | // negative coefficient |
---|
| 671 | if (marginalsMin_[i] == INT_MAX) |
---|
| 672 | marginalsMin_[i] = -32768; |
---|
| 673 | // coefficients |
---|
| 674 | if (marginalsMin_[i] - joySticksCalibration_[0].zeroStates[i]) |
---|
[1505] | 675 | joySticksCalibration_[0].negativeCoeff[i] = -1.0f/(marginalsMin_[i] - joySticksCalibration_[0].zeroStates[i]); |
---|
| 676 | else |
---|
| 677 | joySticksCalibration_[0].negativeCoeff[i] = 1.0f; |
---|
[1502] | 678 | // config value |
---|
[1505] | 679 | cont = getIdentifier()->getConfigValueContainer("CoeffNeg"); |
---|
| 680 | assert(cont); |
---|
| 681 | cont->set(i, joySticksCalibration_[0].negativeCoeff[i]); |
---|
| 682 | |
---|
[1502] | 683 | // zero states |
---|
| 684 | if (i < 8) |
---|
| 685 | { |
---|
| 686 | if (!(i & 1)) |
---|
| 687 | joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mSliders[i/2].abX; |
---|
| 688 | else |
---|
| 689 | joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mSliders[i/2].abY; |
---|
| 690 | } |
---|
| 691 | else |
---|
| 692 | { |
---|
| 693 | if (i - 8 < joySticks_[0]->getJoyStickState().mAxes.size()) |
---|
| 694 | joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mAxes[i - 8].abs; |
---|
| 695 | else |
---|
| 696 | joySticksCalibration_[0].zeroStates[i] = 0; |
---|
| 697 | } |
---|
| 698 | // config value |
---|
[1505] | 699 | cont = getIdentifier()->getConfigValueContainer("Zero"); |
---|
| 700 | assert(cont); |
---|
| 701 | cont->set(i, joySticksCalibration_[0].zeroStates[i]); |
---|
[1502] | 702 | } |
---|
[1219] | 703 | } |
---|
| 704 | |
---|
| 705 | // ###### Key Events ###### |
---|
| 706 | |
---|
| 707 | /** |
---|
| 708 | @brief Event handler for the keyPressed Event. |
---|
| 709 | @param e Event information |
---|
| 710 | */ |
---|
| 711 | bool InputManager::keyPressed(const OIS::KeyEvent &e) |
---|
| 712 | { |
---|
| 713 | // check whether the key already is in the list (can happen when focus was lost) |
---|
[1293] | 714 | unsigned int iKey = 0; |
---|
| 715 | while (iKey < keysDown_.size() && keysDown_[iKey].key != (KeyCode::Enum)e.key) |
---|
| 716 | iKey++; |
---|
| 717 | if (iKey == keysDown_.size()) |
---|
| 718 | keysDown_.push_back(Key(e)); |
---|
[1219] | 719 | |
---|
[1293] | 720 | // update modifiers |
---|
| 721 | if(e.key == OIS::KC_RMENU || e.key == OIS::KC_LMENU) |
---|
| 722 | keyboardModifiers_ |= KeyboardModifier::Alt; // alt key |
---|
| 723 | if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL) |
---|
| 724 | keyboardModifiers_ |= KeyboardModifier::Ctrl; // ctrl key |
---|
| 725 | if(e.key == OIS::KC_RSHIFT || e.key == OIS::KC_LSHIFT) |
---|
| 726 | keyboardModifiers_ |= KeyboardModifier::Shift; // shift key |
---|
| 727 | |
---|
[1219] | 728 | for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++) |
---|
[1293] | 729 | activeKeyHandlers_[i]->keyPressed(KeyEvent(e, keyboardModifiers_)); |
---|
[1219] | 730 | |
---|
| 731 | return true; |
---|
| 732 | } |
---|
| 733 | |
---|
| 734 | /** |
---|
| 735 | @brief Event handler for the keyReleased Event. |
---|
| 736 | @param e Event information |
---|
| 737 | */ |
---|
| 738 | bool InputManager::keyReleased(const OIS::KeyEvent &e) |
---|
| 739 | { |
---|
| 740 | // remove the key from the keysDown_ list |
---|
[1293] | 741 | for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++) |
---|
[1219] | 742 | { |
---|
[1293] | 743 | if (keysDown_[iKey].key == (KeyCode::Enum)e.key) |
---|
[1219] | 744 | { |
---|
[1293] | 745 | keysDown_.erase(keysDown_.begin() + iKey); |
---|
[1022] | 746 | break; |
---|
[1219] | 747 | } |
---|
| 748 | } |
---|
| 749 | |
---|
[1293] | 750 | // update modifiers |
---|
| 751 | if(e.key == OIS::KC_RMENU || e.key == OIS::KC_LMENU) |
---|
| 752 | keyboardModifiers_ &= ~KeyboardModifier::Alt; // alt key |
---|
| 753 | if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL) |
---|
| 754 | keyboardModifiers_ &= ~KeyboardModifier::Ctrl; // ctrl key |
---|
| 755 | if(e.key == OIS::KC_RSHIFT || e.key == OIS::KC_LSHIFT) |
---|
| 756 | keyboardModifiers_ &= ~KeyboardModifier::Shift; // shift key |
---|
| 757 | |
---|
[1219] | 758 | for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++) |
---|
[1293] | 759 | activeKeyHandlers_[i]->keyReleased(KeyEvent(e, keyboardModifiers_)); |
---|
[1219] | 760 | |
---|
| 761 | return true; |
---|
| 762 | } |
---|
| 763 | |
---|
| 764 | |
---|
| 765 | // ###### Mouse Events ###### |
---|
| 766 | |
---|
| 767 | /** |
---|
| 768 | @brief Event handler for the mouseMoved Event. |
---|
| 769 | @param e Event information |
---|
| 770 | */ |
---|
| 771 | bool InputManager::mouseMoved(const OIS::MouseEvent &e) |
---|
| 772 | { |
---|
[1293] | 773 | // check for actual moved event |
---|
| 774 | if (e.state.X.rel != 0 || e.state.Y.rel != 0) |
---|
| 775 | { |
---|
| 776 | for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) |
---|
[1349] | 777 | activeMouseHandlers_[i]->mouseMoved(IntVector2(e.state.X.abs, e.state.Y.abs), |
---|
| 778 | IntVector2(e.state.X.rel, e.state.Y.rel), IntVector2(e.state.width, e.state.height)); |
---|
[1293] | 779 | } |
---|
[1219] | 780 | |
---|
[1293] | 781 | // check for mouse scrolled event |
---|
| 782 | if (e.state.Z.rel != 0) |
---|
| 783 | { |
---|
| 784 | for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) |
---|
[1349] | 785 | activeMouseHandlers_[i]->mouseScrolled(e.state.Z.abs, e.state.Z.rel); |
---|
[1293] | 786 | } |
---|
| 787 | |
---|
[1219] | 788 | return true; |
---|
| 789 | } |
---|
| 790 | |
---|
| 791 | /** |
---|
| 792 | @brief Event handler for the mousePressed Event. |
---|
| 793 | @param e Event information |
---|
| 794 | @param id The ID of the mouse button |
---|
| 795 | */ |
---|
| 796 | bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
| 797 | { |
---|
| 798 | // check whether the button already is in the list (can happen when focus was lost) |
---|
[1293] | 799 | unsigned int iButton = 0; |
---|
| 800 | while (iButton < mouseButtonsDown_.size() && mouseButtonsDown_[iButton] != (MouseButton::Enum)id) |
---|
| 801 | iButton++; |
---|
| 802 | if (iButton == mouseButtonsDown_.size()) |
---|
| 803 | mouseButtonsDown_.push_back((MouseButton::Enum)id); |
---|
[1219] | 804 | |
---|
| 805 | for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) |
---|
[1349] | 806 | activeMouseHandlers_[i]->mouseButtonPressed((MouseButton::Enum)id); |
---|
[1219] | 807 | |
---|
| 808 | return true; |
---|
| 809 | } |
---|
| 810 | |
---|
| 811 | /** |
---|
| 812 | @brief Event handler for the mouseReleased Event. |
---|
| 813 | @param e Event information |
---|
| 814 | @param id The ID of the mouse button |
---|
| 815 | */ |
---|
| 816 | bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
| 817 | { |
---|
| 818 | // remove the button from the keysDown_ list |
---|
[1293] | 819 | for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++) |
---|
[1219] | 820 | { |
---|
[1293] | 821 | if (mouseButtonsDown_[iButton] == (MouseButton::Enum)id) |
---|
[1219] | 822 | { |
---|
[1293] | 823 | mouseButtonsDown_.erase(mouseButtonsDown_.begin() + iButton); |
---|
[1022] | 824 | break; |
---|
| 825 | } |
---|
| 826 | } |
---|
| 827 | |
---|
[1219] | 828 | for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) |
---|
[1349] | 829 | activeMouseHandlers_[i]->mouseButtonReleased((MouseButton::Enum)id); |
---|
[918] | 830 | |
---|
[1219] | 831 | return true; |
---|
[918] | 832 | } |
---|
| 833 | |
---|
[1219] | 834 | |
---|
| 835 | // ###### Joy Stick Events ###### |
---|
| 836 | |
---|
[1502] | 837 | inline unsigned int InputManager::_getJoystick(const OIS::JoyStickEvent& arg) |
---|
| 838 | { |
---|
| 839 | // use the device to identify which one called the method |
---|
| 840 | OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device; |
---|
[1293] | 841 | unsigned int iJoyStick = 0; |
---|
| 842 | while (joySticks_[iJoyStick] != joyStick) |
---|
[1502] | 843 | { |
---|
[1293] | 844 | iJoyStick++; |
---|
[1502] | 845 | if (iJoyStick == joySticksSize_) |
---|
| 846 | { |
---|
| 847 | CCOUT(3) << "Unknown joystick fired an event. This means there is a bug somewhere! Aborting." << std::endl; |
---|
| 848 | abort(); |
---|
| 849 | } |
---|
| 850 | } |
---|
| 851 | return iJoyStick; |
---|
| 852 | } |
---|
| 853 | |
---|
| 854 | bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button) |
---|
| 855 | { |
---|
| 856 | unsigned int iJoyStick = _getJoystick(arg); |
---|
| 857 | |
---|
[1219] | 858 | // check whether the button already is in the list (can happen when focus was lost) |
---|
[1502] | 859 | std::vector<int>& buttonsDown = joyStickButtonsDown_[iJoyStick]; |
---|
[1293] | 860 | unsigned int iButton = 0; |
---|
| 861 | while (iButton < buttonsDown.size() && buttonsDown[iButton] != button) |
---|
| 862 | iButton++; |
---|
| 863 | if (iButton == buttonsDown.size()) |
---|
| 864 | buttonsDown.push_back(button); |
---|
[1219] | 865 | |
---|
[1293] | 866 | for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) |
---|
[1349] | 867 | activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonPressed(iJoyStick, button); |
---|
[1219] | 868 | |
---|
| 869 | return true; |
---|
[1502] | 870 | } |
---|
| 871 | |
---|
| 872 | bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button) |
---|
| 873 | { |
---|
| 874 | unsigned int iJoyStick = _getJoystick(arg); |
---|
| 875 | |
---|
[1219] | 876 | // remove the button from the joyStickButtonsDown_ list |
---|
[1502] | 877 | std::vector<int>& buttonsDown = joyStickButtonsDown_[iJoyStick]; |
---|
[1293] | 878 | for (unsigned int iButton = 0; iButton < buttonsDown.size(); iButton++) |
---|
[1219] | 879 | { |
---|
[1293] | 880 | if (buttonsDown[iButton] == button) |
---|
[1219] | 881 | { |
---|
[1293] | 882 | buttonsDown.erase(buttonsDown.begin() + iButton); |
---|
[1219] | 883 | break; |
---|
| 884 | } |
---|
| 885 | } |
---|
| 886 | |
---|
[1293] | 887 | for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) |
---|
[1349] | 888 | activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonReleased(iJoyStick, button); |
---|
[1219] | 889 | |
---|
| 890 | return true; |
---|
[1502] | 891 | } |
---|
[1219] | 892 | |
---|
[1502] | 893 | void InputManager::_fireAxis(unsigned int iJoyStick, int axis, int value) |
---|
| 894 | { |
---|
| 895 | if (state_ == IS_CALIBRATE) |
---|
[1349] | 896 | { |
---|
[1502] | 897 | if (value > marginalsMax_[axis]) |
---|
| 898 | marginalsMax_[axis] = value; |
---|
| 899 | if (value < marginalsMin_[axis]) |
---|
| 900 | marginalsMin_[axis] = value; |
---|
[1349] | 901 | } |
---|
[1502] | 902 | else |
---|
[1349] | 903 | { |
---|
[1502] | 904 | float fValue = value - joySticksCalibration_[iJoyStick].zeroStates[axis]; |
---|
| 905 | if (fValue > 0.0f) |
---|
| 906 | fValue *= joySticksCalibration_[iJoyStick].positiveCoeff[axis]; |
---|
| 907 | else |
---|
| 908 | fValue *= joySticksCalibration_[iJoyStick].negativeCoeff[axis]; |
---|
| 909 | |
---|
[1349] | 910 | for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) |
---|
[1502] | 911 | activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(iJoyStick, axis, fValue); |
---|
[1349] | 912 | } |
---|
[1502] | 913 | } |
---|
[1293] | 914 | |
---|
[1502] | 915 | bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis) |
---|
| 916 | { |
---|
| 917 | //if (arg.state.mAxes[axis].abs > 10000 || arg.state.mAxes[axis].abs < -10000) |
---|
| 918 | //{ CCOUT(3) << "axis " << axis << " moved" << arg.state.mAxes[axis].abs << std::endl;} |
---|
| 919 | |
---|
| 920 | unsigned int iJoyStick = _getJoystick(arg); |
---|
| 921 | |
---|
| 922 | // keep in mind that the first 8 axes are reserved for the sliders |
---|
| 923 | _fireAxis(iJoyStick, axis + 8, arg.state.mAxes[axis].abs); |
---|
| 924 | |
---|
| 925 | return true; |
---|
| 926 | } |
---|
| 927 | |
---|
| 928 | bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id) |
---|
| 929 | { |
---|
| 930 | //if (arg.state.mSliders[id].abX > 10000 || arg.state.mSliders[id].abX < -10000) |
---|
| 931 | //{CCOUT(3) << "slider " << id << " moved" << arg.state.mSliders[id].abX << std::endl;} |
---|
| 932 | //CCOUT(3) << arg.state.mSliders[id].abX << "\t |" << arg.state.mSliders[id].abY << std::endl; |
---|
| 933 | |
---|
| 934 | unsigned int iJoyStick = _getJoystick(arg); |
---|
| 935 | |
---|
| 936 | if (sliderStates_[iJoyStick].sliderStates[id].x != arg.state.mSliders[id].abX) |
---|
| 937 | _fireAxis(iJoyStick, id * 2, arg.state.mSliders[id].abX); |
---|
| 938 | else if (sliderStates_[iJoyStick].sliderStates[id].y != arg.state.mSliders[id].abY) |
---|
| 939 | _fireAxis(iJoyStick, id * 2 + 1, arg.state.mSliders[id].abY); |
---|
| 940 | |
---|
| 941 | return true; |
---|
| 942 | } |
---|
| 943 | |
---|
| 944 | bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id) |
---|
| 945 | { |
---|
| 946 | unsigned int iJoyStick = _getJoystick(arg); |
---|
| 947 | |
---|
[1349] | 948 | // translate the POV into 8 simple buttons |
---|
| 949 | int lastState = povStates_[iJoyStick][id]; |
---|
| 950 | if (lastState & OIS::Pov::North) |
---|
| 951 | buttonReleased(arg, 32 + id * 4 + 0); |
---|
| 952 | if (lastState & OIS::Pov::South) |
---|
| 953 | buttonReleased(arg, 32 + id * 4 + 1); |
---|
| 954 | if (lastState & OIS::Pov::East) |
---|
| 955 | buttonReleased(arg, 32 + id * 4 + 2); |
---|
| 956 | if (lastState & OIS::Pov::West) |
---|
| 957 | buttonReleased(arg, 32 + id * 4 + 3); |
---|
[1502] | 958 | |
---|
[1349] | 959 | povStates_[iJoyStick].povStates[id] = arg.state.mPOV[id].direction; |
---|
[1502] | 960 | |
---|
[1349] | 961 | int currentState = povStates_[iJoyStick][id]; |
---|
| 962 | if (currentState & OIS::Pov::North) |
---|
| 963 | buttonPressed(arg, 32 + id * 4 + 0); |
---|
| 964 | if (currentState & OIS::Pov::South) |
---|
| 965 | buttonPressed(arg, 32 + id * 4 + 1); |
---|
| 966 | if (currentState & OIS::Pov::East) |
---|
| 967 | buttonPressed(arg, 32 + id * 4 + 2); |
---|
| 968 | if (currentState & OIS::Pov::West) |
---|
| 969 | buttonPressed(arg, 32 + id * 4 + 3); |
---|
[1219] | 970 | |
---|
[1502] | 971 | return true; |
---|
| 972 | } |
---|
[1293] | 973 | |
---|
[1502] | 974 | /*bool InputManager::vector3Moved(const OIS::JoyStickEvent &arg, int id) |
---|
| 975 | { |
---|
| 976 | unsigned int iJoyStick = _getJoystick(arg); |
---|
| 977 | |
---|
[1293] | 978 | for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) |
---|
| 979 | activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickVector3Moved(JoyStickState(arg.state, iJoyStick), id); |
---|
[1219] | 980 | |
---|
[1502] | 981 | return true; |
---|
| 982 | }*/ |
---|
[1219] | 983 | |
---|
[1502] | 984 | |
---|
[1219] | 985 | // ################################ |
---|
| 986 | // ### Static Interface Methods ### |
---|
| 987 | // ################################ |
---|
| 988 | // ################################ |
---|
| 989 | |
---|
[1502] | 990 | std::string InputManager::bindingCommmandString_s = ""; |
---|
| 991 | |
---|
[1293] | 992 | bool InputManager::initialise(const size_t windowHnd, int windowWidth, int windowHeight, |
---|
| 993 | bool createKeyboard, bool createMouse, bool createJoySticks) |
---|
[1219] | 994 | { |
---|
| 995 | return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight, |
---|
| 996 | createKeyboard, createMouse, createJoySticks); |
---|
| 997 | } |
---|
| 998 | |
---|
| 999 | bool InputManager::initialiseKeyboard() |
---|
| 1000 | { |
---|
| 1001 | return _getSingleton()._initialiseKeyboard(); |
---|
| 1002 | } |
---|
| 1003 | |
---|
| 1004 | bool InputManager::initialiseMouse() |
---|
| 1005 | { |
---|
| 1006 | return _getSingleton()._initialiseMouse(); |
---|
| 1007 | } |
---|
| 1008 | |
---|
| 1009 | bool InputManager::initialiseJoySticks() |
---|
| 1010 | { |
---|
| 1011 | return _getSingleton()._initialiseJoySticks(); |
---|
| 1012 | } |
---|
| 1013 | |
---|
| 1014 | int InputManager::numberOfKeyboards() |
---|
| 1015 | { |
---|
| 1016 | if (_getSingleton().keyboard_ != 0) |
---|
| 1017 | return 1; |
---|
| 1018 | else |
---|
| 1019 | return 0; |
---|
| 1020 | } |
---|
| 1021 | |
---|
| 1022 | int InputManager::numberOfMice() |
---|
| 1023 | { |
---|
| 1024 | if (_getSingleton().mouse_ != 0) |
---|
| 1025 | return 1; |
---|
| 1026 | else |
---|
| 1027 | return 0; |
---|
| 1028 | } |
---|
| 1029 | |
---|
| 1030 | int InputManager::numberOfJoySticks() |
---|
| 1031 | { |
---|
[1293] | 1032 | return _getSingleton().joySticksSize_; |
---|
[1219] | 1033 | } |
---|
| 1034 | |
---|
[1502] | 1035 | /*bool InputManager::isKeyDown(KeyCode::Enum key) |
---|
[1293] | 1036 | { |
---|
| 1037 | if (_getSingleton().keyboard_) |
---|
| 1038 | return _getSingleton().keyboard_->isKeyDown((OIS::KeyCode)key); |
---|
| 1039 | else |
---|
| 1040 | return false; |
---|
[1502] | 1041 | }*/ |
---|
[1219] | 1042 | |
---|
[1502] | 1043 | /*bool InputManager::isModifierDown(KeyboardModifier::Enum modifier) |
---|
[1293] | 1044 | { |
---|
| 1045 | if (_getSingleton().keyboard_) |
---|
| 1046 | return isModifierDown(modifier); |
---|
| 1047 | else |
---|
| 1048 | return false; |
---|
[1502] | 1049 | }*/ |
---|
[1293] | 1050 | |
---|
[1349] | 1051 | /*const MouseState InputManager::getMouseState() |
---|
[1293] | 1052 | { |
---|
| 1053 | if (_getSingleton().mouse_) |
---|
| 1054 | return _getSingleton().mouse_->getMouseState(); |
---|
| 1055 | else |
---|
| 1056 | return MouseState(); |
---|
[1349] | 1057 | }*/ |
---|
[1293] | 1058 | |
---|
[1349] | 1059 | /*const JoyStickState InputManager::getJoyStickState(unsigned int ID) |
---|
[1293] | 1060 | { |
---|
| 1061 | if (ID < _getSingleton().joySticksSize_) |
---|
| 1062 | return JoyStickState(_getSingleton().joySticks_[ID]->getJoyStickState(), ID); |
---|
| 1063 | else |
---|
| 1064 | return JoyStickState(); |
---|
[1349] | 1065 | }*/ |
---|
[1293] | 1066 | |
---|
[1219] | 1067 | void InputManager::destroy() |
---|
| 1068 | { |
---|
| 1069 | _getSingleton()._destroy(); |
---|
| 1070 | } |
---|
| 1071 | |
---|
| 1072 | void InputManager::destroyKeyboard() |
---|
| 1073 | { |
---|
| 1074 | return _getSingleton()._destroyKeyboard(); |
---|
| 1075 | } |
---|
| 1076 | |
---|
| 1077 | void InputManager::destroyMouse() |
---|
| 1078 | { |
---|
| 1079 | return _getSingleton()._destroyMouse(); |
---|
| 1080 | } |
---|
| 1081 | |
---|
| 1082 | void InputManager::destroyJoySticks() |
---|
| 1083 | { |
---|
| 1084 | return _getSingleton()._destroyJoySticks(); |
---|
| 1085 | } |
---|
| 1086 | |
---|
| 1087 | |
---|
[919] | 1088 | /** |
---|
| 1089 | @brief Adjusts the mouse window metrics. |
---|
| 1090 | This method has to be called every time the size of the window changes. |
---|
| 1091 | @param width The new width of the render window |
---|
| 1092 | @param height the new height of the render window |
---|
| 1093 | */ |
---|
[1219] | 1094 | void InputManager::setWindowExtents(const int width, const int height) |
---|
[918] | 1095 | { |
---|
[1219] | 1096 | if (_getSingleton().mouse_) |
---|
| 1097 | { |
---|
| 1098 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
---|
| 1099 | const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState(); |
---|
| 1100 | mouseState.width = width; |
---|
| 1101 | mouseState.height = height; |
---|
| 1102 | } |
---|
[918] | 1103 | } |
---|
| 1104 | |
---|
[919] | 1105 | /** |
---|
[1022] | 1106 | @brief Sets the input mode to either GUI, inGame or Buffer |
---|
| 1107 | @param mode The new input mode |
---|
| 1108 | @remark Only has an affect if the mode actually changes |
---|
[922] | 1109 | */ |
---|
[1219] | 1110 | void InputManager::setInputState(const InputState state) |
---|
[922] | 1111 | { |
---|
[1219] | 1112 | _getSingleton().stateRequest_ = state; |
---|
[922] | 1113 | } |
---|
| 1114 | |
---|
| 1115 | /** |
---|
[1022] | 1116 | @brief Returns the current input handling method |
---|
| 1117 | @return The current input mode. |
---|
[919] | 1118 | */ |
---|
[1219] | 1119 | InputManager::InputState InputManager::getInputState() |
---|
[918] | 1120 | { |
---|
[1219] | 1121 | return _getSingleton().state_; |
---|
[918] | 1122 | } |
---|
| 1123 | |
---|
[1502] | 1124 | void InputManager::storeKeyStroke(const std::string& name) |
---|
| 1125 | { |
---|
| 1126 | setInputState(IS_NODETECT); |
---|
| 1127 | COUT(0) << "Binding string \"" << bindingCommmandString_s << "\" on key '" << name << "'" << std::endl; |
---|
| 1128 | CommandExecutor::execute("config KeyBinder " + name + " " + bindingCommmandString_s, false); |
---|
| 1129 | } |
---|
[1219] | 1130 | |
---|
[1502] | 1131 | void InputManager::keyBind(const std::string& command) |
---|
| 1132 | { |
---|
| 1133 | bindingCommmandString_s = command; |
---|
| 1134 | setInputState(IS_DETECT); |
---|
| 1135 | COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl; |
---|
| 1136 | } |
---|
| 1137 | |
---|
| 1138 | void InputManager::calibrate() |
---|
| 1139 | { |
---|
| 1140 | _getSingleton().setInputState(IS_CALIBRATE); |
---|
| 1141 | } |
---|
| 1142 | |
---|
[1524] | 1143 | void InputManager::tick(float dt) |
---|
| 1144 | { |
---|
| 1145 | _getSingleton()._tick(dt); |
---|
| 1146 | } |
---|
| 1147 | |
---|
[1219] | 1148 | // ###### KeyHandler ###### |
---|
| 1149 | |
---|
| 1150 | /** |
---|
| 1151 | @brief Adds a new key handler. |
---|
| 1152 | @param handler Pointer to the handler object. |
---|
| 1153 | @param name Unique name of the handler. |
---|
| 1154 | @return True if added, false if name already existed. |
---|
| 1155 | */ |
---|
| 1156 | bool InputManager::addKeyHandler(KeyHandler* handler, const std::string& name) |
---|
[1066] | 1157 | { |
---|
[1293] | 1158 | if (!handler) |
---|
| 1159 | return false; |
---|
[1219] | 1160 | if (_getSingleton().keyHandlers_.find(name) == _getSingleton().keyHandlers_.end()) |
---|
| 1161 | { |
---|
| 1162 | _getSingleton().keyHandlers_[name] = handler; |
---|
| 1163 | return true; |
---|
| 1164 | } |
---|
| 1165 | else |
---|
| 1166 | return false; |
---|
[1066] | 1167 | } |
---|
| 1168 | |
---|
[1219] | 1169 | /** |
---|
| 1170 | @brief Removes a Key handler from the list. |
---|
| 1171 | @param name Unique name of the handler. |
---|
| 1172 | @return True if removal was successful, false if name was not found. |
---|
| 1173 | */ |
---|
| 1174 | bool InputManager::removeKeyHandler(const std::string &name) |
---|
| 1175 | { |
---|
| 1176 | disableKeyHandler(name); |
---|
| 1177 | std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name); |
---|
| 1178 | if (it != _getSingleton().keyHandlers_.end()) |
---|
| 1179 | { |
---|
| 1180 | _getSingleton().keyHandlers_.erase(it); |
---|
| 1181 | return true; |
---|
| 1182 | } |
---|
| 1183 | else |
---|
| 1184 | return false; |
---|
| 1185 | } |
---|
[1066] | 1186 | |
---|
[1219] | 1187 | /** |
---|
| 1188 | @brief Returns the pointer to a handler. |
---|
| 1189 | @param name Unique name of the handler. |
---|
| 1190 | @return Pointer to the instance, 0 if name was not found. |
---|
| 1191 | */ |
---|
| 1192 | KeyHandler* InputManager::getKeyHandler(const std::string& name) |
---|
| 1193 | { |
---|
| 1194 | std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name); |
---|
| 1195 | if (it != _getSingleton().keyHandlers_.end()) |
---|
| 1196 | { |
---|
| 1197 | return (*it).second; |
---|
| 1198 | } |
---|
| 1199 | else |
---|
| 1200 | return 0; |
---|
| 1201 | } |
---|
| 1202 | |
---|
| 1203 | /** |
---|
| 1204 | @brief Enables a specific key handler that has already been added. |
---|
| 1205 | @param name Unique name of the handler. |
---|
| 1206 | @return False if name was not found, true otherwise. |
---|
| 1207 | */ |
---|
| 1208 | bool InputManager::enableKeyHandler(const std::string& name) |
---|
| 1209 | { |
---|
| 1210 | // get pointer from the map with all stored handlers |
---|
| 1211 | std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name); |
---|
| 1212 | if (mapIt == _getSingleton().keyHandlers_.end()) |
---|
| 1213 | return false; |
---|
| 1214 | // see whether the handler already is in the list |
---|
| 1215 | for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin(); |
---|
| 1216 | it != _getSingleton().activeKeyHandlers_.end(); it++) |
---|
| 1217 | { |
---|
| 1218 | if ((*it) == (*mapIt).second) |
---|
| 1219 | { |
---|
| 1220 | return true; |
---|
| 1221 | } |
---|
| 1222 | } |
---|
| 1223 | _getSingleton().activeKeyHandlers_.push_back((*mapIt).second); |
---|
| 1224 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
[1349] | 1225 | _getSingleton()._updateTickables(); |
---|
[1219] | 1226 | return true; |
---|
| 1227 | } |
---|
| 1228 | |
---|
| 1229 | /** |
---|
| 1230 | @brief Disables a specific key handler. |
---|
| 1231 | @param name Unique name of the handler. |
---|
| 1232 | @return False if name was not found, true otherwise. |
---|
| 1233 | */ |
---|
| 1234 | bool InputManager::disableKeyHandler(const std::string &name) |
---|
| 1235 | { |
---|
| 1236 | // get pointer from the map with all stored handlers |
---|
| 1237 | std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name); |
---|
| 1238 | if (mapIt == _getSingleton().keyHandlers_.end()) |
---|
| 1239 | return false; |
---|
| 1240 | // look for the handler in the list |
---|
| 1241 | for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin(); |
---|
| 1242 | it != _getSingleton().activeKeyHandlers_.end(); it++) |
---|
| 1243 | { |
---|
| 1244 | if ((*it) == (*mapIt).second) |
---|
| 1245 | { |
---|
| 1246 | _getSingleton().activeKeyHandlers_.erase(it); |
---|
| 1247 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
[1349] | 1248 | _getSingleton()._updateTickables(); |
---|
[1219] | 1249 | return true; |
---|
| 1250 | } |
---|
| 1251 | } |
---|
| 1252 | return true; |
---|
| 1253 | } |
---|
| 1254 | |
---|
| 1255 | /** |
---|
| 1256 | @brief Checks whether a key handler is active |
---|
| 1257 | @param name Unique name of the handler. |
---|
| 1258 | @return False if key handler is not active or doesn't exist, true otherwise. |
---|
| 1259 | */ |
---|
| 1260 | bool InputManager::isKeyHandlerActive(const std::string& name) |
---|
| 1261 | { |
---|
| 1262 | // get pointer from the map with all stored handlers |
---|
| 1263 | std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name); |
---|
| 1264 | if (mapIt == _getSingleton().keyHandlers_.end()) |
---|
| 1265 | return false; |
---|
| 1266 | // see whether the handler already is in the list |
---|
| 1267 | for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin(); |
---|
| 1268 | it != _getSingleton().activeKeyHandlers_.end(); it++) |
---|
| 1269 | { |
---|
| 1270 | if ((*it) == (*mapIt).second) |
---|
| 1271 | return true; |
---|
| 1272 | } |
---|
| 1273 | return false; |
---|
| 1274 | } |
---|
| 1275 | |
---|
| 1276 | |
---|
| 1277 | // ###### MouseHandler ###### |
---|
| 1278 | /** |
---|
| 1279 | @brief Adds a new mouse handler. |
---|
| 1280 | @param handler Pointer to the handler object. |
---|
| 1281 | @param name Unique name of the handler. |
---|
| 1282 | @return True if added, false if name already existed. |
---|
| 1283 | */ |
---|
| 1284 | bool InputManager::addMouseHandler(MouseHandler* handler, const std::string& name) |
---|
| 1285 | { |
---|
[1293] | 1286 | if (!handler) |
---|
| 1287 | return false; |
---|
[1219] | 1288 | if (_getSingleton().mouseHandlers_.find(name) == _getSingleton().mouseHandlers_.end()) |
---|
| 1289 | { |
---|
| 1290 | _getSingleton().mouseHandlers_[name] = handler; |
---|
| 1291 | return true; |
---|
| 1292 | } |
---|
| 1293 | else |
---|
| 1294 | return false; |
---|
| 1295 | } |
---|
| 1296 | |
---|
| 1297 | /** |
---|
| 1298 | @brief Removes a Mouse handler from the list. |
---|
| 1299 | @param name Unique name of the handler. |
---|
| 1300 | @return True if removal was successful, false if name was not found. |
---|
| 1301 | */ |
---|
| 1302 | bool InputManager::removeMouseHandler(const std::string &name) |
---|
| 1303 | { |
---|
| 1304 | disableMouseHandler(name); |
---|
| 1305 | std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name); |
---|
| 1306 | if (it != _getSingleton().mouseHandlers_.end()) |
---|
| 1307 | { |
---|
| 1308 | _getSingleton().mouseHandlers_.erase(it); |
---|
| 1309 | return true; |
---|
| 1310 | } |
---|
| 1311 | else |
---|
| 1312 | return false; |
---|
| 1313 | } |
---|
| 1314 | |
---|
| 1315 | /** |
---|
| 1316 | @brief Returns the pointer to a handler. |
---|
| 1317 | @param name Unique name of the handler. |
---|
| 1318 | @return Pointer to the instance, 0 if name was not found. |
---|
| 1319 | */ |
---|
| 1320 | MouseHandler* InputManager::getMouseHandler(const std::string& name) |
---|
| 1321 | { |
---|
| 1322 | std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name); |
---|
| 1323 | if (it != _getSingleton().mouseHandlers_.end()) |
---|
| 1324 | { |
---|
| 1325 | return (*it).second; |
---|
| 1326 | } |
---|
| 1327 | else |
---|
| 1328 | return 0; |
---|
| 1329 | } |
---|
| 1330 | |
---|
| 1331 | /** |
---|
| 1332 | @brief Enables a specific mouse handler that has already been added. |
---|
| 1333 | @param name Unique name of the handler. |
---|
| 1334 | @return False if name was not found, true otherwise. |
---|
| 1335 | */ |
---|
| 1336 | bool InputManager::enableMouseHandler(const std::string& name) |
---|
| 1337 | { |
---|
| 1338 | // get pointer from the map with all stored handlers |
---|
| 1339 | std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name); |
---|
| 1340 | if (mapIt == _getSingleton().mouseHandlers_.end()) |
---|
| 1341 | return false; |
---|
| 1342 | // see whether the handler already is in the list |
---|
| 1343 | for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin(); |
---|
| 1344 | it != _getSingleton().activeMouseHandlers_.end(); it++) |
---|
| 1345 | { |
---|
| 1346 | if ((*it) == (*mapIt).second) |
---|
| 1347 | { |
---|
| 1348 | return true; |
---|
| 1349 | } |
---|
| 1350 | } |
---|
| 1351 | _getSingleton().activeMouseHandlers_.push_back((*mapIt).second); |
---|
| 1352 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
[1349] | 1353 | _getSingleton()._updateTickables(); |
---|
[1219] | 1354 | return true; |
---|
| 1355 | } |
---|
| 1356 | |
---|
| 1357 | /** |
---|
| 1358 | @brief Disables a specific mouse handler. |
---|
| 1359 | @param name Unique name of the handler. |
---|
| 1360 | @return False if name was not found, true otherwise. |
---|
| 1361 | */ |
---|
| 1362 | bool InputManager::disableMouseHandler(const std::string &name) |
---|
| 1363 | { |
---|
| 1364 | // get pointer from the map with all stored handlers |
---|
| 1365 | std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name); |
---|
| 1366 | if (mapIt == _getSingleton().mouseHandlers_.end()) |
---|
| 1367 | return false; |
---|
| 1368 | // look for the handler in the list |
---|
| 1369 | for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin(); |
---|
| 1370 | it != _getSingleton().activeMouseHandlers_.end(); it++) |
---|
| 1371 | { |
---|
| 1372 | if ((*it) == (*mapIt).second) |
---|
| 1373 | { |
---|
| 1374 | _getSingleton().activeMouseHandlers_.erase(it); |
---|
| 1375 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
[1349] | 1376 | _getSingleton()._updateTickables(); |
---|
[1219] | 1377 | return true; |
---|
| 1378 | } |
---|
| 1379 | } |
---|
| 1380 | return true; |
---|
| 1381 | } |
---|
| 1382 | |
---|
| 1383 | /** |
---|
| 1384 | @brief Checks whether a mouse handler is active |
---|
| 1385 | @param name Unique name of the handler. |
---|
| 1386 | @return False if key handler is not active or doesn't exist, true otherwise. |
---|
| 1387 | */ |
---|
| 1388 | bool InputManager::isMouseHandlerActive(const std::string& name) |
---|
| 1389 | { |
---|
| 1390 | // get pointer from the map with all stored handlers |
---|
| 1391 | std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name); |
---|
| 1392 | if (mapIt == _getSingleton().mouseHandlers_.end()) |
---|
| 1393 | return false; |
---|
| 1394 | // see whether the handler already is in the list |
---|
| 1395 | for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin(); |
---|
| 1396 | it != _getSingleton().activeMouseHandlers_.end(); it++) |
---|
| 1397 | { |
---|
| 1398 | if ((*it) == (*mapIt).second) |
---|
| 1399 | return true; |
---|
| 1400 | } |
---|
| 1401 | return false; |
---|
| 1402 | } |
---|
| 1403 | |
---|
| 1404 | |
---|
| 1405 | // ###### JoyStickHandler ###### |
---|
| 1406 | |
---|
| 1407 | /** |
---|
| 1408 | @brief Adds a new joy stick handler. |
---|
| 1409 | @param handler Pointer to the handler object. |
---|
| 1410 | @param name Unique name of the handler. |
---|
| 1411 | @return True if added, false if name already existed. |
---|
| 1412 | */ |
---|
| 1413 | bool InputManager::addJoyStickHandler(JoyStickHandler* handler, const std::string& name) |
---|
| 1414 | { |
---|
[1293] | 1415 | if (!handler) |
---|
| 1416 | return false; |
---|
[1219] | 1417 | if (_getSingleton().joyStickHandlers_.find(name) == _getSingleton().joyStickHandlers_.end()) |
---|
| 1418 | { |
---|
| 1419 | _getSingleton().joyStickHandlers_[name] = handler; |
---|
| 1420 | return true; |
---|
| 1421 | } |
---|
| 1422 | else |
---|
| 1423 | return false; |
---|
| 1424 | } |
---|
| 1425 | |
---|
| 1426 | /** |
---|
| 1427 | @brief Removes a JoyStick handler from the list. |
---|
| 1428 | @param name Unique name of the handler. |
---|
| 1429 | @return True if removal was successful, false if name was not found. |
---|
| 1430 | */ |
---|
| 1431 | bool InputManager::removeJoyStickHandler(const std::string &name) |
---|
| 1432 | { |
---|
| 1433 | for (std::vector<OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin(); |
---|
| 1434 | itstick != _getSingleton().joySticks_.end(); itstick++) |
---|
| 1435 | disableJoyStickHandler(name, itstick - _getSingleton().joySticks_.begin()); |
---|
| 1436 | |
---|
| 1437 | std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name); |
---|
| 1438 | if (it != _getSingleton().joyStickHandlers_.end()) |
---|
| 1439 | { |
---|
| 1440 | _getSingleton().joyStickHandlers_.erase(it); |
---|
| 1441 | return true; |
---|
| 1442 | } |
---|
| 1443 | else |
---|
| 1444 | return false; |
---|
| 1445 | } |
---|
| 1446 | |
---|
| 1447 | /** |
---|
| 1448 | @brief Returns the pointer to a handler. |
---|
| 1449 | @param name Unique name of the handler. |
---|
| 1450 | @return Pointer to the instance, 0 if name was not found. |
---|
| 1451 | */ |
---|
| 1452 | JoyStickHandler* InputManager::getJoyStickHandler(const std::string& name) |
---|
| 1453 | { |
---|
| 1454 | std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name); |
---|
| 1455 | if (it != _getSingleton().joyStickHandlers_.end()) |
---|
| 1456 | { |
---|
| 1457 | return (*it).second; |
---|
| 1458 | } |
---|
| 1459 | else |
---|
| 1460 | return 0; |
---|
| 1461 | } |
---|
| 1462 | |
---|
| 1463 | /** |
---|
| 1464 | @brief Enables a specific joy stick handler that has already been added. |
---|
| 1465 | @param name Unique name of the handler. |
---|
| 1466 | @return False if name or id was not found, true otherwise. |
---|
| 1467 | */ |
---|
[1293] | 1468 | bool InputManager::enableJoyStickHandler(const std::string& name, unsigned int ID) |
---|
[1219] | 1469 | { |
---|
| 1470 | // get handler pointer from the map with all stored handlers |
---|
| 1471 | std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name); |
---|
| 1472 | if (handlerIt == _getSingleton().joyStickHandlers_.end()) |
---|
| 1473 | return false; |
---|
| 1474 | |
---|
| 1475 | // check for existence of the ID |
---|
[1293] | 1476 | if (ID >= _getSingleton().joySticksSize_) |
---|
[1219] | 1477 | return false; |
---|
| 1478 | |
---|
| 1479 | // see whether the handler already is in the list |
---|
[1293] | 1480 | for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin(); |
---|
| 1481 | it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++) |
---|
[1219] | 1482 | { |
---|
| 1483 | if ((*it) == (*handlerIt).second) |
---|
| 1484 | { |
---|
| 1485 | return true; |
---|
| 1486 | } |
---|
| 1487 | } |
---|
[1293] | 1488 | _getSingleton().activeJoyStickHandlers_[ID].push_back((*handlerIt).second); |
---|
[1219] | 1489 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
[1349] | 1490 | _getSingleton()._updateTickables(); |
---|
[1219] | 1491 | return true; |
---|
| 1492 | } |
---|
| 1493 | |
---|
| 1494 | /** |
---|
| 1495 | @brief Disables a specific joy stick handler. |
---|
| 1496 | @param name Unique name of the handler. |
---|
| 1497 | @return False if name or id was not found, true otherwise. |
---|
| 1498 | */ |
---|
[1293] | 1499 | bool InputManager::disableJoyStickHandler(const std::string &name, unsigned int ID) |
---|
[1219] | 1500 | { |
---|
| 1501 | // get handler pointer from the map with all stored handlers |
---|
| 1502 | std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name); |
---|
| 1503 | if (handlerIt == _getSingleton().joyStickHandlers_.end()) |
---|
| 1504 | return false; |
---|
| 1505 | |
---|
| 1506 | // check for existence of the ID |
---|
[1293] | 1507 | if (ID >= _getSingleton().joySticksSize_) |
---|
[1219] | 1508 | return false; |
---|
| 1509 | |
---|
| 1510 | // look for the handler in the list |
---|
[1293] | 1511 | for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin(); |
---|
| 1512 | it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++) |
---|
[1219] | 1513 | { |
---|
| 1514 | if ((*it) == (*handlerIt).second) |
---|
| 1515 | { |
---|
[1293] | 1516 | _getSingleton().activeJoyStickHandlers_[ID].erase(it); |
---|
[1219] | 1517 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
[1349] | 1518 | _getSingleton()._updateTickables(); |
---|
[1219] | 1519 | return true; |
---|
| 1520 | } |
---|
| 1521 | } |
---|
| 1522 | return true; |
---|
| 1523 | } |
---|
| 1524 | |
---|
| 1525 | /** |
---|
| 1526 | @brief Checks whether a joy stick handler is active |
---|
| 1527 | @param name Unique name of the handler. |
---|
| 1528 | @return False if key handler is not active or doesn't exist, true otherwise. |
---|
| 1529 | */ |
---|
[1293] | 1530 | bool InputManager::isJoyStickHandlerActive(const std::string& name, unsigned int ID) |
---|
[1219] | 1531 | { |
---|
| 1532 | // get handler pointer from the map with all stored handlers |
---|
| 1533 | std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name); |
---|
| 1534 | if (handlerIt == _getSingleton().joyStickHandlers_.end()) |
---|
| 1535 | return false; |
---|
| 1536 | |
---|
| 1537 | // check for existence of the ID |
---|
[1293] | 1538 | if (ID >= _getSingleton().joySticksSize_) |
---|
[1219] | 1539 | return false; |
---|
| 1540 | |
---|
| 1541 | // see whether the handler already is in the list |
---|
[1293] | 1542 | for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin(); |
---|
| 1543 | it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++) |
---|
[1219] | 1544 | { |
---|
| 1545 | if ((*it) == (*handlerIt).second) |
---|
| 1546 | return true; |
---|
| 1547 | } |
---|
| 1548 | return false; |
---|
| 1549 | } |
---|
| 1550 | |
---|
[918] | 1551 | } |
---|