Changeset 1219 for code/trunk/src/core/InputHandler.cc
- Timestamp:
- May 2, 2008, 10:44:57 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/core/InputHandler.cc
r1214 r1219 34 34 #include "InputHandler.h" 35 35 #include "Debug.h" 36 #include "ConfigValueIncludes.h" 37 #include "CoreIncludes.h" 36 38 #include "util/Convert.h" 37 #include "InputEventListener.h" 38 #include "InputEvent.h" 39 #include "InputManager.h" 39 #include "core/CommandExecutor.h" 40 40 41 41 namespace orxonox 42 42 { 43 43 // ############################### 44 // ### InputHandlerGame###44 // ###### KeyBinder ###### 45 45 // ############################### 46 46 47 47 /** 48 @brief standard constructor 49 */ 50 InputHandlerGame::InputHandlerGame() 51 { 48 @brief Constructor that does as little as necessary. 49 */ 50 KeyBinder::KeyBinder() 51 { 52 RegisterObject(KeyBinder); 53 clearBindings(); 54 55 std::string keyNames[] = { 56 "UNASSIGNED", 57 "ESCAPE", 58 "1", 59 "2", 60 "3", 61 "4", 62 "5", 63 "6", 64 "7", 65 "8", 66 "9", 67 "0", 68 "MINUS", 69 "EQUALS", 70 "BACK", 71 "TAB", 72 "Q", 73 "W", 74 "E", 75 "R", 76 "T", 77 "Y", 78 "U", 79 "I", 80 "O", 81 "P", 82 "LBRACKET", 83 "RBRACKET", 84 "RETURN", 85 "LCONTROL", 86 "A", 87 "S", 88 "D", 89 "F", 90 "G", 91 "H", 92 "J", 93 "K", 94 "L", 95 "SEMICOLON", 96 "APOSTROPHE", 97 "GRAVE", 98 "LSHIFT", 99 "BACKSLASH", 100 "Z", 101 "X", 102 "C", 103 "V", 104 "B", 105 "N", 106 "M", 107 "COMMA", 108 "PERIOD", 109 "SLASH", 110 "RSHIFT", 111 "MULTIPLY", 112 "LMENU", 113 "SPACE", 114 "CAPITAL", 115 "F1", 116 "F2", 117 "F3", 118 "F4", 119 "F5", 120 "F6", 121 "F7", 122 "F8", 123 "F9", 124 "F10", 125 "NUMLOCK", 126 "SCROLL", 127 "NUMPAD7", 128 "NUMPAD8", 129 "NUMPAD9", 130 "SUBTRACT", 131 "NUMPAD4", 132 "NUMPAD5", 133 "NUMPAD6", 134 "ADD", 135 "NUMPAD1", 136 "NUMPAD2", 137 "NUMPAD3", 138 "NUMPAD0", 139 "DECIMAL", 140 "","", 141 "OEM_102", 142 "F11", 143 "F12", 144 "","","","","","","","","","","", 145 "F13", 146 "F14", 147 "F15", 148 "","","","","","","","","","", 149 "KANA", 150 "","", 151 "ABNT_C1", 152 "","","","","", 153 "CONVERT", 154 "", 155 "NOCONVERT", 156 "", 157 "YEN", 158 "ABNT_C2", 159 "","","","","","","","","","","","","","", 160 "NUMPADEQUALS", 161 "","", 162 "PREVTRACK", 163 "AT", 164 "COLON", 165 "UNDERLINE", 166 "KANJI", 167 "STOP", 168 "AX", 169 "UNLABELED", 170 "NEXTTRACK", 171 "","", 172 "NUMPADENTER", 173 "RCONTROL", 174 "","", 175 "MUTE", 176 "CALCULATOR", 177 "PLAYPAUSE", 178 "", 179 "MEDIASTOP", 180 "","","","","","","","","", 181 "VOLUMEDOWN", 182 "", 183 "VOLUMEUP", 184 "", 185 "WEBHOME", 186 "NUMPADCOMMA", 187 "", 188 "DIVIDE", 189 "", 190 "SYSRQ", 191 "RMENU", 192 "","","","","","","","","","","","", 193 "PAUSE", 194 "", 195 "HOME", 196 "UP", 197 "PGUP", 198 "", 199 "LEFT", 200 "", 201 "RIGHT", 202 "", 203 "END", 204 "DOWN", 205 "PGDOWN", 206 "INSERT", 207 "DELETE", 208 "","","","","","","", 209 "LWIN", 210 "RWIN", 211 "APPS", 212 "POWER", 213 "SLEEP", 214 "","","", 215 "WAKE", 216 "", 217 "WEBSEARCH", 218 "WEBFAVORITES", 219 "WEBREFRESH", 220 "WEBSTOP", 221 "WEBFORWARD", 222 "WEBBACK", 223 "MYCOMPUTER", 224 "MAIL", 225 "MEDIASELECT" 226 }; 227 for (int i = 0; i < numberOfKeys_s; i++) 228 keyNames_[i] = keyNames[i]; 229 230 std::string mouseButtonNames[] = { 231 "MouseLeft", "MouseRight", "MouseMiddle", 232 "MouseButton3", "MouseButton4", "MouseButton5", 233 "MouseButton6", "MouseButton7" }; 234 for (int i = 0; i < numberOfMouseButtons_s; i++) 235 mouseButtonNames_[i] = mouseButtonNames[i]; 236 237 for (int i = 0; i < numberOfJoyStickButtons_s; i++) 238 joyStickButtonNames_[i] = "JoyStick" + getConvertedValue<int, std::string>(i); 52 239 } 53 240 … … 55 242 @brief Destructor 56 243 */ 57 InputHandlerGame::~InputHandlerGame() 58 { 59 } 60 61 /** 62 @brief Loads the key bindings from the ini file. 63 Currently, this is just a simple test routine that fills the list with numbers. 64 */ 65 bool InputHandlerGame::loadBindings() 66 { 244 KeyBinder::~KeyBinder() 245 { 246 } 247 248 /** 249 @brief Loader for the key bindings, managed by config values. 250 */ 251 void KeyBinder::setConfigValues() 252 { 253 ConfigValueContainer* cont; 254 std::string modes[] = {"P_", "R_", "H_"}; 255 256 // keys 67 257 for (int i = 0; i < numberOfKeys_s; i++) 68 258 { 69 // simply write the key number (i) in the string 70 this->bindingsKeyPressed_[i] = getConvertedValue<int, std::string>(i); 71 this->bindingsKeyReleased_[i] = getConvertedValue<int, std::string>(i); 72 } 73 return true; 74 } 259 for (int j = 0; j < 3; j++) 260 { 261 cont = getIdentifier()->getConfigValueContainer(modes[j] + keyNames_[i]); 262 if (!cont) 263 { 264 cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), modes[j] + keyNames_[i], ""); 265 getIdentifier()->addConfigValueContainer(modes[j] + keyNames_[i], cont); 266 } 267 switch (j) 268 { 269 // note: the first element in the struct is the string, so the following pointer 270 // arithmetic works. 271 case 0: 272 cont->getValue(&bindingsKeyPress_[i].commandStr); 273 break; 274 case 1: 275 cont->getValue(bindingsKeyRelease_ + i); 276 break; 277 case 2: 278 cont->getValue(bindingsKeyHold_ + i); 279 } 280 } 281 } 282 283 // mouse buttons 284 for (int i = 0; i < numberOfMouseButtons_s; i++) 285 { 286 for (int j = 0; j < 3; j++) 287 { 288 cont = getIdentifier()->getConfigValueContainer(modes[j] + mouseButtonNames_[i]); 289 if (!cont) 290 { 291 cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), modes[j] + mouseButtonNames_[i], ""); 292 getIdentifier()->addConfigValueContainer(modes[j] + mouseButtonNames_[i], cont); 293 } 294 switch (j) 295 { 296 case 0: 297 cont->getValue(bindingsMouseButtonPress_ + i); 298 break; 299 case 1: 300 cont->getValue(bindingsMouseButtonRelease_ + i); 301 break; 302 case 2: 303 cont->getValue(bindingsMouseButtonHold_ + i); 304 } 305 } 306 } 307 308 // joy stick buttons 309 for (int i = 0; i < numberOfJoyStickButtons_s; i++) 310 { 311 for (int j = 0; j < 3; j++) 312 { 313 cont = getIdentifier()->getConfigValueContainer(modes[j] + joyStickButtonNames_[i]); 314 if (!cont) 315 { 316 cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), modes[j] + joyStickButtonNames_[i], ""); 317 getIdentifier()->addConfigValueContainer(modes[j] + joyStickButtonNames_[i], cont); 318 } 319 switch (j) 320 { 321 case 0: 322 cont->getValue(bindingsJoyStickButtonPress_ + i); 323 break; 324 case 1: 325 cont->getValue(bindingsJoyStickButtonRelease_ + i); 326 break; 327 case 2: 328 cont->getValue(bindingsJoyStickButtonHold_ + i); 329 } 330 } 331 } 332 } 333 334 /** 335 @brief Overwrites all bindings with "" 336 */ 337 void KeyBinder::clearBindings() 338 { 339 for (int i = 0; i < numberOfKeys_s; i++) 340 { 341 bindingsKeyPress_ [i].commandStr = ""; 342 bindingsKeyRelease_[i].commandStr = ""; 343 bindingsKeyHold_ [i].commandStr = ""; 344 } 345 for (int i = 0; i < numberOfMouseButtons_s; i++) 346 { 347 bindingsMouseButtonPress_ [i] = ""; 348 bindingsMouseButtonRelease_[i] = ""; 349 bindingsMouseButtonHold_ [i] = ""; 350 } 351 for (int i = 0; i < numberOfJoyStickButtons_s; i++) 352 { 353 bindingsJoyStickButtonPress_ [i] = ""; 354 bindingsJoyStickButtonRelease_[i] = ""; 355 bindingsJoyStickButtonHold_ [i] = ""; 356 } 357 } 358 359 /** 360 @brief Loads the key and button bindings. 361 @return True if loading succeeded. 362 */ 363 bool KeyBinder::loadBindings() 364 { 365 COUT(ORX_DEBUG) << "KeyBinder: Loading key bindings..." << std::endl; 366 367 ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings.ini"); 368 setConfigValues(); 369 370 // evaluate the key bindings 371 // TODO: what if binding is invalid? 372 //for (int i = 0; i < numberOfKeys_s; i++) 373 //{ 374 // if (bindingsKeyPress_[i].commandStr != "") 375 // { 376 // bindingsKeyPress_[i].evaluation = CommandExecutor::evaluate(bindingsKeyPress_[i].commandStr); 377 // bindingsKeyPress_[i].commandStr = bindingsKeyPress_[i].evaluation.getCommandString(); 378 // } 379 //} 380 381 COUT(ORX_DEBUG) << "KeyBinder: Loading key bindings done." << std::endl; 382 return true; 383 } 384 385 bool KeyBinder::executeBinding(KeyBinding& binding) 386 { 387 if (binding.commandStr != "") 388 { 389 //if (binding.commandStr != binding.evaluation.getCommandString()) 390 //{ 391 // // key binding has changed, reevaluate the command string. 392 // binding.evaluation = CommandExecutor::evaluate(binding.commandStr); 393 // binding.commandStr = binding.evaluation.getCommandString(); 394 //} 395 COUT(3) << "Executing command: " << binding.commandStr << std::endl; 396 CommandExecutor::execute(binding.commandStr); 397 } 398 399 return true; 400 } 401 75 402 76 403 /** … … 78 405 @param e Event information 79 406 */ 80 bool InputHandlerGame::keyPressed(const OIS::KeyEvent &e) 81 { 82 if (e.key == OIS::KC_ESCAPE) 83 { 84 InputEvent e = {1, true, 0, 0, 0}; 85 InputHandlerGame::callListeners(e); 86 } 87 else if (e.key == OIS::KC_UNASSIGNED || e.key == OIS::KC_NUMPADENTER) 88 { 89 InputManager::getSingleton().setInputMode(IM_KEYBOARD); 90 } 91 else 92 { 93 // find the appropriate key binding 94 std::string cmdStr = bindingsKeyPressed_[int(e.key)]; 95 //COUT(3) << cmdStr << " pressed" << std::endl; 96 } 407 bool KeyBinder::keyPressed(const OIS::KeyEvent &e) 408 { 409 COUT(3) << "Key: " << e.key << std::endl; 410 // find the appropriate key binding 411 executeBinding(bindingsKeyPress_[int(e.key)]); 412 97 413 return true; 98 414 } … … 102 418 @param e Event information 103 419 */ 104 bool InputHandlerGame::keyReleased(const OIS::KeyEvent &e) 105 { 106 // find the appropriate key binding 107 std::string cmdStr = bindingsKeyReleased_[int(e.key)]; 108 //COUT(3) << cmdStr << " released" << std::endl; 420 bool KeyBinder::keyReleased(const OIS::KeyEvent &e) 421 { 422 // find the appropriate key binding 423 executeBinding(bindingsKeyRelease_[int(e.key)]); 424 425 return true; 426 } 427 428 /** 429 @brief Event handler for the keyHeld Event. 430 @param e Event information 431 */ 432 bool KeyBinder::keyHeld(const OIS::KeyEvent &e) 433 { 434 // find the appropriate key binding 435 executeBinding(bindingsKeyHold_[int(e.key)]); 436 109 437 return true; 110 438 } … … 114 442 @param e Event information 115 443 */ 116 bool InputHandlerGame::mouseMoved(const OIS::MouseEvent &e)444 bool KeyBinder::mouseMoved(const OIS::MouseEvent &e) 117 445 { 118 446 return true; … … 124 452 @param id The ID of the mouse button 125 453 */ 126 bool InputHandlerGame::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) 127 { 454 bool KeyBinder::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) 455 { 456 // find the appropriate key binding 457 std::string cmdStr = bindingsMouseButtonPress_[int(id)]; 458 if (cmdStr != "") 459 { 460 CommandExecutor::execute(cmdStr); 461 COUT(3) << "Executing command: " << cmdStr << std::endl; 462 } 463 128 464 return true; 129 465 } … … 134 470 @param id The ID of the mouse button 135 471 */ 136 bool InputHandlerGame::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) 137 { 138 return true; 139 } 140 141 /** 142 @brief Calls all the objects from classes that derive from InputEventListener. 143 @param evt The input event that occured. 144 */ 145 inline void InputHandlerGame::callListeners(orxonox::InputEvent &evt) 146 { 147 for (Iterator<InputEventListener> it = ObjectList<InputEventListener>::start(); it; ) 148 { 149 if (it->bActive_) 150 (it++)->eventOccured(evt); 151 else 152 it++; 153 } 154 } 155 156 157 // ############################### 158 // ### InputHandlerGUI ### 159 // ############################### 160 161 /** 162 @brief standard constructor 163 */ 164 InputHandlerGUI::InputHandlerGUI() 165 { 166 } 167 168 /** 169 @brief Destructor 170 */ 171 InputHandlerGUI::~InputHandlerGUI() 172 { 173 } 174 175 /** 176 @brief Event handler for the keyPressed Event. 177 @param e Event information 178 */ 179 bool InputHandlerGUI::keyPressed(const OIS::KeyEvent &e) 180 { 181 //CEGUI::System::getSingleton().injectKeyDown( arg.key ); 182 //CEGUI::System::getSingleton().injectChar( arg.text ); 183 return true; 184 } 185 186 /** 187 @brief Event handler for the keyReleased Event. 188 @param e Event information 189 */ 190 bool InputHandlerGUI::keyReleased(const OIS::KeyEvent &e) 191 { 192 //CEGUI::System::getSingleton().injectKeyUp( arg.key ); 193 return true; 194 } 195 196 /** 197 @brief Event handler for the mouseMoved Event. 198 @param e Event information 199 */ 200 bool InputHandlerGUI::mouseMoved(const OIS::MouseEvent &e) 201 { 202 //CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel ); 203 return true; 204 } 205 206 /** 207 @brief Event handler for the mousePressed Event. 472 bool KeyBinder::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) 473 { 474 // find the appropriate key binding 475 std::string cmdStr = bindingsMouseButtonRelease_[int(id)]; 476 if (cmdStr != "") 477 { 478 CommandExecutor::execute(cmdStr); 479 COUT(3) << "Executing command: " << cmdStr << std::endl; 480 } 481 482 return true; 483 } 484 485 /** 486 @brief Event handler for the mouseHeld Event. 208 487 @param e Event information 209 488 @param id The ID of the mouse button 210 489 */ 211 bool InputHandlerGUI::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) 212 { 213 //CEGUI::System::getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id)); 214 return true; 215 } 216 217 /** 218 @brief Event handler for the mouseReleased Event. 219 @param e Event information 220 @param id The ID of the mouse button 221 */ 222 bool InputHandlerGUI::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) 223 { 224 //CEGUI::System::getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id)); 225 return true; 226 } 490 bool KeyBinder::mouseHeld(const OIS::MouseEvent &e, OIS::MouseButtonID id) 491 { 492 // find the appropriate key binding 493 std::string cmdStr = bindingsMouseButtonHold_[int(id)]; 494 if (cmdStr != "") 495 { 496 CommandExecutor::execute(cmdStr); 497 COUT(3) << "Executing command: " << cmdStr << std::endl; 498 } 499 500 return true; 501 } 502 503 bool KeyBinder::buttonPressed(int joyStickID, const OIS::JoyStickEvent &arg, int button) 504 { 505 // find the appropriate key binding 506 std::string cmdStr = bindingsJoyStickButtonPress_[button]; 507 if (cmdStr != "") 508 { 509 CommandExecutor::execute(cmdStr); 510 COUT(3) << "Executing command: " << cmdStr << std::endl; 511 } 512 513 return true; 514 } 515 516 bool KeyBinder::buttonReleased(int joyStickID, const OIS::JoyStickEvent &arg, int button) 517 { 518 // find the appropriate key binding 519 std::string cmdStr = bindingsJoyStickButtonRelease_[button]; 520 if (cmdStr != "") 521 { 522 CommandExecutor::execute(cmdStr); 523 COUT(3) << "Executing command: " << cmdStr << std::endl; 524 } 525 526 return true; 527 } 528 529 bool KeyBinder::buttonHeld(int joyStickID, const OIS::JoyStickEvent &arg, int button) 530 { 531 // find the appropriate key binding 532 std::string cmdStr = bindingsJoyStickButtonHold_[button]; 533 if (cmdStr != "") 534 { 535 CommandExecutor::execute(cmdStr); 536 COUT(3) << "Executing command: " << cmdStr << std::endl; 537 } 538 539 return true; 540 } 541 542 bool KeyBinder::axisMoved(int joyStickID, const OIS::JoyStickEvent &arg, int axis) 543 { 544 return true; 545 } 546 547 bool KeyBinder::sliderMoved(int joyStickID, const OIS::JoyStickEvent &arg, int id) 548 { 549 return true; 550 } 551 552 bool KeyBinder::povMoved(int joyStickID, const OIS::JoyStickEvent &arg, int id) 553 { 554 return true; 555 } 556 557 bool KeyBinder::vector3Moved(int joyStickID, const OIS::JoyStickEvent &arg, int id) 558 { 559 return true; 560 } 561 562 563 564 // ############################### 565 // ### GUIInputHandler ### 566 // ############################### 567 568 ///** 569 // @brief standard constructor 570 //*/ 571 //GUIInputHandler::GUIInputHandler() 572 //{ 573 //} 574 575 ///** 576 // @brief Destructor 577 //*/ 578 //GUIInputHandler::~GUIInputHandler() 579 //{ 580 //} 581 582 ///** 583 // @brief Event handler for the keyPressed Event. 584 // @param e Event information 585 //*/ 586 //bool GUIInputHandler::keyPressed(const OIS::KeyEvent &e) 587 //{ 588 ////CEGUI::System::getSingleton().injectKeyDown( arg.key ); 589 ////CEGUI::System::getSingleton().injectChar( arg.text ); 590 // return true; 591 //} 592 593 ///** 594 // @brief Event handler for the keyReleased Event. 595 // @param e Event information 596 //*/ 597 //bool GUIInputHandler::keyReleased(const OIS::KeyEvent &e) 598 //{ 599 ////CEGUI::System::getSingleton().injectKeyUp( arg.key ); 600 // return true; 601 //} 602 603 ///** 604 // @brief Event handler for the mouseMoved Event. 605 // @param e Event information 606 //*/ 607 //bool GUIInputHandler::mouseMoved(const OIS::MouseEvent &e) 608 //{ 609 ////CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel ); 610 // return true; 611 //} 612 613 ///** 614 // @brief Event handler for the mousePressed Event. 615 // @param e Event information 616 // @param id The ID of the mouse button 617 //*/ 618 //bool GUIInputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) 619 //{ 620 ////CEGUI::System::getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id)); 621 // return true; 622 //} 623 624 ///** 625 // @brief Event handler for the mouseReleased Event. 626 // @param e Event information 627 // @param id The ID of the mouse button 628 //*/ 629 //bool GUIInputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) 630 //{ 631 ////CEGUI::System::getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id)); 632 // return true; 633 //} 227 634 228 635 }
Note: See TracChangeset
for help on using the changeset viewer.