From 505b1659cc28597ace69f1632d336b8bfb299307 Mon Sep 17 00:00:00 2001 From: KorGgenT Date: Sun, 24 May 2020 17:08:58 -0400 Subject: [PATCH] order items in nested mode by parent --- src/inventory_ui.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/inventory_ui.h | 3 +++ 2 files changed, 47 insertions(+) diff --git a/src/inventory_ui.cpp b/src/inventory_ui.cpp index cc8f5790aa4af..5dbb89a0469f2 100644 --- a/src/inventory_ui.cpp +++ b/src/inventory_ui.cpp @@ -700,6 +700,49 @@ void inventory_column::on_input( const inventory_input &input ) } } +void inventory_column::order_by_parent() +{ + std::vector base_entries; + std::vector 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() ) { @@ -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(); } diff --git a/src/inventory_ui.h b/src/inventory_ui.h index 661d384e379a8..c3acfa15b5d5e 100644 --- a/src/inventory_ui.h +++ b/src/inventory_ui.h @@ -285,6 +285,9 @@ class inventory_column std::vector get_entries( const std::function &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;