Skip to content

Commit

Permalink
Migrate vending menu to ui_adaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Qrox committed Apr 26, 2020
1 parent 6381b01 commit 80ace93
Showing 1 changed file with 41 additions and 20 deletions.
61 changes: 41 additions & 20 deletions src/iexamine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,20 +621,37 @@ void iexamine::vending( player &p, const tripoint &examp )
popup( _( "You need some money on a cash card to buy things." ) );
}

const int padding_x = std::max( 0, TERMX - FULL_SCREEN_WIDTH ) / 4;
const int padding_y = std::max( 0, TERMY - FULL_SCREEN_HEIGHT ) / 6;
const int window_h = FULL_SCREEN_HEIGHT + std::max( 0, TERMY - FULL_SCREEN_HEIGHT ) * 2 / 3;
const int window_w = FULL_SCREEN_WIDTH + std::max( 0, TERMX - FULL_SCREEN_WIDTH ) / 2;
const int w_items_w = window_w / 2;
const int w_info_w = window_w - w_items_w;
const int list_lines = window_h - 4; // minus for header and footer

int w_items_w = 0;
int w_info_w = 0;
int list_lines = 0;
int lines_above = 0;
int lines_below = 0;
constexpr int first_item_offset = 3; // header size

catacurses::window const w = catacurses::newwin( window_h, w_items_w, point( padding_x,
padding_y ) );
catacurses::window const w_item_info = catacurses::newwin( window_h, w_info_w,
point( padding_x + w_items_w, padding_y ) );
catacurses::window w;
catacurses::window w_item_info;

ui_adaptor ui;
ui.on_screen_resize( [&]( ui_adaptor & ui ) {
const int padding_x = std::max( 0, TERMX - FULL_SCREEN_WIDTH ) / 4;
const int padding_y = std::max( 0, TERMY - FULL_SCREEN_HEIGHT ) / 6;
const int window_h = FULL_SCREEN_HEIGHT + std::max( 0, TERMY - FULL_SCREEN_HEIGHT ) * 2 / 3;
const int window_w = FULL_SCREEN_WIDTH + std::max( 0, TERMX - FULL_SCREEN_WIDTH ) / 2;
w_items_w = window_w / 2;
w_info_w = window_w - w_items_w;
list_lines = window_h - 4; // minus for header and footer

lines_above = list_lines / 2; // lines above the selector
lines_below = list_lines / 2 + list_lines % 2; // lines below the selector

w = catacurses::newwin( window_h, w_items_w,
point( padding_x, padding_y ) );
w_item_info = catacurses::newwin( window_h, w_info_w,
point( padding_x + w_items_w, padding_y ) );

ui.position( point( padding_x, padding_y ), point( window_w, window_h ) );
} );
ui.mark_resize();

bool used_machine = false;
input_context ctxt( "VENDING_MACHINE" );
Expand All @@ -651,7 +668,7 @@ void iexamine::vending( player &p, const tripoint &examp )
for( auto it = std::begin( vend_items ); it != std::end( vend_items ); ++it ) {
// |# {name}|
// 123 4
item_map[utf8_truncate( it->tname(), static_cast<size_t>( w_items_w - 4 ) )].push_back( it );
item_map[it->tname()].push_back( it );
}

// Next, put pointers to the pairs in the map in a vector to allow indexing.
Expand All @@ -661,14 +678,8 @@ void iexamine::vending( player &p, const tripoint &examp )
item_list.emplace_back( &pair );
}

const int lines_above = list_lines / 2; // lines above the selector
const int lines_below = list_lines / 2 + list_lines % 2; // lines below the selector

// FIXME: temporarily disable redrawing of lower UIs before this UI is migrated to `ui_adaptor`
ui_adaptor ui( ui_adaptor::disable_uis_below {} );

int cur_pos = 0;
for( ;; ) {
ui.on_redraw( [&]( const ui_adaptor & ) {
const int num_items = item_list.size();
const int page_size = std::min( num_items, list_lines );

Expand Down Expand Up @@ -721,6 +732,16 @@ void iexamine::vending( player &p, const tripoint &examp )
static_cast<size_t>( w_info_w - 4 ) );
mvwprintw( w_item_info, point_east, "<%s>", name );
wrefresh( w_item_info );
} );

for( ;; ) {
ui_manager::redraw();

const int num_items = item_list.size();

// Item info
auto &cur_items = item_list[static_cast<size_t>( cur_pos )]->second;
auto &cur_item = cur_items.back();

const std::string &action = ctxt.handle_input();
if( action == "DOWN" ) {
Expand Down

0 comments on commit 80ace93

Please sign in to comment.