ShortcutHandler.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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_r, EShortcut::LOBBY_RANDOM_MAP },
  65. {SDLK_h, EShortcut::LOBBY_HIDE_CHAT },
  66. {SDLK_a, EShortcut::LOBBY_ADDITIONAL_OPTIONS },
  67. {SDLK_s, EShortcut::LOBBY_SELECT_SCENARIO },
  68. {SDLK_e, EShortcut::GAME_END_TURN },
  69. {SDLK_l, EShortcut::GAME_LOAD_GAME },
  70. {SDLK_s, EShortcut::GAME_SAVE_GAME },
  71. {SDLK_r, EShortcut::GAME_RESTART_GAME },
  72. {SDLK_m, EShortcut::GAME_TO_MAIN_MENU },
  73. {SDLK_q, EShortcut::GAME_QUIT_GAME },
  74. {SDLK_t, EShortcut::GAME_OPEN_MARKETPLACE },
  75. {SDLK_g, EShortcut::GAME_OPEN_THIEVES_GUILD },
  76. {SDLK_TAB, EShortcut::GAME_ACTIVATE_CONSOLE },
  77. {SDLK_o, EShortcut::ADVENTURE_GAME_OPTIONS },
  78. {SDLK_F6, EShortcut::ADVENTURE_TOGGLE_GRID },
  79. {SDLK_z, EShortcut::ADVENTURE_TOGGLE_SLEEP },
  80. {SDLK_w, EShortcut::ADVENTURE_TOGGLE_SLEEP },
  81. {SDLK_m, EShortcut::ADVENTURE_MOVE_HERO },
  82. {SDLK_SPACE, EShortcut::ADVENTURE_VISIT_OBJECT },
  83. {SDLK_KP_1, EShortcut::ADVENTURE_MOVE_HERO_SW },
  84. {SDLK_KP_2, EShortcut::ADVENTURE_MOVE_HERO_SS },
  85. {SDLK_KP_3, EShortcut::ADVENTURE_MOVE_HERO_SE },
  86. {SDLK_KP_4, EShortcut::ADVENTURE_MOVE_HERO_WW },
  87. {SDLK_KP_6, EShortcut::ADVENTURE_MOVE_HERO_EE },
  88. {SDLK_KP_7, EShortcut::ADVENTURE_MOVE_HERO_NW },
  89. {SDLK_KP_8, EShortcut::ADVENTURE_MOVE_HERO_NN },
  90. {SDLK_KP_9, EShortcut::ADVENTURE_MOVE_HERO_NE },
  91. {SDLK_DOWN, EShortcut::ADVENTURE_MOVE_HERO_SS },
  92. {SDLK_LEFT, EShortcut::ADVENTURE_MOVE_HERO_WW },
  93. {SDLK_RIGHT, EShortcut::ADVENTURE_MOVE_HERO_EE },
  94. {SDLK_UP, EShortcut::ADVENTURE_MOVE_HERO_NN },
  95. {SDLK_RETURN, EShortcut::ADVENTURE_VIEW_SELECTED },
  96. {SDLK_KP_ENTER, EShortcut::ADVENTURE_VIEW_SELECTED },
  97. // {SDLK_, EShortcut::ADVENTURE_NEXT_OBJECT },
  98. {SDLK_t, EShortcut::ADVENTURE_NEXT_TOWN },
  99. {SDLK_h, EShortcut::ADVENTURE_NEXT_HERO },
  100. // {SDLK_, EShortcut::ADVENTURE_FIRST_TOWN },
  101. // {SDLK_, EShortcut::ADVENTURE_FIRST_HERO },
  102. {SDLK_i, EShortcut::ADVENTURE_VIEW_SCENARIO },
  103. {SDLK_d, EShortcut::ADVENTURE_DIG_GRAIL },
  104. {SDLK_p, EShortcut::ADVENTURE_VIEW_PUZZLE },
  105. {SDLK_v, EShortcut::ADVENTURE_VIEW_WORLD },
  106. {SDLK_u, EShortcut::ADVENTURE_TOGGLE_MAP_LEVEL},
  107. {SDLK_k, EShortcut::ADVENTURE_KINGDOM_OVERVIEW},
  108. {SDLK_q, EShortcut::ADVENTURE_QUEST_LOG },
  109. {SDLK_c, EShortcut::ADVENTURE_CAST_SPELL },
  110. {SDLK_e, EShortcut::ADVENTURE_END_TURN },
  111. {SDLK_g, EShortcut::ADVENTURE_THIEVES_GUILD },
  112. {SDLK_q, EShortcut::BATTLE_TOGGLE_QUEUE },
  113. {SDLK_c, EShortcut::BATTLE_USE_CREATURE_SPELL },
  114. {SDLK_s, EShortcut::BATTLE_SURRENDER },
  115. {SDLK_r, EShortcut::BATTLE_RETREAT },
  116. {SDLK_a, EShortcut::BATTLE_AUTOCOMBAT },
  117. {SDLK_c, EShortcut::BATTLE_CAST_SPELL },
  118. {SDLK_w, EShortcut::BATTLE_WAIT },
  119. {SDLK_d, EShortcut::BATTLE_DEFEND },
  120. {SDLK_SPACE, EShortcut::BATTLE_DEFEND },
  121. {SDLK_UP, EShortcut::BATTLE_CONSOLE_UP },
  122. {SDLK_DOWN, EShortcut::BATTLE_CONSOLE_DOWN },
  123. {SDLK_SPACE, EShortcut::BATTLE_TACTICS_NEXT },
  124. {SDLK_RETURN, EShortcut::BATTLE_TACTICS_END },
  125. {SDLK_KP_ENTER, EShortcut::BATTLE_TACTICS_END },
  126. {SDLK_s, EShortcut::BATTLE_SELECT_ACTION },
  127. {SDLK_t, EShortcut::TOWN_OPEN_TAVERN },
  128. {SDLK_SPACE, EShortcut::TOWN_SWAP_ARMIES },
  129. {SDLK_END, EShortcut::RECRUITMENT_MAX },
  130. {SDLK_HOME, EShortcut::RECRUITMENT_MIN },
  131. {SDLK_u, EShortcut::RECRUITMENT_UPGRADE },
  132. {SDLK_a, EShortcut::RECRUITMENT_UPGRADE_ALL },
  133. {SDLK_u, EShortcut::RECRUITMENT_UPGRADE_ALL },
  134. {SDLK_h, EShortcut::KINGDOM_HEROES_TAB },
  135. {SDLK_t, EShortcut::KINGDOM_TOWNS_TAB },
  136. {SDLK_d, EShortcut::HERO_DISMISS },
  137. {SDLK_c, EShortcut::HERO_COMMANDER },
  138. {SDLK_l, EShortcut::HERO_LOOSE_FORMATION },
  139. {SDLK_t, EShortcut::HERO_TIGHT_FORMATION },
  140. {SDLK_b, EShortcut::HERO_TOGGLE_TACTICS },
  141. {SDLK_a, EShortcut::SPELLBOOK_TAB_ADVENTURE },
  142. {SDLK_c, EShortcut::SPELLBOOK_TAB_COMBAT }
  143. };
  144. auto range = keyToShortcut.equal_range(key);
  145. // FIXME: some code expects calls to keyPressed / captureThisKey even without defined hotkeys
  146. if (range.first == range.second)
  147. return {EShortcut::NONE};
  148. std::vector<EShortcut> result;
  149. for (auto it = range.first; it != range.second; ++it)
  150. result.push_back(it->second);
  151. return result;
  152. }
  153. EShortcut ShortcutHandler::findShortcut(const std::string & identifier ) const
  154. {
  155. static const std::map<std::string, EShortcut> shortcutNames = {
  156. {"globalAccept", EShortcut::GLOBAL_ACCEPT },
  157. {"globalCancel", EShortcut::GLOBAL_CANCEL },
  158. {"globalReturn", EShortcut::GLOBAL_RETURN },
  159. {"globalFullscreen", EShortcut::GLOBAL_FULLSCREEN },
  160. {"globalOptions", EShortcut::GLOBAL_OPTIONS },
  161. {"globalBackspace", EShortcut::GLOBAL_BACKSPACE },
  162. {"globalMoveFocus", EShortcut::GLOBAL_MOVE_FOCUS },
  163. {"moveLeft", EShortcut::MOVE_LEFT },
  164. {"moveRight", EShortcut::MOVE_RIGHT },
  165. {"moveUp", EShortcut::MOVE_UP },
  166. {"moveDown", EShortcut::MOVE_DOWN },
  167. {"moveFirst", EShortcut::MOVE_FIRST },
  168. {"moveLast", EShortcut::MOVE_LAST },
  169. {"movePageUp", EShortcut::MOVE_PAGE_UP },
  170. {"movePageDown", EShortcut::MOVE_PAGE_DOWN },
  171. {"selectIndex1", EShortcut::SELECT_INDEX_1 },
  172. {"selectIndex2", EShortcut::SELECT_INDEX_2 },
  173. {"selectIndex3", EShortcut::SELECT_INDEX_3 },
  174. {"selectIndex4", EShortcut::SELECT_INDEX_4 },
  175. {"selectIndex5", EShortcut::SELECT_INDEX_5 },
  176. {"selectIndex6", EShortcut::SELECT_INDEX_6 },
  177. {"selectIndex7", EShortcut::SELECT_INDEX_7 },
  178. {"selectIndex8", EShortcut::SELECT_INDEX_8 },
  179. {"mainMenuNewGame", EShortcut::MAIN_MENU_NEW_GAME },
  180. {"mainMenuLoadGame", EShortcut::MAIN_MENU_LOAD_GAME },
  181. {"mainMenuHighScores", EShortcut::MAIN_MENU_HIGH_SCORES },
  182. {"mainMenuCredits", EShortcut::MAIN_MENU_CREDITS },
  183. {"mainMenuQuit", EShortcut::MAIN_MENU_QUIT },
  184. {"mainMenuBack", EShortcut::MAIN_MENU_BACK },
  185. {"mainMenuSingleplayer", EShortcut::MAIN_MENU_SINGLEPLAYER },
  186. {"mainMenuMultiplayer", EShortcut::MAIN_MENU_MULTIPLAYER },
  187. {"mainMenuCampaign", EShortcut::MAIN_MENU_CAMPAIGN },
  188. {"mainMenuTutorial", EShortcut::MAIN_MENU_TUTORIAL },
  189. {"mainMenuCampaignSod", EShortcut::MAIN_MENU_CAMPAIGN_SOD },
  190. {"mainMenuCampaignRoe", EShortcut::MAIN_MENU_CAMPAIGN_ROE },
  191. {"mainMenuCampaignAb", EShortcut::MAIN_MENU_CAMPAIGN_AB },
  192. {"mainMenuCampaignCustom", EShortcut::MAIN_MENU_CAMPAIGN_CUSTOM },
  193. {"lobbyBeginGame", EShortcut::LOBBY_BEGIN_GAME },
  194. {"lobbyLoadGame", EShortcut::LOBBY_LOAD_GAME },
  195. {"lobbySaveGame", EShortcut::LOBBY_SAVE_GAME },
  196. {"lobbyRandomMap", EShortcut::LOBBY_RANDOM_MAP },
  197. {"lobbyHideChat", EShortcut::LOBBY_HIDE_CHAT },
  198. {"lobbyAdditionalOptions", EShortcut::LOBBY_ADDITIONAL_OPTIONS },
  199. {"lobbySelectScenario", EShortcut::LOBBY_SELECT_SCENARIO },
  200. {"gameEndTurn", EShortcut::GAME_END_TURN },
  201. {"gameLoadGame", EShortcut::GAME_LOAD_GAME },
  202. {"gameSaveGame", EShortcut::GAME_SAVE_GAME },
  203. {"gameRestartGame", EShortcut::GAME_RESTART_GAME },
  204. {"gameMainMenu", EShortcut::GAME_TO_MAIN_MENU },
  205. {"gameQuitGame", EShortcut::GAME_QUIT_GAME },
  206. {"gameOpenMarketplace", EShortcut::GAME_OPEN_MARKETPLACE },
  207. {"gameOpenThievesGuild", EShortcut::GAME_OPEN_THIEVES_GUILD },
  208. {"gameActivateConsole", EShortcut::GAME_ACTIVATE_CONSOLE },
  209. {"adventureGameOptions", EShortcut::ADVENTURE_GAME_OPTIONS },
  210. {"adventureToggleGrid", EShortcut::ADVENTURE_TOGGLE_GRID },
  211. {"adventureToggleSleep", EShortcut::ADVENTURE_TOGGLE_SLEEP },
  212. {"adventureMoveHero", EShortcut::ADVENTURE_MOVE_HERO },
  213. {"adventureVisitObject", EShortcut::ADVENTURE_VISIT_OBJECT },
  214. {"adventureMoveHeroSW", EShortcut::ADVENTURE_MOVE_HERO_SW },
  215. {"adventureMoveHeroSS", EShortcut::ADVENTURE_MOVE_HERO_SS },
  216. {"adventureMoveHeroSE", EShortcut::ADVENTURE_MOVE_HERO_SE },
  217. {"adventureMoveHeroWW", EShortcut::ADVENTURE_MOVE_HERO_WW },
  218. {"adventureMoveHeroEE", EShortcut::ADVENTURE_MOVE_HERO_EE },
  219. {"adventureMoveHeroNW", EShortcut::ADVENTURE_MOVE_HERO_NW },
  220. {"adventureMoveHeroNN", EShortcut::ADVENTURE_MOVE_HERO_NN },
  221. {"adventureMoveHeroNE", EShortcut::ADVENTURE_MOVE_HERO_NE },
  222. {"adventureViewSelected", EShortcut::ADVENTURE_VIEW_SELECTED },
  223. {"adventureNextObject", EShortcut::ADVENTURE_NEXT_OBJECT },
  224. {"adventureNextTown", EShortcut::ADVENTURE_NEXT_TOWN },
  225. {"adventureNextHero", EShortcut::ADVENTURE_NEXT_HERO },
  226. {"adventureFirstTown", EShortcut::ADVENTURE_FIRST_TOWN },
  227. {"adventureFirstHero", EShortcut::ADVENTURE_FIRST_HERO },
  228. {"adventureViewScenario", EShortcut::ADVENTURE_VIEW_SCENARIO },
  229. {"adventureDigGrail", EShortcut::ADVENTURE_DIG_GRAIL },
  230. {"adventureViewPuzzle", EShortcut::ADVENTURE_VIEW_PUZZLE },
  231. {"adventureViewWorld", EShortcut::ADVENTURE_VIEW_WORLD },
  232. {"adventureToggleMapLevel", EShortcut::ADVENTURE_TOGGLE_MAP_LEVEL},
  233. {"adventureKingdomOverview", EShortcut::ADVENTURE_KINGDOM_OVERVIEW},
  234. {"adventureQuestLog", EShortcut::ADVENTURE_QUEST_LOG },
  235. {"adventureCastSpell", EShortcut::ADVENTURE_CAST_SPELL },
  236. {"adventureEndTurn", EShortcut::ADVENTURE_END_TURN },
  237. {"adventureThievesGuild", EShortcut::ADVENTURE_THIEVES_GUILD },
  238. {"battleToggleQueue", EShortcut::BATTLE_TOGGLE_QUEUE },
  239. {"battleUseCreatureSpell", EShortcut::BATTLE_USE_CREATURE_SPELL },
  240. {"battleSurrender", EShortcut::BATTLE_SURRENDER },
  241. {"battleRetreat", EShortcut::BATTLE_RETREAT },
  242. {"battleAutocombat", EShortcut::BATTLE_AUTOCOMBAT },
  243. {"battleCastSpell", EShortcut::BATTLE_CAST_SPELL },
  244. {"battleWait", EShortcut::BATTLE_WAIT },
  245. {"battleDefend", EShortcut::BATTLE_DEFEND },
  246. {"battleConsoleUp", EShortcut::BATTLE_CONSOLE_UP },
  247. {"battleConsoleDown", EShortcut::BATTLE_CONSOLE_DOWN },
  248. {"battleTacticsNext", EShortcut::BATTLE_TACTICS_NEXT },
  249. {"battleTacticsEnd", EShortcut::BATTLE_TACTICS_END },
  250. {"battleSelectAction", EShortcut::BATTLE_SELECT_ACTION },
  251. {"townOpenTavern", EShortcut::TOWN_OPEN_TAVERN },
  252. {"townSwapArmies", EShortcut::TOWN_SWAP_ARMIES },
  253. {"recruitmentMax", EShortcut::RECRUITMENT_MAX },
  254. {"recruitmentMin", EShortcut::RECRUITMENT_MIN },
  255. {"recruitmentUpgrade", EShortcut::RECRUITMENT_UPGRADE },
  256. {"recruitmentUpgradeAll", EShortcut::RECRUITMENT_UPGRADE_ALL },
  257. {"kingdomHeroesTab", EShortcut::KINGDOM_HEROES_TAB },
  258. {"kingdomTownsTab", EShortcut::KINGDOM_TOWNS_TAB },
  259. {"heroDismiss", EShortcut::HERO_DISMISS },
  260. {"heroCommander", EShortcut::HERO_COMMANDER },
  261. {"heroLooseFormation", EShortcut::HERO_LOOSE_FORMATION },
  262. {"heroTightFormation", EShortcut::HERO_TIGHT_FORMATION },
  263. {"heroToggleTactics", EShortcut::HERO_TOGGLE_TACTICS },
  264. {"spellbookTabAdventure", EShortcut::SPELLBOOK_TAB_ADVENTURE },
  265. {"spellbookTabCombat", EShortcut::SPELLBOOK_TAB_COMBAT }
  266. };
  267. if (shortcutNames.count(identifier))
  268. return shortcutNames.at(identifier);
  269. return EShortcut::NONE;
  270. }