InputSourceGameController.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 TRIGGER_PRESS_THRESHOLD = 8000;
  19. /// Class that handles game controller input from SDL events
  20. class InputSourceGameController
  21. {
  22. static void gameControllerDeleter(SDL_GameController * gameController);
  23. using GameControllerPtr = std::unique_ptr<SDL_GameController, decltype(&gameControllerDeleter)>;
  24. std::map<int, GameControllerPtr> gameControllerMap;
  25. std::set<SDL_GameControllerAxis> pressedAxes;
  26. std::chrono::steady_clock::time_point lastCheckTime;
  27. int cursorAxisValueX;
  28. int cursorAxisValueY;
  29. float cursorPlanDisX;
  30. float cursorPlanDisY;
  31. bool scrollAxisMoved;
  32. Point scrollStart;
  33. Point scrollCurrent;
  34. int scrollAxisValueX;
  35. int scrollAxisValueY;
  36. float scrollPlanDisX;
  37. float scrollPlanDisY;
  38. void openGameController(int index);
  39. int getJoystickIndex(SDL_GameController * controller);
  40. int getRealAxisValue(int value);
  41. void dispatchAxisShortcuts(const std::vector<EShortcut> & shortcutsVector, SDL_GameControllerAxis axisID, int axisValue);
  42. void tryToConvertCursor();
  43. void doCursorMove(int deltaX, int deltaY);
  44. int getMoveDis(float planDis);
  45. void handleCursorUpdate(int32_t deltaTimeMs);
  46. void handleScrollUpdate(int32_t deltaTimeMs);
  47. bool isScrollAxisReleased();
  48. public:
  49. InputSourceGameController();
  50. void tryOpenAllGameControllers();
  51. void handleEventDeviceAdded(const SDL_ControllerDeviceEvent & device);
  52. void handleEventDeviceRemoved(const SDL_ControllerDeviceEvent & device);
  53. void handleEventDeviceRemapped(const SDL_ControllerDeviceEvent & device);
  54. void handleEventAxisMotion(const SDL_ControllerAxisEvent & axis);
  55. void handleEventButtonDown(const SDL_ControllerButtonEvent & button);
  56. void handleEventButtonUp(const SDL_ControllerButtonEvent & button);
  57. void handleUpdate();
  58. };