Skip to content

Commit

Permalink
Allow marking construction recipes as favorite, sort recipes by name …
Browse files Browse the repository at this point in the history
…in construction UI (cataclysmbnteam#2109)

* Sort constructions according to user locale

* Allow marking construction recipes as favorite
  • Loading branch information
olanti-p authored Oct 31, 2022
1 parent 8938ddb commit 2fb69f3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
6 changes: 6 additions & 0 deletions data/json/construction_category.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
"id": "OTHER",
"name": "Others"
},
{
"//": "Should be second to last in the list",
"type": "construction_category",
"id": "FAVORITE",
"name": "Favorite"
},
{
"//": "Should be last in the list",
"type": "construction_category",
Expand Down
7 changes: 7 additions & 0 deletions data/raw/keybindings.json
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,13 @@
"name": "Toggle unavailable constructions",
"bindings": [ { "input_method": "keyboard", "key": ";" } ]
},
{
"type": "keybinding",
"id": "TOGGLE_FAVORITE",
"category": "CONSTRUCTION",
"name": "Toggle construction as favorite",
"bindings": [ { "input_method": "keyboard", "key": "*" } ]
},
{
"type": "keybinding",
"id": "SCROLL_STAGE_UP",
Expand Down
58 changes: 57 additions & 1 deletion src/construction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ static const activity_id ACT_BUILD( "ACT_BUILD" );
static const activity_id ACT_MULTIPLE_CONSTRUCTION( "ACT_MULTIPLE_CONSTRUCTION" );

static const construction_category_id construction_category_ALL( "ALL" );
static const construction_category_id construction_category_FAVORITE( "FAVORITE" );
static const construction_category_id construction_category_FILTER( "FILTER" );

static const itype_id itype_2x4( "2x4" );
Expand Down Expand Up @@ -278,6 +279,14 @@ static std::vector<const construction *> constructions_by_group( const construct
return result;
}

static void sort_constructions_by_name( std::vector<construction_group_str_id> &list )
{
std::sort( list.begin(), list.end(),
[]( const construction_group_str_id & a, const construction_group_str_id & b ) {
return localized_compare( a->name(), b->name() );
} );
}

static void list_available_constructions( std::vector<construction_group_str_id> &available,
std::map<construction_category_id, std::vector<construction_group_str_id>> &cat_available,
bool hide_unconstructable )
Expand Down Expand Up @@ -310,6 +319,10 @@ static void list_available_constructions( std::vector<construction_group_str_id>
}
}
}
sort_constructions_by_name( available );
for( auto &it : cat_available ) {
sort_constructions_by_name( it.second );
}
}

static void draw_grid( const catacurses::window &w, const int list_width )
Expand Down Expand Up @@ -359,6 +372,21 @@ static nc_color construction_color( const construction_group_str_id &group, bool
return highlight ? hilite( col ) : col;
}

static bool is_favorite( const construction_group_str_id &c )
{
return uistate.favorite_construct_recipes.count( c ) > 0;
}

static void favorite_add( const construction_group_str_id &c )
{
uistate.favorite_construct_recipes.insert( c );
}

static void favorite_remove( const construction_group_str_id &c )
{
uistate.favorite_construct_recipes.erase( c );
}

cata::optional<construction_id> construction_menu( const bool blueprint )
{
if( !all_constructions.is_finalized() ) {
Expand Down Expand Up @@ -419,6 +447,7 @@ cata::optional<construction_id> construction_menu( const bool blueprint )
ctxt.register_action( "CONFIRM" );
ctxt.register_action( "TOGGLE_UNAVAILABLE_CONSTRUCTIONS" );
ctxt.register_action( "QUIT" );
ctxt.register_action( "TOGGLE_FAVORITE" );
ctxt.register_action( "HELP_KEYBINDINGS" );
ctxt.register_action( "FILTER" );
ctxt.register_action( "RESET_FILTER" );
Expand Down Expand Up @@ -622,8 +651,9 @@ cata::optional<construction_id> construction_menu( const bool blueprint )
if( highlight ) {
cursor_pos = print_from;
}
const std::string group_name = is_favorite( group ) ? "* " + group->name() : group->name();
trim_and_print( w_list, print_from, w_list_width,
construction_color( group, highlight ), group->name() );
construction_color( group, highlight ), group_name );
}

