From 6817dec0b405b325a2762e352ad2a5916c24542a Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 31 Jul 2016 14:14:25 +0200 Subject: Added reset_controls function to revert to the default keyboard configuration, don't override unbound keys with default binds. --- src/client/control.cc | 15 +++++++++++++-- src/client/control.h | 29 +++++++++++++++++++++++++---- src/client/input.cc | 16 +++++++++++++--- src/client/key.cc | 3 ++- src/client/keyboard.cc | 22 ++++++++++++++++++++-- src/client/keyboard.h | 5 ++++- 6 files changed, 77 insertions(+), 13 deletions(-) diff --git a/src/client/control.cc b/src/client/control.cc index ab8c99f..0393333 100644 --- a/src/client/control.cc +++ b/src/client/control.cc @@ -5,15 +5,26 @@ */ #include "client/control.h" +#include namespace client { Control::Control(const char *name, const char *command, const char *keyname) { + assert(name); _name.assign(name); - _command.assign(command); - _keyname.assign(keyname); + + if (command) + { + _command.assign(command); + } + + if (keyname) + { + _keyname.assign(keyname); + _defaultkeyname.assign(keyname); + } } Control::~Control() diff --git a/src/client/control.h b/src/client/control.h index bf30418..5f72aa1 100644 --- a/src/client/control.h +++ b/src/client/control.h @@ -22,21 +22,41 @@ public: Control(const char *name, const char *command, const char * keyname = 0); ~Control(); + /** + * @brief name of this control + * */ inline const std::string &name() const { return _name; } - - inline const std::string & command() const + + /** + * @brief the command executed by this control + */ + inline const std::string &command() const { return _command; } - - inline const std::string & keyname() const + + /** + * @brief the currently assigned to this control. + * */ + inline const std::string &keyname() const { return _keyname; } + /** + * @brief the default key assigned to this control. + * */ + inline const std::string &defaultkeyname() const + { + return _defaultkeyname; + } + + /** + * @brief assign a key to this control + * */ inline void set_keyname(const std::string &keyname) { _keyname.assign(keyname); @@ -46,6 +66,7 @@ private: std::string _name; std::string _command; std::string _keyname; + std::string _defaultkeyname; }; } // namespace client diff --git a/src/client/input.cc b/src/client/input.cc index 7edc1c2..8b40216 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -254,6 +254,15 @@ void func_unbind(std::string const &args) } } +void func_reset_controls(std::string const &args) +{ + if (keyboard) + { + keyboard->unbindall(); + keyboard->load_defaults(); + } +} + //--- input functions --------------------------------------------- void init() @@ -302,8 +311,8 @@ void init() func = core::Func::add("unbind", func_unbind); func->set_info("[key] unbind a key"); - func = core::Func::add("unbindall", func_unbind); - func->set_info("unbind all keys"); + func = core::Func::add("reset_controls", func_reset_controls); + func->set_info("load default controls"); func = core::Func::add("view_next", func_view_next); func->set_info("switch to next view"); @@ -329,7 +338,8 @@ void shutdown() core::Func::remove("bind"); core::Func::remove("unbind"); - core::Func::remove("unbindall"); + + core::Func::remove("reset_controls"); core::Func::remove("screenshot"); diff --git a/src/client/key.cc b/src/client/key.cc index 36c9787..e2e56e5 100644 --- a/src/client/key.cc +++ b/src/client/key.cc @@ -6,7 +6,7 @@ #include "auxiliary/functions.h" #include "client/key.h" - +#include namespace client { @@ -18,6 +18,7 @@ Key::Key(const unsigned int scancode, const char *name, const char ascii) key_lastpressed = 0; key_waspressed = 0; + assert(name); key_name.assign(name); clear(); diff --git a/src/client/keyboard.cc b/src/client/keyboard.cc index b05aaa8..fe6496f 100644 --- a/src/client/keyboard.cc +++ b/src/client/keyboard.cc @@ -253,8 +253,8 @@ Keyboard::Keyboard() add_control("Roll left", "+rollleft", "q"); add_control("Roll right", "+rollright", "e"); - add_control("Decrease thruster", "+thrustdown", "kpmin"); - add_control("Increase thruster", "+thrustup", "kpplus"); + add_control("Increase thruster", "+thrustup", "mwheelup"); + add_control("Decrease thruster", "+thrustdown", "mwheeldown"); add_control("Aferburner", "+afterburner", "w"); add_control("Reverse engine", "+reverse", "s"); @@ -382,6 +382,9 @@ void Keyboard::load_binds() } con_print << " reading keyboard binds from " << filename << std::endl; + + // clear default configuration + unbindall(); char line[MAXCMDSIZE]; while (ifs.getline(line, MAXCMDSIZE - 1)) { @@ -393,6 +396,21 @@ void Keyboard::load_binds() load_controls(); } + +// load keybinds from control definitions +void Keyboard::load_defaults() +{ + for (Controls::iterator cit = _controls.begin(); cit != _controls.end(); ++cit) + { + Control *control = *cit; + + std::stringstream str; + str << "bind " << control->defaultkeyname() << " " << control->command(); + core::CommandBuffer::exec(str.str().c_str()); + } +} + +// load control keys from binds void Keyboard::load_controls() { for (Controls::iterator cit = _controls.begin(); cit != _controls.end(); ++cit) diff --git a/src/client/keyboard.h b/src/client/keyboard.h index a665c55..6e0a600 100644 --- a/src/client/keyboard.h +++ b/src/client/keyboard.h @@ -65,8 +65,11 @@ public: /// save keyboard binds void save_binds() const; - /// load controls + /// load controls from key binds void load_controls(); + + /// load default controls + void load_defaults(); /// a key has been pressed Key *press(const unsigned int scancode); -- cgit v1.2.3