Skip to content

Commit df59d87

Browse files
mqrausemqrause
andauthored
refactor inventory_multiselector and remove inventory_iuse_selector (#55072)
Co-authored-by: mqrause <[email protected]>
1 parent ff07260 commit df59d87

File tree

3 files changed

+51
-124
lines changed

3 files changed

+51
-124
lines changed

src/inventory_ui.cpp

Lines changed: 42 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,10 +2633,13 @@ void inventory_selector::highlight()
26332633

26342634
inventory_multiselector::inventory_multiselector( Character &p,
26352635
const inventory_selector_preset &preset,
2636-
const std::string &selection_column_title, const bool allow_select_contained ) :
2636+
const std::string &selection_column_title,
2637+
const GetStats &get_stats,
2638+
const bool allow_select_contained ) :
26372639
inventory_selector( p, preset ),
26382640
allow_select_contained( allow_select_contained ),
2639-
selection_col( new selection_column( "SELECTION_COLUMN", selection_column_title ) )
2641+
selection_col( new selection_column( "SELECTION_COLUMN", selection_column_title ) ),
2642+
get_stats( get_stats )
26402643
{
26412644
ctxt.register_action( "TOGGLE_ENTRY", to_translation( "Mark/unmark selected item" ) );
26422645
ctxt.register_action( "MARK_WITH_COUNT",
@@ -2787,6 +2790,38 @@ void inventory_multiselector::toggle_entries( int &count, const toggle_mode mode
27872790
on_toggle();
27882791
}
27892792

2793+
drop_locations inventory_multiselector::execute()
2794+
{
2795+
shared_ptr_fast<ui_adaptor> ui = create_or_get_ui_adaptor();
2796+
2797+
while( true ) {
2798+
ui_manager::redraw();
2799+
2800+
const inventory_input input = get_input();
2801+
2802+
if( input.action == "CONFIRM" ) {
2803+
if( to_use.empty() ) {
2804+
popup_getkey( _( "No items were selected. Use %s to select them." ),
2805+
ctxt.get_desc( "TOGGLE_ENTRY" ) );
2806+
continue;
2807+
}
2808+
break;
2809+
}
2810+
2811+
if( input.action == "QUIT" ) {
2812+
return drop_locations();
2813+
}
2814+
2815+
on_input( input );
2816+
}
2817+
drop_locations dropped_pos_and_qty;
2818+
for( const std::pair<item_location, int> &drop_pair : to_use ) {
2819+
dropped_pos_and_qty.push_back( drop_pair );
2820+
}
2821+
2822+
return dropped_pos_and_qty;
2823+
}
2824+
27902825
inventory_compare_selector::inventory_compare_selector( Character &p ) :
27912826
inventory_multiselector( p, default_preset, _( "ITEMS TO COMPARE" ) ) {}
27922827

@@ -2824,7 +2859,7 @@ std::pair<const item *, const item *> inventory_compare_selector::execute()
28242859
} else if( input.action == "TOGGLE_FAVORITE" ) {
28252860
// TODO: implement favoriting in multi selection menus while maintaining selection
28262861
} else {
2827-
on_input( input );
2862+
inventory_selector::on_input( input );
28282863
}
28292864

28302865
if( compared.size() == 2 ) {
@@ -2855,79 +2890,7 @@ void inventory_compare_selector::toggle_entry( inventory_entry *entry )
28552890
on_change( *entry );
28562891
}
28572892

2858-
inventory_iuse_selector::inventory_iuse_selector(
2859-
Character &p,
2860-
const std::string &selector_title,
2861-
const inventory_selector_preset &preset,
2862-
const GetStats &get_st
2863-
) :
2864-
inventory_multiselector( p, preset, selector_title, /*allow_select_contained=*/true ),
2865-
get_stats( get_st )
2866-
{}
2867-
2868-
drop_locations inventory_iuse_selector::execute()
2869-
{
2870-
shared_ptr_fast<ui_adaptor> ui = create_or_get_ui_adaptor();
2871-
2872-
auto is_entry = []( const inventory_entry & elem ) {
2873-
return elem.is_selectable();
2874-
};
2875-
for( inventory_column *col : get_all_columns() ) {
2876-
if( col->allows_selecting() ) {
2877-
for( inventory_entry *ie : col->get_entries( is_entry ) ) {
2878-
for( item_location const &x : ie->locations ) {
2879-
usable_locs.push_back( x );
2880-
}
2881-
}
2882-
}
2883-
}
2884-
int count = 0;
2885-
while( true ) {
2886-
ui_manager::redraw();
2887-
2888-
const bool noMarkCountBound = ctxt.keys_bound_to( "MARK_WITH_COUNT" ).empty();
2889-
const inventory_input input = get_input();
2890-
2891-
if( input.entry != nullptr ) { // Single Item from mouse
2892-
highlight( input.entry->any_item() );
2893-
toggle_entries( count );
2894-
} else if( input.action == "TOGGLE_NON_FAVORITE" ) {
2895-
toggle_entries( count, toggle_mode::NON_FAVORITE_NON_WORN );
2896-
} else if( input.action == "MARK_WITH_COUNT" ) { // Set count and mark selected with specific key
2897-
int query_result = query_count();
2898-
if( query_result < 0 ) {
2899-
continue; // Skip selecting any if invalid result or user canceled prompt
2900-
}
2901-
toggle_entries( query_result, toggle_mode::SELECTED );
2902-
} else if( noMarkCountBound && input.ch >= '0' && input.ch <= '9' ) {
2903-
count = std::min( count, INT_MAX / 10 - 10 );
2904-
count *= 10;
2905-
count += input.ch - '0';
2906-
} else if( input.action == "TOGGLE_ENTRY" ) { // Mark selected
2907-
toggle_entries( count, toggle_mode::SELECTED );
2908-
} else if( input.action == "CONFIRM" ) {
2909-
if( to_use.empty() ) {
2910-
popup_getkey( _( "No items were selected. Use %s to select them." ),
2911-
ctxt.get_desc( "TOGGLE_ENTRY" ) );
2912-
continue;
2913-
}
2914-
break;
2915-
} else if( input.action == "QUIT" ) {
2916-
return drop_locations();
2917-
} else {
2918-
on_input( input );
2919-
}
2920-
}
2921-
drop_locations dropped_pos_and_qty;
2922-
2923-
for( const std::pair<const item_location, int> use_pair : to_use ) {
2924-
dropped_pos_and_qty.push_back( use_pair );
2925-
}
2926-
2927-
return dropped_pos_and_qty;
2928-
}
2929-
2930-
inventory_selector::stats inventory_iuse_selector::get_raw_stats() const
2893+
inventory_selector::stats inventory_multiselector::get_raw_stats() const
29312894
{
29322895
if( get_stats ) {
29332896
return get_stats( to_use );
@@ -2985,7 +2948,7 @@ void inventory_multiselector::deselect_contained_items()
29852948
}
29862949
}
29872950

2988-
void inventory_drop_selector::on_input( const inventory_input &input )
2951+
void inventory_multiselector::on_input( const inventory_input &input )
29892952
{
29902953
bool const noMarkCountBound = ctxt.keys_bound_to( "MARK_WITH_COUNT" ).empty();
29912954

@@ -3007,7 +2970,7 @@ void inventory_drop_selector::on_input( const inventory_input &input )
30072970
} else if( input.action == "TOGGLE_ENTRY" ) { // Mark selected
30082971
toggle_entries( count, toggle_mode::SELECTED );
30092972
} else {
3010-
inventory_multiselector::on_input( input );
2973+
inventory_selector::on_input( input );
30112974
}
30122975
}
30132976

