[918] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1056] | 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 | /** |
---|
[1193] | 30 | @file |
---|
| 31 | @brief Implementation of the InputManager that captures all the input from OIS |
---|
| 32 | and redirects it to listeners if necessary. |
---|
[918] | 33 | */ |
---|
| 34 | |
---|
[1062] | 35 | #include "InputManager.h" |
---|
| 36 | #include "CoreIncludes.h" |
---|
| 37 | #include "Debug.h" |
---|
[1066] | 38 | #include "InputBuffer.h" |
---|
[1084] | 39 | #include "ConsoleCommand.h" |
---|
[1182] | 40 | #include "util/Convert.h" |
---|
[918] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[1193] | 44 | // ############################### |
---|
| 45 | // ### Internal Methods ### |
---|
| 46 | // ############################### |
---|
[1203] | 47 | // ############################### |
---|
[1193] | 48 | |
---|
[919] | 49 | /** |
---|
[1193] | 50 | @brief Constructor only sets member fields to initial zero values |
---|
| 51 | and registers the class in the class hierarchy. |
---|
[919] | 52 | */ |
---|
[973] | 53 | InputManager::InputManager() : |
---|
[1034] | 54 | inputSystem_(0), keyboard_(0), mouse_(0), |
---|
[1182] | 55 | state_(IS_UNINIT), stateRequest_(IS_UNINIT) |
---|
[918] | 56 | { |
---|
[1089] | 57 | RegisterObject(InputManager); |
---|
[918] | 58 | } |
---|
| 59 | |
---|
[919] | 60 | /** |
---|
[1182] | 61 | @brief The one instance of the InputManager is stored in this function. |
---|
| 62 | @return A reference to the only instance of the InputManager |
---|
[929] | 63 | */ |
---|
[1182] | 64 | InputManager& InputManager::_getSingleton() |
---|
[929] | 65 | { |
---|
[1182] | 66 | static InputManager theOnlyInstance; |
---|
| 67 | return theOnlyInstance; |
---|
[929] | 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
[1193] | 71 | @brief Destructor only called at the end of the program, after main. |
---|
[919] | 72 | */ |
---|
[1182] | 73 | InputManager::~InputManager() |
---|
[919] | 74 | { |
---|
[1203] | 75 | _destroy(); |
---|
[919] | 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
[1193] | 79 | @brief Creates the OIS::InputMananger, the keyboard, the mouse and |
---|
| 80 | the joysticks and assigns the key bindings. |
---|
[919] | 81 | @param windowHnd The window handle of the render window |
---|
| 82 | @param windowWidth The width of the render window |
---|
| 83 | @param windowHeight The height of the render window |
---|
| 84 | */ |
---|
[1193] | 85 | bool InputManager::_initialise(size_t windowHnd, int windowWidth, int windowHeight, |
---|
| 86 | bool createKeyboard, bool createMouse, bool createJoySticks) |
---|
[919] | 87 | { |
---|
[1193] | 88 | if (state_ == IS_UNINIT) |
---|
[918] | 89 | { |
---|
[1193] | 90 | CCOUT(ORX_DEBUG) << "Initialising OIS components..." << std::endl; |
---|
| 91 | |
---|
[918] | 92 | OIS::ParamList paramList; |
---|
| 93 | std::ostringstream windowHndStr; |
---|
| 94 | |
---|
| 95 | // Fill parameter list |
---|
| 96 | windowHndStr << (unsigned int)windowHnd; |
---|
| 97 | paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
| 98 | |
---|
[1182] | 99 | //#if defined OIS_LINUX_PLATFORM |
---|
| 100 | // paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true"))); |
---|
| 101 | //#endif |
---|
[928] | 102 | |
---|
[934] | 103 | try |
---|
[918] | 104 | { |
---|
[934] | 105 | inputSystem_ = OIS::InputManager::createInputSystem(paramList); |
---|
[1193] | 106 | CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl; |
---|
| 107 | } |
---|
| 108 | catch (OIS::Exception ex) |
---|
| 109 | { |
---|
| 110 | CCOUT(ORX_ERROR) << "Error: Failed creating an OIS input system." |
---|
| 111 | << "OIS message: \"" << ex.eText << "\"" << std::endl; |
---|
| 112 | inputSystem_ = 0; |
---|
| 113 | return false; |
---|
| 114 | } |
---|
[918] | 115 | |
---|
[1193] | 116 | if (createKeyboard) |
---|
| 117 | _initialiseKeyboard(); |
---|
[918] | 118 | |
---|
[1193] | 119 | if (createMouse) |
---|
| 120 | _initialiseMouse(); |
---|
[1182] | 121 | |
---|
[1193] | 122 | if (createJoySticks) |
---|
| 123 | _initialiseJoySticks(); |
---|
[934] | 124 | |
---|
[1193] | 125 | // Set mouse/joystick region |
---|
| 126 | setWindowExtents(windowWidth, windowHeight); |
---|
[1182] | 127 | |
---|
[1203] | 128 | state_ = IS_NONE; |
---|
[1193] | 129 | CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl; |
---|
| 130 | } |
---|
| 131 | else |
---|
| 132 | { |
---|
| 133 | CCOUT(ORX_WARNING) << "Warning: OIS compoments already initialised, skipping" << std::endl; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | // InputManager holds the input buffer --> create one and add it. |
---|
| 137 | addKeyListener(new InputBuffer(), "buffer"); |
---|
| 138 | |
---|
[1203] | 139 | KeyBinder* binder = new KeyBinder(); |
---|
| 140 | binder->loadBindings(); |
---|
| 141 | addKeyListener(binder, "keybinder"); |
---|
| 142 | addMouseListener(binder, "keybinder"); |
---|
| 143 | |
---|
[1193] | 144 | // Read all the key bindings and assign them |
---|
[1203] | 145 | //if (!_loadBindings()) |
---|
| 146 | // return false; |
---|
[1193] | 147 | |
---|
| 148 | CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl; |
---|
| 149 | return true; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | /** |
---|
| 153 | @brief Creates a keyboard and sets the event handler. |
---|
[1203] | 154 | @return False if keyboard stays uninitialised, true otherwise. |
---|
[1193] | 155 | */ |
---|
[1203] | 156 | bool InputManager::_initialiseKeyboard() |
---|
[1193] | 157 | { |
---|
[1203] | 158 | if (keyboard_ != 0) |
---|
| 159 | { |
---|
| 160 | CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl; |
---|
| 161 | return true; |
---|
| 162 | } |
---|
[1193] | 163 | try |
---|
| 164 | { |
---|
[1195] | 165 | if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0) |
---|
[1193] | 166 | { |
---|
| 167 | keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true); |
---|
| 168 | // register our listener in OIS. |
---|
| 169 | keyboard_->setEventCallback(this); |
---|
| 170 | // note: OIS will not detect keys that have already been down when the keyboard was created. |
---|
| 171 | CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl; |
---|
[1203] | 172 | return true; |
---|
[918] | 173 | } |
---|
[1193] | 174 | else |
---|
[1203] | 175 | { |
---|
[1193] | 176 | CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl; |
---|
[1203] | 177 | return false; |
---|
| 178 | } |
---|
[1193] | 179 | } |
---|
| 180 | catch (OIS::Exception ex) |
---|
| 181 | { |
---|
[1203] | 182 | // TODO: Test this output regarding formatting |
---|
[1193] | 183 | CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n" |
---|
| 184 | << "OIS error message: \"" << ex.eText << "\"" << std::endl; |
---|
| 185 | keyboard_ = 0; |
---|
[1203] | 186 | return false; |
---|
[1193] | 187 | } |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /** |
---|
| 191 | @brief Creates a mouse and sets the event handler. |
---|
[1203] | 192 | @return False if mouse stays uninitialised, true otherwise. |
---|
[1193] | 193 | */ |
---|
[1203] | 194 | bool InputManager::_initialiseMouse() |
---|
[1193] | 195 | { |
---|
[1203] | 196 | if (mouse_ != 0) |
---|
| 197 | { |
---|
| 198 | CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl; |
---|
| 199 | return true; |
---|
| 200 | } |
---|
[1193] | 201 | try |
---|
| 202 | { |
---|
[1195] | 203 | if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0) |
---|
[934] | 204 | { |
---|
[1193] | 205 | mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); |
---|
| 206 | // register our listener in OIS. |
---|
| 207 | mouse_->setEventCallback(this); |
---|
| 208 | CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl; |
---|
[1203] | 209 | return true; |
---|
[934] | 210 | } |
---|
[1193] | 211 | else |
---|
[1203] | 212 | { |
---|
[1193] | 213 | CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl; |
---|
[1203] | 214 | return false; |
---|
| 215 | } |
---|
[918] | 216 | } |
---|
[1193] | 217 | catch (OIS::Exception ex) |
---|
| 218 | { |
---|
| 219 | CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS mouse\n" |
---|
| 220 | << "OIS error message: \"" << ex.eText << "\"" << std::endl; |
---|
| 221 | mouse_ = 0; |
---|
[1203] | 222 | return false; |
---|
[1193] | 223 | } |
---|
| 224 | } |
---|
[922] | 225 | |
---|
[1193] | 226 | /** |
---|
| 227 | @brief Creates all joy sticks and sets the event handler. |
---|
[1203] | 228 | @return False joy stick stay uninitialised, true otherwise. |
---|
[1193] | 229 | */ |
---|
[1203] | 230 | bool InputManager::_initialiseJoySticks() |
---|
[1193] | 231 | { |
---|
[1203] | 232 | if (joySticks_.size() > 0) |
---|
| 233 | { |
---|
| 234 | CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl; |
---|
| 235 | return true; |
---|
| 236 | } |
---|
| 237 | bool success = false; |
---|
[1193] | 238 | if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0) |
---|
| 239 | { |
---|
[1203] | 240 | for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++) |
---|
[1193] | 241 | { |
---|
| 242 | try |
---|
| 243 | { |
---|
[1203] | 244 | OIS::JoyStick* stig = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true)); |
---|
| 245 | joySticks_[stig->getID()] = stig; |
---|
[1193] | 246 | // register our listener in OIS. |
---|
[1203] | 247 | stig->setEventCallback(this); |
---|
| 248 | CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl; |
---|
| 249 | success = true; |
---|
[1193] | 250 | } |
---|
| 251 | catch (OIS::Exception ex) |
---|
| 252 | { |
---|
[1203] | 253 | CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n" |
---|
[1193] | 254 | << "OIS error message: \"" << ex.eText << "\"" << std::endl; |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | else |
---|
[1203] | 259 | { |
---|
[1193] | 260 | CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl; |
---|
[1203] | 261 | return false; |
---|
| 262 | } |
---|
| 263 | return success; |
---|
[1193] | 264 | } |
---|
[1022] | 265 | |
---|
[1193] | 266 | /** |
---|
[1203] | 267 | @brief Destroys all the created input devices and sets the InputManager to construction state. |
---|
[1193] | 268 | */ |
---|
[1203] | 269 | void InputManager::_destroy() |
---|
[1193] | 270 | { |
---|
[1203] | 271 | CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl; |
---|
| 272 | |
---|
| 273 | if (state_ != IS_UNINIT) |
---|
[1193] | 274 | { |
---|
[1203] | 275 | if (listenersKey_.find("buffer") != listenersKey_.end()) |
---|
| 276 | delete listenersKey_["buffer"]; |
---|
| 277 | |
---|
| 278 | if (listenersKey_.find("keybinder") != listenersKey_.end()) |
---|
| 279 | delete listenersKey_["keybinder"]; |
---|
| 280 | |
---|
| 281 | listenersKeyActive_.clear(); |
---|
| 282 | listenersMouseActive_.clear(); |
---|
| 283 | listenersJoySticksActive_.clear(); |
---|
| 284 | listenersKey_.clear(); |
---|
| 285 | listenersMouse_.clear(); |
---|
| 286 | listenersJoySticks_.clear(); |
---|
| 287 | |
---|
| 288 | keysDown_.clear(); |
---|
| 289 | mouseButtonsDown_.clear(); |
---|
| 290 | joySticksButtonsDown_.clear(); |
---|
| 291 | |
---|
| 292 | _destroyKeyboard(); |
---|
| 293 | _destroyMouse(); |
---|
| 294 | _destroyJoySticks(); |
---|
| 295 | |
---|
| 296 | // inputSystem_ can never be 0, or else the code is mistaken |
---|
| 297 | OIS::InputManager::destroyInputSystem(inputSystem_); |
---|
| 298 | inputSystem_ = 0; |
---|
| 299 | |
---|
| 300 | state_ = IS_UNINIT; |
---|
[1193] | 301 | } |
---|
[1203] | 302 | else |
---|
| 303 | CCOUT(ORX_WARNING) << "Warning: Cannot be destroyed, since not initialised." << std::endl; |
---|
| 304 | |
---|
| 305 | CCOUT(ORX_DEBUG) << "Destroying done." << std::endl; |
---|
[1193] | 306 | } |
---|
[922] | 307 | |
---|
[1193] | 308 | /** |
---|
[1203] | 309 | @brief Destroys the keyboard and sets it to 0. |
---|
[1193] | 310 | */ |
---|
[1203] | 311 | void InputManager::_destroyKeyboard() |
---|
[1193] | 312 | { |
---|
[1203] | 313 | if (keyboard_) |
---|
| 314 | // inputSystem_ can never be 0, or else the code is mistaken |
---|
| 315 | inputSystem_->destroyInputObject(keyboard_); |
---|
| 316 | keyboard_ = 0; |
---|
| 317 | CCOUT(ORX_DEBUG) << "Keyboard destroyed." << std::endl; |
---|
[918] | 318 | } |
---|
| 319 | |
---|
[1193] | 320 | /** |
---|
[1203] | 321 | @brief Destroys the mouse and sets it to 0. |
---|
[1193] | 322 | */ |
---|
[1203] | 323 | void InputManager::_destroyMouse() |
---|
[1182] | 324 | { |
---|
[1203] | 325 | if (mouse_) |
---|
| 326 | // inputSystem_ can never be 0, or else the code is mistaken |
---|
| 327 | inputSystem_->destroyInputObject(mouse_); |
---|
| 328 | mouse_ = 0; |
---|
| 329 | CCOUT(ORX_DEBUG) << "Mouse destroyed." << std::endl; |
---|
[1182] | 330 | } |
---|
| 331 | |
---|
[919] | 332 | /** |
---|
[1203] | 333 | @brief Destroys all the joy sticks and resizes the lists to 0. |
---|
[928] | 334 | */ |
---|
[1203] | 335 | void InputManager::_destroyJoySticks() |
---|
[928] | 336 | { |
---|
[1203] | 337 | if (joySticks_.size() > 0) |
---|
[1193] | 338 | { |
---|
[1203] | 339 | // note: inputSystem_ can never be 0, or else the code is mistaken |
---|
| 340 | for (std::map<int, OIS::JoyStick*>::iterator itstick = joySticks_.begin(); itstick != joySticks_.end(); itstick++) |
---|
[1193] | 341 | { |
---|
[1203] | 342 | if ((*itstick).second != 0) |
---|
| 343 | inputSystem_->destroyInputObject((*itstick).second); |
---|
[1193] | 344 | } |
---|
[1203] | 345 | joySticks_.clear(); |
---|
[1182] | 346 | } |
---|
[1203] | 347 | CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl; |
---|
[928] | 348 | } |
---|
| 349 | |
---|
[1193] | 350 | |
---|
[1203] | 351 | // ################################# |
---|
| 352 | // ### Private Interface Methods ### |
---|
| 353 | // ################################# |
---|
| 354 | // ################################# |
---|
[1193] | 355 | |
---|
[928] | 356 | /** |
---|
[1195] | 357 | @brief Updates the InputManager. Tick is called by Orxonox. |
---|
[919] | 358 | @param dt Delta time |
---|
| 359 | */ |
---|
[973] | 360 | void InputManager::tick(float dt) |
---|
[918] | 361 | { |
---|
[1182] | 362 | if (state_ == IS_UNINIT) |
---|
| 363 | return; |
---|
| 364 | |
---|
[1022] | 365 | // reset the game if it has changed |
---|
[1182] | 366 | if (state_ != stateRequest_) |
---|
[1022] | 367 | { |
---|
[1203] | 368 | if (stateRequest_ != IS_CUSTOM) |
---|
[1022] | 369 | { |
---|
[1203] | 370 | listenersKeyActive_.clear(); |
---|
| 371 | listenersMouseActive_.clear(); |
---|
| 372 | listenersJoySticksActive_.clear(); |
---|
[1193] | 373 | |
---|
[1203] | 374 | switch (stateRequest_) |
---|
[1193] | 375 | { |
---|
[1203] | 376 | case IS_NORMAL: |
---|
| 377 | // normal play mode |
---|
| 378 | if (listenersKey_.find("keybinder") != listenersKey_.end()) |
---|
| 379 | listenersKeyActive_.push_back(listenersKey_["keybinder"]); |
---|
| 380 | if (listenersMouse_.find("keybinder") != listenersMouse_.end()) |
---|
| 381 | listenersMouseActive_.push_back(listenersMouse_["keybinder"]); |
---|
| 382 | if (listenersJoySticks_.find("keybinder") != listenersJoySticks_.end()) |
---|
| 383 | { |
---|
| 384 | for (std::map<int, OIS::JoyStick*>::const_iterator it = joySticks_.begin(); |
---|
| 385 | it != joySticks_.end(); it++) |
---|
| 386 | listenersJoySticksActive_[(*it).first].push_back(listenersJoySticks_["keybinder"]); |
---|
| 387 | } |
---|
| 388 | break; |
---|
[1193] | 389 | |
---|
[1203] | 390 | case IS_GUI: |
---|
| 391 | // FIXME: do stuff |
---|
| 392 | break; |
---|
[1193] | 393 | |
---|
[1203] | 394 | case IS_CONSOLE: |
---|
| 395 | if (listenersMouse_.find("keybinder") != listenersMouse_.end()) |
---|
| 396 | listenersMouseActive_.push_back(listenersMouse_["keybinder"]); |
---|
| 397 | if (listenersJoySticks_.find("keybinder") != listenersJoySticks_.end()) |
---|
| 398 | { |
---|
| 399 | for (std::map<int, OIS::JoyStick*>::const_iterator it = joySticks_.begin(); |
---|
| 400 | it != joySticks_.end(); it++) |
---|
| 401 | listenersJoySticksActive_[(*it).first].push_back(listenersJoySticks_["keybinder"]); |
---|
| 402 | } |
---|
[1195] | 403 | |
---|
[1203] | 404 | if (listenersKey_.find("buffer") != listenersKey_.end()) |
---|
| 405 | listenersKeyActive_.push_back(listenersKey_["buffer"]); |
---|
| 406 | else |
---|
| 407 | { |
---|
| 408 | // someone fiddled with the InputBuffer |
---|
| 409 | CCOUT(2) << "Error: Cannot redirect input to console, InputBuffer instance missing." << std::endl; |
---|
| 410 | if (listenersKey_.find("keybinder") != listenersKey_.end()) |
---|
| 411 | listenersKeyActive_.push_back(listenersKey_["keybinder"]); |
---|
| 412 | else |
---|
| 413 | // this is bad |
---|
| 414 | CCOUT(2) << "Error: Cannot reactivate key binder: not found!" << std::endl; |
---|
| 415 | } |
---|
| 416 | break; |
---|
| 417 | |
---|
| 418 | default: |
---|
| 419 | break; |
---|
| 420 | } |
---|
| 421 | state_ = stateRequest_; |
---|
[1022] | 422 | } |
---|
| 423 | } |
---|
| 424 | |
---|
[1193] | 425 | // Capture all the input. This calls the event handlers in InputManager. |
---|
[918] | 426 | if (mouse_) |
---|
| 427 | mouse_->capture(); |
---|
| 428 | if (keyboard_) |
---|
| 429 | keyboard_->capture(); |
---|
[1203] | 430 | |
---|
| 431 | |
---|
| 432 | // call all the listeners for the held key events |
---|
| 433 | for (std::list<OIS::KeyCode>::const_iterator itKey = keysDown_.begin(); |
---|
| 434 | itKey != keysDown_.end(); itKey++) |
---|
| 435 | { |
---|
| 436 | OIS::KeyEvent keyArg(keyboard_, *itKey, 0); |
---|
| 437 | for (std::list<KeyHandler*>::const_iterator itKeyHandler = listenersKeyActive_.begin(); |
---|
| 438 | itKeyHandler != listenersKeyActive_.end(); itKeyHandler++) |
---|
| 439 | { |
---|
| 440 | (*itKeyHandler)->keyHeld(keyArg); |
---|
| 441 | } |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | // call all the listeners for the held mouse button events |
---|
| 445 | for (std::list<OIS::MouseButtonID>::const_iterator itMouseButton = mouseButtonsDown_.begin(); |
---|
| 446 | itMouseButton != mouseButtonsDown_.end(); itMouseButton++) |
---|
| 447 | { |
---|
| 448 | OIS::MouseEvent mouseButtonArg(mouse_, mouse_->getMouseState()); |
---|
| 449 | for (std::list<MouseHandler*>::const_iterator itMouseHandler = listenersMouseActive_.begin(); |
---|
| 450 | itMouseHandler != listenersMouseActive_.end(); itMouseHandler++) |
---|
| 451 | { |
---|
| 452 | (*itMouseHandler)->mouseHeld(mouseButtonArg, *itMouseButton); |
---|
| 453 | } |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | // call all the listeners for the held joy stick button events |
---|
| 457 | for (std::map<int, std::list <int> >::const_iterator itJoyStick = joySticksButtonsDown_.begin(); |
---|
| 458 | itJoyStick != joySticksButtonsDown_.end(); itJoyStick++) |
---|
| 459 | { |
---|
| 460 | int id = (*itJoyStick).first; |
---|
| 461 | for (std::list<int>::const_iterator itJoyStickButton = (*itJoyStick).second.begin(); |
---|
| 462 | itJoyStickButton != (*itJoyStick).second.end(); itJoyStickButton++) |
---|
| 463 | { |
---|
| 464 | OIS::JoyStickEvent joyStickButtonArg(joySticks_[id], joySticks_[id]->getJoyStickState()); |
---|
| 465 | for (std::list<JoyStickHandler*>::const_iterator itJoyStickHandler = listenersJoySticksActive_[id].begin(); |
---|
| 466 | itJoyStickHandler != listenersJoySticksActive_[id].end(); itJoyStickHandler++) |
---|
| 467 | { |
---|
| 468 | (*itJoyStickHandler)->buttonHeld(joyStickButtonArg, *itJoyStickButton); |
---|
| 469 | } |
---|
| 470 | } |
---|
| 471 | } |
---|
[918] | 472 | } |
---|
| 473 | |
---|
[1182] | 474 | |
---|
[919] | 475 | /** |
---|
[1182] | 476 | @brief Event handler for the keyPressed Event. |
---|
| 477 | @param e Event information |
---|
| 478 | */ |
---|
| 479 | bool InputManager::keyPressed(const OIS::KeyEvent &e) |
---|
| 480 | { |
---|
[1203] | 481 | // check whether the key is already in the list (can happen when focus was lost) |
---|
| 482 | for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++) |
---|
[1182] | 483 | { |
---|
[1203] | 484 | if (*it == e.key) |
---|
[1182] | 485 | { |
---|
[1203] | 486 | keysDown_.erase(it); |
---|
| 487 | break; |
---|
[1182] | 488 | } |
---|
| 489 | } |
---|
[1203] | 490 | keysDown_.push_back(e.key); |
---|
| 491 | |
---|
| 492 | for (std::list<KeyHandler*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++) |
---|
| 493 | (*it)->keyPressed(e); |
---|
| 494 | |
---|
[1182] | 495 | return true; |
---|
| 496 | } |
---|
| 497 | |
---|
| 498 | /** |
---|
| 499 | @brief Event handler for the keyReleased Event. |
---|
| 500 | @param e Event information |
---|
| 501 | */ |
---|
| 502 | bool InputManager::keyReleased(const OIS::KeyEvent &e) |
---|
| 503 | { |
---|
| 504 | // remove the key from the keysDown_ list |
---|
| 505 | for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++) |
---|
| 506 | { |
---|
| 507 | if (*it == e.key) |
---|
| 508 | { |
---|
| 509 | keysDown_.erase(it); |
---|
| 510 | break; |
---|
| 511 | } |
---|
| 512 | } |
---|
| 513 | |
---|
[1203] | 514 | for (std::list<KeyHandler*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++) |
---|
| 515 | (*it)->keyReleased(e); |
---|
| 516 | |
---|
[1182] | 517 | return true; |
---|
| 518 | } |
---|
| 519 | |
---|
| 520 | /** |
---|
| 521 | @brief Event handler for the mouseMoved Event. |
---|
| 522 | @param e Event information |
---|
| 523 | */ |
---|
| 524 | bool InputManager::mouseMoved(const OIS::MouseEvent &e) |
---|
| 525 | { |
---|
[1203] | 526 | for (std::list<MouseHandler*>::const_iterator it = listenersMouseActive_.begin(); it != listenersMouseActive_.end(); it++) |
---|
| 527 | (*it)->mouseMoved(e); |
---|
| 528 | |
---|
[1182] | 529 | return true; |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | /** |
---|
| 533 | @brief Event handler for the mousePressed Event. |
---|
| 534 | @param e Event information |
---|
| 535 | @param id The ID of the mouse button |
---|
| 536 | */ |
---|
| 537 | bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
| 538 | { |
---|
[1203] | 539 | // check whether the button is already in the list (can happen when focus was lost) |
---|
| 540 | for (std::list<OIS::MouseButtonID>::iterator it = mouseButtonsDown_.begin(); it != mouseButtonsDown_.end(); it++) |
---|
| 541 | { |
---|
| 542 | if (*it == id) |
---|
| 543 | { |
---|
| 544 | mouseButtonsDown_.erase(it); |
---|
| 545 | break; |
---|
| 546 | } |
---|
| 547 | } |
---|
| 548 | mouseButtonsDown_.push_back(id); |
---|
| 549 | |
---|
| 550 | for (std::list<MouseHandler*>::const_iterator it = listenersMouseActive_.begin(); it != listenersMouseActive_.end(); it++) |
---|
| 551 | (*it)->mousePressed(e, id); |
---|
| 552 | |
---|
[1182] | 553 | return true; |
---|
| 554 | } |
---|
| 555 | |
---|
| 556 | /** |
---|
| 557 | @brief Event handler for the mouseReleased Event. |
---|
| 558 | @param e Event information |
---|
| 559 | @param id The ID of the mouse button |
---|
| 560 | */ |
---|
| 561 | bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
| 562 | { |
---|
[1203] | 563 | // remove the button from the keysDown_ list |
---|
| 564 | for (std::list<OIS::MouseButtonID>::iterator it = mouseButtonsDown_.begin(); it != mouseButtonsDown_.end(); it++) |
---|
| 565 | { |
---|
| 566 | if (*it == id) |
---|
| 567 | { |
---|
| 568 | mouseButtonsDown_.erase(it); |
---|
| 569 | break; |
---|
| 570 | } |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | for (std::list<MouseHandler*>::const_iterator it = listenersMouseActive_.begin(); it != listenersMouseActive_.end(); it++) |
---|
| 574 | (*it)->mouseReleased(e, id); |
---|
| 575 | |
---|
[1182] | 576 | return true; |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button) |
---|
| 580 | { |
---|
[1203] | 581 | int devID = arg.device->getID(); |
---|
| 582 | |
---|
| 583 | // check whether the button is already in the list (can happen when focus was lost) |
---|
| 584 | std::list<int>& buttonsDownList = joySticksButtonsDown_[devID]; |
---|
| 585 | for (std::list<int>::iterator it = buttonsDownList.begin(); it != buttonsDownList.end(); it++) |
---|
| 586 | { |
---|
| 587 | if (*it == button) |
---|
| 588 | { |
---|
| 589 | buttonsDownList.erase(it); |
---|
| 590 | break; |
---|
| 591 | } |
---|
| 592 | } |
---|
| 593 | joySticksButtonsDown_[devID].push_back(button); |
---|
| 594 | |
---|
| 595 | std::list<JoyStickHandler*>::iterator end = listenersJoySticksActive_[devID].end(); |
---|
| 596 | for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++) |
---|
| 597 | (*it)->buttonPressed(arg, button); |
---|
| 598 | |
---|
| 599 | return true; |
---|
[1182] | 600 | } |
---|
| 601 | |
---|
| 602 | bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button) |
---|
| 603 | { |
---|
[1203] | 604 | int devID = arg.device->getID(); |
---|
| 605 | |
---|
| 606 | // remove the button from the joySticksButtonsDown_ list |
---|
| 607 | std::list<int>& buttonsDownList = joySticksButtonsDown_[devID]; |
---|
| 608 | for (std::list<int>::iterator it = buttonsDownList.begin(); it != buttonsDownList.end(); it++) |
---|
| 609 | { |
---|
| 610 | if (*it == button) |
---|
| 611 | { |
---|
| 612 | buttonsDownList.erase(it); |
---|
| 613 | break; |
---|
| 614 | } |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end(); |
---|
| 618 | for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++) |
---|
| 619 | (*it)->buttonReleased(arg, button); |
---|
| 620 | |
---|
| 621 | return true; |
---|
[1182] | 622 | } |
---|
| 623 | |
---|
| 624 | bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis) |
---|
| 625 | { |
---|
[1203] | 626 | int devID = arg.device->getID(); |
---|
| 627 | std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end(); |
---|
| 628 | for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++) |
---|
| 629 | (*it)->axisMoved(arg, axis); |
---|
| 630 | |
---|
[1182] | 631 | return true; |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id) |
---|
| 635 | { |
---|
[1203] | 636 | int devID = arg.device->getID(); |
---|
| 637 | std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end(); |
---|
| 638 | for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++) |
---|
| 639 | (*it)->sliderMoved(arg, id); |
---|
| 640 | |
---|
[1182] | 641 | return true; |
---|
| 642 | } |
---|
| 643 | |
---|
| 644 | bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id) |
---|
| 645 | { |
---|
[1203] | 646 | int devID = arg.device->getID(); |
---|
| 647 | std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end(); |
---|
| 648 | for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++) |
---|
| 649 | (*it)->povMoved(arg, id); |
---|
| 650 | |
---|
[1182] | 651 | return true; |
---|
| 652 | } |
---|
| 653 | |
---|
| 654 | |
---|
[1193] | 655 | // ################################ |
---|
| 656 | // ### Static Interface Methods ### |
---|
| 657 | // ################################ |
---|
[1203] | 658 | // ################################ |
---|
[1182] | 659 | |
---|
[1203] | 660 | bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight, |
---|
| 661 | bool createKeyboard, bool createMouse, bool createJoySticks) |
---|
| 662 | { |
---|
| 663 | return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight, |
---|
| 664 | createKeyboard, createMouse, createJoySticks); |
---|
| 665 | } |
---|
| 666 | |
---|
| 667 | bool InputManager::initialiseKeyboard() |
---|
| 668 | { |
---|
| 669 | return _getSingleton()._initialiseKeyboard(); |
---|
| 670 | } |
---|
| 671 | |
---|
| 672 | bool InputManager::initialiseMouse() |
---|
| 673 | { |
---|
| 674 | return _getSingleton()._initialiseMouse(); |
---|
| 675 | } |
---|
| 676 | |
---|
| 677 | bool InputManager::initialiseJoySticks() |
---|
| 678 | { |
---|
| 679 | return _getSingleton()._initialiseJoySticks(); |
---|
| 680 | } |
---|
| 681 | |
---|
| 682 | |
---|
| 683 | void InputManager::destroy() |
---|
| 684 | { |
---|
| 685 | _getSingleton()._destroy(); |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | void InputManager::destroyKeyboard() |
---|
| 689 | { |
---|
| 690 | return _getSingleton()._destroyKeyboard(); |
---|
| 691 | } |
---|
| 692 | |
---|
| 693 | void InputManager::destroyMouse() |
---|
| 694 | { |
---|
| 695 | return _getSingleton()._destroyMouse(); |
---|
| 696 | } |
---|
| 697 | |
---|
| 698 | void InputManager::destroyJoySticks() |
---|
| 699 | { |
---|
| 700 | return _getSingleton()._destroyJoySticks(); |
---|
| 701 | } |
---|
| 702 | |
---|
| 703 | |
---|
[1182] | 704 | /** |
---|
[919] | 705 | @brief Adjusts the mouse window metrics. |
---|
| 706 | This method has to be called every time the size of the window changes. |
---|
| 707 | @param width The new width of the render window |
---|
| 708 | @param height the new height of the render window |
---|
| 709 | */ |
---|
[973] | 710 | void InputManager::setWindowExtents(int width, int height) |
---|
[918] | 711 | { |
---|
[1193] | 712 | if (_getSingleton().mouse_) |
---|
| 713 | { |
---|
| 714 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
---|
| 715 | const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState(); |
---|
| 716 | mouseState.width = width; |
---|
| 717 | mouseState.height = height; |
---|
| 718 | } |
---|
[918] | 719 | } |
---|
| 720 | |
---|
[919] | 721 | /** |
---|
[1022] | 722 | @brief Sets the input mode to either GUI, inGame or Buffer |
---|
| 723 | @param mode The new input mode |
---|
| 724 | @remark Only has an affect if the mode actually changes |
---|
[922] | 725 | */ |
---|
[1182] | 726 | void InputManager::setInputState(const InputState state) |
---|
[922] | 727 | { |
---|
[1182] | 728 | _getSingleton().stateRequest_ = state; |
---|
[922] | 729 | } |
---|
| 730 | |
---|
| 731 | /** |
---|
[1022] | 732 | @brief Returns the current input handling method |
---|
| 733 | @return The current input mode. |
---|
[919] | 734 | */ |
---|
[1182] | 735 | InputManager::InputState InputManager::getInputState() |
---|
[918] | 736 | { |
---|
[1182] | 737 | return _getSingleton().state_; |
---|
[918] | 738 | } |
---|
| 739 | |
---|
[1203] | 740 | |
---|
| 741 | // ###### KeyHandler ###### |
---|
| 742 | |
---|
| 743 | /** |
---|
| 744 | @brief Adds a new key listener. |
---|
| 745 | @param listener Pointer to the listener object. |
---|
| 746 | @param name Unique name of the listener. |
---|
| 747 | @return True if added, false if name already existed. |
---|
| 748 | */ |
---|
| 749 | bool InputManager::addKeyListener(KeyHandler* listener, const std::string& name) |
---|
[1066] | 750 | { |
---|
[1203] | 751 | if (_getSingleton().listenersKey_.find(name) == _getSingleton().listenersKey_.end()) |
---|
| 752 | { |
---|
| 753 | _getSingleton().listenersKey_[name] = listener; |
---|
| 754 | return true; |
---|
| 755 | } |
---|
| 756 | else |
---|
| 757 | return false; |
---|
[1066] | 758 | } |
---|
| 759 | |
---|
[1203] | 760 | /** |
---|
| 761 | @brief Removes a Key Listener from the list. |
---|
| 762 | @param name Unique name of the listener. |
---|
| 763 | @return True if removal was successful, false if name was not found. |
---|
| 764 | */ |
---|
| 765 | bool InputManager::removeKeyListener(const std::string &name) |
---|
[1182] | 766 | { |
---|
[1203] | 767 | disableKeyListener(name); |
---|
| 768 | std::map<std::string, KeyHandler*>::iterator it = _getSingleton().listenersKey_.find(name); |
---|
| 769 | if (it != _getSingleton().listenersKey_.end()) |
---|
| 770 | { |
---|
| 771 | _getSingleton().listenersKey_.erase(it); |
---|
| 772 | return true; |
---|
| 773 | } |
---|
| 774 | else |
---|
| 775 | return false; |
---|
[1182] | 776 | } |
---|
[1066] | 777 | |
---|
[1203] | 778 | /** |
---|
| 779 | @brief Returns the pointer to a listener. |
---|
| 780 | @param name Unique name of the listener. |
---|
| 781 | @return Pointer to the instance, 0 if name was not found. |
---|
| 782 | */ |
---|
| 783 | KeyHandler* InputManager::getKeyListener(const std::string& name) |
---|
[1195] | 784 | { |
---|
[1203] | 785 | std::map<std::string, KeyHandler*>::iterator it = _getSingleton().listenersKey_.find(name); |
---|
| 786 | if (it != _getSingleton().listenersKey_.end()) |
---|
| 787 | { |
---|
| 788 | return (*it).second; |
---|
| 789 | } |
---|
| 790 | else |
---|
| 791 | return 0; |
---|
[1195] | 792 | } |
---|
| 793 | |
---|
[1203] | 794 | /** |
---|
| 795 | @brief Enables a specific key listener that has already been added. |
---|
| 796 | @param name Unique name of the listener. |
---|
| 797 | @return False if name was not found, true otherwise. |
---|
| 798 | */ |
---|
| 799 | bool InputManager::enableKeyListener(const std::string& name) |
---|
[1195] | 800 | { |
---|
[1203] | 801 | // get pointer from the map with all stored listeners |
---|
| 802 | std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().listenersKey_.find(name); |
---|
| 803 | if (mapIt == _getSingleton().listenersKey_.end()) |
---|
| 804 | return false; |
---|
| 805 | // see whether the listener is already in the list |
---|
| 806 | for (std::list<KeyHandler*>::iterator it = _getSingleton().listenersKeyActive_.begin(); |
---|
| 807 | it != _getSingleton().listenersKeyActive_.end(); it++) |
---|
| 808 | { |
---|
| 809 | if ((*it) == (*mapIt).second) |
---|
| 810 | return true; |
---|
| 811 | } |
---|
| 812 | _getSingleton().listenersKeyActive_.push_back((*mapIt).second); |
---|
| 813 | return true; |
---|
[1195] | 814 | } |
---|
| 815 | |
---|
[1203] | 816 | /** |
---|
| 817 | @brief Disables a specific key listener. |
---|
| 818 | @param name Unique name of the listener. |
---|
| 819 | @return False if name was not found, true otherwise. |
---|
| 820 | */ |
---|
| 821 | bool InputManager::disableKeyListener(const std::string &name) |
---|
[1195] | 822 | { |
---|
[1203] | 823 | // get pointer from the map with all stored listeners |
---|
| 824 | std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().listenersKey_.find(name); |
---|
| 825 | if (mapIt == _getSingleton().listenersKey_.end()) |
---|
| 826 | return false; |
---|
| 827 | // look for the listener in the list |
---|
| 828 | for (std::list<KeyHandler*>::iterator it = _getSingleton().listenersKeyActive_.begin(); |
---|
| 829 | it != _getSingleton().listenersKeyActive_.end(); it++) |
---|
| 830 | { |
---|
| 831 | if ((*it) == (*mapIt).second) |
---|
| 832 | { |
---|
| 833 | _getSingleton().listenersKeyActive_.erase(it); |
---|
| 834 | return true; |
---|
| 835 | } |
---|
| 836 | } |
---|
| 837 | return true; |
---|
| 838 | } |
---|
[1195] | 839 | |
---|
[1203] | 840 | |
---|
| 841 | // ###### MouseHandler ###### |
---|
| 842 | /** |
---|
| 843 | @brief Adds a new mouse listener. |
---|
| 844 | @param listener Pointer to the listener object. |
---|
| 845 | @param name Unique name of the listener. |
---|
| 846 | @return True if added, false if name already existed. |
---|
| 847 | */ |
---|
| 848 | bool InputManager::addMouseListener(MouseHandler* listener, const std::string& name) |
---|
[1182] | 849 | { |
---|
[1203] | 850 | if (_getSingleton().listenersMouse_.find(name) == _getSingleton().listenersMouse_.end()) |
---|
[1182] | 851 | { |
---|
[1203] | 852 | _getSingleton().listenersMouse_[name] = listener; |
---|
[1182] | 853 | return true; |
---|
| 854 | } |
---|
| 855 | else |
---|
| 856 | return false; |
---|
| 857 | } |
---|
| 858 | |
---|
[1203] | 859 | /** |
---|
| 860 | @brief Removes a Mouse Listener from the list. |
---|
| 861 | @param name Unique name of the listener. |
---|
| 862 | @return True if removal was successful, false if name was not found. |
---|
| 863 | */ |
---|
| 864 | bool InputManager::removeMouseListener(const std::string &name) |
---|
[1182] | 865 | { |
---|
[1203] | 866 | disableMouseListener(name); |
---|
| 867 | std::map<std::string, MouseHandler*>::iterator it = _getSingleton().listenersMouse_.find(name); |
---|
| 868 | if (it != _getSingleton().listenersMouse_.end()) |
---|
[1182] | 869 | { |
---|
[1203] | 870 | _getSingleton().listenersMouse_.erase(it); |
---|
[1182] | 871 | return true; |
---|
| 872 | } |
---|
| 873 | else |
---|
| 874 | return false; |
---|
| 875 | } |
---|
| 876 | |
---|
[1203] | 877 | /** |
---|
| 878 | @brief Returns the pointer to a listener. |
---|
| 879 | @param name Unique name of the listener. |
---|
| 880 | @return Pointer to the instance, 0 if name was not found. |
---|
| 881 | */ |
---|
| 882 | MouseHandler* InputManager::getMouseListener(const std::string& name) |
---|
[1182] | 883 | { |
---|
[1203] | 884 | std::map<std::string, MouseHandler*>::iterator it = _getSingleton().listenersMouse_.find(name); |
---|
| 885 | if (it != _getSingleton().listenersMouse_.end()) |
---|
[1182] | 886 | { |
---|
| 887 | return (*it).second; |
---|
| 888 | } |
---|
| 889 | else |
---|
| 890 | return 0; |
---|
| 891 | } |
---|
| 892 | |
---|
[1203] | 893 | /** |
---|
| 894 | @brief Enables a specific mouse listener that has already been added. |
---|
| 895 | @param name Unique name of the listener. |
---|
| 896 | @return False if name was not found, true otherwise. |
---|
| 897 | */ |
---|
| 898 | bool InputManager::enableMouseListener(const std::string& name) |
---|
| 899 | { |
---|
| 900 | // get pointer from the map with all stored listeners |
---|
| 901 | std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().listenersMouse_.find(name); |
---|
| 902 | if (mapIt == _getSingleton().listenersMouse_.end()) |
---|
| 903 | return false; |
---|
| 904 | // see whether the listener is already in the list |
---|
| 905 | for (std::list<MouseHandler*>::iterator it = _getSingleton().listenersMouseActive_.begin(); |
---|
| 906 | it != _getSingleton().listenersMouseActive_.end(); it++) |
---|
| 907 | { |
---|
| 908 | if ((*it) == (*mapIt).second) |
---|
| 909 | return true; |
---|
| 910 | } |
---|
| 911 | _getSingleton().listenersMouseActive_.push_back((*mapIt).second); |
---|
| 912 | return true; |
---|
| 913 | } |
---|
| 914 | |
---|
| 915 | /** |
---|
| 916 | @brief Disables a specific mouse listener. |
---|
| 917 | @param name Unique name of the listener. |
---|
| 918 | @return False if name was not found, true otherwise. |
---|
| 919 | */ |
---|
| 920 | bool InputManager::disableMouseListener(const std::string &name) |
---|
| 921 | { |
---|
| 922 | // get pointer from the map with all stored listeners |
---|
| 923 | std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().listenersMouse_.find(name); |
---|
| 924 | if (mapIt == _getSingleton().listenersMouse_.end()) |
---|
| 925 | return false; |
---|
| 926 | // look for the listener in the list |
---|
| 927 | for (std::list<MouseHandler*>::iterator it = _getSingleton().listenersMouseActive_.begin(); |
---|
| 928 | it != _getSingleton().listenersMouseActive_.end(); it++) |
---|
| 929 | { |
---|
| 930 | if ((*it) == (*mapIt).second) |
---|
| 931 | { |
---|
| 932 | _getSingleton().listenersMouseActive_.erase(it); |
---|
| 933 | return true; |
---|
| 934 | } |
---|
| 935 | } |
---|
| 936 | return true; |
---|
| 937 | } |
---|
| 938 | |
---|
| 939 | |
---|
| 940 | // ###### JoyStickHandler ###### |
---|
| 941 | |
---|
| 942 | /** |
---|
| 943 | @brief Adds a new joy stick listener. |
---|
| 944 | @param listener Pointer to the listener object. |
---|
| 945 | @param name Unique name of the listener. |
---|
| 946 | @return True if added, false if name already existed. |
---|
| 947 | */ |
---|
| 948 | bool InputManager::addJoyStickListener(JoyStickHandler* listener, const std::string& name) |
---|
| 949 | { |
---|
| 950 | if (_getSingleton().listenersJoySticks_.find(name) == _getSingleton().listenersJoySticks_.end()) |
---|
| 951 | { |
---|
| 952 | _getSingleton().listenersJoySticks_[name] = listener; |
---|
| 953 | return true; |
---|
| 954 | } |
---|
| 955 | else |
---|
| 956 | return false; |
---|
| 957 | } |
---|
| 958 | |
---|
| 959 | /** |
---|
| 960 | @brief Removes a JoyStick Listener from the list. |
---|
| 961 | @param name Unique name of the listener. |
---|
| 962 | @return True if removal was successful, false if name was not found. |
---|
| 963 | */ |
---|
| 964 | bool InputManager::removeJoyStickListener(const std::string &name) |
---|
| 965 | { |
---|
| 966 | for (std::map<int, OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin(); itstick != _getSingleton().joySticks_.end(); itstick++) |
---|
| 967 | disableJoyStickListener(name, (*itstick).first); |
---|
| 968 | |
---|
| 969 | std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticks_.find(name); |
---|
| 970 | if (it != _getSingleton().listenersJoySticks_.end()) |
---|
| 971 | { |
---|
| 972 | _getSingleton().listenersJoySticks_.erase(it); |
---|
| 973 | return true; |
---|
| 974 | } |
---|
| 975 | else |
---|
| 976 | return false; |
---|
| 977 | } |
---|
| 978 | |
---|
| 979 | /** |
---|
| 980 | @brief Returns the pointer to a listener. |
---|
| 981 | @param name Unique name of the listener. |
---|
| 982 | @return Pointer to the instance, 0 if name was not found. |
---|
| 983 | */ |
---|
| 984 | JoyStickHandler* InputManager::getJoyStickListener(const std::string& name) |
---|
| 985 | { |
---|
| 986 | std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticks_.find(name); |
---|
| 987 | if (it != _getSingleton().listenersJoySticks_.end()) |
---|
| 988 | { |
---|
| 989 | return (*it).second; |
---|
| 990 | } |
---|
| 991 | else |
---|
| 992 | return 0; |
---|
| 993 | } |
---|
| 994 | |
---|
| 995 | /** |
---|
| 996 | @brief Enables a specific joy stick listener that has already been added. |
---|
| 997 | @param name Unique name of the listener. |
---|
| 998 | @return False if name or id was not found, true otherwise. |
---|
| 999 | */ |
---|
| 1000 | bool InputManager::enableJoyStickListener(const std::string& name, const int id) |
---|
| 1001 | { |
---|
| 1002 | // get pointer from the map with all stored listeners |
---|
| 1003 | std::map<std::string, JoyStickHandler*>::const_iterator listenerIt = _getSingleton().listenersJoySticks_.find(name); |
---|
| 1004 | if (listenerIt == _getSingleton().listenersJoySticks_.end()) |
---|
| 1005 | return false; |
---|
| 1006 | |
---|
| 1007 | // check for existance of the ID |
---|
| 1008 | std::map<int, OIS::JoyStick*>::const_iterator joyStickIt = _getSingleton().joySticks_.find(id); |
---|
| 1009 | if (joyStickIt == _getSingleton().joySticks_.end()) |
---|
| 1010 | return false; |
---|
| 1011 | |
---|
| 1012 | // see whether the listener is already in the list |
---|
| 1013 | for (std::list<JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticksActive_[id].begin(); |
---|
| 1014 | it != _getSingleton().listenersJoySticksActive_[id].end(); it++) |
---|
| 1015 | { |
---|
| 1016 | if ((*it) == (*listenerIt).second) |
---|
| 1017 | return true; |
---|
| 1018 | } |
---|
| 1019 | _getSingleton().listenersJoySticksActive_[id].push_back((*listenerIt).second); |
---|
| 1020 | return true; |
---|
| 1021 | } |
---|
| 1022 | |
---|
| 1023 | /** |
---|
| 1024 | @brief Disables a specific joy stick listener. |
---|
| 1025 | @param name Unique name of the listener. |
---|
| 1026 | @return False if name or id was not found, true otherwise. |
---|
| 1027 | */ |
---|
| 1028 | bool InputManager::disableJoyStickListener(const std::string &name, int id) |
---|
| 1029 | { |
---|
| 1030 | // get pointer from the map with all stored listeners |
---|
| 1031 | std::map<std::string, JoyStickHandler*>::const_iterator listenerIt = _getSingleton().listenersJoySticks_.find(name); |
---|
| 1032 | if (listenerIt == _getSingleton().listenersJoySticks_.end()) |
---|
| 1033 | return false; |
---|
| 1034 | |
---|
| 1035 | // check for existance of the ID |
---|
| 1036 | std::map<int, OIS::JoyStick*>::const_iterator joyStickIt = _getSingleton().joySticks_.find(id); |
---|
| 1037 | if (joyStickIt == _getSingleton().joySticks_.end()) |
---|
| 1038 | return false; |
---|
| 1039 | |
---|
| 1040 | // look for the listener in the list |
---|
| 1041 | for (std::list<JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticksActive_[id].begin(); |
---|
| 1042 | it != _getSingleton().listenersJoySticksActive_[id].end(); it++) |
---|
| 1043 | { |
---|
| 1044 | if ((*it) == (*listenerIt).second) |
---|
| 1045 | { |
---|
| 1046 | _getSingleton().listenersJoySticksActive_[id].erase(it); |
---|
| 1047 | return true; |
---|
| 1048 | } |
---|
| 1049 | } |
---|
| 1050 | return true; |
---|
| 1051 | } |
---|
| 1052 | |
---|
[918] | 1053 | } |
---|