#include #include "input/Controller.h" Controller::Axis::Axis() : positive(0), negative(0) { } Controller::Controller() : keyToButton(0), gamepadToButton(0)/*activeController(GLFW_JOYSTICK_1)*/ { } bool Controller::mapKey(int key, int button) { if(key < 0 || key >= keyToButton.getLength()) { return true; } button++; if(button < 0 || button >= buttons.getLength()) { return true; } keyToButton[key] = button; return false; } bool Controller::mapGamepad(int gamepad, int button) { if(gamepad < 0 || gamepad >= gamepadToButton.getLength()) { return true; } button++; if(button < 0 || button >= buttons.getLength()) { return true; } gamepadToButton[gamepad] = button; return false; } bool Controller::mapAxis(int axis, int positiveButton, int negativeButton) { if(axis < 0 || axis >= axisToButton.getLength()) { return true; } positiveButton++; if(positiveButton < 0 || positiveButton >= buttons.getLength()) { return true; } negativeButton++; if(negativeButton < 0 || negativeButton >= buttons.getLength()) { return true; } axisToButton[axis].positive = positiveButton; axisToButton[axis].negative = negativeButton; return false; } void Controller::press(int key) { if(key < 0 || key >= keyToButton.getLength()) { return; } buttons[keyToButton[key]].press(); } void Controller::release(int key) { if(key < 0 || key >= keyToButton.getLength()) { return; } buttons[keyToButton[key]].release(); } void Controller::tick() { for(Button& b : buttons) { b.tick(); } /*if(!glfwJoystickPresent(activeController) && findController()) { std::cout << "cannot find any controller - resetting buttons\n"; reset(); return; } int buttonCount = 0; const u8* buttonMap = glfwGetJoystickButtons(activeController, &buttonCount); int axisCount = 0; const float* axisMap = glfwGetJoystickAxes(activeController, &axisCount); if(buttonCount < 10 || axisCount < 2) { std::cout << "cannot find any supported controller - resetting buttons\n"; std::cout << "buttons found: " << buttonCount << "\n"; std::cout << "axes found: " << axisCount << "\n"; reset(); return; } increment(Button::A, buttonMap, 1); increment(Button::B, buttonMap, 2); increment(Button::X, buttonMap, 0); increment(Button::Y, buttonMap, 3); increment(Button::L, buttonMap, 4); increment(Button::R, buttonMap, 5); increment(Button::SELECT, buttonMap, 8); increment(Button::START, buttonMap, 9); increment(Button::LEFT, axisMap[0] < -0.5f); increment(Button::RIGHT, axisMap[0] > 0.5f); increment(Button::UP, axisMap[1] < -0.5f); increment(Button::DOWN, axisMap[1] > 0.5f);*/ } /*bool Controller::findController() { for(uint i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) { if(glfwJoystickPresent(i)) { activeController = i; return false; } } return true; } void Controller::reset() { for(ButtonState& b : buttons) { b.downTime = 0; } } void Controller::increment(uint button, const u8* mapping, uint index) { increment(button, mapping[index] != GLFW_RELEASE); } void Controller::increment(uint button, bool notReleased) { buttons[button].justReleased = (buttons[button].downTime > 0 && !notReleased && !buttons[button].justReleased); bool b = notReleased || buttons[button].justReleased; buttons[button].downTime = buttons[button].downTime * b + b; }*/ int Controller::getButtonAmount() const { return buttons.getLength(); } const char* Controller::getName(int button) const { static constexpr const char* buttonNames[BUTTON_AMOUNT] = { "Unknown", "A", "B", "X", "Y", "L", "R", "Start", "Select", "Left", "Right", "Up", "Down" }; return buttonNames[(button + 1) & getRangeMask(button + 1)]; } bool Controller::isDown(int button) const { return buttons[(button + 1) & getRangeMask(button + 1)].isDown(); } int Controller::getDownTime(int button) const { return buttons[(button + 1) & getRangeMask(button + 1)].getDownTime(); } bool Controller::isUp(int button) const { return buttons[(button + 1) & getRangeMask(button + 1)].isUp(); } int Controller::getUpTime(int button) const { return buttons[(button + 1) & getRangeMask(button + 1)].getUpTime(); } int Controller::getRangeMask(int button) const { return -(button >= 0 && button < buttons.getLength()); }