InputSourceGameController.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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/CursorHandler.h"
  15. #include "../gui/CGuiHandler.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. lastCheckTime(0),
  25. cursorAxisValueX(0),
  26. cursorAxisValueY(0),
  27. cursorPlanDisX(0.0),
  28. cursorPlanDisY(0.0),
  29. scrollAxisMoved(false),
  30. scrollStart(Point(0,0)),
  31. scrollCurrent(Point(0,0)),
  32. scrollAxisValueX(0),
  33. scrollAxisValueY(0),
  34. scrollPlanDisX(0.0),
  35. scrollPlanDisY(0.0)
  36. {
  37. tryOpenAllGameControllers();
  38. }
  39. void InputSourceGameController::tryOpenAllGameControllers()
  40. {
  41. for(int i = 0; i < SDL_NumJoysticks(); ++i)
  42. if(SDL_IsGameController(i))
  43. openGameController(i);
  44. else
  45. logGlobal->warn("Joystick %d is an unsupported game controller!", i);
  46. }
  47. void InputSourceGameController::openGameController(int index)
  48. {
  49. SDL_GameController * controller = SDL_GameControllerOpen(index);
  50. if(!controller)
  51. {
  52. logGlobal->error("Fail to open game controller %d!", index);
  53. return;
  54. }
  55. GameControllerPtr controllerPtr(controller, gameControllerDeleter);
  56. // Need to save joystick index for event. Joystick index may not be equal to index sometimes.
  57. int joystickIndex = getJoystickIndex(controllerPtr.get());
  58. if(joystickIndex < 0)
  59. {
  60. logGlobal->error("Fail to get joystick index of game controller %d!", index);
  61. return;
  62. }
  63. if(gameControllerMap.find(joystickIndex) != gameControllerMap.end())
  64. {
  65. logGlobal->warn("Game controller with joystick index %d is already opened.", joystickIndex);
  66. return;
  67. }
  68. gameControllerMap.emplace(joystickIndex, std::move(controllerPtr));
  69. }
  70. int InputSourceGameController::getJoystickIndex(SDL_GameController * controller)
  71. {
  72. SDL_Joystick* joystick = SDL_GameControllerGetJoystick(controller);
  73. if(!joystick)
  74. return -1;
  75. SDL_JoystickID instanceID = SDL_JoystickInstanceID(joystick);
  76. if(instanceID < 0)
  77. return -1;
  78. return (int)instanceID;
  79. }
  80. void InputSourceGameController::handleEventDeviceAdded(const SDL_ControllerDeviceEvent & device)
  81. {
  82. if(gameControllerMap.find(device.which) != gameControllerMap.end())
  83. {
  84. logGlobal->warn("Game controller %d is already opened.", device.which);
  85. return;
  86. }
  87. openGameController(device.which);
  88. }
  89. void InputSourceGameController::handleEventDeviceRemoved(const SDL_ControllerDeviceEvent & device)
  90. {
  91. if(gameControllerMap.find(device.which) == gameControllerMap.end())
  92. {
  93. logGlobal->warn("Game controller %d is not opened before.", device.which);
  94. return;
  95. }
  96. gameControllerMap.erase(device.which);
  97. }
  98. void InputSourceGameController::handleEventDeviceRemapped(const SDL_ControllerDeviceEvent & device)
  99. {
  100. if(gameControllerMap.find(device.which) == gameControllerMap.end())
  101. {
  102. logGlobal->warn("Game controller %d is not opened.", device.which);
  103. return;
  104. }
  105. gameControllerMap.erase(device.which);
  106. openGameController(device.which);
  107. }
  108. int InputSourceGameController::getRealAxisValue(int value)
  109. {
  110. if(value < AXIS_DEAD_ZOOM && value > -AXIS_DEAD_ZOOM)
  111. return 0;
  112. if(value > AXIS_MAX_ZOOM)
  113. return AXIS_MAX_ZOOM;
  114. if(value < -AXIS_MAX_ZOOM)
  115. return -AXIS_MAX_ZOOM;
  116. int base = value > 0 ? AXIS_DEAD_ZOOM: -AXIS_DEAD_ZOOM;
  117. return (value - base) * AXIS_MAX_ZOOM / (AXIS_MAX_ZOOM - AXIS_DEAD_ZOOM);
  118. }
  119. void InputSourceGameController::dispatchTriggerShortcuts(const std::vector<EShortcut> & shortcutsVector, int axisValue)
  120. {
  121. if(axisValue >= TRIGGER_PRESS_THRESHOLD)
  122. GH.events().dispatchShortcutPressed(shortcutsVector);
  123. else
  124. GH.events().dispatchShortcutReleased(shortcutsVector);
  125. }
  126. void InputSourceGameController::dispatchTriggerLeftClick(int axisValue)
  127. {
  128. const Point & position = GH.input().getCursorPosition();
  129. if(axisValue >= TRIGGER_PRESS_THRESHOLD)
  130. GH.events().dispatchMouseLeftButtonPressed(position, 0);
  131. else
  132. GH.events().dispatchMouseLeftButtonReleased(position, 0);
  133. }
  134. void InputSourceGameController::dispatchTriggerRightClick(int axisValue)
  135. {
  136. const Point & position = GH.input().getCursorPosition();
  137. if(axisValue >= TRIGGER_PRESS_THRESHOLD)
  138. GH.events().dispatchShowPopup(position, 0);
  139. else
  140. GH.events().dispatchClosePopup(position);
  141. }
  142. void InputSourceGameController::handleEventAxisMotion(const SDL_ControllerAxisEvent & axis)
  143. {
  144. tryToConvertCursor();
  145. if(axis.axis == SDL_CONTROLLER_AXIS_LEFTX)
  146. {
  147. if(config.getLeftAxisType() == AxisType::CURSOR_MOTION)
  148. cursorAxisValueX = getRealAxisValue(axis.value);
  149. else if(config.getLeftAxisType() == AxisType::MAP_SCROLL)
  150. scrollAxisValueX = getRealAxisValue(axis.value);
  151. }
  152. else if(axis.axis == SDL_CONTROLLER_AXIS_LEFTY)
  153. {
  154. if(config.getLeftAxisType() == AxisType::CURSOR_MOTION)
  155. cursorAxisValueY = getRealAxisValue(axis.value);
  156. else if(config.getLeftAxisType() == AxisType::MAP_SCROLL)
  157. scrollAxisValueY = getRealAxisValue(axis.value);
  158. }
  159. if(axis.axis == SDL_CONTROLLER_AXIS_RIGHTX)
  160. {
  161. if(config.getRightAxisType() == AxisType::CURSOR_MOTION)
  162. cursorAxisValueX = getRealAxisValue(axis.value);
  163. else if(config.getRightAxisType() == AxisType::MAP_SCROLL)
  164. scrollAxisValueX = getRealAxisValue(axis.value);
  165. }
  166. else if(axis.axis == SDL_CONTROLLER_AXIS_RIGHTY)
  167. {
  168. if(config.getRightAxisType() == AxisType::CURSOR_MOTION)
  169. cursorAxisValueY = getRealAxisValue(axis.value);
  170. else if(config.getRightAxisType() == AxisType::MAP_SCROLL)
  171. scrollAxisValueY = getRealAxisValue(axis.value);
  172. }
  173. else if(config.isLeftClickTrigger(axis.axis))
  174. {
  175. dispatchTriggerLeftClick(axis.value);
  176. }
  177. else if(config.isRightClickTrigger(axis.axis))
  178. {
  179. dispatchTriggerRightClick(axis.value);
  180. }
  181. else if(config.isShortcutsTrigger(axis.axis))
  182. {
  183. const auto & shortcutsVector = config.getTriggerShortcuts(axis.axis);
  184. dispatchTriggerShortcuts(shortcutsVector, axis.value);
  185. }
  186. }
  187. void InputSourceGameController::tryToConvertCursor()
  188. {
  189. if(CCS && CCS->curh && CCS->curh->getShowType() == Cursor::ShowType::HARDWARE)
  190. {
  191. const Point & cursorPosition = CCS->curh->getCursorPosition();
  192. CCS->curh->ChangeCursor(Cursor::ShowType::SOFTWARE);
  193. CCS->curh->cursorMove(cursorPosition.x, cursorPosition.y);
  194. GH.input().setCursorPosition(cursorPosition);
  195. }
  196. }
  197. void InputSourceGameController::handleEventButtonDown(const SDL_ControllerButtonEvent & button)
  198. {
  199. const Point & position = GH.input().getCursorPosition();
  200. if(config.isLeftClickButton(button.button))
  201. {
  202. GH.events().dispatchMouseLeftButtonPressed(position, 0);
  203. }
  204. if(config.isRightClickButton(button.button))
  205. {
  206. GH.events().dispatchShowPopup(position, 0);
  207. }
  208. if(config.isShortcutsButton(button.button))
  209. {
  210. const auto & shortcutsVector = config.getButtonShortcuts(button.button);
  211. GH.events().dispatchShortcutPressed(shortcutsVector);
  212. }
  213. }
  214. void InputSourceGameController::handleEventButtonUp(const SDL_ControllerButtonEvent & button)
  215. {
  216. const Point & position = GH.input().getCursorPosition();
  217. if(config.isLeftClickButton(button.button))
  218. {
  219. GH.events().dispatchMouseLeftButtonReleased(position, 0);
  220. }
  221. if(config.isRightClickButton(button.button))
  222. {
  223. GH.events().dispatchClosePopup(position);
  224. }
  225. if(config.isShortcutsButton(button.button))
  226. {
  227. const auto & shortcutsVector = config.getButtonShortcuts(button.button);
  228. GH.events().dispatchShortcutReleased(shortcutsVector);
  229. }
  230. }
  231. void InputSourceGameController::doCursorMove(int deltaX, int deltaY)
  232. {
  233. if(deltaX == 0 && deltaY == 0)
  234. return;
  235. const Point & screenSize = GH.screenDimensions();
  236. const Point & cursorPosition = GH.getCursorPosition();
  237. int newX = std::min(std::max(cursorPosition.x + deltaX, 0), screenSize.x);
  238. int newY = std::min(std::max(cursorPosition.y + deltaY, 0), screenSize.y);
  239. Point targetPosition{newX, newY};
  240. GH.input().setCursorPosition(targetPosition);
  241. if(CCS && CCS->curh)
  242. CCS->curh->cursorMove(GH.getCursorPosition().x, GH.getCursorPosition().y);
  243. }
  244. int InputSourceGameController::getMoveDis(float planDis)
  245. {
  246. if(planDis >= 0)
  247. return std::floor(planDis);
  248. else
  249. return std::ceil(planDis);
  250. }
  251. void InputSourceGameController::handleUpdate()
  252. {
  253. auto now = std::chrono::high_resolution_clock::now();
  254. auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
  255. if (lastCheckTime == 0) {
  256. lastCheckTime = nowMs;
  257. return;
  258. }
  259. long long deltaTime = nowMs - lastCheckTime;
  260. handleCursorUpdate(deltaTime);
  261. handleScrollUpdate(deltaTime);
  262. lastCheckTime = nowMs;
  263. }
  264. void InputSourceGameController::handleCursorUpdate(long long deltaTime)
  265. {
  266. if(cursorAxisValueX == 0)
  267. cursorPlanDisX = 0;
  268. else
  269. cursorPlanDisX += ((float)deltaTime / 1000) * ((float)cursorAxisValueX / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
  270. if(cursorAxisValueY == 0)
  271. cursorPlanDisY = 0;
  272. else
  273. cursorPlanDisY += ((float)deltaTime / 1000) * ((float)cursorAxisValueY / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
  274. int moveDisX = getMoveDis(cursorPlanDisX);
  275. int moveDisY = getMoveDis(cursorPlanDisY);
  276. cursorPlanDisX -= moveDisX;
  277. cursorPlanDisY -= moveDisY;
  278. doCursorMove(moveDisX, moveDisY);
  279. }
  280. void InputSourceGameController::handleScrollUpdate(long long deltaTime)
  281. {
  282. if(!scrollAxisMoved && isScrollAxisReleased())
  283. {
  284. return;
  285. }
  286. else if(!scrollAxisMoved && !isScrollAxisReleased())
  287. {
  288. scrollAxisMoved = true;
  289. scrollCurrent = scrollStart = GH.input().getCursorPosition();
  290. GH.events().dispatchGesturePanningStarted(scrollStart);
  291. }
  292. else if(scrollAxisMoved && isScrollAxisReleased())
  293. {
  294. GH.events().dispatchGesturePanningEnded(scrollStart, scrollCurrent);
  295. scrollAxisMoved = false;
  296. scrollPlanDisX = scrollPlanDisY = 0;
  297. return;
  298. }
  299. scrollPlanDisX += ((float)deltaTime / 1000) * ((float)scrollAxisValueX / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
  300. scrollPlanDisY += ((float)deltaTime / 1000) * ((float)scrollAxisValueY / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
  301. int moveDisX = getMoveDis(scrollPlanDisX);
  302. int moveDisY = getMoveDis(scrollPlanDisY);
  303. if(moveDisX != 0 || moveDisY != 0)
  304. {
  305. scrollPlanDisX -= moveDisX;
  306. scrollPlanDisY -= moveDisY;
  307. scrollCurrent.x += moveDisX;
  308. scrollCurrent.y += moveDisY;
  309. Point distance(moveDisX, moveDisY);
  310. GH.events().dispatchGesturePanning(scrollStart, scrollCurrent, distance);
  311. }
  312. }
  313. bool InputSourceGameController::isScrollAxisReleased()
  314. {
  315. return scrollAxisValueX == 0 && scrollAxisValueY == 0;
  316. }