Changeset 1656 for code/branches/gui/src/core/input
- Timestamp:
- Aug 10, 2008, 9:43:50 PM (16 years ago)
- Location:
- code/branches/gui/src/core/input
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/gui/src/core/input/InputManager.cc
r1653 r1656 45 45 #include "core/ConfigValueIncludes.h" 46 46 #include "core/Debug.h" 47 #include "core/Exception.h" 47 48 #include "core/CommandExecutor.h" 48 49 #include "core/ConsoleCommand.h" … … 67 68 using namespace InputDevice; 68 69 70 /** 71 @brief 72 Defines the |= operator for easier use. 73 */ 74 inline InputManager::InputManagerState operator|=(InputManager::InputManagerState& lval, 75 InputManager::InputManagerState rval) 76 { 77 return (lval = (InputManager::InputManagerState)(lval | rval)); 78 } 79 80 /** 81 @brief 82 Defines the &= operator for easier use. 83 */ 84 inline InputManager::InputManagerState operator&=(InputManager::InputManagerState& lval, int rval) 85 { 86 return (lval = (InputManager::InputManagerState)(lval & rval)); 87 } 88 69 89 // ############################################################ 70 90 // ##### Initialisation ##### … … 83 103 , joySticksSize_(0) 84 104 , devicesNum_(0) 105 , windowHnd_(0) 106 , internalState_(Uninitialised) 85 107 , stateDetector_(0) 86 108 , stateCalibrator_(0) … … 105 127 @param windowHeight 106 128 The height of the render window 107 */ 108 bool InputManager::initialise(const size_t windowHnd, int windowWidth, int windowHeight, 109 bool createKeyboard, bool createMouse, bool createJoySticks) 110 { 111 if (inputSystem_ == 0) 129 @param joyStickSupport 130 Whether or not to load the joy sticks as well 131 */ 132 void InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport) 133 { 134 if (internalState_ == Uninitialised) 112 135 { 113 136 CCOUT(3) << "Initialising Input System..." << std::endl; 114 137 CCOUT(4) << "Initialising OIS components..." << std::endl; 115 138 139 // store handle internally so we can reload OIS 140 windowHnd_ = windowHnd; 141 116 142 OIS::ParamList paramList; 117 143 std::ostringstream windowHndStr; 118 144 119 145 // Fill parameter list 120 windowHndStr << (unsigned int)windowHnd ;146 windowHndStr << (unsigned int)windowHnd_; 121 147 paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); 122 148 //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE"))); … … 126 152 //#endif 127 153 128 try 129 { 130 inputSystem_ = OIS::InputManager::createInputSystem(paramList); 131 CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl; 132 } 133 catch (OIS::Exception ex) 134 { 135 CCOUT(ORX_ERROR) << "Error: Failed creating an OIS input system." 136 << "OIS message: \"" << ex.eText << "\"" << std::endl; 137 inputSystem_ = 0; 138 return false; 139 } 140 141 if (createKeyboard) 142 _initialiseKeyboard(); 143 144 if (createMouse) 145 _initialiseMouse(); 146 147 if (createJoySticks) 154 inputSystem_ = OIS::InputManager::createInputSystem(paramList); 155 CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl; 156 157 _initialiseKeyboard(); 158 159 _initialiseMouse(); 160 161 if (joyStickSupport) 148 162 _initialiseJoySticks(); 149 150 // set all the std::vector list sizes now that the devices have been created151 _redimensionLists();152 163 153 164 // Set mouse/joystick region 154 165 if (mouse_) 155 {156 166 setWindowExtents(windowWidth, windowHeight); 157 } 167 168 // clear all buffers 169 _clearBuffers(); 158 170 159 171 CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl; … … 178 190 _updateActiveStates(); 179 191 192 internalState_ = Ready; 193 180 194 CCOUT(3) << "Initialising complete." << std::endl; 181 195 } … … 184 198 CCOUT(2) << "Warning: OIS compoments already initialised, skipping" << std::endl; 185 199 } 186 return true;187 200 } 188 201 … … 193 206 False if keyboard stays uninitialised, true otherwise. 194 207 */ 195 boolInputManager::_initialiseKeyboard()208 void InputManager::_initialiseKeyboard() 196 209 { 197 210 if (keyboard_ != 0) 198 211 { 199 212 CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl; 200 return true; 201 } 202 try 203 { 204 if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0) 205 { 206 keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true); 207 // register our listener in OIS. 208 keyboard_->setEventCallback(this); 209 // note: OIS will not detect keys that have already been down when the keyboard was created. 210 CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl; 211 return true; 212 } 213 else 214 { 215 CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl; 216 return false; 217 } 218 } 219 catch (OIS::Exception ex) 220 { 221 CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n" 222 << "OIS error message: \"" << ex.eText << "\"" << std::endl; 223 keyboard_ = 0; 224 return false; 213 return; 214 } 215 if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0) 216 { 217 keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true); 218 // register our listener in OIS. 219 keyboard_->setEventCallback(this); 220 // note: OIS will not detect keys that have already been down when the keyboard was created. 221 CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl; 222 } 223 else 224 { 225 ThrowException(InitialisationFailed, "No keyboard found!"); 225 226 } 226 227 } … … 232 233 False if mouse stays uninitialised, true otherwise. 233 234 */ 234 boolInputManager::_initialiseMouse()235 void InputManager::_initialiseMouse() 235 236 { 236 237 if (mouse_ != 0) 237 238 { 238 239 CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl; 239 return true;240 return; 240 241 } 241 242 try … … 247 248 mouse_->setEventCallback(this); 248 249 CCOUT(ORX_DEBUG) << "Created OIS mouse" << std::endl; 249 return true;250 250 } 251 251 else 252 252 { 253 253 CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl; 254 return false;255 254 } 256 255 } … … 260 259 << "OIS error message: \"" << ex.eText << "\"" << std::endl; 261 260 mouse_ = 0; 262 return false;263 261 } 264 262 } … … 270 268 False joy stick stay uninitialised, true otherwise. 271 269 */ 272 boolInputManager::_initialiseJoySticks()270 void InputManager::_initialiseJoySticks() 273 271 { 274 272 if (joySticksSize_ > 0) 275 273 { 276 274 CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl; 277 return true; 278 } 279 bool success = false; 275 return; 276 } 280 277 if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0) 281 278 { … … 290 287 // register our listener in OIS. 291 288 stig->setEventCallback(this); 292 success = true;293 289 } 294 290 catch (OIS::Exception ex) … … 301 297 else 302 298 { 303 CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl; 304 return false; 305 } 306 return success; 299 //CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl; 300 } 301 _redimensionLists(); 307 302 } 308 303 … … 412 407 InputManager::~InputManager() 413 408 { 414 if (in putSystem_)409 if (internalState_ != Uninitialised) 415 410 { 416 411 try … … 429 424 (*rit).second->onLeave(); 430 425 } 431 //activeStates_.clear();432 //_updateActiveStates();433 426 434 427 // destroy all input states 435 428 while (inputStatesByPriority_.size() > 0) 436 {437 429 _destroyState((*inputStatesByPriority_.rbegin()).second); 438 }439 440 //stateEmpty_ = 0;441 //stateCalibrator_ = 0;442 //stateDetector_ = 0;443 430 444 431 // destroy the devices … … 447 434 _destroyJoySticks(); 448 435 449 // 0 joy sticks now450 //_redimensionLists();451 452 436 OIS::InputManager::destroyInputSystem(inputSystem_); 453 //inputSystem_ = 0;454 437 455 438 CCOUT(3) << "Destroying done." << std::endl; … … 457 440 catch (OIS::Exception& ex) 458 441 { 459 CCOUT(1) << "An exception has occured while destroying:\n" << ex.what() << std::endl; 442 CCOUT(1) << "An exception has occured while destroying:\n" << ex.what() 443 << "This could lead to a possible memory/resource leak!" << std::endl; 460 444 } 461 445 } … … 468 452 void InputManager::_destroyKeyboard() 469 453 { 454 assert(inputSystem_); 470 455 if (keyboard_) 471 456 inputSystem_->destroyInputObject(keyboard_); 472 457 keyboard_ = 0; 473 keysDown_.clear();474 458 CCOUT(4) << "Keyboard destroyed." << std::endl; 475 459 } … … 481 465 void InputManager::_destroyMouse() 482 466 { 467 assert(inputSystem_); 483 468 if (mouse_) 484 469 inputSystem_->destroyInputObject(mouse_); 485 470 mouse_ = 0; 486 mouseButtonsDown_.clear();487 471 CCOUT(4) << "Mouse destroyed." << std::endl; 488 472 } … … 496 480 if (joySticksSize_ > 0) 497 481 { 498 // note: inputSystem_ can never be 0, or else the code is mistaken482 assert(inputSystem_); 499 483 for (unsigned int i = 0; i < joySticksSize_; i++) 500 484 if (joySticks_[i] != 0) … … 520 504 } 521 505 506 void InputManager::_clearBuffers() 507 { 508 keysDown_.clear(); 509 keyboardModifiers_ = 0; 510 mouseButtonsDown_.clear(); 511 for (unsigned int i = 0; i < joySticksSize_; ++i) 512 { 513 joyStickButtonsDown_[i].clear(); 514 for (int j = 0; j < 4; ++j) 515 { 516 sliderStates_[i].sliderStates[j].x = 0; 517 sliderStates_[i].sliderStates[j].y = 0; 518 povStates_[i][j] = 0; 519 } 520 } 521 } 522 523 524 // ############################################################ 525 // ##### Reloading ##### 526 // ########## ########## 527 // ############################################################ 528 529 /** 530 @brief 531 Public interface. Only reloads immediately if the call stack doesn't 532 include the tick() method. 533 @param joyStickSupport 534 Whether or not to initialise joy sticks as well. 535 */ 536 void InputManager::reloadInputSystem(bool joyStickSupport) 537 { 538 if (internalState_ & Ticking) 539 { 540 // We cannot destroy OIS right now, because reload was probably 541 // caused by a user clicking on a GUI item. The backtrace would then 542 // include an OIS method. So it would be a very bad thing to destroy it.. 543 internalState_ |= ReloadRequest; 544 // Misuse of internalState_: We can easily store the joyStickSupport bool. 545 // use Uninitialised as 0 value in order to use the overloaded |= operator 546 internalState_ |= joyStickSupport ? JoyStickSupport : Uninitialised; 547 } 548 else if (internalState_ == Ready) 549 { 550 _reload(joyStickSupport); 551 } 552 else 553 { 554 CCOUT(2) << "Warning: Cannot reload OIS. May not yet be initialised or" 555 << "joy sticks are currently calibrating." << std::endl; 556 } 557 } 558 559 /** 560 @brief 561 Internal reload method. Destroys the OIS devices and loads them again. 562 */ 563 void InputManager::_reload(bool joyStickSupport) 564 { 565 try 566 { 567 CCOUT(3) << "Reloading ..." << std::endl; 568 569 // Save mouse clipping size 570 int mouseWidth = mouse_->getMouseState().width; 571 int mouseHeight = mouse_->getMouseState().height; 572 573 internalState_ = Uninitialised; 574 // destroy the devices 575 _destroyKeyboard(); 576 _destroyMouse(); 577 _destroyJoySticks(); 578 579 OIS::InputManager::destroyInputSystem(inputSystem_); 580 inputSystem_ = 0; 581 582 // clear all buffers containing input information 583 _clearBuffers(); 584 585 initialise(windowHnd_, mouseWidth, mouseHeight, joyStickSupport); 586 587 CCOUT(3) << "Reloading done." << std::endl; 588 } 589 catch (OIS::Exception& ex) 590 { 591 CCOUT(1) << "An exception has occured while reloading:\n" << ex.what() << std::endl; 592 } 593 } 522 594 523 595 // ############################################################ … … 534 606 void InputManager::tick(float dt) 535 607 { 536 if (in putSystem_ == 0)608 if (internalState_ == Uninitialised) 537 609 return; 610 else if (internalState_ & ReloadRequest) 611 { 612 _reload(internalState_ & JoyStickSupport); 613 internalState_ &= ~ReloadRequest; 614 internalState_ &= ~JoyStickSupport; 615 } 616 internalState_ |= Ticking; 538 617 539 618 // check for states to leave (don't use unsigned int!) … … 594 673 activeStatesTicked_[i]->tickInput(dt); 595 674 } 675 676 internalState_ &= ~Ready; 596 677 } 597 678 -
code/branches/gui/src/core/input/InputManager.h
r1653 r1656 55 55 { 56 56 public: 57 int operator[](unsigned int index) { return povStates[index]; }57 int& operator[](unsigned int index) { return povStates[index]; } 58 58 int povStates[4]; 59 59 }; … … 90 90 91 91 public: 92 enum InputManagerState 93 { 94 Uninitialised = 0, 95 Ready = 1, 96 Ticking = 2, 97 Calibrating = 4, 98 ReloadRequest = 8, 99 JoyStickSupport = 16 // used with ReloadRequest to store a bool 100 }; 101 92 102 InputManager (); 93 103 ~InputManager(); 94 104 95 bool initialise(const size_t windowHnd, int windowWidth, int windowHeight, 96 bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false); 105 void initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport = true); 106 107 void reloadInputSystem(bool joyStickSupport = true); 97 108 98 109 int numberOfKeyboards() { return keyboard_ ? 1 : 0; } … … 134 145 135 146 // Intenal methods 136 bool_initialiseKeyboard();137 bool_initialiseMouse();138 bool_initialiseJoySticks();147 void _initialiseKeyboard(); 148 void _initialiseMouse(); 149 void _initialiseJoySticks(); 139 150 void _redimensionLists(); 140 151 … … 143 154 void _destroyJoySticks(); 144 155 void _destroyState(InputState* state); 156 void _clearBuffers(); 157 158 void _reload(bool joyStickSupport); 145 159 146 160 void _completeCalibration(); … … 177 191 unsigned int joySticksSize_; 178 192 unsigned int devicesNum_; 193 size_t windowHnd_; //!< Render window handle 194 InputManagerState internalState_; //!< Current internal state 179 195 180 196 // some internally handled states
Note: See TracChangeset
for help on using the changeset viewer.