Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide ability to execute an action from the keybindings menu #36859

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions data/raw/keybindings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2379,6 +2379,13 @@
"name": "Add global keybinding",
"bindings": [ { "input_method": "keyboard", "key": "=" } ]
},
{
"type": "keybinding",
"id": "EXECUTE",
"category": "HELP_KEYBINDINGS",
"name": "Execute action keybinding",
"bindings": [ { "input_method": "keyboard", "key": "." } ]
},
{
"type": "keybinding",
"id": "ADD_ZONE",
Expand Down
15 changes: 10 additions & 5 deletions src/handle_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,15 @@ bool game::handle_action()
#endif
}

if( act == ACTION_KEYBINDINGS ) {
u.clear_destination();
destination_preview.clear();
act = ctxt.display_menu( true );
if( act == ACTION_NULL ) {
return false;
}
}

if( can_action_change_worldstate( act ) ) {
user_action_counter += 1;
}
Expand Down Expand Up @@ -1676,6 +1685,7 @@ bool game::handle_action()
break; // dummy entries
case ACTION_ACTIONMENU:
case ACTION_MAIN_MENU:
case ACTION_KEYBINDINGS:
break; // handled above

case ACTION_TIMEOUT:
Expand Down Expand Up @@ -2266,11 +2276,6 @@ bool game::handle_action()
refresh_all();
break;

case ACTION_KEYBINDINGS:
ctxt.display_menu();
refresh_all();
break;

case ACTION_OPTIONS:
get_options().show( true );
g->init_ui( true );
Expand Down
26 changes: 20 additions & 6 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -975,12 +975,13 @@ cata::optional<tripoint> input_context::get_direction( const std::string &action
// alternative hotkeys, which mustn't be included so that the hardcoded
// hotkeys do not show up beside entries within the window.
const std::string display_help_hotkeys =
"abcdefghijkpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:;'\",./<>?!@#$%^&*()_[]\\{}|`~";
"abcdefghijkpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:;'\",/<>?!@#$%^&*()_[]\\{}|`~";

void input_context::display_menu()
action_id input_context::display_menu( const bool permit_execute_action )
{
// Shamelessly stolen from help.cpp
action_id action_to_execute = ACTION_NULL;

// Shamelessly stolen from help.cpp
input_context ctxt( "HELP_KEYBINDINGS" );
ctxt.register_action( "UP", translate_marker( "Scroll up" ) );
ctxt.register_action( "DOWN", translate_marker( "Scroll down" ) );
Expand All @@ -989,6 +990,7 @@ void input_context::display_menu()
ctxt.register_action( "REMOVE" );
ctxt.register_action( "ADD_LOCAL" );
ctxt.register_action( "ADD_GLOBAL" );
ctxt.register_action( "EXECUTE" );
ctxt.register_action( "QUIT" );
ctxt.register_action( "ANY_INPUT" );

Expand All @@ -1011,8 +1013,8 @@ void input_context::display_menu()
bool changed = false;
// keybindings before the user changed anything.
input_manager::t_action_contexts old_action_contexts( inp_mngr.action_contexts );
// current status: adding/removing/showing keybindings
enum { s_remove, s_add, s_add_global, s_show } status = s_show;
// current status: adding/removing/executing/showing keybindings
enum { s_remove, s_add, s_add_global, s_execute, s_show } status = s_show;
// copy of registered_actions, but without the ANY_INPUT and COORDINATE, which should not be shown
std::vector<std::string> org_registered_actions( registered_actions );
org_registered_actions.erase( std::remove_if( org_registered_actions.begin(),
Expand All @@ -1037,6 +1039,9 @@ void input_context::display_menu()
legend += colorize( _( "Keybinding active only on this screen" ), local_key ) + "\n";
legend += colorize( _( "Keybinding active globally" ), global_key ) + "\n";
legend += _( "Press - to remove keybinding\nPress + to add local keybinding\nPress = to add global keybinding\n" );
if( permit_execute_action ) {
legend += _( "Press . to execute action\n" );
}

std::vector<std::string> filtered_registered_actions = org_registered_actions;
std::string filter_phrase;
Expand Down Expand Up @@ -1079,6 +1084,8 @@ void input_context::display_menu()
mvwprintz( w_help, point( 2, i + 10 ), c_light_blue, "%c ", invlet );
} else if( status == s_remove ) {
mvwprintz( w_help, point( 2, i + 10 ), c_light_blue, "%c ", invlet );
} else if( status == s_execute ) {
mvwprintz( w_help, point( 2, i + 10 ), c_white, "%c ", invlet );
} else {
mvwprintz( w_help, point( 2, i + 10 ), c_blue, " " );
}
Expand Down Expand Up @@ -1117,14 +1124,16 @@ void input_context::display_menu()
}

// In addition to the modifiable hotkeys, we also check for hardcoded
// keys, e.g. '+', '-', '=', in order to prevent the user from
// keys, e.g. '+', '-', '=', '.' in order to prevent the user from
// entering an unrecoverable state.
if( action == "ADD_LOCAL" || raw_input_char == '+' ) {
status = s_add;
} else if( action == "ADD_GLOBAL" || raw_input_char == '=' ) {
status = s_add_global;
} else if( action == "REMOVE" || raw_input_char == '-' ) {
status = s_remove;
} else if( ( action == "EXECUTE" || raw_input_char == '.' ) && permit_execute_action ) {
status = s_execute;
} else if( action == "ANY_INPUT" ) {
const size_t hotkey_index = hotkeys.find_first_of( raw_input_char );
if( hotkey_index == std::string::npos ) {
Expand Down Expand Up @@ -1198,6 +1207,9 @@ void input_context::display_menu()
inp_mngr.add_input_for_action( action_id, category_to_access, new_event );
changed = true;
}
} else if( status == s_execute && permit_execute_action ) {
action_to_execute = look_up_action( action_id );
break;
}
status = s_show;
} else if( action == "DOWN" ) {
Expand Down Expand Up @@ -1248,6 +1260,8 @@ void input_context::display_menu()
}
werase( w_help );
wrefresh( w_help );

return action_to_execute;
}

input_event input_context::get_raw_input()
Expand Down
6 changes: 5 additions & 1 deletion src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "point.h"

enum action_id : int;

namespace cata
{
template<typename T>
Expand Down Expand Up @@ -624,8 +626,10 @@ class input_context
/**
* Displays the possible actions in the current context and their
* keybindings.
* @param permit_execute_action If `true` the function allows the user to specify an action to execute
* @returns action_id of any action the user specified to execute, or ACTION_NULL if none
*/
void display_menu();
action_id display_menu( bool permit_execute_action = false );

/**
* Temporary method to retrieve the raw input received, so that input_contexts
Expand Down