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

Adds a key to toggle the language to English and back on the fly #65109

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f462bc9
Adds a key to toggle the language to English and back on the fly
MarcusAseth Apr 16, 2023
f771508
fixed formatting
MarcusAseth Apr 16, 2023
80a6317
small formatting fix
MarcusAseth Apr 16, 2023
e513068
small formatting fix
MarcusAseth Apr 16, 2023
6a1d3f7
small formatting fix
MarcusAseth Apr 16, 2023
4794e9f
Improved readability
MarcusAseth Apr 16, 2023
3b47d88
small formatting fix
MarcusAseth Apr 16, 2023
8afcbe9
by default the keybind is now unbound
MarcusAseth Apr 16, 2023
ce69887
made the change_language code into it's own function
MarcusAseth Apr 16, 2023
286fe4a
added const + format fix
MarcusAseth Apr 16, 2023
e785728
added const
MarcusAseth Apr 16, 2023
938e2af
improved naming of functions
MarcusAseth Apr 16, 2023
d4bca6e
Allows "Toggle language to en" to work in every menu
MarcusAseth Apr 22, 2023
621fb78
Added .empty() checks for safety
MarcusAseth Apr 22, 2023
1ad1d8a
Changed to compare actions instead of strings
MarcusAseth Apr 23, 2023
20368b5
using references instead of making copies
MarcusAseth Apr 23, 2023
6fc4c2a
Item text inside inventory tab gets correctly refreshed
MarcusAseth Apr 23, 2023
4d396d2
astyle format
MarcusAseth Apr 23, 2023
532f4df
now the action is not registered to a specific contest, should be global
MarcusAseth Apr 23, 2023
94a9912
fix comment and spacing
MarcusAseth Apr 23, 2023
1d4ccc7
Invalidate all ui_adaptors after changing language
MarcusAseth Apr 23, 2023
e90ae19
initialized undefined variable
MarcusAseth Apr 23, 2023
708bf53
Correctly regenerate cache for item descriptions in the crafting menu…
MarcusAseth Apr 24, 2023
7c081ef
moved lang_version variable inside recipe_result_info_cache
MarcusAseth Apr 24, 2023
ffe608c
Makes inventory_item_menu refresh when the language change
MarcusAseth Apr 24, 2023
fa92d38
Toggle now also work in the Compare menu
MarcusAseth Apr 24, 2023
eebb2b6
central portion of the crafting menu was still cached - fixed
MarcusAseth Apr 24, 2023
ac65634
implemented suggestions
MarcusAseth Apr 25, 2023
1bee9ff
Apply suggestions from code review
MarcusAseth Apr 25, 2023
cf311bf
moved variable closed to where it's used
MarcusAseth Apr 25, 2023
e719bd3
added missing update of a bool
MarcusAseth Apr 25, 2023
64d533b
moved variable outside of loop
MarcusAseth Apr 25, 2023
6d0cf93
moved variable outside of loop
MarcusAseth Apr 25, 2023
42ea1f2
added comment
MarcusAseth Apr 25, 2023
3be2585
update cache for the right portion of the building screen
MarcusAseth Apr 25, 2023
14b748f
added comment
MarcusAseth Apr 25, 2023
fa02262
added comment
MarcusAseth Apr 25, 2023
f3bc119
fixed comment -
MarcusAseth Apr 25, 2023
1195d74
empty commit
MarcusAseth May 1, 2023
45d6b53
empty commit
MarcusAseth May 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions data/raw/keybindings.json
Expand Up @@ -2100,6 +2100,11 @@
{ "input_method": "gamepad", "key": "JOY_7" }
]
},
{
"type": "keybinding",
"name": "Toggle language to English",
"id": "toggle_language_to_en"
},
{
"type": "keybinding",
"name": "Toggle map memory",
Expand Down
2 changes: 2 additions & 0 deletions src/action.cpp
Expand Up @@ -287,6 +287,8 @@ std::string action_ident( action_id act )
return "autosafe";
case ACTION_TOGGLE_THIEF_MODE:
return "toggle_thief_mode";
case ACTION_TOGGLE_LANGUAGE_TO_EN:
return "toggle_language_to_en";
case ACTION_IGNORE_ENEMY:
return "ignore_enemy";
case ACTION_WHITELIST_ENEMY:
Expand Down
2 changes: 2 additions & 0 deletions src/action.h
Expand Up @@ -229,6 +229,8 @@ enum action_id : int {
ACTION_TOGGLE_AUTOSAFE,
/** Toggle permanent attitude to stealing */
ACTION_TOGGLE_THIEF_MODE,
/** Switch current language to English and back */
ACTION_TOGGLE_LANGUAGE_TO_EN,
/** Ignore the enemy that triggered safemode */
ACTION_IGNORE_ENEMY,
/** Whitelist the enemy that triggered safemode */
Expand Down
5 changes: 4 additions & 1 deletion src/construction.cpp
Expand Up @@ -718,9 +718,12 @@ construction_id construction_menu( const bool blueprint )
}
}
isnew = false;
static int lang_version = detail::get_current_language_version();

