InputManagerCommands.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <OgreStringConverter.h>
  2. #include "Console.h"
  3. #include "ConfigCmdManager.h"
  4. #include "ConfigVarManager.h"
  5. #include "Logger.h"
  6. #include "Utilites.h"
  7. bool
  8. ParseKeys( const Ogre::String& string, ButtonList& key_codes )
  9. {
  10. Ogre::StringVector keys = Ogre::StringUtil::split( string, "+" );
  11. for( unsigned int i = 0; i < keys.size(); ++i )
  12. {
  13. key_codes.push_back( StringToKey( keys[ i ] ) );
  14. }
  15. bool fail = false;
  16. for( unsigned int i = 0; i < key_codes.size(); ++i )
  17. {
  18. if( key_codes[ i ] == OIS::KC_UNASSIGNED )
  19. {
  20. LOG_ERROR( "Failed to parse key string \"" + string + "\". Can't recognize key " + Ogre::StringConverter::toString( i ) );
  21. fail = true;
  22. }
  23. }
  24. return fail;
  25. }
  26. void
  27. CmdBind( const Ogre::StringVector& params )
  28. {
  29. if( params.size() != 3 )
  30. {
  31. Console::getSingleton().AddTextToOutput( "Usage: /bind <key1>+[key2]+[key3] \"<command line>\"" );
  32. return;
  33. }
  34. ButtonList key_codes;
  35. if( ParseKeys( params[ 1 ], key_codes ) == false )
  36. {
  37. Ogre::StringVector params_cmd = StringTokenise( params[ 2 ] );
  38. // handle command
  39. ConfigCmd* cmd = ConfigCmdManager::getSingleton().Find( params_cmd[ 0 ] );
  40. if( cmd != NULL )
  41. {
  42. InputManager::getSingleton().BindCommand( cmd, params_cmd, key_codes );
  43. LOG_TRIVIAL( "Bind \"" + params[ 1 ] + "\" to command \"" + params[ 2 ] + "\"." );
  44. }
  45. else
  46. {
  47. LOG_ERROR( "Can't find command \"" + params_cmd[ 0 ] + "\" in bind command \"" + params[ 2 ] + "\"." );
  48. }
  49. }
  50. }
  51. void
  52. CmdBindGameEvent( const Ogre::StringVector& params )
  53. {
  54. if( params.size() != 3 )
  55. {
  56. Console::getSingleton().AddTextToOutput( "Usage: /game_bind <key1>+[key2]+[key3] \"<game event>\"" );
  57. return;
  58. }
  59. ButtonList key_codes;
  60. if( ParseKeys( params[ 1 ], key_codes ) == false )
  61. {
  62. InputManager::getSingleton().BindGameEvent( params[ 2 ], key_codes );
  63. LOG_TRIVIAL( "Bind \"" + params[ 1 ] + "\" to game event \"" + params[ 2 ] + "\"." );
  64. }
  65. }
  66. void
  67. InputManager::InitCmd()
  68. {
  69. ConfigCmdManager::getSingleton().AddCommand( "bind", "Bind command to keys", "", CmdBind, NULL );
  70. ConfigCmdManager::getSingleton().AddCommand( "bind_game_event", "Bind game event to keys", "", CmdBindGameEvent, NULL );
  71. }