Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandBind.cpp 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*!
  2. * \file src/commands/CommandBind.cpp
  3. * \brief Bind Command
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Log.h"
  9. #include "RunTime.h"
  10. #include "commands/CommandBind.h"
  11. std::string CommandBind::name() {
  12. return "bind";
  13. }
  14. std::string CommandBind::brief() {
  15. return "bind a keyboard/mouse action";
  16. }
  17. void CommandBind::printHelp() {
  18. Log::get(LOG_USER) << "bind-Command Usage:" << Log::endl;
  19. Log::get(LOG_USER) << " bind ACTION KEY" << Log::endl;
  20. Log::get(LOG_USER) << "Available Actions:" << Log::endl;
  21. Log::get(LOG_USER) << " menu" << Log::endl;
  22. Log::get(LOG_USER) << " debug" << Log::endl;
  23. Log::get(LOG_USER) << " forward" << Log::endl;
  24. Log::get(LOG_USER) << " backward" << Log::endl;
  25. Log::get(LOG_USER) << " left" << Log::endl;
  26. Log::get(LOG_USER) << " right" << Log::endl;
  27. Log::get(LOG_USER) << " jump" << Log::endl;
  28. Log::get(LOG_USER) << " crouch" << Log::endl;
  29. Log::get(LOG_USER) << " use" << Log::endl;
  30. Log::get(LOG_USER) << " holster" << Log::endl;
  31. Log::get(LOG_USER) << " walk" << Log::endl;
  32. Log::get(LOG_USER) << "Key-Format:" << Log::endl;
  33. Log::get(LOG_USER) << " 'a' or '1' for character/number keys" << Log::endl;
  34. Log::get(LOG_USER) << " \"leftctrl\" for symbols and special keys" << Log::endl;
  35. }
  36. int CommandBind::execute(std::istream& args) {
  37. std::string a, b;
  38. if (!(args >> a >> b)) {
  39. Log::get(LOG_USER) << "Invalid use of bind-command" << Log::endl;
  40. return -1;
  41. } else {
  42. ActionEvents e = stringToActionEvent(a);
  43. if (e == ActionEventCount) {
  44. Log::get(LOG_USER) << "bind-Error: Unknown action (" << a << ")" << Log::endl;
  45. return -2;
  46. }
  47. KeyboardButton c = stringToKeyboardButton(b);
  48. if (c == unknownKey) {
  49. Log::get(LOG_USER) << "bind-Error: Unknown key (" << b << ")" << Log::endl;
  50. return -3;
  51. }
  52. RunTime::setKeyBinding(e, c);
  53. return 0;
  54. }
  55. }
  56. ActionEvents CommandBind::stringToActionEvent(std::string action) {
  57. if (action == "menu") {
  58. return menuAction;
  59. } else if (action == "debug") {
  60. return debugAction;
  61. } else if (action == "console") {
  62. return consoleAction;
  63. } else if (action == "forward") {
  64. return forwardAction;
  65. } else if (action == "backward") {
  66. return backwardAction;
  67. } else if (action == "left") {
  68. return leftAction;
  69. } else if (action == "right") {
  70. return rightAction;
  71. } else if (action == "jump") {
  72. return jumpAction;
  73. } else if (action == "crouch") {
  74. return crouchAction;
  75. } else if (action == "use") {
  76. return useAction;
  77. } else if (action == "holster") {
  78. return holsterAction;
  79. } else if (action == "walk") {
  80. return walkAction;
  81. } else {
  82. return ActionEventCount;
  83. }
  84. }
  85. KeyboardButton CommandBind::stringToKeyboardButton(std::string key) {
  86. if ((key.length() == 3) && (key[0] == '\'') && (key[2] == '\'')) {
  87. // Literal character like w, a, s, d, 0, 1...
  88. char c = key[1];
  89. if (((c >= '0') && (c <= '9'))
  90. || ((c >= 'a') && (c <= 'z')))
  91. return (KeyboardButton)c;
  92. } else if ((key.length() >= 3) && (key[0] == '\"') && (key[key.length() - 1] == '\"')) {
  93. // Special characters like tilde, esc, quote...
  94. key.erase(key.length() - 1); // Delete " at end
  95. key.erase(0, 1); // Delete " at beginning
  96. if (key == "quote") {
  97. return quoteKey;
  98. } else if (key == "backslash") {
  99. return backslashKey;
  100. } else if (key == "backspace") {
  101. return backspaceKey;
  102. } else if (key == "capslock") {
  103. return capslockKey;
  104. } else if (key == "comma") {
  105. return commaKey;
  106. } else if (key == "del") {
  107. return delKey;
  108. } else if (key == "up") {
  109. return upKey;
  110. } else if (key == "down") {
  111. return downKey;
  112. } else if (key == "left") {
  113. return leftKey;
  114. } else if (key == "right") {
  115. return rightKey;
  116. } else if (key == "end") {
  117. return endKey;
  118. } else if (key == "equals") {
  119. return equalsKey;
  120. } else if (key == "escape") {
  121. return escapeKey;
  122. } else if (key == "f1") {
  123. return f1Key;
  124. } else if (key == "f2") {
  125. return f2Key;
  126. } else if (key == "f3") {
  127. return f3Key;
  128. } else if (key == "f4") {
  129. return f4Key;
  130. } else if (key == "f5") {
  131. return f5Key;
  132. } else if (key == "f6") {
  133. return f6Key;
  134. } else if (key == "f7") {
  135. return f7Key;
  136. } else if (key == "f8") {
  137. return f8Key;
  138. } else if (key == "f9") {
  139. return f9Key;
  140. } else if (key == "f10") {
  141. return f10Key;
  142. } else if (key == "f11") {
  143. return f11Key;
  144. } else if (key == "f12") {
  145. return f12Key;
  146. } else if (key == "backquote") {
  147. return backquoteKey;
  148. } else if (key == "home") {
  149. return homeKey;
  150. } else if (key == "insert") {
  151. return insertKey;
  152. } else if (key == "leftalt") {
  153. return leftaltKey;
  154. } else if (key == "leftctrl") {
  155. return leftctrlKey;
  156. } else if (key == "leftbracket") {
  157. return leftbracketKey;
  158. } else if (key == "leftgui") {
  159. return leftguiKey;
  160. } else if (key == "leftshift") {
  161. return leftshiftKey;
  162. } else if (key == "minus") {
  163. return minusKey;
  164. } else if (key == "numlock") {
  165. return numlockKey;
  166. } else if (key == "pagedown") {
  167. return pagedownKey;
  168. } else if (key == "pageup") {
  169. return pageupKey;
  170. } else if (key == "pause") {
  171. return pauseKey;
  172. } else if (key == "dot") {
  173. return dotKey;
  174. } else if (key == "rightalt") {
  175. return rightaltKey;
  176. } else if (key == "rightctrl") {
  177. return rightctrlKey;
  178. } else if (key == "enter") {
  179. return enterKey;
  180. } else if (key == "rightgui") {
  181. return rightguiKey;
  182. } else if (key == "rightbracket") {
  183. return rightbracketKey;
  184. } else if (key == "rightshift") {
  185. return rightshiftKey;
  186. } else if (key == "scrolllock") {
  187. return scrolllockKey;
  188. } else if (key == "semicolon") {
  189. return semicolonKey;
  190. } else if (key == "slash") {
  191. return slashKey;
  192. } else if (key == "space") {
  193. return spaceKey;
  194. } else if (key == "tab") {
  195. return tabKey;
  196. } else if (key == "leftmouse") {
  197. return leftmouseKey;
  198. } else if (key == "middlemouse") {
  199. return middlemouseKey;
  200. } else if (key == "rightmouse") {
  201. return rightmouseKey;
  202. } else if (key == "fourthmouse") {
  203. return fourthmouseKey;
  204. } else if (key == "fifthmouse") {
  205. return fifthmouseKey;
  206. }
  207. }
  208. return unknownKey;
  209. }