Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

CommandBind.cpp 6.8KB

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