Controller.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include <iostream>
  2. #include "input/Controller.h"
  3. Controller::Axis::Axis() : positive(0), negative(0) {
  4. }
  5. Controller::Controller() : keyToButton(0), gamepadToButton(0)/*activeController(GLFW_JOYSTICK_1)*/ {
  6. }
  7. bool Controller::mapKey(int key, int button) {
  8. if(key < 0 || key >= keyToButton.getLength()) {
  9. return true;
  10. }
  11. button++;
  12. if(button < 0 || button >= buttons.getLength()) {
  13. return true;
  14. }
  15. keyToButton[key] = button;
  16. return false;
  17. }
  18. bool Controller::mapGamepad(int gamepad, int button) {
  19. if(gamepad < 0 || gamepad >= gamepadToButton.getLength()) {
  20. return true;
  21. }
  22. button++;
  23. if(button < 0 || button >= buttons.getLength()) {
  24. return true;
  25. }
  26. gamepadToButton[gamepad] = button;
  27. return false;
  28. }
  29. bool Controller::mapAxis(int axis, int positiveButton, int negativeButton) {
  30. if(axis < 0 || axis >= axisToButton.getLength()) {
  31. return true;
  32. }
  33. positiveButton++;
  34. if(positiveButton < 0 || positiveButton >= buttons.getLength()) {
  35. return true;
  36. }
  37. negativeButton++;
  38. if(negativeButton < 0 || negativeButton >= buttons.getLength()) {
  39. return true;
  40. }
  41. axisToButton[axis].positive = positiveButton;
  42. axisToButton[axis].negative = negativeButton;
  43. return false;
  44. }
  45. void Controller::press(int key) {
  46. if(key < 0 || key >= keyToButton.getLength()) {
  47. return;
  48. }
  49. buttons[keyToButton[key]].press();
  50. }
  51. void Controller::release(int key) {
  52. if(key < 0 || key >= keyToButton.getLength()) {
  53. return;
  54. }
  55. buttons[keyToButton[key]].release();
  56. }
  57. void Controller::tick() {
  58. for(Button& b : buttons) {
  59. b.tick();
  60. }
  61. /*if(!glfwJoystickPresent(activeController) && findController()) {
  62. std::cout << "cannot find any controller - resetting buttons\n";
  63. reset();
  64. return;
  65. }
  66. int buttonCount = 0;
  67. const u8* buttonMap = glfwGetJoystickButtons(activeController, &buttonCount);
  68. int axisCount = 0;
  69. const float* axisMap = glfwGetJoystickAxes(activeController, &axisCount);
  70. if(buttonCount < 10 || axisCount < 2) {
  71. std::cout << "cannot find any supported controller - resetting buttons\n";
  72. std::cout << "buttons found: " << buttonCount << "\n";
  73. std::cout << "axes found: " << axisCount << "\n";
  74. reset();
  75. return;
  76. }
  77. increment(Button::A, buttonMap, 1);
  78. increment(Button::B, buttonMap, 2);
  79. increment(Button::X, buttonMap, 0);
  80. increment(Button::Y, buttonMap, 3);
  81. increment(Button::L, buttonMap, 4);
  82. increment(Button::R, buttonMap, 5);
  83. increment(Button::SELECT, buttonMap, 8);
  84. increment(Button::START, buttonMap, 9);
  85. increment(Button::LEFT, axisMap[0] < -0.5f);
  86. increment(Button::RIGHT, axisMap[0] > 0.5f);
  87. increment(Button::UP, axisMap[1] < -0.5f);
  88. increment(Button::DOWN, axisMap[1] > 0.5f);*/
  89. }
  90. /*bool Controller::findController() {
  91. for(uint i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) {
  92. if(glfwJoystickPresent(i)) {
  93. activeController = i;
  94. return false;
  95. }
  96. }
  97. return true;
  98. }
  99. void Controller::reset() {
  100. for(ButtonState& b : buttons) {
  101. b.downTime = 0;
  102. }
  103. }
  104. void Controller::increment(uint button, const u8* mapping, uint index) {
  105. increment(button, mapping[index] != GLFW_RELEASE);
  106. }
  107. void Controller::increment(uint button, bool notReleased) {
  108. buttons[button].justReleased = (buttons[button].downTime > 0 && !notReleased && !buttons[button].justReleased);
  109. bool b = notReleased || buttons[button].justReleased;
  110. buttons[button].downTime = buttons[button].downTime * b + b;
  111. }*/
  112. int Controller::getButtonAmount() const {
  113. return buttons.getLength();
  114. }
  115. const char* Controller::getName(int button) const {
  116. static constexpr const char* buttonNames[BUTTON_AMOUNT] = {
  117. "Unknown", "A", "B", "X", "Y", "L", "R", "Start", "Select", "Left", "Right", "Up", "Down"
  118. };
  119. return buttonNames[(button + 1) & getRangeMask(button + 1)];
  120. }
  121. bool Controller::isDown(int button) const {
  122. return buttons[(button + 1) & getRangeMask(button + 1)].isDown();
  123. }
  124. int Controller::getDownTime(int button) const {
  125. return buttons[(button + 1) & getRangeMask(button + 1)].getDownTime();
  126. }
  127. bool Controller::isUp(int button) const {
  128. return buttons[(button + 1) & getRangeMask(button + 1)].isUp();
  129. }
  130. int Controller::getUpTime(int button) const {
  131. return buttons[(button + 1) & getRangeMask(button + 1)].getUpTime();
  132. }
  133. int Controller::getRangeMask(int button) const {
  134. return -(button >= 0 && button < buttons.getLength());
  135. }