21 #define GETTEXT_DOMAIN "wesnoth-lib"
27 #include <boost/algorithm/string/join.hpp>
28 #include <boost/algorithm/string.hpp>
29 #include <boost/algorithm/string/predicate.hpp>
38 #define ERR_G LOG_STREAM(err, lg::general())
39 #define LOG_G LOG_STREAM(info, lg::general())
40 #define DBG_G LOG_STREAM(debug, lg::general())
41 #define ERR_CF LOG_STREAM(err, log_config)
51 mods = SDL_GetModState();
58 if (mods & KMOD_SHIFT)
89 if (
mod_ & KMOD_SHIFT)
120 if (scopematch.none()) {
138 if ((mods !=
mod_)) {
150 item[
"shift"] = !!(
mod_ & KMOD_SHIFT);
151 item[
"ctrl"] = !!(
mod_ & KMOD_CTRL);
152 item[
"cmd"] = !!(
mod_ & KMOD_GUI);
153 item[
"alt"] = !!(
mod_ & KMOD_ALT);
162 switch (event.type) {
166 base = boost::dynamic_pointer_cast<
hotkey_base>(keyboard);
168 code =
event.key.keysym.scancode;
169 keyboard->set_scancode(code);
172 case SDL_MOUSEBUTTONDOWN:
173 case SDL_MOUSEBUTTONUP: {
175 base = boost::dynamic_pointer_cast<
hotkey_base>(mouse);
176 mouse->set_button(event.button.button);
180 ERR_G<<
"Trying to bind an unknown event type:" <<
event.type <<
"\n";
185 base->set_command(
id);
186 base->unset_default();
196 if (!mouse_cfg.empty()) {
198 base = boost::dynamic_pointer_cast<
hotkey_base>(mouse);
199 mouse->set_button(cfg[
"button"].to_int());
204 if (!joystick_cfg.empty()) {
205 joystick_ = cfg[
"joystick"].to_int();
209 hat_ = cfg[
"hat"].to_int();
210 value_ = cfg[
"value"].to_int();
214 if (!button.empty()) {
215 button_ = cfg[
"button"].to_int();
220 if (!key_cfg.empty()) {
222 base = boost::dynamic_pointer_cast<
hotkey_base>(keyboard);
224 SDL_Scancode scancode = SDL_GetScancodeFromName(key_cfg.c_str());
225 if (scancode == SDL_SCANCODE_UNKNOWN) {
226 ERR_G<<
"Unknown key: " << key_cfg <<
"\n";
228 keyboard->set_scancode(scancode);
235 unsigned int mods = 0;
237 if (cfg[
"shift"].to_bool())
239 if (cfg[
"ctrl"].to_bool())
241 if (cfg[
"cmd"].to_bool())
243 if (cfg[
"alt"].to_bool())
246 base->set_mods(mods);
247 base->set_command(cfg[
"command"].str());
249 cfg[
"disabled"].to_bool() ? base->disable() : base->enable();
256 if (event.type != SDL_MOUSEBUTTONUP && event.type != SDL_MOUSEBUTTONDOWN) {
260 if (event.button.button !=
button_) {
269 return "mouse " + std::to_string(
button_);
284 if (ret.size() == 1) {
285 boost::algorithm::to_lower(ret);
293 if (event.type != SDL_KEYDOWN && event.type != SDL_KEYUP) {
298 code =
event.key.keysym.scancode;
315 return button_ == other_m->button_;
321 item[
"key"] = SDL_GetScancodeName(
scancode_);
328 if (item->get_command() == command) {
349 if (!hotkeys_.empty()) {
350 hotkeys_.erase(
std::remove(hotkeys_.begin(), hotkeys_.end(), item));
364 if (!hotkeys_.empty()) {
366 std::remove_if(hotkeys_.begin(), hotkeys_.end(),
371 hotkeys_.push_back(item);
378 if (item->get_command() == command)
380 if (item->is_default())
396 if (item->matches(event)) {
408 if (!set_as_default) {
409 item->unset_default();
417 if (set_as_default) {
418 default_hotkey_cfg_ = cfg;
426 if (!default_hotkey_cfg_.
empty()) {
429 ERR_G<<
"no default hotkeys set yet; all hotkeys are now unassigned!" << std::endl;
443 if ((!item->is_default() && item->active()) ||
444 (item->is_default() && item->is_disabled())) {
453 std::vector<std::string>
names;
455 if (item->get_command() ==
id && !item->null() && !item->is_disabled()) {
456 names.push_back(item->get_name());
462 names.push_back(
"escape");
464 else if (
id ==
"quit-to-desktop") {
466 names.push_back(
"cmd+q");
468 names.push_back(
"alt+F4");
void save(config &cfg) const
Save the hotkey into the configuration object.
child_itors child_range(const std::string &key)
static unsigned int sdl_get_mods()
hotkey_ptr load_from_config(const config &cfg)
Create and instantiate a hotkey from a config element.
boost::shared_ptr< hotkey_keyboard > hotkey_keyboard_ptr
void save_hotkeys(config &cfg)
Save the non-default hotkeys to the config.
scope
Available hotkey scopes.
bool ends_with(const std::string &str, const std::string &suffix)
bool matches(const SDL_Event &event) const
Used to evaluate whether:
virtual bool bindings_equal_helper(hotkey_ptr other) const =0
This is invoked by hotkey_base::bindings_equal as a helper for the concrete classes.
virtual const std::string get_name_helper() const
This is invoked by hotkey_base::get_name and must be implemented by subclasses.
static lg::log_domain log_config("config")
void del_hotkey(hotkey_ptr item)
Remove a hotkey from the list of hotkeys.
void clear_hotkeys(const std::string &command)
Unset the command bindings for all hotkeys matching the command.
bool is_scope_active(hk_scopes s)
bool has_hotkey_item(const std::string &command)
virtual bool matches_helper(const SDL_Event &event) const
This is invoked by hotkey_base::matches as a helper for the concrete classes.
virtual void save_helper(config &cfg) const
void clear_children(const std::string &key)
Definitions for the interface to Wesnoth Markup Language (WML).
virtual bool matches_helper(const SDL_Event &event) const =0
This is invoked by hotkey_base::matches as a helper for the concrete classes.
void set_active_scopes(hk_scopes s)
Keyboard shortcuts for game actions.
const hotkey_list & get_hotkeys()
Returns the list of hotkeys.
std::bitset< SCOPE_COUNT > hk_scopes
virtual void save_helper(config &cfg) const =0
const hotkey_ptr get_hotkey(const SDL_Event &event)
Iterate through the list of hotkeys and return a hotkey that matches the SDL_Event and the current ke...
config & add_child(const std::string &key)
hotkey_ptr create_hotkey(const std::string &id, SDL_Event &event)
Create a new hotkey item for a command from an SDL_Event.
void add_hotkey(const hotkey_ptr item)
Add a hotkey to the list of hotkeys.
This class is used to return non-valid results in order to save other people from null checks...
const hotkey::hk_scopes scope
The visibility scope of the command.
std::vector< hotkey::hotkey_ptr > hotkey_list
GLuint const GLuint * names
std::string join(T const &v, const std::string &s=",")
Generates a new string joining container items in a list.
virtual const std::string get_name_helper() const =0
This is invoked by hotkey_base::get_name and must be implemented by subclasses.
virtual bool matches_helper(const SDL_Event &event) const
This is invoked by hotkey_base::matches as a helper for the concrete classes.
boost::shared_ptr< hotkey_mouse > hotkey_mouse_ptr
std::string get_names(std::string id)
Returns a comma-separated string of hotkey names.
boost::shared_ptr< hotkey_base > hotkey_ptr
void reset_default_hotkeys()
Reset all hotkeys to the defaults.
virtual const std::string get_name_helper() const
This is invoked by hotkey_base::get_name and must be implemented by subclasses.
config default_hotkey_cfg_
This class is responsible for handling mouse button presses.
const std::string & get_command() const
Returns the string name of the HOTKEY_COMMAND.
This is the base class for hotkey event matching.
virtual bool bindings_equal_helper(hotkey_ptr other) const
This is invoked by hotkey_base::bindings_equal as a helper for the concrete classes.
void load_hotkeys(const config &cfg, bool set_as_default)
Iterates through all hotkeys present in the config struct and creates and adds them to the hotkey lis...
This class is responsible for handling keys, not modifiers.
virtual bool bindings_equal_helper(hotkey_ptr other) const
This is invoked by hotkey_base::bindings_equal as a helper for the concrete classes.
Standard logging facilities (interface).
virtual bool bindings_equal(hotkey_ptr other)
Checks whether the hotkey bindings and scope are equal.
const std::string remove
remove directive
A config object defines a single node in a WML file, with access to child nodes.
virtual void save_helper(config &cfg) const
const hotkey_command & get_hotkey_command(const std::string &command)
returns the hotkey_command with the given name
GLsizei const GLcharARB ** string
const std::string get_name() const
Return "name" of hotkey.