-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy pathadvanced_inv_pane.cpp
281 lines (263 loc) · 8.77 KB
/
advanced_inv_pane.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <algorithm>
#include <cassert>
#include <list>
#include <memory>
#include <string>
#include <vector>
#include "advanced_inv_area.h"
#include "advanced_inv_pane.h"
#include "avatar.h"
#include "inventory.h"
#include "item.h"
#include "item_contents.h"
#include "item_search.h"
#include "make_static.h"
#include "map.h"
#include "options.h"
#include "player.h"
#include "uistate.h"
#include "units.h"
#include "vehicle.h"
#include "vehicle_part.h"
#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" ) &&
!is_re_enter ) ? 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 = !get_map().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
{
return is_filtered( *it.items.front() );
}
bool advanced_inventory_pane::is_filtered( const item &it ) const
{
if( it.has_flag( STATIC( flag_id( "HIDDEN_ITEM" ) ) ) ) {
return true;
}
if( filter.empty() ) {
return false;
}
const std::string str = it.tname();
if( filtercache.find( str ) == filtercache.end() ) {
const auto filter_fn = item_filter_from_string( filter );
filtercache[str] = filter_fn;
return !filter_fn( it );
}
return !filtercache[str]( it );
}
void advanced_inventory_pane::add_items_from_area( advanced_inv_area &square,
bool vehicle_override )
{
assert( square.id != AIM_ALL );
square.volume = 0_ml;
square.weight = 0_gram;
if( !square.canputitems() ) {
return;
}
map &m = get_map();
avatar &u = get_avatar();
// Existing items are *not* cleared on purpose, this might be called
// several times in case all surrounding squares are to be shown.
if( square.id == AIM_INVENTORY ) {
const_invslice stacks = u.inv_const_slice();
for( size_t x = 0; x < stacks.size(); ++x ) {
std::list<item *> item_pointers;
for( item * const &i : *stacks[x] ) {
item_pointers.push_back( i );
}
advanced_inv_listitem it( item_pointers, x, square.id, false );
if( is_filtered( *it.items.front() ) ) {
continue;
}
square.volume += it.volume;
square.weight += it.weight;
items.push_back( it );
}
} else if( square.id == AIM_WORN ) {
auto iter = u.worn.begin();
for( size_t i = 0; i < u.worn.size(); ++i, ++iter ) {
advanced_inv_listitem it( *iter, i, 1, square.id, false );
if( is_filtered( *it.items.front() ) ) {
continue;
}
square.volume += it.volume;
square.weight += it.weight;
items.push_back( it );
}
} else if( square.id == AIM_CONTAINER ) {
item *cont = square.get_container( in_vehicle() );
if( cont != nullptr ) {
if( !cont->is_container_empty() ) {
// filtering does not make sense for liquid in container
item *it = &square.get_container( in_vehicle() )->contents.front();
advanced_inv_listitem ait( it, 0, 1, square.id, in_vehicle() );
square.volume += ait.volume;
square.weight += ait.weight;
items.push_back( ait );
}
square.desc[0] = cont->tname( 1, false );
}
} else {
bool is_in_vehicle = square.can_store_in_vehicle() && ( in_vehicle() || vehicle_override );
const advanced_inv_area::itemstack &stacks = is_in_vehicle ?
square.i_stacked( square.veh->get_items( square.vstor ) ) :
square.i_stacked( m.i_at( square.pos ) );
for( size_t x = 0; x < stacks.size(); ++x ) {
advanced_inv_listitem it( stacks[x], x, square.id, is_in_vehicle );
if( is_filtered( *it.items.front() ) ) {
continue;
}
square.volume += it.volume;
square.weight += it.weight;
items.push_back( it );
}
}
}
void advanced_inventory_pane::paginate( size_t itemsPerPage )
{
// not needed as there are no category entries here.
if( sortby != SORTBY_CATEGORY ) {
return;
}
// first, we insert all the items, then we sort the result
for( size_t i = 0; i < items.size(); ++i ) {
if( i % itemsPerPage == 0 ) {
// first entry on the page, should be a category header
if( items[i].is_item_entry() ) {
items.insert( items.begin() + i, advanced_inv_listitem( items[i].cat ) );
}
}
if( ( i + 1 ) % itemsPerPage == 0 && i + 1 < items.size() ) {
// last entry of the page, but not the last entry at all!
// Must *not* be a category header!
if( items[i].is_category_header() ) {
items.insert( items.begin() + i, advanced_inv_listitem() );
}
}
}
}
void advanced_inventory_pane::fix_index()
{
if( items.empty() ) {
index = 0;
return;
}
if( index < 0 ) {
index = 0;
} else if( static_cast<size_t>( index ) >= items.size() ) {
index = static_cast<int>( items.size() ) - 1;
}
skip_category_headers( +1 );
}
void advanced_inventory_pane::skip_category_headers( int offset )
{
// 0 would make no sense
assert( offset != 0 );
// valid index is required
assert( static_cast<size_t>( index ) < items.size() );
// only those two offsets are allowed
assert( offset == -1 || offset == +1 );
// index would not be valid, and this would be an endless loop
assert( !items.empty() );
while( !items[index].is_item_entry() ) {
mod_index( offset );
}
}
void advanced_inventory_pane::mod_index( int offset )
{
// 0 would make no sense
assert( offset != 0 );
assert( !items.empty() );
index += offset;
if( index < 0 ) {
index = static_cast<int>( items.size() ) - 1;
} else if( static_cast<size_t>( index ) >= items.size() ) {
index = 0;
}
}
void advanced_inventory_pane::scroll_by( int offset )
{
// 0 would make no sense
assert( offset != 0 );
if( items.empty() ) {
return;
}
mod_index( offset );
skip_category_headers( offset > 0 ? +1 : -1 );
}
void advanced_inventory_pane::scroll_category( int offset )
{
// 0 would make no sense
assert( offset != 0 );
// only those two offsets are allowed
assert( offset == -1 || offset == +1 );
if( items.empty() ) {
return;
}
// index must already be valid!
assert( get_cur_item_ptr() != nullptr );
auto cur_cat = items[index].cat;
if( offset > 0 ) {
while( items[index].cat == cur_cat ) {
index++;
// wrap to begin, stop there.
if( static_cast<size_t>( index ) >= items.size() ) {
index = 0;
break;
}
}
} else {
while( items[index].cat == cur_cat ) {
index--;
// wrap to end, stop there.
if( index < 0 ) {
index = static_cast<int>( items.size() ) - 1;
break;
}
}
}
// Make sure we land on an item entry.
skip_category_headers( offset > 0 ? +1 : -1 );
}
advanced_inv_listitem *advanced_inventory_pane::get_cur_item_ptr()
{
if( static_cast<size_t>( index ) >= items.size() ) {
return nullptr;
}
return &items[index];
}
void advanced_inventory_pane::set_filter( const std::string &new_filter )
{
if( filter == new_filter ) {
return;
}
filter = new_filter;
filtercache.clear();
recalc = true;
}