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/action.cc | 23 +++++--- src/client/action.h | 80 ++++++++++++++++---------- src/client/input.cc | 18 +++--- src/client/keyboard.cc | 150 ++++++++++++++++++++++++++++++------------------- src/client/keyboard.h | 50 ++++++++--------- 5 files changed, 187 insertions(+), 134 deletions(-) (limited to 'src') diff --git a/src/client/action.cc b/src/client/action.cc index 7c7a34c..2f54dc3 100644 --- a/src/client/action.cc +++ b/src/client/action.cc @@ -9,23 +9,28 @@ namespace client { -Action::Action(const char *name, Identifier action, const char *info) +Action::Action(const char *name, Identifier action, const char *description) { - action_name.assign(name); - action_id = action; + _name.assign(name); + _id = action; - set_info(info); + set_description(description); } Action::~Action() { } -void Action::set_info(const char *info) +void Action::set_description(const char *description) { - if (info) - action_info.assign(info); + if (description) + { + _description.assign(description); + } else - action_info.clear(); -} + { + _description.clear(); + } } + +} // namespace client diff --git a/src/client/action.h b/src/client/action.h index 00ef210..0a7e247 100644 --- a/src/client/action.h +++ b/src/client/action.h @@ -20,48 +20,70 @@ namespace client class Action { public: - /// actions - enum Identifier {None = 0, - Console, - Left, Right, Up, Down, - RollLeft, RollRight, - StrafeUp, StrafeDown, - StrafeLeft, StrafeRight, - ThrustUp, ThrustDown, - Afterburner, Reverse, - Control, - CamLeft, CamRight, CamUp, CamDown, - ZoomIn, ZoomOut, - Fire + /** + * @brief type definition for the action type identifier + * */ + enum Identifier + { + None = 0, + Console, + Left, Right, Up, Down, + RollLeft, RollRight, + StrafeUp, StrafeDown, + StrafeLeft, StrafeRight, + ThrustUp, ThrustDown, + Afterburner, Reverse, + Control, + CamLeft, CamRight, CamUp, CamDown, + ZoomIn, ZoomOut, + Fire, + FreeLook }; - /// define a new action - Action(const char *name, Identifier action, const char *info = 0); + /** + * @brief define a new action + * */ + Action(const char *name, Identifier action, const char *description = 0); + + /** + * @brief default destructor + * */ ~Action(); - /// name - inline std::string const & name() const { - return action_name; + /** + * @brief action name + * */ + inline std::string const & name() const + { + return _name; } - /// identifier - inline Identifier id() const { - return action_id; + /** + * @brief action type identifier + * */ + inline Identifier id() const + { + return _id; } - /// text description - inline std::string const & info() const { - return action_info; + /** + * @brief text description + * */ + inline std::string const & description() const + { + return _description; } - /// set info - void set_info(const char *info); + /** + * @brief set the action's description + * */ + void set_description(const char *description); private: - std::string action_name; - std::string action_info; - Identifier action_id; + std::string _name; + std::string _description; + Identifier _id; }; } // namespace client diff --git a/src/client/input.cc b/src/client/input.cc index fed76c9..ab0c847 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -434,6 +434,9 @@ void action_press(Key *key) case Action::ZoomOut: render::Camera::set_zoom(+0.1f); break; + case Action::FreeLook: + // TODO freelook + break; /* -- fire control -------------------------------- */ @@ -537,6 +540,10 @@ void action_release(Key *key) break; case Action::ZoomOut: break; + case Action::FreeLook: + // TODO freelook + break; + /* -- fire control -------------------------------- */ @@ -716,14 +723,7 @@ void reset() mouse_control_override_time = 0; targets::reset(); - for (Keyboard::iterator it = keyboard->begin(); it != keyboard->end(); it++) { - Key *key = (*it).second; - if (key) { - key->key_pressed = 0; - key->key_lastpressed = 0; - key->key_waspressed = 0; - } - } + keyboard->reset(); last_key = 0; mouse_lastmoved = 0; @@ -852,7 +852,7 @@ void frame() repeat /= 1000.0f; delay /= 1000.0f; - for (Keyboard::iterator it = keyboard->begin(); it != keyboard->end(); it++) { + for (Keyboard::Keys::iterator it = keyboard->keys().begin(); it != keyboard->keys().end(); it++) { key = (*it).second; if (key && (key->sym() < 512) && key->pressed()) { if ((key->pressed() + delay < core::application()->time()) && (key->lastpressed() + repeat < core::application()->time())) { 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 diff --git a/src/client/keyboard.h b/src/client/keyboard.h index f533838..0742b9c 100644 --- a/src/client/keyboard.h +++ b/src/client/keyboard.h @@ -21,7 +21,11 @@ namespace client class Keyboard { -public: +public: + typedef std::map Keys; + + typedef std::list Actions; + Keyboard(); ~Keyboard(); @@ -39,21 +43,24 @@ public: /// celar all key binds void unbindall(); + + /// reset key states + void reset(); /// list keyboard key names - void list_keys(); + void list_keys() const; /// list actions - void list_actions(); + void list_actions() const; /// list keyboard binds - void list_binds(); + void list_binds() const; /// load keyboard binds - void load_binds(); + void load_binds() const; /// save keyboard binds - void save_binds(); + void save_binds() const; /// a key has been pressed Key *press(unsigned int sym); @@ -64,36 +71,23 @@ public: /// a key has been pressed Key *release(unsigned int sym); - typedef std::map::iterator iterator; - - inline iterator begin() { - return keys.begin(); - } - - inline iterator end() { - return keys.end(); - } - /// convert SDL_keysym to a keystroke - static unsigned int translate_keysym(int keysym, int modifier); + static unsigned int translate_keysym(const int keysym, const int modifiers); + + inline Keys & keys() + { + return _keys; + } private: Key *add_key(const char *name, const unsigned int keysym, const char ascii = 0, const char *bind = 0); - Action *add_action(const char *name, Action::Identifier action, const char *info = 0); + Action *add_action(const char *name, Action::Identifier id, const char *description = 0); - std::map keys; - std::list actions; - - bool numlock; - bool capslock; + Keys _keys; + Actions _actions; }; -/// set the keyboard input mode -/** @param input true for console input, false for game input - */ -//void setkeyboardmode(bool input); - } #endif // __INCLUDED_CLIENT_KEYBOARD_H__ -- cgit v1.2.3