Controller.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <iostream>
  2. #include "input/Controller.h"
  3. Controller::Axis::Axis() : positive(0), negative(0), split(0.5f) {
  4. }
  5. Controller::Controller() : keyToButton(0), gamepadToButton(0), activeController(-1) {
  6. mapGamepad(GLFW_GAMEPAD_BUTTON_A, Controller::A);
  7. mapGamepad(GLFW_GAMEPAD_BUTTON_B, Controller::B);
  8. mapGamepad(GLFW_GAMEPAD_BUTTON_X, Controller::X);
  9. mapGamepad(GLFW_GAMEPAD_BUTTON_Y, Controller::Y);
  10. mapGamepad(GLFW_GAMEPAD_BUTTON_LEFT_BUMPER, Controller::L);
  11. mapGamepad(GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER, Controller::R);
  12. mapGamepad(GLFW_GAMEPAD_BUTTON_BACK, Controller::SELECT);
  13. mapGamepad(GLFW_GAMEPAD_BUTTON_START, Controller::START);
  14. mapGamepad(GLFW_GAMEPAD_BUTTON_GUIDE, Controller::UNKNOWN);
  15. mapGamepad(GLFW_GAMEPAD_BUTTON_LEFT_THUMB, Controller::SELECT);
  16. mapGamepad(GLFW_GAMEPAD_BUTTON_RIGHT_THUMB, Controller::START);
  17. mapGamepad(GLFW_GAMEPAD_BUTTON_DPAD_UP, Controller::UP);
  18. mapGamepad(GLFW_GAMEPAD_BUTTON_DPAD_RIGHT, Controller::RIGHT);
  19. mapGamepad(GLFW_GAMEPAD_BUTTON_DPAD_DOWN, Controller::DOWN);
  20. mapGamepad(GLFW_GAMEPAD_BUTTON_DPAD_LEFT, Controller::LEFT);
  21. mapAxis(GLFW_GAMEPAD_AXIS_LEFT_X, Controller::RIGHT, Controller::LEFT);
  22. mapAxis(GLFW_GAMEPAD_AXIS_LEFT_Y, Controller::DOWN, Controller::UP);
  23. mapAxis(GLFW_GAMEPAD_AXIS_RIGHT_X, Controller::RIGHT, Controller::LEFT);
  24. mapAxis(GLFW_GAMEPAD_AXIS_RIGHT_Y, Controller::DOWN, Controller::UP);
  25. mapAxis(GLFW_GAMEPAD_AXIS_LEFT_TRIGGER, Controller::L, Controller::UNKNOWN, -0.9f);
  26. mapAxis(GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER, Controller::R, Controller::UNKNOWN, -0.9f);
  27. }
  28. bool Controller::mapKey(int key, int button) {
  29. if(key < 0 || key >= keyToButton.getLength()) {
  30. return true;
  31. }
  32. button++;
  33. if(button < 0 || button >= buttons.getLength()) {
  34. return true;
  35. }
  36. keyToButton[key] = button;
  37. return false;
  38. }
  39. bool Controller::mapGamepad(int gamepad, int button) {
  40. if(gamepad < 0 || gamepad >= gamepadToButton.getLength()) {
  41. return true;
  42. }
  43. button++;
  44. if(button < 0 || button >= buttons.getLength()) {
  45. return true;
  46. }
  47. gamepadToButton[gamepad] = button;
  48. return false;
  49. }
  50. bool Controller::mapAxis(int axis, int positiveButton, int negativeButton, float split) {
  51. if(axis < 0 || axis >= axisToButton.getLength()) {
  52. return true;
  53. }
  54. positiveButton++;
  55. if(positiveButton < 0 || positiveButton >= buttons.getLength()) {
  56. return true;
  57. }
  58. negativeButton++;
  59. if(negativeButton < 0 || negativeButton >= buttons.getLength()) {
  60. return true;
  61. }
  62. axisToButton[axis].positive = positiveButton;
  63. axisToButton[axis].negative = negativeButton;
  64. axisToButton[axis].split = split;
  65. return false;
  66. }
  67. void Controller::press(int key) {
  68. if(key < 0 || key >= keyToButton.getLength()) {
  69. return;
  70. }
  71. buttons[keyToButton[key]].pressKey();
  72. }
  73. void Controller::release(int key) {
  74. if(key < 0 || key >= keyToButton.getLength()) {
  75. return;
  76. }
  77. buttons[keyToButton[key]].releaseKey();
  78. }
  79. void Controller::tick() {
  80. for(Button& b : buttons) {
  81. b.tick();
  82. }
  83. if(searchForGamepad()) {
  84. checkGamepad();
  85. }
  86. }
  87. bool Controller::searchForGamepad() {
  88. if(activeController != -1) {
  89. return true;
  90. }
  91. for(int i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) {
  92. if(glfwJoystickIsGamepad(i)) {
  93. activeController = i;
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. void Controller::checkGamepad() {
  100. GLFWgamepadstate state;
  101. if(!glfwGetGamepadState(activeController, &state)) {
  102. return;
  103. }
  104. for(int i = 0; i <= GLFW_GAMEPAD_BUTTON_LAST; i++) {
  105. if(state.buttons[i]) {
  106. buttons[gamepadToButton[i]].pressButton();
  107. }
  108. }
  109. for(int i = 0; i <= GLFW_GAMEPAD_AXIS_LAST; i++) {
  110. if(state.axes[i] > axisToButton[i].split) {
  111. buttons[axisToButton[i].positive].pressButton();
  112. } else if(state.axes[i] < -axisToButton[i].split) {
  113. buttons[axisToButton[i].negative].pressButton();
  114. }
  115. }
  116. }
  117. int Controller::getButtonAmount() const {
  118. return buttons.getLength() - 1;
  119. }
  120. const char* Controller::getName(int button) const {
  121. static constexpr const char* buttonNames[BUTTON_AMOUNT] = {
  122. "Unknown", "A", "B", "X", "Y", "L", "R", "Start", "Select", "Left", "Right", "Up", "Down"
  123. };
  124. return buttonNames[(button + 1) & getRangeMask(button + 1)];
  125. }
  126. bool Controller::isDown(int button) const {
  127. return buttons[(button + 1) & getRangeMask(button + 1)].isDown();
  128. }
  129. int Controller::getDownTime(int button) const {
  130. return buttons[(button + 1) & getRangeMask(button + 1)].getDownTime();
  131. }
  132. bool Controller::wasReleased(int button) const {
  133. return buttons[(button + 1) & getRangeMask(button + 1)].wasReleased();
  134. }
  135. int Controller::getRangeMask(int button) const {
  136. return -(button >= 0 && button < buttons.getLength());
  137. }