Skip to content

Commit

Permalink
Merge pull request #77782 from Brambor/search-cancel
Browse files Browse the repository at this point in the history
Crafting GUI Search: cancel the search with ESC
  • Loading branch information
Night-Pryanik authored Nov 14, 2024
2 parents 3102c3d + ea67ed4 commit e6dade6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/crafting_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,12 @@ std::pair<Character *, const recipe *> select_crafter_and_crafting_recipe( int &
static_popup popup;
std::chrono::steady_clock::time_point last_update = std::chrono::steady_clock::now();
static constexpr std::chrono::milliseconds update_interval( 500 );

// Get a key description for the cancel button.
// Rather than propagating the context, create a new one here as a one-off.
// See register_action( "QUIT" ) in recipe_dictionary.cpp (line 289 when this was commited).
input_context dummy;
dummy.register_action( "QUIT" );
std::string cancel_btn = dummy.get_button_text( "QUIT", _( "Cancel" ) );
std::function<void( size_t, size_t )> progress_callback =
[&]( size_t at, size_t out_of ) {
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
Expand All @@ -1603,7 +1608,7 @@ std::pair<Character *, const recipe *> select_crafter_and_crafting_recipe( int &
}
last_update = now;
double percent = 100.0 * at / out_of;
popup.message( _( "Searching… %3.0f%%\n" ), percent );
popup.message( _( "Searching… %3.0f%%\n%s\n" ), percent, cancel_btn );
ui_manager::redraw();
refresh_display();
inp_mngr.pump_events();
Expand Down
9 changes: 9 additions & 0 deletions src/recipe_dictionary.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "recipe_dictionary.h"

#include <algorithm>
#include <chrono>
#include <iterator>
#include <memory>
#include <unordered_map>
Expand Down Expand Up @@ -285,7 +286,15 @@ std::vector<const recipe *> recipe_subset::search(

std::vector<const recipe *> res;
size_t i = 0;
ctxt.register_action( "QUIT" );
std::chrono::steady_clock::time_point next_input_check = std::chrono::steady_clock::now();
for( const recipe *r : recipes ) {
if( std::chrono::steady_clock::now() > next_input_check ) {
next_input_check = std::chrono::steady_clock::now() + std::chrono::milliseconds( 250 );
if( ctxt.handle_input( 1 ) == "QUIT" ) {
return res;
}
}
if( progress_callback ) {
progress_callback( i, recipes.size() );
}
Expand Down
2 changes: 2 additions & 0 deletions src/recipe_dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <unordered_set>
#include <vector>

#include "input_context.h"
#include "recipe.h"
#include "type_id.h"

Expand Down Expand Up @@ -205,6 +206,7 @@ class recipe_subset
std::map<const recipe *, int> difficulties;
std::map<crafting_category_id, std::set<const recipe *>> category;
std::map<itype_id, std::set<const recipe *>> component;
mutable input_context ctxt;
};

void serialize( const recipe_subset &value, JsonOut &jsout );
Expand Down

0 comments on commit e6dade6

Please sign in to comment.