if( update_info ) {
//lang check here is needed to redraw the menu when using "Toggle language to English" option
if( update_info || lang_version != detail::get_current_language_version() ) {
update_info = false;
lang_version = detail::get_current_language_version();

notes.clear();
if( tabindex == tabcount - 1 && !filter.empty() ) {
Expand Down
26 changes: 18 additions & 8 deletions src/crafting_gui.cpp
Expand Up @@ -576,6 +576,7 @@ class recipe_result_info_cache
int last_terminal_width = 0;
int panel_width;
int cached_batch_size = 1;
int lang_version = 0;

void get_byproducts_data( const recipe *rec, std::vector<iteminfo> &summary_info,
std::vector<iteminfo> &details_info );
Expand Down Expand Up @@ -674,13 +675,18 @@ void recipe_result_info_cache::get_item_header( item &dummy_item, const int quan
item_info_data recipe_result_info_cache::get_result_data( const recipe *rec, const int batch_size,
int &scroll_pos, const catacurses::window &window )
{
/* If the recipe has not changed, return the cached version in info.
Unfortunately, the separator lines are baked into info at a specific width, so if the terminal width
has changed, the info needs to be regenerated */
if( rec == last_recipe && rec != nullptr && TERMX == last_terminal_width &&
batch_size == cached_batch_size ) {
item_info_data data( "", "", info, {}, scroll_pos );
return data;
//lang check here is needed to rebuild cache when using "Toggle language to English" option
if( lang_version == detail::get_current_language_version() ) {
/* If the recipe has not changed, return the cached version in info.
Unfortunately, the separator lines are baked into info at a specific width, so if the terminal width
has changed, the info needs to be regenerated */
if( rec == last_recipe && rec != nullptr && TERMX == last_terminal_width &&
batch_size == cached_batch_size ) {
item_info_data data( "", "", info, {}, scroll_pos );
return data;
}
} else {
lang_version = detail::get_current_language_version();
}

cached_batch_size = batch_size;
Expand Down Expand Up @@ -812,15 +818,19 @@ static const std::vector<std::string> &cached_recipe_info( recipe_info_cache &in
const recipe &recp, const availability &avail, Character &guy, const std::string &qry_comps,
const int batch_size, const int fold_width, const nc_color &color )
{
static int lang_version = detail::get_current_language_version();

if( info_cache.recp != &recp ||
info_cache.qry_comps != qry_comps ||
info_cache.batch_size != batch_size ||
info_cache.fold_width != fold_width ) {
info_cache.fold_width != fold_width ||
lang_version != detail::get_current_language_version() ) {
info_cache.recp = &recp;
info_cache.qry_comps = qry_comps;
info_cache.batch_size = batch_size;
info_cache.fold_width = fold_width;
info_cache.text = recipe_info( recp, avail, guy, qry_comps, batch_size, fold_width, color );
lang_version = detail::get_current_language_version();
}
return info_cache.text;
}
Expand Down
225 changes: 123 additions & 102 deletions src/game.cpp
Expand Up @@ -633,6 +633,16 @@ void game::toggle_pixel_minimap() const
#endif // TILES
}

void game::toggle_language_to_en()
{
const std::string english = "en" ;
static std::string secondary_lang = english;
std::string current_lang = TranslationManager::GetInstance().GetCurrentLanguage();
secondary_lang = current_lang != english ? current_lang : secondary_lang;
std::string new_lang = current_lang != english ? english : secondary_lang;
set_language( new_lang );
}

bool game::is_tileset_isometric() const
{
#if defined(TILES)
Expand Down Expand Up @@ -1964,119 +1974,129 @@ int game::inventory_item_menu( item_location locThisItem,
add_key_to_quick_shortcuts( oThisItem.invlet, "INVENTORY", false );
}
#endif

const bool bHPR = get_auto_pickup().has_rule( &oThisItem );
std::vector<iteminfo> vThisItem;
std::vector<iteminfo> vDummy;

const bool bHPR = get_auto_pickup().has_rule( &oThisItem );
const hint_rating rate_drop_item = u.get_wielded_item() &&
u.get_wielded_item()->has_flag( flag_NO_UNWIELD ) ?
hint_rating::cant : hint_rating::good;

item_info_data data;
int iScrollPos = 0;
int iScrollHeight = 0;
uilist action_menu;
action_menu.allow_anykey = true;
const auto addentry = [&]( const char key, const std::string & text, const hint_rating hint ) {
// The char is used as retval from the uilist *and* as hotkey.
action_menu.addentry( key, true, key, text );
auto &entry = action_menu.entries.back();
switch( hint ) {
case hint_rating::cant:
entry.text_color = c_light_gray;
break;
case hint_rating::iffy:
entry.text_color = c_light_red;
break;
case hint_rating::good:
entry.text_color = c_light_green;
break;
}
};
addentry( 'a', pgettext( "action", "activate" ), rate_action_use( u, oThisItem ) );
addentry( 'R', pgettext( "action", "read" ), rate_action_read( u, oThisItem ) );
addentry( 'E', pgettext( "action", "eat" ), rate_action_eat( u, oThisItem ) );
addentry( 'W', pgettext( "action", "wear" ), rate_action_wear( u, oThisItem ) );
addentry( 'w', pgettext( "action", "wield" ), rate_action_wield( u, oThisItem ) );
addentry( 't', pgettext( "action", "throw" ), rate_action_wield( u, oThisItem ) );
addentry( 'c', pgettext( "action", "change side" ), rate_action_change_side( u, oThisItem ) );
addentry( 'T', pgettext( "action", "take off" ), rate_action_take_off( u, oThisItem ) );
addentry( 'd', pgettext( "action", "drop" ), rate_drop_item );
addentry( 'U', pgettext( "action", "unload" ), u.rate_action_unload( oThisItem ) );
addentry( 'r', pgettext( "action", "reload" ), u.rate_action_reload( oThisItem ) );
addentry( 'p', pgettext( "action", "part reload" ), u.rate_action_reload( oThisItem ) );
addentry( 'm', pgettext( "action", "mend" ), rate_action_mend( u, oThisItem ) );
addentry( 'D', pgettext( "action", "disassemble" ), rate_action_disassemble( u, oThisItem ) );
if( oThisItem.is_container() && !oThisItem.is_corpse() ) {
addentry( 'i', pgettext( "action", "insert" ), rate_action_insert( u, locThisItem ) );
if( oThisItem.num_item_stacks() > 0 ) {
addentry( 'o', pgettext( "action", "open" ), hint_rating::good );
}
addentry( 'v', pgettext( "action", "pocket settings" ), hint_rating::good );
}

if( oThisItem.is_favorite ) {
addentry( 'f', pgettext( "action", "unfavorite" ), hint_rating::good );
} else {
addentry( 'f', pgettext( "action", "favorite" ), hint_rating::good );
}

addentry( 'V', pgettext( "action", "view recipe" ), rate_action_view_recipe( u, oThisItem ) );
addentry( '>', pgettext( "action", "hide contents" ), rate_action_collapse( oThisItem ) );
addentry( '<', pgettext( "action", "show contents" ), rate_action_expand( oThisItem ) );
addentry( '=', pgettext( "action", "reassign" ), hint_rating::good );

if( bHPR ) {
addentry( '-', _( "Auto pickup" ), hint_rating::iffy );
} else {
addentry( '+', _( "Auto pickup" ), hint_rating::good );
}
std::unique_ptr<ui_adaptor> ui;

int iScrollPos = 0;
oThisItem.info( true, vThisItem );
bool exit = false;
bool first_execution = true;
static int lang_version = detail::get_current_language_version();
do {
//lang check here is needed to redraw the menu when using "Toggle language to English" option
if( first_execution || lang_version != detail::get_current_language_version() ) {

const hint_rating rate_drop_item = u.get_wielded_item() &&
u.get_wielded_item()->has_flag( flag_NO_UNWIELD ) ?
hint_rating::cant : hint_rating::good;
action_menu.reset();
action_menu.allow_anykey = true;
const auto addentry = [&]( const char key, const std::string & text, const hint_rating hint ) {
// The char is used as retval from the uilist *and* as hotkey.
action_menu.addentry( key, true, key, text );
auto &entry = action_menu.entries.back();
switch( hint ) {
case hint_rating::cant:
entry.text_color = c_light_gray;
break;
case hint_rating::iffy:
entry.text_color = c_light_red;
break;
case hint_rating::good:
entry.text_color = c_light_green;
break;
}
};
addentry( 'a', pgettext( "action", "activate" ), rate_action_use( u, oThisItem ) );
addentry( 'R', pgettext( "action", "read" ), rate_action_read( u, oThisItem ) );
addentry( 'E', pgettext( "action", "eat" ), rate_action_eat( u, oThisItem ) );
addentry( 'W', pgettext( "action", "wear" ), rate_action_wear( u, oThisItem ) );
addentry( 'w', pgettext( "action", "wield" ), rate_action_wield( u, oThisItem ) );
addentry( 't', pgettext( "action", "throw" ), rate_action_wield( u, oThisItem ) );
addentry( 'c', pgettext( "action", "change side" ), rate_action_change_side( u, oThisItem ) );
addentry( 'T', pgettext( "action", "take off" ), rate_action_take_off( u, oThisItem ) );
addentry( 'd', pgettext( "action", "drop" ), rate_drop_item );
addentry( 'U', pgettext( "action", "unload" ), u.rate_action_unload( oThisItem ) );
addentry( 'r', pgettext( "action", "reload" ), u.rate_action_reload( oThisItem ) );
addentry( 'p', pgettext( "action", "part reload" ), u.rate_action_reload( oThisItem ) );
addentry( 'm', pgettext( "action", "mend" ), rate_action_mend( u, oThisItem ) );
addentry( 'D', pgettext( "action", "disassemble" ), rate_action_disassemble( u, oThisItem ) );
if( oThisItem.is_container() && !oThisItem.is_corpse() ) {
addentry( 'i', pgettext( "action", "insert" ), rate_action_insert( u, locThisItem ) );
if( oThisItem.num_item_stacks() > 0 ) {
addentry( 'o', pgettext( "action", "open" ), hint_rating::good );
}
addentry( 'v', pgettext( "action", "pocket settings" ), hint_rating::good );
}

action_menu.w_y_setup = 0;
action_menu.w_x_setup = [&]( const int popup_width ) -> int {
switch( position )
{
default:
case RIGHT_TERMINAL_EDGE:
return 0;
case LEFT_OF_INFO:
return iStartX() - popup_width;
case RIGHT_OF_INFO:
return iStartX() + iWidth();
case LEFT_TERMINAL_EDGE:
return TERMX - popup_width;
}
};
// Filtering isn't needed, the number of entries is manageable.
action_menu.filtering = false;
// Default menu border color is different, this matches the border of the item info window.
action_menu.border_color = BORDER_COLOR;
if( oThisItem.is_favorite ) {
addentry( 'f', pgettext( "action", "unfavorite" ), hint_rating::good );
} else {
addentry( 'f', pgettext( "action", "favorite" ), hint_rating::good );
}

item_info_data data( oThisItem.tname(), oThisItem.type_name(), vThisItem, vDummy, iScrollPos );
data.without_getch = true;
addentry( 'V', pgettext( "action", "view recipe" ), rate_action_view_recipe( u, oThisItem ) );
addentry( '>', pgettext( "action", "hide contents" ), rate_action_collapse( oThisItem ) );
addentry( '<', pgettext( "action", "show contents" ), rate_action_expand( oThisItem ) );
addentry( '=', pgettext( "action", "reassign" ), hint_rating::good );

catacurses::window w_info;
int iScrollHeight = 0;
if( bHPR ) {
addentry( '-', _( "Auto pickup" ), hint_rating::iffy );
} else {
addentry( '+', _( "Auto pickup" ), hint_rating::good );
}

oThisItem.info( true, vThisItem );

action_menu.w_y_setup = 0;
action_menu.w_x_setup = [&]( const int popup_width ) -> int {
switch( position )
{
default:
case RIGHT_TERMINAL_EDGE:
return 0;
case LEFT_OF_INFO:
return iStartX() - popup_width;
case RIGHT_OF_INFO:
return iStartX() + iWidth();
case LEFT_TERMINAL_EDGE:
return TERMX - popup_width;
}
};
// Filtering isn't needed, the number of entries is manageable.
action_menu.filtering = false;
// Default menu border color is different, this matches the border of the item info window.
action_menu.border_color = BORDER_COLOR;

data = item_info_data( oThisItem.tname(), oThisItem.type_name(), vThisItem, vDummy, iScrollPos );
data.without_getch = true;

catacurses::window w_info;

ui = std::make_unique<ui_adaptor>();
ui->on_screen_resize( [&]( ui_adaptor & ui ) {
w_info = catacurses::newwin( TERMY, iWidth(), point( iStartX(), 0 ) );
iScrollHeight = TERMY - 2;
ui.position_from_window( w_info );
} );
ui->mark_resize();

std::unique_ptr<ui_adaptor> ui = std::make_unique<ui_adaptor>();
ui->on_screen_resize( [&]( ui_adaptor & ui ) {
w_info = catacurses::newwin( TERMY, iWidth(), point( iStartX(), 0 ) );
iScrollHeight = TERMY - 2;
ui.position_from_window( w_info );
} );
ui->mark_resize();
ui->on_redraw( [&]( const ui_adaptor & ) {
draw_item_info( w_info, data );
} );

ui->on_redraw( [&]( const ui_adaptor & ) {
draw_item_info( w_info, data );
} );
action_menu.additional_actions = {
{ "RIGHT", translation() }
};

action_menu.additional_actions = {
{ "RIGHT", translation() }
};
lang_version = detail::get_current_language_version();
first_execution = false;
}

bool exit = false;
do {
const int prev_selected = action_menu.selected;
action_menu.query( false );
if( action_menu.ret >= 0 ) {
Expand All @@ -2101,6 +2121,7 @@ int game::inventory_item_menu( item_location locThisItem,
ui = nullptr;
}


switch( cMenu ) {
case 'a': {
contents_change_handler handler;
Expand Down
1 change: 1 addition & 0 deletions src/game.h
Expand Up @@ -671,6 +671,7 @@ class game

void toggle_fullscreen();
void toggle_pixel_minimap() const;
void toggle_language_to_en();
bool is_tileset_isometric() const;
void reload_tileset();
void temp_exit_fullscreen();
Expand Down