Skip to content

Commit

Permalink
Advanced inv save settings (#35239)
Browse files Browse the repository at this point in the history
  • Loading branch information
akozhevn authored Mar 17, 2020
1 parent 6230283 commit be45877
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 136 deletions.
89 changes: 33 additions & 56 deletions src/advanced_inv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,23 @@ advanced_inventory::advanced_inventory()
}
} )
{
save_state = &uistate.transfer_save;
}
// *INDENT-ON*

advanced_inventory::~advanced_inventory()
{
save_settings( false );
auto &aim_code = uistate.adv_inv_exit_code;
if( aim_code != exit_re_entry ) {
aim_code = exit_okay;
if( save_state->exit_code != exit_re_entry ) {
save_state->exit_code = exit_okay;
}
// Only refresh if we exited manually, otherwise we're going to be right back
if( exit ) {
werase( head );
werase( minimap );
werase( mm_border );
werase( left_window );
werase( right_window );
werase( panes[left].window );
werase( panes[right].window );
g->refresh_all();
g->u.check_item_encumbrance_flag();
}
Expand All @@ -134,44 +134,19 @@ advanced_inventory::~advanced_inventory()
void advanced_inventory::save_settings( bool only_panes )
{
if( !only_panes ) {
uistate.adv_inv_last_coords = g->u.pos();
uistate.adv_inv_src = src;
uistate.adv_inv_dest = dest;
save_state->active_left = ( src == left );
}
for( int i = 0; i < NUM_PANES; ++i ) {
uistate.adv_inv_in_vehicle[i] = panes[i].in_vehicle();
uistate.adv_inv_area[i] = panes[i].get_area();
uistate.adv_inv_index[i] = panes[i].index;
uistate.adv_inv_filter[i] = panes[i].filter;
uistate.adv_inv_sort[i] = panes[i].sortby;
panes[i].save_settings();
}
}

void advanced_inventory::load_settings()
{
aim_exit aim_code = static_cast<aim_exit>( uistate.adv_inv_exit_code );
for( int i = 0; i < NUM_PANES; ++i ) {
aim_location location;
if( get_option<bool>( "OPEN_DEFAULT_ADV_INV" ) ) {
location = static_cast<aim_location>( uistate.adv_inv_default_areas[i] );
} else {
location = static_cast<aim_location>( uistate.adv_inv_area[i] );
}
auto square = squares[location];
// determine the square's vehicle/map item presence
bool has_veh_items = square.can_store_in_vehicle() ?
!square.veh->get_items( square.vstor ).empty() : false;
bool has_map_items = !g->m.i_at( square.pos ).empty();
// determine based on map items and settings to show cargo
bool show_vehicle = aim_code == exit_re_entry ?
uistate.adv_inv_in_vehicle[i] : has_veh_items ? true :
has_map_items ? false : square.can_store_in_vehicle();
panes[i].set_area( square, show_vehicle );
panes[i].sortby = static_cast<advanced_inv_sortby>( uistate.adv_inv_sort[i] );
panes[i].index = uistate.adv_inv_index[i];
panes[i].filter = uistate.adv_inv_filter[i];
}
uistate.adv_inv_exit_code = exit_none;
aim_exit aim_code = static_cast<aim_exit>( save_state->exit_code );
panes[left].load_settings( save_state->saved_area, squares, aim_code == exit_re_entry );
panes[right].load_settings( save_state->saved_area_right, squares, aim_code == exit_re_entry );
save_state->exit_code = exit_none;
}

std::string advanced_inventory::get_sortname( advanced_inv_sortby sortby )
Expand Down Expand Up @@ -232,10 +207,13 @@ void advanced_inventory::init()
square.init();
}

panes[left].save_state = &save_state->pane;
panes[right].save_state = &save_state->pane_right;

load_settings();

src = static_cast<side>( uistate.adv_inv_src );
dest = static_cast<side>( uistate.adv_inv_dest );
src = ( save_state->active_left ) ? left : right;
dest = ( save_state->active_left ) ? right : left;

w_height = TERMY < min_w_height + head_height ? min_w_height : TERMY - head_height;
w_width = TERMX < min_w_width ? min_w_width : TERMX > max_w_width ? max_w_width :
Expand All @@ -250,16 +228,13 @@ void advanced_inventory::init()
point( colstart + ( w_width - ( minimap_width + 2 ) ), headstart ) );
minimap = catacurses::newwin( minimap_height, minimap_width,
point( colstart + ( w_width - ( minimap_width + 1 ) ), headstart + 1 ) );
left_window = catacurses::newwin( w_height, w_width / 2, point( colstart,
headstart + head_height ) );
right_window = catacurses::newwin( w_height, w_width / 2, point( colstart + w_width / 2,
headstart + head_height ) );
panes[left].window = catacurses::newwin( w_height, w_width / 2, point( colstart,
headstart + head_height ) );
panes[right].window = catacurses::newwin( w_height, w_width / 2, point( colstart + w_width / 2,
headstart + head_height ) );

