Permalink
Browse files

Fix how the hotkey system handles default bindings.

Only hotkeys which are changed from the defaults are saved in the users
config file.

Isolated a method for internal use of read write hotkey_item's and one
that offers read only access to external functions.

Removed a obsolete method from the preferences to save single hotkey
bindings.
  • Loading branch information...
fendrin
fendrin committed Dec 10, 2013
1 parent c3fb5de commit 5463068caffb42df8eeb9a044479ef7fc379e622
@@ -275,7 +275,7 @@ bool is_scope_active(scope s)
}
-hotkey_command& get_hotkey_command(const std::string& command)
+const hotkey_command& get_hotkey_command(const std::string& command)
{
if (command_map_.find(command) == command_map_.end())
{
@@ -294,7 +294,7 @@ const boost::ptr_vector<hotkey_command>& get_hotkey_commands()
// Returns whether a hotkey was deleted.
bool remove_wml_hotkey(const std::string& id)
{
- hotkey::hotkey_command& command = get_hotkey_command(id);
+ const hotkey::hotkey_command& command = get_hotkey_command(id);
if(command.id == hotkey::HOTKEY_NULL)
{
LOG_G << "remove_wml_hotkey: command with id=" + id + " doesn't exist\n";
@@ -352,13 +352,12 @@ void add_wml_hotkey(const std::string& id, const t_string& description, const co
if(!default_hotkey.empty() && !has_hotkey_item(id))
{
- hotkey_item new_item(default_hotkey);
+ hotkey_item new_item(default_hotkey, true);
new_item.set_command(id);
if(new_item.valid())
{
DBG_G << "added default description for the wml hotkey with id=" + id;
add_hotkey(new_item);
- preferences::save_hotkey(new_item);
}
else
{
@@ -382,7 +381,7 @@ hotkey_command::hotkey_command(hotkey::HOTKEY_COMMAND cmd, const std::string& id
{
}
-hotkey_command& hotkey_command::null_command()
+const hotkey_command& hotkey_command::null_command()
{
return get_hotkey_null();
}
@@ -391,7 +390,7 @@ bool hotkey_command::null() const
{
if(id == HOTKEY_NULL || command == "null")
{
- hotkey_command& null_cmd = null_command();
+ const hotkey_command& null_cmd = null_command();
if(id == null_cmd.id && command == null_cmd.command && scope == null_cmd.scope && description == null_cmd.description)
return true;
else
@@ -404,7 +403,7 @@ bool hotkey_command::null() const
}
-hotkey_command& hotkey_command::get_command_by_command(hotkey::HOTKEY_COMMAND command)
+const hotkey_command& hotkey_command::get_command_by_command(hotkey::HOTKEY_COMMAND command)
{
BOOST_FOREACH(hotkey_command& cmd, known_hotkeys)
{
@@ -416,7 +415,7 @@ hotkey_command& hotkey_command::get_command_by_command(hotkey::HOTKEY_COMMAND co
}
-hotkey_command& get_hotkey_null()
+const hotkey_command& get_hotkey_null()
{
//it is the last entry in that array, and the indexes in hotkey_list_ and known_hotkeys are the same.
return known_hotkeys[sizeof(hotkey_list_) / sizeof(hotkey_list_[0]) - 1];
@@ -434,6 +433,7 @@ void delete_all_wml_hotkeys()
known_hotkeys.erase(last_element);
}
}
+
const std::string get_description(const std::string& command)
{
return get_hotkey_command(command).description;
@@ -167,10 +167,10 @@ struct hotkey_command {
/// checks weather this is the null hotkey_command
bool null() const;
/// returns the command that is treated as null
- static hotkey_command& null_command();
+ static const hotkey_command& null_command();
/// the execute_command argument was changed from HOTKEY_COMMAND to hotkey_command,
/// to be able to call it with HOTKEY_COMMAND, this function was created
- static hotkey_command& get_command_by_command(HOTKEY_COMMAND command);
+ static const hotkey_command& get_command_by_command(HOTKEY_COMMAND command);
};
/// Do not use this outside hotkeys.cpp.
@@ -202,9 +202,10 @@ class scope_changer {
const boost::ptr_vector<hotkey_command>& get_hotkey_commands();
/// returns the hotkey_command with the given name
-hotkey_command& get_hotkey_command(const std::string& command);
+const hotkey_command& get_hotkey_command(const std::string& command);
+
/// returns the hotkey_command that is treated as null.
-hotkey_command& get_hotkey_null();
+const hotkey_command& get_hotkey_null();
void deactivate_all_scopes();
void set_scope_active(scope s, bool set = true);
View
@@ -35,66 +35,41 @@ static lg::log_domain log_config("config");
namespace {
-
std::vector<hotkey::hotkey_item> hotkeys_;
config default_hotkey_cfg_;
hotkey::hotkey_item null_hotkey_("null");
-}
-
-namespace hotkey {
-
-
-const hotkey_item& get_hotkey(const SDL_JoyButtonEvent& event)
-{
- CKey keystate;
- bool shift = keystate[SDLK_RSHIFT] || keystate[SDLK_LSHIFT];
- bool ctrl = keystate[SDLK_RCTRL] || keystate[SDLK_LCTRL];
- bool cmd = keystate[SDLK_RMETA] || keystate[SDLK_LMETA];
- bool alt = keystate[SDLK_RALT] || keystate[SDLK_LALT];
-
- return get_hotkey(-1, event.which, event.button, -1, -1, shift, ctrl, cmd, alt);
-}
-
-const hotkey_item& get_hotkey(const SDL_JoyHatEvent& event)
+hotkey::hotkey_item& get_hotkey(int mouse, int joystick, int button, int hat, int value,
+ bool shift, bool ctrl, bool cmd, bool alt)
{
- CKey keystate;
- bool shift = keystate[SDLK_RSHIFT] || keystate[SDLK_LSHIFT];
- bool ctrl = keystate[SDLK_RCTRL] || keystate[SDLK_LCTRL];
- bool cmd = keystate[SDLK_RMETA] || keystate[SDLK_LMETA];
- bool alt = keystate[SDLK_RALT] || keystate[SDLK_LALT];
-
- return get_hotkey(-1, event.which, -1, event.hat, event.value, shift, ctrl, cmd, alt);
-}
+ std::vector<hotkey::hotkey_item>::iterator itor;
+ for (itor = hotkeys_.begin(); itor != hotkeys_.end(); ++itor) {
-const hotkey_item& get_hotkey(const SDL_MouseButtonEvent& event)
-{
- CKey keystate;
- bool shift = keystate[SDLK_RSHIFT] || keystate[SDLK_LSHIFT];
- bool ctrl = keystate[SDLK_RCTRL] || keystate[SDLK_LCTRL];
- bool cmd = keystate[SDLK_RMETA] || keystate[SDLK_LMETA];
- bool alt = keystate[SDLK_RALT] || keystate[SDLK_LALT];
+ if ( !( hotkey::is_scope_active(hotkey::get_hotkey_command(itor->get_command()).scope) && itor->active() ) ) {
+ continue;
+ }
- return get_hotkey(event.which, -1, event.button, -1, -1, shift, ctrl, cmd, alt);
-}
+ if ( itor->get_shift() != shift || itor->get_ctrl() != ctrl
+ || itor->get_cmd() != cmd || itor->get_alt() != alt ) {
+ continue;
+ }
+ if ( itor->get_joystick() == joystick && itor->get_button() == button
+ && itor->get_hat() == hat && itor->get_value() == value
+ && itor->get_mouse() == mouse ) {
+ return *itor;
+ }
+ }
-const hotkey_item& get_hotkey(const SDL_KeyboardEvent& event)
-{
- return get_hotkey(event.keysym.unicode, event.keysym.sym,
- (event.keysym.mod & KMOD_SHIFT) != 0,
- (event.keysym.mod & KMOD_CTRL) != 0,
- (event.keysym.mod & KMOD_META) != 0,
- (event.keysym.mod & KMOD_ALT) != 0
- );
+ return null_hotkey_;
}
-hotkey_item& get_hotkey(int character, int keycode,
+hotkey::hotkey_item& get_hotkey(int character, int keycode,
bool shift, bool ctrl, bool cmd, bool alt)
{
- std::vector<hotkey_item>::iterator itor;
+ std::vector<hotkey::hotkey_item>::iterator itor;
DBG_G << "getting hotkey: char=" << lexical_cast<std::string>(character)
<< " keycode=" << lexical_cast<std::string>(keycode) << " "
@@ -127,7 +102,7 @@ hotkey_item& get_hotkey(int character, int keycode,
if (ctrl == itor->get_ctrl()
&& cmd == itor->get_cmd()
&& alt == itor->get_alt()) {
- if (is_scope_active(get_hotkey_command(itor->get_command()).scope) && itor->active()) {
+ if (hotkey::is_scope_active(hotkey::get_hotkey_command(itor->get_command()).scope) && itor->active()) {
DBG_G << "Could match by character..." << "yes\n";
found = true;
break;
@@ -143,7 +118,7 @@ hotkey_item& get_hotkey(int character, int keycode,
&& ctrl == itor->get_ctrl()
&& cmd == itor->get_cmd()
&& alt == itor->get_alt()) {
- if (is_scope_active(get_hotkey_command(itor->get_command()).scope) && itor->active()) {
+ if (hotkey::is_scope_active(hotkey::get_hotkey_command(itor->get_command()).scope) && itor->active()) {
DBG_G << "Could match by keycode..." << "yes\n";
found = true;
break;
@@ -163,6 +138,59 @@ hotkey_item& get_hotkey(int character, int keycode,
return *itor;
}
+
+}
+
+namespace hotkey {
+
+
+const hotkey_item& get_hotkey(const SDL_JoyButtonEvent& event)
+{
+ CKey keystate;
+ bool shift = keystate[SDLK_RSHIFT] || keystate[SDLK_LSHIFT];
+ bool ctrl = keystate[SDLK_RCTRL] || keystate[SDLK_LCTRL];
+ bool cmd = keystate[SDLK_RMETA] || keystate[SDLK_LMETA];
+ bool alt = keystate[SDLK_RALT] || keystate[SDLK_LALT];
+
+ return get_hotkey(-1, event.which, event.button, -1, -1, shift, ctrl, cmd, alt);
+}
+
+const hotkey_item& get_hotkey(const SDL_JoyHatEvent& event)
+{
+ CKey keystate;
+ bool shift = keystate[SDLK_RSHIFT] || keystate[SDLK_LSHIFT];
+ bool ctrl = keystate[SDLK_RCTRL] || keystate[SDLK_LCTRL];
+ bool cmd = keystate[SDLK_RMETA] || keystate[SDLK_LMETA];
+ bool alt = keystate[SDLK_RALT] || keystate[SDLK_LALT];
+
+ return get_hotkey(-1, event.which, -1, event.hat, event.value, shift, ctrl, cmd, alt);
+}
+
+
+const hotkey_item& get_hotkey(const SDL_MouseButtonEvent& event)
+{
+ CKey keystate;
+ bool shift = keystate[SDLK_RSHIFT] || keystate[SDLK_LSHIFT];
+ bool ctrl = keystate[SDLK_RCTRL] || keystate[SDLK_LCTRL];
+ bool cmd = keystate[SDLK_RMETA] || keystate[SDLK_LMETA];
+ bool alt = keystate[SDLK_RALT] || keystate[SDLK_LALT];
+
+ return get_hotkey(event.which, -1, event.button, -1, -1, shift, ctrl, cmd, alt);
+}
+
+
+const hotkey_item& get_hotkey(const SDL_KeyboardEvent& event)
+{
+ return get_hotkey(event.keysym.unicode, event.keysym.sym,
+ (event.keysym.mod & KMOD_SHIFT) != 0,
+ (event.keysym.mod & KMOD_CTRL) != 0,
+ (event.keysym.mod & KMOD_META) != 0,
+ (event.keysym.mod & KMOD_ALT) != 0
+ );
+}
+
+
+
std::string get_names(std::string id) {
std::vector<std::string> names;
@@ -175,30 +203,18 @@ std::string get_names(std::string id) {
return boost::algorithm::join(names, ", ");
}
-hotkey_item& get_hotkey(int mouse, int joystick, int button, int hat, int value,
+const hotkey_item& get_hotkey(int mouse, int joystick, int button, int hat, int value,
bool shift, bool ctrl, bool cmd, bool alt)
{
- std::vector<hotkey_item>::iterator itor;
-
- for (itor = hotkeys_.begin(); itor != hotkeys_.end(); ++itor) {
-
- if ( !( is_scope_active(get_hotkey_command(itor->get_command()).scope) && itor->active() ) ) {
- continue;
- }
-
- if ( itor->get_shift() != shift || itor->get_ctrl() != ctrl
- || itor->get_cmd() != cmd || itor->get_alt() != alt ) {
- continue;
- }
-
- if ( itor->get_joystick() == joystick && itor->get_button() == button
- && itor->get_hat() == hat && itor->get_value() == value
- && itor->get_mouse() == mouse ) {
- return *itor;
- }
- }
+ return ::get_hotkey(mouse, joystick, button, hat, value,
+ shift, ctrl, cmd, alt);
+}
- return null_hotkey_;
+const hotkey::hotkey_item& get_hotkey(int character, int keycode,
+ bool shift, bool ctrl, bool cmd, bool alt)
+{
+ return ::get_hotkey(character, keycode,
+ shift, ctrl, cmd, alt);
}
bool has_hotkey_item(const std::string& command)
@@ -210,8 +226,25 @@ bool has_hotkey_item(const std::string& command)
}
return false;
}
+
void add_hotkey(const hotkey_item& item) {
- hotkeys_.push_back(item);
+
+ scope new_scope = hotkey::get_hotkey_command(item.get_command()).scope;
+ scope_changer scope_ch;
+ deactivate_all_scopes();
+ hotkey::set_scope_active(new_scope);
+ hotkey::set_scope_active(hotkey::SCOPE_GENERAL);
+
+ hotkey_item& old_hk = (item.get_mouse() != -1 || item.get_joystick() != -1) ?
+ ::get_hotkey(item.get_mouse(), item.get_joystick(), item.get_button(), item.get_hat()
+ , item.get_value(), item.get_shift(), item.get_ctrl(), item.get_cmd(), item.get_alt()) :
+ ::get_hotkey(item.get_character(), item.get_keycode(),
+ item.get_shift(), item.get_ctrl(), item.get_cmd(), item.get_alt());
+
+ if (old_hk.active())
+ old_hk.set_command(item.get_command());
+ else
+ hotkeys_.push_back(item);
}
void clear_hotkeys(const std::string& command)
@@ -229,14 +262,11 @@ void clear_hotkeys()
void load_hotkeys(const config& cfg, bool set_as_default)
{
- hotkeys_.clear();
BOOST_FOREACH(const config &hk, cfg.child_range("hotkey")) {
- hotkeys_.push_back(hotkey_item(hk));
+ add_hotkey(hotkey_item(hk, set_as_default));
}
- if (hotkeys_.empty()) {
- reset_default_hotkeys();
- } else if (set_as_default) {
+ if (set_as_default) {
default_hotkey_cfg_ = cfg;
}
}
@@ -246,31 +276,26 @@ void reset_default_hotkeys()
hotkeys_.clear();
if(!default_hotkey_cfg_.empty()) {
- load_hotkeys(default_hotkey_cfg_, false);
+ load_hotkeys(default_hotkey_cfg_, true);
} else {
ERR_G << "no default hotkeys set yet; all hotkeys are now unassigned!\n";
}
}
-std::vector<hotkey_item>& get_hotkeys()
+const std::vector<hotkey_item>& get_hotkeys()
{
return hotkeys_;
}
-void save_hotkey(config& cfg, const hotkey_item & item)
-{
- if ( !item.null() )
- item.save(cfg.add_child("hotkey"));
-}
-
void save_hotkeys(config& cfg)
{
cfg.clear_children("hotkey");
for(std::vector<hotkey_item>::iterator i = hotkeys_.begin();
i != hotkeys_.end(); ++i)
{
- save_hotkey(cfg, *i);
+ if (!i->is_default())
+ i->save(cfg.add_child("hotkey"));
}
}
Oops, something went wrong.

0 comments on commit 5463068

Please sign in to comment.