Skip to content

Commit

Permalink
Merge pull request CleverRaven#40807 from KorGgenT/order-items-in-nes…
Browse files Browse the repository at this point in the history
…ted-mode-on-inventory

order items in nested mode by parent
  • Loading branch information
ZhilkinSerg authored May 24, 2020
2 parents 2c5b68a + 505b165 commit 2c49679
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/inventory_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,49 @@ void inventory_column::on_input( const inventory_input &input )
}
}

void inventory_column::order_by_parent()
{
std::vector<inventory_entry> base_entries;
std::vector<inventory_entry> child_entries;
for( const inventory_entry &entry : entries ) {
if( entry.is_item() && entry.locations.front().where() ==
item_location::type::container ) {
child_entries.push_back( entry );
} else {
base_entries.push_back( entry );
}
}

while( !child_entries.empty() ) {
const inventory_entry &possible = child_entries.back();
const item_location parent = possible.locations.front().parent_item();
bool found = false;
for( auto base_entry_iter = base_entries.begin(); base_entry_iter != base_entries.end(); ) {
if( base_entry_iter->is_item() ) {
for( const item_location loc : base_entry_iter->locations ) {
if( loc == parent ) {
base_entries.insert( base_entry_iter + 1, possible );
child_entries.pop_back();
found = true;
break;
}
}
if( found ) {
break;
}
}
++base_entry_iter;
}
if( !found ) {
// move it to the front of the vector to check it again later
child_entries.insert( child_entries.begin(), possible );
child_entries.pop_back();
}
}

entries = base_entries;
}

void inventory_column::add_entry( const inventory_entry &entry )
{
if( std::find( entries.begin(), entries.end(), entry ) != entries.end() ) {
Expand Down Expand Up @@ -1936,6 +1979,7 @@ void inventory_selector::toggle_categorize_contained()
&item_category_id( "ITEMS_WORN" ).obj() );
}
}
own_gear_column.order_by_parent();
own_inv_column.clear();
}

Expand Down
3 changes: 3 additions & 0 deletions src/inventory_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ class inventory_column
std::vector<inventory_entry *> get_entries(
const std::function<bool( const inventory_entry &entry )> &filter_func ) const;

// orders the child entries in this column to be under their parent
void order_by_parent();

inventory_entry *find_by_invlet( int invlet ) const;

void draw( const catacurses::window &win, size_t x, size_t y ) const;
Expand Down

0 comments on commit 2c49679

Please sign in to comment.