Skip to content

Commit 4c7d32e

Browse files
committed
Dedupe faction ui list drawing
1 parent cd59a1e commit 4c7d32e

File tree

1 file changed

+27
-37
lines changed

1 file changed

+27
-37
lines changed

src/faction.cpp

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -943,17 +943,21 @@ void faction_manager::display() const
943943
// entries_per_page * page number
944944
const size_t top_of_page = entries_per_page * ( selection / entries_per_page );
945945

946+
const auto draw_list = [&]( const std::function<const std::string( size_t )> &text ) {
947+
draw_scrollbar( w_missions, selection, entries_per_page, active_vec_size, point( 0, 3 ) );
948+
for( size_t i = top_of_page; i < active_vec_size && i < top_of_page + entries_per_page; i++ ) {
949+
const int y = i - top_of_page + 3;
950+
trim_and_print( w_missions, point( 1, y ), 28, selection == i ? hilite( col ) : col, text( i ) );
951+
}
952+
};
953+
946954
switch( tab ) {
947955
case tab_mode::TAB_MYFACTION: {
948956
const std::string no_camp = _( "You have no camps" );
949957
if( active_vec_size > 0 ) {
950-
draw_scrollbar( w_missions, selection, entries_per_page, active_vec_size,
951-
point( 0, 3 ) );
952-
for( size_t i = top_of_page; i < active_vec_size && i < top_of_page + entries_per_page; i++ ) {
953-
const int y = i - top_of_page + 3;
954-
trim_and_print( w_missions, point( 1, y ), 28, selection == i ? hilite( col ) : col,
955-
camps[i]->camp_name() );
956-
}
958+
draw_list( std::function( [&camps]( size_t i ) {
959+
return camps[i]->camp_name();
960+
} ) );
957961
if( camp ) {
958962
camp->faction_display( w_missions, 31 );
959963
} else {
@@ -968,13 +972,9 @@ void faction_manager::display() const
968972
case tab_mode::TAB_FOLLOWERS: {
969973
const std::string no_ally = _( "You have no followers" );
970974
if( !followers.empty() ) {
971-
draw_scrollbar( w_missions, selection, entries_per_page, active_vec_size,
972-
point( 0, 3 ) );
973-
for( size_t i = top_of_page; i < active_vec_size && i < top_of_page + entries_per_page; i++ ) {
974-
const int y = i - top_of_page + 3;
975-
trim_and_print( w_missions, point( 1, y ), 28, selection == i ? hilite( col ) : col,
976-
followers[i]->disp_name() );
977-
}
975+
draw_list( std::function( [&followers]( size_t i ) {
976+
return followers[i]->disp_name();
977+
} ) );
978978
if( guy ) {
979979
int retval = guy->faction_display( w_missions, 31 );
980980
if( retval == 2 ) {
@@ -994,13 +994,9 @@ void faction_manager::display() const
994994
case tab_mode::TAB_OTHERFACTIONS: {
995995
const std::string no_fac = _( "You don't know of any factions." );
996996
if( active_vec_size > 0 ) {
997-
draw_scrollbar( w_missions, selection, entries_per_page, active_vec_size,
998-
point( 0, 3 ) );
999-
for( size_t i = top_of_page; i < active_vec_size && i < top_of_page + entries_per_page; i++ ) {
1000-
const int y = i - top_of_page + 3;
1001-
trim_and_print( w_missions, point( 1, y ), 28, selection == i ? hilite( col ) : col,
1002-
_( valfac[i]->name ) );
1003-
}
997+
draw_list( std::function( [&valfac]( size_t i ) {
998+
return _( valfac[i]->name );
999+
} ) );
10041000
if( cur_fac ) {
10051001
cur_fac->faction_display( w_missions, 31 );
10061002
} else {
@@ -1015,13 +1011,9 @@ void faction_manager::display() const
10151011
case tab_mode::TAB_LORE: {
10161012
const std::string no_lore = _( "You haven't learned anything about the world." );
10171013
if( active_vec_size > 0 ) {
1018-
draw_scrollbar( w_missions, selection, entries_per_page, active_vec_size,
1019-
point( 0, 3 ) );
1020-
for( size_t i = top_of_page; i < active_vec_size && i < top_of_page + entries_per_page; i++ ) {
1021-
const int y = i - top_of_page + 3;
1022-
trim_and_print( w_missions, point( 1, y ), 28, selection == i ? hilite( col ) : col,
1023-
_( lore[i].second ) );
1024-
}
1014+
draw_list( std::function( [&lore]( size_t i ) {
1015+
return _( lore[i].second );
1016+
} ) );
10251017
if( snippet != nullptr ) {
10261018
int y = 2;
10271019
fold_and_print( w_missions, point( 31, ++y ), getmaxx( w_missions ) - 31 - 2, c_light_gray,
@@ -1040,15 +1032,13 @@ void faction_manager::display() const
10401032
_( "You haven't recorded sightings of any creatures. Taking photos can be a good way to keep track of them." );
10411033
const int w = getmaxx( w_missions ) - 31 - 2;
10421034
if( active_vec_size > 0 ) {
1043-
draw_scrollbar( w_missions, selection, entries_per_page, active_vec_size,
1044-
point( 0, 3 ) );
1045-
for( size_t i = top_of_page; i < active_vec_size && i < top_of_page + entries_per_page; i++ ) {
1046-
const int y = i - top_of_page + 3;
1047-
trim_and_print( w_missions, point( 1, y ), 28, selection == i ? hilite( col ) : col,
1048-
string_format( "%s %s", colorize( creatures[i]->sym,
1049-
selection == i ? hilite( creatures[i]->color ) : creatures[i]->color ),
1050-
creatures[i]->nname() ) );
1051-
}
1035+
draw_list( std::function( [&creatures, &selection]( size_t i ) {
1036+
return string_format( "%s %s",
1037+
colorize( creatures[i]->sym, selection == i ?
1038+
hilite( creatures[i]->color ) :
1039+
creatures[i]->color ),
1040+
creatures[i]->nname() );
1041+
} ) );
10521042
if( !cur_creature.is_null() ) {
10531043
cur_creature->faction_display( w_missions, point( 31, 3 ), w );
10541044
} else {

0 commit comments

Comments
 (0)