// 2 for the borders, 5 for the header stuff
itemsPerPage = w_height - 2 - 5;

panes[left].window = left_window;
panes[right].window = right_window;
}

void advanced_inventory::print_items( const advanced_inventory_pane &pane, bool active )
Expand Down Expand Up @@ -795,9 +770,9 @@ bool advanced_inventory::move_all_items( bool nested_call )
advanced_inventory_pane shadow = panes[src];
// here we recursively call this function with each area in order to
// put all items in the proper destination area, with minimal fuss
int &loc = uistate.adv_inv_aim_all_location;
int &loc = save_state->aim_all_location;
// re-entry nonsense
int &entry = uistate.adv_inv_re_enter_move_all;
int &entry = save_state->re_enter_move_all;
// if we are just starting out, set entry to initial value
switch( static_cast<aim_entry>( entry++ ) ) {
case ENTRY_START:
Expand Down Expand Up @@ -1133,16 +1108,17 @@ void advanced_inventory::display()
left, right
} ) {
auto &pane = panes[cside];
aim_location location = static_cast<aim_location>( uistate.adv_inv_default_areas[cside] );
int i_location = cside == left ? save_state->saved_area : save_state->saved_area_right;
aim_location location = static_cast<aim_location>( i_location );
if( pane.get_area() != location || location == AIM_ALL ) {
pane.recalc = true;
}
pane.set_area( squares[location] );
}
redraw = true;
} else if( action == "SAVE_DEFAULT" ) {
uistate.adv_inv_default_areas[left] = panes[left].get_area();
uistate.adv_inv_default_areas[right] = panes[right].get_area();
save_state->saved_area = panes[left].get_area();
save_state->saved_area_right = panes[right].get_area();
popup( _( "Default layout was saved." ) );
redraw = true;
} else if( get_square( action, changeSquare ) ) {
Expand Down Expand Up @@ -1351,7 +1327,6 @@ void advanced_inventory::display()
} else if( action == "SORT" ) {
if( show_sort_menu( spane ) ) {
recalc = true;
uistate.adv_inv_sort[src] = spane.sortby;
}
redraw = true;
} else if( action == "FILTER" ) {
Expand Down Expand Up @@ -1574,7 +1549,7 @@ bool advanced_inventory::query_destination( aim_location &def )
}
}
// Selected keyed to uilist.entries, which starts at 0.
menu.selected = uistate.adv_inv_last_popup_dest - AIM_SOUTHWEST;
menu.selected = save_state->last_popup_dest - AIM_SOUTHWEST;
// generate and show window.
menu.show();
// query, but don't loop
Expand All @@ -1589,7 +1564,7 @@ bool advanced_inventory::query_destination( aim_location &def )
// we have to set the destination pane so that move actions will target it
// we can use restore_area later to undo this
panes[dest].set_area( squares[def], true );
uistate.adv_inv_last_popup_dest = menu.ret;
save_state->last_popup_dest = menu.ret;
return true;
}
return false;
Expand Down Expand Up @@ -1838,6 +1813,8 @@ void advanced_inventory::swap_panes()
{
// Switch left and right pane.
std::swap( panes[left], panes[right] );
// Switch save states
std::swap( panes[left].save_state, panes[right].save_state );
// Window pointer must be unchanged!
std::swap( panes[left].window, panes[right].window );
// Recalculation required for weight & volume
Expand All @@ -1851,15 +1828,15 @@ void advanced_inventory::do_return_entry()
save_settings( true );
g->u.assign_activity( ACT_ADV_INVENTORY );
g->u.activity.auto_resume = true;
uistate.adv_inv_exit_code = exit_re_entry;
save_state->exit_code = exit_re_entry;
}

