Skip to content

Commit

Permalink
Merge pull request #78401 from moxian/examine-stack
Browse files Browse the repository at this point in the history
Deduplicate item contents description in the examine display
  • Loading branch information
Night-Pryanik authored Dec 21, 2024
2 parents f4932f9 + 5db93b3 commit 58256ea
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 32 deletions.
41 changes: 36 additions & 5 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5661,6 +5661,27 @@ void item::melee_combat_info( std::vector<iteminfo> &info, const iteminfo_query
}
}

std::vector<std::pair<const item *, int>> get_item_duplicate_counts(
const std::list<const item *> &items )
{
std::vector<std::pair<item const *, int>> counted_items;
for( const item *contents_item : items ) {
bool found = false;
for( std::pair<const item *, int> &content : counted_items ) {
if( content.first->display_stacked_with( *contents_item ) ) {
content.second += 1;
found = true;
}
}
if( !found ) {
std::pair<const item *, int> new_content( contents_item, 1 );
counted_items.push_back( new_content );
}
}
return counted_items;
}


void item::contents_info( std::vector<iteminfo> &info, const iteminfo_query *parts, int batch,
bool /*debug*/ ) const
{
Expand All @@ -5686,8 +5707,14 @@ void item::contents_info( std::vector<iteminfo> &info, const iteminfo_query *par
info.emplace_back( "DESCRIPTION", mod_str );
info.emplace_back( "DESCRIPTION", mod->type->description.translated() );
}

const std::list<const item *> all_contents = contents.all_items_top();
const std::vector<std::pair<item const *, int>> counted_contents = get_item_duplicate_counts(
all_contents );
bool contents_header = false;
for( const item *contents_item : contents.all_items_top() ) {
for( const std::pair<const item *, int> &content_w_count : counted_contents ) {
const item *contents_item = content_w_count.first;
int count = content_w_count.second;
if( !contents_header ) {
insert_separation_line( info );
info.emplace_back( "DESCRIPTION", _( "<bold>Contents of this item</bold>:" ) );
Expand All @@ -5701,12 +5728,16 @@ void item::contents_info( std::vector<iteminfo> &info, const iteminfo_query *par

if( contents_item->made_of_from_type( phase_id::LIQUID ) ) {
units::volume contents_volume = contents_item->volume() * batch;
info.emplace_back( "DESCRIPTION", colorize( contents_item->display_name(),
contents_item->color_in_inventory() ) );
info.emplace_back( "DESCRIPTION",
colorize( contents_item->display_name(), contents_item->color_in_inventory() ) );
info.emplace_back( vol_to_info( "CONTAINER", description + space, contents_volume ) );
} else {
info.emplace_back( "DESCRIPTION", colorize( contents_item->display_name(),
contents_item->color_in_inventory() ) );
std::string name;
if( count > 1 ) {
name += std::to_string( count ) + " ";
}
name += colorize( contents_item->display_name( count ), contents_item->color_in_inventory() );
info.emplace_back( "DESCRIPTION", name );
info.emplace_back( "DESCRIPTION", description.translated() );
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -3347,3 +3347,12 @@ struct disp_mod_by_barrel {
dispersion_modifier( disp ) {}
void deserialize( const JsonObject &jo );
};

/**
* Given an iterable of `const item* ` (such as obtained from `all_items_top()`),
* returns the vector of each unique item in the iterable, and the amount of times it
* was encountered.
* For display purposes only.
*/
std::vector<std::pair<const item *, int>> get_item_duplicate_counts(
const std::list<const item *> &items );
46 changes: 19 additions & 27 deletions src/item_pocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1268,42 +1268,34 @@ void item_pocket::contents_info( std::vector<iteminfo> &info, int pocket_number,

// ablative pockets have their contents displayed earlier in the UI
if( !is_ablative() ) {
std::vector<std::pair<item const *, int>> counted_contents;
std::vector<std::pair<const item *, int>> counted_contents = get_item_duplicate_counts(
all_items_top() );
bool contents_header = false;
for( const item &contents_item : contents ) {

for( std::pair<item const *, int> content : counted_contents ) {
const item *contents_item = content.first;
const int count = content.second;

if( !contents_header ) {
info.emplace_back( "DESCRIPTION", _( "<bold>Contents of this pocket</bold>:" ) );
contents_header = true;
}

const translation &desc = contents_item.type->description;
const translation &desc = contents_item->type->description;

if( contents_item.made_of_from_type( phase_id::LIQUID ) ) {
info.emplace_back( "DESCRIPTION", colorize( space + contents_item.display_name(),
contents_item.color_in_inventory() ) );
info.emplace_back( vol_to_info( cont_type_str, desc + space, contents_item.volume() ) );
} else {
bool found = false;
for( std::pair<item const *, int> &content : counted_contents ) {
if( content.first->display_stacked_with( contents_item ) ) {
content.second += 1;
found = true;
}
}
if( !found ) {
std::pair<item const *, int> new_content( &contents_item, 1 );
counted_contents.push_back( new_content );
}
}
}
for( std::pair<item const *, int> content : counted_contents ) {
if( content.second > 1 ) {
if( contents_item->made_of_from_type( phase_id::LIQUID ) ) {
info.emplace_back( "DESCRIPTION",
space + std::to_string( content.second ) + " " + colorize( content.first->display_name(
content.second ), content.first->color_in_inventory() ) );
colorize( space + contents_item->display_name(), contents_item->color_in_inventory() ) );
info.emplace_back( vol_to_info( cont_type_str, desc + space, contents_item->volume() ) );
} else {
info.emplace_back( "DESCRIPTION", space + colorize( content.first->display_name(),
content.first->color_in_inventory() ) );
if( count > 1 ) {
info.emplace_back( "DESCRIPTION",
space + std::to_string( count ) + " " + colorize( contents_item->display_name(
count ), contents_item->color_in_inventory() ) );
} else {
info.emplace_back( "DESCRIPTION",
space + colorize( contents_item->display_name(), contents_item->color_in_inventory() ) );
}
}
}
}
Expand Down

0 comments on commit 58256ea

Please sign in to comment.