InputSourceGameController.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. axisValueX(0),
  26. axisValueY(0),
  27. planDisX(0.0),
  28. planDisY(0.0)
  29. {
  30. tryOpenAllGameControllers();
  31. }
  32. void InputSourceGameController::tryOpenAllGameControllers()
  33. {
  34. for(int i = 0; i < SDL_NumJoysticks(); ++i)
  35. if(SDL_IsGameController(i))
  36. openGameController(i);
  37. else
  38. logGlobal->warn("Joystick %d is an unsupported game controller!", i);
  39. }
  40. void InputSourceGameController::openGameController(int index)
  41. {
  42. SDL_GameController * controller = SDL_GameControllerOpen(index);
  43. if(!controller)
  44. {
  45. logGlobal->error("Fail to open game controller %d!", index);
  46. return;
  47. }
  48. GameControllerPtr controllerPtr(controller, gameControllerDeleter);
  49. // Need to save joystick index for event. Joystick index may not be equal to index sometimes.
  50. int joystickIndex = getJoystickIndex(controllerPtr.get());
  51. if(joystickIndex < 0)
  52. {
  53. logGlobal->error("Fail to get joystick index of game controller %d!", index);
  54. return;
  55. }
  56. if(gameControllerMap.find(joystickIndex) != gameControllerMap.end())
  57. {
  58. logGlobal->warn("Game controller with joystick index %d is already opened.", joystickIndex);
  59. return;
  60. }
  61. gameControllerMap.emplace(joystickIndex, std::move(controllerPtr));
  62. }
  63. int InputSourceGameController::getJoystickIndex(SDL_GameController * controller)
  64. {
  65. SDL_Joystick* joystick = SDL_GameControllerGetJoystick(controller);
  66. if(!joystick)
  67. return -1;
  68. SDL_JoystickID instanceID = SDL_JoystickInstanceID(joystick);
  69. if(instanceID < 0)
  70. return -1;
  71. return (int)instanceID;
  72. }
  73. void InputSourceGameController::handleEventDeviceAdded(const SDL_ControllerDeviceEvent & device)
  74. {
  75. if(gameControllerMap.find(device.which) != gameControllerMap.end())
  76. {
  77. logGlobal->warn("Game controller %d is already opened.", device.which);
  78. return;
  79. }
  80. openGameController(device.which);
  81. }
  82. void InputSourceGameController::handleEventDeviceRemoved(const SDL_ControllerDeviceEvent & device)
  83. {
  84. if(gameControllerMap.find(device.which) == gameControllerMap.end())
  85. {
  86. logGlobal->warn("Game controller %d is not opened before.", device.which);
  87. return;
  88. }
  89. gameControllerMap.erase(device.which);
  90. }
  91. void InputSourceGameController::handleEventDeviceRemapped(const SDL_ControllerDeviceEvent & device)
  92. {
  93. if(gameControllerMap.find(device.which) == gameControllerMap.end())
  94. {
  95. logGlobal->warn("Game controller %d is not opened.", device.which);
  96. return;
  97. }
  98. gameControllerMap.erase(device.which);
  99. openGameController(device.which);
  100. }
  101. int InputSourceGameController::getRealAxisValue(int value)
  102. {
  103. if(value < AXIS_DEAD_ZOOM && value > -AXIS_DEAD_ZOOM)
  104. return 0;
  105. if(value > AXIS_MAX_ZOOM)
  106. return AXIS_MAX_ZOOM;
  107. if(value < -AXIS_MAX_ZOOM)
  108. return -AXIS_MAX_ZOOM;
  109. int base = value > 0 ? AXIS_DEAD_ZOOM: -AXIS_DEAD_ZOOM;
  110. return (value - base) * AXIS_MAX_ZOOM / (AXIS_MAX_ZOOM - AXIS_DEAD_ZOOM);
  111. }
  112. void InputSourceGameController::dispatchTriggerShortcuts(const std::vector<EShortcut> & shortcutsVector, int axisValue)
  113. {
  114. if(axisValue >= TRIGGER_PRESS_THRESHOLD)
  115. GH.events().dispatchShortcutPressed(shortcutsVector);
  116. else
  117. GH.events().dispatchShortcutReleased(shortcutsVector);
  118. }
  119. void InputSourceGameController::dispatchTriggerLeftClick(int axisValue)
  120. {
  121. const Point & position = GH.input().getCursorPosition();
  122. if(axisValue >= TRIGGER_PRESS_THRESHOLD)
  123. GH.events().dispatchMouseLeftButtonPressed(position, 0);
  124. else
  125. GH.events().dispatchMouseLeftButtonReleased(position, 0);
  126. }
  127. void InputSourceGameController::dispatchTriggerRightClick(int axisValue)
  128. {
  129. const Point & position = GH.input().getCursorPosition();
  130. if(axisValue >= TRIGGER_PRESS_THRESHOLD)
  131. GH.events().dispatchShowPopup(position, 0);
  132. else
  133. GH.events().dispatchClosePopup(position);
  134. }
  135. void InputSourceGameController::handleEventAxisMotion(const SDL_ControllerAxisEvent & axis)
  136. {
  137. if(axis.axis == SDL_CONTROLLER_AXIS_LEFTX)
  138. {
  139. if(config.getLeftAxisType() == AxisType::CURSOR_MOTION)
  140. axisValueX = getRealAxisValue(axis.value);
  141. }
  142. else if(axis.axis == SDL_CONTROLLER_AXIS_LEFTY)
  143. {
  144. if(config.getLeftAxisType() == AxisType::CURSOR_MOTION)
  145. axisValueY = getRealAxisValue(axis.value);
  146. }
  147. if(axis.axis == SDL_CONTROLLER_AXIS_RIGHTX)
  148. {
  149. if(config.getRightAxisType() == AxisType::CURSOR_MOTION)
  150. axisValueX = getRealAxisValue(axis.value);
  151. }
  152. else if(axis.axis == SDL_CONTROLLER_AXIS_RIGHTY)
  153. {
  154. if(config.getRightAxisType() == AxisType::CURSOR_MOTION)
  155. axisValueY = getRealAxisValue(axis.value);
  156. }
  157. else if(config.isLeftClickTrigger(axis.axis))
  158. {
  159. dispatchTriggerLeftClick(axis.value);
  160. }
  161. else if(config.isRightClickTrigger(axis.axis))
  162. {
  163. dispatchTriggerRightClick(axis.value);
  164. }
  165. else if(config.isShortcutsTrigger(axis.axis))
  166. {
  167. const auto & shortcutsVector = config.getTriggerShortcuts(axis.axis);
  168. dispatchTriggerShortcuts(shortcutsVector, axis.value);
  169. }
  170. }
  171. void InputSourceGameController::handleEventButtonDown(const SDL_ControllerButtonEvent & button)
  172. {
  173. const Point & position = GH.input().getCursorPosition();
  174. if(config.isLeftClickButton(button.button))
  175. {
  176. GH.events().dispatchMouseLeftButtonPressed(position, 0);
  177. }
  178. if(config.isRightClickButton(button.button))
  179. {
  180. GH.events().dispatchShowPopup(position, 0);
  181. }
  182. if(config.isShortcutsButton(button.button))
  183. {
  184. const auto & shortcutsVector = config.getButtonShortcuts(button.button);
  185. GH.events().dispatchShortcutPressed(shortcutsVector);
  186. }
  187. }
  188. void InputSourceGameController::handleEventButtonUp(const SDL_ControllerButtonEvent & button)
  189. {
  190. const Point & position = GH.input().getCursorPosition();
  191. if(config.isLeftClickButton(button.button))
  192. {
  193. GH.events().dispatchMouseLeftButtonReleased(position, 0);
  194. }
  195. if(config.isRightClickButton(button.button))
  196. {
  197. GH.events().dispatchClosePopup(position);
  198. }
  199. if(config.isShortcutsButton(button.button))
  200. {
  201. const auto & shortcutsVector = config.getButtonShortcuts(button.button);
  202. GH.events().dispatchShortcutReleased(shortcutsVector);
  203. }
  204. }
  205. void InputSourceGameController::doCursorMove(int deltaX, int deltaY)
  206. {
  207. if(deltaX == 0 && deltaY == 0)
  208. return;
  209. const Point & screenSize = GH.screenDimensions();
  210. // if joystick is connected when intro video plays, using hardware position will move cursor to the right position.
  211. bool isHardwareCursor = CCS && CCS->curh && CCS->curh->getShowType() == Cursor::ShowType::HARDWARE;
  212. const Point & cursorPosition = isHardwareCursor ? CCS->curh->getCursorPosition() : GH.getCursorPosition();
  213. int newX = std::min(std::max(cursorPosition.x + deltaX, 0), screenSize.x);
  214. int newY = std::min(std::max(cursorPosition.y + deltaY, 0), screenSize.y);
  215. Point targetPosition{newX, newY};
  216. GH.input().setCursorPosition(targetPosition);
  217. if(CCS && CCS->curh) {
  218. if(CCS->curh->getShowType() == Cursor::ShowType::HARDWARE)
  219. CCS->curh->ChangeCursor(Cursor::ShowType::SOFTWARE);
  220. CCS->curh->cursorMove(GH.getCursorPosition().x, GH.getCursorPosition().y);
  221. }
  222. }
  223. int InputSourceGameController::getMoveDis(float planDis)
  224. {
  225. if(planDis >= 0)
  226. return std::floor(planDis);
  227. else
  228. return std::ceil(planDis);
  229. }
  230. void InputSourceGameController::handleUpdate()
  231. {
  232. auto now = std::chrono::high_resolution_clock::now();
  233. auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
  234. if(lastCheckTime == 0)
  235. {
  236. lastCheckTime = nowMs;
  237. return;
  238. }
  239. long long deltaTime = nowMs - lastCheckTime;
  240. if(axisValueX == 0)
  241. planDisX = 0;
  242. else
  243. planDisX += ((float)deltaTime / 1000) * ((float)axisValueX / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
  244. if(axisValueY == 0)
  245. planDisY = 0;
  246. else
  247. planDisY += ((float)deltaTime / 1000) * ((float)axisValueY / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
  248. int moveDisX = getMoveDis(planDisX);
  249. int moveDisY = getMoveDis(planDisY);
  250. planDisX -= moveDisX;
  251. planDisY -= moveDisY;
  252. doCursorMove(moveDisX, moveDisY);
  253. lastCheckTime = nowMs;
  254. }