From 257ba3e3f9765bcdd71e59aeacc162847d080705 Mon Sep 17 00:00:00 2001 From: akozhevn Date: Tue, 17 Mar 2020 04:54:32 -0400 Subject: [PATCH] Advanced inv save settings (#35239) --- src/advanced_inv.cpp | 89 +++++++++-------------- src/advanced_inv.h | 5 +- src/advanced_inv_pane.cpp | 30 ++++++++ src/advanced_inv_pane.h | 9 ++- src/uistate.h | 148 +++++++++++++++++++------------------- 5 files changed, 145 insertions(+), 136 deletions(-) diff --git a/src/advanced_inv.cpp b/src/advanced_inv.cpp index d0edeb4db1a78..ff51cc2c61d64 100644 --- a/src/advanced_inv.cpp +++ b/src/advanced_inv.cpp @@ -116,23 +116,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(); } @@ -141,44 +141,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( uistate.adv_inv_exit_code ); - for( int i = 0; i < NUM_PANES; ++i ) { - aim_location location; - if( get_option( "OPEN_DEFAULT_ADV_INV" ) ) { - location = static_cast( uistate.adv_inv_default_areas[i] ); - } else { - location = static_cast( 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( 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( 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 ) @@ -239,10 +214,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( uistate.adv_inv_src ); - dest = static_cast( 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 : @@ -257,16 +235,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 ) @@ -802,9 +777,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( entry++ ) ) { case ENTRY_START: @@ -1141,7 +1116,8 @@ void advanced_inventory::display() left, right } ) { auto &pane = panes[cside]; - aim_location location = static_cast( 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( i_location ); if( pane.get_area() != location || location == AIM_ALL ) { pane.recalc = true; } @@ -1149,8 +1125,8 @@ void advanced_inventory::display() } 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 ) ) { @@ -1359,7 +1335,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" ) { @@ -1582,7 +1557,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 @@ -1597,7 +1572,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; @@ -1846,6 +1821,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 @@ -1859,15 +1836,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; } diff --git a/src/advanced_inv.h b/src/advanced_inv.h index 78605f1b2dd05..ea081c6799dfd 100644 --- a/src/advanced_inv.h +++ b/src/advanced_inv.h @@ -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 ) ); @@ -108,11 +110,10 @@ class advanced_inventory std::array 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(); diff --git a/src/advanced_inv_pane.cpp b/src/advanced_inv_pane.cpp index b8101ca6c9717..1fddc51349c6f 100644 --- a/src/advanced_inv_pane.cpp +++ b/src/advanced_inv_pane.cpp @@ -29,6 +29,7 @@ #include "advanced_inv_pane.h" #include "vehicle.h" #include "map.h" +#include "options.h" #include #include @@ -43,6 +44,35 @@ #if defined(__ANDROID__) # include #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 &squares, bool is_re_enter ) +{ + const int i_location = ( get_option( "OPEN_DEFAULT_ADV_INV" ) ) ? saved_area_idx : + save_state->area_idx; + const aim_location location = static_cast( 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( save_state->sort_idx ); + index = save_state->selected_idx; + filter = save_state->filter; +} static const std::string flag_HIDDEN_ITEM( "HIDDEN_ITEM" ); diff --git a/src/advanced_inv_pane.h b/src/advanced_inv_pane.h index e20d91843c4ac..86c1e4b672643 100644 --- a/src/advanced_inv_pane.h +++ b/src/advanced_inv_pane.h @@ -10,6 +10,8 @@ #include #include +struct advanced_inv_pane_save_state; + enum aim_location : char; enum advanced_inv_sortby { @@ -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 &squares, bool is_re_enter ); /** * Index of the selected item (index of @ref items), */ diff --git a/src/uistate.h b/src/uistate.h index 8a70e4e9f0777..aea5f15eeeb94 100644 --- a/src/uistate.h +++ b/src/uistate.h @@ -14,6 +14,71 @@ class item; + +struct advanced_inv_pane_save_state { + public: + int sort_idx = 1; + std::string filter; + int area_idx = 11; + int selected_idx = 0; + + bool in_vehicle = false; + + template + void serialize( JsonStream &json, std::string prefix ) const { + json.member( prefix + "sort_idx", sort_idx ); + json.member( prefix + "filter", filter ); + json.member( prefix + "area_idx", area_idx ); + json.member( prefix + "selected_idx", selected_idx ); + json.member( prefix + "in_vehicle", in_vehicle ); + } + + void deserialize( JsonObject &jo, std::string prefix ) { + jo.read( prefix + "sort_idx", sort_idx ); + jo.read( prefix + "filter", filter ); + jo.read( prefix + "area_idx", area_idx ); + jo.read( prefix + "selected_idx", selected_idx ); + jo.read( prefix + "in_vehicle", in_vehicle ); + } +}; + +struct advanced_inv_save_state { + public: + int exit_code = 0; + int re_enter_move_all = 0; + int aim_all_location = 1; + + bool active_left = true; + int last_popup_dest = 0; + + int saved_area = 11; + int saved_area_right = 0; + advanced_inv_pane_save_state pane; + advanced_inv_pane_save_state pane_right; + + template + void serialize( JsonStream &json, std::string prefix ) const { + json.member( prefix + "active_left", active_left ); + json.member( prefix + "last_popup_dest", last_popup_dest ); + + json.member( prefix + "saved_area", saved_area ); + json.member( prefix + "saved_area_right", saved_area_right ); + pane.serialize( json, prefix + "pane_" ); + pane_right.serialize( json, prefix + "pane_right_" ); + } + + void deserialize( JsonObject &jo, std::string prefix ) { + jo.read( prefix + "active_left", active_left ); + jo.read( prefix + "last_popup_dest", last_popup_dest ); + + jo.read( prefix + "saved_area", saved_area ); + jo.read( prefix + "saved_area_right", saved_area_right ); + pane.area_idx = saved_area; + pane_right.area_idx = saved_area_right; + pane.deserialize( jo, prefix + "pane_" ); + pane_right.deserialize( jo, prefix + "pane_right_" ); + } +}; /* centralized depot for trivial ui data such as sorting, string_input_popup history, etc. To use this, see the ****notes**** below @@ -27,7 +92,7 @@ class uistatedata private: // not needed for compilation, but keeps syntax plugins happy using itype_id = std::string; - enum side { left = 0, right = 1, NUM_PANES = 2 }; + enum side { left = 0, right = 1, NUM_PANES = 2 }; public: int ags_pay_gas_selected_pump = 0; @@ -35,25 +100,15 @@ class uistatedata int wishmutate_selected = 0; int wishmonster_selected = 0; int iexamine_atm_selected = 0; - std::array adv_inv_sort = {{1, 1}}; - std::array adv_inv_area = {{5, 0}}; - std::array adv_inv_index = {{0, 0}}; - std::array adv_inv_in_vehicle = {{false, false}}; - std::array adv_inv_filter = {{"", ""}}; - std::array adv_inv_default_areas = {{11, 0}}; //left: All, right: Inventory - int adv_inv_src = left; - int adv_inv_dest = right; - int adv_inv_last_popup_dest = 0; + int adv_inv_container_location = -1; int adv_inv_container_index = 0; - int adv_inv_exit_code = 0; itype_id adv_inv_container_type = "null"; itype_id adv_inv_container_content_type = "null"; - int adv_inv_re_enter_move_all = 0; - int adv_inv_aim_all_location = 1; - std::map> adv_inv_veh_items, adv_inv_map_items; bool adv_inv_container_in_vehicle = false; + advanced_inv_save_state transfer_save; + bool editmap_nsa_viewmode = false; // true: ignore LOS and lighting bool overmap_blinking = true; // toggles active blinking of overlays. bool overmap_show_overlays = false; // whether overlays are shown or not. @@ -63,11 +118,6 @@ class uistatedata bool overmap_show_hordes = true; bool overmap_show_forest_trails = true; - bool debug_ranged = false; - tripoint adv_inv_last_coords = {-999, -999, -999}; - int last_inv_start = -2; - int last_inv_sel = -2; - // V Menu Stuff int list_item_sort = 0; std::string list_item_filter; @@ -125,17 +175,10 @@ class uistatedata const unsigned int input_history_save_max = 25; json.start_object(); + transfer_save.serialize( json, "transfer_save_" ); + /**** if you want to save whatever so it's whatever when the game is started next, declare here and.... ****/ - serialize_array( json, "adv_inv_sort", adv_inv_sort ); - serialize_array( json, "adv_inv_area", adv_inv_area ); - serialize_array( json, "adv_inv_index", adv_inv_index ); - serialize_array( json, "adv_inv_in_vehicle", adv_inv_in_vehicle ); - serialize_array( json, "adv_inv_filter", adv_inv_filter ); - serialize_array( json, "adv_inv_default_areas", adv_inv_default_areas ); // non array stuffs - json.member( "adv_inv_src", adv_inv_src ); - json.member( "adv_inv_dest", adv_inv_dest ); - json.member( "adv_inv_last_popup_dest", adv_inv_last_popup_dest ); json.member( "adv_inv_container_location", adv_inv_container_location ); json.member( "adv_inv_container_index", adv_inv_container_index ); json.member( "adv_inv_container_in_vehicle", adv_inv_container_in_vehicle ); @@ -182,54 +225,9 @@ class uistatedata template void deserialize( JsonStream &jsin ) { auto jo = jsin.get_object(); - /**** here ****/ - if( jo.has_array( "adv_inv_sort" ) ) { - auto tmp = jo.get_int_array( "adv_inv_sort" ); - std::move( tmp.begin(), tmp.end(), adv_inv_sort.begin() ); - } else { - jo.read( "adv_inv_leftsort", adv_inv_sort[left] ); - jo.read( "adv_inv_rightsort", adv_inv_sort[right] ); - } - // pane area selected - if( jo.has_array( "adv_inv_area" ) ) { - auto tmp = jo.get_int_array( "adv_inv_area" ); - std::move( tmp.begin(), tmp.end(), adv_inv_area.begin() ); - } else { - jo.read( "adv_inv_leftarea", adv_inv_area[left] ); - jo.read( "adv_inv_rightarea", adv_inv_area[right] ); - } - // pane current index - if( jo.has_array( "adv_inv_index" ) ) { - auto tmp = jo.get_int_array( "adv_inv_index" ); - std::move( tmp.begin(), tmp.end(), adv_inv_index.begin() ); - } else { - jo.read( "adv_inv_leftindex", adv_inv_index[left] ); - jo.read( "adv_inv_rightindex", adv_inv_index[right] ); - } - // viewing vehicle cargo - if( jo.has_array( "adv_inv_in_vehicle" ) ) { - const JsonArray ja = jo.get_array( "adv_inv_in_vehicle" ); - for( size_t i = 0; i < adv_inv_in_vehicle.size() && i < ja.size(); ++i ) { - adv_inv_in_vehicle[i] = ja.get_bool( i ); - } - } - // filter strings - if( jo.has_array( "adv_inv_filter" ) ) { - auto tmp = jo.get_string_array( "adv_inv_filter" ); - std::move( tmp.begin(), tmp.end(), adv_inv_filter.begin() ); - } else { - jo.read( "adv_inv_leftfilter", adv_inv_filter[left] ); - jo.read( "adv_inv_rightfilter", adv_inv_filter[right] ); - } - // default areas - if( jo.has_array( "adv_inv_deafult_areas" ) ) { - auto tmp = jo.get_int_array( "adv_inv_deafult_areas" ); - std::move( tmp.begin(), tmp.end(), adv_inv_default_areas.begin() ); - } + + transfer_save.deserialize( jo, "transfer_save_" ); // the rest - jo.read( "adv_inv_src", adv_inv_src ); - jo.read( "adv_inv_dest", adv_inv_dest ); - jo.read( "adv_inv_last_popup_dest", adv_inv_last_popup_dest ); jo.read( "adv_inv_container_location", adv_inv_container_location ); jo.read( "adv_inv_container_index", adv_inv_container_index ); jo.read( "adv_inv_container_in_vehicle", adv_inv_container_in_vehicle );