|
@@ -2,10 +2,31 @@
|
|
|
|
|
|
#include "input/Controller.h"
|
|
|
|
|
|
-Controller::Axis::Axis() : positive(0), negative(0) {
|
|
|
-}
|
|
|
-
|
|
|
-Controller::Controller() : keyToButton(0), gamepadToButton(0) {
|
|
|
+Controller::Axis::Axis() : positive(0), negative(0), split(0.5f) {
|
|
|
+}
|
|
|
+
|
|
|
+Controller::Controller() : keyToButton(0), gamepadToButton(0), activeController(-1) {
|
|
|
+ mapGamepad(GLFW_GAMEPAD_BUTTON_A, Controller::A);
|
|
|
+ mapGamepad(GLFW_GAMEPAD_BUTTON_B, Controller::B);
|
|
|
+ mapGamepad(GLFW_GAMEPAD_BUTTON_X, Controller::X);
|
|
|
+ mapGamepad(GLFW_GAMEPAD_BUTTON_Y, Controller::Y);
|
|
|
+ mapGamepad(GLFW_GAMEPAD_BUTTON_LEFT_BUMPER, Controller::L);
|
|
|
+ mapGamepad(GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER, Controller::R);
|
|
|
+ mapGamepad(GLFW_GAMEPAD_BUTTON_BACK, Controller::SELECT);
|
|
|
+ mapGamepad(GLFW_GAMEPAD_BUTTON_START, Controller::START);
|
|
|
+ mapGamepad(GLFW_GAMEPAD_BUTTON_GUIDE, Controller::UNKNOWN);
|
|
|
+ mapGamepad(GLFW_GAMEPAD_BUTTON_LEFT_THUMB, Controller::SELECT);
|
|
|
+ mapGamepad(GLFW_GAMEPAD_BUTTON_RIGHT_THUMB, Controller::START);
|
|
|
+ mapGamepad(GLFW_GAMEPAD_BUTTON_DPAD_UP, Controller::UP);
|
|
|
+ mapGamepad(GLFW_GAMEPAD_BUTTON_DPAD_RIGHT, Controller::RIGHT);
|
|
|
+ mapGamepad(GLFW_GAMEPAD_BUTTON_DPAD_DOWN, Controller::DOWN);
|
|
|
+ mapGamepad(GLFW_GAMEPAD_BUTTON_DPAD_LEFT, Controller::LEFT);
|
|
|
+ mapAxis(GLFW_GAMEPAD_AXIS_LEFT_X, Controller::RIGHT, Controller::LEFT);
|
|
|
+ mapAxis(GLFW_GAMEPAD_AXIS_LEFT_Y, Controller::DOWN, Controller::UP);
|
|
|
+ mapAxis(GLFW_GAMEPAD_AXIS_RIGHT_X, Controller::RIGHT, Controller::LEFT);
|
|
|
+ mapAxis(GLFW_GAMEPAD_AXIS_RIGHT_Y, Controller::DOWN, Controller::UP);
|
|
|
+ mapAxis(GLFW_GAMEPAD_AXIS_LEFT_TRIGGER, Controller::L, Controller::UNKNOWN, -0.9f);
|
|
|
+ mapAxis(GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER, Controller::R, Controller::UNKNOWN, -0.9f);
|
|
|
}
|
|
|
|
|
|
bool Controller::mapKey(int key, int button) {
|
|
@@ -32,7 +53,7 @@ bool Controller::mapGamepad(int gamepad, int button) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-bool Controller::mapAxis(int axis, int positiveButton, int negativeButton) {
|
|
|
+bool Controller::mapAxis(int axis, int positiveButton, int negativeButton, float split) {
|
|
|
if(axis < 0 || axis >= axisToButton.getLength()) {
|
|
|
return true;
|
|
|
}
|
|
@@ -46,6 +67,7 @@ bool Controller::mapAxis(int axis, int positiveButton, int negativeButton) {
|
|
|
}
|
|
|
axisToButton[axis].positive = positiveButton;
|
|
|
axisToButton[axis].negative = negativeButton;
|
|
|
+ axisToButton[axis].split = split;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
@@ -53,78 +75,59 @@ void Controller::press(int key) {
|
|
|
if(key < 0 || key >= keyToButton.getLength()) {
|
|
|
return;
|
|
|
}
|
|
|
- buttons[keyToButton[key]].press();
|
|
|
+ buttons[keyToButton[key]].pressKey();
|
|
|
}
|
|
|
|
|
|
void Controller::release(int key) {
|
|
|
if(key < 0 || key >= keyToButton.getLength()) {
|
|
|
return;
|
|
|
}
|
|
|
- buttons[keyToButton[key]].release();
|
|
|
+ buttons[keyToButton[key]].releaseKey();
|
|
|
}
|
|
|
|
|
|
void Controller::tick() {
|
|
|
for(Button& b : buttons) {
|
|
|
b.tick();
|
|
|
}
|
|
|
-
|
|
|
- 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;
|
|
|
+ if(searchForGamepad()) {
|
|
|
+ checkGamepad();
|
|
|
}
|
|
|
- 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);*/
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- for(uint i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) {
|
|
|
- if(glfwJoystickPresent(i)) {
|
|
|
+bool Controller::searchForGamepad() {
|
|
|
+ if(activeController != -1) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ for(int i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) {
|
|
|
+ if(glfwJoystickIsGamepad(i)) {
|
|
|
activeController = i;
|
|
|
- return false;
|
|
|
+ return true;
|
|
|
}
|
|
|
}
|
|
|
- return true;
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
-void Controller::reset() {
|
|
|
- for(ButtonState& b : buttons) {
|
|
|
- b.downTime = 0;
|
|
|
+void Controller::checkGamepad() {
|
|
|
+ GLFWgamepadstate state;
|
|
|
+ if(!glfwGetGamepadState(activeController, &state)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for(int i = 0; i <= GLFW_GAMEPAD_BUTTON_LAST; i++) {
|
|
|
+ if(state.buttons[i]) {
|
|
|
+ buttons[gamepadToButton[i]].pressButton();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for(int i = 0; i <= GLFW_GAMEPAD_AXIS_LAST; i++) {
|
|
|
+ if(state.axes[i] > axisToButton[i].split) {
|
|
|
+ buttons[axisToButton[i].positive].pressButton();
|
|
|
+ } else if(state.axes[i] < -axisToButton[i].split) {
|
|
|
+ buttons[axisToButton[i].negative].pressButton();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-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();
|
|
|
+ return buttons.getLength() - 1;
|
|
|
}
|
|
|
|
|
|
const char* Controller::getName(int button) const {
|
|
@@ -142,12 +145,8 @@ 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();
|
|
|
+bool Controller::wasReleased(int button) const {
|
|
|
+ return buttons[(button + 1) & getRangeMask(button + 1)].wasReleased();
|
|
|
}
|
|
|
|
|
|
int Controller::getRangeMask(int button) const {
|