Buttons.cpp 3.0 KB

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