GameControllerConfig.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * GameControllerConfig.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 <SDL.h>
  11. #include "StdInc.h"
  12. #include "GameControllerConfig.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../gui/ShortcutHandler.h"
  15. GameControllerConfig::GameControllerConfig(): leftAxisType(AxisType::NONE), rightAxisType(AxisType::NONE)
  16. {
  17. load();
  18. }
  19. void GameControllerConfig::load()
  20. {
  21. const JsonNode config = JsonUtils::assembleFromFiles("config/shortcutsConfig");
  22. for(auto const & entry : config["joystick"].Struct())
  23. {
  24. std::string configName = entry.first;
  25. if(configName == "leftaxis")
  26. leftAxisType = parseAxis(entry.first, entry.second);
  27. else if (configName == "rightaxis")
  28. rightAxisType = parseAxis(entry.first, entry.second);
  29. else if (configName == "lefttrigger" || configName == "righttrigger")
  30. parseTrigger(entry.first, entry.second);
  31. else
  32. parseButton(entry.first, entry.second);
  33. }
  34. }
  35. AxisType GameControllerConfig::parseAxis(const std::string & key, const JsonNode & value)
  36. {
  37. if(!value.isString())
  38. {
  39. logGlobal->error("The value of joystick config key %s should be a string!", key);
  40. return AxisType::NONE;
  41. }
  42. std::string featureName = value.String();
  43. if(featureName == "cursorMotion")
  44. return AxisType::CURSOR_MOTION;
  45. else if(featureName == "mapScroll")
  46. return AxisType::MAP_SCROLL;
  47. else if(featureName != "")
  48. logGlobal->error("Unknown value %s of joystick config key %s!", featureName, key);
  49. return AxisType::NONE;
  50. }
  51. void GameControllerConfig::parseTrigger(const std::string & key, const JsonNode & value)
  52. {
  53. std::vector<std::string> operations = getOperations(key, value);
  54. SDL_GameControllerAxis triggerAxis = key == "lefttrigger" ?
  55. SDL_CONTROLLER_AXIS_TRIGGERLEFT : SDL_CONTROLLER_AXIS_TRIGGERRIGHT;
  56. std::vector<EShortcut> shortcuts;
  57. for(const auto & operation : operations)
  58. {
  59. if(operation == "mouseLeftClick")
  60. {
  61. leftClickTriggerSet.insert(triggerAxis);
  62. }
  63. else if(operation == "mouseRightClick")
  64. {
  65. rightClickTriggerSet.insert(triggerAxis);
  66. }
  67. else
  68. {
  69. EShortcut shortcut = GH.shortcuts().findShortcut(operation);
  70. if(shortcut == EShortcut::NONE)
  71. logGlobal->error("Shortcut %s in joystick config key %s is invalid.", operation, key);
  72. else
  73. shortcuts.push_back(shortcut);
  74. }
  75. }
  76. if(!shortcuts.empty())
  77. triggerShortcutsMap.emplace(triggerAxis, std::move(shortcuts));
  78. }
  79. void GameControllerConfig::parseButton(const std::string & key, const JsonNode & value)
  80. {
  81. std::vector<std::string> operations = getOperations(key, value);
  82. SDL_GameControllerButton button = SDL_GameControllerGetButtonFromString(key.c_str());
  83. if(button == SDL_CONTROLLER_BUTTON_INVALID)
  84. {
  85. logGlobal->error("Joystick config key %s is invalid.", key);
  86. return;
  87. }
  88. std::vector<EShortcut> shortcuts;
  89. for(const auto & operation : operations)
  90. {
  91. if(operation == "mouseLeftClick")
  92. {
  93. leftClickButtonSet.insert(button);
  94. }
  95. else if(operation == "mouseRightClick")
  96. {
  97. rightClickButtonSet.insert(button);
  98. }
  99. else
  100. {
  101. EShortcut shortcut = GH.shortcuts().findShortcut(operation);
  102. if(shortcut == EShortcut::NONE)
  103. logGlobal->error("Shortcut %s in joystick config key %s is invalid.", operation, key);
  104. else
  105. shortcuts.push_back(shortcut);
  106. }
  107. }
  108. if(!shortcuts.empty())
  109. buttonShortcutsMap.emplace(button, std::move(shortcuts));
  110. }
  111. const AxisType & GameControllerConfig::getLeftAxisType()
  112. {
  113. return leftAxisType;
  114. }
  115. const AxisType & GameControllerConfig::getRightAxisType()
  116. {
  117. return rightAxisType;
  118. }
  119. std::vector<std::string> GameControllerConfig::getOperations(const std::string & key, const JsonNode & value)
  120. {
  121. std::vector<std::string> operations;
  122. if(value.isString())
  123. {
  124. operations.push_back(value.String());
  125. }
  126. else if(value.isVector())
  127. {
  128. for(auto const & entryVector : value.Vector())
  129. {
  130. if(!entryVector.isString())
  131. logGlobal->error("The vector of joystick config key %s can not contain non-string element.", key);
  132. else
  133. operations.push_back(entryVector.String());
  134. }
  135. }
  136. else
  137. {
  138. logGlobal->error("The value of joystick config key %s should be string or string vector.", key);
  139. }
  140. return operations;
  141. }
  142. bool GameControllerConfig::isLeftClickButton(int buttonValue)
  143. {
  144. SDL_GameControllerButton button = static_cast<SDL_GameControllerButton>(buttonValue);
  145. return leftClickButtonSet.find(button) != leftClickButtonSet.end();
  146. }
  147. bool GameControllerConfig::isRightClickButton(int buttonValue)
  148. {
  149. SDL_GameControllerButton button = static_cast<SDL_GameControllerButton>(buttonValue);
  150. return rightClickButtonSet.find(button) != rightClickButtonSet.end();
  151. }
  152. bool GameControllerConfig::isShortcutsButton(int buttonValue)
  153. {
  154. SDL_GameControllerButton button = static_cast<SDL_GameControllerButton>(buttonValue);
  155. return buttonShortcutsMap.find(button) != buttonShortcutsMap.end();
  156. }
  157. const std::vector<EShortcut> & GameControllerConfig::getButtonShortcuts(int buttonValue)
  158. {
  159. SDL_GameControllerButton button = static_cast<SDL_GameControllerButton>(buttonValue);
  160. auto it = buttonShortcutsMap.find(button);
  161. if(it != buttonShortcutsMap.end())
  162. return it->second;
  163. static std::vector<EShortcut> emptyVec;
  164. return emptyVec;
  165. }
  166. bool GameControllerConfig::isLeftClickTrigger(int axisValue)
  167. {
  168. SDL_GameControllerAxis axis = static_cast<SDL_GameControllerAxis>(axisValue);
  169. return leftClickTriggerSet.find(axis) != leftClickTriggerSet.end();
  170. }
  171. bool GameControllerConfig::isRightClickTrigger(int axisValue)
  172. {
  173. SDL_GameControllerAxis axis = static_cast<SDL_GameControllerAxis>(axisValue);
  174. return rightClickTriggerSet.find(axis) != rightClickTriggerSet.end();
  175. }
  176. bool GameControllerConfig::isShortcutsTrigger(int axisValue)
  177. {
  178. SDL_GameControllerAxis axis = static_cast<SDL_GameControllerAxis>(axisValue);
  179. return triggerShortcutsMap.find(axis) != triggerShortcutsMap.end();
  180. }
  181. const std::vector<EShortcut> & GameControllerConfig::getTriggerShortcuts(int axisValue)
  182. {
  183. SDL_GameControllerAxis axis = static_cast<SDL_GameControllerAxis>(axisValue);
  184. auto it = triggerShortcutsMap.find(axis);
  185. if(it != triggerShortcutsMap.end())
  186. return it->second;
  187. static std::vector<EShortcut> emptyVec;
  188. return emptyVec;
  189. }