[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 | /** |
---|
[1755] | 30 | @file |
---|
| 31 | @brief |
---|
| 32 | Implementation of the InputManager that captures all the input from OIS |
---|
| 33 | and redirects it to handlers. |
---|
[918] | 34 | */ |
---|
| 35 | |
---|
[1062] | 36 | #include "InputManager.h" |
---|
[1519] | 37 | |
---|
[1755] | 38 | #include <climits> |
---|
| 39 | #include <cassert> |
---|
[3196] | 40 | #include <ois/OISException.h> |
---|
| 41 | #include <ois/OISInputManager.h> |
---|
[1555] | 42 | |
---|
[3196] | 43 | #include "util/Convert.h" |
---|
[1764] | 44 | #include "util/Exception.h" |
---|
[3280] | 45 | #include "util/ScopeGuard.h" |
---|
[2896] | 46 | #include "core/Clock.h" |
---|
[1519] | 47 | #include "core/CoreIncludes.h" |
---|
| 48 | #include "core/ConfigValueIncludes.h" |
---|
[3196] | 49 | #include "core/ConsoleCommand.h" |
---|
[1934] | 50 | #include "core/CommandLine.h" |
---|
[1555] | 51 | |
---|
[1219] | 52 | #include "InputBuffer.h" |
---|
[1520] | 53 | #include "KeyDetector.h" |
---|
[1755] | 54 | #include "InputState.h" |
---|
| 55 | #include "SimpleInputState.h" |
---|
| 56 | #include "ExtendedInputState.h" |
---|
[1887] | 57 | #include "JoyStickDeviceNumberListener.h" |
---|
[918] | 58 | |
---|
[3196] | 59 | // HACK (include this as last, X11 seems to define some macros...) |
---|
| 60 | #ifdef ORXONOX_PLATFORM_LINUX |
---|
| 61 | # include <ois/linux/LinuxMouse.h> |
---|
| 62 | #endif |
---|
| 63 | |
---|
[918] | 64 | namespace orxonox |
---|
| 65 | { |
---|
[1755] | 66 | SetConsoleCommand(InputManager, calibrate, true); |
---|
| 67 | SetConsoleCommand(InputManager, reload, false); |
---|
[3084] | 68 | #ifdef ORXONOX_PLATFORM_LINUX |
---|
| 69 | SetConsoleCommand(InputManager, grabMouse, true); |
---|
| 70 | SetConsoleCommand(InputManager, ungrabMouse, true); |
---|
| 71 | #endif |
---|
[3280] | 72 | SetCommandLineSwitch(keyboard_no_grab).information("Whether not to exclusively grab the keyboard"); |
---|
[1502] | 73 | |
---|
[1878] | 74 | EmptyHandler InputManager::EMPTY_HANDLER; |
---|
[1755] | 75 | InputManager* InputManager::singletonRef_s = 0; |
---|
[1084] | 76 | |
---|
[1755] | 77 | using namespace InputDevice; |
---|
[929] | 78 | |
---|
[1755] | 79 | /** |
---|
| 80 | @brief |
---|
| 81 | Defines the |= operator for easier use. |
---|
| 82 | */ |
---|
| 83 | inline InputManager::InputManagerState operator|=(InputManager::InputManagerState& lval, |
---|
| 84 | InputManager::InputManagerState rval) |
---|
| 85 | { |
---|
| 86 | return (lval = (InputManager::InputManagerState)(lval | rval)); |
---|
| 87 | } |
---|
[919] | 88 | |
---|
[1755] | 89 | /** |
---|
| 90 | @brief |
---|
| 91 | Defines the &= operator for easier use. |
---|
| 92 | */ |
---|
| 93 | inline InputManager::InputManagerState operator&=(InputManager::InputManagerState& lval, int rval) |
---|
| 94 | { |
---|
| 95 | return (lval = (InputManager::InputManagerState)(lval & rval)); |
---|
| 96 | } |
---|
[1219] | 97 | |
---|
[1755] | 98 | // ############################################################ |
---|
| 99 | // ##### Initialisation ##### |
---|
| 100 | // ########## ########## |
---|
| 101 | // ############################################################ |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | @brief |
---|
| 105 | Constructor only sets member fields to initial zero values |
---|
| 106 | and registers the class in the class hierarchy. |
---|
| 107 | */ |
---|
| 108 | InputManager::InputManager() |
---|
| 109 | : inputSystem_(0) |
---|
| 110 | , keyboard_(0) |
---|
| 111 | , mouse_(0) |
---|
| 112 | , joySticksSize_(0) |
---|
| 113 | , devicesNum_(0) |
---|
| 114 | , windowHnd_(0) |
---|
| 115 | , internalState_(Uninitialised) |
---|
| 116 | , stateEmpty_(0) |
---|
[1881] | 117 | , keyDetector_(0) |
---|
| 118 | , calibratorCallbackBuffer_(0) |
---|
[1755] | 119 | , keyboardModifiers_(0) |
---|
[918] | 120 | { |
---|
[1755] | 121 | RegisterRootObject(InputManager); |
---|
[1219] | 122 | |
---|
[1755] | 123 | assert(singletonRef_s == 0); |
---|
| 124 | singletonRef_s = this; |
---|
[2662] | 125 | |
---|
| 126 | setConfigValues(); |
---|
[1755] | 127 | } |
---|
[918] | 128 | |
---|
[1755] | 129 | /** |
---|
| 130 | @brief |
---|
[2662] | 131 | Sets the configurable values. |
---|
| 132 | */ |
---|
| 133 | void InputManager::setConfigValues() |
---|
| 134 | { |
---|
| 135 | SetConfigValue(calibrationFilename_, "joystick_calibration.ini") |
---|
| 136 | .description("Ini filename for the the joy stick calibration data.") |
---|
| 137 | .callback(this, &InputManager::_calibrationFileCallback); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | @brief |
---|
| 142 | Callback for the joy stick calibration config file. @see setConfigValues. |
---|
| 143 | */ |
---|
| 144 | void InputManager::_calibrationFileCallback() |
---|
| 145 | { |
---|
| 146 | ConfigFileManager::getInstance().setFilename(ConfigFileType::JoyStickCalibration, calibrationFilename_); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /** |
---|
| 150 | @brief |
---|
[1755] | 151 | Creates the OIS::InputMananger, the keyboard, the mouse and |
---|
| 152 | the joysticks and assigns the key bindings. |
---|
| 153 | @param windowHnd |
---|
| 154 | The window handle of the render window |
---|
| 155 | @param windowWidth |
---|
| 156 | The width of the render window |
---|
| 157 | @param windowHeight |
---|
| 158 | The height of the render window |
---|
| 159 | @param joyStickSupport |
---|
| 160 | Whether or not to load the joy sticks as well |
---|
| 161 | */ |
---|
| 162 | void InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport) |
---|
| 163 | { |
---|
| 164 | CCOUT(3) << "Initialising Input System..." << std::endl; |
---|
| 165 | |
---|
| 166 | if (!(internalState_ & OISReady)) |
---|
| 167 | { |
---|
| 168 | CCOUT(4) << "Initialising OIS components..." << std::endl; |
---|
| 169 | |
---|
| 170 | // store handle internally so we can reload OIS |
---|
| 171 | windowHnd_ = windowHnd; |
---|
| 172 | |
---|
| 173 | OIS::ParamList paramList; |
---|
| 174 | std::ostringstream windowHndStr; |
---|
| 175 | |
---|
| 176 | // Fill parameter list |
---|
| 177 | windowHndStr << (unsigned int)windowHnd_; |
---|
| 178 | paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
[3280] | 179 | #if defined(ORXONOX_PLATFORM_WINDOWS) |
---|
[1755] | 180 | //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE"))); |
---|
| 181 | //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND"))); |
---|
[3280] | 182 | //paramList.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_NONEXCLUSIVE"))); |
---|
| 183 | //paramList.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_FOREGROUND"))); |
---|
| 184 | #elif defined(ORXONOX_PLATFORM_LINUX) |
---|
[1755] | 185 | paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true"))); |
---|
[1934] | 186 | paramList.insert(std::make_pair(std::string("x11_mouse_grab"), "true")); |
---|
| 187 | paramList.insert(std::make_pair(std::string("x11_mouse_hide"), "true")); |
---|
| 188 | bool kbNoGrab; |
---|
| 189 | CommandLine::getValue("keyboard_no_grab", &kbNoGrab); |
---|
| 190 | if (kbNoGrab) |
---|
| 191 | paramList.insert(std::make_pair(std::string("x11_keyboard_grab"), std::string("false"))); |
---|
| 192 | else |
---|
| 193 | paramList.insert(std::make_pair(std::string("x11_keyboard_grab"), std::string("true"))); |
---|
[1735] | 194 | #endif |
---|
[928] | 195 | |
---|
[3280] | 196 | try |
---|
| 197 | { |
---|
| 198 | inputSystem_ = OIS::InputManager::createInputSystem(paramList); |
---|
| 199 | // Exception-safety |
---|
| 200 | Loki::ScopeGuard guard = Loki::MakeGuard(OIS::InputManager::destroyInputSystem, inputSystem_); |
---|
| 201 | CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl; |
---|
[1219] | 202 | |
---|
[3280] | 203 | _initialiseKeyboard(); |
---|
[1219] | 204 | |
---|
[3280] | 205 | // Nothing below should throw anymore, dismiss the guard |
---|
| 206 | guard.Dismiss(); |
---|
| 207 | } |
---|
| 208 | catch (OIS::Exception& ex) |
---|
| 209 | { |
---|
| 210 | ThrowException(InitialisationFailed, "Could not initialise the input system: " << ex.what()); |
---|
| 211 | } |
---|
| 212 | |
---|
[1755] | 213 | _initialiseMouse(); |
---|
[1219] | 214 | |
---|
[1755] | 215 | if (joyStickSupport) |
---|
| 216 | _initialiseJoySticks(); |
---|
[3280] | 217 | // Do this anyway to also inform when a joystick was detached. |
---|
[2662] | 218 | _configureJoySticks(); |
---|
[1219] | 219 | |
---|
[1755] | 220 | // Set mouse/joystick region |
---|
| 221 | if (mouse_) |
---|
| 222 | setWindowExtents(windowWidth, windowHeight); |
---|
[1219] | 223 | |
---|
[1755] | 224 | // clear all buffers |
---|
| 225 | _clearBuffers(); |
---|
[1502] | 226 | |
---|
[1755] | 227 | internalState_ |= OISReady; |
---|
[1502] | 228 | |
---|
[1755] | 229 | CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl; |
---|
| 230 | } |
---|
| 231 | else |
---|
| 232 | { |
---|
| 233 | CCOUT(2) << "Warning: OIS compoments already initialised, skipping" << std::endl; |
---|
| 234 | } |
---|
[1502] | 235 | |
---|
[1755] | 236 | if (!(internalState_ & InternalsReady)) |
---|
| 237 | { |
---|
| 238 | CCOUT(4) << "Initialising InputStates components..." << std::endl; |
---|
[1502] | 239 | |
---|
[1881] | 240 | // Lowest priority empty InputState |
---|
[2896] | 241 | stateEmpty_ = createInputState<SimpleInputState>("empty", false, false, InputStatePriority::Empty); |
---|
[1878] | 242 | stateEmpty_->setHandler(&EMPTY_HANDLER); |
---|
[1755] | 243 | activeStates_[stateEmpty_->getPriority()] = stateEmpty_; |
---|
[1502] | 244 | |
---|
[1881] | 245 | // KeyDetector to evaluate a pressed key's name |
---|
[2896] | 246 | SimpleInputState* detector = createInputState<SimpleInputState>("detector", false, false, InputStatePriority::Detector); |
---|
[1881] | 247 | keyDetector_ = new KeyDetector(); |
---|
| 248 | detector->setHandler(keyDetector_); |
---|
| 249 | |
---|
| 250 | // Joy stick calibration helper callback |
---|
[2896] | 251 | SimpleInputState* calibrator = createInputState<SimpleInputState>("calibrator", false, false, InputStatePriority::Calibrator); |
---|
[1881] | 252 | calibrator->setHandler(&EMPTY_HANDLER); |
---|
| 253 | calibratorCallbackBuffer_ = new InputBuffer(); |
---|
| 254 | calibratorCallbackBuffer_->registerListener(this, &InputManager::_completeCalibration, '\r', true); |
---|
| 255 | calibrator->setKeyHandler(calibratorCallbackBuffer_); |
---|
| 256 | |
---|
[1755] | 257 | internalState_ |= InternalsReady; |
---|
| 258 | |
---|
| 259 | CCOUT(4) << "Initialising InputStates complete." << std::endl; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | _updateActiveStates(); |
---|
| 263 | |
---|
| 264 | CCOUT(3) << "Initialising complete." << std::endl; |
---|
[1219] | 265 | } |
---|
[928] | 266 | |
---|
[1755] | 267 | /** |
---|
| 268 | @brief |
---|
| 269 | Creates a keyboard and sets the event handler. |
---|
| 270 | @return |
---|
| 271 | False if keyboard stays uninitialised, true otherwise. |
---|
| 272 | */ |
---|
| 273 | void InputManager::_initialiseKeyboard() |
---|
[1219] | 274 | { |
---|
[1755] | 275 | if (keyboard_ != 0) |
---|
| 276 | { |
---|
| 277 | CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl; |
---|
| 278 | return; |
---|
| 279 | } |
---|
| 280 | if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0) |
---|
| 281 | { |
---|
| 282 | keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true); |
---|
| 283 | // register our listener in OIS. |
---|
| 284 | keyboard_->setEventCallback(this); |
---|
| 285 | // note: OIS will not detect keys that have already been down when the keyboard was created. |
---|
| 286 | CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl; |
---|
| 287 | } |
---|
| 288 | else |
---|
| 289 | { |
---|
[3280] | 290 | ThrowException(InitialisationFailed, "InputManager: No keyboard found, cannot proceed!"); |
---|
[1755] | 291 | } |
---|
[1219] | 292 | } |
---|
[1035] | 293 | |
---|
[1755] | 294 | /** |
---|
| 295 | @brief |
---|
| 296 | Creates a mouse and sets the event handler. |
---|
| 297 | @return |
---|
| 298 | False if mouse stays uninitialised, true otherwise. |
---|
| 299 | */ |
---|
| 300 | void InputManager::_initialiseMouse() |
---|
[1219] | 301 | { |
---|
[1755] | 302 | if (mouse_ != 0) |
---|
| 303 | { |
---|
| 304 | CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl; |
---|
| 305 | return; |
---|
| 306 | } |
---|
[1219] | 307 | try |
---|
| 308 | { |
---|
[1755] | 309 | if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0) |
---|
| 310 | { |
---|
| 311 | mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); |
---|
| 312 | // register our listener in OIS. |
---|
| 313 | mouse_->setEventCallback(this); |
---|
| 314 | CCOUT(ORX_DEBUG) << "Created OIS mouse" << std::endl; |
---|
| 315 | } |
---|
| 316 | else |
---|
| 317 | { |
---|
[3280] | 318 | CCOUT(ORX_WARNING) << "Warning: No mouse found! Proceeding without mouse support." << std::endl; |
---|
[1755] | 319 | } |
---|
[1219] | 320 | } |
---|
| 321 | catch (OIS::Exception ex) |
---|
| 322 | { |
---|
[1755] | 323 | CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS mouse\n" |
---|
[3280] | 324 | << "OIS error message: \"" << ex.eText << "\"\n Proceeding without mouse support." << std::endl; |
---|
[1755] | 325 | mouse_ = 0; |
---|
[1219] | 326 | } |
---|
| 327 | } |
---|
[1755] | 328 | |
---|
| 329 | /** |
---|
| 330 | @brief |
---|
| 331 | Creates all joy sticks and sets the event handler. |
---|
| 332 | @return |
---|
| 333 | False joy stick stay uninitialised, true otherwise. |
---|
| 334 | */ |
---|
| 335 | void InputManager::_initialiseJoySticks() |
---|
[1219] | 336 | { |
---|
[1755] | 337 | if (joySticksSize_ > 0) |
---|
| 338 | { |
---|
| 339 | CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl; |
---|
| 340 | return; |
---|
| 341 | } |
---|
| 342 | if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0) |
---|
| 343 | { |
---|
| 344 | for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++) |
---|
| 345 | { |
---|
| 346 | try |
---|
| 347 | { |
---|
| 348 | OIS::JoyStick* stig = static_cast<OIS::JoyStick*> |
---|
| 349 | (inputSystem_->createInputObject(OIS::OISJoyStick, true)); |
---|
| 350 | CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl; |
---|
| 351 | joySticks_.push_back(stig); |
---|
| 352 | // register our listener in OIS. |
---|
| 353 | stig->setEventCallback(this); |
---|
| 354 | } |
---|
| 355 | catch (OIS::Exception ex) |
---|
| 356 | { |
---|
| 357 | CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n" |
---|
| 358 | << "OIS error message: \"" << ex.eText << "\"" << std::endl; |
---|
| 359 | } |
---|
| 360 | } |
---|
| 361 | } |
---|
[1219] | 362 | } |
---|
[1035] | 363 | |
---|
[1755] | 364 | /** |
---|
| 365 | @brief |
---|
[2662] | 366 | Helper function that loads the config value vector of one coefficient |
---|
| 367 | */ |
---|
| 368 | void loadCalibration(std::vector<int>& list, const std::string& sectionName, const std::string& valueName, size_t size, int defaultValue) |
---|
| 369 | { |
---|
| 370 | list.resize(size); |
---|
| 371 | unsigned int configValueVectorSize = ConfigFileManager::getInstance().getVectorSize(ConfigFileType::JoyStickCalibration, sectionName, valueName); |
---|
| 372 | if (configValueVectorSize > size) |
---|
| 373 | configValueVectorSize = size; |
---|
| 374 | |
---|
| 375 | for (unsigned int i = 0; i < configValueVectorSize; ++i) |
---|
| 376 | { |
---|
[3196] | 377 | list[i] = multi_cast<int>(ConfigFileManager::getInstance().getValue( |
---|
| 378 | ConfigFileType::JoyStickCalibration, sectionName, valueName, i, multi_cast<std::string>(defaultValue), false)); |
---|
[2662] | 379 | } |
---|
| 380 | |
---|
| 381 | // fill the rest with default values |
---|
| 382 | for (unsigned int i = configValueVectorSize; i < size; ++i) |
---|
| 383 | { |
---|
| 384 | list[i] = defaultValue; |
---|
| 385 | } |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | /** |
---|
| 389 | @brief |
---|
[1755] | 390 | Sets the size of all the different lists that are dependent on the number |
---|
[2662] | 391 | of joy stick devices created and loads the joy stick calibration. |
---|
[1755] | 392 | @remarks |
---|
| 393 | No matter whether there are a mouse and/or keyboard, they will always |
---|
| 394 | occupy 2 places in the device number dependent lists. |
---|
| 395 | */ |
---|
[2662] | 396 | void InputManager::_configureJoySticks() |
---|
[1505] | 397 | { |
---|
[1755] | 398 | joySticksSize_ = joySticks_.size(); |
---|
[2662] | 399 | devicesNum_ = 2 + joySticksSize_; |
---|
| 400 | joyStickIDs_ .resize(joySticksSize_); |
---|
[1755] | 401 | joyStickButtonsDown_ .resize(joySticksSize_); |
---|
| 402 | povStates_ .resize(joySticksSize_); |
---|
| 403 | sliderStates_ .resize(joySticksSize_); |
---|
[2662] | 404 | joyStickMinValues_ .resize(joySticksSize_); |
---|
| 405 | joyStickMaxValues_ .resize(joySticksSize_); |
---|
| 406 | joyStickMiddleValues_.resize(joySticksSize_); |
---|
| 407 | joyStickCalibrations_.resize(joySticksSize_); |
---|
[1502] | 408 | |
---|
[1755] | 409 | for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) |
---|
| 410 | { |
---|
[2662] | 411 | // Generate some sort of execution unique id per joy stick |
---|
| 412 | std::string id = "JoyStick_"; |
---|
[3196] | 413 | id += multi_cast<std::string>(joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_Button)) + "_"; |
---|
| 414 | id += multi_cast<std::string>(joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_Axis)) + "_"; |
---|
| 415 | id += multi_cast<std::string>(joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_Slider)) + "_"; |
---|
| 416 | id += multi_cast<std::string>(joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_POV)) + "_"; |
---|
| 417 | id += multi_cast<std::string>(joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_Vector3)) + "_"; |
---|
[2662] | 418 | id += joySticks_[iJoyStick]->vendor(); |
---|
| 419 | for (unsigned int i = 0; i < iJoyStick; ++i) |
---|
[1755] | 420 | { |
---|
[2662] | 421 | if (id == joyStickIDs_[i]) |
---|
| 422 | { |
---|
| 423 | // Two joysticks are probably equal --> add the index as well |
---|
[3196] | 424 | id += "_" + multi_cast<std::string>(iJoyStick); |
---|
[2662] | 425 | } |
---|
[1755] | 426 | } |
---|
[2662] | 427 | joyStickIDs_[iJoyStick] = id; |
---|
| 428 | |
---|
| 429 | size_t axes = sliderAxes + (size_t)this->joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_Axis); |
---|
| 430 | loadCalibration(joyStickMinValues_[iJoyStick], id, "MinValue", axes, -32768); |
---|
| 431 | loadCalibration(joyStickMaxValues_[iJoyStick], id, "MaxValue", axes, 32768); |
---|
| 432 | loadCalibration(joyStickMiddleValues_[iJoyStick], id, "MiddleValue", axes, 0); |
---|
[1755] | 433 | } |
---|
[1505] | 434 | |
---|
[2662] | 435 | _evaluateCalibration(); |
---|
| 436 | |
---|
[1755] | 437 | // state management |
---|
[2896] | 438 | activeStatesTriggered_.resize(devicesNum_); |
---|
[1505] | 439 | |
---|
[1755] | 440 | // inform all states |
---|
[2896] | 441 | for (std::map<std::string, InputState*>::const_iterator it = inputStatesByName_.begin(); |
---|
| 442 | it != inputStatesByName_.end(); ++it) |
---|
[1887] | 443 | { |
---|
[1755] | 444 | it->second->setNumOfJoySticks(joySticksSize_); |
---|
[1887] | 445 | } |
---|
| 446 | |
---|
| 447 | // inform all JoyStick Device Number Listeners |
---|
| 448 | for (ObjectList<JoyStickDeviceNumberListener>::iterator it = ObjectList<JoyStickDeviceNumberListener>::begin(); it; ++it) |
---|
| 449 | it->JoyStickDeviceNumberChanged(joySticksSize_); |
---|
| 450 | |
---|
[1505] | 451 | } |
---|
[1502] | 452 | |
---|
[2662] | 453 | void InputManager::_evaluateCalibration() |
---|
[1219] | 454 | { |
---|
[2662] | 455 | for (unsigned int iJoyStick = 0; iJoyStick < this->joySticksSize_; ++iJoyStick) |
---|
[1755] | 456 | { |
---|
[2662] | 457 | for (unsigned int i = 0; i < this->joyStickMinValues_[iJoyStick].size(); i++) |
---|
[1755] | 458 | { |
---|
[2662] | 459 | this->joyStickCalibrations_[iJoyStick].middleValue[i] = this->joyStickMiddleValues_[iJoyStick][i]; |
---|
| 460 | this->joyStickCalibrations_[iJoyStick].negativeCoeff[i] = - 1.0f / (this->joyStickMinValues_[iJoyStick][i] - this->joyStickMiddleValues_[iJoyStick][i]); |
---|
| 461 | this->joyStickCalibrations_[iJoyStick].positiveCoeff[i] = 1.0f / (this->joyStickMaxValues_[iJoyStick][i] - this->joyStickMiddleValues_[iJoyStick][i]); |
---|
[1755] | 462 | } |
---|
[2662] | 463 | } |
---|
| 464 | } |
---|
[2896] | 465 | |
---|
[2662] | 466 | void InputManager::_startCalibration() |
---|
| 467 | { |
---|
| 468 | for (unsigned int iJoyStick = 0; iJoyStick < this->joySticksSize_; ++iJoyStick) |
---|
| 469 | { |
---|
| 470 | // Set initial values |
---|
| 471 | for (unsigned int i = 0; i < this->joyStickMinValues_[iJoyStick].size(); ++i) |
---|
| 472 | this->joyStickMinValues_[iJoyStick][i] = INT_MAX; |
---|
| 473 | for (unsigned int i = 0; i < this->joyStickMaxValues_[iJoyStick].size(); ++i) |
---|
| 474 | this->joyStickMaxValues_[iJoyStick][i] = INT_MIN; |
---|
| 475 | for (unsigned int i = 0; i < this->joyStickMiddleValues_[iJoyStick].size(); ++i) |
---|
| 476 | this->joyStickMiddleValues_[iJoyStick][i] = 0; |
---|
| 477 | } |
---|
[1293] | 478 | |
---|
[2662] | 479 | getInstance().internalState_ |= Calibrating; |
---|
| 480 | getInstance().requestEnterState("calibrator"); |
---|
| 481 | } |
---|
| 482 | |
---|
| 483 | void InputManager::_completeCalibration() |
---|
| 484 | { |
---|
| 485 | for (unsigned int iJoyStick = 0; iJoyStick < this->joySticksSize_; ++iJoyStick) |
---|
| 486 | { |
---|
| 487 | // Get the middle positions now |
---|
| 488 | unsigned int iAxis = 0; |
---|
| 489 | for (unsigned int i = 0; i < sliderAxes/2; ++i) |
---|
[1755] | 490 | { |
---|
[2662] | 491 | this->joyStickMiddleValues_[iJoyStick][iAxis++] = this->joySticks_[iJoyStick]->getJoyStickState().mSliders[i].abX; |
---|
| 492 | this->joyStickMiddleValues_[iJoyStick][iAxis++] = this->joySticks_[iJoyStick]->getJoyStickState().mSliders[i].abY; |
---|
[1755] | 493 | } |
---|
[2662] | 494 | // Note: joyStickMiddleValues_[iJoyStick] was already correctly resized in _configureJoySticks() |
---|
| 495 | assert(joySticks_[iJoyStick]->getJoyStickState().mAxes.size() == joyStickMiddleValues_[iJoyStick].size() - sliderAxes); |
---|
| 496 | for (unsigned int i = 0; i < joyStickMiddleValues_[iJoyStick].size() - sliderAxes; ++i) |
---|
[1755] | 497 | { |
---|
[2662] | 498 | this->joyStickMiddleValues_[iJoyStick][iAxis++] = this->joySticks_[iJoyStick]->getJoyStickState().mAxes[i].abs; |
---|
[1755] | 499 | } |
---|
[1219] | 500 | |
---|
[2662] | 501 | for (unsigned int i = 0; i < joyStickMinValues_[iJoyStick].size(); ++i) |
---|
[1755] | 502 | { |
---|
[2662] | 503 | // Minimum values |
---|
| 504 | if (joyStickMinValues_[iJoyStick][i] == INT_MAX) |
---|
| 505 | joyStickMinValues_[iJoyStick][i] = -32768; |
---|
| 506 | ConfigFileManager::getInstance().setValue(ConfigFileType::JoyStickCalibration, |
---|
[3196] | 507 | this->joyStickIDs_[iJoyStick], "MinValue", i, multi_cast<std::string>(joyStickMinValues_[iJoyStick][i]), false); |
---|
[1502] | 508 | |
---|
[2662] | 509 | // Maximum values |
---|
| 510 | if (joyStickMaxValues_[iJoyStick][i] == INT_MIN) |
---|
| 511 | joyStickMaxValues_[iJoyStick][i] = 32767; |
---|
| 512 | ConfigFileManager::getInstance().setValue(ConfigFileType::JoyStickCalibration, |
---|
[3196] | 513 | this->joyStickIDs_[iJoyStick], "MaxValue", i, multi_cast<std::string>(joyStickMaxValues_[iJoyStick][i]), false); |
---|
[2662] | 514 | |
---|
| 515 | // Middle values |
---|
| 516 | ConfigFileManager::getInstance().setValue(ConfigFileType::JoyStickCalibration, |
---|
[3196] | 517 | this->joyStickIDs_[iJoyStick], "MiddleValue", i, multi_cast<std::string>(joyStickMiddleValues_[iJoyStick][i]), false); |
---|
[1755] | 518 | } |
---|
| 519 | } |
---|
[2662] | 520 | |
---|
| 521 | _evaluateCalibration(); |
---|
| 522 | |
---|
| 523 | // restore old input state |
---|
| 524 | requestLeaveState("calibrator"); |
---|
| 525 | internalState_ &= ~Calibrating; |
---|
[1219] | 526 | } |
---|
[928] | 527 | |
---|
[1755] | 528 | // ############################################################ |
---|
| 529 | // ##### Destruction ##### |
---|
| 530 | // ########## ########## |
---|
| 531 | // ############################################################ |
---|
[1219] | 532 | |
---|
[1755] | 533 | /** |
---|
| 534 | @brief |
---|
| 535 | Destroys all the created input devices and states. |
---|
| 536 | */ |
---|
| 537 | InputManager::~InputManager() |
---|
[1219] | 538 | { |
---|
[1755] | 539 | if (internalState_ != Uninitialised) |
---|
| 540 | { |
---|
[3280] | 541 | CCOUT(3) << "Destroying ..." << std::endl; |
---|
| 542 | |
---|
| 543 | // kick all active states 'nicely' |
---|
| 544 | for (std::map<int, InputState*>::reverse_iterator rit = activeStates_.rbegin(); |
---|
| 545 | rit != activeStates_.rend(); ++rit) |
---|
[1755] | 546 | { |
---|
[3280] | 547 | (*rit).second->onLeave(); |
---|
| 548 | } |
---|
[1219] | 549 | |
---|
[3280] | 550 | // Destroy calibrator helper handler and state |
---|
| 551 | delete keyDetector_; |
---|
| 552 | requestDestroyState("calibrator"); |
---|
| 553 | // Destroy KeyDetector and state |
---|
| 554 | delete calibratorCallbackBuffer_; |
---|
| 555 | requestDestroyState("detector"); |
---|
| 556 | // destroy the empty InputState |
---|
| 557 | _destroyState(this->stateEmpty_); |
---|
[1502] | 558 | |
---|
[3280] | 559 | // destroy all user InputStates |
---|
| 560 | while (inputStatesByName_.size() > 0) |
---|
| 561 | _destroyState((*inputStatesByName_.rbegin()).second); |
---|
[1788] | 562 | |
---|
[3280] | 563 | // destroy the devices |
---|
| 564 | _destroyKeyboard(); |
---|
| 565 | _destroyMouse(); |
---|
| 566 | _destroyJoySticks(); |
---|
[1881] | 567 | |
---|
[3280] | 568 | try |
---|
| 569 | { |
---|
[1755] | 570 | OIS::InputManager::destroyInputSystem(inputSystem_); |
---|
| 571 | } |
---|
[3280] | 572 | catch (...) |
---|
[1755] | 573 | { |
---|
[3280] | 574 | CCOUT(1) << "OIS::InputManager destruction failed! Potential resource leak!" << std::endl; |
---|
[1755] | 575 | } |
---|
| 576 | } |
---|
[2662] | 577 | |
---|
| 578 | singletonRef_s = 0; |
---|
[1755] | 579 | } |
---|
[1349] | 580 | |
---|
[1755] | 581 | /** |
---|
| 582 | @brief |
---|
| 583 | Destroys the keyboard and sets it to 0. |
---|
| 584 | */ |
---|
| 585 | void InputManager::_destroyKeyboard() |
---|
| 586 | { |
---|
| 587 | assert(inputSystem_); |
---|
[3280] | 588 | try |
---|
| 589 | { |
---|
| 590 | if (keyboard_) |
---|
| 591 | inputSystem_->destroyInputObject(keyboard_); |
---|
| 592 | keyboard_ = 0; |
---|
| 593 | CCOUT(4) << "Keyboard destroyed." << std::endl; |
---|
| 594 | } |
---|
| 595 | catch (...) |
---|
| 596 | { |
---|
| 597 | CCOUT(1) << "Keyboard destruction failed! Potential resource leak!" << std::endl; |
---|
| 598 | } |
---|
[1755] | 599 | } |
---|
[1219] | 600 | |
---|
[1755] | 601 | /** |
---|
| 602 | @brief |
---|
| 603 | Destroys the mouse and sets it to 0. |
---|
| 604 | */ |
---|
| 605 | void InputManager::_destroyMouse() |
---|
| 606 | { |
---|
| 607 | assert(inputSystem_); |
---|
[3280] | 608 | try |
---|
| 609 | { |
---|
| 610 | if (mouse_) |
---|
| 611 | inputSystem_->destroyInputObject(mouse_); |
---|
| 612 | mouse_ = 0; |
---|
| 613 | CCOUT(4) << "Mouse destroyed." << std::endl; |
---|
| 614 | } |
---|
| 615 | catch (...) |
---|
| 616 | { |
---|
| 617 | CCOUT(1) << "Mouse destruction failed! Potential resource leak!" << std::endl; |
---|
| 618 | } |
---|
[1755] | 619 | } |
---|
[1219] | 620 | |
---|
[1755] | 621 | /** |
---|
| 622 | @brief |
---|
| 623 | Destroys all the joy sticks and resizes the lists to 0. |
---|
| 624 | */ |
---|
| 625 | void InputManager::_destroyJoySticks() |
---|
[1022] | 626 | { |
---|
[1755] | 627 | if (joySticksSize_ > 0) |
---|
| 628 | { |
---|
| 629 | assert(inputSystem_); |
---|
| 630 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
[3280] | 631 | { |
---|
| 632 | try |
---|
| 633 | { |
---|
| 634 | if (joySticks_[i] != 0) |
---|
| 635 | inputSystem_->destroyInputObject(joySticks_[i]); |
---|
| 636 | } |
---|
| 637 | catch (...) |
---|
| 638 | { |
---|
| 639 | CCOUT(1) << "Joy stick destruction failed! Potential resource leak!" << std::endl; |
---|
| 640 | } |
---|
| 641 | } |
---|
[1219] | 642 | |
---|
[1755] | 643 | joySticks_.clear(); |
---|
[1887] | 644 | // don't use _configureNumberOfJoySticks(), might mess with registered handler if |
---|
[1755] | 645 | // downgrading from 2 to 1 joystick |
---|
[1887] | 646 | //_configureNumberOfJoySticks(); |
---|
[1755] | 647 | joySticksSize_ = 0; |
---|
| 648 | } |
---|
| 649 | CCOUT(4) << "Joy sticks destroyed." << std::endl; |
---|
| 650 | } |
---|
[1502] | 651 | |
---|
[1755] | 652 | /** |
---|
| 653 | @brief |
---|
| 654 | Removes and destroys an InputState. |
---|
| 655 | @return |
---|
| 656 | True if state was removed immediately, false if postponed. |
---|
| 657 | */ |
---|
| 658 | void InputManager::_destroyState(InputState* state) |
---|
| 659 | { |
---|
| 660 | assert(state && !(this->internalState_ & Ticking)); |
---|
| 661 | std::map<int, InputState*>::iterator it = this->activeStates_.find(state->getPriority()); |
---|
| 662 | if (it != this->activeStates_.end()) |
---|
| 663 | { |
---|
| 664 | this->activeStates_.erase(it); |
---|
| 665 | _updateActiveStates(); |
---|
| 666 | } |
---|
| 667 | inputStatesByName_.erase(state->getName()); |
---|
| 668 | delete state; |
---|
| 669 | } |
---|
[1502] | 670 | |
---|
[1755] | 671 | void InputManager::_clearBuffers() |
---|
| 672 | { |
---|
[1502] | 673 | keysDown_.clear(); |
---|
[1755] | 674 | keyboardModifiers_ = 0; |
---|
[1502] | 675 | mouseButtonsDown_.clear(); |
---|
[1755] | 676 | for (unsigned int i = 0; i < joySticksSize_; ++i) |
---|
[1219] | 677 | { |
---|
[1755] | 678 | joyStickButtonsDown_[i].clear(); |
---|
| 679 | for (int j = 0; j < 4; ++j) |
---|
| 680 | { |
---|
| 681 | sliderStates_[i].sliderStates[j].x = 0; |
---|
| 682 | sliderStates_[i].sliderStates[j].y = 0; |
---|
| 683 | povStates_[i][j] = 0; |
---|
| 684 | } |
---|
| 685 | } |
---|
| 686 | } |
---|
[1219] | 687 | |
---|
| 688 | |
---|
[1755] | 689 | // ############################################################ |
---|
| 690 | // ##### Reloading ##### |
---|
| 691 | // ########## ########## |
---|
| 692 | // ############################################################ |
---|
[1219] | 693 | |
---|
[1755] | 694 | /** |
---|
| 695 | @brief |
---|
| 696 | Public interface. Only reloads immediately if the call stack doesn't |
---|
[2896] | 697 | include the update() method. |
---|
[1755] | 698 | @param joyStickSupport |
---|
| 699 | Whether or not to initialise joy sticks as well. |
---|
| 700 | */ |
---|
| 701 | void InputManager::reloadInputSystem(bool joyStickSupport) |
---|
| 702 | { |
---|
| 703 | if (internalState_ & Ticking) |
---|
| 704 | { |
---|
| 705 | // We cannot destroy OIS right now, because reload was probably |
---|
| 706 | // caused by a user clicking on a GUI item. The backtrace would then |
---|
| 707 | // include an OIS method. So it would be a very bad thing to destroy it.. |
---|
| 708 | internalState_ |= ReloadRequest; |
---|
| 709 | // Misuse of internalState_: We can easily store the joyStickSupport bool. |
---|
| 710 | // use Uninitialised as 0 value in order to make use of the overloaded |= operator |
---|
| 711 | internalState_ |= joyStickSupport ? JoyStickSupport : Uninitialised; |
---|
[1219] | 712 | } |
---|
[1755] | 713 | else if (internalState_ & OISReady) |
---|
| 714 | { |
---|
| 715 | _reload(joyStickSupport); |
---|
| 716 | } |
---|
[1502] | 717 | else |
---|
| 718 | { |
---|
[1755] | 719 | CCOUT(2) << "Warning: Cannot reload OIS. May not yet be initialised or" |
---|
| 720 | << "joy sticks are currently calibrating." << std::endl; |
---|
[1502] | 721 | } |
---|
[1755] | 722 | } |
---|
[1502] | 723 | |
---|
[1755] | 724 | /** |
---|
| 725 | @brief |
---|
| 726 | Internal reload method. Destroys the OIS devices and loads them again. |
---|
| 727 | */ |
---|
| 728 | void InputManager::_reload(bool joyStickSupport) |
---|
| 729 | { |
---|
| 730 | try |
---|
| 731 | { |
---|
| 732 | CCOUT(3) << "Reloading ..." << std::endl; |
---|
[1502] | 733 | |
---|
[1755] | 734 | // Save mouse clipping size |
---|
| 735 | int mouseWidth = mouse_->getMouseState().width; |
---|
| 736 | int mouseHeight = mouse_->getMouseState().height; |
---|
[1502] | 737 | |
---|
[1755] | 738 | internalState_ &= ~OISReady; |
---|
[1219] | 739 | |
---|
[1755] | 740 | // destroy the devices |
---|
| 741 | _destroyKeyboard(); |
---|
| 742 | _destroyMouse(); |
---|
| 743 | _destroyJoySticks(); |
---|
[1219] | 744 | |
---|
[1755] | 745 | OIS::InputManager::destroyInputSystem(inputSystem_); |
---|
| 746 | inputSystem_ = 0; |
---|
[1219] | 747 | |
---|
[1755] | 748 | // clear all buffers containing input information |
---|
| 749 | _clearBuffers(); |
---|
[1219] | 750 | |
---|
[1755] | 751 | initialise(windowHnd_, mouseWidth, mouseHeight, joyStickSupport); |
---|
| 752 | |
---|
| 753 | CCOUT(3) << "Reloading done." << std::endl; |
---|
| 754 | } |
---|
| 755 | catch (OIS::Exception& ex) |
---|
| 756 | { |
---|
| 757 | CCOUT(1) << "An exception has occured while reloading:\n" << ex.what() << std::endl; |
---|
| 758 | } |
---|
[1502] | 759 | } |
---|
[1219] | 760 | |
---|
[1755] | 761 | // ############################################################ |
---|
| 762 | // ##### Runtime Methods ##### |
---|
| 763 | // ########## ########## |
---|
| 764 | // ############################################################ |
---|
[1555] | 765 | |
---|
[1755] | 766 | /** |
---|
| 767 | @brief |
---|
[2662] | 768 | Updates the states and the InputState situation. |
---|
[2896] | 769 | @param time |
---|
| 770 | Clock holding the current time. |
---|
[1755] | 771 | */ |
---|
[2896] | 772 | void InputManager::update(const Clock& time) |
---|
[1502] | 773 | { |
---|
[1755] | 774 | if (internalState_ == Uninitialised) |
---|
| 775 | return; |
---|
| 776 | else if (internalState_ & ReloadRequest) |
---|
| 777 | { |
---|
| 778 | _reload(internalState_ & JoyStickSupport); |
---|
| 779 | internalState_ &= ~ReloadRequest; |
---|
| 780 | internalState_ &= ~JoyStickSupport; |
---|
| 781 | } |
---|
[1349] | 782 | |
---|
[1755] | 783 | // check for states to leave |
---|
[2662] | 784 | if (!stateLeaveRequests_.empty()) |
---|
[1755] | 785 | { |
---|
[2896] | 786 | for (std::set<InputState*>::iterator it = stateLeaveRequests_.begin(); |
---|
| 787 | it != stateLeaveRequests_.end(); ++it) |
---|
[2662] | 788 | { |
---|
[2896] | 789 | (*it)->onLeave(); |
---|
[2662] | 790 | // just to be sure that the state actually is registered |
---|
[2896] | 791 | assert(inputStatesByName_.find((*it)->getName()) != inputStatesByName_.end()); |
---|
[1505] | 792 | |
---|
[2896] | 793 | activeStates_.erase((*it)->getPriority()); |
---|
| 794 | if ((*it)->getPriority() < InputStatePriority::HighPriority) |
---|
| 795 | (*it)->setPriority(0); |
---|
[2662] | 796 | _updateActiveStates(); |
---|
| 797 | } |
---|
| 798 | stateLeaveRequests_.clear(); |
---|
[1755] | 799 | } |
---|
[1505] | 800 | |
---|
[1755] | 801 | // check for states to enter |
---|
[2662] | 802 | if (!stateEnterRequests_.empty()) |
---|
[1755] | 803 | { |
---|
[2896] | 804 | for (std::set<InputState*>::const_iterator it = stateEnterRequests_.begin(); |
---|
| 805 | it != stateEnterRequests_.end(); ++it) |
---|
[2662] | 806 | { |
---|
| 807 | // just to be sure that the state actually is registered |
---|
[2896] | 808 | assert(inputStatesByName_.find((*it)->getName()) != inputStatesByName_.end()); |
---|
[1219] | 809 | |
---|
[2896] | 810 | if ((*it)->getPriority() == 0) |
---|
| 811 | { |
---|
| 812 | // Get smallest possible priority between 1 and maxStateStackSize_s |
---|
| 813 | #if defined( __MINGW32__ ) // Avoid the strange mingw-stl bug with const_reverse_iterator |
---|
| 814 | for(std::map<int, InputState*>::reverse_iterator rit = activeStates_.rbegin(); |
---|
| 815 | rit != activeStates_.rend(); ++rit) |
---|
| 816 | #else |
---|
| 817 | for(std::map<int, InputState*>::const_reverse_iterator rit = activeStates_.rbegin(); |
---|
| 818 | rit != activeStates_.rend(); ++rit) |
---|
| 819 | #endif |
---|
| 820 | { |
---|
| 821 | if (rit->first < InputStatePriority::HighPriority) |
---|
| 822 | { |
---|
| 823 | (*it)->setPriority(rit->first + 1); |
---|
| 824 | break; |
---|
| 825 | } |
---|
| 826 | } |
---|
| 827 | // In case no normal handler was on the stack |
---|
| 828 | if ((*it)->getPriority() == 0) |
---|
| 829 | (*it)->setPriority(1); |
---|
| 830 | } |
---|
| 831 | activeStates_[(*it)->getPriority()] = (*it); |
---|
[2662] | 832 | _updateActiveStates(); |
---|
[2896] | 833 | (*it)->onEnter(); |
---|
[2662] | 834 | } |
---|
| 835 | stateEnterRequests_.clear(); |
---|
[1755] | 836 | } |
---|
[1219] | 837 | |
---|
[1755] | 838 | // check for states to destroy |
---|
[2662] | 839 | if (!stateDestroyRequests_.empty()) |
---|
[1755] | 840 | { |
---|
[2896] | 841 | for (std::set<InputState*>::iterator it = stateDestroyRequests_.begin(); |
---|
| 842 | it != stateDestroyRequests_.end(); ++it) |
---|
[2662] | 843 | { |
---|
[2896] | 844 | _destroyState((*it)); |
---|
[2662] | 845 | } |
---|
| 846 | stateDestroyRequests_.clear(); |
---|
[1755] | 847 | } |
---|
[1219] | 848 | |
---|
[1878] | 849 | // check whether a state has changed its EMPTY_HANDLER situation |
---|
| 850 | bool bUpdateRequired = false; |
---|
| 851 | for (std::map<int, InputState*>::iterator it = activeStates_.begin(); it != activeStates_.end(); ++it) |
---|
| 852 | { |
---|
| 853 | if (it->second->handlersChanged()) |
---|
| 854 | { |
---|
| 855 | it->second->resetHandlersChanged(); |
---|
| 856 | bUpdateRequired = true; |
---|
| 857 | } |
---|
| 858 | } |
---|
| 859 | if (bUpdateRequired) |
---|
| 860 | _updateActiveStates(); |
---|
| 861 | |
---|
[2896] | 862 | // mark that we now start capturing and distributing input |
---|
[1755] | 863 | internalState_ |= Ticking; |
---|
[1293] | 864 | |
---|
[1755] | 865 | // Capture all the input. This calls the event handlers in InputManager. |
---|
| 866 | if (keyboard_) |
---|
| 867 | keyboard_->capture(); |
---|
| 868 | if (mouse_) |
---|
| 869 | mouse_->capture(); |
---|
| 870 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
| 871 | joySticks_[i]->capture(); |
---|
[1219] | 872 | |
---|
[2662] | 873 | if (!(internalState_ & Calibrating)) |
---|
[1755] | 874 | { |
---|
| 875 | // call all the handlers for the held key events |
---|
| 876 | for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++) |
---|
[1788] | 877 | { |
---|
| 878 | KeyEvent kEvt(keysDown_[iKey], keyboardModifiers_); |
---|
[2896] | 879 | |
---|
| 880 | for (unsigned int iState = 0; iState < activeStatesTriggered_[Keyboard].size(); ++iState) |
---|
| 881 | activeStatesTriggered_[Keyboard][iState]->keyHeld(kEvt); |
---|
[1788] | 882 | } |
---|
[1219] | 883 | |
---|
[1755] | 884 | // call all the handlers for the held mouse button events |
---|
| 885 | for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++) |
---|
[1788] | 886 | { |
---|
[2896] | 887 | for (unsigned int iState = 0; iState < activeStatesTriggered_[Mouse].size(); ++iState) |
---|
| 888 | activeStatesTriggered_[Mouse][iState]->mouseButtonHeld(mouseButtonsDown_[iButton]); |
---|
[1788] | 889 | } |
---|
[1219] | 890 | |
---|
[1755] | 891 | // call all the handlers for the held joy stick button events |
---|
| 892 | for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) |
---|
| 893 | for (unsigned int iButton = 0; iButton < joyStickButtonsDown_[iJoyStick].size(); iButton++) |
---|
[1788] | 894 | { |
---|
[2896] | 895 | for (unsigned int iState = 0; iState < activeStatesTriggered_[JoyStick0 + iJoyStick].size(); ++iState) |
---|
| 896 | activeStatesTriggered_[JoyStick0 + iJoyStick][iState]->joyStickButtonHeld(iJoyStick, joyStickButtonsDown_[iJoyStick][iButton]); |
---|
[1788] | 897 | } |
---|
[1293] | 898 | |
---|
[2896] | 899 | // update the handlers for each active handler |
---|
[1755] | 900 | for (unsigned int i = 0; i < devicesNum_; ++i) |
---|
[1788] | 901 | { |
---|
[2896] | 902 | for (unsigned int iState = 0; iState < activeStatesTriggered_[i].size(); ++iState) |
---|
| 903 | activeStatesTriggered_[i][iState]->updateInput(time.getDeltaTime(), i); |
---|
[1788] | 904 | } |
---|
[1219] | 905 | |
---|
[2896] | 906 | // update the handler with a general tick afterwards |
---|
[1755] | 907 | for (unsigned int i = 0; i < activeStatesTicked_.size(); ++i) |
---|
[2896] | 908 | activeStatesTicked_[i]->updateInput(time.getDeltaTime()); |
---|
[1755] | 909 | } |
---|
[1219] | 910 | |
---|
[1755] | 911 | internalState_ &= ~Ticking; |
---|
[1293] | 912 | } |
---|
[1219] | 913 | |
---|
[1755] | 914 | /** |
---|
| 915 | @brief |
---|
| 916 | Updates the currently active states (according to activeStates_) for each device. |
---|
[2896] | 917 | Also, a list of all active states (no duplicates!) is compiled for the general update(). |
---|
[1755] | 918 | */ |
---|
| 919 | void InputManager::_updateActiveStates() |
---|
[1293] | 920 | { |
---|
[2896] | 921 | for (unsigned int i = 0; i < devicesNum_; ++i) |
---|
| 922 | { |
---|
| 923 | bool occupied = false; |
---|
| 924 | activeStatesTriggered_[i].clear(); |
---|
| 925 | #if defined( __MINGW32__ ) // Avoid the strange mingw-stl bug with const_reverse_iterator |
---|
| 926 | for (std::map<int, InputState*>::reverse_iterator rit = activeStates_.rbegin(); rit != activeStates_.rend(); ++rit) |
---|
| 927 | { |
---|
| 928 | #else |
---|
| 929 | for (std::map<int, InputState*>::const_reverse_iterator rit = activeStates_.rbegin(); rit != activeStates_.rend(); ++rit) |
---|
| 930 | { |
---|
| 931 | #endif |
---|
| 932 | if (rit->second->isInputDeviceEnabled(i) && (!occupied || rit->second->bAlwaysGetsInput_)) |
---|
| 933 | { |
---|
| 934 | activeStatesTriggered_[i].push_back(rit->second); |
---|
| 935 | if (!rit->second->bTransparent_) |
---|
| 936 | occupied = true; |
---|
| 937 | } |
---|
| 938 | } |
---|
| 939 | } |
---|
[1293] | 940 | |
---|
[1755] | 941 | // update tickables (every state will only appear once) |
---|
| 942 | // Using a std::set to avoid duplicates |
---|
| 943 | std::set<InputState*> tempSet; |
---|
| 944 | for (unsigned int i = 0; i < devicesNum_; ++i) |
---|
[2896] | 945 | for (unsigned int iState = 0; iState < activeStatesTriggered_[i].size(); ++iState) |
---|
| 946 | tempSet.insert(activeStatesTriggered_[i][iState]); |
---|
[1219] | 947 | |
---|
[2896] | 948 | // copy the content of the std::set back to the actual vector |
---|
[1755] | 949 | activeStatesTicked_.clear(); |
---|
| 950 | for (std::set<InputState*>::const_iterator it = tempSet.begin();it != tempSet.end(); ++it) |
---|
| 951 | activeStatesTicked_.push_back(*it); |
---|
[1219] | 952 | |
---|
[1755] | 953 | this->mouseButtonsDown_.clear(); |
---|
| 954 | } |
---|
[1219] | 955 | |
---|
[1755] | 956 | /** |
---|
| 957 | @brief |
---|
[2662] | 958 | Clears all buffers that store what keys/buttons are being pressed at the moment. |
---|
[1755] | 959 | */ |
---|
[1878] | 960 | void InputManager::clearBuffers() |
---|
| 961 | { |
---|
| 962 | this->keysDown_.clear(); |
---|
| 963 | this->mouseButtonsDown_.clear(); |
---|
| 964 | for (unsigned int i = 0; i < this->joySticksSize_; ++i) |
---|
| 965 | this->joyStickButtonsDown_[i].clear(); |
---|
| 966 | } |
---|
[1502] | 967 | |
---|
[1878] | 968 | |
---|
[1755] | 969 | // ############################################################ |
---|
| 970 | // ##### OIS events ##### |
---|
| 971 | // ########## ########## |
---|
| 972 | // ############################################################ |
---|
[1219] | 973 | |
---|
[1755] | 974 | // ###### Key Events ###### |
---|
[1219] | 975 | |
---|
[1755] | 976 | /** |
---|
| 977 | @brief |
---|
| 978 | Event handler for the keyPressed Event. |
---|
| 979 | @param e |
---|
| 980 | Event information |
---|
| 981 | */ |
---|
| 982 | bool InputManager::keyPressed(const OIS::KeyEvent &e) |
---|
[1219] | 983 | { |
---|
[1755] | 984 | // check whether the key already is in the list (can happen when focus was lost) |
---|
| 985 | unsigned int iKey = 0; |
---|
[1887] | 986 | while (iKey < keysDown_.size() && keysDown_[iKey].key != (KeyCode::ByEnum)e.key) |
---|
[1755] | 987 | iKey++; |
---|
| 988 | if (iKey == keysDown_.size()) |
---|
| 989 | keysDown_.push_back(Key(e)); |
---|
| 990 | else |
---|
[1788] | 991 | { |
---|
| 992 | // This happens when XAutoRepeat is set under linux. The KeyPressed event gets then sent |
---|
| 993 | // continuously. |
---|
[1755] | 994 | return true; |
---|
[1788] | 995 | } |
---|
[1219] | 996 | |
---|
[1755] | 997 | // update modifiers |
---|
| 998 | if(e.key == OIS::KC_RMENU || e.key == OIS::KC_LMENU) |
---|
| 999 | keyboardModifiers_ |= KeyboardModifier::Alt; // alt key |
---|
| 1000 | if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL) |
---|
| 1001 | keyboardModifiers_ |= KeyboardModifier::Ctrl; // ctrl key |
---|
| 1002 | if(e.key == OIS::KC_RSHIFT || e.key == OIS::KC_LSHIFT) |
---|
| 1003 | keyboardModifiers_ |= KeyboardModifier::Shift; // shift key |
---|
[1219] | 1004 | |
---|
[1788] | 1005 | KeyEvent kEvt(e, keyboardModifiers_); |
---|
[2896] | 1006 | for (unsigned int iState = 0; iState < activeStatesTriggered_[Keyboard].size(); ++iState) |
---|
| 1007 | activeStatesTriggered_[Keyboard][iState]->keyPressed(kEvt); |
---|
[1219] | 1008 | |
---|
[1755] | 1009 | return true; |
---|
[1349] | 1010 | } |
---|
[1755] | 1011 | |
---|
| 1012 | /** |
---|
| 1013 | @brief |
---|
| 1014 | Event handler for the keyReleased Event. |
---|
| 1015 | @param e |
---|
| 1016 | Event information |
---|
| 1017 | */ |
---|
| 1018 | bool InputManager::keyReleased(const OIS::KeyEvent &e) |
---|
[1349] | 1019 | { |
---|
[1755] | 1020 | // remove the key from the keysDown_ list |
---|
| 1021 | for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++) |
---|
| 1022 | { |
---|
[1887] | 1023 | if (keysDown_[iKey].key == (KeyCode::ByEnum)e.key) |
---|
[1755] | 1024 | { |
---|
| 1025 | keysDown_.erase(keysDown_.begin() + iKey); |
---|
| 1026 | break; |
---|
| 1027 | } |
---|
| 1028 | } |
---|
[1502] | 1029 | |
---|
[1755] | 1030 | // update modifiers |
---|
| 1031 | if(e.key == OIS::KC_RMENU || e.key == OIS::KC_LMENU) |
---|
| 1032 | keyboardModifiers_ &= ~KeyboardModifier::Alt; // alt key |
---|
| 1033 | if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL) |
---|
| 1034 | keyboardModifiers_ &= ~KeyboardModifier::Ctrl; // ctrl key |
---|
| 1035 | if(e.key == OIS::KC_RSHIFT || e.key == OIS::KC_LSHIFT) |
---|
| 1036 | keyboardModifiers_ &= ~KeyboardModifier::Shift; // shift key |
---|
[1293] | 1037 | |
---|
[1788] | 1038 | KeyEvent kEvt(e, keyboardModifiers_); |
---|
[2896] | 1039 | for (unsigned int iState = 0; iState < activeStatesTriggered_[Keyboard].size(); ++iState) |
---|
| 1040 | activeStatesTriggered_[Keyboard][iState]->keyReleased(kEvt); |
---|
[1502] | 1041 | |
---|
[1755] | 1042 | return true; |
---|
| 1043 | } |
---|
[1502] | 1044 | |
---|
| 1045 | |
---|
[1755] | 1046 | // ###### Mouse Events ###### |
---|
[1502] | 1047 | |
---|
[1755] | 1048 | /** |
---|
| 1049 | @brief |
---|
| 1050 | Event handler for the mouseMoved Event. |
---|
| 1051 | @param e |
---|
| 1052 | Event information |
---|
| 1053 | */ |
---|
| 1054 | bool InputManager::mouseMoved(const OIS::MouseEvent &e) |
---|
| 1055 | { |
---|
| 1056 | // check for actual moved event |
---|
| 1057 | if (e.state.X.rel != 0 || e.state.Y.rel != 0) |
---|
| 1058 | { |
---|
[1788] | 1059 | IntVector2 abs(e.state.X.abs, e.state.Y.abs); |
---|
| 1060 | IntVector2 rel(e.state.X.rel, e.state.Y.rel); |
---|
| 1061 | IntVector2 clippingSize(e.state.width, e.state.height); |
---|
[2896] | 1062 | for (unsigned int iState = 0; iState < activeStatesTriggered_[Mouse].size(); ++iState) |
---|
| 1063 | activeStatesTriggered_[Mouse][iState]->mouseMoved(abs, rel, clippingSize); |
---|
[1755] | 1064 | } |
---|
[1502] | 1065 | |
---|
[1755] | 1066 | // check for mouse scrolled event |
---|
| 1067 | if (e.state.Z.rel != 0) |
---|
| 1068 | { |
---|
[2896] | 1069 | for (unsigned int iState = 0; iState < activeStatesTriggered_[Mouse].size(); ++iState) |
---|
| 1070 | activeStatesTriggered_[Mouse][iState]->mouseScrolled(e.state.Z.abs, e.state.Z.rel); |
---|
[1755] | 1071 | } |
---|
[1502] | 1072 | |
---|
[1755] | 1073 | return true; |
---|
| 1074 | } |
---|
[1502] | 1075 | |
---|
[1755] | 1076 | /** |
---|
| 1077 | @brief |
---|
| 1078 | Event handler for the mousePressed Event. |
---|
| 1079 | @param e |
---|
| 1080 | Event information |
---|
| 1081 | @param id |
---|
| 1082 | The ID of the mouse button |
---|
| 1083 | */ |
---|
| 1084 | bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
| 1085 | { |
---|
| 1086 | // check whether the button already is in the list (can happen when focus was lost) |
---|
| 1087 | unsigned int iButton = 0; |
---|
[1887] | 1088 | while (iButton < mouseButtonsDown_.size() && mouseButtonsDown_[iButton] != (MouseButtonCode::ByEnum)id) |
---|
[1755] | 1089 | iButton++; |
---|
| 1090 | if (iButton == mouseButtonsDown_.size()) |
---|
[1887] | 1091 | mouseButtonsDown_.push_back((MouseButtonCode::ByEnum)id); |
---|
[1502] | 1092 | |
---|
[2896] | 1093 | for (unsigned int iState = 0; iState < activeStatesTriggered_[Mouse].size(); ++iState) |
---|
| 1094 | activeStatesTriggered_[Mouse][iState]->mouseButtonPressed((MouseButtonCode::ByEnum)id); |
---|
[1502] | 1095 | |
---|
[1755] | 1096 | return true; |
---|
| 1097 | } |
---|
[1502] | 1098 | |
---|
[1755] | 1099 | /** |
---|
| 1100 | @brief |
---|
| 1101 | Event handler for the mouseReleased Event. |
---|
| 1102 | @param e |
---|
| 1103 | Event information |
---|
| 1104 | @param id |
---|
| 1105 | The ID of the mouse button |
---|
| 1106 | */ |
---|
| 1107 | bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
| 1108 | { |
---|
| 1109 | // remove the button from the keysDown_ list |
---|
| 1110 | for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++) |
---|
| 1111 | { |
---|
[1887] | 1112 | if (mouseButtonsDown_[iButton] == (MouseButtonCode::ByEnum)id) |
---|
[1755] | 1113 | { |
---|
| 1114 | mouseButtonsDown_.erase(mouseButtonsDown_.begin() + iButton); |
---|
| 1115 | break; |
---|
| 1116 | } |
---|
| 1117 | } |
---|
[1502] | 1118 | |
---|
[2896] | 1119 | for (unsigned int iState = 0; iState < activeStatesTriggered_[Mouse].size(); ++iState) |
---|
| 1120 | activeStatesTriggered_[Mouse][iState]->mouseButtonReleased((MouseButtonCode::ByEnum)id); |
---|
[1219] | 1121 | |
---|
[1755] | 1122 | return true; |
---|
| 1123 | } |
---|
[1293] | 1124 | |
---|
[1502] | 1125 | |
---|
[1755] | 1126 | // ###### Joy Stick Events ###### |
---|
[1219] | 1127 | |
---|
[1755] | 1128 | /** |
---|
| 1129 | @brief |
---|
| 1130 | Returns the joy stick ID (orxonox) according to a OIS::JoyStickEvent |
---|
| 1131 | */ |
---|
| 1132 | inline unsigned int InputManager::_getJoystick(const OIS::JoyStickEvent& arg) |
---|
| 1133 | { |
---|
| 1134 | // use the device to identify which one called the method |
---|
| 1135 | OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device; |
---|
| 1136 | unsigned int iJoyStick = 0; |
---|
| 1137 | while (joySticks_[iJoyStick] != joyStick) |
---|
| 1138 | iJoyStick++; |
---|
| 1139 | // assert: Unknown joystick fired an event. |
---|
| 1140 | assert(iJoyStick != joySticksSize_); |
---|
| 1141 | return iJoyStick; |
---|
| 1142 | } |
---|
[1219] | 1143 | |
---|
[1755] | 1144 | bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button) |
---|
| 1145 | { |
---|
| 1146 | unsigned int iJoyStick = _getJoystick(arg); |
---|
[1502] | 1147 | |
---|
[1755] | 1148 | // check whether the button already is in the list (can happen when focus was lost) |
---|
[1887] | 1149 | std::vector<JoyStickButtonCode::ByEnum>& buttonsDown = joyStickButtonsDown_[iJoyStick]; |
---|
[1755] | 1150 | unsigned int iButton = 0; |
---|
| 1151 | while (iButton < buttonsDown.size() && buttonsDown[iButton] != button) |
---|
| 1152 | iButton++; |
---|
| 1153 | if (iButton == buttonsDown.size()) |
---|
[1887] | 1154 | buttonsDown.push_back((JoyStickButtonCode::ByEnum)button); |
---|
[1219] | 1155 | |
---|
[2896] | 1156 | for (unsigned int iState = 0; iState < activeStatesTriggered_[2 + iJoyStick].size(); ++iState) |
---|
| 1157 | activeStatesTriggered_[2 + iJoyStick][iState]->joyStickButtonPressed(iJoyStick, (JoyStickButtonCode::ByEnum)button); |
---|
[1502] | 1158 | |
---|
[1755] | 1159 | return true; |
---|
| 1160 | } |
---|
[1219] | 1161 | |
---|
[1755] | 1162 | bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button) |
---|
| 1163 | { |
---|
| 1164 | unsigned int iJoyStick = _getJoystick(arg); |
---|
[1219] | 1165 | |
---|
[1755] | 1166 | // remove the button from the joyStickButtonsDown_ list |
---|
[1887] | 1167 | std::vector<JoyStickButtonCode::ByEnum>& buttonsDown = joyStickButtonsDown_[iJoyStick]; |
---|
[1755] | 1168 | for (unsigned int iButton = 0; iButton < buttonsDown.size(); iButton++) |
---|
| 1169 | { |
---|
| 1170 | if (buttonsDown[iButton] == button) |
---|
| 1171 | { |
---|
| 1172 | buttonsDown.erase(buttonsDown.begin() + iButton); |
---|
| 1173 | break; |
---|
| 1174 | } |
---|
| 1175 | } |
---|
[1219] | 1176 | |
---|
[2896] | 1177 | for (unsigned int iState = 0; iState < activeStatesTriggered_[2 + iJoyStick].size(); ++iState) |
---|
| 1178 | activeStatesTriggered_[2 + iJoyStick][iState]->joyStickButtonReleased(iJoyStick, (JoyStickButtonCode::ByEnum)button); |
---|
[1219] | 1179 | |
---|
[1755] | 1180 | return true; |
---|
| 1181 | } |
---|
[1219] | 1182 | |
---|
[1755] | 1183 | /** |
---|
| 1184 | @brief |
---|
| 1185 | Calls the states for a particular axis with our enumeration. |
---|
| 1186 | Used by OIS sliders and OIS axes. |
---|
| 1187 | */ |
---|
| 1188 | void InputManager::_fireAxis(unsigned int iJoyStick, int axis, int value) |
---|
| 1189 | { |
---|
[2662] | 1190 | if (internalState_ & Calibrating) |
---|
[1755] | 1191 | { |
---|
[2662] | 1192 | if (value < joyStickMinValues_[iJoyStick][axis]) |
---|
| 1193 | joyStickMinValues_[iJoyStick][axis] = value; |
---|
| 1194 | if (value > joyStickMaxValues_[iJoyStick][axis]) |
---|
| 1195 | joyStickMaxValues_[iJoyStick][axis] = value; |
---|
[1755] | 1196 | } |
---|
| 1197 | else |
---|
| 1198 | { |
---|
[3196] | 1199 | float fValue = static_cast<float>(value - joyStickCalibrations_[iJoyStick].middleValue[axis]); |
---|
[1755] | 1200 | if (fValue > 0.0f) |
---|
[2662] | 1201 | fValue *= joyStickCalibrations_[iJoyStick].positiveCoeff[axis]; |
---|
[1755] | 1202 | else |
---|
[2662] | 1203 | fValue *= joyStickCalibrations_[iJoyStick].negativeCoeff[axis]; |
---|
[1219] | 1204 | |
---|
[2896] | 1205 | for (unsigned int iState = 0; iState < activeStatesTriggered_[2 + iJoyStick].size(); ++iState) |
---|
| 1206 | activeStatesTriggered_[2 + iJoyStick][iState]->joyStickAxisMoved(iJoyStick, axis, fValue); |
---|
[1755] | 1207 | } |
---|
| 1208 | } |
---|
[1219] | 1209 | |
---|
[1755] | 1210 | bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis) |
---|
| 1211 | { |
---|
| 1212 | unsigned int iJoyStick = _getJoystick(arg); |
---|
[1219] | 1213 | |
---|
[1755] | 1214 | // keep in mind that the first 8 axes are reserved for the sliders |
---|
[2662] | 1215 | _fireAxis(iJoyStick, axis + sliderAxes, arg.state.mAxes[axis].abs); |
---|
[1293] | 1216 | |
---|
[1755] | 1217 | return true; |
---|
| 1218 | } |
---|
[1293] | 1219 | |
---|
[1755] | 1220 | bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id) |
---|
| 1221 | { |
---|
| 1222 | unsigned int iJoyStick = _getJoystick(arg); |
---|
[1293] | 1223 | |
---|
[1755] | 1224 | if (sliderStates_[iJoyStick].sliderStates[id].x != arg.state.mSliders[id].abX) |
---|
| 1225 | _fireAxis(iJoyStick, id * 2, arg.state.mSliders[id].abX); |
---|
| 1226 | else if (sliderStates_[iJoyStick].sliderStates[id].y != arg.state.mSliders[id].abY) |
---|
| 1227 | _fireAxis(iJoyStick, id * 2 + 1, arg.state.mSliders[id].abY); |
---|
[1219] | 1228 | |
---|
[1755] | 1229 | return true; |
---|
| 1230 | } |
---|
[1219] | 1231 | |
---|
[1755] | 1232 | bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id) |
---|
[1219] | 1233 | { |
---|
[1755] | 1234 | unsigned int iJoyStick = _getJoystick(arg); |
---|
[918] | 1235 | |
---|
[1755] | 1236 | // translate the POV into 8 simple buttons |
---|
[922] | 1237 | |
---|
[1755] | 1238 | int lastState = povStates_[iJoyStick][id]; |
---|
| 1239 | if (lastState & OIS::Pov::North) |
---|
| 1240 | buttonReleased(arg, 32 + id * 4 + 0); |
---|
| 1241 | if (lastState & OIS::Pov::South) |
---|
| 1242 | buttonReleased(arg, 32 + id * 4 + 1); |
---|
| 1243 | if (lastState & OIS::Pov::East) |
---|
| 1244 | buttonReleased(arg, 32 + id * 4 + 2); |
---|
| 1245 | if (lastState & OIS::Pov::West) |
---|
| 1246 | buttonReleased(arg, 32 + id * 4 + 3); |
---|
[918] | 1247 | |
---|
[1755] | 1248 | povStates_[iJoyStick].povStates[id] = arg.state.mPOV[id].direction; |
---|
[1219] | 1249 | |
---|
[1755] | 1250 | int currentState = povStates_[iJoyStick][id]; |
---|
| 1251 | if (currentState & OIS::Pov::North) |
---|
| 1252 | buttonPressed(arg, 32 + id * 4 + 0); |
---|
| 1253 | if (currentState & OIS::Pov::South) |
---|
| 1254 | buttonPressed(arg, 32 + id * 4 + 1); |
---|
| 1255 | if (currentState & OIS::Pov::East) |
---|
| 1256 | buttonPressed(arg, 32 + id * 4 + 2); |
---|
| 1257 | if (currentState & OIS::Pov::West) |
---|
| 1258 | buttonPressed(arg, 32 + id * 4 + 3); |
---|
[1502] | 1259 | |
---|
[1755] | 1260 | return true; |
---|
[1219] | 1261 | } |
---|
[1066] | 1262 | |
---|
| 1263 | |
---|
[1755] | 1264 | // ############################################################ |
---|
| 1265 | // ##### Other Public Interface Methods ##### |
---|
| 1266 | // ########## ########## |
---|
| 1267 | // ############################################################ |
---|
[1219] | 1268 | |
---|
[1755] | 1269 | /** |
---|
| 1270 | @brief |
---|
| 1271 | Adjusts the mouse window metrics. |
---|
| 1272 | This method has to be called every time the size of the window changes. |
---|
| 1273 | @param width |
---|
| 1274 | The new width of the render window |
---|
| 1275 | @param^height |
---|
| 1276 | The new height of the render window |
---|
| 1277 | */ |
---|
| 1278 | void InputManager::setWindowExtents(const int width, const int height) |
---|
[1219] | 1279 | { |
---|
[1755] | 1280 | if (mouse_) |
---|
| 1281 | { |
---|
| 1282 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
---|
| 1283 | mouse_->getMouseState().width = width; |
---|
| 1284 | mouse_->getMouseState().height = height; |
---|
| 1285 | } |
---|
[1219] | 1286 | } |
---|
| 1287 | |
---|
[1887] | 1288 | /** |
---|
| 1289 | @brief |
---|
| 1290 | Sets the the name of the command used by the KeyDetector as callback. |
---|
| 1291 | @param command |
---|
| 1292 | Command name as string |
---|
| 1293 | */ |
---|
| 1294 | void InputManager::setKeyDetectorCallback(const std::string& command) |
---|
| 1295 | { |
---|
| 1296 | this->keyDetector_->setCallbackCommand(command); |
---|
| 1297 | } |
---|
[1219] | 1298 | |
---|
[1755] | 1299 | // ###### InputStates ###### |
---|
[1219] | 1300 | |
---|
[1755] | 1301 | /** |
---|
| 1302 | @brief |
---|
| 1303 | Adds a new key handler. |
---|
| 1304 | @param handler |
---|
| 1305 | Pointer to the handler object. |
---|
| 1306 | @param name |
---|
| 1307 | Unique name of the handler. |
---|
| 1308 | @param priority |
---|
[2896] | 1309 | Determines which InputState gets the input. Higher is better. |
---|
| 1310 | Use 0 to handle it implicitely by the order of activation. |
---|
| 1311 | Otherwise numbers larger than maxStateStackSize_s have to be used! |
---|
[1755] | 1312 | @return |
---|
| 1313 | True if added, false if name or priority already existed. |
---|
| 1314 | */ |
---|
[2896] | 1315 | bool InputManager::_configureInputState(InputState* state, const std::string& name, bool bAlwaysGetsInput, bool bTransparent, int priority) |
---|
[1219] | 1316 | { |
---|
[1755] | 1317 | if (name == "") |
---|
| 1318 | return false; |
---|
| 1319 | if (!state) |
---|
| 1320 | return false; |
---|
| 1321 | if (inputStatesByName_.find(name) == inputStatesByName_.end()) |
---|
| 1322 | { |
---|
[2896] | 1323 | if (priority >= InputStatePriority::HighPriority || priority == InputStatePriority::Empty) |
---|
[1755] | 1324 | { |
---|
[2896] | 1325 | // Make sure we don't add two high priority states with the same priority |
---|
| 1326 | for (std::map<std::string, InputState*>::const_iterator it = this->inputStatesByName_.begin(); |
---|
| 1327 | it != this->inputStatesByName_.end(); ++it) |
---|
| 1328 | { |
---|
| 1329 | if (it->second->getPriority() == priority) |
---|
| 1330 | { |
---|
| 1331 | COUT(2) << "Warning: Could not add an InputState with the same priority '" |
---|
| 1332 | << priority << "' != 0." << std::endl; |
---|
| 1333 | return false; |
---|
| 1334 | } |
---|
| 1335 | } |
---|
| 1336 | } |
---|
| 1337 | inputStatesByName_[name] = state; |
---|
| 1338 | state->setNumOfJoySticks(numberOfJoySticks()); |
---|
| 1339 | state->setName(name); |
---|
| 1340 | state->bAlwaysGetsInput_ = bAlwaysGetsInput; |
---|
| 1341 | state->bTransparent_ = bTransparent; |
---|
| 1342 | if (priority >= InputStatePriority::HighPriority || priority == InputStatePriority::Empty) |
---|
[1755] | 1343 | state->setPriority(priority); |
---|
[2896] | 1344 | return true; |
---|
[1755] | 1345 | } |
---|
| 1346 | else |
---|
| 1347 | { |
---|
| 1348 | COUT(2) << "Warning: Could not add an InputState with the same name '" << name << "'." << std::endl; |
---|
| 1349 | return false; |
---|
| 1350 | } |
---|
[1219] | 1351 | } |
---|
| 1352 | |
---|
[1755] | 1353 | /** |
---|
| 1354 | @brief |
---|
| 1355 | Removes and destroys an input state internally. |
---|
| 1356 | @param name |
---|
| 1357 | Name of the handler. |
---|
| 1358 | @return |
---|
| 1359 | True if removal was successful, false if name was not found. |
---|
| 1360 | @remarks |
---|
| 1361 | You can't remove the internal states "empty", "calibrator" and "detector". |
---|
[2896] | 1362 | The removal process is being postponed if InputManager::update() is currently running. |
---|
[1755] | 1363 | */ |
---|
| 1364 | bool InputManager::requestDestroyState(const std::string& name) |
---|
[1219] | 1365 | { |
---|
[1881] | 1366 | if (name == "empty") |
---|
[1755] | 1367 | { |
---|
[1881] | 1368 | COUT(2) << "InputManager: Removing the empty state is not allowed!" << std::endl; |
---|
[1755] | 1369 | return false; |
---|
| 1370 | } |
---|
| 1371 | std::map<std::string, InputState*>::iterator it = inputStatesByName_.find(name); |
---|
| 1372 | if (it != inputStatesByName_.end()) |
---|
| 1373 | { |
---|
| 1374 | if (activeStates_.find(it->second->getPriority()) != activeStates_.end()) |
---|
| 1375 | { |
---|
| 1376 | // The state is still active. We have to postpone |
---|
| 1377 | stateLeaveRequests_.insert(it->second); |
---|
| 1378 | stateDestroyRequests_.insert(it->second); |
---|
| 1379 | } |
---|
| 1380 | else if (this->internalState_ & Ticking) |
---|
| 1381 | { |
---|
| 1382 | // cannot remove state while ticking |
---|
| 1383 | stateDestroyRequests_.insert(it->second); |
---|
| 1384 | } |
---|
| 1385 | else |
---|
| 1386 | _destroyState(it->second); |
---|
[1219] | 1387 | |
---|
[1755] | 1388 | return true; |
---|
| 1389 | } |
---|
| 1390 | return false; |
---|
[1219] | 1391 | } |
---|
| 1392 | |
---|
[1755] | 1393 | /** |
---|
| 1394 | @brief |
---|
| 1395 | Returns the pointer to the requested InputState. |
---|
| 1396 | @param name |
---|
| 1397 | Unique name of the state. |
---|
| 1398 | @return |
---|
| 1399 | Pointer to the instance, 0 if name was not found. |
---|
| 1400 | */ |
---|
| 1401 | InputState* InputManager::getState(const std::string& name) |
---|
[1219] | 1402 | { |
---|
[1755] | 1403 | std::map<std::string, InputState*>::iterator it = inputStatesByName_.find(name); |
---|
| 1404 | if (it != inputStatesByName_.end()) |
---|
| 1405 | return it->second; |
---|
| 1406 | else |
---|
| 1407 | return 0; |
---|
[1219] | 1408 | } |
---|
| 1409 | |
---|
[1755] | 1410 | /** |
---|
| 1411 | @brief |
---|
| 1412 | Returns the current input state (there might be others active too!) |
---|
| 1413 | @return |
---|
| 1414 | The current highest prioritised active input state. |
---|
| 1415 | */ |
---|
| 1416 | InputState* InputManager::getCurrentState() |
---|
[1219] | 1417 | { |
---|
[1755] | 1418 | return (*activeStates_.rbegin()).second; |
---|
[1219] | 1419 | } |
---|
| 1420 | |
---|
[1755] | 1421 | /** |
---|
| 1422 | @brief |
---|
| 1423 | Activates a specific input state. |
---|
| 1424 | It might not be really activated if the priority is too low! |
---|
| 1425 | @param name |
---|
| 1426 | Unique name of the state. |
---|
| 1427 | @return |
---|
| 1428 | False if name was not found, true otherwise. |
---|
| 1429 | */ |
---|
| 1430 | bool InputManager::requestEnterState(const std::string& name) |
---|
[1219] | 1431 | { |
---|
[1755] | 1432 | // get pointer from the map with all stored handlers |
---|
| 1433 | std::map<std::string, InputState*>::const_iterator it = inputStatesByName_.find(name); |
---|
| 1434 | if (it != inputStatesByName_.end()) |
---|
| 1435 | { |
---|
| 1436 | // exists |
---|
| 1437 | if (activeStates_.find(it->second->getPriority()) == activeStates_.end()) |
---|
| 1438 | { |
---|
| 1439 | // not active |
---|
| 1440 | if (stateDestroyRequests_.find(it->second) == stateDestroyRequests_.end()) |
---|
| 1441 | { |
---|
| 1442 | // not scheduled for destruction |
---|
[2896] | 1443 | // prevents a state being added multiple times |
---|
[1755] | 1444 | stateEnterRequests_.insert(it->second); |
---|
| 1445 | return true; |
---|
| 1446 | } |
---|
| 1447 | } |
---|
| 1448 | } |
---|
| 1449 | return false; |
---|
[1219] | 1450 | } |
---|
| 1451 | |
---|
[1755] | 1452 | /** |
---|
| 1453 | @brief |
---|
| 1454 | Deactivates a specific input state. |
---|
| 1455 | @param name |
---|
| 1456 | Unique name of the state. |
---|
| 1457 | @return |
---|
| 1458 | False if name was not found, true otherwise. |
---|
| 1459 | */ |
---|
| 1460 | bool InputManager::requestLeaveState(const std::string& name) |
---|
[1219] | 1461 | { |
---|
[2896] | 1462 | if (name == "empty") |
---|
| 1463 | { |
---|
| 1464 | COUT(2) << "InputManager: Leaving the empty state is not allowed!" << std::endl; |
---|
| 1465 | return false; |
---|
| 1466 | } |
---|
[1755] | 1467 | // get pointer from the map with all stored handlers |
---|
| 1468 | std::map<std::string, InputState*>::const_iterator it = inputStatesByName_.find(name); |
---|
| 1469 | if (it != inputStatesByName_.end()) |
---|
| 1470 | { |
---|
| 1471 | // exists |
---|
| 1472 | if (activeStates_.find(it->second->getPriority()) != activeStates_.end()) |
---|
| 1473 | { |
---|
| 1474 | // active |
---|
| 1475 | stateLeaveRequests_.insert(it->second); |
---|
| 1476 | return true; |
---|
| 1477 | } |
---|
| 1478 | } |
---|
| 1479 | return false; |
---|
[1219] | 1480 | } |
---|
| 1481 | |
---|
| 1482 | |
---|
[1755] | 1483 | // ############################################################ |
---|
| 1484 | // ##### Console Commands ##### |
---|
| 1485 | // ########## ########## |
---|
| 1486 | // ############################################################ |
---|
[1219] | 1487 | |
---|
[1755] | 1488 | /** |
---|
| 1489 | @brief |
---|
| 1490 | Starts joy stick calibration. |
---|
| 1491 | */ |
---|
| 1492 | void InputManager::calibrate() |
---|
[1219] | 1493 | { |
---|
[2662] | 1494 | COUT(0) << "Move all joy stick axes fully in all directions." << std::endl |
---|
| 1495 | << "When done, put the axex in the middle position and press enter." << std::endl; |
---|
| 1496 | |
---|
| 1497 | getInstance()._startCalibration(); |
---|
[1219] | 1498 | } |
---|
| 1499 | |
---|
[1755] | 1500 | /** |
---|
| 1501 | @brief |
---|
| 1502 | Reloads the input system |
---|
| 1503 | */ |
---|
| 1504 | void InputManager::reload(bool joyStickSupport) |
---|
[1219] | 1505 | { |
---|
[1755] | 1506 | getInstance().reloadInputSystem(joyStickSupport); |
---|
[1219] | 1507 | } |
---|
[3084] | 1508 | |
---|
| 1509 | |
---|
| 1510 | // ############################################################ |
---|
| 1511 | // ##### ugly hacks ##### |
---|
| 1512 | // ########## ########## |
---|
| 1513 | // ############################################################ |
---|
| 1514 | |
---|
| 1515 | #ifdef ORXONOX_PLATFORM_LINUX |
---|
| 1516 | void InputManager::grabMouse() |
---|
| 1517 | { |
---|
| 1518 | OIS::LinuxMouse* linuxMouse = dynamic_cast<OIS::LinuxMouse*>(singletonRef_s->mouse_); |
---|
| 1519 | assert(linuxMouse); |
---|
| 1520 | linuxMouse->grab(true); |
---|
| 1521 | } |
---|
| 1522 | |
---|
| 1523 | void InputManager::ungrabMouse() |
---|
| 1524 | { |
---|
| 1525 | OIS::LinuxMouse* linuxMouse = dynamic_cast<OIS::LinuxMouse*>(singletonRef_s->mouse_); |
---|
| 1526 | assert(linuxMouse); |
---|
| 1527 | linuxMouse->grab(false); |
---|
| 1528 | } |
---|
| 1529 | #endif |
---|
[918] | 1530 | } |
---|