diff --git a/src/input.cpp b/src/input.cpp index 5b3b010..1ae57ba 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -14,7 +14,8 @@ Input::Input(sf::Window *w) : m_closeRequested(false), m_mouseGrabbed(false) { - m_heldkeys = std::vector(); + m_heldKeys = std::vector(); + m_heldMouseButtons = std::vector(); m_actions = std::vector(); m_window->setKeyRepeatEnabled(false); } @@ -44,16 +45,6 @@ void Input::updateEvents(){ m_actions.clear(); Action action; - /* - sf::Joystick::X; - sf::Joystick::Y; - sf::Joystick::Z; - sf::Joystick::R; - sf::Joystick::U; - sf::Joystick::V; - sf::Joystick::PovX; - sf::Joystick::PovY; - */ /* event-parsing loop */ while(m_window->pollEvent(event)) @@ -68,7 +59,16 @@ void Input::updateEvents(){ m_last_mouse_position = sf::Vector2i(m_window->getSize())/2; break; case sf::Event::LostFocus: + if(m_mouseGrabbed){ + m_mouseWasGrabbed = true; + m_mouseGrabbed = false; + } + break; case sf::Event::GainedFocus: + if(m_mouseWasGrabbed){ + m_mouseGrabbed = true; + m_mouseWasGrabbed = false; + } //Nothing to add here because the window already keep the state of the focus break; case sf::Event::TextEntered: @@ -79,13 +79,13 @@ void Input::updateEvents(){ action = kb.getPressedAction(input::KEYBOARD,event.key.code); if(!action.isNull()) m_actions.push_back(action); -// m_heldkeys.push_back(event.key.code); + m_heldKeys.push_back(event.key.code); break; case sf::Event::KeyReleased: action = kb.getReleasedAction(input::KEYBOARD,event.key.code); if(!action.isNull()) m_actions.push_back(action); -// releaseHeldKeys(event.key.code); + releaseHeldKeys(event.key.code); break; case sf::Event::MouseWheelScrolled: if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel) @@ -95,13 +95,13 @@ void Input::updateEvents(){ action = kb.getPressedAction(input::MOUSE,event.mouseButton.button); if(!action.isNull()) m_actions.push_back(action); -// m_heldkeys.push_back((sf::Keyboard::Key) (event.mouseButton.button)); + m_heldMouseButtons.push_back(event.mouseButton.button); break; case sf::Event::MouseButtonReleased: action = kb.getReleasedAction(input::MOUSE,event.mouseButton.button); if(!action.isNull()) m_actions.push_back(action); -// releaseHeldKeys((sf::Keyboard::Key) (event.mouseButton.button)); + releaseHeldMouseButton(event.mouseButton.button); break; case sf::Event::MouseMoved: m_mouse_position = sf::Mouse::getPosition(*m_window); @@ -110,7 +110,8 @@ void Input::updateEvents(){ // action MouseEntered break; case sf::Event::MouseLeft: - //action MouseLeft + if(m_mouseGrabbed) + sf::Mouse::setPosition(m_last_mouse_position,*m_window); break; case sf::Event::JoystickButtonPressed: action = kb.getPressedAction(input::CONTROLLER,event.joystickButton.button); // + idjoy * joystick::buttoncount @@ -118,7 +119,7 @@ void Input::updateEvents(){ action.controller_id = event.joystickButton.joystickId; m_actions.push_back(action); } - m_heldkeys.push_back((sf::Keyboard::Key) (event.joystickButton.button)); + m_heldJoystickButtons[action.controller_id].push_back(event.joystickButton.button); break; case sf::Event::JoystickButtonReleased: action = kb.getReleasedAction(input::CONTROLLER, event.joystickButton.button); // + idjoy * joystick::buttoncount @@ -126,23 +127,34 @@ void Input::updateEvents(){ action.controller_id = event.joystickButton.joystickId; m_actions.push_back(action); } - releaseHeldKeys((sf::Keyboard::Key) (event.joystickButton.button)); + releaseHeldJoystickButton(event.joystickButton.button,action.controller_id); break; case sf::Event::JoystickMoved: + switch(event.joystickMove.axis){ + case 6: + case 7: + event.joystickMove.position; + break; + default: + + break; + } // i'll handle that later std::cout << "Joystick axis " << event.joystickMove.axis << " of controller " << event.joystickMove.joystickId << " moved at" << event.joystickMove.position << std::endl; break; case sf::Event::JoystickConnected: { - sf::Joystick::Identification id = sf::Joystick::getIdentification(event.joystickConnect.joystickId); - std::cout << "Joystick "<< event.joystickConnect.joystickId << " connected :" << id.name.toAnsiString() << std::endl; + m_heldJoystickButtons[event.joystickConnect.joystickId] = std::vector(); +// sf::Joystick::Identification id = sf::Joystick::getIdentification(event.joystickConnect.joystickId); +// std::cout << "Joystick "<< event.joystickConnect.joystickId << " connected :" << id.name.toAnsiString() << std::endl; break; } case sf::Event::JoystickDisconnected: { - sf::Joystick::Identification id = sf::Joystick::getIdentification(event.joystickConnect.joystickId); - std::cout << "Joystick "<< event.joystickConnect.joystickId << " disconnected :" << id.name.toAnsiString() << std::endl; +// sf::Joystick::Identification id = sf::Joystick::getIdentification(event.joystickConnect.joystickId); + m_heldJoystickButtons.erase(event.joystickConnect.joystickId); +// std::cout << "Joystick "<< event.joystickConnect.joystickId << " disconnected :" << id.name.toAnsiString() << std::endl; break; } } @@ -150,10 +162,21 @@ void Input::updateEvents(){ if(m_mouseGrabbed) sf::Mouse::setPosition(m_last_mouse_position, *m_window); - for (auto key: m_heldkeys) + for (auto key: m_heldKeys) + m_actions.push_back(kb.getHoldAction(input::KEYBOARD,key)); + + for (auto button: m_heldMouseButtons) + m_actions.push_back(kb.getHoldAction(input::MOUSE,button)); + + for (auto heldJoystickButton : m_heldJoystickButtons) { -// m_actions.push_back(kb.getHoldAction(key)); - } + for (auto button: heldJoystickButton.second) + { + action = kb.getHoldAction(input::CONTROLLER,button); + action.controller_id = heldJoystickButton.first; + m_actions.push_back(action); + } + } } std::vector Input::getActions() @@ -187,13 +210,25 @@ bool Input::isKeyPressed(int key) const return sf::Keyboard::isKeyPressed((sf::Keyboard::Key) key); } -void Input::releaseHeldKeys(sf::Keyboard::Key keycode){ - for(auto iter = m_heldkeys.begin();iter < m_heldkeys.end();) - iter = *iter == keycode ? m_heldkeys.erase(iter) : iter+1; +void Input::releaseHeldKeys(int keycode) +{ + for(auto iter = m_heldKeys.begin();iter < m_heldKeys.end();) + iter = *iter == keycode ? m_heldKeys.erase(iter) : iter+1; +} + +void Input::releaseHeldMouseButton(int buttoncode) +{ + for(auto iter = m_heldMouseButtons.begin();itergetSize())/2; } -float getAxisPosition(int device_id, int axis_id) -{ +std::vector Input::getControllersConnected() const{ + std::vector v; + for(auto &controller : m_heldJoystickButtons) + v.push_back(controller.first); +} +float Input::getAxisPosition(int device_id, int axis_id) +{ + if(m_joystick_position.count(device_id) && m_joystick_position[device_id].count(axis_id)) + return m_joystick_position[device_id][axis_id]; + else + return 0; } std::wstring Input::getText() diff --git a/src/input.h b/src/input.h index dba7d91..187e7d6 100644 --- a/src/input.h +++ b/src/input.h @@ -7,6 +7,7 @@ #include "keybindings.h" #include #include +#include #include #include "textbuffer.h" #include "glm/vec2.hpp" @@ -28,15 +29,23 @@ private: std::vector m_contexts; std::unordered_map m_keybindings; - /* keyboard-related variables */ - std::vector m_heldkeys; - void releaseHeldKeys(sf::Keyboard::Key keycode); + /* keyboard */ + std::vector m_heldKeys; + void releaseHeldKeys(int keycode); - /* mouse-related variables */ + /* mouse */ sf::Vector2i m_mouse_position; sf::Vector2i m_last_mouse_position; float m_delta_vertical_scroll; bool m_mouseGrabbed; + bool m_mouseWasGrabbed; + std::vector m_heldMouseButtons; + void releaseHeldMouseButton(int buttoncode); + + /* joystick */ + std::map> m_joystick_position; + std::map> m_heldJoystickButtons; + void releaseHeldJoystickButton(int buttoncode, int controller_id); /* text-related variable */ std::wstring m_buffer; @@ -63,9 +72,7 @@ public: bool isKeyPressed(int key) const; /* mouse-related function */ - //sf::Vector2i getPosition() const; glm::vec2 getPosition() const; - //sf::Vector2i getDeltaPosition() const; glm::vec2 getDeltaPosition() const; float getDeltaVerticalScroll() const; @@ -74,6 +81,10 @@ public: void toggleMouseGrab() { setMouseGrabbed(!m_mouseGrabbed); } bool isMouseGrabbed() const { return m_mouseGrabbed; } + /* joystick-related function */ + std::vector getControllersConnected() const; + float getAxisPosition(int device_id, int axis_id); + /* text-related function */ std::wstring getText();