[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> |
---|
[1555] | 40 | |
---|
[1755] | 41 | #include "ois/OISException.h" |
---|
| 42 | #include "ois/OISInputManager.h" |
---|
| 43 | |
---|
[1519] | 44 | #include "core/CoreIncludes.h" |
---|
| 45 | #include "core/ConfigValueIncludes.h" |
---|
[1755] | 46 | #include "core/Exception.h" |
---|
[1519] | 47 | #include "core/CommandExecutor.h" |
---|
| 48 | #include "core/ConsoleCommand.h" |
---|
[1747] | 49 | #include "util/Debug.h" |
---|
[1555] | 50 | |
---|
[1219] | 51 | #include "InputBuffer.h" |
---|
[1502] | 52 | #include "KeyBinder.h" |
---|
[1520] | 53 | #include "KeyDetector.h" |
---|
| 54 | #include "CalibratorCallback.h" |
---|
[1755] | 55 | #include "InputState.h" |
---|
| 56 | #include "SimpleInputState.h" |
---|
| 57 | #include "ExtendedInputState.h" |
---|
[918] | 58 | |
---|
| 59 | namespace orxonox |
---|
| 60 | { |
---|
[1755] | 61 | SetConsoleCommand(InputManager, keyBind, true); |
---|
| 62 | SetConsoleCommand(InputManager, storeKeyStroke, true); |
---|
| 63 | SetConsoleCommand(InputManager, calibrate, true); |
---|
| 64 | SetConsoleCommand(InputManager, reload, false); |
---|
[1502] | 65 | |
---|
[1755] | 66 | std::string InputManager::bindingCommmandString_s = ""; |
---|
| 67 | InputManager* InputManager::singletonRef_s = 0; |
---|
[1084] | 68 | |
---|
[1755] | 69 | using namespace InputDevice; |
---|
[929] | 70 | |
---|
[1755] | 71 | /** |
---|
| 72 | @brief |
---|
| 73 | Defines the |= operator for easier use. |
---|
| 74 | */ |
---|
| 75 | inline InputManager::InputManagerState operator|=(InputManager::InputManagerState& lval, |
---|
| 76 | InputManager::InputManagerState rval) |
---|
| 77 | { |
---|
| 78 | return (lval = (InputManager::InputManagerState)(lval | rval)); |
---|
| 79 | } |
---|
[919] | 80 | |
---|
[1755] | 81 | /** |
---|
| 82 | @brief |
---|
| 83 | Defines the &= operator for easier use. |
---|
| 84 | */ |
---|
| 85 | inline InputManager::InputManagerState operator&=(InputManager::InputManagerState& lval, int rval) |
---|
| 86 | { |
---|
| 87 | return (lval = (InputManager::InputManagerState)(lval & rval)); |
---|
| 88 | } |
---|
[1219] | 89 | |
---|
[1755] | 90 | // ############################################################ |
---|
| 91 | // ##### Initialisation ##### |
---|
| 92 | // ########## ########## |
---|
| 93 | // ############################################################ |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | @brief |
---|
| 97 | Constructor only sets member fields to initial zero values |
---|
| 98 | and registers the class in the class hierarchy. |
---|
| 99 | */ |
---|
| 100 | InputManager::InputManager() |
---|
| 101 | : inputSystem_(0) |
---|
| 102 | , keyboard_(0) |
---|
| 103 | , mouse_(0) |
---|
| 104 | , joySticksSize_(0) |
---|
| 105 | , devicesNum_(0) |
---|
| 106 | , windowHnd_(0) |
---|
| 107 | , internalState_(Uninitialised) |
---|
| 108 | , stateDetector_(0) |
---|
| 109 | , stateCalibrator_(0) |
---|
| 110 | , stateEmpty_(0) |
---|
| 111 | , bCalibrating_(false) |
---|
| 112 | , keyboardModifiers_(0) |
---|
[918] | 113 | { |
---|
[1755] | 114 | RegisterRootObject(InputManager); |
---|
[1219] | 115 | |
---|
[1755] | 116 | assert(singletonRef_s == 0); |
---|
| 117 | singletonRef_s = this; |
---|
| 118 | } |
---|
[918] | 119 | |
---|
[1755] | 120 | /** |
---|
| 121 | @brief |
---|
| 122 | Creates the OIS::InputMananger, the keyboard, the mouse and |
---|
| 123 | the joysticks and assigns the key bindings. |
---|
| 124 | @param windowHnd |
---|
| 125 | The window handle of the render window |
---|
| 126 | @param windowWidth |
---|
| 127 | The width of the render window |
---|
| 128 | @param windowHeight |
---|
| 129 | The height of the render window |
---|
| 130 | @param joyStickSupport |
---|
| 131 | Whether or not to load the joy sticks as well |
---|
| 132 | */ |
---|
| 133 | void InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport) |
---|
| 134 | { |
---|
| 135 | CCOUT(3) << "Initialising Input System..." << std::endl; |
---|
| 136 | |
---|
| 137 | if (!(internalState_ & OISReady)) |
---|
| 138 | { |
---|
| 139 | CCOUT(4) << "Initialising OIS components..." << std::endl; |
---|
| 140 | |
---|
| 141 | // store handle internally so we can reload OIS |
---|
| 142 | windowHnd_ = windowHnd; |
---|
| 143 | |
---|
| 144 | OIS::ParamList paramList; |
---|
| 145 | std::ostringstream windowHndStr; |
---|
| 146 | |
---|
| 147 | // Fill parameter list |
---|
| 148 | windowHndStr << (unsigned int)windowHnd_; |
---|
| 149 | paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
| 150 | //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE"))); |
---|
| 151 | //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND"))); |
---|
[1735] | 152 | #if defined OIS_LINUX_PLATFORM |
---|
[1755] | 153 | paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true"))); |
---|
[1735] | 154 | #endif |
---|
[928] | 155 | |
---|
[1755] | 156 | inputSystem_ = OIS::InputManager::createInputSystem(paramList); |
---|
| 157 | CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl; |
---|
[1219] | 158 | |
---|
[1755] | 159 | _initialiseKeyboard(); |
---|
[1219] | 160 | |
---|
[1755] | 161 | _initialiseMouse(); |
---|
[1219] | 162 | |
---|
[1755] | 163 | if (joyStickSupport) |
---|
| 164 | _initialiseJoySticks(); |
---|
[1219] | 165 | |
---|
[1755] | 166 | // Set mouse/joystick region |
---|
| 167 | if (mouse_) |
---|
| 168 | setWindowExtents(windowWidth, windowHeight); |
---|
[1219] | 169 | |
---|
[1755] | 170 | // clear all buffers |
---|
| 171 | _clearBuffers(); |
---|
[1502] | 172 | |
---|
[1755] | 173 | // load joy stick calibration |
---|
| 174 | setConfigValues(); |
---|
[1502] | 175 | |
---|
[1755] | 176 | internalState_ |= OISReady; |
---|
[1502] | 177 | |
---|
[1755] | 178 | CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl; |
---|
| 179 | } |
---|
| 180 | else |
---|
| 181 | { |
---|
| 182 | CCOUT(2) << "Warning: OIS compoments already initialised, skipping" << std::endl; |
---|
| 183 | } |
---|
[1502] | 184 | |
---|
[1755] | 185 | if (!(internalState_ & InternalsReady)) |
---|
| 186 | { |
---|
| 187 | CCOUT(4) << "Initialising InputStates components..." << std::endl; |
---|
[1502] | 188 | |
---|
[1755] | 189 | stateEmpty_ = createInputState<SimpleInputState>("empty", -1); |
---|
| 190 | stateEmpty_->setHandler(new EmptyHandler()); |
---|
| 191 | activeStates_[stateEmpty_->getPriority()] = stateEmpty_; |
---|
[1502] | 192 | |
---|
[1755] | 193 | stateDetector_ = createInputState<SimpleInputState>("detector", 101); |
---|
| 194 | KeyDetector* temp = new KeyDetector(); |
---|
| 195 | temp->loadBindings("storeKeyStroke"); |
---|
| 196 | stateDetector_->setHandler(temp); |
---|
[918] | 197 | |
---|
[1755] | 198 | stateCalibrator_ = createInputState<SimpleInputState>("calibrator", 100); |
---|
| 199 | stateCalibrator_->setHandler(new EmptyHandler()); |
---|
| 200 | InputBuffer* buffer = new InputBuffer(); |
---|
| 201 | buffer->registerListener(this, &InputManager::_completeCalibration, '\r', true); |
---|
| 202 | stateCalibrator_->setKeyHandler(buffer); |
---|
| 203 | |
---|
| 204 | internalState_ |= InternalsReady; |
---|
| 205 | |
---|
| 206 | CCOUT(4) << "Initialising InputStates complete." << std::endl; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | _updateActiveStates(); |
---|
| 210 | |
---|
| 211 | CCOUT(3) << "Initialising complete." << std::endl; |
---|
[1219] | 212 | } |
---|
[928] | 213 | |
---|
[1755] | 214 | /** |
---|
| 215 | @brief |
---|
| 216 | Creates a keyboard and sets the event handler. |
---|
| 217 | @return |
---|
| 218 | False if keyboard stays uninitialised, true otherwise. |
---|
| 219 | */ |
---|
| 220 | void InputManager::_initialiseKeyboard() |
---|
[1219] | 221 | { |
---|
[1755] | 222 | if (keyboard_ != 0) |
---|
| 223 | { |
---|
| 224 | CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl; |
---|
| 225 | return; |
---|
| 226 | } |
---|
| 227 | if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0) |
---|
| 228 | { |
---|
| 229 | keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true); |
---|
| 230 | // register our listener in OIS. |
---|
| 231 | keyboard_->setEventCallback(this); |
---|
| 232 | // note: OIS will not detect keys that have already been down when the keyboard was created. |
---|
| 233 | CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl; |
---|
| 234 | } |
---|
| 235 | else |
---|
| 236 | { |
---|
| 237 | ThrowException(InitialisationFailed, "No keyboard found!"); |
---|
| 238 | } |
---|
[1219] | 239 | } |
---|
[1035] | 240 | |
---|
[1755] | 241 | /** |
---|
| 242 | @brief |
---|
| 243 | Creates a mouse and sets the event handler. |
---|
| 244 | @return |
---|
| 245 | False if mouse stays uninitialised, true otherwise. |
---|
| 246 | */ |
---|
| 247 | void InputManager::_initialiseMouse() |
---|
[1219] | 248 | { |
---|
[1755] | 249 | if (mouse_ != 0) |
---|
| 250 | { |
---|
| 251 | CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl; |
---|
| 252 | return; |
---|
| 253 | } |
---|
[1219] | 254 | try |
---|
| 255 | { |
---|
[1755] | 256 | if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0) |
---|
| 257 | { |
---|
| 258 | mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); |
---|
| 259 | // register our listener in OIS. |
---|
| 260 | mouse_->setEventCallback(this); |
---|
| 261 | CCOUT(ORX_DEBUG) << "Created OIS mouse" << std::endl; |
---|
| 262 | } |
---|
| 263 | else |
---|
| 264 | { |
---|
| 265 | CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl; |
---|
| 266 | } |
---|
[1219] | 267 | } |
---|
| 268 | catch (OIS::Exception ex) |
---|
| 269 | { |
---|
[1755] | 270 | CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS mouse\n" |
---|
| 271 | << "OIS error message: \"" << ex.eText << "\"" << std::endl; |
---|
| 272 | mouse_ = 0; |
---|
[1219] | 273 | } |
---|
| 274 | } |
---|
[1755] | 275 | |
---|
| 276 | /** |
---|
| 277 | @brief |
---|
| 278 | Creates all joy sticks and sets the event handler. |
---|
| 279 | @return |
---|
| 280 | False joy stick stay uninitialised, true otherwise. |
---|
| 281 | */ |
---|
| 282 | void InputManager::_initialiseJoySticks() |
---|
[1219] | 283 | { |
---|
[1755] | 284 | if (joySticksSize_ > 0) |
---|
| 285 | { |
---|
| 286 | CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl; |
---|
| 287 | return; |
---|
| 288 | } |
---|
| 289 | if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0) |
---|
| 290 | { |
---|
| 291 | for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++) |
---|
| 292 | { |
---|
| 293 | try |
---|
| 294 | { |
---|
| 295 | OIS::JoyStick* stig = static_cast<OIS::JoyStick*> |
---|
| 296 | (inputSystem_->createInputObject(OIS::OISJoyStick, true)); |
---|
| 297 | CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl; |
---|
| 298 | joySticks_.push_back(stig); |
---|
| 299 | // register our listener in OIS. |
---|
| 300 | stig->setEventCallback(this); |
---|
| 301 | } |
---|
| 302 | catch (OIS::Exception ex) |
---|
| 303 | { |
---|
| 304 | CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n" |
---|
| 305 | << "OIS error message: \"" << ex.eText << "\"" << std::endl; |
---|
| 306 | } |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | else |
---|
| 310 | { |
---|
| 311 | //CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl; |
---|
| 312 | } |
---|
| 313 | _redimensionLists(); |
---|
[1219] | 314 | } |
---|
[1035] | 315 | |
---|
[1755] | 316 | /** |
---|
| 317 | @brief |
---|
| 318 | Sets the size of all the different lists that are dependent on the number |
---|
| 319 | of joy stick devices created. |
---|
| 320 | @remarks |
---|
| 321 | No matter whether there are a mouse and/or keyboard, they will always |
---|
| 322 | occupy 2 places in the device number dependent lists. |
---|
| 323 | */ |
---|
| 324 | void InputManager::_redimensionLists() |
---|
[1505] | 325 | { |
---|
[1755] | 326 | joySticksSize_ = joySticks_.size(); |
---|
| 327 | devicesNum_ = 2 + joySticksSize_; |
---|
| 328 | joyStickButtonsDown_ .resize(joySticksSize_); |
---|
| 329 | povStates_ .resize(joySticksSize_); |
---|
| 330 | sliderStates_ .resize(joySticksSize_); |
---|
| 331 | joySticksCalibration_.resize(joySticksSize_); |
---|
[1502] | 332 | |
---|
[1755] | 333 | for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) |
---|
| 334 | { |
---|
| 335 | // reset the calibration with default values |
---|
| 336 | for (unsigned int i = 0; i < 24; i++) |
---|
| 337 | { |
---|
| 338 | joySticksCalibration_[iJoyStick].negativeCoeff[i] = 1.0f/32767.0f; |
---|
| 339 | joySticksCalibration_[iJoyStick].positiveCoeff[i] = 1.0f/32768.0f; |
---|
| 340 | joySticksCalibration_[iJoyStick].zeroStates[i] = 0; |
---|
| 341 | } |
---|
| 342 | } |
---|
[1505] | 343 | |
---|
[1755] | 344 | // state management |
---|
| 345 | activeStatesTop_.resize(devicesNum_); |
---|
[1505] | 346 | |
---|
[1755] | 347 | // inform all states |
---|
| 348 | for (std::map<int, InputState*>::const_iterator it = inputStatesByPriority_.begin(); |
---|
| 349 | it != inputStatesByPriority_.end(); ++it) |
---|
| 350 | it->second->setNumOfJoySticks(joySticksSize_); |
---|
[1505] | 351 | } |
---|
[1502] | 352 | |
---|
[1755] | 353 | /** |
---|
| 354 | @brief |
---|
| 355 | Sets the configurable values. |
---|
| 356 | This mainly concerns joy stick calibrations. |
---|
| 357 | */ |
---|
| 358 | void InputManager::setConfigValues() |
---|
[1219] | 359 | { |
---|
[1755] | 360 | if (joySticksSize_ > 0) |
---|
| 361 | { |
---|
| 362 | std::vector<double> coeffPos; |
---|
| 363 | std::vector<double> coeffNeg; |
---|
| 364 | std::vector<int> zero; |
---|
| 365 | coeffPos.resize(24); |
---|
| 366 | coeffNeg.resize(24); |
---|
| 367 | zero.resize(24); |
---|
| 368 | for (unsigned int i = 0; i < 24; i++) |
---|
| 369 | { |
---|
| 370 | coeffPos[i] = 1.0f/32767.0f; |
---|
| 371 | coeffNeg[i] = 1.0f/32768.0f; |
---|
| 372 | zero[i] = 0; |
---|
| 373 | } |
---|
[1293] | 374 | |
---|
[1755] | 375 | ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer("CoeffPos"); |
---|
| 376 | if (!cont) |
---|
| 377 | { |
---|
| 378 | cont = new ConfigValueContainer(CFT_Settings, getIdentifier(), "CoeffPos", coeffPos); |
---|
| 379 | getIdentifier()->addConfigValueContainer("CoeffPos", cont); |
---|
| 380 | } |
---|
| 381 | cont->getValue(&coeffPos, this); |
---|
[1219] | 382 | |
---|
[1755] | 383 | cont = getIdentifier()->getConfigValueContainer("CoeffNeg"); |
---|
| 384 | if (!cont) |
---|
| 385 | { |
---|
| 386 | cont = new ConfigValueContainer(CFT_Settings, getIdentifier(), "CoeffNeg", coeffNeg); |
---|
| 387 | getIdentifier()->addConfigValueContainer("CoeffNeg", cont); |
---|
| 388 | } |
---|
| 389 | cont->getValue(&coeffNeg, this); |
---|
[1219] | 390 | |
---|
[1755] | 391 | cont = getIdentifier()->getConfigValueContainer("Zero"); |
---|
| 392 | if (!cont) |
---|
| 393 | { |
---|
| 394 | cont = new ConfigValueContainer(CFT_Settings, getIdentifier(), "Zero", zero); |
---|
| 395 | getIdentifier()->addConfigValueContainer("Zero", cont); |
---|
| 396 | } |
---|
| 397 | cont->getValue(&zero, this); |
---|
[1502] | 398 | |
---|
[1755] | 399 | // copy values to our own variables |
---|
| 400 | for (unsigned int i = 0; i < 24; i++) |
---|
| 401 | { |
---|
| 402 | joySticksCalibration_[0].positiveCoeff[i] = coeffPos[i]; |
---|
| 403 | joySticksCalibration_[0].negativeCoeff[i] = coeffNeg[i]; |
---|
| 404 | joySticksCalibration_[0].zeroStates[i] = zero[i]; |
---|
| 405 | } |
---|
| 406 | } |
---|
[1219] | 407 | } |
---|
[928] | 408 | |
---|
[1219] | 409 | |
---|
[1755] | 410 | // ############################################################ |
---|
| 411 | // ##### Destruction ##### |
---|
| 412 | // ########## ########## |
---|
| 413 | // ############################################################ |
---|
[1219] | 414 | |
---|
[1755] | 415 | /** |
---|
| 416 | @brief |
---|
| 417 | Destroys all the created input devices and states. |
---|
| 418 | */ |
---|
| 419 | InputManager::~InputManager() |
---|
[1219] | 420 | { |
---|
[1755] | 421 | if (internalState_ != Uninitialised) |
---|
| 422 | { |
---|
| 423 | try |
---|
| 424 | { |
---|
| 425 | CCOUT(3) << "Destroying ..." << std::endl; |
---|
[1219] | 426 | |
---|
[1755] | 427 | // clear our own states |
---|
| 428 | stateEmpty_->removeAndDestroyAllHandlers(); |
---|
| 429 | stateCalibrator_->removeAndDestroyAllHandlers(); |
---|
| 430 | stateDetector_->removeAndDestroyAllHandlers(); |
---|
[1219] | 431 | |
---|
[1755] | 432 | // kick all active states 'nicely' |
---|
| 433 | for (std::map<int, InputState*>::reverse_iterator rit = activeStates_.rbegin(); |
---|
| 434 | rit != activeStates_.rend(); ++rit) |
---|
| 435 | { |
---|
| 436 | (*rit).second->onLeave(); |
---|
| 437 | } |
---|
[1502] | 438 | |
---|
[1755] | 439 | // destroy all input states |
---|
| 440 | while (inputStatesByPriority_.size() > 0) |
---|
| 441 | _destroyState((*inputStatesByPriority_.rbegin()).second); |
---|
[1502] | 442 | |
---|
[1755] | 443 | // destroy the devices |
---|
| 444 | _destroyKeyboard(); |
---|
| 445 | _destroyMouse(); |
---|
| 446 | _destroyJoySticks(); |
---|
[1219] | 447 | |
---|
[1755] | 448 | OIS::InputManager::destroyInputSystem(inputSystem_); |
---|
[1349] | 449 | |
---|
[1755] | 450 | CCOUT(3) << "Destroying done." << std::endl; |
---|
| 451 | } |
---|
| 452 | catch (OIS::Exception& ex) |
---|
| 453 | { |
---|
| 454 | CCOUT(1) << "An exception has occured while destroying:\n" << ex.what() |
---|
| 455 | << "This could lead to a possible memory/resource leak!" << std::endl; |
---|
| 456 | } |
---|
| 457 | } |
---|
| 458 | } |
---|
[1349] | 459 | |
---|
[1755] | 460 | /** |
---|
| 461 | @brief |
---|
| 462 | Destroys the keyboard and sets it to 0. |
---|
| 463 | */ |
---|
| 464 | void InputManager::_destroyKeyboard() |
---|
| 465 | { |
---|
| 466 | assert(inputSystem_); |
---|
| 467 | if (keyboard_) |
---|
| 468 | inputSystem_->destroyInputObject(keyboard_); |
---|
| 469 | keyboard_ = 0; |
---|
| 470 | CCOUT(4) << "Keyboard destroyed." << std::endl; |
---|
| 471 | } |
---|
[1219] | 472 | |
---|
[1755] | 473 | /** |
---|
| 474 | @brief |
---|
| 475 | Destroys the mouse and sets it to 0. |
---|
| 476 | */ |
---|
| 477 | void InputManager::_destroyMouse() |
---|
| 478 | { |
---|
| 479 | assert(inputSystem_); |
---|
| 480 | if (mouse_) |
---|
| 481 | inputSystem_->destroyInputObject(mouse_); |
---|
| 482 | mouse_ = 0; |
---|
| 483 | CCOUT(4) << "Mouse destroyed." << std::endl; |
---|
| 484 | } |
---|
[1219] | 485 | |
---|
[1755] | 486 | /** |
---|
| 487 | @brief |
---|
| 488 | Destroys all the joy sticks and resizes the lists to 0. |
---|
| 489 | */ |
---|
| 490 | void InputManager::_destroyJoySticks() |
---|
[1022] | 491 | { |
---|
[1755] | 492 | if (joySticksSize_ > 0) |
---|
| 493 | { |
---|
| 494 | assert(inputSystem_); |
---|
| 495 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
| 496 | if (joySticks_[i] != 0) |
---|
| 497 | inputSystem_->destroyInputObject(joySticks_[i]); |
---|
[1219] | 498 | |
---|
[1755] | 499 | joySticks_.clear(); |
---|
| 500 | // don't use _redimensionLists(), might mess with registered handler if |
---|
| 501 | // downgrading from 2 to 1 joystick |
---|
| 502 | //_redimensionLists(); |
---|
| 503 | joySticksSize_ = 0; |
---|
| 504 | } |
---|
| 505 | CCOUT(4) << "Joy sticks destroyed." << std::endl; |
---|
| 506 | } |
---|
[1502] | 507 | |
---|
[1755] | 508 | /** |
---|
| 509 | @brief |
---|
| 510 | Removes and destroys an InputState. |
---|
| 511 | @return |
---|
| 512 | True if state was removed immediately, false if postponed. |
---|
| 513 | */ |
---|
| 514 | void InputManager::_destroyState(InputState* state) |
---|
| 515 | { |
---|
| 516 | assert(state && !(this->internalState_ & Ticking)); |
---|
| 517 | std::map<int, InputState*>::iterator it = this->activeStates_.find(state->getPriority()); |
---|
| 518 | if (it != this->activeStates_.end()) |
---|
| 519 | { |
---|
| 520 | this->activeStates_.erase(it); |
---|
| 521 | _updateActiveStates(); |
---|
| 522 | } |
---|
| 523 | inputStatesByPriority_.erase(state->getPriority()); |
---|
| 524 | inputStatesByName_.erase(state->getName()); |
---|
| 525 | delete state; |
---|
| 526 | } |
---|
[1502] | 527 | |
---|
[1755] | 528 | void InputManager::_clearBuffers() |
---|
| 529 | { |
---|
[1502] | 530 | keysDown_.clear(); |
---|
[1755] | 531 | keyboardModifiers_ = 0; |
---|
[1502] | 532 | mouseButtonsDown_.clear(); |
---|
[1755] | 533 | for (unsigned int i = 0; i < joySticksSize_; ++i) |
---|
[1219] | 534 | { |
---|
[1755] | 535 | joyStickButtonsDown_[i].clear(); |
---|
| 536 | for (int j = 0; j < 4; ++j) |
---|
| 537 | { |
---|
| 538 | sliderStates_[i].sliderStates[j].x = 0; |
---|
| 539 | sliderStates_[i].sliderStates[j].y = 0; |
---|
| 540 | povStates_[i][j] = 0; |
---|
| 541 | } |
---|
| 542 | } |
---|
| 543 | } |
---|
[1219] | 544 | |
---|
| 545 | |
---|
[1755] | 546 | // ############################################################ |
---|
| 547 | // ##### Reloading ##### |
---|
| 548 | // ########## ########## |
---|
| 549 | // ############################################################ |
---|
[1219] | 550 | |
---|
[1755] | 551 | /** |
---|
| 552 | @brief |
---|
| 553 | Public interface. Only reloads immediately if the call stack doesn't |
---|
| 554 | include the tick() method. |
---|
| 555 | @param joyStickSupport |
---|
| 556 | Whether or not to initialise joy sticks as well. |
---|
| 557 | */ |
---|
| 558 | void InputManager::reloadInputSystem(bool joyStickSupport) |
---|
| 559 | { |
---|
| 560 | if (internalState_ & Ticking) |
---|
| 561 | { |
---|
| 562 | // We cannot destroy OIS right now, because reload was probably |
---|
| 563 | // caused by a user clicking on a GUI item. The backtrace would then |
---|
| 564 | // include an OIS method. So it would be a very bad thing to destroy it.. |
---|
| 565 | internalState_ |= ReloadRequest; |
---|
| 566 | // Misuse of internalState_: We can easily store the joyStickSupport bool. |
---|
| 567 | // use Uninitialised as 0 value in order to make use of the overloaded |= operator |
---|
| 568 | internalState_ |= joyStickSupport ? JoyStickSupport : Uninitialised; |
---|
[1219] | 569 | } |
---|
[1755] | 570 | else if (internalState_ & OISReady) |
---|
| 571 | { |
---|
| 572 | _reload(joyStickSupport); |
---|
| 573 | } |
---|
[1502] | 574 | else |
---|
| 575 | { |
---|
[1755] | 576 | CCOUT(2) << "Warning: Cannot reload OIS. May not yet be initialised or" |
---|
| 577 | << "joy sticks are currently calibrating." << std::endl; |
---|
[1502] | 578 | } |
---|
[1755] | 579 | } |
---|
[1502] | 580 | |
---|
[1755] | 581 | /** |
---|
| 582 | @brief |
---|
| 583 | Internal reload method. Destroys the OIS devices and loads them again. |
---|
| 584 | */ |
---|
| 585 | void InputManager::_reload(bool joyStickSupport) |
---|
| 586 | { |
---|
| 587 | try |
---|
| 588 | { |
---|
| 589 | CCOUT(3) << "Reloading ..." << std::endl; |
---|
[1502] | 590 | |
---|
[1755] | 591 | // Save mouse clipping size |
---|
| 592 | int mouseWidth = mouse_->getMouseState().width; |
---|
| 593 | int mouseHeight = mouse_->getMouseState().height; |
---|
[1502] | 594 | |
---|
[1755] | 595 | internalState_ &= ~OISReady; |
---|
[1219] | 596 | |
---|
[1755] | 597 | // destroy the devices |
---|
| 598 | _destroyKeyboard(); |
---|
| 599 | _destroyMouse(); |
---|
| 600 | _destroyJoySticks(); |
---|
[1219] | 601 | |
---|
[1755] | 602 | OIS::InputManager::destroyInputSystem(inputSystem_); |
---|
| 603 | inputSystem_ = 0; |
---|
[1219] | 604 | |
---|
[1755] | 605 | // clear all buffers containing input information |
---|
| 606 | _clearBuffers(); |
---|
[1219] | 607 | |
---|
[1755] | 608 | initialise(windowHnd_, mouseWidth, mouseHeight, joyStickSupport); |
---|
| 609 | |
---|
| 610 | CCOUT(3) << "Reloading done." << std::endl; |
---|
| 611 | } |
---|
| 612 | catch (OIS::Exception& ex) |
---|
| 613 | { |
---|
| 614 | CCOUT(1) << "An exception has occured while reloading:\n" << ex.what() << std::endl; |
---|
| 615 | } |
---|
[1502] | 616 | } |
---|
[1219] | 617 | |
---|
[1755] | 618 | // ############################################################ |
---|
| 619 | // ##### Runtime Methods ##### |
---|
| 620 | // ########## ########## |
---|
| 621 | // ############################################################ |
---|
[1555] | 622 | |
---|
[1755] | 623 | /** |
---|
| 624 | @brief |
---|
| 625 | Updates the InputManager. Tick is called by the Core class. |
---|
| 626 | @param dt |
---|
| 627 | Delta time |
---|
| 628 | */ |
---|
| 629 | void InputManager::tick(float dt) |
---|
[1502] | 630 | { |
---|
[1755] | 631 | if (internalState_ == Uninitialised) |
---|
| 632 | return; |
---|
| 633 | else if (internalState_ & ReloadRequest) |
---|
| 634 | { |
---|
| 635 | _reload(internalState_ & JoyStickSupport); |
---|
| 636 | internalState_ &= ~ReloadRequest; |
---|
| 637 | internalState_ &= ~JoyStickSupport; |
---|
| 638 | } |
---|
[1349] | 639 | |
---|
[1755] | 640 | // check for states to leave |
---|
| 641 | for (std::set<InputState*>::reverse_iterator rit = stateLeaveRequests_.rbegin(); |
---|
| 642 | rit != stateLeaveRequests_.rend(); ++rit) |
---|
| 643 | { |
---|
| 644 | (*rit)->onLeave(); |
---|
| 645 | // just to be sure that the state actually is registered |
---|
| 646 | assert(inputStatesByName_.find((*rit)->getName()) != inputStatesByName_.end()); |
---|
[1505] | 647 | |
---|
[1755] | 648 | activeStates_.erase((*rit)->getPriority()); |
---|
| 649 | _updateActiveStates(); |
---|
| 650 | } |
---|
| 651 | stateLeaveRequests_.clear(); |
---|
[1505] | 652 | |
---|
[1755] | 653 | // check for states to enter |
---|
| 654 | for (std::set<InputState*>::reverse_iterator rit = stateEnterRequests_.rbegin(); |
---|
| 655 | rit != stateEnterRequests_.rend(); ++rit) |
---|
| 656 | { |
---|
| 657 | // just to be sure that the state actually is registered |
---|
| 658 | assert(inputStatesByName_.find((*rit)->getName()) != inputStatesByName_.end()); |
---|
[1219] | 659 | |
---|
[1755] | 660 | activeStates_[(*rit)->getPriority()] = (*rit); |
---|
| 661 | _updateActiveStates(); |
---|
| 662 | (*rit)->onEnter(); |
---|
| 663 | } |
---|
| 664 | stateEnterRequests_.clear(); |
---|
[1219] | 665 | |
---|
[1755] | 666 | // check for states to destroy |
---|
| 667 | for (std::set<InputState*>::reverse_iterator rit = stateDestroyRequests_.rbegin(); |
---|
| 668 | rit != stateDestroyRequests_.rend(); ++rit) |
---|
| 669 | { |
---|
| 670 | _destroyState((*rit)); |
---|
| 671 | } |
---|
| 672 | stateDestroyRequests_.clear(); |
---|
[1219] | 673 | |
---|
[1755] | 674 | // mark that we capture and distribute input |
---|
| 675 | internalState_ |= Ticking; |
---|
[1293] | 676 | |
---|
[1755] | 677 | // Capture all the input. This calls the event handlers in InputManager. |
---|
| 678 | if (keyboard_) |
---|
| 679 | keyboard_->capture(); |
---|
| 680 | if (mouse_) |
---|
| 681 | mouse_->capture(); |
---|
| 682 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
| 683 | joySticks_[i]->capture(); |
---|
[1219] | 684 | |
---|
[1755] | 685 | if (!bCalibrating_) |
---|
| 686 | { |
---|
| 687 | // call all the handlers for the held key events |
---|
| 688 | for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++) |
---|
| 689 | activeStatesTop_[Keyboard]->keyHeld(KeyEvent(keysDown_[iKey], keyboardModifiers_)); |
---|
[1219] | 690 | |
---|
[1755] | 691 | // call all the handlers for the held mouse button events |
---|
| 692 | for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++) |
---|
| 693 | activeStatesTop_[Mouse]->mouseButtonHeld(mouseButtonsDown_[iButton]); |
---|
[1219] | 694 | |
---|
[1755] | 695 | // call all the handlers for the held joy stick button events |
---|
| 696 | for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) |
---|
| 697 | for (unsigned int iButton = 0; iButton < joyStickButtonsDown_[iJoyStick].size(); iButton++) |
---|
| 698 | activeStatesTop_[JoyStick0 + iJoyStick] |
---|
| 699 | ->joyStickButtonHeld(iJoyStick, joyStickButtonsDown_[iJoyStick][iButton]); |
---|
[1293] | 700 | |
---|
[1755] | 701 | // tick the handlers for each active handler |
---|
| 702 | for (unsigned int i = 0; i < devicesNum_; ++i) |
---|
| 703 | activeStatesTop_[i]->tickInput(dt, i); |
---|
[1219] | 704 | |
---|
[1755] | 705 | // tick the handler with a general tick afterwards |
---|
| 706 | for (unsigned int i = 0; i < activeStatesTicked_.size(); ++i) |
---|
| 707 | activeStatesTicked_[i]->tickInput(dt); |
---|
| 708 | } |
---|
[1219] | 709 | |
---|
[1755] | 710 | internalState_ &= ~Ticking; |
---|
[1293] | 711 | } |
---|
[1219] | 712 | |
---|
[1755] | 713 | /** |
---|
| 714 | @brief |
---|
| 715 | Updates the currently active states (according to activeStates_) for each device. |
---|
| 716 | Also, a list of all active states (no duplicates!) is compiled for the general tick. |
---|
| 717 | */ |
---|
| 718 | void InputManager::_updateActiveStates() |
---|
[1293] | 719 | { |
---|
[1755] | 720 | for (std::map<int, InputState*>::const_iterator it = activeStates_.begin(); it != activeStates_.end(); ++it) |
---|
| 721 | for (unsigned int i = 0; i < devicesNum_; ++i) |
---|
| 722 | if (it->second->isInputDeviceEnabled(i)) |
---|
| 723 | activeStatesTop_[i] = it->second; |
---|
[1293] | 724 | |
---|
[1755] | 725 | // update tickables (every state will only appear once) |
---|
| 726 | // Using a std::set to avoid duplicates |
---|
| 727 | std::set<InputState*> tempSet; |
---|
| 728 | for (unsigned int i = 0; i < devicesNum_; ++i) |
---|
| 729 | tempSet.insert(activeStatesTop_[i]); |
---|
[1219] | 730 | |
---|
[1755] | 731 | // copy the content of the set back to the actual vector |
---|
| 732 | activeStatesTicked_.clear(); |
---|
| 733 | for (std::set<InputState*>::const_iterator it = tempSet.begin();it != tempSet.end(); ++it) |
---|
| 734 | activeStatesTicked_.push_back(*it); |
---|
[1219] | 735 | |
---|
[1755] | 736 | this->mouseButtonsDown_.clear(); |
---|
| 737 | } |
---|
[1219] | 738 | |
---|
[1755] | 739 | /** |
---|
| 740 | @brief |
---|
| 741 | Processes the accumultated data for the joy stick calibration. |
---|
| 742 | */ |
---|
| 743 | void InputManager::_completeCalibration() |
---|
[1219] | 744 | { |
---|
[1755] | 745 | for (unsigned int i = 0; i < 24; i++) |
---|
| 746 | { |
---|
| 747 | // positive coefficient |
---|
| 748 | if (marginalsMax_[i] == INT_MIN) |
---|
| 749 | marginalsMax_[i] = 32767; |
---|
| 750 | // coefficients |
---|
| 751 | if (marginalsMax_[i] - joySticksCalibration_[0].zeroStates[i]) |
---|
| 752 | { |
---|
| 753 | joySticksCalibration_[0].positiveCoeff[i] |
---|
| 754 | = 1.0f/(marginalsMax_[i] - joySticksCalibration_[0].zeroStates[i]); |
---|
| 755 | } |
---|
| 756 | else |
---|
| 757 | joySticksCalibration_[0].positiveCoeff[i] = 1.0f; |
---|
[1022] | 758 | |
---|
[1755] | 759 | // config value |
---|
| 760 | ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer("CoeffPos"); |
---|
| 761 | assert(cont); |
---|
| 762 | cont->set(i, joySticksCalibration_[0].positiveCoeff[i]); |
---|
[918] | 763 | |
---|
[1755] | 764 | // negative coefficient |
---|
| 765 | if (marginalsMin_[i] == INT_MAX) |
---|
| 766 | marginalsMin_[i] = -32768; |
---|
| 767 | // coefficients |
---|
| 768 | if (marginalsMin_[i] - joySticksCalibration_[0].zeroStates[i]) |
---|
| 769 | { |
---|
| 770 | joySticksCalibration_[0].negativeCoeff[i] = -1.0f |
---|
| 771 | / (marginalsMin_[i] - joySticksCalibration_[0].zeroStates[i]); |
---|
| 772 | } |
---|
| 773 | else |
---|
| 774 | joySticksCalibration_[0].negativeCoeff[i] = 1.0f; |
---|
| 775 | // config value |
---|
| 776 | cont = getIdentifier()->getConfigValueContainer("CoeffNeg"); |
---|
| 777 | assert(cont); |
---|
| 778 | cont->set(i, joySticksCalibration_[0].negativeCoeff[i]); |
---|
[918] | 779 | |
---|
[1755] | 780 | // zero states |
---|
| 781 | if (i < 8) |
---|
| 782 | { |
---|
| 783 | if (!(i & 1)) |
---|
| 784 | joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mSliders[i/2].abX; |
---|
| 785 | else |
---|
| 786 | joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mSliders[i/2].abY; |
---|
| 787 | } |
---|
| 788 | else |
---|
| 789 | { |
---|
| 790 | if (i - 8 < joySticks_[0]->getJoyStickState().mAxes.size()) |
---|
| 791 | joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mAxes[i - 8].abs; |
---|
| 792 | else |
---|
| 793 | joySticksCalibration_[0].zeroStates[i] = 0; |
---|
| 794 | } |
---|
| 795 | // config value |
---|
| 796 | cont = getIdentifier()->getConfigValueContainer("Zero"); |
---|
| 797 | assert(cont); |
---|
| 798 | cont->set(i, joySticksCalibration_[0].zeroStates[i]); |
---|
| 799 | } |
---|
[1219] | 800 | |
---|
[1755] | 801 | // restore old input state |
---|
| 802 | requestLeaveState("calibrator"); |
---|
| 803 | bCalibrating_ = false; |
---|
[1502] | 804 | } |
---|
| 805 | |
---|
| 806 | |
---|
[1755] | 807 | // ############################################################ |
---|
| 808 | // ##### OIS events ##### |
---|
| 809 | // ########## ########## |
---|
| 810 | // ############################################################ |
---|
[1219] | 811 | |
---|
[1755] | 812 | // ###### Key Events ###### |
---|
[1219] | 813 | |
---|
[1755] | 814 | /** |
---|
| 815 | @brief |
---|
| 816 | Event handler for the keyPressed Event. |
---|
| 817 | @param e |
---|
| 818 | Event information |
---|
| 819 | */ |
---|
| 820 | bool InputManager::keyPressed(const OIS::KeyEvent &e) |
---|
[1219] | 821 | { |
---|
[1755] | 822 | // check whether the key already is in the list (can happen when focus was lost) |
---|
| 823 | unsigned int iKey = 0; |
---|
| 824 | while (iKey < keysDown_.size() && keysDown_[iKey].key != (KeyCode::Enum)e.key) |
---|
| 825 | iKey++; |
---|
| 826 | if (iKey == keysDown_.size()) |
---|
| 827 | keysDown_.push_back(Key(e)); |
---|
| 828 | else |
---|
| 829 | return true; |
---|
[1219] | 830 | |
---|
[1755] | 831 | // update modifiers |
---|
| 832 | if(e.key == OIS::KC_RMENU || e.key == OIS::KC_LMENU) |
---|
| 833 | keyboardModifiers_ |= KeyboardModifier::Alt; // alt key |
---|
| 834 | if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL) |
---|
| 835 | keyboardModifiers_ |= KeyboardModifier::Ctrl; // ctrl key |
---|
| 836 | if(e.key == OIS::KC_RSHIFT || e.key == OIS::KC_LSHIFT) |
---|
| 837 | keyboardModifiers_ |= KeyboardModifier::Shift; // shift key |
---|
[1219] | 838 | |
---|
[1755] | 839 | activeStatesTop_[Keyboard]->keyPressed(KeyEvent(e, keyboardModifiers_)); |
---|
[1219] | 840 | |
---|
[1755] | 841 | return true; |
---|
[1349] | 842 | } |
---|
[1755] | 843 | |
---|
| 844 | /** |
---|
| 845 | @brief |
---|
| 846 | Event handler for the keyReleased Event. |
---|
| 847 | @param e |
---|
| 848 | Event information |
---|
| 849 | */ |
---|
| 850 | bool InputManager::keyReleased(const OIS::KeyEvent &e) |
---|
[1349] | 851 | { |
---|
[1755] | 852 | // remove the key from the keysDown_ list |
---|
| 853 | for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++) |
---|
| 854 | { |
---|
| 855 | if (keysDown_[iKey].key == (KeyCode::Enum)e.key) |
---|
| 856 | { |
---|
| 857 | keysDown_.erase(keysDown_.begin() + iKey); |
---|
| 858 | break; |
---|
| 859 | } |
---|
| 860 | } |
---|
[1502] | 861 | |
---|
[1755] | 862 | // update modifiers |
---|
| 863 | if(e.key == OIS::KC_RMENU || e.key == OIS::KC_LMENU) |
---|
| 864 | keyboardModifiers_ &= ~KeyboardModifier::Alt; // alt key |
---|
| 865 | if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL) |
---|
| 866 | keyboardModifiers_ &= ~KeyboardModifier::Ctrl; // ctrl key |
---|
| 867 | if(e.key == OIS::KC_RSHIFT || e.key == OIS::KC_LSHIFT) |
---|
| 868 | keyboardModifiers_ &= ~KeyboardModifier::Shift; // shift key |
---|
[1293] | 869 | |
---|
[1755] | 870 | activeStatesTop_[Keyboard]->keyReleased(KeyEvent(e, keyboardModifiers_)); |
---|
[1502] | 871 | |
---|
[1755] | 872 | return true; |
---|
| 873 | } |
---|
[1502] | 874 | |
---|
| 875 | |
---|
[1755] | 876 | // ###### Mouse Events ###### |
---|
[1502] | 877 | |
---|
[1755] | 878 | /** |
---|
| 879 | @brief |
---|
| 880 | Event handler for the mouseMoved Event. |
---|
| 881 | @param e |
---|
| 882 | Event information |
---|
| 883 | */ |
---|
| 884 | bool InputManager::mouseMoved(const OIS::MouseEvent &e) |
---|
| 885 | { |
---|
| 886 | // check for actual moved event |
---|
| 887 | if (e.state.X.rel != 0 || e.state.Y.rel != 0) |
---|
| 888 | { |
---|
| 889 | activeStatesTop_[Mouse]->mouseMoved(IntVector2(e.state.X.abs, e.state.Y.abs), |
---|
| 890 | IntVector2(e.state.X.rel, e.state.Y.rel), IntVector2(e.state.width, e.state.height)); |
---|
| 891 | } |
---|
[1502] | 892 | |
---|
[1755] | 893 | // check for mouse scrolled event |
---|
| 894 | if (e.state.Z.rel != 0) |
---|
| 895 | { |
---|
| 896 | activeStatesTop_[Mouse]->mouseScrolled(e.state.Z.abs, e.state.Z.rel); |
---|
| 897 | } |
---|
[1502] | 898 | |
---|
[1755] | 899 | return true; |
---|
| 900 | } |
---|
[1502] | 901 | |
---|
[1755] | 902 | /** |
---|
| 903 | @brief |
---|
| 904 | Event handler for the mousePressed Event. |
---|
| 905 | @param e |
---|
| 906 | Event information |
---|
| 907 | @param id |
---|
| 908 | The ID of the mouse button |
---|
| 909 | */ |
---|
| 910 | bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
| 911 | { |
---|
| 912 | // check whether the button already is in the list (can happen when focus was lost) |
---|
| 913 | unsigned int iButton = 0; |
---|
| 914 | while (iButton < mouseButtonsDown_.size() && mouseButtonsDown_[iButton] != (MouseButton::Enum)id) |
---|
| 915 | iButton++; |
---|
| 916 | if (iButton == mouseButtonsDown_.size()) |
---|
| 917 | mouseButtonsDown_.push_back((MouseButton::Enum)id); |
---|
[1502] | 918 | |
---|
[1755] | 919 | activeStatesTop_[Mouse]->mouseButtonPressed((MouseButton::Enum)id); |
---|
[1502] | 920 | |
---|
[1755] | 921 | return true; |
---|
| 922 | } |
---|
[1502] | 923 | |
---|
[1755] | 924 | /** |
---|
| 925 | @brief |
---|
| 926 | Event handler for the mouseReleased Event. |
---|
| 927 | @param e |
---|
| 928 | Event information |
---|
| 929 | @param id |
---|
| 930 | The ID of the mouse button |
---|
| 931 | */ |
---|
| 932 | bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
| 933 | { |
---|
| 934 | // remove the button from the keysDown_ list |
---|
| 935 | for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++) |
---|
| 936 | { |
---|
| 937 | if (mouseButtonsDown_[iButton] == (MouseButton::Enum)id) |
---|
| 938 | { |
---|
| 939 | mouseButtonsDown_.erase(mouseButtonsDown_.begin() + iButton); |
---|
| 940 | break; |
---|
| 941 | } |
---|
| 942 | } |
---|
[1502] | 943 | |
---|
[1755] | 944 | activeStatesTop_[Mouse]->mouseButtonReleased((MouseButton::Enum)id); |
---|
[1219] | 945 | |
---|
[1755] | 946 | return true; |
---|
| 947 | } |
---|
[1293] | 948 | |
---|
[1502] | 949 | |
---|
[1755] | 950 | // ###### Joy Stick Events ###### |
---|
[1219] | 951 | |
---|
[1755] | 952 | /** |
---|
| 953 | @brief |
---|
| 954 | Returns the joy stick ID (orxonox) according to a OIS::JoyStickEvent |
---|
| 955 | */ |
---|
| 956 | inline unsigned int InputManager::_getJoystick(const OIS::JoyStickEvent& arg) |
---|
| 957 | { |
---|
| 958 | // use the device to identify which one called the method |
---|
| 959 | OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device; |
---|
| 960 | unsigned int iJoyStick = 0; |
---|
| 961 | while (joySticks_[iJoyStick] != joyStick) |
---|
| 962 | iJoyStick++; |
---|
| 963 | // assert: Unknown joystick fired an event. |
---|
| 964 | assert(iJoyStick != joySticksSize_); |
---|
| 965 | return iJoyStick; |
---|
| 966 | } |
---|
[1219] | 967 | |
---|
[1755] | 968 | bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button) |
---|
| 969 | { |
---|
| 970 | unsigned int iJoyStick = _getJoystick(arg); |
---|
[1502] | 971 | |
---|
[1755] | 972 | // check whether the button already is in the list (can happen when focus was lost) |
---|
| 973 | std::vector<JoyStickButton::Enum>& buttonsDown = joyStickButtonsDown_[iJoyStick]; |
---|
| 974 | unsigned int iButton = 0; |
---|
| 975 | while (iButton < buttonsDown.size() && buttonsDown[iButton] != button) |
---|
| 976 | iButton++; |
---|
| 977 | if (iButton == buttonsDown.size()) |
---|
| 978 | buttonsDown.push_back((JoyStickButton::Enum)button); |
---|
[1219] | 979 | |
---|
[1755] | 980 | activeStatesTop_[2 + iJoyStick]->joyStickButtonPressed(iJoyStick, (JoyStickButton::Enum)button); |
---|
[1502] | 981 | |
---|
[1755] | 982 | return true; |
---|
| 983 | } |
---|
[1219] | 984 | |
---|
[1755] | 985 | bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button) |
---|
| 986 | { |
---|
| 987 | unsigned int iJoyStick = _getJoystick(arg); |
---|
[1219] | 988 | |
---|
[1755] | 989 | // remove the button from the joyStickButtonsDown_ list |
---|
| 990 | std::vector<JoyStickButton::Enum>& buttonsDown = joyStickButtonsDown_[iJoyStick]; |
---|
| 991 | for (unsigned int iButton = 0; iButton < buttonsDown.size(); iButton++) |
---|
| 992 | { |
---|
| 993 | if (buttonsDown[iButton] == button) |
---|
| 994 | { |
---|
| 995 | buttonsDown.erase(buttonsDown.begin() + iButton); |
---|
| 996 | break; |
---|
| 997 | } |
---|
| 998 | } |
---|
[1219] | 999 | |
---|
[1755] | 1000 | activeStatesTop_[2 + iJoyStick]->joyStickButtonReleased(iJoyStick, (JoyStickButton::Enum)button); |
---|
[1219] | 1001 | |
---|
[1755] | 1002 | return true; |
---|
| 1003 | } |
---|
[1219] | 1004 | |
---|
[1755] | 1005 | /** |
---|
| 1006 | @brief |
---|
| 1007 | Calls the states for a particular axis with our enumeration. |
---|
| 1008 | Used by OIS sliders and OIS axes. |
---|
| 1009 | */ |
---|
| 1010 | void InputManager::_fireAxis(unsigned int iJoyStick, int axis, int value) |
---|
| 1011 | { |
---|
| 1012 | if (bCalibrating_) |
---|
| 1013 | { |
---|
| 1014 | if (value > marginalsMax_[axis]) |
---|
| 1015 | marginalsMax_[axis] = value; |
---|
| 1016 | if (value < marginalsMin_[axis]) |
---|
| 1017 | marginalsMin_[axis] = value; |
---|
| 1018 | } |
---|
| 1019 | else |
---|
| 1020 | { |
---|
| 1021 | float fValue = value - joySticksCalibration_[iJoyStick].zeroStates[axis]; |
---|
| 1022 | if (fValue > 0.0f) |
---|
| 1023 | fValue *= joySticksCalibration_[iJoyStick].positiveCoeff[axis]; |
---|
| 1024 | else |
---|
| 1025 | fValue *= joySticksCalibration_[iJoyStick].negativeCoeff[axis]; |
---|
[1219] | 1026 | |
---|
[1755] | 1027 | activeStatesTop_[2 + iJoyStick]->joyStickAxisMoved(iJoyStick, axis, fValue); |
---|
| 1028 | } |
---|
| 1029 | } |
---|
[1219] | 1030 | |
---|
[1755] | 1031 | bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis) |
---|
| 1032 | { |
---|
| 1033 | unsigned int iJoyStick = _getJoystick(arg); |
---|
[1219] | 1034 | |
---|
[1755] | 1035 | // keep in mind that the first 8 axes are reserved for the sliders |
---|
| 1036 | _fireAxis(iJoyStick, axis + 8, arg.state.mAxes[axis].abs); |
---|
[1293] | 1037 | |
---|
[1755] | 1038 | return true; |
---|
| 1039 | } |
---|
[1293] | 1040 | |
---|
[1755] | 1041 | bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id) |
---|
| 1042 | { |
---|
| 1043 | unsigned int iJoyStick = _getJoystick(arg); |
---|
[1293] | 1044 | |
---|
[1755] | 1045 | if (sliderStates_[iJoyStick].sliderStates[id].x != arg.state.mSliders[id].abX) |
---|
| 1046 | _fireAxis(iJoyStick, id * 2, arg.state.mSliders[id].abX); |
---|
| 1047 | else if (sliderStates_[iJoyStick].sliderStates[id].y != arg.state.mSliders[id].abY) |
---|
| 1048 | _fireAxis(iJoyStick, id * 2 + 1, arg.state.mSliders[id].abY); |
---|
[1219] | 1049 | |
---|
[1755] | 1050 | return true; |
---|
| 1051 | } |
---|
[1219] | 1052 | |
---|
[1755] | 1053 | bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id) |
---|
[1219] | 1054 | { |
---|
[1755] | 1055 | unsigned int iJoyStick = _getJoystick(arg); |
---|
[918] | 1056 | |
---|
[1755] | 1057 | // translate the POV into 8 simple buttons |
---|
[922] | 1058 | |
---|
[1755] | 1059 | int lastState = povStates_[iJoyStick][id]; |
---|
| 1060 | if (lastState & OIS::Pov::North) |
---|
| 1061 | buttonReleased(arg, 32 + id * 4 + 0); |
---|
| 1062 | if (lastState & OIS::Pov::South) |
---|
| 1063 | buttonReleased(arg, 32 + id * 4 + 1); |
---|
| 1064 | if (lastState & OIS::Pov::East) |
---|
| 1065 | buttonReleased(arg, 32 + id * 4 + 2); |
---|
| 1066 | if (lastState & OIS::Pov::West) |
---|
| 1067 | buttonReleased(arg, 32 + id * 4 + 3); |
---|
[918] | 1068 | |
---|
[1755] | 1069 | povStates_[iJoyStick].povStates[id] = arg.state.mPOV[id].direction; |
---|
[1219] | 1070 | |
---|
[1755] | 1071 | int currentState = povStates_[iJoyStick][id]; |
---|
| 1072 | if (currentState & OIS::Pov::North) |
---|
| 1073 | buttonPressed(arg, 32 + id * 4 + 0); |
---|
| 1074 | if (currentState & OIS::Pov::South) |
---|
| 1075 | buttonPressed(arg, 32 + id * 4 + 1); |
---|
| 1076 | if (currentState & OIS::Pov::East) |
---|
| 1077 | buttonPressed(arg, 32 + id * 4 + 2); |
---|
| 1078 | if (currentState & OIS::Pov::West) |
---|
| 1079 | buttonPressed(arg, 32 + id * 4 + 3); |
---|
[1502] | 1080 | |
---|
[1755] | 1081 | return true; |
---|
[1219] | 1082 | } |
---|
[1066] | 1083 | |
---|
| 1084 | |
---|
[1755] | 1085 | // ############################################################ |
---|
| 1086 | // ##### Other Public Interface Methods ##### |
---|
| 1087 | // ########## ########## |
---|
| 1088 | // ############################################################ |
---|
[1219] | 1089 | |
---|
[1755] | 1090 | /** |
---|
| 1091 | @brief |
---|
| 1092 | Adjusts the mouse window metrics. |
---|
| 1093 | This method has to be called every time the size of the window changes. |
---|
| 1094 | @param width |
---|
| 1095 | The new width of the render window |
---|
| 1096 | @param^height |
---|
| 1097 | The new height of the render window |
---|
| 1098 | */ |
---|
| 1099 | void InputManager::setWindowExtents(const int width, const int height) |
---|
[1219] | 1100 | { |
---|
[1755] | 1101 | if (mouse_) |
---|
| 1102 | { |
---|
| 1103 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
---|
| 1104 | mouse_->getMouseState().width = width; |
---|
| 1105 | mouse_->getMouseState().height = height; |
---|
| 1106 | } |
---|
[1219] | 1107 | } |
---|
| 1108 | |
---|
| 1109 | |
---|
[1755] | 1110 | // ###### InputStates ###### |
---|
[1219] | 1111 | |
---|
[1755] | 1112 | /** |
---|
| 1113 | @brief |
---|
| 1114 | Adds a new key handler. |
---|
| 1115 | @param handler |
---|
| 1116 | Pointer to the handler object. |
---|
| 1117 | @param name |
---|
| 1118 | Unique name of the handler. |
---|
| 1119 | @param priority |
---|
| 1120 | Unique integer number. Higher means more prioritised. |
---|
| 1121 | @return |
---|
| 1122 | True if added, false if name or priority already existed. |
---|
| 1123 | */ |
---|
| 1124 | bool InputManager::_configureInputState(InputState* state, const std::string& name, int priority) |
---|
[1219] | 1125 | { |
---|
[1755] | 1126 | if (name == "") |
---|
| 1127 | return false; |
---|
| 1128 | if (!state) |
---|
| 1129 | return false; |
---|
| 1130 | if (inputStatesByName_.find(name) == inputStatesByName_.end()) |
---|
| 1131 | { |
---|
| 1132 | if (inputStatesByPriority_.find(priority) |
---|
| 1133 | == inputStatesByPriority_.end()) |
---|
| 1134 | { |
---|
| 1135 | inputStatesByName_[name] = state; |
---|
| 1136 | inputStatesByPriority_[priority] = state; |
---|
| 1137 | state->setNumOfJoySticks(numberOfJoySticks()); |
---|
| 1138 | state->setName(name); |
---|
| 1139 | state->setPriority(priority); |
---|
| 1140 | return true; |
---|
| 1141 | } |
---|
| 1142 | else |
---|
| 1143 | { |
---|
| 1144 | COUT(2) << "Warning: Could not add an InputState with the same priority '" |
---|
| 1145 | << priority << "'." << std::endl; |
---|
| 1146 | return false; |
---|
| 1147 | } |
---|
| 1148 | } |
---|
| 1149 | else |
---|
| 1150 | { |
---|
| 1151 | COUT(2) << "Warning: Could not add an InputState with the same name '" << name << "'." << std::endl; |
---|
| 1152 | return false; |
---|
| 1153 | } |
---|
[1219] | 1154 | } |
---|
| 1155 | |
---|
[1755] | 1156 | /** |
---|
| 1157 | @brief |
---|
| 1158 | Removes and destroys an input state internally. |
---|
| 1159 | @param name |
---|
| 1160 | Name of the handler. |
---|
| 1161 | @return |
---|
| 1162 | True if removal was successful, false if name was not found. |
---|
| 1163 | @remarks |
---|
| 1164 | You can't remove the internal states "empty", "calibrator" and "detector". |
---|
| 1165 | The removal process is being postponed if InputManager::tick() is currently running. |
---|
| 1166 | */ |
---|
| 1167 | bool InputManager::requestDestroyState(const std::string& name) |
---|
[1219] | 1168 | { |
---|
[1755] | 1169 | if (name == "empty" || name == "calibrator" || name == "detector") |
---|
| 1170 | { |
---|
| 1171 | COUT(2) << "InputManager: Removing the '" << name << "' state is not allowed!" << std::endl; |
---|
| 1172 | return false; |
---|
| 1173 | } |
---|
| 1174 | std::map<std::string, InputState*>::iterator it = inputStatesByName_.find(name); |
---|
| 1175 | if (it != inputStatesByName_.end()) |
---|
| 1176 | { |
---|
| 1177 | if (activeStates_.find(it->second->getPriority()) != activeStates_.end()) |
---|
| 1178 | { |
---|
| 1179 | // The state is still active. We have to postpone |
---|
| 1180 | stateLeaveRequests_.insert(it->second); |
---|
| 1181 | stateDestroyRequests_.insert(it->second); |
---|
| 1182 | } |
---|
| 1183 | else if (this->internalState_ & Ticking) |
---|
| 1184 | { |
---|
| 1185 | // cannot remove state while ticking |
---|
| 1186 | stateDestroyRequests_.insert(it->second); |
---|
| 1187 | } |
---|
| 1188 | else |
---|
| 1189 | _destroyState(it->second); |
---|
[1219] | 1190 | |
---|
[1755] | 1191 | return true; |
---|
| 1192 | } |
---|
| 1193 | return false; |
---|
[1219] | 1194 | } |
---|
| 1195 | |
---|
[1755] | 1196 | /** |
---|
| 1197 | @brief |
---|
| 1198 | Returns the pointer to the requested InputState. |
---|
| 1199 | @param name |
---|
| 1200 | Unique name of the state. |
---|
| 1201 | @return |
---|
| 1202 | Pointer to the instance, 0 if name was not found. |
---|
| 1203 | */ |
---|
| 1204 | InputState* InputManager::getState(const std::string& name) |
---|
[1219] | 1205 | { |
---|
[1755] | 1206 | std::map<std::string, InputState*>::iterator it = inputStatesByName_.find(name); |
---|
| 1207 | if (it != inputStatesByName_.end()) |
---|
| 1208 | return it->second; |
---|
| 1209 | else |
---|
| 1210 | return 0; |
---|
[1219] | 1211 | } |
---|
| 1212 | |
---|
[1755] | 1213 | /** |
---|
| 1214 | @brief |
---|
| 1215 | Returns the current input state (there might be others active too!) |
---|
| 1216 | @return |
---|
| 1217 | The current highest prioritised active input state. |
---|
| 1218 | */ |
---|
| 1219 | InputState* InputManager::getCurrentState() |
---|
[1219] | 1220 | { |
---|
[1755] | 1221 | return (*activeStates_.rbegin()).second; |
---|
[1219] | 1222 | } |
---|
| 1223 | |
---|
[1755] | 1224 | /** |
---|
| 1225 | @brief |
---|
| 1226 | Activates a specific input state. |
---|
| 1227 | It might not be really activated if the priority is too low! |
---|
| 1228 | @param name |
---|
| 1229 | Unique name of the state. |
---|
| 1230 | @return |
---|
| 1231 | False if name was not found, true otherwise. |
---|
| 1232 | */ |
---|
| 1233 | bool InputManager::requestEnterState(const std::string& name) |
---|
[1219] | 1234 | { |
---|
[1755] | 1235 | // get pointer from the map with all stored handlers |
---|
| 1236 | std::map<std::string, InputState*>::const_iterator it = inputStatesByName_.find(name); |
---|
| 1237 | if (it != inputStatesByName_.end()) |
---|
| 1238 | { |
---|
| 1239 | // exists |
---|
| 1240 | if (activeStates_.find(it->second->getPriority()) == activeStates_.end()) |
---|
| 1241 | { |
---|
| 1242 | // not active |
---|
| 1243 | if (stateDestroyRequests_.find(it->second) == stateDestroyRequests_.end()) |
---|
| 1244 | { |
---|
| 1245 | // not scheduled for destruction |
---|
| 1246 | // set prevents a state being added multiple times |
---|
| 1247 | stateEnterRequests_.insert(it->second); |
---|
| 1248 | return true; |
---|
| 1249 | } |
---|
| 1250 | } |
---|
| 1251 | } |
---|
| 1252 | return false; |
---|
[1219] | 1253 | } |
---|
| 1254 | |
---|
[1755] | 1255 | /** |
---|
| 1256 | @brief |
---|
| 1257 | Deactivates a specific input state. |
---|
| 1258 | @param name |
---|
| 1259 | Unique name of the state. |
---|
| 1260 | @return |
---|
| 1261 | False if name was not found, true otherwise. |
---|
| 1262 | */ |
---|
| 1263 | bool InputManager::requestLeaveState(const std::string& name) |
---|
[1219] | 1264 | { |
---|
[1755] | 1265 | // get pointer from the map with all stored handlers |
---|
| 1266 | std::map<std::string, InputState*>::const_iterator it = inputStatesByName_.find(name); |
---|
| 1267 | if (it != inputStatesByName_.end()) |
---|
| 1268 | { |
---|
| 1269 | // exists |
---|
| 1270 | if (activeStates_.find(it->second->getPriority()) != activeStates_.end()) |
---|
| 1271 | { |
---|
| 1272 | // active |
---|
| 1273 | stateLeaveRequests_.insert(it->second); |
---|
| 1274 | return true; |
---|
| 1275 | } |
---|
| 1276 | } |
---|
| 1277 | return false; |
---|
[1219] | 1278 | } |
---|
| 1279 | |
---|
| 1280 | |
---|
[1755] | 1281 | // ############################################################ |
---|
| 1282 | // ##### Console Commands ##### |
---|
| 1283 | // ########## ########## |
---|
| 1284 | // ############################################################ |
---|
[1219] | 1285 | |
---|
[1755] | 1286 | /** |
---|
| 1287 | @brief |
---|
| 1288 | Method for easily storing a string with the command executor. It is used by the |
---|
| 1289 | KeyDetector to get assign commands. The KeyDetector simply executes |
---|
| 1290 | the command 'storeKeyStroke myName' for each button/axis. |
---|
| 1291 | @remarks |
---|
| 1292 | This is only a temporary hack until we thourouhgly support multiple KeyBinders. |
---|
| 1293 | @param name |
---|
| 1294 | The name of the button/axis. |
---|
| 1295 | */ |
---|
| 1296 | void InputManager::storeKeyStroke(const std::string& name) |
---|
[1219] | 1297 | { |
---|
[1755] | 1298 | getInstance().requestLeaveState("detector"); |
---|
| 1299 | COUT(0) << "Binding string \"" << bindingCommmandString_s << "\" on key '" << name << "'" << std::endl; |
---|
| 1300 | CommandExecutor::execute("config KeyBinder " + name + " " + bindingCommmandString_s, false); |
---|
[1219] | 1301 | } |
---|
| 1302 | |
---|
[1755] | 1303 | /** |
---|
| 1304 | @brief |
---|
| 1305 | Assigns a command string to a key/button/axis. The name is determined via KeyDetector |
---|
| 1306 | and InputManager::storeKeyStroke(.). |
---|
| 1307 | @param command |
---|
| 1308 | Command string that can be executed by the CommandExecutor |
---|
| 1309 | */ |
---|
| 1310 | void InputManager::keyBind(const std::string& command) |
---|
[1219] | 1311 | { |
---|
[1755] | 1312 | bindingCommmandString_s = command; |
---|
| 1313 | getInstance().requestEnterState("detector"); |
---|
| 1314 | COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl; |
---|
[1219] | 1315 | } |
---|
| 1316 | |
---|
[1755] | 1317 | /** |
---|
| 1318 | @brief |
---|
| 1319 | Starts joy stick calibration. |
---|
| 1320 | */ |
---|
| 1321 | void InputManager::calibrate() |
---|
[1219] | 1322 | { |
---|
[1755] | 1323 | getInstance().bCalibrating_ = true; |
---|
| 1324 | getInstance().requestEnterState("calibrator"); |
---|
[1219] | 1325 | } |
---|
| 1326 | |
---|
[1755] | 1327 | /** |
---|
| 1328 | @brief |
---|
| 1329 | Reloads the input system |
---|
| 1330 | */ |
---|
| 1331 | void InputManager::reload(bool joyStickSupport) |
---|
[1219] | 1332 | { |
---|
[1755] | 1333 | getInstance().reloadInputSystem(joyStickSupport); |
---|
[1219] | 1334 | } |
---|
[918] | 1335 | } |
---|