diff --git a/src/input.cpp b/src/input.cpp index 1ae57ba..5a9b031 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -59,16 +59,11 @@ 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; - } + m_mouseWasGrabbed = m_mouseGrabbed; + m_mouseGrabbed = false; break; case sf::Event::GainedFocus: - if(m_mouseWasGrabbed){ - m_mouseGrabbed = true; - m_mouseWasGrabbed = false; - } + m_mouseGrabbed = m_mouseWasGrabbed; //Nothing to add here because the window already keep the state of the focus break; case sf::Event::TextEntered: @@ -114,7 +109,8 @@ void Input::updateEvents(){ 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 + std::cout << event.joystickButton.button << std::endl; + action = kb.getPressedAction(input::CONTROLLER,event.joystickButton.button); if(!action.isNull()){ action.controller_id = event.joystickButton.joystickId; m_actions.push_back(action); @@ -122,7 +118,7 @@ void Input::updateEvents(){ 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 + action = kb.getReleasedAction(input::CONTROLLER, event.joystickButton.button); if(!action.isNull()){ action.controller_id = event.joystickButton.joystickId; m_actions.push_back(action); @@ -130,34 +126,14 @@ void Input::updateEvents(){ 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: - { 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); m_heldJoystickButtons.erase(event.joystickConnect.joystickId); -// std::cout << "Joystick "<< event.joystickConnect.joystickId << " disconnected :" << id.name.toAnsiString() << std::endl; break; } - } } if(m_mouseGrabbed) sf::Mouse::setPosition(m_last_mouse_position, *m_window); @@ -254,16 +230,19 @@ void Input::setMouseGrabbed(bool isGrabbed) std::vector Input::getControllersConnected() const{ std::vector v; + std::cout << "pliplop " << m_heldJoystickButtons.size(); for(auto &controller : m_heldJoystickButtons) v.push_back(controller.first); + return v; } 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; + return sf::Joystick::getAxisPosition(device_id,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.cpp.autosave b/src/input.cpp.autosave new file mode 100644 index 0000000..5a9b031 --- /dev/null +++ b/src/input.cpp.autosave @@ -0,0 +1,265 @@ +/** +* @author: Thomas Brandého +*/ + +#include "input.h" + + +#include +#include +#include + +Input::Input(sf::Window *w) : + m_window(w), + m_closeRequested(false), + m_mouseGrabbed(false) +{ + m_heldKeys = std::vector(); + m_heldMouseButtons = std::vector(); + m_actions = std::vector(); + m_window->setKeyRepeatEnabled(false); +} + +void Input::setKeysMap(IKeysMap km){ + m_keysmap = km; +} + +void Input::updateEvents(){ + sf::Event event; + KeyBindings kb; + char c; + + /* reset variables */ + m_closeRequested = false; + m_hasBeenResized = false; + m_delta_vertical_scroll = 0; + m_buffer.clear(); + + if(m_mouseGrabbed) + m_last_mouse_position = sf::Vector2i(m_window->getSize())/2; + else + m_last_mouse_position = m_mouse_position; + + /* global affectation */ + kb = m_keybindings[m_current_context]; + m_actions.clear(); + + Action action; + + /* event-parsing loop */ + while(m_window->pollEvent(event)) + { + switch(event.type){ + case sf::Event::Closed: + m_closeRequested = true; + break; + case sf::Event::Resized: + m_hasBeenResized = true; + if(m_mouseGrabbed) + m_last_mouse_position = sf::Vector2i(m_window->getSize())/2; + break; + case sf::Event::LostFocus: + m_mouseWasGrabbed = m_mouseGrabbed; + m_mouseGrabbed = false; + break; + case sf::Event::GainedFocus: + m_mouseGrabbed = m_mouseWasGrabbed; + //Nothing to add here because the window already keep the state of the focus + break; + case sf::Event::TextEntered: + c = (char) event.text.unicode; + sf::Utf32::encodeAnsi(event.text.unicode,std::back_inserter(m_buffer)); + break; + case sf::Event::KeyPressed: + action = kb.getPressedAction(input::KEYBOARD,event.key.code); + if(!action.isNull()) + m_actions.push_back(action); + 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); + break; + case sf::Event::MouseWheelScrolled: + if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel) + m_delta_vertical_scroll = event.mouseWheelScroll.delta; + break; + case sf::Event::MouseButtonPressed: + action = kb.getPressedAction(input::MOUSE,event.mouseButton.button); + if(!action.isNull()) + m_actions.push_back(action); + 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); + releaseHeldMouseButton(event.mouseButton.button); + break; + case sf::Event::MouseMoved: + m_mouse_position = sf::Mouse::getPosition(*m_window); + break; + case sf::Event::MouseEntered: + // action MouseEntered + break; + case sf::Event::MouseLeft: + if(m_mouseGrabbed) + sf::Mouse::setPosition(m_last_mouse_position,*m_window); + break; + case sf::Event::JoystickButtonPressed: + std::cout << event.joystickButton.button << std::endl; + action = kb.getPressedAction(input::CONTROLLER,event.joystickButton.button); + if(!action.isNull()){ + action.controller_id = event.joystickButton.joystickId; + m_actions.push_back(action); + } + m_heldJoystickButtons[action.controller_id].push_back(event.joystickButton.button); + break; + case sf::Event::JoystickButtonReleased: + action = kb.getReleasedAction(input::CONTROLLER, event.joystickButton.button); + if(!action.isNull()){ + action.controller_id = event.joystickButton.joystickId; + m_actions.push_back(action); + } + releaseHeldJoystickButton(event.joystickButton.button,action.controller_id); + break; + case sf::Event::JoystickMoved: + break; + case sf::Event::JoystickConnected: + m_heldJoystickButtons[event.joystickConnect.joystickId] = std::vector(); + break; + case sf::Event::JoystickDisconnected: + m_heldJoystickButtons.erase(event.joystickConnect.joystickId); + break; + } + } + if(m_mouseGrabbed) + sf::Mouse::setPosition(m_last_mouse_position, *m_window); + + 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) + { + 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() +{ + return m_actions; +} + +/* context-related functions */ +void Input::updateKeyBindings(){ + m_keybindings.clear(); + for (auto iter= m_contexts.begin(); iter != m_contexts.end(); ++iter) + m_keybindings[iter->getName()]= KeyBindings(*iter,m_keysmap); +} + +/* window-related function */ + +bool Input::isCloseRequested() const +{ + return m_closeRequested; +} + +bool Input::isResized() const +{ + return m_hasBeenResized; +} + +/* keyboard-related functions */ + +bool Input::isKeyPressed(int key) const +{ + return sf::Keyboard::isKeyPressed((sf::Keyboard::Key) key); +} + +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; +} + +std::vector Input::getControllersConnected() const{ + std::vector v; + std::cout << "pliplop " << m_heldJoystickButtons.size(); + for(auto &controller : m_heldJoystickButtons) + v.push_back(controller.first); + return v; +} + +float Input::getAxisPosition(int device_id, int axis_id) +{ + return sf::Joystick::getAxisPosition(device_id,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() +{ + return m_buffer; +} + +/* ------ */ + +void Input::test() +{ +/* KeyBindings kb = m_keybindings[m_current_context]; + int action; + action = kb.getPressedAction(sf::Keyboard::I); + if (action != NO_ACTION) + std::cerr << action << std::endl; + action = kb.getPressedAction(sf::Keyboard::O); + if (action != NO_ACTION) + std::cerr << action << std::endl;*/ +} diff --git a/src/main.cpp b/src/main.cpp index 1c69835..9d0caaa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,8 +41,12 @@ int main() myInput.updateEvents(); for (Action action : myInput.getActions()) { - if (action.action == TestKeysMap::ACTION) - std::cout << "Plop" << action.source << std::endl; + if (action.action == TestKeysMap::ACTION && action.source == input::CONTROLLER && action.controller_id == 0){ + std::cout << "Controllers connected: " << myInput.getControllersConnected().size(); +// for(auto controller : myInput.getControllersConnected()) + // std::cout << controller << " "; + std::cout << std::endl; + } } } /* IKeysMap* keymap = new IKeysMap("test", 1);