From 85671408eed36060f5ff4799f2ee153d66e16282 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 3 Jan 2015 16:54:18 +0000 Subject: Minor cleanup of client::Action and client::Keyboard code, added Action::FreeLook defintions. --- src/client/keyboard.cc | 150 ++++++++++++++++++++++++++++++------------------- 1 file changed, 91 insertions(+), 59 deletions(-) (limited to 'src/client/keyboard.cc') diff --git a/src/client/keyboard.cc b/src/client/keyboard.cc index c762a29..70bca9e 100644 --- a/src/client/keyboard.cc +++ b/src/client/keyboard.cc @@ -25,9 +25,6 @@ http://docs.mandragor.org/files/Common_libs_documentation/SDL/SDL_Documentation_ Keyboard::Keyboard() { - numlock = false; - capslock = false; - // ------------------ ACTIONS // FIXME actions should be state keys and not use key repeat @@ -63,6 +60,8 @@ Keyboard::Keyboard() add_action("+control", Action::Control, "enable mouse control while pressed"); add_action("+fire", Action::Fire, "fire weapons"); + + add_action("+freelook", Action::FreeLook, "free look"); // ------------------ KEYS Key *key = 0; @@ -254,17 +253,21 @@ Keyboard::Keyboard() Keyboard::~Keyboard() { // clear key map - for (iterator it = begin(); it != end(); it++) + for (Keys::iterator it = _keys.begin(); it != _keys.end(); ++it) + { delete(*it).second; - keys.clear(); + } + _keys.clear(); // clear action list - for (std::list::iterator ait = actions.begin(); ait != actions.end(); ait++) + for (Actions::iterator ait = _actions.begin(); ait != _actions.end(); ++ait) + { delete(*ait); - actions.clear(); + } + _actions.clear(); } -void Keyboard::save_binds() +void Keyboard::save_binds() const { std::string filename(filesystem::writedir()); filename.append("binds.cfg"); @@ -281,8 +284,8 @@ void Keyboard::save_binds() ofs << "# this file is automaticly generated" << std::endl; ofs << std::endl; - iterator it; - for (it = begin(); it != end(); it++) { + for (Keys::const_iterator it = _keys.begin(); it != _keys.end(); ++it) + { Key *key = (*it).second; if (key->bind(Key::None).size()) { ofs << "bind " << key->name() << " \"" << key->bind(Key::None) << '\"' << std::endl; @@ -305,7 +308,7 @@ void Keyboard::save_binds() ofs.close(); } -void Keyboard::load_binds() +void Keyboard::load_binds() const { std::string filename(filesystem::writedir()); filename.append("binds.cfg"); @@ -326,6 +329,19 @@ void Keyboard::load_binds() } } +void Keyboard::reset() +{ + for (Keys::iterator it = _keys.begin(); it != _keys.end(); ++it) + { + Key *key = (*it).second; + if (key) { + key->key_pressed = 0; + key->key_lastpressed = 0; + key->key_waspressed = 0; + } + } +} + Key * Keyboard::release(unsigned int sym) { Key *key = find(sym); @@ -363,8 +379,10 @@ Key * Keyboard::press(Key *key) Key *Keyboard::find(std::string const & name) { Key *key = 0; - for (iterator it = begin(); it != end() && !key; it++) { - if ((*it).second->name().compare(name) == 0) { + for (Keys::iterator it = _keys.begin(); it != _keys.end(); ++it) + { + if ((*it).second->name().compare(name) == 0) + { key = (*it).second; } } @@ -373,11 +391,15 @@ Key *Keyboard::find(std::string const & name) Key *Keyboard::find(unsigned int keysym) { - iterator it = keys.find(keysym); - if (it == end()) + Keys::iterator it = _keys.find(keysym); + if (it == _keys.end()) + { return 0; + } else + { return (*it).second; + } } void Keyboard::bind(std::string const &name, const std::string str) @@ -411,7 +433,7 @@ void Keyboard::bind(std::string const &name, const std::string str) // assign new bind of requested if (str.size()) { Action *action = 0; - for (std::list::iterator it = actions.begin(); it != actions.end(); it++) { + for (Actions::iterator it = _actions.begin(); it != _actions.end(); ++it) { if ((*it)->name().compare(str) == 0) { action = (*it); } @@ -482,7 +504,8 @@ void Keyboard::unbind(std::string const &name) void Keyboard::unbindall() { - for (iterator it = begin(); it != end(); it++) { + for (Keys::iterator it = _keys.begin(); it != _keys.end(); ++it) + { Key *key = (*it).second; key->clear(); } @@ -490,56 +513,64 @@ void Keyboard::unbindall() Key * Keyboard::add_key(const char *name, const unsigned int keysym, const char ascii, const char *bind) { - Key *newkey = new Key(name, keysym, ascii); - keys[keysym] = newkey; - if (bind) { + Key *key = new Key(name, keysym, ascii); + _keys[keysym] = key; + if (bind) + { std::string bindstr(bind); - this->bind(newkey->name(), bindstr); + this->bind(key->name(), bindstr); } - return newkey; + return key; } -Action * Keyboard::add_action(const char *name, Action::Identifier action, const char *info) +Action * Keyboard::add_action(const char *name, Action::Identifier id, const char *description) { - Action *newaction = new Action(name, action, info); - actions.push_back(newaction); - return newaction; + Action *action = new Action(name, id, description); + _actions.push_back(action); + return action; } -void Keyboard::list_actions() +void Keyboard::list_actions() const { - for (std::list::iterator it = actions.begin(); it != actions.end(); it++) { - con_print << " " << (*it)->name() << " " << (*it)->info() << std::endl; + for (Actions::const_iterator it = _actions.begin(); it != _actions.end(); ++it) + { + con_print << " " << (*it)->name() << " " << (*it)->description() << std::endl; } - con_print << actions.size() << " registered actions" << std::endl; + con_print << _actions.size() << " registered actions" << std::endl; } -void Keyboard::list_keys() +void Keyboard::list_keys() const { - for (iterator it = begin(); it != end(); it++) { + for (Keys::const_iterator it = _keys.begin(); it != _keys.end(); ++it) + { con_print << " " << aux::pad_left((*it).second->name(), 6) << " " << (*it).second->bind(Key::None) << std::endl; } - con_print << keys.size() << " registered keys" << std::endl; + con_print << _keys.size() << " registered keys" << std::endl; } -void Keyboard::list_binds() +void Keyboard::list_binds() const { size_t n = 0; - for (iterator it = begin(); it != end(); it++) { - if ((*it).second->bind(Key::None).size()) { + for (Keys::const_iterator it = _keys.begin(); it != _keys.end(); ++it) + { + if ((*it).second->bind(Key::None).size()) + { con_print << " " << aux::pad_right((*it).second->name(), 6) << " " << (*it).second->bind(Key::None) << std::endl; n++; } - if ((*it).second->bind(Key::Shift).size()) { + if ((*it).second->bind(Key::Shift).size()) + { con_print << " shift+" << aux::pad_right((*it).second->name(), 6) << " " << (*it).second->bind(Key::Shift) << std::endl; n++; } - if ((*it).second->bind(Key::Ctrl).size()) { + if ((*it).second->bind(Key::Ctrl).size()) + { con_print << " ctrl+" << aux::pad_right((*it).second->name(), 6) << " " << (*it).second->bind(Key::Ctrl) << std::endl; n++; } - if ((*it).second->bind(Key::Alt).size()) { + if ((*it).second->bind(Key::Alt).size()) + { con_print << " alt+" << aux::pad_right((*it).second->name(), 6) << " " << (*it).second->bind(Key::Alt) << std::endl; n++; } @@ -548,13 +579,15 @@ void Keyboard::list_binds() con_print << n << " registered binds" << std::endl; } -unsigned int Keyboard::translate_keysym(int keysym, int modifier) +unsigned int Keyboard::translate_keysym(const int keysym, const int modifiers) { bool shift = false; // keypad keys - if (modifier & KMOD_NUM) { - switch (keysym) { + if (modifiers & KMOD_NUM) + { + switch (keysym) + { case SDLK_KP0: return '0'; break; @@ -589,8 +622,11 @@ unsigned int Keyboard::translate_keysym(int keysym, int modifier) return '.'; break; } - } else { - switch (keysym) { + } + else + { + switch (keysym) + { case SDLK_KP0: return SDLK_INSERT; break; @@ -625,7 +661,8 @@ unsigned int Keyboard::translate_keysym(int keysym, int modifier) } // special keys - switch (keysym) { + switch (keysym) + { case SDLK_ESCAPE: return SDLK_ESCAPE; break; @@ -650,16 +687,21 @@ unsigned int Keyboard::translate_keysym(int keysym, int modifier) } // caps lock - if (modifier & KMOD_CAPS) + if (modifiers & KMOD_CAPS) + { shift = true; + } // left/right shift - if ((KMOD_LSHIFT + KMOD_RSHIFT) & modifier) { + if ((KMOD_LSHIFT + KMOD_RSHIFT) & modifiers) + { shift = !shift; } - if (shift) { - if ((keysym >= 'a' && keysym <= 'z')) { + if (shift) + { + if ((keysym >= 'a' && keysym <= 'z')) + { return keysym + 'A' - 'a'; } @@ -736,14 +778,4 @@ unsigned int Keyboard::translate_keysym(int keysym, int modifier) return keysym; } -/* -void setkeyboardmode(bool input) -{ - if(input) - SDL_EnableKeyRepeat(250, SDL_DEFAULT_REPEAT_INTERVAL); - else - SDL_EnableKeyRepeat(10, SDL_DEFAULT_REPEAT_INTERVAL); - -} -*/ } // namespace client -- cgit v1.2.3