123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #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];
- }
- Button& Buttons::addMouse(int mouse, const char* name) {
- if(mouseButtons.add(mouse, name)) {
- return dummy;
- }
- return mouseButtons[mouseButtons.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]);
- }
- for(int i = 0; i < mouseButtons.getLength(); i++) {
- mouseButtons[i].tick(window.isMouseDown(mouseButtons[i].key));
- }
- lastMouseX = mouseX;
- lastMouseY = mouseY;
- window.getMousePosition(mouseX, mouseY);
- }
- const ButtonList& Buttons::get() const {
- return buttons;
- }
- const MouseList& Buttons::getMouse() const {
- return mouseButtons;
- }
- double Buttons::getLastMouseX() const {
- return lastMouseX;
- }
- double Buttons::getLastMouseY() const {
- return lastMouseY;
- }
- double Buttons::getMouseX() const {
- return mouseX;
- }
- double Buttons::getMouseY() const {
- return mouseY;
- }
- 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;
- }
|