Controller.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. #include <iostream>*/
  3. #include "input/Controller.h"
  4. /*Controller::ButtonState::ButtonState() : downTime(0), justReleased(false) {
  5. }*/
  6. Controller::Controller() /*activeController(GLFW_JOYSTICK_1)*/ {
  7. }
  8. void Controller::tick() {
  9. /*if(!glfwJoystickPresent(activeController) && findController()) {
  10. std::cout << "cannot find any controller - resetting buttons\n";
  11. reset();
  12. return;
  13. }
  14. int buttonCount = 0;
  15. const u8* buttonMap = glfwGetJoystickButtons(activeController, &buttonCount);
  16. int axisCount = 0;
  17. const float* axisMap = glfwGetJoystickAxes(activeController, &axisCount);
  18. if(buttonCount < 10 || axisCount < 2) {
  19. std::cout << "cannot find any supported controller - resetting buttons\n";
  20. std::cout << "buttons found: " << buttonCount << "\n";
  21. std::cout << "axes found: " << axisCount << "\n";
  22. reset();
  23. return;
  24. }
  25. increment(Button::A, buttonMap, 1);
  26. increment(Button::B, buttonMap, 2);
  27. increment(Button::X, buttonMap, 0);
  28. increment(Button::Y, buttonMap, 3);
  29. increment(Button::L, buttonMap, 4);
  30. increment(Button::R, buttonMap, 5);
  31. increment(Button::SELECT, buttonMap, 8);
  32. increment(Button::START, buttonMap, 9);
  33. increment(Button::LEFT, axisMap[0] < -0.5f);
  34. increment(Button::RIGHT, axisMap[0] > 0.5f);
  35. increment(Button::UP, axisMap[1] < -0.5f);
  36. increment(Button::DOWN, axisMap[1] > 0.5f);*/
  37. }
  38. /*bool Controller::findController() {
  39. for(uint i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) {
  40. if(glfwJoystickPresent(i)) {
  41. activeController = i;
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. void Controller::reset() {
  48. for(ButtonState& b : buttons) {
  49. b.downTime = 0;
  50. }
  51. }
  52. void Controller::increment(uint button, const u8* mapping, uint index) {
  53. increment(button, mapping[index] != GLFW_RELEASE);
  54. }
  55. void Controller::increment(uint button, bool notReleased) {
  56. buttons[button].justReleased = (buttons[button].downTime > 0 && !notReleased && !buttons[button].justReleased);
  57. bool b = notReleased || buttons[button].justReleased;
  58. buttons[button].downTime = buttons[button].downTime * b + b;
  59. }
  60. uint Controller::getButtonAmount() const {
  61. return buttons.getLength();
  62. }
  63. const char* Controller::getName(uint button) const {
  64. static constexpr const char* buttonNames[] = {
  65. "A", "B", "X", "Y", "L", "R", "Select", "Start", "Left", "Right", "Up", "Down"
  66. };
  67. return buttonNames[button];
  68. }
  69. uint Controller::getDownTime(uint button) const {
  70. return buttons[button].downTime;
  71. }
  72. bool Controller::isDown(uint button) const {
  73. return buttons[button].downTime > 0;
  74. }
  75. bool Controller::wasReleased(uint button) const {
  76. return buttons[button].justReleased;
  77. }
  78. */
  79. bool Controller::isDown(int button) const {
  80. return buttons[(button + 1) & getRangeMask(button)].isDown();
  81. }
  82. int Controller::getDownTime(int button) const {
  83. return buttons[(button + 1) & getRangeMask(button)].getDownTime();
  84. }
  85. bool Controller::isUp(int button) const {
  86. return buttons[(button + 1) & getRangeMask(button)].isUp();
  87. }
  88. int Controller::getUpTime(int button) const {
  89. return buttons[(button + 1) & getRangeMask(button)].getUpTime();
  90. }
  91. int Controller::getRangeMask(int button) const {
  92. return -(button >= 0 && button < buttons.getLength());
  93. }