InputSourceGameController.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * InputSourceGameController.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 "InputSourceGameController.h"
  12. #include "InputHandler.h"
  13. #include "../CGameInfo.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../gui/CursorHandler.h"
  16. #include "../gui/EventDispatcher.h"
  17. #include "../gui/ShortcutHandler.h"
  18. void InputSourceGameController::gameControllerDeleter(SDL_GameController * gameController)
  19. {
  20. if(gameController)
  21. SDL_GameControllerClose(gameController);
  22. }
  23. InputSourceGameController::InputSourceGameController():
  24. cursorAxisValueX(0),
  25. cursorAxisValueY(0),
  26. cursorPlanDisX(0.0),
  27. cursorPlanDisY(0.0),
  28. scrollAxisMoved(false),
  29. scrollStart(Point(0,0)),
  30. scrollCurrent(Point(0,0)),
  31. scrollAxisValueX(0),
  32. scrollAxisValueY(0),
  33. scrollPlanDisX(0.0),
  34. scrollPlanDisY(0.0)
  35. {
  36. tryOpenAllGameControllers();
  37. }
  38. void InputSourceGameController::tryOpenAllGameControllers()
  39. {
  40. for(int i = 0; i < SDL_NumJoysticks(); ++i)
  41. if(SDL_IsGameController(i))
  42. openGameController(i);
  43. else
  44. logGlobal->warn("Joystick %d is an unsupported game controller!", i);
  45. }
  46. void InputSourceGameController::openGameController(int index)
  47. {
  48. SDL_GameController * controller = SDL_GameControllerOpen(index);
  49. if(!controller)
  50. {
  51. logGlobal->error("Fail to open game controller %d!", index);
  52. return;
  53. }
  54. GameControllerPtr controllerPtr(controller, &gameControllerDeleter);
  55. // Need to save joystick index for event. Joystick index may not be equal to index sometimes.
  56. int joystickIndex = getJoystickIndex(controllerPtr.get());
  57. if(joystickIndex < 0)
  58. {
  59. logGlobal->error("Fail to get joystick index of game controller %d!", index);
  60. return;
  61. }
  62. if(gameControllerMap.find(joystickIndex) != gameControllerMap.end())
  63. {
  64. logGlobal->warn("Game controller with joystick index %d is already opened.", joystickIndex);
  65. return;
  66. }
  67. gameControllerMap.try_emplace(joystickIndex, std::move(controllerPtr));
  68. }
  69. int InputSourceGameController::getJoystickIndex(SDL_GameController * controller)
  70. {
  71. SDL_Joystick * joystick = SDL_GameControllerGetJoystick(controller);
  72. if(!joystick)
  73. return -1;
  74. SDL_JoystickID instanceID = SDL_JoystickInstanceID(joystick);
  75. if(instanceID < 0)
  76. return -1;
  77. return instanceID;
  78. }
  79. void InputSourceGameController::handleEventDeviceAdded(const SDL_ControllerDeviceEvent & device)
  80. {
  81. if(gameControllerMap.find(device.which) != gameControllerMap.end())
  82. {
  83. logGlobal->warn("Game controller %d is already opened.", device.which);
  84. return;
  85. }
  86. openGameController(device.which);
  87. }
  88. void InputSourceGameController::handleEventDeviceRemoved(const SDL_ControllerDeviceEvent & device)
  89. {
  90. if(gameControllerMap.find(device.which) == gameControllerMap.end())
  91. {
  92. logGlobal->warn("Game controller %d is not opened before.", device.which);
  93. return;
  94. }
  95. gameControllerMap.erase(device.which);
  96. }
  97. void InputSourceGameController::handleEventDeviceRemapped(const SDL_ControllerDeviceEvent & device)
  98. {
  99. if(gameControllerMap.find(device.which) == gameControllerMap.end())
  100. {
  101. logGlobal->warn("Game controller %d is not opened.", device.which);
  102. return;
  103. }
  104. gameControllerMap.erase(device.which);
  105. openGameController(device.which);
  106. }
  107. int InputSourceGameController::getRealAxisValue(int value)
  108. {
  109. if(value < AXIS_DEAD_ZOOM && value > -AXIS_DEAD_ZOOM)
  110. return 0;
  111. if(value > AXIS_MAX_ZOOM)
  112. return AXIS_MAX_ZOOM;
  113. if(value < -AXIS_MAX_ZOOM)
  114. return -AXIS_MAX_ZOOM;
  115. int base = value > 0 ? AXIS_DEAD_ZOOM : -AXIS_DEAD_ZOOM;
  116. return (value - base) * AXIS_MAX_ZOOM / (AXIS_MAX_ZOOM - AXIS_DEAD_ZOOM);
  117. }
  118. void InputSourceGameController::dispatchAxisShortcuts(const std::vector<EShortcut> & shortcutsVector, SDL_GameControllerAxis axisID, int axisValue)
  119. {
  120. if(axisValue >= TRIGGER_PRESS_THRESHOLD)
  121. {
  122. if(!pressedAxes.count(axisID))
  123. {
  124. GH.events().dispatchShortcutPressed(shortcutsVector);
  125. pressedAxes.insert(axisID);
  126. }
  127. }
  128. else
  129. {
  130. if(pressedAxes.count(axisID))
  131. {
  132. GH.events().dispatchShortcutReleased(shortcutsVector);
  133. pressedAxes.erase(axisID);
  134. }
  135. }
  136. }
  137. void InputSourceGameController::handleEventAxisMotion(const SDL_ControllerAxisEvent & axis)
  138. {
  139. tryToConvertCursor();
  140. SDL_GameControllerAxis axisID = static_cast<SDL_GameControllerAxis>(axis.axis);
  141. std::string axisName = SDL_GameControllerGetStringForAxis(axisID);
  142. auto axisActions = GH.shortcuts().translateJoystickAxis(axisName);
  143. auto buttonActions = GH.shortcuts().translateJoystickButton(axisName);
  144. for(const auto & action : axisActions)
  145. {
  146. switch(action)
  147. {
  148. case EShortcut::MOUSE_CURSOR_X:
  149. cursorAxisValueX = getRealAxisValue(axis.value);
  150. break;
  151. case EShortcut::MOUSE_CURSOR_Y:
  152. cursorAxisValueY = getRealAxisValue(axis.value);
  153. break;
  154. case EShortcut::MOUSE_SWIPE_X:
  155. scrollAxisValueX = getRealAxisValue(axis.value);
  156. break;
  157. case EShortcut::MOUSE_SWIPE_Y:
  158. scrollAxisValueY = getRealAxisValue(axis.value);
  159. break;
  160. }
  161. }
  162. dispatchAxisShortcuts(buttonActions, axisID, axis.value);
  163. }
  164. void InputSourceGameController::tryToConvertCursor()
  165. {
  166. assert(CCS);
  167. assert(CCS->curh);
  168. if(CCS->curh->getShowType() == Cursor::ShowType::HARDWARE)
  169. {
  170. const Point & cursorPosition = GH.getCursorPosition();
  171. CCS->curh->changeCursor(Cursor::ShowType::SOFTWARE);
  172. CCS->curh->cursorMove(cursorPosition.x, cursorPosition.y);
  173. GH.input().setCursorPosition(cursorPosition);
  174. }
  175. }
  176. void InputSourceGameController::handleEventButtonDown(const SDL_ControllerButtonEvent & button)
  177. {
  178. std::string buttonName = SDL_GameControllerGetStringForButton(static_cast<SDL_GameControllerButton>(button.button));
  179. const auto & shortcutsVector = GH.shortcuts().translateJoystickButton(buttonName);
  180. GH.events().dispatchShortcutPressed(shortcutsVector);
  181. }
  182. void InputSourceGameController::handleEventButtonUp(const SDL_ControllerButtonEvent & button)
  183. {
  184. std::string buttonName = SDL_GameControllerGetStringForButton(static_cast<SDL_GameControllerButton>(button.button));
  185. const auto & shortcutsVector = GH.shortcuts().translateJoystickButton(buttonName);
  186. GH.events().dispatchShortcutReleased(shortcutsVector);
  187. }
  188. void InputSourceGameController::doCursorMove(int deltaX, int deltaY)
  189. {
  190. if(deltaX == 0 && deltaY == 0)
  191. return;
  192. const Point & screenSize = GH.screenDimensions();
  193. const Point & cursorPosition = GH.getCursorPosition();
  194. int newX = std::min(std::max(cursorPosition.x + deltaX, 0), screenSize.x);
  195. int newY = std::min(std::max(cursorPosition.y + deltaY, 0), screenSize.y);
  196. Point targetPosition{newX, newY};
  197. GH.input().setCursorPosition(targetPosition);
  198. if(CCS && CCS->curh)
  199. CCS->curh->cursorMove(GH.getCursorPosition().x, GH.getCursorPosition().y);
  200. }
  201. int InputSourceGameController::getMoveDis(float planDis)
  202. {
  203. if(planDis >= 0)
  204. return std::floor(planDis);
  205. else
  206. return std::ceil(planDis);
  207. }
  208. void InputSourceGameController::handleUpdate()
  209. {
  210. std::chrono::steady_clock::time_point nowMs = std::chrono::steady_clock::now();
  211. if(lastCheckTime == std::chrono::steady_clock::time_point())
  212. {
  213. lastCheckTime = nowMs;
  214. return;
  215. }
  216. int32_t deltaTime = std::chrono::duration_cast<std::chrono::milliseconds>(nowMs - lastCheckTime).count();
  217. handleCursorUpdate(deltaTime);
  218. handleScrollUpdate(deltaTime);
  219. lastCheckTime = nowMs;
  220. }
  221. void InputSourceGameController::handleCursorUpdate(int32_t deltaTimeMs)
  222. {
  223. float deltaTimeSeconds = static_cast<float>(deltaTimeMs) / 1000;
  224. if(cursorAxisValueX == 0)
  225. cursorPlanDisX = 0;
  226. else
  227. cursorPlanDisX += deltaTimeSeconds * AXIS_MOVE_SPEED * cursorAxisValueX / AXIS_MAX_ZOOM;
  228. if(cursorAxisValueY == 0)
  229. cursorPlanDisY = 0;
  230. else
  231. cursorPlanDisY += deltaTimeSeconds * AXIS_MOVE_SPEED * cursorAxisValueY / AXIS_MAX_ZOOM;
  232. int moveDisX = getMoveDis(cursorPlanDisX);
  233. int moveDisY = getMoveDis(cursorPlanDisY);
  234. cursorPlanDisX -= moveDisX;
  235. cursorPlanDisY -= moveDisY;
  236. doCursorMove(moveDisX, moveDisY);
  237. }
  238. void InputSourceGameController::handleScrollUpdate(int32_t deltaTimeMs)
  239. {
  240. if(!scrollAxisMoved && isScrollAxisReleased())
  241. {
  242. return;
  243. }
  244. else if(!scrollAxisMoved && !isScrollAxisReleased())
  245. {
  246. scrollAxisMoved = true;
  247. scrollCurrent = scrollStart = GH.input().getCursorPosition();
  248. GH.events().dispatchGesturePanningStarted(scrollStart);
  249. }
  250. else if(scrollAxisMoved && isScrollAxisReleased())
  251. {
  252. GH.events().dispatchGesturePanningEnded(scrollStart, scrollCurrent);
  253. scrollAxisMoved = false;
  254. scrollPlanDisX = scrollPlanDisY = 0;
  255. return;
  256. }
  257. float deltaTimeSeconds = static_cast<float>(deltaTimeMs) / 1000;
  258. scrollPlanDisX += deltaTimeSeconds * AXIS_MOVE_SPEED * scrollAxisValueX / AXIS_MAX_ZOOM;
  259. scrollPlanDisY += deltaTimeSeconds * AXIS_MOVE_SPEED * scrollAxisValueY / AXIS_MAX_ZOOM;
  260. int moveDisX = getMoveDis(scrollPlanDisX);
  261. int moveDisY = getMoveDis(scrollPlanDisY);
  262. if(moveDisX != 0 || moveDisY != 0)
  263. {
  264. scrollPlanDisX -= moveDisX;
  265. scrollPlanDisY -= moveDisY;
  266. scrollCurrent.x += moveDisX;
  267. scrollCurrent.y += moveDisY;
  268. Point distance(moveDisX, moveDisY);
  269. GH.events().dispatchGesturePanning(scrollStart, scrollCurrent, distance);
  270. }
  271. }
  272. bool InputSourceGameController::isScrollAxisReleased()
  273. {
  274. return scrollAxisValueX == 0 && scrollAxisValueY == 0;
  275. }