InputSourceGameController.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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::dispatchAxisShortcuts(const std::vector<EShortcut> & shortcutsVector, SDL_GameControllerAxis axisID, int axisValue)
  120. {
  121. if(axisValue >= TRIGGER_PRESS_THRESHOLD)
  122. {
  123. if (!pressedAxes.count(axisID))
  124. {
  125. GH.events().dispatchShortcutPressed(shortcutsVector);
  126. pressedAxes.insert(axisID);
  127. }
  128. }
  129. else
  130. {
  131. if (pressedAxes.count(axisID))
  132. {
  133. GH.events().dispatchShortcutReleased(shortcutsVector);
  134. pressedAxes.erase(axisID);
  135. }
  136. }
  137. }
  138. void InputSourceGameController::handleEventAxisMotion(const SDL_ControllerAxisEvent & axis)
  139. {
  140. tryToConvertCursor();
  141. SDL_GameControllerAxis axisID = static_cast<SDL_GameControllerAxis>(axis.axis);
  142. std::string axisName = SDL_GameControllerGetStringForAxis(axisID);
  143. auto axisActions = GH.shortcuts().translateJoystickAxis(axisName);
  144. auto buttonActions = GH.shortcuts().translateJoystickButton(axisName);
  145. for (auto const & action : axisActions)
  146. {
  147. switch (action)
  148. {
  149. case EShortcut::MOUSE_CURSOR_X:
  150. cursorAxisValueX = getRealAxisValue(axis.value);
  151. break;
  152. case EShortcut::MOUSE_CURSOR_Y:
  153. cursorAxisValueY = getRealAxisValue(axis.value);
  154. break;
  155. case EShortcut::MOUSE_SWIPE_X:
  156. scrollAxisValueX = getRealAxisValue(axis.value);
  157. break;
  158. case EShortcut::MOUSE_SWIPE_Y:
  159. scrollAxisValueY = getRealAxisValue(axis.value);
  160. break;
  161. }
  162. }
  163. dispatchAxisShortcuts(buttonActions, axisID, axis.value);
  164. }
  165. void InputSourceGameController::tryToConvertCursor()
  166. {
  167. assert(CCS);
  168. assert(CCS->curh);
  169. if(CCS->curh->getShowType() == Cursor::ShowType::HARDWARE)
  170. {
  171. const Point & cursorPosition = GH.getCursorPosition();
  172. CCS->curh->ChangeCursor(Cursor::ShowType::SOFTWARE);
  173. CCS->curh->cursorMove(cursorPosition.x, cursorPosition.y);
  174. GH.input().setCursorPosition(cursorPosition);
  175. }
  176. }
  177. void InputSourceGameController::handleEventButtonDown(const SDL_ControllerButtonEvent & button)
  178. {
  179. std::string buttonName = SDL_GameControllerGetStringForButton(static_cast<SDL_GameControllerButton>(button.button));
  180. const auto & shortcutsVector = GH.shortcuts().translateJoystickButton(buttonName);
  181. GH.events().dispatchShortcutPressed(shortcutsVector);
  182. }
  183. void InputSourceGameController::handleEventButtonUp(const SDL_ControllerButtonEvent & button)
  184. {
  185. std::string buttonName = SDL_GameControllerGetStringForButton(static_cast<SDL_GameControllerButton>(button.button));
  186. const auto & shortcutsVector = GH.shortcuts().translateJoystickButton(buttonName);
  187. GH.events().dispatchShortcutReleased(shortcutsVector);
  188. }
  189. void InputSourceGameController::doCursorMove(int deltaX, int deltaY)
  190. {
  191. if(deltaX == 0 && deltaY == 0)
  192. return;
  193. const Point & screenSize = GH.screenDimensions();
  194. const Point & cursorPosition = GH.getCursorPosition();
  195. int newX = std::min(std::max(cursorPosition.x + deltaX, 0), screenSize.x);
  196. int newY = std::min(std::max(cursorPosition.y + deltaY, 0), screenSize.y);
  197. Point targetPosition{newX, newY};
  198. GH.input().setCursorPosition(targetPosition);
  199. if(CCS && CCS->curh)
  200. CCS->curh->cursorMove(GH.getCursorPosition().x, GH.getCursorPosition().y);
  201. }
  202. int InputSourceGameController::getMoveDis(float planDis)
  203. {
  204. if(planDis >= 0)
  205. return std::floor(planDis);
  206. else
  207. return std::ceil(planDis);
  208. }
  209. void InputSourceGameController::handleUpdate()
  210. {
  211. auto now = std::chrono::high_resolution_clock::now();
  212. auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
  213. if (lastCheckTime == 0) {
  214. lastCheckTime = nowMs;
  215. return;
  216. }
  217. long long deltaTime = nowMs - lastCheckTime;
  218. handleCursorUpdate(deltaTime);
  219. handleScrollUpdate(deltaTime);
  220. lastCheckTime = nowMs;
  221. }
  222. void InputSourceGameController::handleCursorUpdate(long long deltaTime)
  223. {
  224. if(cursorAxisValueX == 0)
  225. cursorPlanDisX = 0;
  226. else
  227. cursorPlanDisX += ((float)deltaTime / 1000) * ((float)cursorAxisValueX / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
  228. if(cursorAxisValueY == 0)
  229. cursorPlanDisY = 0;
  230. else
  231. cursorPlanDisY += ((float)deltaTime / 1000) * ((float)cursorAxisValueY / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
  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(long long deltaTime)
  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. scrollPlanDisX += ((float)deltaTime / 1000) * ((float)scrollAxisValueX / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
  258. scrollPlanDisY += ((float)deltaTime / 1000) * ((float)scrollAxisValueY / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
  259. int moveDisX = getMoveDis(scrollPlanDisX);
  260. int moveDisY = getMoveDis(scrollPlanDisY);
  261. if(moveDisX != 0 || moveDisY != 0)
  262. {
  263. scrollPlanDisX -= moveDisX;
  264. scrollPlanDisY -= moveDisY;
  265. scrollCurrent.x += moveDisX;
  266. scrollCurrent.y += moveDisY;
  267. Point distance(moveDisX, moveDisY);
  268. GH.events().dispatchGesturePanning(scrollStart, scrollCurrent, distance);
  269. }
  270. }
  271. bool InputSourceGameController::isScrollAxisReleased()
  272. {
  273. return scrollAxisValueX == 0 && scrollAxisValueY == 0;
  274. }