InputSourceGameController.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "GameControllerShortcuts.h"
  13. #include "InputHandler.h"
  14. #include "../CGameInfo.h"
  15. #include "../gui/CursorHandler.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../gui/EventDispatcher.h"
  18. #include "../gui/ShortcutHandler.h"
  19. void InputSourceGameController::gameControllerDeleter(SDL_GameController * gameController)
  20. {
  21. if(gameController)
  22. SDL_GameControllerClose(gameController);
  23. }
  24. InputSourceGameController::InputSourceGameController():
  25. lastCheckTime(0),
  26. axisValueX(0),
  27. axisValueY(0),
  28. planDisX(0.0),
  29. planDisY(0.0)
  30. {
  31. // SDL_init has not been called. so it is unnecessary to open joystick.
  32. }
  33. void InputSourceGameController::tryOpenAllGameControllers()
  34. {
  35. for(int i = 0; i < SDL_NumJoysticks(); ++i)
  36. if(SDL_IsGameController(i))
  37. openGameController(i);
  38. else
  39. logGlobal->warn("Joystick %d is an unsupported game controller!", i);
  40. }
  41. void InputSourceGameController::openGameController(int index)
  42. {
  43. SDL_GameController * controller = SDL_GameControllerOpen(index);
  44. if(!controller)
  45. {
  46. logGlobal->error("Fail to open game controller %d!", index);
  47. return;
  48. }
  49. GameControllerPtr controllerPtr(controller, gameControllerDeleter);
  50. // Need to save joystick index for event. Joystick index may not be equal to index sometimes.
  51. int joystickIndex = getJoystickIndex(controllerPtr.get());
  52. if(joystickIndex < 0)
  53. {
  54. logGlobal->error("Fail to get joystick index of game controller %d!", index);
  55. return;
  56. }
  57. if(gameControllerMap.find(joystickIndex) != gameControllerMap.end())
  58. {
  59. logGlobal->warn("Game controller with joystick index %d is already opened.", joystickIndex);
  60. return;
  61. }
  62. gameControllerMap.emplace(joystickIndex, std::move(controllerPtr));
  63. }
  64. int InputSourceGameController::getJoystickIndex(SDL_GameController * controller)
  65. {
  66. SDL_Joystick* joystick = SDL_GameControllerGetJoystick(controller);
  67. if(!joystick)
  68. return -1;
  69. SDL_JoystickID instanceID = SDL_JoystickInstanceID(joystick);
  70. if(instanceID < 0)
  71. return -1;
  72. return (int)instanceID;
  73. }
  74. void InputSourceGameController::handleEventDeviceAdded(const SDL_ControllerDeviceEvent & device)
  75. {
  76. if(gameControllerMap.find(device.which) != gameControllerMap.end())
  77. {
  78. logGlobal->warn("Game controller %d is already opened.", device.which);
  79. return;
  80. }
  81. openGameController(device.which);
  82. }
  83. void InputSourceGameController::handleEventDeviceRemoved(const SDL_ControllerDeviceEvent & device)
  84. {
  85. if(gameControllerMap.find(device.which) == gameControllerMap.end())
  86. {
  87. logGlobal->warn("Game controller %d is not opened before.", device.which);
  88. return;
  89. }
  90. gameControllerMap.erase(device.which);
  91. }
  92. void InputSourceGameController::handleEventDeviceRemapped(const SDL_ControllerDeviceEvent & device)
  93. {
  94. if(gameControllerMap.find(device.which) == gameControllerMap.end())
  95. {
  96. logGlobal->warn("Game controller %d is not opened.", device.which);
  97. return;
  98. }
  99. gameControllerMap.erase(device.which);
  100. openGameController(device.which);
  101. }
  102. int InputSourceGameController::getRealAxisValue(int value)
  103. {
  104. if(value < AXIS_DEAD_ZOOM && value > -AXIS_DEAD_ZOOM)
  105. return 0;
  106. if(value > AXIS_MAX_ZOOM)
  107. return AXIS_MAX_ZOOM;
  108. if(value < -AXIS_MAX_ZOOM)
  109. return -AXIS_MAX_ZOOM;
  110. int base = value > 0 ? AXIS_DEAD_ZOOM: -AXIS_DEAD_ZOOM;
  111. return (value - base) * AXIS_MAX_ZOOM / (AXIS_MAX_ZOOM - AXIS_DEAD_ZOOM);
  112. }
  113. void InputSourceGameController::dispatchTriggerShortcuts(const std::vector<EShortcut> & shortcutsVector, int axisValue)
  114. {
  115. if(axisValue >= TRIGGER_PRESS_THRESHOLD)
  116. GH.events().dispatchShortcutPressed(shortcutsVector);
  117. else
  118. GH.events().dispatchShortcutReleased(shortcutsVector);
  119. }
  120. void InputSourceGameController::handleEventAxisMotion(const SDL_ControllerAxisEvent & axis)
  121. {
  122. const auto & triggerShortcutsMap = getTriggerShortcutsMap();
  123. if(axis.axis == SDL_CONTROLLER_AXIS_LEFTX)
  124. {
  125. axisValueX = getRealAxisValue(axis.value);
  126. }
  127. else if(axis.axis == SDL_CONTROLLER_AXIS_LEFTY)
  128. {
  129. axisValueY = getRealAxisValue(axis.value);
  130. }
  131. else if(triggerShortcutsMap.find(axis.axis) != triggerShortcutsMap.end())
  132. {
  133. const auto & shortcutsVector = triggerShortcutsMap.find(axis.axis)->second;
  134. dispatchTriggerShortcuts(shortcutsVector, axis.value);
  135. }
  136. }
  137. void InputSourceGameController::handleEventButtonDown(const SDL_ControllerButtonEvent & button)
  138. {
  139. const Point & position = GH.input().getCursorPosition();
  140. const auto & buttonShortcutsMap = getButtonShortcutsMap();
  141. // TODO: define keys by user
  142. if(button.button == SDL_CONTROLLER_BUTTON_A)
  143. {
  144. GH.events().dispatchMouseLeftButtonPressed(position, 0);
  145. }
  146. else if(button.button == SDL_CONTROLLER_BUTTON_Y)
  147. {
  148. GH.events().dispatchShowPopup(position, 0);
  149. }
  150. else if(buttonShortcutsMap.find(button.button) != buttonShortcutsMap.end())
  151. {
  152. const auto & shortcutsVector = buttonShortcutsMap.find(button.button)->second;
  153. GH.events().dispatchShortcutPressed(shortcutsVector);
  154. }
  155. }
  156. void InputSourceGameController::handleEventButtonUp(const SDL_ControllerButtonEvent & button)
  157. {
  158. const Point & position = GH.input().getCursorPosition();
  159. const auto & buttonShortcutsMap = getButtonShortcutsMap();
  160. if(button.button == SDL_CONTROLLER_BUTTON_A)
  161. {
  162. GH.events().dispatchMouseLeftButtonReleased(position, 0);
  163. }
  164. else if(button.button == SDL_CONTROLLER_BUTTON_Y)
  165. {
  166. GH.events().dispatchClosePopup(position);
  167. }
  168. else if(buttonShortcutsMap.find(button.button) != buttonShortcutsMap.end())
  169. {
  170. const auto & shortcutsVector = buttonShortcutsMap.find(button.button)->second;
  171. GH.events().dispatchShortcutReleased(shortcutsVector);
  172. }
  173. }
  174. void InputSourceGameController::doCursorMove(int deltaX, int deltaY)
  175. {
  176. if(deltaX == 0 && deltaY == 0)
  177. return;
  178. const Point & screenSize = GH.screenDimensions();
  179. const Point &cursorPosition = GH.getCursorPosition();
  180. int newX = std::min(std::max(cursorPosition.x + deltaX, 0), screenSize.x);
  181. int newY = std::min(std::max(cursorPosition.y + deltaY, 0), screenSize.y);
  182. Point targetPosition{newX, newY};
  183. GH.input().setCursorPosition(targetPosition);
  184. if(CCS && CCS->curh)
  185. CCS->curh->cursorMove(GH.getCursorPosition().x, GH.getCursorPosition().y);
  186. }
  187. int InputSourceGameController::getMoveDis(float planDis)
  188. {
  189. if(planDis >= 0)
  190. return std::floor(planDis);
  191. else
  192. return std::ceil(planDis);
  193. }
  194. void InputSourceGameController::handleUpdate()
  195. {
  196. auto now = std::chrono::high_resolution_clock::now();
  197. auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
  198. if(lastCheckTime == 0)
  199. {
  200. lastCheckTime = nowMs;
  201. return;
  202. }
  203. long long deltaTime = nowMs - lastCheckTime;
  204. if(axisValueX == 0)
  205. planDisX = 0;
  206. else
  207. planDisX += ((float)deltaTime / 1000) * ((float)axisValueX / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
  208. if(axisValueY == 0)
  209. planDisY = 0;
  210. else
  211. planDisY += ((float)deltaTime / 1000) * ((float)axisValueY / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
  212. int moveDisX = getMoveDis(planDisX);
  213. int moveDisY = getMoveDis(planDisY);
  214. planDisX -= moveDisX;
  215. planDisY -= moveDisY;
  216. doCursorMove(moveDisX, moveDisY);
  217. lastCheckTime = nowMs;
  218. }