ShortcutHandler.cpp 11 KB

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