/** * @author: Thomas Brandého */ #include "input.h" #include #include #include Input::Input(sf::Window *w): window(w) { heldkeys = std::vector(); action_file = std::queue(); window->setKeyRepeatEnabled(false); } void Input::setKeysMap(IKeysMap km){ keysmap = km; } void Input::updateEvents(){ sf::Event event; KeyBindings kb; char c; /* reset variables */ closeRequested = false; delta_vertical_scroll = 0; /* global affectation */ kb = keybindings[current_context]; /* event-parsing loop */ while(window->pollEvent(event)){ switch(event.type){ case sf::Event::Closed: closeRequested = true; break; case sf::Event::TextEntered: c = (char) event.text.unicode; buffer.append(&c); break; case sf::Event::KeyPressed: action_file.push(kb.getPressedAction(event.key.code)); heldkeys.push_back(event.key.code); break; case sf::Event::KeyReleased: action_file.push(kb.getReleasedAction(event.key.code)); releaseHeldKeys(event.key.code); break; case sf::Event::MouseButtonPressed: action_file.push(kb.getPressedAction(sf::Keyboard::KeyCount + event.mouseButton.button)); heldkeys.push_back((sf::Keyboard::Key) (sf::Keyboard::KeyCount + event.mouseButton.button)); break; case sf::Event::MouseButtonReleased: action_file.push(kb.getReleasedAction(sf::Keyboard::KeyCount + event.mouseButton.button)); releaseHeldKeys((sf::Keyboard::Key) (sf::Keyboard::KeyCount + event.mouseButton.button)); break; case sf::Event::MouseWheelScrolled: if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel) delta_vertical_scroll = event.mouseWheelScroll.delta; break; case sf::Event::MouseMoved: last_mouse_position = mouse_position; mouse_position = sf::Mouse::getPosition(); break; case sf::Event::MouseEntered: // action MouseEntered break; case sf::Event::MouseLeft: //action MouseLeft break; } for (auto key: heldkeys){ action_file.push(kb.getHoldAction(key)); } } } int Input::getAction() { if (action_file.empty()) return -1; int val = action_file.front(); action_file.pop(); return val; } /* context-related functions */ void Input::addContext(Context context) { contexts.push_back(context); } void Input::setCurrentContext(std::string context_name){ current_context = context_name; } void Input::updateKeyBindings(){ keybindings.clear(); for (auto iter= contexts.begin(); iter != contexts.end(); ++iter) keybindings[iter->getName()]= KeyBindings(*iter,keysmap); } /* window-related function */ bool Input::isCloseRequested() const { return closeRequested; } /* keyboard-related functions */ bool Input::isKeyPressed(int key) const { return sf::Keyboard::isKeyPressed((sf::Keyboard::Key) key); } void Input::releaseHeldKeys(sf::Keyboard::Key keycode){ auto iter = heldkeys.begin(); while(*iter != keycode) ++iter; heldkeys.erase(iter); } /* mouse-related functions */ sf::Vector2i Input::getPosition() const { return mouse_position; } sf::Vector2i Input::getDeltaPosition() const { return mouse_position - last_mouse_position; } float Input::getDeltaVerticalScroll() const { return delta_vertical_scroll; } /* ------ */ void Input::test() { KeyBindings kb = keybindings[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; }