Controller.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #ifndef CONTROLLER_H
  2. #define CONTROLLER_H
  3. #include <GL/glew.h>
  4. #include <GLFW/glfw3.h>
  5. #include "utils/Array.h"
  6. #include "input/Button.h"
  7. class Controller final {
  8. static constexpr int BUTTON_AMOUNT = 13;
  9. Array<Button, BUTTON_AMOUNT> buttons;
  10. Array<int, 1 + GLFW_KEY_LAST> keyToButton;
  11. Array<int, 1 + GLFW_GAMEPAD_BUTTON_LAST> gamepadToButton;
  12. struct Axis {
  13. int positive;
  14. int negative;
  15. float split;
  16. Axis();
  17. };
  18. Array<Axis, 1 + GLFW_GAMEPAD_AXIS_LAST> axisToButton;
  19. int activeController;
  20. public:
  21. static constexpr int UNKNOWN = -1;
  22. static constexpr int A = 0;
  23. static constexpr int B = 1;
  24. static constexpr int X = 2;
  25. static constexpr int Y = 3;
  26. static constexpr int L = 4;
  27. static constexpr int R = 5;
  28. static constexpr int START = 6;
  29. static constexpr int SELECT = 7;
  30. static constexpr int LEFT = 8;
  31. static constexpr int RIGHT = 9;
  32. static constexpr int UP = 10;
  33. static constexpr int DOWN = 11;
  34. Controller();
  35. bool mapKey(int key, int button);
  36. void press(int key);
  37. void release(int key);
  38. void tick();
  39. int getButtonAmount() const;
  40. const char* getName(int button) const;
  41. bool isDown(int button) const;
  42. int getDownTime(int button) const;
  43. bool wasReleased(int button) const;
  44. private:
  45. int getRangeMask(int button) const;
  46. bool searchForGamepad();
  47. void checkGamepad();
  48. bool mapGamepad(int gamepad, int button);
  49. bool mapAxis(int axis, int positiveButton, int negativeButton, float split = 0.5f);
  50. };
  51. #endif