changed the way the actions are handled
This commit is contained in:
parent
5b3cdb7449
commit
d9fec5af18
@ -10,16 +10,16 @@
|
||||
#include <list>
|
||||
|
||||
Input::Input(sf::Window *w) :
|
||||
window(w),
|
||||
closeRequested(false)
|
||||
m_window(w),
|
||||
m_closeRequested(false)
|
||||
{
|
||||
heldkeys = std::vector<sf::Keyboard::Key>();
|
||||
action_file = std::queue<int>();
|
||||
window->setKeyRepeatEnabled(false);
|
||||
m_heldkeys = std::vector<sf::Keyboard::Key>();
|
||||
m_actions = std::vector<int>();
|
||||
m_window->setKeyRepeatEnabled(false);
|
||||
}
|
||||
|
||||
void Input::setKeysMap(IKeysMap km){
|
||||
keysmap = km;
|
||||
m_keysmap = km;
|
||||
}
|
||||
|
||||
void Input::updateEvents(){
|
||||
@ -28,46 +28,48 @@ void Input::updateEvents(){
|
||||
char c;
|
||||
|
||||
/* reset variables */
|
||||
closeRequested = false;
|
||||
hasBeenResized = false;
|
||||
delta_vertical_scroll = 0;
|
||||
m_closeRequested = false;
|
||||
m_hasBeenResized = false;
|
||||
m_delta_vertical_scroll = 0;
|
||||
|
||||
/* global affectation */
|
||||
kb = keybindings[current_context];
|
||||
kb = m_keybindings[m_current_context];
|
||||
m_actions.clear();
|
||||
|
||||
/* event-parsing loop */
|
||||
while(window->pollEvent(event)){
|
||||
while(m_window->pollEvent(event)){
|
||||
// std::cout << event.type << std::endl;
|
||||
switch(event.type){
|
||||
case sf::Event::Closed:
|
||||
closeRequested = true;
|
||||
m_closeRequested = true;
|
||||
break;
|
||||
case sf::Event::TextEntered:
|
||||
c = (char) event.text.unicode;
|
||||
buffer.append(&c);
|
||||
m_buffer.append(&c);
|
||||
break;
|
||||
case sf::Event::KeyPressed:
|
||||
action_file.push(kb.getPressedAction(event.key.code));
|
||||
heldkeys.push_back(event.key.code);
|
||||
m_actions.push_back(kb.getPressedAction(event.key.code));
|
||||
m_heldkeys.push_back(event.key.code);
|
||||
break;
|
||||
case sf::Event::KeyReleased:
|
||||
action_file.push(kb.getReleasedAction(event.key.code));
|
||||
m_actions.push_back(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));
|
||||
m_actions.push_back(kb.getPressedAction(sf::Keyboard::KeyCount + event.mouseButton.button));
|
||||
m_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));
|
||||
m_actions.push_back(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;
|
||||
m_delta_vertical_scroll = event.mouseWheelScroll.delta;
|
||||
break;
|
||||
case sf::Event::MouseMoved:
|
||||
last_mouse_position = mouse_position;
|
||||
mouse_position = sf::Mouse::getPosition();
|
||||
m_last_mouse_position = m_mouse_position;
|
||||
m_mouse_position = sf::Mouse::getPosition();
|
||||
break;
|
||||
case sf::Event::MouseEntered:
|
||||
// action MouseEntered
|
||||
@ -76,53 +78,48 @@ void Input::updateEvents(){
|
||||
//action MouseLeft
|
||||
break;
|
||||
case sf::Event::Resized:
|
||||
hasBeenResized = true;
|
||||
m_hasBeenResized = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (auto key: heldkeys){
|
||||
action_file.push(kb.getHoldAction(key));
|
||||
for (auto key: m_heldkeys){
|
||||
m_actions.push_back(kb.getHoldAction(key));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int Input::getAction()
|
||||
std::vector<int> Input::getActions()
|
||||
{
|
||||
if (action_file.empty())
|
||||
return -1;
|
||||
int val = action_file.front();
|
||||
action_file.pop();
|
||||
return val;
|
||||
return m_actions;
|
||||
}
|
||||
|
||||
|
||||
/* context-related functions */
|
||||
void Input::addContext(Context context)
|
||||
{
|
||||
contexts.push_back(context);
|
||||
m_contexts.push_back(context);
|
||||
}
|
||||
|
||||
void Input::setCurrentContext(std::string context_name){
|
||||
current_context = context_name;
|
||||
m_current_context = context_name;
|
||||
}
|
||||
|
||||
void Input::updateKeyBindings(){
|
||||
keybindings.clear();
|
||||
for (auto iter= contexts.begin(); iter != contexts.end(); ++iter)
|
||||
keybindings[iter->getName()]= KeyBindings(*iter,keysmap);
|
||||
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 closeRequested;
|
||||
return m_closeRequested;
|
||||
}
|
||||
|
||||
bool Input::isResized() const
|
||||
{
|
||||
return hasBeenResized;
|
||||
return m_hasBeenResized;
|
||||
}
|
||||
|
||||
/* keyboard-related functions */
|
||||
@ -133,33 +130,33 @@ bool Input::isKeyPressed(int key) const
|
||||
}
|
||||
|
||||
void Input::releaseHeldKeys(sf::Keyboard::Key keycode){
|
||||
auto iter = heldkeys.begin();
|
||||
auto iter = m_heldkeys.begin();
|
||||
while(*iter != keycode) ++iter;
|
||||
heldkeys.erase(iter);
|
||||
m_heldkeys.erase(iter);
|
||||
}
|
||||
|
||||
/* mouse-related functions */
|
||||
|
||||
sf::Vector2i Input::getPosition() const
|
||||
{
|
||||
return mouse_position;
|
||||
return m_mouse_position;
|
||||
}
|
||||
|
||||
sf::Vector2i Input::getDeltaPosition() const
|
||||
{
|
||||
return mouse_position - last_mouse_position;
|
||||
return m_mouse_position - m_last_mouse_position;
|
||||
}
|
||||
|
||||
float Input::getDeltaVerticalScroll() const
|
||||
{
|
||||
return delta_vertical_scroll;
|
||||
return m_delta_vertical_scroll;
|
||||
}
|
||||
|
||||
/* ------ */
|
||||
|
||||
void Input::test()
|
||||
{
|
||||
KeyBindings kb = keybindings[current_context];
|
||||
KeyBindings kb = m_keybindings[m_current_context];
|
||||
int action;
|
||||
action = kb.getPressedAction(sf::Keyboard::I);
|
||||
if (action != NO_ACTION)
|
||||
|
32
src/input.h
32
src/input.h
@ -16,9 +16,9 @@ class Input{
|
||||
Input(sf::Window *w);
|
||||
|
||||
/* general action-mapping functions */
|
||||
void setKeysMap(IKeysMap km);
|
||||
void updateEvents();
|
||||
int getAction();
|
||||
void setKeysMap(IKeysMap km); //set bindings
|
||||
void updateEvents(); //handle the input and put the associated event in the file
|
||||
std::vector<int> getActions(); //get the first action in the file of event
|
||||
|
||||
/* context-related functions */
|
||||
void addContext(Context context);
|
||||
@ -44,32 +44,32 @@ class Input{
|
||||
|
||||
private:
|
||||
/* window-related variables */
|
||||
sf::Window* window;
|
||||
bool closeRequested;
|
||||
bool hasBeenResized;
|
||||
sf::Window* m_window;
|
||||
bool m_closeRequested;
|
||||
bool m_hasBeenResized;
|
||||
|
||||
/* general action-mapping variables */
|
||||
IKeysMap keysmap;
|
||||
std::queue<int> action_file;
|
||||
IKeysMap m_keysmap;
|
||||
std::vector<int> m_actions;
|
||||
int nb_actions;
|
||||
|
||||
/* context-related variables */
|
||||
std::string current_context;
|
||||
std::string m_current_context;
|
||||
//std::unordered_map<std::string, std::vector<int>> contexts;
|
||||
std::vector<Context> contexts;
|
||||
std::unordered_map<std::string,KeyBindings> keybindings;
|
||||
std::vector<Context> m_contexts;
|
||||
std::unordered_map<std::string,KeyBindings> m_keybindings;
|
||||
|
||||
/* keyboard-related variables */
|
||||
std::vector<sf::Keyboard::Key> heldkeys;
|
||||
std::vector<sf::Keyboard::Key> m_heldkeys;
|
||||
void releaseHeldKeys(sf::Keyboard::Key keycode);
|
||||
|
||||
/* mouse-related variables */
|
||||
sf::Vector2i mouse_position;
|
||||
sf::Vector2i last_mouse_position;
|
||||
float delta_vertical_scroll;
|
||||
sf::Vector2i m_mouse_position;
|
||||
sf::Vector2i m_last_mouse_position;
|
||||
float m_delta_vertical_scroll;
|
||||
|
||||
/* text-related variable */
|
||||
std::string buffer;
|
||||
std::string m_buffer;
|
||||
};
|
||||
|
||||
#endif //INPUT_H
|
||||
|
@ -33,8 +33,8 @@ KeyBindings::KeyBindings()
|
||||
}
|
||||
KeyBindings::KeyBindings(const Context &context, const IKeysMap &keysmap)
|
||||
{
|
||||
std::vector<int> actions = context.getActions();
|
||||
for (int action : actions){
|
||||
for (int action : context.getActions()){
|
||||
std::cout << "plop" << std::endl;
|
||||
for (Binding binding : keysmap.getBindings(action)){
|
||||
switch(binding.type){
|
||||
case IKeysMap::PRESSED:
|
||||
|
@ -14,6 +14,8 @@ int main()
|
||||
std::vector<int> myvector;
|
||||
myvector.push_back(ACTION_1);
|
||||
myvector.push_back(ACTION_2);
|
||||
while(!myInput.isCloseRequested())
|
||||
myInput.updateEvents();
|
||||
|
||||
// IKeysMap* keymap = new IKeysMap("test", 1);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user