|
@@ -1,75 +1,91 @@
|
|
|
+#include <GL/glew.h>
|
|
|
+#include <GLFW/glfw3.h>
|
|
|
+#include <iostream>
|
|
|
+
|
|
|
#include "input/Controller.h"
|
|
|
|
|
|
-void Controller::tick() {
|
|
|
+Controller::ButtonState::ButtonState() : downTime(0), justReleased(false) {
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+Controller::Controller() : activeController(GLFW_JOYSTICK_1) {
|
|
|
+}
|
|
|
|
|
|
-#include "client/input/Keys.h"
|
|
|
+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);
|
|
|
+}
|
|
|
|
|
|
-Keys::Key::Key() : down(false), shouldRelease(false), downTime(0) {
|
|
|
+bool Controller::findController() {
|
|
|
+ for(uint i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) {
|
|
|
+ if(glfwJoystickPresent(i)) {
|
|
|
+ activeController = i;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
-bool Keys::Key::isDown() const {
|
|
|
- return down;
|
|
|
+void Controller::reset() {
|
|
|
+ for(ButtonState& b : buttons) {
|
|
|
+ b.downTime = 0;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-bool Keys::Key::isReleased() const {
|
|
|
- return shouldRelease;
|
|
|
+void Controller::increment(uint button, const u8* mapping, uint index) {
|
|
|
+ increment(button, mapping[index] != GLFW_RELEASE);
|
|
|
}
|
|
|
|
|
|
-u32 Keys::Key::getDownTime() const {
|
|
|
- return downTime;
|
|
|
+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;
|
|
|
}
|
|
|
|
|
|
-std::ostream& operator<<(std::ostream& os, const Keys::Key& k) {
|
|
|
- os << "Key(down: " << k.isDown() << ", release: " << k.isReleased() <<
|
|
|
- ", time: " << k.getDownTime() << ")";
|
|
|
- return os;
|
|
|
+uint Controller::getButtonAmount() const {
|
|
|
+ return buttons.getLength();
|
|
|
}
|
|
|
|
|
|
-Keys::Keys() : left(keys[0]), right(keys[1]), up(keys[2]), down(keys[3]), jump(keys[4]), sneak(keys[5]),
|
|
|
-camLeft(keys[6]), camRight(keys[7]), camUp(keys[8]), camDown(keys[9]), test(keys[10]), test2(keys[11]), test3(keys[12]),
|
|
|
-test4(keys[13]), test5(keys[14]) {
|
|
|
- keys[0].glfwKey = GLFW_KEY_A;
|
|
|
- keys[1].glfwKey = GLFW_KEY_D;
|
|
|
- keys[2].glfwKey = GLFW_KEY_W;
|
|
|
- keys[3].glfwKey = GLFW_KEY_S;
|
|
|
- keys[4].glfwKey = GLFW_KEY_SPACE;
|
|
|
- keys[5].glfwKey = GLFW_KEY_LEFT_SHIFT;
|
|
|
- keys[6].glfwKey = GLFW_KEY_LEFT;
|
|
|
- keys[7].glfwKey = GLFW_KEY_RIGHT;
|
|
|
- keys[8].glfwKey = GLFW_KEY_UP;
|
|
|
- keys[9].glfwKey = GLFW_KEY_DOWN;
|
|
|
- keys[10].glfwKey = GLFW_KEY_T;
|
|
|
- keys[11].glfwKey = GLFW_KEY_Y;
|
|
|
- keys[12].glfwKey = GLFW_KEY_U;
|
|
|
- keys[13].glfwKey = GLFW_KEY_I;
|
|
|
- keys[14].glfwKey = GLFW_KEY_R;
|
|
|
+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];
|
|
|
}
|
|
|
|
|
|
-void Keys::release(int key) {
|
|
|
- for(Key& k : keys) {
|
|
|
- if(k.glfwKey == key) {
|
|
|
- k.shouldRelease = true;
|
|
|
- }
|
|
|
- }
|
|
|
+uint Controller::getDownTime(uint button) const {
|
|
|
+ return buttons[button].downTime;
|
|
|
}
|
|
|
|
|
|
-void Keys::press(int key) {
|
|
|
- for(Key& k : keys) {
|
|
|
- if(k.glfwKey == key) {
|
|
|
- k.down = true;
|
|
|
- k.shouldRelease = false;
|
|
|
- }
|
|
|
- }
|
|
|
+bool Controller::isDown(uint button) const {
|
|
|
+ return buttons[button].downTime > 0;
|
|
|
}
|
|
|
|
|
|
-void Keys::tick() {
|
|
|
- for(Key& k : keys) {
|
|
|
- k.downTime += k.down;
|
|
|
- k.down = k.down && !k.shouldRelease;
|
|
|
- k.downTime *= !k.shouldRelease;
|
|
|
- k.shouldRelease = false;
|
|
|
- }
|
|
|
-}*/
|
|
|
+bool Controller::wasReleased(uint button) const {
|
|
|
+ return buttons[button].justReleased;
|
|
|
+}
|