// Clear out lines for tools & materials
Expand Down Expand Up @@ -708,6 +738,9 @@ cata::optional<construction_id> construction_menu( const bool blueprint )
[&]( const construction_group_str_id & group ) {
return lcmatch( group->name(), filter );
} );
} else if( category_id == construction_category_FAVORITE ) {
constructs.clear();
std::copy_if( available.begin(), available.end(), std::back_inserter( constructs ), is_favorite );
} else {
constructs = cat_available[category_id];
}
Expand Down Expand Up @@ -744,6 +777,16 @@ cata::optional<construction_id> construction_menu( const bool blueprint )
_( "Press [<color_red>%s</color>] to show unavailable constructions." ),
ctxt.get_desc( "TOGGLE_UNAVAILABLE_CONSTRUCTIONS" ) ) );
}
if( select >= 0 && static_cast<size_t>( select ) < constructs.size() &&
is_favorite( constructs[select] ) ) {
notes.push_back( string_format(
_( "Press [<color_yellow>%s</color>] to remove from favorites." ),
ctxt.get_desc( "TOGGLE_FAVORITE" ) ) );
} else {
notes.push_back( string_format(
_( "Press [<color_yellow>%s</color>] to add to favorites." ),
ctxt.get_desc( "TOGGLE_FAVORITE" ) ) );
}
notes.push_back( string_format(
_( "Press [<color_yellow>%s</color>] to view and edit keybindings." ),
ctxt.get_desc( "HELP_KEYBINDINGS" ) ) );
Expand Down Expand Up @@ -818,6 +861,19 @@ cata::optional<construction_id> construction_menu( const bool blueprint )
}
} else if( action == "QUIT" ) {
exit = true;
} else if( action == "TOGGLE_FAVORITE" ) {
if( constructs.empty() || select >= static_cast<int>( constructs.size() ) ) {
// Nothing to be done here
continue;
}
update_info = true;
update_cat = true;
const auto &c = constructs[select];
if( is_favorite( c ) ) {
favorite_remove( c );
} else {
favorite_add( c );
}
} else if( action == "TOGGLE_UNAVAILABLE_CONSTRUCTIONS" ) {
update_info = true;
update_cat = true;
Expand Down
2 changes: 2 additions & 0 deletions src/savegame_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4288,6 +4288,7 @@ void uistatedata::serialize( JsonOut &json ) const
json.member( "hidden_recipes", hidden_recipes );
json.member( "favorite_recipes", favorite_recipes );
json.member( "recent_recipes", recent_recipes );
json.member( "favorite_construct_recipes", favorite_construct_recipes );
json.member( "bionic_ui_sort_mode", bionic_sort_mode );
json.member( "overmap_debug_weather", overmap_debug_weather );
json.member( "overmap_visible_weather", overmap_visible_weather );
Expand Down Expand Up @@ -4336,6 +4337,7 @@ void uistatedata::deserialize( const JsonObject &jo )
jo.read( "hidden_recipes", hidden_recipes );
jo.read( "favorite_recipes", favorite_recipes );
jo.read( "recent_recipes", recent_recipes );
jo.read( "favorite_construct_recipes", favorite_construct_recipes );
jo.read( "bionic_ui_sort_mode", bionic_sort_mode );
jo.read( "overmap_debug_weather", overmap_debug_weather );
jo.read( "overmap_visible_weather", overmap_visible_weather );
Expand Down
2 changes: 2 additions & 0 deletions src/uistate.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ class uistatedata
std::set<recipe_id> favorite_recipes;
std::vector<recipe_id> recent_recipes;

std::set<construction_group_str_id> favorite_construct_recipes;

bionic_ui_sort_mode bionic_sort_mode = bionic_ui_sort_mode::POWER;

/* to save input history and make accessible via 'up', you don't need to edit this file, just run:
Expand Down

0 comments on commit 2fb69f3

Please sign in to comment.