Skip to content

Commit

Permalink
Fix hotkeys functions returning only printable bound keys for a menu. (
Browse files Browse the repository at this point in the history
…#30441)

* Allows functions checking for bound keys to return them even if they are not printable.

- Change keys_bound_to and hotkey_for_action to take a new parameter.
- The new parameter defaults to the old behavior (only printable keys are returned).
- To get all bounds keys (even if not printable) pass `false`.

* Get debug menu hotkey even if it's not printable.
  • Loading branch information
neitsa authored and ZhilkinSerg committed May 12, 2019
1 parent 1d2255b commit 24a03cd
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ Fix \"%s\" at your next chance!", ch, id, FILENAMES["keymap"] );
}
}

std::vector<char> keys_bound_to( action_id act )
std::vector<char> keys_bound_to( action_id act, const bool restrict_to_printable )
{
input_context ctxt = get_default_mode_input_context();
return ctxt.keys_bound_to( action_ident( act ) );
return ctxt.keys_bound_to( action_ident( act ), restrict_to_printable );
}

action_id action_from_key( char ch )
Expand Down Expand Up @@ -525,12 +525,12 @@ point get_delta_from_movement_direction( action_id act )
}
}

long hotkey_for_action( action_id action )
long hotkey_for_action( action_id action, const bool restrict_to_printable )
{
auto is_valid_key = []( char key ) {
return key != '?';
};
std::vector<char> keys = keys_bound_to( action );
std::vector<char> keys = keys_bound_to( action, restrict_to_printable );
auto valid = std::find_if( keys.begin(), keys.end(), is_valid_key );
return valid == keys.end() ? -1 : *valid;
}
Expand Down
6 changes: 4 additions & 2 deletions src/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,19 @@ void load_keyboard_settings( std::map<char, action_id> &keymap,
* given action then the returned vector is simply left empty.
*
* @param act Action ID to lookup in keymap
* @param restrict_to_printable If `true` the function returns the bound keys only if they are printable. If `false`, all keys (whether they are printable or not) are returned.
* @returns all keys (as characters) currently bound to a give action ID
*/
std::vector<char> keys_bound_to( action_id act );
std::vector<char> keys_bound_to( action_id act, bool restrict_to_printable = true );

/**
* Get the key for an action, used in the action menu to give each action the hotkey it is bound to.
* @param action Action ID to lookup in keymap.
* @param restrict_to_printable If `true` the function returns the bound key only if it is printable. If `false`, any key (whether they it is printable or not) is returned.
* @returns the key code for the hotkey or -1 if no key is associated with the given action.
* @note We ignore bindings to '?' because that will already do something else in this menu (open the menu keybindings).
*/
long hotkey_for_action( action_id action );
long hotkey_for_action( action_id action, bool restrict_to_printable = true );

/**
* Lookup an action ID by its unique string identifier
Expand Down
2 changes: 1 addition & 1 deletion src/debug_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ void draw_benchmark( const int max_difference )

void debug()
{
bool debug_menu_has_hotkey = hotkey_for_action( ACTION_DEBUG ) != -1;
bool debug_menu_has_hotkey = hotkey_for_action( ACTION_DEBUG, false ) != -1;
int action = debug_menu_uilist( debug_menu_has_hotkey );
g->refresh_all();
player &u = g->u;
Expand Down
11 changes: 7 additions & 4 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,17 +656,20 @@ void input_context::register_action( const std::string &action_descriptor, const
}
}

std::vector<char> input_context::keys_bound_to( const std::string &action_descriptor ) const
std::vector<char> input_context::keys_bound_to( const std::string &action_descriptor,
const bool restrict_to_printable ) const
{
std::vector<char> result;
const std::vector<input_event> &events = inp_mngr.get_input_for_action( action_descriptor,
category );
for( const auto &events_event : events ) {
// Ignore multi-key input and non-keyboard input
// TODO: fix for Unicode.
if( events_event.type == CATA_INPUT_KEYBOARD && events_event.sequence.size() == 1 &&
events_event.sequence.front() < 0xFF && isprint( events_event.sequence.front() ) ) {
result.push_back( static_cast<char>( events_event.sequence.front() ) );
if( events_event.type == CATA_INPUT_KEYBOARD && events_event.sequence.size() == 1 ) {
if( !restrict_to_printable || events_event.sequence.front() < 0xFF &&
isprint( events_event.sequence.front() ) ) {
result.push_back( static_cast<char>( events_event.sequence.front() ) );
}
}
}
return result;
Expand Down
6 changes: 5 additions & 1 deletion src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,12 @@ class input_context
/**
* Keys (and only keys, other input types are not included) that
* trigger the given action.
* @param action_descriptor The action descriptor for which to get the bound keys.
* @param restrict_to_printable If `true` the function returns the bound keys only if they are printable. If `false`, all keys (whether they are printable or not) are returned.
* @returns All keys bound to the given action descriptor.
*/
std::vector<char> keys_bound_to( const std::string &action_id ) const;
std::vector<char> keys_bound_to( const std::string &action_descriptor,
bool restrict_to_printable = true ) const;

/**
* Get/Set edittext to display IME unspecified string.
Expand Down

0 comments on commit 24a03cd

Please sign in to comment.