V1 of SparrowInput, textbuffer not used

This commit is contained in:
Lendemor 2015-07-21 19:34:12 +02:00
parent 390e4b413f
commit cb384a676f
7 changed files with 228 additions and 203 deletions

View File

@ -8,67 +8,30 @@
#include <SFML/Window.hpp> #include <SFML/Window.hpp>
#include <list> #include <list>
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<sf::Keyboard::Key>(); heldkeys = std::vector<sf::Keyboard::Key>();
action_file = std::queue<int>(); action_file = std::queue<int>();
window->setKeyRepeatEnabled(false); 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){ void Input::setKeysMap(IKeysMap km){
return keysmap.getKeyBinding(action,num-1); //offset num between 0 and 1 keysmap = km;
}
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(){ void Input::updateEvents(){
sf::Event event; sf::Event event;
KeyBindings kb; KeyBindings kb;
char c;
/* TEST ------------------------------------------------- */
// for(auto iter= kb.bindings_hold.begin(); iter != kb.bindings_hold.end(); ++iter){
// std::cerr << iter->second << std::endl;
// }
/* ------------------------------------------------------- */
/* reset variables */ /* reset variables */
closeRequested = false; closeRequested = false;
delta_vertical_scroll = 0;
/* global affectation */ /* global affectation */
kb = keybindings[current_context]; kb = keybindings[current_context];
/* event-parsing loop */ /* event-parsing loop */
while(window->pollEvent(event)){ while(window->pollEvent(event)){
switch(event.type){ switch(event.type){
@ -76,7 +39,8 @@ void Input::updateEvents(){
closeRequested = true; closeRequested = true;
break; break;
case sf::Event::TextEntered: case sf::Event::TextEntered:
// add text entered to buffer c = (char) event.text.unicode;
buffer.append(&c);
break; break;
case sf::Event::KeyPressed: case sf::Event::KeyPressed:
action_file.push(kb.getPressedAction(event.key.code)); action_file.push(kb.getPressedAction(event.key.code));
@ -98,13 +62,16 @@ void Input::updateEvents(){
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel) if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel)
delta_vertical_scroll = event.mouseWheelScroll.delta; delta_vertical_scroll = event.mouseWheelScroll.delta;
break; break;
case sf::Event::MouseMoved:
last_mouse_position = mouse_position;
mouse_position = sf::Mouse::getPosition();
break;
case sf::Event::MouseEntered: case sf::Event::MouseEntered:
// action MouseEntered // action MouseEntered
break; break;
case sf::Event::MouseLeft: case sf::Event::MouseLeft:
//action MouseLeft //action MouseLeft
break; break;
} }
for (auto key: heldkeys){ for (auto key: heldkeys){
action_file.push(kb.getHoldAction(key)); action_file.push(kb.getHoldAction(key));
@ -112,7 +79,8 @@ void Input::updateEvents(){
} }
} }
int Input::getAction(){ int Input::getAction()
{
if (action_file.empty()) if (action_file.empty())
return -1; return -1;
int val = action_file.front(); int val = action_file.front();
@ -120,18 +88,34 @@ int Input::getAction(){
return val; 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 */ /* window-related function */
bool Input::isCloseRequested() bool Input::isCloseRequested() const
{ {
return closeRequested; 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); return sf::Keyboard::isKeyPressed((sf::Keyboard::Key) key);
} }
@ -142,31 +126,27 @@ void Input::releaseHeldKeys(sf::Keyboard::Key keycode){
heldkeys.erase(iter); 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; return mouse_position;
} }
sf::Vector2i Input::getDeltaPosition() sf::Vector2i Input::getDeltaPosition() const
{ {
sf::Vector2i last_position = mouse_position; return mouse_position - last_mouse_position;
mouse_position = sf::Mouse::getPosition();
return mouse_position - last_position;
} }
int Input::getDeltaVerticalScroll() float Input::getDeltaVerticalScroll() const
{ {
return delta_vertical_scroll; return delta_vertical_scroll;
} }
/* ------ */
void Input::test() void Input::test()
{ {
std::cerr << keysmap.getTypeAction(1) << std::endl;
std::cerr << keysmap.getKeyBinding(1,0) << std::endl;
KeyBindings kb = keybindings[current_context]; KeyBindings kb = keybindings[current_context];
int action; int action;
action = kb.getPressedAction(sf::Keyboard::I); action = kb.getPressedAction(sf::Keyboard::I);

37
input.h
View File

@ -8,36 +8,36 @@
#include <queue> #include <queue>
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#include "textbuffer.h"
class Input{ class Input{
public: public:
/* Constructors */ /* Constructors */
Input(sf::Window* w, std::string keysmappings, int nb_actions); Input(sf::Window *w);
// enum SpecialAction {}
/* general action-mapping functions */ /* general action-mapping functions */
void setKeysMap(IKeysMap km);
void updateEvents(); void updateEvents();
int getAction(); 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 */ /* context-related functions */
void createContext(std::string context_name, std::vector<int> action_list); void addContext(Context context);
void setCurrentContext(std::string context_name); void setCurrentContext(std::string context_name);
void reloadKeyBindings(); void updateKeyBindings();
/* window-related function */ /* window-related function */
bool isCloseRequested(); bool isCloseRequested() const;
/* keyboard-related functions */ /* keyboard-related functions */
bool isKeyPressed(int key); bool isKeyPressed(int key) const;
/* mouse-related function */ /* mouse-related function */
sf::Vector2i getPosition(); sf::Vector2i getPosition() const;
sf::Vector2i getDeltaPosition(); sf::Vector2i getDeltaPosition() const;
int getDeltaVerticalScroll(); float getDeltaVerticalScroll() const;
/* text-related function */
std::string getText();
void test(); void test();
@ -47,13 +47,14 @@ class Input{
bool closeRequested; bool closeRequested;
/* general action-mapping variables */ /* general action-mapping variables */
KeysMap keysmap; IKeysMap keysmap;
std::queue<int> action_file; std::queue<int> action_file;
int nb_actions; int nb_actions;
/* context-related variables */ /* context-related variables */
std::string current_context; std::string 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::unordered_map<std::string,KeyBindings> keybindings; std::unordered_map<std::string,KeyBindings> keybindings;
/* keyboard-related variables */ /* keyboard-related variables */
@ -62,7 +63,11 @@ class Input{
/* mouse-related variables */ /* mouse-related variables */
sf::Vector2i mouse_position; 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 #endif //INPUT_H

View File

@ -5,59 +5,23 @@
#include "keybindings.h" #include "keybindings.h"
#include <iostream> #include <iostream>
/* Implementation of KeysMap class /* Implementation of IKeysMap class
* @author: Thomas Brandého * @author: Thomas Brandého
* @info: This class register all the association between key and action. * @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) std::vector<Binding> IKeysMap::getBindings(int action) const {
{ std::vector<Binding> bindings;
keys_map = (int*) malloc(size_keys_map*sizeof(int)); for (auto binding : keys)
for(int i = 0; i < size_keys_map; i++) if (binding.action == action) bindings.push_back(binding);
keys_map[i] = NO_KEY; return bindings;
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();
} }
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 /* Implementation of KeyBindings class
* @author: Thomas Brandého * @author: Thomas Brandého
* @info: This class map a list of action with the associated keys, for a quick access in-game. * @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()
{ {
} }
KeyBindings::KeyBindings(const Context &context, const IKeysMap &keysmap)
KeyBindings::KeyBindings(std::vector<int> action_list, KeysMap* keysmap)
{ {
int key; std::vector<int> actions = context.getActions();
for (int action : action_list){ for (int action : actions){
switch(keysmap->getTypeAction(action)){ for (Binding binding : keysmap.getBindings(action)){
case KeysMap::PRESSED: switch(binding.type){
for(int i=0; i<KeysMap::NB_BINDINGS; i++){ case IKeysMap::PRESSED:
key = keysmap->getKeyBinding(action,i); setPressedAction(binding.key,binding.action);
if (key != NO_KEY)
setPressedAction(key,action);
}
break; break;
case KeysMap::RELEASED: case IKeysMap::RELEASED:
for(int i=0; i<KeysMap::NB_BINDINGS; i++){ setReleasedAction(binding.key,binding.action);
key = keysmap->getKeyBinding(action,i);
if (key != NO_KEY)
setReleasedAction(key,action);
}
break; break;
case KeysMap::HOLD: case IKeysMap::HOLD:
for(int i=0; i<KeysMap::NB_BINDINGS; i++){ setHoldAction(binding.key,binding.action);
key = keysmap->getKeyBinding(action,i);
if (key != NO_KEY)
setHoldAction(key,action);
}
break; break;
default: default:
//raiseError; //raiseError;
break; 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) const
int KeyBindings::getReleasedAction(int key)
{ {
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) const
int KeyBindings::getHoldAction(int key)
{ {
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) void KeyBindings::setPressedAction(int key, int action)
{ {
bindings_pressed[key]=action; bindings_pressed[key]=action;
} }
void KeyBindings::setReleasedAction(int key, int action) void KeyBindings::setReleasedAction(int key, int action)
{ {
bindings_released[key]=action; bindings_released[key]=action;
@ -130,3 +80,28 @@ void KeyBindings::setHoldAction(int key, int action)
{ {
bindings_hold[key]=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<int> _actions): name(_name), actions(_actions)
{
}
std::string Context::getName()
{
return name;
}
std::vector<int> Context::getActions() const
{
return actions;
}

View File

@ -12,38 +12,43 @@
#define NO_KEY -1 #define NO_KEY -1
#define NO_ACTION -1 #define NO_ACTION -1
class KeysMap { typedef struct{
public: int action;
enum {PRIMARY,SECONDARY,NB_BINDINGS}; int key;
enum {PRESSED, RELEASED, HOLD}; int type;
KeysMap(); } Binding;
KeysMap(std::string keysmapping, int nb_actions);
int getKeyBinding(int action,int num); class IKeysMap{
void setKeyBinding(int action, int num, int key); public:
int getTypeAction(int action); enum {PRESSED, RELEASED, HOLD};
void setTypeAction(int action, int type); IKeysMap();
void saveKeysMap(); std::vector<Binding> getBindings(int action) const;
void loadKeysMap(); protected:
std::vector<Binding> keys;
};
class Context {
public:
Context(std::string name, std::vector<int> actions);
std::string getName();
std::vector<int> getActions() const;
private: private:
std::string keysmapping_file; std::string name;
int size_keys_map = 0; std::vector<int> actions;
int* keys_map;
int* actiontype_map;
}; };
class KeyBindings { class KeyBindings {
public: public:
KeyBindings(); KeyBindings();
KeyBindings(const std::vector<int> action_list, KeysMap* keysmap); KeyBindings(const Context &context, const IKeysMap &keysmap);
int getPressedAction(int key); int getPressedAction(int key) const;
int getReleasedAction(int key); int getReleasedAction(int key) const;
int getHoldAction(int key); int getHoldAction(int key) const;
std::unordered_map<int,int> bindings_hold;
private: private:
std::unordered_map<int,int> bindings_pressed; std::unordered_map<int,int> bindings_pressed;
std::unordered_map<int,int> bindings_released; std::unordered_map<int,int> bindings_released;
std::unordered_map<int,int> bindings_hold;
void setPressedAction(int key, int action); void setPressedAction(int key, int action);
void setReleasedAction(int key, int action); void setReleasedAction(int key, int action);
void setHoldAction(int key, int action); void setHoldAction(int key, int action);

View File

@ -8,47 +8,55 @@ int main()
{ {
cerr << "Hello World!"<< endl; cerr << "Hello World!"<< endl;
sf::Window* window= new sf::Window(sf::VideoMode(400,200),"testSparrowInput"); 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}; enum {ACTION_1,ACTION_2};
std::vector<int> myvector; std::vector<int> myvector;
myvector.push_back(ACTION_1);
myvector.push_back(ACTION_2); 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_2,0,sf::Keyboard::Z);
keymap->setKeyBinding(ACTION_1,1,sf::Keyboard::Z); // keymap->setTypeAction(ACTION_2,KeysMap::RELEASED);
keymap->setTypeAction(ACTION_1,KeysMap::HOLD);
// cerr << keymap->getKeyBinding(ACTION_1,0) << endl; // cerr << keymap->getKeyBinding(ACTION_1,0) << endl;
cerr << keymap->getTypeAction(ACTION_1) << endl; // cerr << keymap->getTypeAction(ACTION_1) << endl;
*/
//KeyBindings keybind(myvector,keymap);
//cerr << " Action : " << keybind.getPressedAction(sf::Keyboard::A) << endl;
myInput.setkeyBinding(ACTION_1,1,sf::Keyboard::A); // KeyBindings keybind(myvector,keymap);
myInput.setkeyBinding(ACTION_1,2,sf::Keyboard::Z); // cerr << " Action : " << keybind.getReleasedAction(sf::Keyboard::Z) << endl;
myInput.setkeyBinding(ACTION_2,1,sf::Keyboard::I); //
myInput.setkeyBinding(ACTION_2,2,sf::Keyboard::O); // 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.createContext("test", myvector);
myInput.setCurrentContext("test"); // myInput.setCurrentContext("test");
// myInput.reloadKeyBindings(); // myInput.reloadKeyBindings();
bool run = true; // int action;
while(run){ // bool run = true;
//myInput.updateEvents(); // while(run){
if (myInput.isCloseRequested()){ // myInput.updateEvents();
window->close(); // if (myInput.isCloseRequested()){
run = false; // window->close();
} // run = false;
while (myInput.getAction() != NO_ACTION) // }
cout << "test " << myInput.getAction() << endl; // while (action = myInput.getAction() != NO_ACTION)
/* if (myInput.getAction() == ACTION_2) // cout << "test " << action << endl;
cout << "hello" << endl;*/ // window->display();
window->display(); // }
}
return 0; return 0;
} }
/*class MyInput : public Input
{
MyInput(sf::Window *w) : Input(w){
}
};
*/

View File

@ -2,6 +2,43 @@
TextBuffer::TextBuffer() 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();
}

View File

@ -7,8 +7,23 @@ class TextBuffer
public: public:
TextBuffer(); 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: private:
std::string buffer; std::string buffer;
int cursor;
}; };