ShortcutHandler.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * ShortcutHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "ShortcutHandler.h"
  12. #include "Shortcut.h"
  13. #include <SDL_keycode.h>
  14. std::vector<EShortcut> ShortcutHandler::translateKeycode(SDL_Keycode key) const
  15. {
  16. static const std::multimap<SDL_Keycode, EShortcut> keyToShortcut = {
  17. {SDLK_RETURN, EShortcut::GLOBAL_ACCEPT },
  18. {SDLK_KP_ENTER, EShortcut::GLOBAL_ACCEPT },
  19. {SDLK_ESCAPE, EShortcut::GLOBAL_CANCEL },
  20. {SDLK_RETURN, EShortcut::GLOBAL_RETURN },
  21. {SDLK_KP_ENTER, EShortcut::GLOBAL_RETURN },
  22. {SDLK_ESCAPE, EShortcut::GLOBAL_RETURN },
  23. {SDLK_F4, EShortcut::GLOBAL_FULLSCREEN },
  24. {SDLK_BACKSPACE, EShortcut::GLOBAL_BACKSPACE },
  25. {SDLK_TAB, EShortcut::GLOBAL_MOVE_FOCUS },
  26. {SDLK_o, EShortcut::GLOBAL_OPTIONS },
  27. {SDLK_LEFT, EShortcut::MOVE_LEFT },
  28. {SDLK_RIGHT, EShortcut::MOVE_RIGHT },
  29. {SDLK_UP, EShortcut::MOVE_UP },
  30. {SDLK_DOWN, EShortcut::MOVE_DOWN },
  31. {SDLK_HOME, EShortcut::MOVE_FIRST },
  32. {SDLK_END, EShortcut::MOVE_LAST },
  33. {SDLK_PAGEUP, EShortcut::MOVE_PAGE_UP },
  34. {SDLK_PAGEDOWN, EShortcut::MOVE_PAGE_DOWN },
  35. {SDLK_1, EShortcut::SELECT_INDEX_1 },
  36. {SDLK_2, EShortcut::SELECT_INDEX_2 },
  37. {SDLK_3, EShortcut::SELECT_INDEX_3 },
  38. {SDLK_4, EShortcut::SELECT_INDEX_4 },
  39. {SDLK_5, EShortcut::SELECT_INDEX_5 },
  40. {SDLK_6, EShortcut::SELECT_INDEX_6 },
  41. {SDLK_7, EShortcut::SELECT_INDEX_7 },
  42. {SDLK_8, EShortcut::SELECT_INDEX_8 },
  43. {SDLK_n, EShortcut::MAIN_MENU_NEW_GAME },
  44. {SDLK_l, EShortcut::MAIN_MENU_LOAD_GAME },
  45. {SDLK_h, EShortcut::MAIN_MENU_HIGH_SCORES },
  46. {SDLK_c, EShortcut::MAIN_MENU_CREDITS },
  47. {SDLK_q, EShortcut::MAIN_MENU_QUIT },
  48. {SDLK_b, EShortcut::MAIN_MENU_BACK },
  49. {SDLK_s, EShortcut::MAIN_MENU_SINGLEPLAYER },
  50. {SDLK_m, EShortcut::MAIN_MENU_MULTIPLAYER },
  51. {SDLK_c, EShortcut::MAIN_MENU_CAMPAIGN },
  52. {SDLK_t, EShortcut::MAIN_MENU_TUTORIAL },
  53. {SDLK_s, EShortcut::MAIN_MENU_CAMPAIGN_SOD },
  54. {SDLK_r, EShortcut::MAIN_MENU_CAMPAIGN_ROE },
  55. {SDLK_a, EShortcut::MAIN_MENU_CAMPAIGN_AB },
  56. {SDLK_c, EShortcut::MAIN_MENU_CAMPAIGN_CUSTOM },
  57. {SDLK_b, EShortcut::LOBBY_BEGIN_GAME },
  58. {SDLK_RETURN, EShortcut::LOBBY_BEGIN_GAME },
  59. {SDLK_KP_ENTER, EShortcut::LOBBY_BEGIN_GAME },
  60. {SDLK_l, EShortcut::LOBBY_LOAD_GAME },
  61. {SDLK_RETURN, EShortcut::LOBBY_LOAD_GAME },
  62. {SDLK_KP_ENTER, EShortcut::LOBBY_LOAD_GAME },
  63. {SDLK_s, EShortcut::LOBBY_SAVE_GAME },
  64. {SDLK_RETURN, EShortcut::LOBBY_SAVE_GAME },
  65. {SDLK_KP_ENTER, EShortcut::LOBBY_SAVE_GAME },
  66. {SDLK_r, EShortcut::LOBBY_RANDOM_MAP },
  67. {SDLK_h, EShortcut::LOBBY_HIDE_CHAT },
  68. {SDLK_a, EShortcut::LOBBY_ADDITIONAL_OPTIONS },
  69. {SDLK_s, EShortcut::LOBBY_SELECT_SCENARIO },
  70. {SDLK_e, EShortcut::GAME_END_TURN },
  71. {SDLK_l, EShortcut::GAME_LOAD_GAME },
  72. {SDLK_s, EShortcut::GAME_SAVE_GAME },
  73. {SDLK_r, EShortcut::GAME_RESTART_GAME },
  74. {SDLK_m, EShortcut::GAME_TO_MAIN_MENU },
  75. {SDLK_q, EShortcut::GAME_QUIT_GAME },
  76. {SDLK_t, EShortcut::GAME_OPEN_MARKETPLACE },
  77. {SDLK_g, EShortcut::GAME_OPEN_THIEVES_GUILD },
  78. {SDLK_TAB, EShortcut::GAME_ACTIVATE_CONSOLE },
  79. {SDLK_o, EShortcut::ADVENTURE_GAME_OPTIONS },
  80. {SDLK_F6, EShortcut::ADVENTURE_TOGGLE_GRID },
  81. {SDLK_z, EShortcut::ADVENTURE_SET_HERO_ASLEEP },
  82. {SDLK_w, EShortcut::ADVENTURE_SET_HERO_AWAKE },
  83. {SDLK_m, EShortcut::ADVENTURE_MOVE_HERO },
  84. {SDLK_SPACE, EShortcut::ADVENTURE_VISIT_OBJECT },
  85. {SDLK_KP_1, EShortcut::ADVENTURE_MOVE_HERO_SW },
  86. {SDLK_KP_2, EShortcut::ADVENTURE_MOVE_HERO_SS },
  87. {SDLK_KP_3, EShortcut::ADVENTURE_MOVE_HERO_SE },
  88. {SDLK_KP_4, EShortcut::ADVENTURE_MOVE_HERO_WW },
  89. {SDLK_KP_6, EShortcut::ADVENTURE_MOVE_HERO_EE },
  90. {SDLK_KP_7, EShortcut::ADVENTURE_MOVE_HERO_NW },
  91. {SDLK_KP_8, EShortcut::ADVENTURE_MOVE_HERO_NN },
  92. {SDLK_KP_9, EShortcut::ADVENTURE_MOVE_HERO_NE },
  93. {SDLK_DOWN, EShortcut::ADVENTURE_MOVE_HERO_SS },
  94. {SDLK_LEFT, EShortcut::ADVENTURE_MOVE_HERO_WW },
  95. {SDLK_RIGHT, EShortcut::ADVENTURE_MOVE_HERO_EE },
  96. {SDLK_UP, EShortcut::ADVENTURE_MOVE_HERO_NN },
  97. {SDLK_RETURN, EShortcut::ADVENTURE_VIEW_SELECTED },
  98. {SDLK_KP_ENTER, EShortcut::ADVENTURE_VIEW_SELECTED },
  99. // {SDLK_, EShortcut::ADVENTURE_NEXT_OBJECT },
  100. {SDLK_t, EShortcut::ADVENTURE_NEXT_TOWN },
  101. {SDLK_h, EShortcut::ADVENTURE_NEXT_HERO },
  102. // {SDLK_, EShortcut::ADVENTURE_FIRST_TOWN },
  103. // {SDLK_, EShortcut::ADVENTURE_FIRST_HERO },
  104. {SDLK_i, EShortcut::ADVENTURE_VIEW_SCENARIO },
  105. {SDLK_d, EShortcut::ADVENTURE_DIG_GRAIL },
  106. {SDLK_p, EShortcut::ADVENTURE_VIEW_PUZZLE },
  107. {SDLK_v, EShortcut::ADVENTURE_VIEW_WORLD },
  108. {SDLK_1, EShortcut::ADVENTURE_VIEW_WORLD_X1 },
  109. {SDLK_2, EShortcut::ADVENTURE_VIEW_WORLD_X2 },
  110. {SDLK_4, EShortcut::ADVENTURE_VIEW_WORLD_X4 },
  111. {SDLK_u, EShortcut::ADVENTURE_TOGGLE_MAP_LEVEL},
  112. {SDLK_k, EShortcut::ADVENTURE_KINGDOM_OVERVIEW},
  113. {SDLK_q, EShortcut::ADVENTURE_QUEST_LOG },
  114. {SDLK_c, EShortcut::ADVENTURE_CAST_SPELL },
  115. {SDLK_g, EShortcut::ADVENTURE_THIEVES_GUILD },
  116. {SDLK_KP_PLUS, EShortcut::ADVENTURE_ZOOM_IN },
  117. {SDLK_KP_MINUS, EShortcut::ADVENTURE_ZOOM_OUT },
  118. {SDLK_BACKSPACE, EShortcut::ADVENTURE_ZOOM_RESET },
  119. {SDLK_q, EShortcut::BATTLE_TOGGLE_QUEUE },
  120. {SDLK_c, EShortcut::BATTLE_USE_CREATURE_SPELL },
  121. {SDLK_s, EShortcut::BATTLE_SURRENDER },
  122. {SDLK_r, EShortcut::BATTLE_RETREAT },
  123. {SDLK_a, EShortcut::BATTLE_AUTOCOMBAT },
  124. {SDLK_c, EShortcut::BATTLE_CAST_SPELL },
  125. {SDLK_w, EShortcut::BATTLE_WAIT },
  126. {SDLK_d, EShortcut::BATTLE_DEFEND },
  127. {SDLK_SPACE, EShortcut::BATTLE_DEFEND },
  128. {SDLK_UP, EShortcut::BATTLE_CONSOLE_UP },
  129. {SDLK_DOWN, EShortcut::BATTLE_CONSOLE_DOWN },
  130. {SDLK_SPACE, EShortcut::BATTLE_TACTICS_NEXT },
  131. {SDLK_RETURN, EShortcut::BATTLE_TACTICS_END },
  132. {SDLK_KP_ENTER, EShortcut::BATTLE_TACTICS_END },
  133. {SDLK_s, EShortcut::BATTLE_SELECT_ACTION },
  134. {SDLK_t, EShortcut::TOWN_OPEN_TAVERN },
  135. {SDLK_SPACE, EShortcut::TOWN_SWAP_ARMIES },
  136. {SDLK_END, EShortcut::RECRUITMENT_MAX },
  137. {SDLK_HOME, EShortcut::RECRUITMENT_MIN },
  138. {SDLK_u, EShortcut::RECRUITMENT_UPGRADE },
  139. {SDLK_a, EShortcut::RECRUITMENT_UPGRADE_ALL },
  140. {SDLK_u, EShortcut::RECRUITMENT_UPGRADE_ALL },
  141. {SDLK_h, EShortcut::KINGDOM_HEROES_TAB },
  142. {SDLK_t, EShortcut::KINGDOM_TOWNS_TAB },
  143. {SDLK_d, EShortcut::HERO_DISMISS },
  144. {SDLK_c, EShortcut::HERO_COMMANDER },
  145. {SDLK_l, EShortcut::HERO_LOOSE_FORMATION },
  146. {SDLK_t, EShortcut::HERO_TIGHT_FORMATION },
  147. {SDLK_b, EShortcut::HERO_TOGGLE_TACTICS },
  148. {SDLK_a, EShortcut::SPELLBOOK_TAB_ADVENTURE },
  149. {SDLK_c, EShortcut::SPELLBOOK_TAB_COMBAT }
  150. };
  151. auto range = keyToShortcut.equal_range(key);
  152. // FIXME: some code expects calls to keyPressed / captureThisKey even without defined hotkeys
  153. if (range.first == range.second)
  154. return {EShortcut::NONE};
  155. std::vector<EShortcut> result;
  156. for (auto it = range.first; it != range.second; ++it)
  157. result.push_back(it->second);
  158. return result;
  159. }
  160. EShortcut ShortcutHandler::findShortcut(const std::string & identifier ) const
  161. {
  162. static const std::map<std::string, EShortcut> shortcutNames = {
  163. {"globalAccept", EShortcut::GLOBAL_ACCEPT },
  164. {"globalCancel", EShortcut::GLOBAL_CANCEL },
  165. {"globalReturn", EShortcut::GLOBAL_RETURN },
  166. {"globalFullscreen", EShortcut::GLOBAL_FULLSCREEN },
  167. {"globalOptions", EShortcut::GLOBAL_OPTIONS },
  168. {"globalBackspace", EShortcut::GLOBAL_BACKSPACE },
  169. {"globalMoveFocus", EShortcut::GLOBAL_MOVE_FOCUS },
  170. {"moveLeft", EShortcut::MOVE_LEFT },
  171. {"moveRight", EShortcut::MOVE_RIGHT },
  172. {"moveUp", EShortcut::MOVE_UP },
  173. {"moveDown", EShortcut::MOVE_DOWN },
  174. {"moveFirst", EShortcut::MOVE_FIRST },
  175. {"moveLast", EShortcut::MOVE_LAST },
  176. {"movePageUp", EShortcut::MOVE_PAGE_UP },
  177. {"movePageDown", EShortcut::MOVE_PAGE_DOWN },
  178. {"selectIndex1", EShortcut::SELECT_INDEX_1 },
  179. {"selectIndex2", EShortcut::SELECT_INDEX_2 },
  180. {"selectIndex3", EShortcut::SELECT_INDEX_3 },
  181. {"selectIndex4", EShortcut::SELECT_INDEX_4 },
  182. {"selectIndex5", EShortcut::SELECT_INDEX_5 },
  183. {"selectIndex6", EShortcut::SELECT_INDEX_6 },
  184. {"selectIndex7", EShortcut::SELECT_INDEX_7 },
  185. {"selectIndex8", EShortcut::SELECT_INDEX_8 },
  186. {"mainMenuNewGame", EShortcut::MAIN_MENU_NEW_GAME },
  187. {"mainMenuLoadGame", EShortcut::MAIN_MENU_LOAD_GAME },
  188. {"mainMenuHighScores", EShortcut::MAIN_MENU_HIGH_SCORES },
  189. {"mainMenuCredits", EShortcut::MAIN_MENU_CREDITS },
  190. {"mainMenuQuit", EShortcut::MAIN_MENU_QUIT },
  191. {"mainMenuBack", EShortcut::MAIN_MENU_BACK },
  192. {"mainMenuSingleplayer", EShortcut::MAIN_MENU_SINGLEPLAYER },
  193. {"mainMenuMultiplayer", EShortcut::MAIN_MENU_MULTIPLAYER },
  194. {"mainMenuCampaign", EShortcut::MAIN_MENU_CAMPAIGN },
  195. {"mainMenuTutorial", EShortcut::MAIN_MENU_TUTORIAL },
  196. {"mainMenuCampaignSod", EShortcut::MAIN_MENU_CAMPAIGN_SOD },
  197. {"mainMenuCampaignRoe", EShortcut::MAIN_MENU_CAMPAIGN_ROE },
  198. {"mainMenuCampaignAb", EShortcut::MAIN_MENU_CAMPAIGN_AB },
  199. {"mainMenuCampaignCustom", EShortcut::MAIN_MENU_CAMPAIGN_CUSTOM },
  200. {"lobbyBeginGame", EShortcut::LOBBY_BEGIN_GAME },
  201. {"lobbyLoadGame", EShortcut::LOBBY_LOAD_GAME },
  202. {"lobbySaveGame", EShortcut::LOBBY_SAVE_GAME },
  203. {"lobbyRandomMap", EShortcut::LOBBY_RANDOM_MAP },
  204. {"lobbyHideChat", EShortcut::LOBBY_HIDE_CHAT },
  205. {"lobbyAdditionalOptions", EShortcut::LOBBY_ADDITIONAL_OPTIONS },
  206. {"lobbySelectScenario", EShortcut::LOBBY_SELECT_SCENARIO },
  207. {"gameEndTurn", EShortcut::GAME_END_TURN },
  208. {"gameLoadGame", EShortcut::GAME_LOAD_GAME },
  209. {"gameSaveGame", EShortcut::GAME_SAVE_GAME },
  210. {"gameRestartGame", EShortcut::GAME_RESTART_GAME },
  211. {"gameMainMenu", EShortcut::GAME_TO_MAIN_MENU },
  212. {"gameQuitGame", EShortcut::GAME_QUIT_GAME },
  213. {"gameOpenMarketplace", EShortcut::GAME_OPEN_MARKETPLACE },
  214. {"gameOpenThievesGuild", EShortcut::GAME_OPEN_THIEVES_GUILD },
  215. {"gameActivateConsole", EShortcut::GAME_ACTIVATE_CONSOLE },
  216. {"adventureGameOptions", EShortcut::ADVENTURE_GAME_OPTIONS },
  217. {"adventureToggleGrid", EShortcut::ADVENTURE_TOGGLE_GRID },
  218. {"adventureToggleSleep", EShortcut::ADVENTURE_TOGGLE_SLEEP },
  219. {"adventureSetHeroAsleep", EShortcut::ADVENTURE_SET_HERO_ASLEEP },
  220. {"adventureSetHeroAwake", EShortcut::ADVENTURE_SET_HERO_AWAKE },
  221. {"adventureMoveHero", EShortcut::ADVENTURE_MOVE_HERO },
  222. {"adventureVisitObject", EShortcut::ADVENTURE_VISIT_OBJECT },
  223. {"adventureMoveHeroSW", EShortcut::ADVENTURE_MOVE_HERO_SW },
  224. {"adventureMoveHeroSS", EShortcut::ADVENTURE_MOVE_HERO_SS },
  225. {"adventureMoveHeroSE", EShortcut::ADVENTURE_MOVE_HERO_SE },
  226. {"adventureMoveHeroWW", EShortcut::ADVENTURE_MOVE_HERO_WW },
  227. {"adventureMoveHeroEE", EShortcut::ADVENTURE_MOVE_HERO_EE },
  228. {"adventureMoveHeroNW", EShortcut::ADVENTURE_MOVE_HERO_NW },
  229. {"adventureMoveHeroNN", EShortcut::ADVENTURE_MOVE_HERO_NN },
  230. {"adventureMoveHeroNE", EShortcut::ADVENTURE_MOVE_HERO_NE },
  231. {"adventureViewSelected", EShortcut::ADVENTURE_VIEW_SELECTED },
  232. {"adventureNextObject", EShortcut::ADVENTURE_NEXT_OBJECT },
  233. {"adventureNextTown", EShortcut::ADVENTURE_NEXT_TOWN },
  234. {"adventureNextHero", EShortcut::ADVENTURE_NEXT_HERO },
  235. {"adventureFirstTown", EShortcut::ADVENTURE_FIRST_TOWN },
  236. {"adventureFirstHero", EShortcut::ADVENTURE_FIRST_HERO },
  237. {"adventureViewScenario", EShortcut::ADVENTURE_VIEW_SCENARIO },
  238. {"adventureDigGrail", EShortcut::ADVENTURE_DIG_GRAIL },
  239. {"adventureViewPuzzle", EShortcut::ADVENTURE_VIEW_PUZZLE },
  240. {"adventureViewWorld", EShortcut::ADVENTURE_VIEW_WORLD },
  241. {"adventureViewWorld1", EShortcut::ADVENTURE_VIEW_WORLD_X1 },
  242. {"adventureViewWorld2", EShortcut::ADVENTURE_VIEW_WORLD_X2 },
  243. {"adventureViewWorld4", EShortcut::ADVENTURE_VIEW_WORLD_X4 },
  244. {"adventureToggleMapLevel", EShortcut::ADVENTURE_TOGGLE_MAP_LEVEL},
  245. {"adventureKingdomOverview", EShortcut::ADVENTURE_KINGDOM_OVERVIEW},
  246. {"adventureQuestLog", EShortcut::ADVENTURE_QUEST_LOG },
  247. {"adventureCastSpell", EShortcut::ADVENTURE_CAST_SPELL },
  248. {"adventureThievesGuild", EShortcut::ADVENTURE_THIEVES_GUILD },
  249. {"adventureExitWorldView", EShortcut::ADVENTURE_EXIT_WORLD_VIEW },
  250. {"adventureZoomIn", EShortcut::ADVENTURE_ZOOM_IN },
  251. {"adventureZoomOut", EShortcut::ADVENTURE_ZOOM_OUT },
  252. {"adventureZoomReset", EShortcut::ADVENTURE_ZOOM_RESET },
  253. {"battleToggleQueue", EShortcut::BATTLE_TOGGLE_QUEUE },
  254. {"battleUseCreatureSpell", EShortcut::BATTLE_USE_CREATURE_SPELL },
  255. {"battleSurrender", EShortcut::BATTLE_SURRENDER },
  256. {"battleRetreat", EShortcut::BATTLE_RETREAT },
  257. {"battleAutocombat", EShortcut::BATTLE_AUTOCOMBAT },
  258. {"battleCastSpell", EShortcut::BATTLE_CAST_SPELL },
  259. {"battleWait", EShortcut::BATTLE_WAIT },
  260. {"battleDefend", EShortcut::BATTLE_DEFEND },
  261. {"battleConsoleUp", EShortcut::BATTLE_CONSOLE_UP },
  262. {"battleConsoleDown", EShortcut::BATTLE_CONSOLE_DOWN },
  263. {"battleTacticsNext", EShortcut::BATTLE_TACTICS_NEXT },
  264. {"battleTacticsEnd", EShortcut::BATTLE_TACTICS_END },
  265. {"battleSelectAction", EShortcut::BATTLE_SELECT_ACTION },
  266. {"townOpenTavern", EShortcut::TOWN_OPEN_TAVERN },
  267. {"townSwapArmies", EShortcut::TOWN_SWAP_ARMIES },
  268. {"recruitmentMax", EShortcut::RECRUITMENT_MAX },
  269. {"recruitmentMin", EShortcut::RECRUITMENT_MIN },
  270. {"recruitmentUpgrade", EShortcut::RECRUITMENT_UPGRADE },
  271. {"recruitmentUpgradeAll", EShortcut::RECRUITMENT_UPGRADE_ALL },
  272. {"kingdomHeroesTab", EShortcut::KINGDOM_HEROES_TAB },
  273. {"kingdomTownsTab", EShortcut::KINGDOM_TOWNS_TAB },
  274. {"heroDismiss", EShortcut::HERO_DISMISS },
  275. {"heroCommander", EShortcut::HERO_COMMANDER },
  276. {"heroLooseFormation", EShortcut::HERO_LOOSE_FORMATION },
  277. {"heroTightFormation", EShortcut::HERO_TIGHT_FORMATION },
  278. {"heroToggleTactics", EShortcut::HERO_TOGGLE_TACTICS },
  279. {"spellbookTabAdventure", EShortcut::SPELLBOOK_TAB_ADVENTURE },
  280. {"spellbookTabCombat", EShortcut::SPELLBOOK_TAB_COMBAT }
  281. };
  282. if (shortcutNames.count(identifier))
  283. return shortcutNames.at(identifier);
  284. return EShortcut::NONE;
  285. }