InputManagerCommands.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <OgreStringConverter.h>
  16. #include "Console.h"
  17. #include "ConfigCmdManager.h"
  18. #include "ConfigVarManager.h"
  19. #include "Logger.h"
  20. #include "Utilites.h"
  21. /**
  22. * Parses a list of keys to retrieve their keycodes.
  23. *
  24. * @param string[in] Every character in this string will be processed, and the
  25. * correspondant keycodes will be added to 'key_codes'.
  26. * @param key_codes[out] Every keycode assigned to a character in 'string' will
  27. * be added here, even repeated ones.
  28. * @return True if there were no errors, false if at least one of the
  29. * characters in 'string' doesn't have an associated keycode. Every case wil be
  30. * logged.
  31. */
  32. bool ParseKeys(const Ogre::String& string, ButtonList& key_codes){
  33. Ogre::StringVector keys = Ogre::StringUtil::split(string, "+");
  34. for (unsigned int i = 0; i < keys.size(); ++ i)
  35. key_codes.push_back(StringToKey(keys[i]));
  36. bool fail = false;
  37. for (unsigned int i = 0; i < key_codes.size(); ++ i){
  38. if (key_codes[i] == OIS::KC_UNASSIGNED){
  39. LOG_ERROR(
  40. "Failed to parse key string \"" + string
  41. + "\". Can't recognize key " + Ogre::StringConverter::toString(i)
  42. );
  43. fail = true;
  44. }
  45. }
  46. return fail;
  47. }
  48. /**
  49. * Binds a key combination to a command.
  50. *
  51. * @param params[in] Binding parameters. Exactly three are required. The first
  52. * one is the binding command name and it's not evaluated here. The second one
  53. * must be one or more keycodes. The third one is the command string. If more
  54. * or less than three parameter are passed, a usage string will be printed
  55. * instead, and nothing will be done.
  56. * @todo How are keycodes separated? spaces or '+'?
  57. */
  58. void CmdBind(const Ogre::StringVector& params){
  59. if (params.size() != 3){
  60. Console::getSingleton().AddTextToOutput(
  61. "Usage: /bind <key1>+[key2]+[key3] \"<command line>\""
  62. );
  63. return;
  64. }
  65. ButtonList key_codes;
  66. if (ParseKeys(params[1], key_codes) == false){
  67. Ogre::StringVector params_cmd = StringTokenise(params[2]);
  68. // Handle command
  69. ConfigCmd* cmd = ConfigCmdManager::getSingleton().Find(params_cmd[0]);
  70. if (cmd != NULL){
  71. InputManager::getSingleton().BindCommand(
  72. cmd, params_cmd, key_codes
  73. );
  74. LOG_TRIVIAL(
  75. "Bind \"" + params[1] + "\" to command \"" + params[2] + "\"."
  76. );
  77. }
  78. else{
  79. LOG_ERROR(
  80. "Can't find command \"" + params_cmd[0]
  81. + "\" in bind command \"" + params[2] + "\"."
  82. );
  83. }
  84. }
  85. }
  86. /**
  87. * Binds a key combination to a game event.
  88. *
  89. * @param params[in] Binding parameters. Exactly three are required. The first
  90. * one is the binding command name and it's not evaluated here. The second one
  91. * must be one or more keycodes. The third one is the game event string. If
  92. * more or less than three parameter are passed, a usage string will be printed
  93. * instead, and nothing will be done.
  94. * @todo How are keycodes separated? spaces or '+'?
  95. */
  96. void CmdBindGameEvent(const Ogre::StringVector& params){
  97. if (params.size() != 3){
  98. Console::getSingleton().AddTextToOutput(
  99. "Usage: /game_bind <key1>+[key2]+[key3] \"<game event>\""
  100. );
  101. return;
  102. }
  103. ButtonList key_codes;
  104. if (ParseKeys(params[1], key_codes) == false){
  105. InputManager::getSingleton().BindGameEvent(params[2], key_codes);
  106. LOG_TRIVIAL(
  107. "Bind \"" + params[1] + "\" to game event \"" + params[2] + "\"."
  108. );
  109. }
  110. }
  111. // TODO: Move this to InpuManager.cpp?
  112. void InputManager::InitCmd(){
  113. ConfigCmdManager::getSingleton().AddCommand(
  114. "bind", "Bind command to keys", "", CmdBind, NULL
  115. );
  116. ConfigCmdManager::getSingleton().AddCommand(
  117. "bind_game_event", "Bind game event to keys", "", CmdBindGameEvent, NULL
  118. );
  119. }