bool advanced_inventory::is_processing() const
{
return uistate.adv_inv_re_enter_move_all != ENTRY_START;
return save_state->re_enter_move_all != ENTRY_START;
}

void cancel_aim_processing()
{
uistate.adv_inv_re_enter_move_all = ENTRY_START;
uistate.transfer_save.re_enter_move_all = ENTRY_START;
}
5 changes: 3 additions & 2 deletions src/advanced_inv.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class uilist;
class vehicle;
class item;

struct advanced_inv_save_state;

struct sort_case_insensitive_less : public std::binary_function< char, char, bool > {
bool operator()( char x, char y ) const {
return toupper( static_cast< unsigned char >( x ) ) < toupper( static_cast< unsigned char >( y ) );
Expand Down Expand Up @@ -108,11 +110,10 @@ class advanced_inventory
std::array<advanced_inv_area, NUM_AIM_LOCATIONS> squares;

catacurses::window head;
catacurses::window left_window;
catacurses::window right_window;

bool exit = false;

advanced_inv_save_state *save_state;
// store/load settings (such as index, filter, etc)
void save_settings( bool only_panes );
void load_settings();
Expand Down
30 changes: 30 additions & 0 deletions src/advanced_inv_pane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "advanced_inv_pane.h"
#include "vehicle.h"
#include "map.h"
#include "options.h"

#include <algorithm>
#include <cassert>
Expand All @@ -44,6 +45,35 @@
#if defined(__ANDROID__)
# include <SDL_keyboard.h>
#endif
void advanced_inventory_pane::save_settings()
{
save_state->in_vehicle = in_vehicle();
save_state->area_idx = get_area();
save_state->selected_idx = index;
save_state->filter = filter;
save_state->sort_idx = sortby;
}

void advanced_inventory_pane::load_settings( int saved_area_idx,
const std::array<advanced_inv_area, NUM_AIM_LOCATIONS> &squares, bool is_re_enter )
{
const int i_location = ( get_option<bool>( "OPEN_DEFAULT_ADV_INV" ) ) ? saved_area_idx :
save_state->area_idx;
const aim_location location = static_cast<aim_location>( i_location );
auto square = squares[location];
// determine the square's vehicle/map item presence
bool has_veh_items = square.can_store_in_vehicle() ?
!square.veh->get_items( square.vstor ).empty() : false;
bool has_map_items = !g->m.i_at( square.pos ).empty();
// determine based on map items and settings to show cargo
bool show_vehicle = is_re_enter ?
save_state->in_vehicle : has_veh_items ? true :
has_map_items ? false : square.can_store_in_vehicle();
set_area( square, show_vehicle );
sortby = static_cast<advanced_inv_sortby>( save_state->sort_idx );
index = save_state->selected_idx;
filter = save_state->filter;
}

bool advanced_inventory_pane::is_filtered( const advanced_inv_listitem &it ) const
{
Expand Down
9 changes: 6 additions & 3 deletions src/advanced_inv_pane.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <string>
#include <vector>

struct advanced_inv_pane_save_state;

enum aim_location : char;

enum advanced_inv_sortby {
Expand Down Expand Up @@ -57,9 +59,10 @@ class advanced_inventory_pane
bool in_vehicle() const {
return viewing_cargo;
}
bool on_ground() const {
return area > AIM_INVENTORY && area < AIM_DRAGGED;
}
advanced_inv_pane_save_state *save_state;
void save_settings();
void load_settings( int saved_area_idx,
const std::array<advanced_inv_area, NUM_AIM_LOCATIONS> &squares, bool is_re_enter );
/**
* Index of the selected item (index of @ref items),
*/
Expand Down
Loading

0 comments on commit be45877

Please sign in to comment.