From cb384a676ffbb55dbddd94aa997d3af736a674d9 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Tue, 21 Jul 2015 19:34:12 +0200 Subject: [PATCH] V1 of SparrowInput, textbuffer not used --- input.cpp | 98 +++++++++++++++---------------------- input.h | 37 ++++++++------ keybindings.cpp | 127 +++++++++++++++++++----------------------------- keybindings.h | 47 ++++++++++-------- main.cpp | 68 ++++++++++++++------------ textbuffer.cpp | 39 ++++++++++++++- textbuffer.h | 15 ++++++ 7 files changed, 228 insertions(+), 203 deletions(-) diff --git a/input.cpp b/input.cpp index b59d561..1688bd7 100644 --- a/input.cpp +++ b/input.cpp @@ -8,67 +8,30 @@ #include #include -Input::Input(sf::Window* w, std::string keysmappings, int nb_actions): window(w) +Input::Input(sf::Window *w): window(w) { - keysmap= KeysMap(keysmappings, nb_actions); heldkeys = std::vector(); action_file = std::queue(); window->setKeyRepeatEnabled(false); - /* test */ - std::vector 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 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::setKeysMap(IKeysMap km){ + keysmap = km; } 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; -// } - - /* ------------------------------------------------------- */ + 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){ @@ -76,7 +39,8 @@ void Input::updateEvents(){ closeRequested = true; break; case sf::Event::TextEntered: - // add text entered to buffer + c = (char) event.text.unicode; + buffer.append(&c); break; case sf::Event::KeyPressed: action_file.push(kb.getPressedAction(event.key.code)); @@ -98,13 +62,16 @@ void Input::updateEvents(){ 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)); @@ -112,7 +79,8 @@ void Input::updateEvents(){ } } -int Input::getAction(){ +int Input::getAction() +{ if (action_file.empty()) return -1; int val = action_file.front(); @@ -120,18 +88,34 @@ int Input::getAction(){ 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() +bool Input::isCloseRequested() const { return closeRequested; } -/* keyboard-related function */ +/* keyboard-related functions */ -bool Input::isKeyPressed(int key) +bool Input::isKeyPressed(int key) const { return sf::Keyboard::isKeyPressed((sf::Keyboard::Key) key); } @@ -142,31 +126,27 @@ void Input::releaseHeldKeys(sf::Keyboard::Key keycode){ heldkeys.erase(iter); } -/* mouse-related function */ +/* mouse-related functions */ -sf::Vector2i Input::getPosition() +sf::Vector2i Input::getPosition() const { - mouse_position = sf::Mouse::getPosition(); return mouse_position; } -sf::Vector2i Input::getDeltaPosition() +sf::Vector2i Input::getDeltaPosition() const { - sf::Vector2i last_position = mouse_position; - mouse_position = sf::Mouse::getPosition(); - return mouse_position - last_position; + return mouse_position - last_mouse_position; } -int Input::getDeltaVerticalScroll() +float Input::getDeltaVerticalScroll() const { 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); diff --git a/input.h b/input.h index b151376..0c2f908 100644 --- a/input.h +++ b/input.h @@ -8,36 +8,36 @@ #include #include #include +#include "textbuffer.h" class Input{ public: /* Constructors */ - Input(sf::Window* w, std::string keysmappings, int nb_actions); - -// enum SpecialAction {} + Input(sf::Window *w); /* general action-mapping functions */ + void setKeysMap(IKeysMap km); void updateEvents(); int getAction(); - int getKeyBinding(int action, int num); - void setkeyBinding(int action, int num, sf::Keyboard::Key key); - void setTypeAction(int action, int type); /* context-related functions */ - void createContext(std::string context_name, std::vector action_list); + void addContext(Context context); void setCurrentContext(std::string context_name); - void reloadKeyBindings(); + void updateKeyBindings(); /* window-related function */ - bool isCloseRequested(); + bool isCloseRequested() const; /* keyboard-related functions */ - bool isKeyPressed(int key); + bool isKeyPressed(int key) const; /* mouse-related function */ - sf::Vector2i getPosition(); - sf::Vector2i getDeltaPosition(); - int getDeltaVerticalScroll(); + sf::Vector2i getPosition() const; + sf::Vector2i getDeltaPosition() const; + float getDeltaVerticalScroll() const; + + /* text-related function */ + std::string getText(); void test(); @@ -47,13 +47,14 @@ class Input{ bool closeRequested; /* general action-mapping variables */ - KeysMap keysmap; + IKeysMap keysmap; std::queue action_file; int nb_actions; /* context-related variables */ std::string current_context; - std::unordered_map> contexts; + //std::unordered_map> contexts; + std::vector contexts; std::unordered_map keybindings; /* keyboard-related variables */ @@ -62,7 +63,11 @@ class Input{ /* mouse-related variables */ sf::Vector2i mouse_position; - int delta_vertical_scroll; + sf::Vector2i last_mouse_position; + float delta_vertical_scroll; + + /* text-related variable */ + std::string buffer; }; #endif //INPUT_H diff --git a/keybindings.cpp b/keybindings.cpp index fff050a..69c8b05 100644 --- a/keybindings.cpp +++ b/keybindings.cpp @@ -5,59 +5,23 @@ #include "keybindings.h" #include -/* Implementation of KeysMap class +/* Implementation of IKeysMap class * @author: Thomas Brandého * @info: This class register all the association between key and action. */ -KeysMap::KeysMap() +IKeysMap::IKeysMap() { + } -KeysMap::KeysMap(std::string keysmapping, int nb_actions):keysmapping_file(keysmapping), size_keys_map(nb_actions*2) -{ - keys_map = (int*) malloc(size_keys_map*sizeof(int)); - for(int i = 0; i < size_keys_map; i++) - keys_map[i] = NO_KEY; - actiontype_map = (int*) malloc((size_keys_map/2)*sizeof(int)); - for (int i = 0; i < size_keys_map/2 ; i++) - actiontype_map[i] = PRESSED; - loadKeysMap(); +std::vector IKeysMap::getBindings(int action) const { + std::vector bindings; + for (auto binding : keys) + if (binding.action == action) bindings.push_back(binding); + return bindings; } -int KeysMap::getKeyBinding(int action,int num) -{ - return keys_map[action*NB_BINDINGS+num]; -} - -void KeysMap::setKeyBinding(int action, int num, int key) -{ - keys_map[(action*NB_BINDINGS)+num] = key; -} - -int KeysMap::getTypeAction(int action) -{ - return actiontype_map[action]; -} - -void KeysMap::setTypeAction(int action, int type) -{ - actiontype_map[action]=type; -} - -void KeysMap::saveKeysMap() -{ - //TODO: Serialize map_keys - //use keysmapping_file -} - -void KeysMap::loadKeysMap() -{ - //TODO: Load map_keys - //use keysmapping_file -} - - /* Implementation of KeyBindings class * @author: Thomas Brandého * @info: This class map a list of action with the associated keys, for a quick access in-game. @@ -67,60 +31,46 @@ void KeysMap::loadKeysMap() KeyBindings::KeyBindings() { } - -KeyBindings::KeyBindings(std::vector action_list, KeysMap* keysmap) +KeyBindings::KeyBindings(const Context &context, const IKeysMap &keysmap) { - int key; - for (int action : action_list){ - switch(keysmap->getTypeAction(action)){ - case KeysMap::PRESSED: - for(int i=0; igetKeyBinding(action,i); - if (key != NO_KEY) - setPressedAction(key,action); - } + std::vector actions = context.getActions(); + for (int action : actions){ + for (Binding binding : keysmap.getBindings(action)){ + switch(binding.type){ + case IKeysMap::PRESSED: + setPressedAction(binding.key,binding.action); break; - case KeysMap::RELEASED: - for(int i=0; igetKeyBinding(action,i); - if (key != NO_KEY) - setReleasedAction(key,action); - } + case IKeysMap::RELEASED: + setReleasedAction(binding.key,binding.action); break; - case KeysMap::HOLD: - for(int i=0; igetKeyBinding(action,i); - if (key != NO_KEY) - setHoldAction(key,action); - } + case IKeysMap::HOLD: + setHoldAction(binding.key,binding.action); break; default: //raiseError; break; + } } } } -int KeyBindings::getPressedAction(int key) +int KeyBindings::getPressedAction(int key) const { - return bindings_pressed.count(key) == 1? bindings_pressed[key] : -1; + return bindings_pressed.count(key) == 1? bindings_pressed.at(key) : -1; } - -int KeyBindings::getReleasedAction(int key) +int KeyBindings::getReleasedAction(int key) const { - return bindings_released.count(key) == 1? bindings_released[key] : -1; + return bindings_released.count(key) == 1? bindings_released.at(key) : -1; } - -int KeyBindings::getHoldAction(int key) +int KeyBindings::getHoldAction(int key) const { - return bindings_hold.count(key) == 1? bindings_hold[key] : -1; + return bindings_hold.count(key) == 1? bindings_hold.at(key) : -1; } void KeyBindings::setPressedAction(int key, int action) { bindings_pressed[key]=action; } - void KeyBindings::setReleasedAction(int key, int action) { bindings_released[key]=action; @@ -130,3 +80,28 @@ void KeyBindings::setHoldAction(int key, int action) { bindings_hold[key]=action; } + +/* Implementation of Context class + * @author: Thomas Brandého + * @info: This class contains a list of actions available in a given situation (context). + */ + +/* Implementation of Context class + * @author: Thomas Brandého + * @info: This class contains a list of action associated to a given situation (context). + */ + + +Context::Context(std::string _name, std::vector _actions): name(_name), actions(_actions) +{ + +} + +std::string Context::getName() +{ + return name; +} +std::vector Context::getActions() const +{ + return actions; +} diff --git a/keybindings.h b/keybindings.h index fa0a390..d202f1a 100644 --- a/keybindings.h +++ b/keybindings.h @@ -12,38 +12,43 @@ #define NO_KEY -1 #define NO_ACTION -1 -class KeysMap { - public: - enum {PRIMARY,SECONDARY,NB_BINDINGS}; - enum {PRESSED, RELEASED, HOLD}; - KeysMap(); - KeysMap(std::string keysmapping, int nb_actions); +typedef struct{ + int action; + int key; + int type; +} Binding; - int getKeyBinding(int action,int num); - void setKeyBinding(int action, int num, int key); - int getTypeAction(int action); - void setTypeAction(int action, int type); - void saveKeysMap(); - void loadKeysMap(); +class IKeysMap{ +public: + enum {PRESSED, RELEASED, HOLD}; + IKeysMap(); + std::vector getBindings(int action) const; +protected: + std::vector keys; +}; + +class Context { + public: + Context(std::string name, std::vector actions); + std::string getName(); + std::vector getActions() const; private: - std::string keysmapping_file; - int size_keys_map = 0; - int* keys_map; - int* actiontype_map; + std::string name; + std::vector actions; }; class KeyBindings { public: KeyBindings(); - KeyBindings(const std::vector action_list, KeysMap* keysmap); - int getPressedAction(int key); - int getReleasedAction(int key); - int getHoldAction(int key); - std::unordered_map bindings_hold; + KeyBindings(const Context &context, const IKeysMap &keysmap); + int getPressedAction(int key) const; + int getReleasedAction(int key) const; + int getHoldAction(int key) const; private: std::unordered_map bindings_pressed; std::unordered_map bindings_released; + std::unordered_map bindings_hold; void setPressedAction(int key, int action); void setReleasedAction(int key, int action); void setHoldAction(int key, int action); diff --git a/main.cpp b/main.cpp index e0b789d..46e018d 100644 --- a/main.cpp +++ b/main.cpp @@ -8,47 +8,55 @@ int main() { cerr << "Hello World!"<< endl; sf::Window* window= new sf::Window(sf::VideoMode(400,200),"testSparrowInput"); - Input myInput(window, "test.esk",2); + Input myInput(window); enum {ACTION_1,ACTION_2}; std::vector myvector; + myvector.push_back(ACTION_1); myvector.push_back(ACTION_2); -/* KeysMap* keymap = new KeysMap("test", 2); +// KeysMap* keymap = new KeysMap("test", 1); - keymap->setKeyBinding(ACTION_1,0,sf::Keyboard::A); - keymap->setKeyBinding(ACTION_1,1,sf::Keyboard::Z); - keymap->setTypeAction(ACTION_1,KeysMap::HOLD); +// keymap->setKeyBinding(ACTION_2,0,sf::Keyboard::Z); +// keymap->setTypeAction(ACTION_2,KeysMap::RELEASED); // cerr << keymap->getKeyBinding(ACTION_1,0) << endl; - cerr << keymap->getTypeAction(ACTION_1) << endl; -*/ - //KeyBindings keybind(myvector,keymap); - //cerr << " Action : " << keybind.getPressedAction(sf::Keyboard::A) << endl; +// cerr << keymap->getTypeAction(ACTION_1) << endl; - myInput.setkeyBinding(ACTION_1,1,sf::Keyboard::A); - myInput.setkeyBinding(ACTION_1,2,sf::Keyboard::Z); - myInput.setkeyBinding(ACTION_2,1,sf::Keyboard::I); - myInput.setkeyBinding(ACTION_2,2,sf::Keyboard::O); +// KeyBindings keybind(myvector,keymap); +// cerr << " Action : " << keybind.getReleasedAction(sf::Keyboard::Z) << endl; +// +// myInput.setkeyBinding(ACTION_1,1,sf::Keyboard::A); +// myInput.setkeyBinding(ACTION_1,2,sf::Keyboard::Z); +// myInput.setkeyBinding(ACTION_2,1,sf::Keyboard::I); +// myInput.setkeyBinding(ACTION_2,2,sf::Keyboard::O); - myInput.setTypeAction(ACTION_1, KeysMap::RELEASED); +// myInput.setTypeAction(ACTION_1, KeysMap::HOLD); +// myInput.setTypeAction(ACTION_2, KeysMap::RELEASED); - myInput.test(); + //myInput.test(); - myInput.createContext("test", myvector); - myInput.setCurrentContext("test"); +// myInput.createContext("test", myvector); +// myInput.setCurrentContext("test"); // myInput.reloadKeyBindings(); - bool run = true; - while(run){ - //myInput.updateEvents(); - if (myInput.isCloseRequested()){ - window->close(); - run = false; - } - while (myInput.getAction() != NO_ACTION) - cout << "test " << myInput.getAction() << endl; -/* if (myInput.getAction() == ACTION_2) - cout << "hello" << endl;*/ - window->display(); - } +// int action; +// bool run = true; +// while(run){ +// myInput.updateEvents(); +// if (myInput.isCloseRequested()){ +// window->close(); +// run = false; +// } +// while (action = myInput.getAction() != NO_ACTION) +// cout << "test " << action << endl; +// window->display(); +// } return 0; } +/*class MyInput : public Input +{ + MyInput(sf::Window *w) : Input(w){ + + } + +}; +*/ diff --git a/textbuffer.cpp b/textbuffer.cpp index 3212f83..154d2a5 100644 --- a/textbuffer.cpp +++ b/textbuffer.cpp @@ -2,6 +2,43 @@ TextBuffer::TextBuffer() { - + resetBuffer(); } + +void TextBuffer::resetBuffer() +{ + buffer.clear(); + cursor = 0; +} + +int TextBuffer::getCursorPosition() const +{ + return cursor; +} + +void TextBuffer::setCursorPosition(int pos) +{ + cursor = pos; +} + +void TextBuffer::moveCursorLeft() +{ + if (cursor > 0) cursor--; +} + +void TextBuffer::moveCursorRight() +{ + if (cursor < buffer.size()) cursor++; +} + +std::string TextBuffer::getText() const +{ + return buffer; +} + +void TextBuffer::insertText(char c) +{ + buffer.insert(cursor,&c); + moveCursorRight(); +} diff --git a/textbuffer.h b/textbuffer.h index e584a56..98c0670 100644 --- a/textbuffer.h +++ b/textbuffer.h @@ -7,8 +7,23 @@ class TextBuffer public: TextBuffer(); + void resetBuffer(); + + // cursor-related function + int getCursorPosition() const; + void setCursorPosition(int pos); + void moveCursorLeft(); + void moveCursorRight(); + + // string-related function + std::string getText() const; + void insertText(char c); + + + private: std::string buffer; + int cursor; };