123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- /*
- #include <iostream>*/
- #include "input/Controller.h"
- /*Controller::ButtonState::ButtonState() : downTime(0), justReleased(false) {
- }*/
- Controller::Controller() /*activeController(GLFW_JOYSTICK_1)*/ {
- }
- void Controller::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;
- }
- uint Controller::getButtonAmount() const {
- return buttons.getLength();
- }
- const char* Controller::getName(uint button) const {
- static constexpr const char* buttonNames[] = {
- "A", "B", "X", "Y", "L", "R", "Select", "Start", "Left", "Right", "Up", "Down"
- };
- return buttonNames[button];
- }
- uint Controller::getDownTime(uint button) const {
- return buttons[button].downTime;
- }
- bool Controller::isDown(uint button) const {
- return buttons[button].downTime > 0;
- }
- bool Controller::wasReleased(uint button) const {
- return buttons[button].justReleased;
- }
- */
- bool Controller::isDown(int button) const {
- return buttons[(button + 1) & getRangeMask(button)].isDown();
- }
- int Controller::getDownTime(int button) const {
- return buttons[(button + 1) & getRangeMask(button)].getDownTime();
- }
- bool Controller::isUp(int button) const {
- return buttons[(button + 1) & getRangeMask(button)].isUp();
- }
- int Controller::getUpTime(int button) const {
- return buttons[(button + 1) & getRangeMask(button)].getUpTime();
- }
- int Controller::getRangeMask(int button) const {
- return -(button >= 0 && button < buttons.getLength());
- }
|