SparrowInput/input.cpp
2015-07-17 20:04:19 +02:00

179 lines
4.5 KiB
C++

/**
* @author: Thomas Brandého
*/
#include "input.h"
#include <iostream>
#include <SFML/Window.hpp>
#include <list>
Input::Input(sf::Window* w, std::string keysmappings, int nb_actions): window(w)
{
keysmap= KeysMap(keysmappings, nb_actions);
heldkeys = std::vector<sf::Keyboard::Key>();
action_file = std::queue<int>();
window->setKeyRepeatEnabled(false);
/* test */
std::vector<int> mv;
mv.push_back(0);
current_context = "default";
keybindings[current_context] = KeyBindings(mv,&keysmap);
// std::cerr << keybindings[current_context].getPressedAction(sf::Keyboard::I) << std::endl;
}
int Input::getKeyBinding(int action,int num){
return keysmap.getKeyBinding(action,num-1); //offset num between 0 and 1
}
void Input::setkeyBinding(int action, int num, sf::Keyboard::Key key){
keysmap.setKeyBinding(action,num-1,key);
}
void Input::setTypeAction(int action, int type)
{
keysmap.setTypeAction(action, type);
}
void Input::createContext(std::string context_name, std::vector<int> action_list)
{
contexts[context_name]=action_list;
}
void Input::setCurrentContext(std::string context_name){
current_context = context_name;
}
void Input::reloadKeyBindings(){
for (auto iter= keybindings.begin(); iter != keybindings.end(); ++iter){
iter->second= KeyBindings(contexts[iter->first],&keysmap);
}
}
void Input::updateEvents(){
sf::Event event;
KeyBindings kb;
/* TEST ------------------------------------------------- */
// for(auto iter= kb.bindings_hold.begin(); iter != kb.bindings_hold.end(); ++iter){
// std::cerr << iter->second << std::endl;
// }
/* ------------------------------------------------------- */
/* reset variables */
closeRequested = false;
/* 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:
// add text entered to buffer
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::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;
}
/* window-related function */
bool Input::isCloseRequested()
{
return closeRequested;
}
/* keyboard-related function */
bool Input::isKeyPressed(int key)
{
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 function */
sf::Vector2i Input::getPosition()
{
mouse_position = sf::Mouse::getPosition();
return mouse_position;
}
sf::Vector2i Input::getDeltaPosition()
{
sf::Vector2i last_position = mouse_position;
mouse_position = sf::Mouse::getPosition();
return mouse_position - last_position;
}
int Input::getDeltaVerticalScroll()
{
return delta_vertical_scroll;
}
void Input::test()
{
std::cerr << keysmap.getTypeAction(1) << std::endl;
std::cerr << keysmap.getKeyBinding(1,0) << std::endl;
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;
}