@@ -3104,31 +3067,12 @@ drop_locations pickup_selector::execute()
31043067
{
31053068
shared_ptr_fast<ui_adaptor> ui = create_or_get_ui_adaptor();
31063069

3107-
int count = 0;
31083070
while( true ) {
31093071
ui_manager::redraw();
31103072

3111-
const bool noMarkCountBound = ctxt.keys_bound_to( "MARK_WITH_COUNT" ).empty();
31123073
const inventory_input input = get_input();
31133074

3114-
if( input.entry != nullptr ) { // Single Item from mouse
3115-
highlight( input.entry->any_item() );
3116-
toggle_entries( count );
3117-
} else if( input.action == "TOGGLE_NON_FAVORITE" ) {
3118-
toggle_entries( count, toggle_mode::NON_FAVORITE_NON_WORN );
3119-
} else if( input.action == "MARK_WITH_COUNT" ) { // Set count and mark selected with specific key
3120-
int query_result = query_count();
3121-
if( query_result < 0 ) {
3122-
continue; // Skip selecting any if invalid result or user canceled prompt
3123-
}
3124-
toggle_entries( query_result );
3125-
} else if( noMarkCountBound && input.ch >= '0' && input.ch <= '9' ) {
3126-
count = std::min( count, INT_MAX / 10 - 10 );
3127-
count *= 10;
3128-
count += input.ch - '0';
3129-
} else if( input.action == "TOGGLE_ENTRY" ) { // Mark selected
3130-
toggle_entries( count );
3131-
} else if( input.action == "CONFIRM" ) {
3075+
if( input.action == "CONFIRM" ) {
31323076
if( to_use.empty() ) {
31333077
popup_getkey( _( "No items were selected. Use %s to select them." ),
31343078
ctxt.get_desc( "TOGGLE_ENTRY" ) );
@@ -3145,10 +3089,6 @@ drop_locations pickup_selector::execute()
31453089
}
31463090
} else if( input.action == "QUIT" ) {
31473091
return drop_locations();
3148-
} else if( input.action == "INVENTORY_FILTER" ) {
3149-
query_set_filter();
3150-
} else if( input.action == "TOGGLE_FAVORITE" ) {
3151-
// TODO: implement favoriting in multi selection menus while maintaining selection
31523092
} else {
31533093
on_input( input );
31543094
}

src/inventory_ui.h

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -842,10 +842,13 @@ class inventory_pick_selector : public inventory_selector
842842
class inventory_multiselector : public inventory_selector
843843
{
844844
public:
845+
using GetStats = std::function<stats( const std::vector<std::pair<item_location, int>> )>;
845846
explicit inventory_multiselector( Character &p,
846847
const inventory_selector_preset &preset = default_preset,
847848
const std::string &selection_column_title = "",
849+
const GetStats & = {},
848850
bool allow_select_contained = false );
851+
drop_locations execute();
849852
protected:
850853
void rearrange_columns( size_t client_width ) override;
851854
size_t max_chosen_count;
@@ -856,8 +859,12 @@ class inventory_multiselector : public inventory_selector
856859
std::vector<item_location> usable_locs;
857860
bool allow_select_contained;
858861
virtual void on_toggle() {};
862+
void on_input( const inventory_input &input );
863+
int count = 0;
864+
stats get_raw_stats() const override;
859865
private:
860866
std::unique_ptr<inventory_column> selection_col;
867+
GetStats get_stats;
861868
};
862869

863870
class inventory_compare_selector : public inventory_multiselector
@@ -871,25 +878,6 @@ class inventory_compare_selector : public inventory_multiselector
871878
void toggle_entry( inventory_entry *entry );
872879
};
873880

874-
// This and inventory_drop_selectors should probably both inherit from a higher-abstraction "action selector".
875-
// Should accept a function to calculate dummy values.
876-
class inventory_iuse_selector : public inventory_multiselector
877-
{
878-
public:
879-
using GetStats = std::function<stats( const std::vector<std::pair<item_location, int>> )>;
880-
inventory_iuse_selector( Character &p,
881-
const std::string &selector_title,
882-
const inventory_selector_preset &preset = default_preset,
883-
const GetStats & = {} );
884-
drop_locations execute();
885-
886-
protected:
887-
stats get_raw_stats() const override;
888-
889-
private:
890-
GetStats get_stats;
891-
};
892-
893881
class inventory_drop_selector : public inventory_multiselector
894882
{
895883
public:
@@ -899,13 +887,11 @@ class inventory_drop_selector : public inventory_multiselector
899887
const std::string &selection_column_title = _( "ITEMS TO DROP" ),
900888
bool warn_liquid = true );
901889
drop_locations execute();
902-
void on_input( const inventory_input &input );
903890
protected:
904891
stats get_raw_stats() const override;
905892

906893
private:
907894
bool warn_liquid;
908-
int count = 0;
909895
};
910896

911897
class pickup_selector : public inventory_multiselector

src/iuse.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9684,7 +9684,8 @@ cata::optional<int> iuse::wash_items( Character *p, bool soft_items, bool hard_i
96849684
display_stat( _( "Cleanser" ), required.cleanser, available_cleanser, to_string )
96859685
}};
96869686
};
9687-
inventory_iuse_selector inv_s( *p, _( "ITEMS TO CLEAN" ), preset, make_raw_stats );
9687+
inventory_multiselector inv_s( *p, preset, _( "ITEMS TO CLEAN" ),
9688+
make_raw_stats, /*allow_select_contained=*/true );
96889689
inv_s.set_invlet_type( inventory_selector::SELECTOR_INVLET_ALPHA );
96899690
inv_s.add_character_items( *p );
96909691
inv_s.add_nearby_items( PICKUP_RANGE );

0 commit comments

Comments
 (0)