Buttons.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "input/Buttons.h"
  2. Buttons::Axis::Axis()
  3. : less(0.0f), greater(0.0f), lessIndex(-1), greaterIndex(-1) {
  4. }
  5. Buttons::Buttons(const Window& window)
  6. : window(window), dummy(0, "Dummy"), activeController(-1),
  7. gamepadToButton(-1) {
  8. }
  9. Button& Buttons::add(int key, const char* name) {
  10. if(buttons.add(key, name)) {
  11. return dummy;
  12. }
  13. return buttons[buttons.getLength() - 1];
  14. }
  15. Button& Buttons::addMouse(int mouse, const char* name) {
  16. if(mouseButtons.add(mouse, name)) {
  17. return dummy;
  18. }
  19. return mouseButtons[mouseButtons.getLength() - 1];
  20. }
  21. void Buttons::mapGamepadButton(const Button& button, int mapping) {
  22. gamepadToButton[mapping] = searchButton(button);
  23. }
  24. void Buttons::mapGamepadAxis(const Button& button, float value, int index) {
  25. if(value > 0.0f) {
  26. gamepadAxisToButton[index].greater = value;
  27. gamepadAxisToButton[index].greaterIndex = searchButton(button);
  28. } else {
  29. gamepadAxisToButton[index].less = value;
  30. gamepadAxisToButton[index].lessIndex = searchButton(button);
  31. }
  32. }
  33. void Buttons::tick() {
  34. DownArray down(false);
  35. if(searchForGamepad()) {
  36. checkGamepad(down);
  37. }
  38. for(int i = 0; i < buttons.getLength(); i++) {
  39. buttons[i].tick(window.isKeyDown(buttons[i].key) || down[i]);
  40. }
  41. for(int i = 0; i < mouseButtons.getLength(); i++) {
  42. mouseButtons[i].tick(window.isMouseDown(mouseButtons[i].key));
  43. }
  44. lastMouseX = mouseX;
  45. lastMouseY = mouseY;
  46. window.getMousePosition(mouseX, mouseY);
  47. }
  48. const ButtonList& Buttons::get() const {
  49. return buttons;
  50. }
  51. const MouseList& Buttons::getMouse() const {
  52. return mouseButtons;
  53. }
  54. double Buttons::getLastMouseX() const {
  55. return lastMouseX;
  56. }
  57. double Buttons::getLastMouseY() const {
  58. return lastMouseY;
  59. }
  60. double Buttons::getMouseX() const {
  61. return mouseX;
  62. }
  63. double Buttons::getMouseY() const {
  64. return mouseY;
  65. }
  66. bool Buttons::searchForGamepad() {
  67. if(activeController != -1) {
  68. return true;
  69. }
  70. for(int i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) {
  71. if(glfwJoystickIsGamepad(i)) {
  72. activeController = i;
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. void Buttons::checkGamepad(DownArray& down) {
  79. GLFWgamepadstate state;
  80. if(!glfwGetGamepadState(activeController, &state)) {
  81. return;
  82. }
  83. for(int i = 0; i <= GLFW_GAMEPAD_BUTTON_LAST; i++) {
  84. if(gamepadToButton[i] != -1 && state.buttons[i]) {
  85. down[gamepadToButton[i]] = true;
  86. }
  87. }
  88. for(int i = 0; i <= GLFW_GAMEPAD_AXIS_LAST; i++) {
  89. if(gamepadAxisToButton[i].greaterIndex != -1 &&
  90. state.axes[i] > gamepadAxisToButton[i].greater) {
  91. down[gamepadAxisToButton[i].greaterIndex] = true;
  92. } else if(gamepadAxisToButton[i].lessIndex != -1 &&
  93. state.axes[i] < gamepadAxisToButton[i].less) {
  94. down[gamepadAxisToButton[i].lessIndex] = true;
  95. }
  96. }
  97. }
  98. int Buttons::searchButton(const Button& button) const {
  99. for(int i = 0; i < buttons.getLength(); i++) {
  100. if(&button == &(buttons[i])) {
  101. return i;
  102. }
  103. }
  104. return -1;
  105. }