InputSourceGameController.h 2.3 KB

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