ShortcutHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "../../lib/json/JsonUtils.h"
  14. ShortcutHandler::ShortcutHandler()
  15. {
  16. const JsonNode config = JsonUtils::assembleFromFiles("config/shortcutsConfig");
  17. mappedKeyboardShortcuts = loadShortcuts(config["keyboard"]);
  18. mappedJoystickShortcuts = loadShortcuts(config["joystickButtons"]);
  19. mappedJoystickAxes = loadShortcuts(config["joystickAxes"]);
  20. }
  21. std::multimap<std::string, EShortcut> ShortcutHandler::loadShortcuts(const JsonNode & data) const
  22. {
  23. std::multimap<std::string, EShortcut> result;
  24. for (auto const & entry : data.Struct())
  25. {
  26. std::string shortcutName = entry.first;
  27. EShortcut shortcutID = findShortcut(shortcutName);
  28. if (shortcutID == EShortcut::NONE)
  29. {
  30. logGlobal->warn("Unknown shortcut '%s' found when loading shortcuts config!", shortcutName);
  31. continue;
  32. }
  33. if (entry.second.isString())
  34. {
  35. result.emplace(entry.second.String(), shortcutID);
  36. }
  37. if (entry.second.isVector())
  38. {
  39. for (auto const & entryVector : entry.second.Vector())
  40. result.emplace(entryVector.String(), shortcutID);
  41. }
  42. }
  43. return result;
  44. }
  45. std::vector<EShortcut> ShortcutHandler::translateShortcut(const std::multimap<std::string, EShortcut> & options, const std::string & key) const
  46. {
  47. auto range = options.equal_range(key);
  48. // FIXME: some code expects calls to keyPressed / captureThisKey even without defined hotkeys
  49. if (range.first == range.second)
  50. return {EShortcut::NONE};
  51. std::vector<EShortcut> result;
  52. for (auto it = range.first; it != range.second; ++it)
  53. result.push_back(it->second);
  54. return result;
  55. }
  56. std::vector<EShortcut> ShortcutHandler::translateKeycode(const std::string & key) const
  57. {
  58. return translateShortcut(mappedKeyboardShortcuts, key);
  59. }
  60. std::vector<EShortcut> ShortcutHandler::translateJoystickButton(const std::string & key) const
  61. {
  62. return translateShortcut(mappedJoystickShortcuts, key);
  63. }
  64. std::vector<EShortcut> ShortcutHandler::translateJoystickAxis(const std::string & key) const
  65. {
  66. return translateShortcut(mappedJoystickAxes, key);
  67. }
  68. EShortcut ShortcutHandler::findShortcut(const std::string & identifier ) const
  69. {
  70. static const std::map<std::string, EShortcut> shortcutNames = {
  71. {"mouseClickLeft", EShortcut::MOUSE_LEFT },
  72. {"mouseClickRight", EShortcut::MOUSE_RIGHT },
  73. {"mouseCursorX", EShortcut::MOUSE_CURSOR_X, },
  74. {"mouseCursorY", EShortcut::MOUSE_CURSOR_Y, },
  75. {"mouseSwipeX", EShortcut::MOUSE_SWIPE_X, },
  76. {"mouseSwipeY", EShortcut::MOUSE_SWIPE_Y, },
  77. {"globalAccept", EShortcut::GLOBAL_ACCEPT },
  78. {"globalCancel", EShortcut::GLOBAL_CANCEL },
  79. {"globalReturn", EShortcut::GLOBAL_RETURN },
  80. {"globalFullscreen", EShortcut::GLOBAL_FULLSCREEN },
  81. {"globalOptions", EShortcut::GLOBAL_OPTIONS },
  82. {"globalBackspace", EShortcut::GLOBAL_BACKSPACE },
  83. {"globalMoveFocus", EShortcut::GLOBAL_MOVE_FOCUS },
  84. {"moveLeft", EShortcut::MOVE_LEFT },
  85. {"moveRight", EShortcut::MOVE_RIGHT },
  86. {"moveUp", EShortcut::MOVE_UP },
  87. {"moveDown", EShortcut::MOVE_DOWN },
  88. {"moveFirst", EShortcut::MOVE_FIRST },
  89. {"moveLast", EShortcut::MOVE_LAST },
  90. {"movePageUp", EShortcut::MOVE_PAGE_UP },
  91. {"movePageDown", EShortcut::MOVE_PAGE_DOWN },
  92. {"selectIndex1", EShortcut::SELECT_INDEX_1 },
  93. {"selectIndex2", EShortcut::SELECT_INDEX_2 },
  94. {"selectIndex3", EShortcut::SELECT_INDEX_3 },
  95. {"selectIndex4", EShortcut::SELECT_INDEX_4 },
  96. {"selectIndex5", EShortcut::SELECT_INDEX_5 },
  97. {"selectIndex6", EShortcut::SELECT_INDEX_6 },
  98. {"selectIndex7", EShortcut::SELECT_INDEX_7 },
  99. {"selectIndex8", EShortcut::SELECT_INDEX_8 },
  100. {"mainMenuNewGame", EShortcut::MAIN_MENU_NEW_GAME },
  101. {"mainMenuLoadGame", EShortcut::MAIN_MENU_LOAD_GAME },
  102. {"mainMenuHighScores", EShortcut::MAIN_MENU_HIGH_SCORES },
  103. {"mainMenuCredits", EShortcut::MAIN_MENU_CREDITS },
  104. {"mainMenuQuit", EShortcut::MAIN_MENU_QUIT },
  105. {"mainMenuBack", EShortcut::MAIN_MENU_BACK },
  106. {"mainMenuSingleplayer", EShortcut::MAIN_MENU_SINGLEPLAYER },
  107. {"mainMenuMultiplayer", EShortcut::MAIN_MENU_MULTIPLAYER },
  108. {"mainMenuCampaign", EShortcut::MAIN_MENU_CAMPAIGN },
  109. {"mainMenuTutorial", EShortcut::MAIN_MENU_TUTORIAL },
  110. {"mainMenuCampaignSod", EShortcut::MAIN_MENU_CAMPAIGN_SOD },
  111. {"mainMenuCampaignRoe", EShortcut::MAIN_MENU_CAMPAIGN_ROE },
  112. {"mainMenuCampaignAb", EShortcut::MAIN_MENU_CAMPAIGN_AB },
  113. {"mainMenuCampaignCustom", EShortcut::MAIN_MENU_CAMPAIGN_CUSTOM },
  114. {"lobbyBeginStandardGame", EShortcut::LOBBY_BEGIN_STANDARD_GAME },
  115. {"lobbyBeginCampaign", EShortcut::LOBBY_BEGIN_CAMPAIGN },
  116. {"lobbyLoadGame", EShortcut::LOBBY_LOAD_GAME },
  117. {"lobbySaveGame", EShortcut::LOBBY_SAVE_GAME },
  118. {"lobbyRandomMap", EShortcut::LOBBY_RANDOM_MAP },
  119. {"lobbyHideChat", EShortcut::LOBBY_HIDE_CHAT },
  120. {"lobbyAdditionalOptions", EShortcut::LOBBY_ADDITIONAL_OPTIONS },
  121. {"lobbySelectScenario", EShortcut::LOBBY_SELECT_SCENARIO },
  122. {"gameEndTurn", EShortcut::GAME_END_TURN },
  123. {"gameLoadGame", EShortcut::GAME_LOAD_GAME },
  124. {"gameSaveGame", EShortcut::GAME_SAVE_GAME },
  125. {"gameRestartGame", EShortcut::GAME_RESTART_GAME },
  126. {"gameMainMenu", EShortcut::GAME_TO_MAIN_MENU },
  127. {"gameQuitGame", EShortcut::GAME_QUIT_GAME },
  128. {"gameOpenMarketplace", EShortcut::GAME_OPEN_MARKETPLACE },
  129. {"gameOpenThievesGuild", EShortcut::GAME_OPEN_THIEVES_GUILD },
  130. {"gameActivateConsole", EShortcut::GAME_ACTIVATE_CONSOLE },
  131. {"adventureGameOptions", EShortcut::ADVENTURE_GAME_OPTIONS },
  132. {"adventureToggleGrid", EShortcut::ADVENTURE_TOGGLE_GRID },
  133. {"adventureToggleSleep", EShortcut::ADVENTURE_TOGGLE_SLEEP },
  134. {"adventureSetHeroAsleep", EShortcut::ADVENTURE_SET_HERO_ASLEEP },
  135. {"adventureSetHeroAwake", EShortcut::ADVENTURE_SET_HERO_AWAKE },
  136. {"adventureMoveHero", EShortcut::ADVENTURE_MOVE_HERO },
  137. {"adventureVisitObject", EShortcut::ADVENTURE_VISIT_OBJECT },
  138. {"adventureMoveHeroSW", EShortcut::ADVENTURE_MOVE_HERO_SW },
  139. {"adventureMoveHeroSS", EShortcut::ADVENTURE_MOVE_HERO_SS },
  140. {"adventureMoveHeroSE", EShortcut::ADVENTURE_MOVE_HERO_SE },
  141. {"adventureMoveHeroWW", EShortcut::ADVENTURE_MOVE_HERO_WW },
  142. {"adventureMoveHeroEE", EShortcut::ADVENTURE_MOVE_HERO_EE },
  143. {"adventureMoveHeroNW", EShortcut::ADVENTURE_MOVE_HERO_NW },
  144. {"adventureMoveHeroNN", EShortcut::ADVENTURE_MOVE_HERO_NN },
  145. {"adventureMoveHeroNE", EShortcut::ADVENTURE_MOVE_HERO_NE },
  146. {"adventureViewSelected", EShortcut::ADVENTURE_VIEW_SELECTED },
  147. {"adventureNextObject", EShortcut::ADVENTURE_NEXT_OBJECT },
  148. {"adventureNextTown", EShortcut::ADVENTURE_NEXT_TOWN },
  149. {"adventureNextHero", EShortcut::ADVENTURE_NEXT_HERO },
  150. {"adventureFirstTown", EShortcut::ADVENTURE_FIRST_TOWN },
  151. {"adventureFirstHero", EShortcut::ADVENTURE_FIRST_HERO },
  152. {"adventureViewScenario", EShortcut::ADVENTURE_VIEW_SCENARIO },
  153. {"adventureDigGrail", EShortcut::ADVENTURE_DIG_GRAIL },
  154. {"adventureViewPuzzle", EShortcut::ADVENTURE_VIEW_PUZZLE },
  155. {"adventureViewWorld", EShortcut::ADVENTURE_VIEW_WORLD },
  156. {"adventureViewWorld1", EShortcut::ADVENTURE_VIEW_WORLD_X1 },
  157. {"adventureViewWorld2", EShortcut::ADVENTURE_VIEW_WORLD_X2 },
  158. {"adventureViewWorld4", EShortcut::ADVENTURE_VIEW_WORLD_X4 },
  159. {"adventureToggleMapLevel", EShortcut::ADVENTURE_TOGGLE_MAP_LEVEL},
  160. {"adventureKingdomOverview", EShortcut::ADVENTURE_KINGDOM_OVERVIEW},
  161. {"adventureQuestLog", EShortcut::ADVENTURE_QUEST_LOG },
  162. {"adventureCastSpell", EShortcut::ADVENTURE_CAST_SPELL },
  163. {"adventureThievesGuild", EShortcut::ADVENTURE_THIEVES_GUILD },
  164. {"adventureExitWorldView", EShortcut::ADVENTURE_EXIT_WORLD_VIEW },
  165. {"adventureZoomIn", EShortcut::ADVENTURE_ZOOM_IN },
  166. {"adventureZoomOut", EShortcut::ADVENTURE_ZOOM_OUT },
  167. {"adventureZoomReset", EShortcut::ADVENTURE_ZOOM_RESET },
  168. {"battleToggleHeroesStats", EShortcut::BATTLE_TOGGLE_HEROES_STATS},
  169. {"battleToggleQueue", EShortcut::BATTLE_TOGGLE_QUEUE },
  170. {"battleUseCreatureSpell", EShortcut::BATTLE_USE_CREATURE_SPELL },
  171. {"battleSurrender", EShortcut::BATTLE_SURRENDER },
  172. {"battleRetreat", EShortcut::BATTLE_RETREAT },
  173. {"battleAutocombat", EShortcut::BATTLE_AUTOCOMBAT },
  174. {"battleAutocombatEnd", EShortcut::BATTLE_END_WITH_AUTOCOMBAT},
  175. {"battleCastSpell", EShortcut::BATTLE_CAST_SPELL },
  176. {"battleWait", EShortcut::BATTLE_WAIT },
  177. {"battleDefend", EShortcut::BATTLE_DEFEND },
  178. {"battleConsoleUp", EShortcut::BATTLE_CONSOLE_UP },
  179. {"battleConsoleDown", EShortcut::BATTLE_CONSOLE_DOWN },
  180. {"battleTacticsNext", EShortcut::BATTLE_TACTICS_NEXT },
  181. {"battleTacticsEnd", EShortcut::BATTLE_TACTICS_END },
  182. {"battleSelectAction", EShortcut::BATTLE_SELECT_ACTION },
  183. {"townOpenTavern", EShortcut::TOWN_OPEN_TAVERN },
  184. {"townSwapArmies", EShortcut::TOWN_SWAP_ARMIES },
  185. {"recruitmentMax", EShortcut::RECRUITMENT_MAX },
  186. {"recruitmentMin", EShortcut::RECRUITMENT_MIN },
  187. {"recruitmentUpgrade", EShortcut::RECRUITMENT_UPGRADE },
  188. {"recruitmentUpgradeAll", EShortcut::RECRUITMENT_UPGRADE_ALL },
  189. {"kingdomHeroesTab", EShortcut::KINGDOM_HEROES_TAB },
  190. {"kingdomTownsTab", EShortcut::KINGDOM_TOWNS_TAB },
  191. {"heroDismiss", EShortcut::HERO_DISMISS },
  192. {"heroCommander", EShortcut::HERO_COMMANDER },
  193. {"heroLooseFormation", EShortcut::HERO_LOOSE_FORMATION },
  194. {"heroTightFormation", EShortcut::HERO_TIGHT_FORMATION },
  195. {"heroToggleTactics", EShortcut::HERO_TOGGLE_TACTICS },
  196. {"heroCostume0", EShortcut::HERO_COSTUME_0 },
  197. {"heroCostume1", EShortcut::HERO_COSTUME_1 },
  198. {"heroCostume2", EShortcut::HERO_COSTUME_2 },
  199. {"heroCostume3", EShortcut::HERO_COSTUME_3 },
  200. {"heroCostume4", EShortcut::HERO_COSTUME_4 },
  201. {"heroCostume5", EShortcut::HERO_COSTUME_5 },
  202. {"heroCostume6", EShortcut::HERO_COSTUME_6 },
  203. {"heroCostume7", EShortcut::HERO_COSTUME_7 },
  204. {"heroCostume8", EShortcut::HERO_COSTUME_8 },
  205. {"heroCostume9", EShortcut::HERO_COSTUME_9 },
  206. {"spellbookTabAdventure", EShortcut::SPELLBOOK_TAB_ADVENTURE },
  207. {"spellbookTabCombat", EShortcut::SPELLBOOK_TAB_COMBAT }
  208. };
  209. if (shortcutNames.count(identifier))
  210. return shortcutNames.at(identifier);
  211. return EShortcut::NONE;
  212. }