InputSourceGameController.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * InputSourceGameController.h, 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. #pragma once
  11. #include <SDL_events.h>
  12. #include <SDL_gamecontroller.h>
  13. #include "../../lib/Point.h"
  14. #include "../gui/Shortcut.h"
  15. constexpr int AXIS_DEAD_ZOOM = 6000;
  16. constexpr int AXIS_MAX_ZOOM = 32000;
  17. constexpr int AXIS_MOVE_SPEED = 500;
  18. constexpr int AXIS_CURSOR_MOVE_INTERVAL = 1000;
  19. constexpr int TRIGGER_PRESS_THRESHOLD = 8000;
  20. enum class AxisType
  21. {
  22. CURSOR_MOTION,
  23. MAP_SCROLL,
  24. NONE
  25. };
  26. /// Class that handles game controller input from SDL events
  27. class InputSourceGameController
  28. {
  29. static void gameControllerDeleter(SDL_GameController * gameController);
  30. using GameControllerPtr = std::unique_ptr<SDL_GameController, decltype(&gameControllerDeleter)>;
  31. std::map<int, GameControllerPtr> gameControllerMap;
  32. std::set<SDL_GameControllerAxis> pressedAxes;
  33. long long lastCheckTime;
  34. int cursorAxisValueX;
  35. int cursorAxisValueY;
  36. float cursorPlanDisX;
  37. float cursorPlanDisY;
  38. bool scrollAxisMoved;
  39. Point scrollStart;
  40. Point scrollCurrent;
  41. int scrollAxisValueX;
  42. int scrollAxisValueY;
  43. float scrollPlanDisX;
  44. float scrollPlanDisY;
  45. void openGameController(int index);
  46. int getJoystickIndex(SDL_GameController * controller);
  47. int getRealAxisValue(int value);
  48. void dispatchAxisShortcuts(const std::vector<EShortcut> & shortcutsVector, SDL_GameControllerAxis axisID, int axisValue);
  49. void tryToConvertCursor();
  50. void doCursorMove(int deltaX, int deltaY);
  51. int getMoveDis(float planDis);
  52. void handleCursorUpdate(long long deltaTime);
  53. void handleScrollUpdate(long long deltaTime);
  54. bool isScrollAxisReleased();
  55. public:
  56. InputSourceGameController();
  57. void tryOpenAllGameControllers();
  58. void handleEventDeviceAdded(const SDL_ControllerDeviceEvent & device);
  59. void handleEventDeviceRemoved(const SDL_ControllerDeviceEvent & device);
  60. void handleEventDeviceRemapped(const SDL_ControllerDeviceEvent & device);
  61. void handleEventAxisMotion(const SDL_ControllerAxisEvent & axis);
  62. void handleEventButtonDown(const SDL_ControllerButtonEvent & button);
  63. void handleEventButtonUp(const SDL_ControllerButtonEvent & button);
  64. void handleUpdate();
  65. };