Changeset 1203 for code/branches/input/src/core/InputManager.cc
- Timestamp:
- Apr 29, 2008, 11:13:11 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/input/src/core/InputManager.cc
r1195 r1203 45 45 // ### Internal Methods ### 46 46 // ############################### 47 // ############################### 47 48 48 49 /** … … 54 55 state_(IS_UNINIT), stateRequest_(IS_UNINIT) 55 56 { 56 // overwrite every key binding with ""57 _clearBindings();58 _setNumberOfJoysticks(0);59 60 57 RegisterObject(InputManager); 61 58 } … … 76 73 InputManager::~InputManager() 77 74 { 78 this->_destroy();75 _destroy(); 79 76 } 80 77 … … 129 126 setWindowExtents(windowWidth, windowHeight); 130 127 131 this->state_ = IS_NONE;128 state_ = IS_NONE; 132 129 CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl; 133 130 } … … 140 137 addKeyListener(new InputBuffer(), "buffer"); 141 138 139 KeyBinder* binder = new KeyBinder(); 140 binder->loadBindings(); 141 addKeyListener(binder, "keybinder"); 142 addMouseListener(binder, "keybinder"); 143 142 144 // Read all the key bindings and assign them 143 if (!_loadBindings())144 return false;145 //if (!_loadBindings()) 146 // return false; 145 147 146 148 CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl; … … 150 152 /** 151 153 @brief Creates a keyboard and sets the event handler. 152 */ 153 void InputManager::_initialiseKeyboard() 154 { 154 @return False if keyboard stays uninitialised, true otherwise. 155 */ 156 bool InputManager::_initialiseKeyboard() 157 { 158 if (keyboard_ != 0) 159 { 160 CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl; 161 return true; 162 } 155 163 try 156 164 { … … 162 170 // note: OIS will not detect keys that have already been down when the keyboard was created. 163 171 CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl; 172 return true; 164 173 } 165 174 else 175 { 166 176 CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl; 177 return false; 178 } 167 179 } 168 180 catch (OIS::Exception ex) 169 181 { 182 // TODO: Test this output regarding formatting 170 183 CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n" 171 184 << "OIS error message: \"" << ex.eText << "\"" << std::endl; 172 185 keyboard_ = 0; 186 return false; 173 187 } 174 188 } … … 176 190 /** 177 191 @brief Creates a mouse and sets the event handler. 178 */ 179 void InputManager::_initialiseMouse() 180 { 192 @return False if mouse stays uninitialised, true otherwise. 193 */ 194 bool InputManager::_initialiseMouse() 195 { 196 if (mouse_ != 0) 197 { 198 CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl; 199 return true; 200 } 181 201 try 182 202 { … … 187 207 mouse_->setEventCallback(this); 188 208 CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl; 209 return true; 189 210 } 190 211 else 212 { 191 213 CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl; 214 return false; 215 } 192 216 } 193 217 catch (OIS::Exception ex) … … 196 220 << "OIS error message: \"" << ex.eText << "\"" << std::endl; 197 221 mouse_ = 0; 222 return false; 198 223 } 199 224 } … … 201 226 /** 202 227 @brief Creates all joy sticks and sets the event handler. 203 */ 204 void InputManager::_initialiseJoySticks() 205 { 228 @return False joy stick stay uninitialised, true otherwise. 229 */ 230 bool InputManager::_initialiseJoySticks() 231 { 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; 206 238 if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0) 207 239 { 208 _setNumberOfJoysticks(inputSystem_->getNumberOfDevices(OIS::OISJoyStick)); 209 for (std::vector<OIS::JoyStick*>::iterator it = joySticks_.begin(); it != joySticks_.end(); it++) 240 for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++) 210 241 { 211 242 try 212 243 { 213 *it = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true)); 244 OIS::JoyStick* stig = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true)); 245 joySticks_[stig->getID()] = stig; 214 246 // register our listener in OIS. 215 (*it)->setEventCallback(this); 216 CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << (*it)->getID() << std::endl; 247 stig->setEventCallback(this); 248 CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl; 249 success = true; 217 250 } 218 251 catch (OIS::Exception ex) 219 252 { 220 CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy stick with ID" << (*it)->getID()<< "\n"253 CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n" 221 254 << "OIS error message: \"" << ex.eText << "\"" << std::endl; 222 (*it) = 0;223 255 } 224 256 } 225 257 } 226 258 else 259 { 227 260 CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl; 228 } 229 230 /** 231 @brief Resizes all lists related to joy sticks and sets joy stick bindings to "". 232 @param size Number of joy sticks available. 233 */ 234 void InputManager::_setNumberOfJoysticks(int size) 235 { 236 this->bindingsJoyStickButtonHold_ .resize(size); 237 this->bindingsJoyStickButtonPress_ .resize(size); 238 this->bindingsJoyStickButtonRelease_.resize(size); 239 this->bJoyStickButtonBindingsActive_.resize(size); 240 this->joyStickButtonsDown_ .resize(size); 241 this->joySticks_ .resize(size); 242 for (int j = 0; j < size; j++) 243 { 244 bindingsJoyStickButtonPress_ [j].resize(numberOfJoyStickButtons_s); 245 bindingsJoyStickButtonRelease_[j].resize(numberOfJoyStickButtons_s); 246 bindingsJoyStickButtonHold_ [j].resize(numberOfJoyStickButtons_s); 247 for (int i = 0; i < numberOfJoyStickButtons_s; i++) 248 { 249 this->bindingsJoyStickButtonPress_ [j][i] = ""; 250 this->bindingsJoyStickButtonRelease_[j][i] = ""; 251 this->bindingsJoyStickButtonHold_ [j][i] = ""; 252 } 253 } 254 } 255 256 /** 257 @brief Loads the key and button bindings. 258 */ 259 bool InputManager::_loadBindings() 260 { 261 CCOUT(ORX_DEBUG) << "Loading key bindings..." << std::endl; 262 263 // clear all the bindings at first. 264 _clearBindings(); 265 266 // TODO: Insert the code to load the bindings from file. 267 this->bindingsKeyPress_[OIS::KC_NUMPADENTER] = "activateConsole"; 268 this->bindingsKeyPress_[OIS::KC_ESCAPE] = "exit"; 269 this->bindingsKeyHold_[OIS::KC_U] = "exec disco.txt"; 270 271 CCOUT(ORX_DEBUG) << "Loading key bindings done." << std::endl; 272 return true; 273 } 274 275 /** 276 @brief Overwrites all bindings with "" 277 */ 278 void InputManager::_clearBindings() 279 { 280 for (int i = 0; i < numberOfKeys_s; i++) 281 { 282 this->bindingsKeyPress_ [i] = ""; 283 this->bindingsKeyRelease_[i] = ""; 284 this->bindingsKeyHold_ [i] = ""; 285 } 286 for (int i = 0; i < numberOfMouseButtons_s; i++) 287 { 288 this->bindingsMouseButtonPress_ [i] = ""; 289 this->bindingsMouseButtonRelease_[i] = ""; 290 this->bindingsMouseButtonHold_ [i] = ""; 291 } 292 for (unsigned int j = 0; j < joySticks_.size(); j++) 293 { 294 for (int i = 0; i < numberOfJoyStickButtons_s; i++) 295 { 296 this->bindingsJoyStickButtonPress_ [j][i] = ""; 297 this->bindingsJoyStickButtonRelease_[j][i] = ""; 298 this->bindingsJoyStickButtonHold_ [j][i] = ""; 299 } 300 } 261 return false; 262 } 263 return success; 301 264 } 302 265 … … 310 273 if (state_ != IS_UNINIT) 311 274 { 312 this->listenersKeyActive_.clear();313 this->listenersMouseActive_.clear();314 this->listenersJoySticksActive_.clear();315 this->listenersKey_.clear();316 this->listenersMouse_.clear();317 this->listenersJoySticks_.clear();318 319 this->keysDown_.clear();320 this->mouseButtonsDown_.clear();321 322 _clearBindings();323 324 if (keyboard_)325 inputSystem_->destroyInputObject(keyboard_);326 keyboard_ = 0;327 328 if (mouse_)329 inputSystem_->destroyInputObject(mouse_);330 mouse_ = 0;331 332 if (joySticks_.size() > 0)333 {334 for (unsigned int i = 0; i < joySticks_.size(); i++)335 {336 if (joySticks_[i] != 0)337 inputSystem_->destroyInputObject(joySticks_[i]);338 }339 _setNumberOfJoysticks(0);340 }341 342 if (inputSystem_)343 OIS::InputManager::destroyInputSystem(inputSystem_);344 inputSystem_ = 0;345 346 275 if (listenersKey_.find("buffer") != listenersKey_.end()) 347 276 delete listenersKey_["buffer"]; 348 277 349 this->state_ = IS_UNINIT; 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; 350 301 } 351 302 else … … 355 306 } 356 307 357 358 // ############################### 359 // ### Interface Methods ### 360 // ############################### 308 /** 309 @brief Destroys the keyboard and sets it to 0. 310 */ 311 void InputManager::_destroyKeyboard() 312 { 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; 318 } 319 320 /** 321 @brief Destroys the mouse and sets it to 0. 322 */ 323 void InputManager::_destroyMouse() 324 { 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; 330 } 331 332 /** 333 @brief Destroys all the joy sticks and resizes the lists to 0. 334 */ 335 void InputManager::_destroyJoySticks() 336 { 337 if (joySticks_.size() > 0) 338 { 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++) 341 { 342 if ((*itstick).second != 0) 343 inputSystem_->destroyInputObject((*itstick).second); 344 } 345 joySticks_.clear(); 346 } 347 CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl; 348 } 349 350 351 // ################################# 352 // ### Private Interface Methods ### 353 // ################################# 354 // ################################# 361 355 362 356 /** … … 372 366 if (state_ != stateRequest_) 373 367 { 374 switch (stateRequest_) 375 { 376 case IS_NORMAL: 377 this->listenersKeyActive_.clear(); 378 this->listenersMouseActive_.clear(); 379 this->listenersJoySticksActive_.clear(); 380 this->bKeyBindingsActive_ = true; 381 this->bMouseButtonBindingsActive_ = true; 382 for (unsigned int i = 0; i < joySticks_.size(); i++) 383 this->bJoyStickButtonBindingsActive_[i] = true; 384 break; 385 386 case IS_GUI: 387 // FIXME: do stuff 388 break; 389 390 case IS_CONSOLE: 391 this->listenersKeyActive_.clear(); 392 this->listenersMouseActive_.clear(); 393 this->listenersJoySticksActive_.clear(); 394 this->bKeyBindingsActive_ = false; 395 this->bMouseButtonBindingsActive_ = true; 396 for (unsigned int i = 0; i < joySticks_.size(); i++) 397 this->bJoyStickButtonBindingsActive_[i] = true; 398 if (listenersKey_.find("buffer") != listenersKey_.end()) 399 listenersKeyActive_.push_back(listenersKey_["buffer"]); 400 else 368 if (stateRequest_ != IS_CUSTOM) 369 { 370 listenersKeyActive_.clear(); 371 listenersMouseActive_.clear(); 372 listenersJoySticksActive_.clear(); 373 374 switch (stateRequest_) 401 375 { 402 // someone fiddled with the InputBuffer 403 CCOUT(2) << "Error: Cannot redirect input to console, InputBuffer instance missing." << std::endl; 404 this->bKeyBindingsActive_ = true; 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; 389 390 case IS_GUI: 391 // FIXME: do stuff 392 break; 393 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 } 403 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; 405 420 } 406 break; 407 408 case IS_NONE: 409 this->listenersKeyActive_.clear(); 410 this->listenersMouseActive_.clear(); 411 this->listenersJoySticksActive_.clear(); 412 this->bKeyBindingsActive_ = false; 413 this->bMouseButtonBindingsActive_ = false; 414 for (unsigned int i = 0; i < joySticks_.size(); i++) 415 this->bJoyStickButtonBindingsActive_[i] = false; 416 break; 417 418 case IS_CUSTOM: 419 // don't do anything 420 break; 421 422 case IS_UNINIT: 423 // this can't happen 424 break; 425 } 426 state_ = stateRequest_; 421 state_ = stateRequest_; 422 } 427 423 } 428 424 … … 432 428 if (keyboard_) 433 429 keyboard_->capture(); 434 } 435 436 /*void InputManager::_setDefaultState() 437 { 438 this->listenersKeyActive_.clear(); 439 this->listenersMouseActive_.clear(); 440 this->listenersJoyStickActive_.clear(); 441 this->bKeyBindingsActive_ = true; 442 this->bMouseButtonBindingsActive_ = true; 443 this->bJoyStickButtonBindingsActive_ = true; 444 }*/ 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 } 472 } 445 473 446 474 … … 451 479 bool InputManager::keyPressed(const OIS::KeyEvent &e) 452 480 { 453 this->keysDown_.push_back(e.key); 454 455 if (this->bKeyBindingsActive_) 456 { 457 // find the appropriate key binding 458 std::string cmdStr = bindingsKeyPress_[int(e.key)]; 459 if (cmdStr != "") 460 { 461 CommandExecutor::execute(cmdStr); 462 COUT(3) << "Executing command: " << cmdStr << std::endl; 463 } 464 } 465 else 466 { 467 for (std::list<OIS::KeyListener*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++) 468 (*it)->keyPressed(e); 469 } 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++) 483 { 484 if (*it == e.key) 485 { 486 keysDown_.erase(it); 487 break; 488 } 489 } 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 470 495 return true; 471 496 } … … 487 512 } 488 513 489 if (this->bKeyBindingsActive_) 490 { 491 // find the appropriate key binding 492 std::string cmdStr = bindingsKeyRelease_[int(e.key)]; 493 if (cmdStr != "") 494 { 495 CommandExecutor::execute(cmdStr); 496 COUT(3) << "Executing command: " << cmdStr << std::endl; 497 } 498 } 499 else 500 { 501 for (std::list<OIS::KeyListener*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++) 502 (*it)->keyReleased(e); 503 } 514 for (std::list<KeyHandler*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++) 515 (*it)->keyReleased(e); 516 504 517 return true; 505 518 } … … 511 524 bool InputManager::mouseMoved(const OIS::MouseEvent &e) 512 525 { 526 for (std::list<MouseHandler*>::const_iterator it = listenersMouseActive_.begin(); it != listenersMouseActive_.end(); it++) 527 (*it)->mouseMoved(e); 528 513 529 return true; 514 530 } … … 521 537 bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) 522 538 { 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 523 553 return true; 524 554 } … … 531 561 bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) 532 562 { 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 533 576 return true; 534 577 } … … 536 579 bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button) 537 580 { 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 538 599 return true; 539 600 } … … 541 602 bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button) 542 603 { 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 543 621 return true; 544 622 } … … 546 624 bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis) 547 625 { 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 548 631 return true; 549 632 } … … 551 634 bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id) 552 635 { 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 553 641 return true; 554 642 } … … 556 644 bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id) 557 645 { 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 558 651 return true; 559 652 } … … 563 656 // ### Static Interface Methods ### 564 657 // ################################ 658 // ################################ 659 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 565 703 566 704 /** … … 600 738 } 601 739 602 void InputManager::destroy() 603 { 604 _getSingleton()._destroy(); 605 } 606 607 bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight, 608 bool createKeyboard, bool createMouse, bool createJoySticks) 609 { 610 return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight, 611 createKeyboard, createMouse, createJoySticks); 612 } 613 614 /*bool InputManager::initialiseKeyboard() 615 { 616 } 617 618 bool InputManager::initialiseMouse() 619 { 620 } 621 622 bool InputManager::initialiseJoySticks() 623 { 624 }*/ 625 626 bool InputManager::addKeyListener(OIS::KeyListener *listener, const std::string& name) 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) 627 750 { 628 751 if (_getSingleton().listenersKey_.find(name) == _getSingleton().listenersKey_.end()) … … 635 758 } 636 759 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 */ 637 765 bool InputManager::removeKeyListener(const std::string &name) 638 766 { 639 std::map<std::string, OIS::KeyListener*>::iterator it = _getSingleton().listenersKey_.find(name); 767 disableKeyListener(name); 768 std::map<std::string, KeyHandler*>::iterator it = _getSingleton().listenersKey_.find(name); 640 769 if (it != _getSingleton().listenersKey_.end()) 641 770 { … … 647 776 } 648 777 649 OIS::KeyListener* InputManager::getKeyListener(const std::string& name) 650 { 651 std::map<std::string, OIS::KeyListener*>::iterator it = _getSingleton().listenersKey_.find(name); 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) 784 { 785 std::map<std::string, KeyHandler*>::iterator it = _getSingleton().listenersKey_.find(name); 652 786 if (it != _getSingleton().listenersKey_.end()) 653 787 { … … 658 792 } 659 793 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) 800 { 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; 814 } 815 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) 822 { 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 } 839 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) 849 { 850 if (_getSingleton().listenersMouse_.find(name) == _getSingleton().listenersMouse_.end()) 851 { 852 _getSingleton().listenersMouse_[name] = listener; 853 return true; 854 } 855 else 856 return false; 857 } 858 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) 865 { 866 disableMouseListener(name); 867 std::map<std::string, MouseHandler*>::iterator it = _getSingleton().listenersMouse_.find(name); 868 if (it != _getSingleton().listenersMouse_.end()) 869 { 870 _getSingleton().listenersMouse_.erase(it); 871 return true; 872 } 873 else 874 return false; 875 } 876 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) 883 { 884 std::map<std::string, MouseHandler*>::iterator it = _getSingleton().listenersMouse_.find(name); 885 if (it != _getSingleton().listenersMouse_.end()) 886 { 887 return (*it).second; 888 } 889 else 890 return 0; 891 } 892 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 660 1053 }
Note: See TracChangeset
for help on using the changeset viewer.