Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rescope game::inv_for_ functions #36016

Merged
merged 2 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,8 @@ void avatar_action::plthrow( avatar &you, int pos,
}

if( pos == INT_MIN ) {
pos = g->inv_for_all( _( "Throw item" ), _( "You don't have any items to throw." ) );
pos = you.get_item_position( game_menus::inv::titled_menu( you, _( "Throw item" ),
_( "You don't have any items to throw." ) ).get_item() );
g->refresh_all();
}

Expand Down
11 changes: 8 additions & 3 deletions src/computer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "creature.h"
#include "enums.h"
#include "game_constants.h"
#include "game_inventory.h"
#include "int_id.h"
#include "item.h"
#include "omdata.h"
Expand Down Expand Up @@ -366,9 +367,13 @@ void computer::load_data( const std::string &data )

static item *pick_usb()
{
const int pos = g->inv_for_id( itype_id( "usb_drive" ), _( "Choose drive:" ) );
if( pos != INT_MIN ) {
return &g->u.i_at( pos );
auto filter = []( const item & it ) {
return it.typeId() == "usb_drive";
};

item_location loc = game_menus::inv::titled_filter_menu( filter, g->u, _( "Choose drive:" ) );
if( loc ) {
return &*loc;
}
return nullptr;
}
Expand Down
8 changes: 6 additions & 2 deletions src/debug_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "faction.h"
#include "filesystem.h"
#include "game.h"
#include "game_inventory.h"
#include "map_extras.h"
#include "messages.h"
#include "mission.h"
Expand Down Expand Up @@ -518,8 +519,11 @@ void character_edit_menu()
p.weapon = item();
break;
case D_ITEM_WORN: {
int item_pos = g->inv_for_all( _( "Make target equip" ) );
item &to_wear = g->u.i_at( item_pos );
item_location loc = game_menus::inv::titled_menu( g->u, _( "Make target equip" ) );
if( !loc ) {
break;
}
item &to_wear = *loc;
if( to_wear.is_armor() ) {
p.on_item_wear( to_wear );
p.worn.push_back( to_wear );
Expand Down
12 changes: 7 additions & 5 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5346,13 +5346,13 @@ bool game::npc_menu( npc &who )
actor->head_power >= 0 &&
actor->torso_power >= 0;
};
const int pos = inv_for_filter( _( "Use which item?" ), will_accept );
item_location loc = game_menus::inv::titled_filter_menu( will_accept, u, _( "Use which item?" ) );

if( pos == INT_MIN ) {
if( !loc ) {
add_msg( _( "Never mind" ) );
return false;
}
item &used = u.i_at( pos );
item &used = *loc;
bool did_use = u.invoke_item( &used, heal_string, who.pos() );
if( did_use ) {
// Note: exiting a body part selection menu counts as use here
Expand Down Expand Up @@ -8446,9 +8446,11 @@ void game::eat( item_location( *menu )( player &p ), int pos )
void game::change_side( int pos )
{
if( pos == INT_MIN ) {
pos = inv_for_filter( _( "Change side for item" ), [&]( const item & it ) {
auto filter = [&]( const item & it ) {
return u.is_worn( it ) && it.is_sided();
}, _( "You don't have sided items worn." ) );
};
pos = u.get_item_position( game_menus::inv::titled_filter_menu( filter, u,
_( "Change side for item" ), _( "You don't have sided items worn." ) ).get_item() );
}
if( pos == INT_MIN ) {
add_msg( _( "Never mind." ) );
Expand Down
7 changes: 0 additions & 7 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,13 +562,6 @@ class game

void draw_trail_to_square( const tripoint &t, bool bDrawX );

// TODO: Move these functions to game_menus::inv and isolate them.
int inv_for_filter( const std::string &title, item_filter filter,
const std::string &none_message = "" );
int inv_for_all( const std::string &title, const std::string &none_message = "" );
int inv_for_flag( const std::string &flag, const std::string &title );
int inv_for_id( const itype_id &id, const std::string &title );

enum inventory_item_menu_positon {
RIGHT_TERMINAL_EDGE,
LEFT_OF_INFO,
Expand Down
27 changes: 7 additions & 20 deletions src/game_inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,31 +207,18 @@ void game_menus::inv::common( avatar &you )
} while( loop_options.count( res ) != 0 );
}

int game::inv_for_filter( const std::string &title, item_filter filter,
const std::string &none_message )
item_location game_menus::inv::titled_filter_menu( item_filter filter, avatar &you,
const std::string &title, const std::string &none_message )
{
return u.get_item_position( inv_map_splice( filter, title, -1, none_message ).get_item() );
return inv_internal( you, inventory_filter_preset( convert_filter( filter ) ),
title, -1, none_message );
}

int game::inv_for_all( const std::string &title, const std::string &none_message )
item_location game_menus::inv::titled_menu( avatar &you, const std::string &title,
const std::string &none_message )
{
const std::string msg = none_message.empty() ? _( "Your inventory is empty." ) : none_message;
return u.get_item_position( inv_internal( u, inventory_selector_preset(), title, -1,
msg ).get_item() );
}

int game::inv_for_flag( const std::string &flag, const std::string &title )
{
return inv_for_filter( title, [ &flag ]( const item & it ) {
return it.has_flag( flag );
} );
}

int game::inv_for_id( const itype_id &id, const std::string &title )
{
return inv_for_filter( title, [ &id ]( const item & it ) {
return it.typeId() == id;
}, string_format( _( "You don't have a %s." ), item::nname( id ) ) );
return inv_internal( you, inventory_selector_preset(), title, -1, msg );
}

class armor_inventory_preset: public inventory_selector_preset
Expand Down
7 changes: 7 additions & 0 deletions src/game_inventory.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class player;
class salvage_actor;
class repair_item_actor;

using item_filter = std::function<bool( const item & )>;
using item_location_filter = std::function<bool ( const item_location & )>;

class inventory_filter_preset : public inventory_selector_preset
Expand All @@ -39,6 +40,12 @@ namespace game_menus

namespace inv
{
// item selector for all items in @you's inventory.
item_location titled_menu( avatar &you, const std::string &title,
const std::string &none_message = "" );
// item selector for items in @you's inventory with a filter
item_location titled_filter_menu( item_filter filter, avatar &you,
const std::string &title, const std::string &none_message = "" );

/**
* @name Customized inventory menus
Expand Down
Loading