From 6bde81ec05752b4f113d9766a9e0a762c3fe0015 Mon Sep 17 00:00:00 2001 From: Qrox Date: Sun, 26 Apr 2020 21:47:41 +0800 Subject: [PATCH] Migrate martial arts info to ui_adaptor --- src/martialarts.cpp | 54 +++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/src/martialarts.cpp b/src/martialarts.cpp index 231a048894b9c..43a012eb8b89f 100644 --- a/src/martialarts.cpp +++ b/src/martialarts.cpp @@ -1475,24 +1475,49 @@ bool ma_style_callback::key( const input_context &ctxt, const input_event &event buffer += enumerate_as_string( weapons ); } - catacurses::window w = catacurses::newwin( FULL_SCREEN_HEIGHT, FULL_SCREEN_WIDTH, - point( TERMX > FULL_SCREEN_WIDTH ? ( TERMX - FULL_SCREEN_WIDTH ) / 2 : 0, - TERMY > FULL_SCREEN_HEIGHT ? ( TERMY - FULL_SCREEN_HEIGHT ) / 2 : 0 ) ); - - std::string text = replace_colors( buffer ); - int width = FULL_SCREEN_WIDTH - 4; - int height = FULL_SCREEN_HEIGHT - 2; - const auto vFolded = foldstring( text, width ); - int iLines = vFolded.size(); + catacurses::window w; + + const std::string text = replace_colors( buffer ); + int width = 0; + int height = 0; + int iLines = 0; int selected = 0; + ui_adaptor ui; + ui.on_screen_resize( [&]( ui_adaptor & ui ) { + w = catacurses::newwin( FULL_SCREEN_HEIGHT, FULL_SCREEN_WIDTH, + point( TERMX > FULL_SCREEN_WIDTH ? ( TERMX - FULL_SCREEN_WIDTH ) / 2 : 0, + TERMY > FULL_SCREEN_HEIGHT ? ( TERMY - FULL_SCREEN_HEIGHT ) / 2 : 0 ) ); + + width = FULL_SCREEN_WIDTH - 4; + height = FULL_SCREEN_HEIGHT - 2; + + const auto vFolded = foldstring( text, width ); + iLines = vFolded.size(); + + if( iLines < height ) { + selected = 0; + } else if( selected >= iLines - height ) { + selected = iLines - height; + } + + ui.position_from_window( w ); + } ); + ui.mark_resize(); + input_context ict; ict.register_action( "UP" ); ict.register_action( "DOWN" ); ict.register_action( "QUIT" ); + ict.register_action( "HELP_KEYBINDINGS" ); - // FIXME: temporarily disable redrawing of lower UIs before this UI is migrated to `ui_adaptor` - ui_adaptor ui( ui_adaptor::disable_uis_below {} ); + ui.on_redraw( [&]( const ui_adaptor & ) { + werase( w ); + fold_and_print_from( w, point( 2, 1 ), width, selected, c_light_gray, text ); + draw_border( w, BORDER_COLOR, string_format( _( " Style: %s " ), ma.name ) ); + draw_scrollbar( w, selected, height, iLines, point_south, BORDER_COLOR, true ); + wrefresh( w ); + } ); do { if( selected < 0 ) { @@ -1503,12 +1528,7 @@ bool ma_style_callback::key( const input_context &ctxt, const input_event &event selected = iLines - height; } - werase( w ); - fold_and_print_from( w, point( 2, 1 ), width, selected, c_light_gray, text ); - draw_border( w, BORDER_COLOR, string_format( _( " Style: %s " ), ma.name ) ); - draw_scrollbar( w, selected, height, iLines, point_south, BORDER_COLOR, true ); - wrefresh( w ); - catacurses::refresh(); + ui_manager::redraw(); std::string action = ict.handle_input();