Skip to content

Commit

Permalink
Refactor non-waiting popups using ui_adaptor (#39054)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qrox authored and ZhilkinSerg committed Apr 1, 2020
1 parent de146cb commit 9a46787
Show file tree
Hide file tree
Showing 16 changed files with 205 additions and 104 deletions.
45 changes: 25 additions & 20 deletions src/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
#include "options.h"
#include "output.h"
#include "path_info.h"
#include "popup.h"
#include "translations.h"
#include "trap.h"
#include "ui.h"
#include "ui_manager.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "creature.h"
Expand Down Expand Up @@ -1030,35 +1032,38 @@ cata::optional<tripoint> choose_direction( const std::string &message, const boo
ctxt.register_directions();
ctxt.register_action( "pause" );
ctxt.register_action( "QUIT" );
// why not?
ctxt.register_action( "HELP_KEYBINDINGS" );
if( allow_vertical ) {
ctxt.register_action( "LEVEL_UP" );
ctxt.register_action( "LEVEL_DOWN" );
}

//~ appended to "Close where?" "Pry where?" etc.
const std::string query_text = message + _( " (Direction button)" );
popup( query_text, PF_NO_WAIT_ON_TOP );
static_popup popup;
//~ %s: "Close where?" "Pry where?" etc.
popup.message( _( "%s (Direction button)" ), message ).on_top( true );

const std::string action = ctxt.handle_input();
if( const cata::optional<tripoint> vec = ctxt.get_direction( action ) ) {
// Make player's sprite face left/right if interacting with something to the left or right
if( vec->x > 0 ) {
g->u.facing = FD_RIGHT;
} else if( vec->x < 0 ) {
g->u.facing = FD_LEFT;
std::string action;
do {
ui_manager::redraw();
action = ctxt.handle_input();
if( const cata::optional<tripoint> vec = ctxt.get_direction( action ) ) {
// Make player's sprite face left/right if interacting with something to the left or right
if( vec->x > 0 ) {
g->u.facing = FD_RIGHT;
} else if( vec->x < 0 ) {
g->u.facing = FD_LEFT;
}
return vec;
} else if( action == "pause" ) {
return tripoint_zero;
} else if( action == "LEVEL_UP" ) {
return tripoint_above;
} else if( action == "LEVEL_DOWN" ) {
return tripoint_below;
}
return vec;
} else if( action == "pause" ) {
return tripoint_zero;
} else if( action == "LEVEL_UP" ) {
return tripoint_above;
} else if( action == "LEVEL_DOWN" ) {
return tripoint_below;
}
} while( action != "QUIT" );

add_msg( _( "Invalid direction." ) );
add_msg( _( "Never mind." ) );
return cata::nullopt;
}

Expand Down
40 changes: 31 additions & 9 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,16 @@ void game::reenter_fullscreen()
*/
void game::setup()
{
popup_status( _( "Please wait while the world data loads…" ), _( "Loading core data" ) );
loading_ui ui( true );
load_core_data( ui );
{
background_pane background;
static_popup popup;
popup.message( "%s", _( "Please wait while the world data loads…\nLoading core data" ) );
ui_manager::redraw();
refresh_display();

load_core_data( ui );
}

load_world_modfiles( ui );

Expand Down Expand Up @@ -696,9 +703,12 @@ bool game::start_game()

init_autosave();

catacurses::clear();
catacurses::refresh();
popup_nowait( _( "Please wait as we build your world" ) );
background_pane background;
static_popup popup;
popup.message( "%s", _( "Please wait as we build your world" ) );
ui_manager::redraw();
refresh_display();

load_master();
u.setID( assign_npc_id() ); // should be as soon as possible, but *after* load_master

Expand Down Expand Up @@ -2691,7 +2701,11 @@ bool game::load( const std::string &world )

void game::load( const save_t &name )
{
popup_status( _( "Please wait…" ), _( "Loading the save…" ) );
background_pane background;
static_popup popup;
popup.message( "%s", _( "Please wait…\nLoading the save…" ) );
ui_manager::redraw();
refresh_display();

using namespace std::placeholders;

Expand Down Expand Up @@ -2830,9 +2844,13 @@ bool game::load_packs( const std::string &msg, const std::vector<mod_id> &packs,

// if mod specifies legacy migrations load any that are required
if( !mod.legacy.empty() ) {
static_popup popup;
for( int i = get_option<int>( "CORE_VERSION" ); i < core_version; ++i ) {
popup_status( msg.c_str(), _( "Applying legacy migration (%s %i/%i)" ),
e.c_str(), i, core_version - 1 );
popup.message( _( "%s Applying legacy migration (%s %i/%i)" ),
msg, e.c_str(), i, core_version - 1 );
ui_manager::redraw();
refresh_display();

load_data_from_dir( string_format( "%s/%i", mod.legacy.c_str(), i ), mod.ident.str(), ui );
}
}
Expand Down Expand Up @@ -11166,7 +11184,11 @@ void game::quicksave()
return;
}
add_msg( m_info, _( "Saving game, this may take a while" ) );
popup_nowait( _( "Saving game, this may take a while" ) );

static_popup popup;
popup.message( "%s", _( "Saving game, this may take a while" ) );
ui_manager::redraw();
refresh_display();

time_t now = time( nullptr ); //timestamp for start of saving procedure

Expand Down
13 changes: 11 additions & 2 deletions src/gamemode_defense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
#include "overmap.h"
#include "overmapbuffer.h"
#include "player.h"
#include "popup.h"
#include "rng.h"
#include "string_formatter.h"
#include "translations.h"
#include "ui_manager.h"
#include "cursesdef.h"
#include "game_constants.h"
#include "item.h"
Expand Down Expand Up @@ -114,7 +116,6 @@ bool defense_game::init()
init_to_style( DEFENSE_EASY );
setup();
g->u.cash = initial_cash;
popup_nowait( _( "Please wait as the map generates [ 0%% ]" ) );
// TODO: support multiple defense games? clean up old defense game
defloc_pos = tripoint( 50, 50, 0 );
init_map();
Expand Down Expand Up @@ -214,6 +215,12 @@ void defense_game::init_constructions()

void defense_game::init_map()
{
background_pane background;
static_popup popup;
popup.message( _( "Please wait as the map generates [%2d%%]" ), 0 );
ui_manager::redraw();
refresh_display();

auto &starting_om = overmap_buffer.get( point_zero );
for( int x = 0; x < OMAPX; x++ ) {
for( int y = 0; y < OMAPY; y++ ) {
Expand Down Expand Up @@ -263,7 +270,9 @@ void defense_game::init_map()
int percent = 100 * ( ( j / 2 + MAPSIZE * ( i / 2 ) ) ) /
( ( MAPSIZE ) * ( MAPSIZE + 1 ) );
if( percent >= old_percent + 1 ) {
popup_nowait( _( "Please wait as the map generates [%2d%%]" ), percent );
popup.message( _( "Please wait as the map generates [%2d%%]" ), percent );
ui_manager::redraw();
refresh_display();
old_percent = percent;
}
// Round down to the nearest even number
Expand Down
10 changes: 1 addition & 9 deletions src/loading_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,7 @@ void loading_ui::new_context( const std::string &desc )
void loading_ui::init()
{
if( menu != nullptr && ui == nullptr ) {
ui_background = std::make_unique<ui_adaptor>();
ui_background->on_screen_resize( []( ui_adaptor & ui_background ) {
ui_background.position_from_window( catacurses::stdscr );
} );
ui_background->position_from_window( catacurses::stdscr );
ui_background->on_redraw( []( const ui_adaptor & ) {
catacurses::erase();
catacurses::refresh();
} );
ui_background = std::make_unique<background_pane>();

ui = std::make_unique<ui_adaptor>();
ui->on_screen_resize( [this]( ui_adaptor & ui ) {
Expand Down
3 changes: 2 additions & 1 deletion src/loading_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vector>
#include <string>

class background_pane;
class ui_adaptor;
class uilist;

Expand All @@ -14,7 +15,7 @@ class loading_ui
private:
std::unique_ptr<uilist> menu;
std::unique_ptr<ui_adaptor> ui;
std::unique_ptr<ui_adaptor> ui_background;
std::unique_ptr<background_pane> ui_background;

void init();
public:
Expand Down
12 changes: 3 additions & 9 deletions src/main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,15 +453,7 @@ bool main_menu::opening_screen()
sel1 = 2;
}

ui_adaptor background;
background.on_redraw( []( const ui_adaptor & ) {
catacurses::erase();
catacurses::refresh();
} );
background.on_screen_resize( []( ui_adaptor & background ) {
background.position_from_window( catacurses::stdscr );
} );
background.position_from_window( catacurses::stdscr );
background_pane background;

ui_adaptor ui;
ui.on_redraw( [&]( const ui_adaptor & ) {
Expand Down Expand Up @@ -996,6 +988,8 @@ bool main_menu::load_character_tab( bool transfer )
}
wrefresh( w_open );
} else if( layer == 3 && sel1 == 2 ) {
savegames = world_generator->get_world( all_worldnames[sel2] )->world_saves;

const std::string &wn = all_worldnames[sel2];

mvwprintz( w_open, menu_offset + point( offset_x + extra_w / 2, -2 - sel2 + offset_y ), h_white,
Expand Down
10 changes: 8 additions & 2 deletions src/mapbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#include "json.h"
#include "map.h"
#include "output.h"
#include "popup.h"
#include "submap.h"
#include "translations.h"
#include "ui_manager.h"
#include "game_constants.h"

#define dbg(x) DebugLog((x),D_MAP) << __FILE__ << ":" << __LINE__ << ": "
Expand Down Expand Up @@ -124,14 +126,18 @@ void mapbuffer::save( bool delete_after_save )
const tripoint map_origin = sm_to_omt_copy( g->m.get_abs_sub() );
const bool map_has_zlevels = g != nullptr && g->m.has_zlevels();

static_popup popup;

// A set of already-saved submaps, in global overmap coordinates.
std::set<tripoint> saved_submaps;
std::list<tripoint> submaps_to_delete;
int next_report = 0;
for( auto &elem : submaps ) {
if( num_total_submaps > 100 && num_saved_submaps >= next_report ) {
popup_nowait( _( "Please wait as the map saves [%d/%d]" ),
num_saved_submaps, num_total_submaps );
popup.message( _( "Please wait as the map saves [%d/%d]" ),
num_saved_submaps, num_total_submaps );
ui_manager::redraw();
refresh_display();
next_report += std::max( 100, num_total_submaps / 20 );
}

Expand Down
7 changes: 6 additions & 1 deletion src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mapsharing.h"
#include "output.h"
#include "path_info.h"
#include "popup.h"
#include "sdlsound.h"
#include "sdltiles.h"
#include "sounds.h"
Expand Down Expand Up @@ -2848,7 +2849,11 @@ std::string options_manager::show( bool ingame, const bool world_options_only,

if( options_changed ) {
if( query_yn( _( "Save changes?" ) ) ) {
popup_status( _( "Please wait…" ), _( "Applying option changes…" ) );
static_popup popup;
popup.message( "%s", _( "Please wait…\nApplying option changes…" ) );
ui_manager::redraw();
refresh_display();

save();
if( ingame && world_options_changed ) {
world_generator->active_world->WORLD_OPTIONS = ACTIVE_WORLD_OPTIONS;
Expand Down
28 changes: 5 additions & 23 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,31 +663,13 @@ int popup( const std::string &text, PopupFlags flags )
pop.on_top( true );
}

if( flags & PF_NO_WAIT ) {
pop.show();
catacurses::refresh();
refresh_display();
return UNKNOWN_UNICODE;
pop.context( "POPUP_WAIT" );
const auto &res = pop.query();
if( res.evt.type == CATA_INPUT_KEYBOARD ) {
return res.evt.get_first_input();
} else {
pop.context( "POPUP_WAIT" );
const auto &res = pop.query();
if( res.evt.type == CATA_INPUT_KEYBOARD ) {
return res.evt.get_first_input();
} else {
return UNKNOWN_UNICODE;
}
}
}

void popup_status( const char *const title, const std::string &mes )
{
std::string text;
if( !test_mode && title != nullptr ) {
text += title;
text += "\n";
return UNKNOWN_UNICODE;
}

popup( text + mes, PF_NO_WAIT );
}

//note that passing in iteminfo instances with sType == "DESCRIPTION" does special things
Expand Down
17 changes: 0 additions & 17 deletions src/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,6 @@ std::vector<std::string> get_hotkeys( const std::string &s );
* - PF_GET_KEY (ignored when combined with PF_NO_WAIT) cancels the popup on *any* user input.
* Without the flag the popup is only canceled when the user enters new-line, Space and Escape.
* This flag is passed by @ref popup_getkey.
* - PF_NO_WAIT displays the popup, but does not wait for the user input. The popup window is
* immediately destroyed (but will be visible until another window is redrawn over it).
* The function always returns 0 upon this flag, no call to `getch` is done at all.
* This flag is passed by @ref popup_nowait.
* - PF_ON_TOP makes the window appear on the top of the screen (at the upper most row). Without
* this flag, the popup is centered on the screen.
* The flag is passed by @ref popup_top.
Expand All @@ -420,10 +416,8 @@ std::vector<std::string> get_hotkeys( const std::string &s );
enum PopupFlags {
PF_NONE = 0,
PF_GET_KEY = 1 << 0,
PF_NO_WAIT = 1 << 1,
PF_ON_TOP = 1 << 2,
PF_FULLSCREEN = 1 << 3,
PF_NO_WAIT_ON_TOP = PF_NO_WAIT | PF_ON_TOP,
};

template<typename ...Args>
Expand All @@ -437,17 +431,6 @@ inline void popup_top( const char *const mes, Args &&... args )
popup( string_format( mes, std::forward<Args>( args )... ), PF_ON_TOP );
}
template<typename ...Args>
inline void popup_nowait( const char *mes, Args &&... args )
{
popup( string_format( mes, std::forward<Args>( args )... ), PF_NO_WAIT );
}
void popup_status( const char *title, const std::string &mes );
template<typename ...Args>
inline void popup_status( const char *const title, const char *const fmt, Args &&... args )
{
return popup_status( title, string_format( fmt, std::forward<Args>( args )... ) );
}
template<typename ...Args>
inline void popup( const char *mes, Args &&... args )
{
popup( string_format( mes, std::forward<Args>( args )... ), PF_NONE );
Expand Down
Loading

0 comments on commit 9a46787

Please sign in to comment.