InputSourceGameController.h 2.2 KB

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