Skip to content

Commit 683bfed

Browse files
authored
Merge pull request #82323 from sparr/dedupe_faction_list_drawing
Dedupe faction ui list drawing
2 parents d9a9d42 + 4c7d32e commit 683bfed

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
@@ -942,17 +942,21 @@ void faction_manager::display() const
942942
// entries_per_page * page number
943943
const size_t top_of_page = entries_per_page * ( selection / entries_per_page );
944944

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

0 commit comments

Comments
 (0)