#include "input/Buttons.h" Buttons::Axis::Axis() : less(0.0f), greater(0.0f), lessIndex(-1), greaterIndex(-1) { } Buttons::Buttons(const Window& window) : window(window), dummy(0, "Dummy"), activeController(-1), gamepadToButton(-1) { } Button& Buttons::add(int key, const char* name) { if(buttons.add(key, name)) { return dummy; } return buttons[buttons.getLength() - 1]; } void Buttons::mapGamepadButton(const Button& button, int mapping) { gamepadToButton[mapping] = searchButton(button); } void Buttons::mapGamepadAxis(const Button& button, float value, int index) { if(value > 0.0f) { gamepadAxisToButton[index].greater = value; gamepadAxisToButton[index].greaterIndex = searchButton(button); } else { gamepadAxisToButton[index].less = value; gamepadAxisToButton[index].lessIndex = searchButton(button); } } void Buttons::tick() { DownArray down(false); if(searchForGamepad()) { checkGamepad(down); } for(int i = 0; i < buttons.getLength(); i++) { buttons[i].tick(window.isKeyDown(buttons[i].key) || down[i]); } } const ButtonList& Buttons::get() const { return buttons; } bool Buttons::searchForGamepad() { if(activeController != -1) { return true; } for(int i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) { if(glfwJoystickIsGamepad(i)) { activeController = i; return true; } } return false; } void Buttons::checkGamepad(DownArray& down) { GLFWgamepadstate state; if(!glfwGetGamepadState(activeController, &state)) { return; } for(int i = 0; i <= GLFW_GAMEPAD_BUTTON_LAST; i++) { if(gamepadToButton[i] != -1 && state.buttons[i]) { down[gamepadToButton[i]] = true; } } for(int i = 0; i <= GLFW_GAMEPAD_AXIS_LAST; i++) { if(gamepadAxisToButton[i].greaterIndex != -1 && state.axes[i] > gamepadAxisToButton[i].greater) { down[gamepadAxisToButton[i].greaterIndex] = true; } else if(gamepadAxisToButton[i].lessIndex != -1 && state.axes[i] < gamepadAxisToButton[i].less) { down[gamepadAxisToButton[i].lessIndex] = true; } } } int Buttons::searchButton(const Button& button) const { for(int i = 0; i < buttons.getLength(); i++) { if(&button == &(buttons[i])) { return i; } } return -1; }