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