Skip to content

Commit

Permalink
use imgui input popups in some places
Browse files Browse the repository at this point in the history
  • Loading branch information
mqrause committed Dec 2, 2024
1 parent 0806c52 commit 6ce41e6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 96 deletions.
17 changes: 7 additions & 10 deletions src/auto_note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
#include "flexbuffer_json.h"
#include "generic_factory.h"
#include "input_context.h"
#include "input_popup.h"
#include "json.h"
#include "map_extras.h"
#include "options.h"
#include "output.h"
#include "path_info.h"
#include "point.h"
#include "string_formatter.h"
#include "string_input_popup.h"
#include "translation.h"
#include "translations.h"
#include "ui.h"
Expand Down Expand Up @@ -510,16 +510,13 @@ void auto_note_manager_gui::show()
entry.second = false;
( bCharacter ? charwasChanged : globalwasChanged ) = true;
} else if( action == "CHANGE_MAPEXTRA_CHARACTER" ) {
string_input_popup custom_symbol_popup;
custom_symbol_popup
.title( _( "Enter a map extra custom symbol (empty to unset):" ) )
.width( 2 )
.query_string();

if( !custom_symbol_popup.canceled() ) {
const std::string &custom_symbol_str = custom_symbol_popup.text();
if( custom_symbol_str.empty() ) {
string_input_popup_imgui custom_symbol_popup( 0 );
custom_symbol_popup.set_label( _( "Enter a map extra custom symbol (empty to unset):" ) );
custom_symbol_popup.set_max_input_length( 2 );
const std::string &custom_symbol_str = custom_symbol_popup.query();

if( !custom_symbol_popup.cancelled() ) {
if( custom_symbol_str.empty() ) {
( bCharacter ? char_custom_symbol_cache : global_custom_symbol_cache ).erase( currentItem );
} else {
uilist ui_colors;
Expand Down
59 changes: 19 additions & 40 deletions src/auto_pickup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "flexbuffer_json-inl.h"
#include "flexbuffer_json.h"
#include "input_context.h"
#include "input_popup.h"
#include "item.h"
#include "item_factory.h"
#include "item_location.h"
Expand All @@ -35,7 +36,6 @@
#include "path_info.h"
#include "point.h"
#include "string_formatter.h"
#include "string_input_popup.h"
#include "translations.h"
#include "type_id.h"
#include "ui.h"
Expand Down Expand Up @@ -517,45 +517,24 @@ void user_interface::show()
ui_manager::redraw();

if( bLeftColumn || action == "ADD_RULE" ) {
ui_adaptor help_ui;
catacurses::window w_help;
const auto init_help_window = [&]( ui_adaptor & help_ui ) {
const point iOffset( TERMX > FULL_SCREEN_WIDTH ? ( TERMX - FULL_SCREEN_WIDTH ) / 2 : 0,
TERMY > FULL_SCREEN_HEIGHT ? ( TERMY - FULL_SCREEN_HEIGHT ) / 2 : 0 );
w_help = catacurses::newwin( FULL_SCREEN_HEIGHT / 2 + 2,
FULL_SCREEN_WIDTH * 3 / 4,
iOffset + point( 19 / 2, 7 + FULL_SCREEN_HEIGHT / 2 / 2 ) );
help_ui.position_from_window( w_help );
};
init_help_window( help_ui );
help_ui.on_screen_resize( init_help_window );

help_ui.on_redraw( [&]( const ui_adaptor & ) {
// NOLINTNEXTLINE(cata-use-named-point-constants)
fold_and_print( w_help, point( 1, 1 ), 999, c_white,
_(
"* is used as a Wildcard. A few Examples:\n"
"\n"
"wooden arrow matches the itemname exactly\n"
"wooden ar* matches items beginning with wood ar\n"
"*rrow matches items ending with rrow\n"
"*avy fle*fi*arrow multiple * are allowed\n"
"heAVY*woOD*arrOW case insensitive search\n"
"\n"
"Pickup based on item materials:\n"
"m:kevlar matches items made of Kevlar\n"
"M:copper matches items made purely of copper\n"
"M:steel,iron multiple materials allowed (OR search)" )
);

draw_border( w_help );
wnoutrefresh( w_help );
} );
const std::string r = string_input_popup()
.title( _( "Pickup Rule:" ) )
.width( 30 )
.text( cur_rules[iLine].sRule )
.query_string();
string_input_popup_imgui popup( 60, cur_rules[iLine].sRule );
popup.set_label( _( "Pickup Rule:" ) );
std::string description = _(
"* is used as a Wildcard. A few Examples:\n"
"\n"
"wooden arrow matches the itemname exactly\n"
"wooden ar* matches items beginning with wood ar\n"
"*rrow matches items ending with rrow\n"
"*avy fle*fi*arrow multiple * are allowed\n"
"heAVY*woOD*arrOW case insensitive search\n"
"\n"
"Pickup based on item materials:\n"
"m:kevlar matches items made of Kevlar\n"
"M:copper matches items made purely of copper\n"
"M:steel,iron multiple materials allowed (OR search)"
);
popup.set_description( description, c_white, true );
const std::string r = popup.query();
// If r is empty, then either (1) The player ESC'ed from the window (changed their mind), or
// (2) Explicitly entered an empty rule- which isn't allowed since "*" should be used
// to include/exclude everything
Expand Down
16 changes: 7 additions & 9 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
#include "input.h"
#include "input_context.h"
#include "input_enums.h"
#include "input_popup.h"
#include "inventory.h"
#include "item.h"
#include "item_category.h"
Expand Down Expand Up @@ -8627,15 +8628,12 @@ game::vmenu_ret game::list_items( const std::vector<map_item_stack> &item_list )
recalc_unread = highlight_unread_items;
} else if( action == "FILTER" ) {
ui.invalidate_ui();
string_input_popup()
.title( _( "Filter:" ) )
.width( 55 )
.description( item_filter_rule_string( item_filter_type::FILTER ) + "\n\n"
+ list_items_filter_history_help() )
.desc_color( c_white )
.identifier( "item_filter" )
.max_length( 256 )
.edit( sFilter );
string_input_popup_imgui imgui_popup( 70, sFilter, _( "Filter items" ) );
imgui_popup.set_label( _( "Filter:" ) );
imgui_popup.set_description( item_filter_rule_string( item_filter_type::FILTER ) + "\n\n" +
list_items_filter_history_help(), c_white, true );
imgui_popup.set_identifier( "item_filter" );
sFilter = imgui_popup.query();
refilter = true;
addcategory = !sort_radius;
uistate.list_item_filter_active = !sFilter.empty();
Expand Down
38 changes: 11 additions & 27 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "game_constants.h"
#include "generic_factory.h"
#include "input_context.h"
#include "input_popup.h"
#include "json.h"
#include "lang_stats.h"
#include "line.h"
Expand Down Expand Up @@ -3755,33 +3756,16 @@ std::string options_manager::show( bool ingame, const bool world_options_only, b
current_opt.setNext();
} else {
const bool is_int = current_opt.getType() == "int";
const bool is_float = current_opt.getType() == "float";
const std::string old_opt_val = current_opt.getValueName();
const std::string opt_val = string_input_popup()
.title( current_opt.getMenuText() )
.width( 10 )
.text( old_opt_val )
.only_digits( is_int )
.query_string();
if( !opt_val.empty() && opt_val != old_opt_val ) {
if( is_float ) {
std::istringstream ssTemp( opt_val );
// This uses the current locale, to allow the users
// to use their own decimal format.
float tmpFloat;
ssTemp >> tmpFloat;
if( ssTemp ) {
current_opt.setValue( tmpFloat );

} else {
popup( _( "Invalid input: not a number" ) );
}
} else {
// option is of type "int": string_input_popup
// has taken care that the string contains
// only digits, parsing is done in setValue
current_opt.setValue( opt_val );
}
if( is_int ) {
number_input_popup<int> popup( 0, current_opt.value_as<int>() );
popup.set_label( current_opt.getMenuText() );
int num = popup.query();
current_opt.setValue( num );
} else {
number_input_popup<float> popup( 0, current_opt.value_as<float>() );
popup.set_label( current_opt.getMenuText() );
float num = popup.query();
current_opt.setValue( num );
}
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/worldfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "enums.h"
#include "filesystem.h"
#include "input_context.h"
#include "input_popup.h"
#include "json.h"
#include "json_loader.h"
#include "mod_manager.h"
Expand Down Expand Up @@ -188,21 +189,21 @@ WORLD *worldfactory::make_new_world( bool show_prompt, const std::string &world_
static std::optional<std::string> prompt_world_name( const std::string &title,
const std::string &cur_worldname )
{
string_input_popup popup;
popup.max_length( max_worldname_len ).title( title ).text( cur_worldname );
string_input_popup_imgui popup( 50, cur_worldname );
popup.set_max_input_length( max_worldname_len );
popup.set_label( title );

input_context ctxt( "STRING_INPUT" );
popup.description( string_format(
_( "Press [<color_c_yellow>%s</color>] to randomize the world name." ),
ctxt.get_desc( "PICK_RANDOM_WORLDNAME", 1U ) ) );
popup.set_description( string_format(
_( "Press [<color_c_yellow>%s</color>] to randomize the world name." ),
ctxt.get_desc( "PICK_RANDOM_WORLDNAME", 1U ) ) );

popup.custom_actions.emplace_back( "PICK_RANDOM_WORLDNAME", translation() );
popup.add_callback( "PICK_RANDOM_WORLDNAME", [&popup]() {
popup.text( get_next_valid_worldname() );
popup.add_callback( { "PICK_RANDOM_WORLDNAME" }, [&popup]() {
popup.set_text( get_next_valid_worldname() );
return true;
} );
std::string message = popup.query_string();
return !popup.canceled() ? std::optional<std::string>( message ) : std::optional<std::string>();
std::string message = popup.query();
return message;
}

int worldfactory::show_worldgen_advanced( WORLD *world )
Expand Down

0 comments on commit 6ce41e6

Please sign in to comment.