forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory_ui.cpp
3469 lines (3045 loc) · 119 KB
/
inventory_ui.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "inventory_ui.h"
#include <cstdint>
#include "activity_actor_definitions.h"
#include "cata_assert.h"
#include "cata_utility.h"
#include "catacharset.h"
#include "character.h"
#include "colony.h"
#include "cuboid_rectangle.h"
#include "debug.h"
#include "enums.h"
#include "inventory.h"
#include "item.h"
#include "item_category.h"
#include "item_pocket.h"
#include "item_search.h"
#include "item_stack.h"
#include "iteminfo_query.h"
#include "line.h"
#include "make_static.h"
#include "map.h"
#include "map_selector.h"
#include "memory_fast.h"
#include "optional.h"
#include "options.h"
#include "output.h"
#include "point.h"
#include "ret_val.h"
#include "sdltiles.h"
#include "localized_comparator.h"
#include "string_formatter.h"
#include "string_input_popup.h"
#include "trade_ui.h"
#include "translations.h"
#include "type_id.h"
#include "ui_manager.h"
#include "units.h"
#include "units_utility.h"
#include "vehicle.h"
#include "vehicle_selector.h"
#include "visitable.h"
#include "vpart_position.h"
#if defined(__ANDROID__)
#include <SDL_keyboard.h>
#endif
#include <algorithm>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <vector>
static const item_category_id item_category_ITEMS_WORN( "ITEMS_WORN" );
static const item_category_id item_category_WEAPON_HELD( "WEAPON_HELD" );
namespace
{
// get topmost visible parent in an unbroken chain
item *get_topmost_parent( item *topmost, item_location loc,
inventory_selector_preset const &preset )
{
return preset.is_shown( loc ) ? topmost != nullptr ? topmost : loc.get_item() : nullptr;
}
using parent_path_t = std::vector<item_location>;
parent_path_t path_to_top( inventory_entry const &e )
{
item_location it = e.any_item();
parent_path_t path{ it };
while( it.has_parent() ) {
it = it.parent_item();
path.emplace_back( it );
}
return path;
}
using pred_t = std::function<bool( inventory_entry const & )>;
void move_if( std::vector<inventory_entry> &src, std::vector<inventory_entry> &dst,
pred_t const &pred )
{
for( auto it = src.begin(); it != src.end(); ) {
if( pred( *it ) ) {
if( it->is_item() ) {
dst.emplace_back( std::move( *it ) );
}
it = src.erase( it );
} else {
++it;
}
}
}
} // namespace
/** The maximum distance from the screen edge, to snap a window to it */
static const size_t max_win_snap_distance = 4;
/** The minimal gap between two cells */
static const int min_cell_gap = 2;
/** The gap between two cells when screen space is limited*/
static const int normal_cell_gap = 4;
/** The minimal gap between the first cell and denial */
static const int min_denial_gap = 2;
/** The minimal gap between two columns */
static const int min_column_gap = 2;
/** The gap between two columns when there's enough space, but they are not centered */
static const int normal_column_gap = 8;
/**
* The minimal occupancy ratio to align columns to the center
* @see inventory_selector::get_columns_occupancy_ratio()
*/
static const double min_ratio_to_center = 0.85;
bool inventory_selector::skip_unselectable = false;
struct navigation_mode_data {
navigation_mode next_mode;
translation name;
nc_color color;
};
struct inventory_input {
std::string action;
int ch = 0;
inventory_entry *entry;
};
struct container_data {
units::volume actual_capacity;
units::volume total_capacity;
units::mass actual_capacity_weight;
units::mass total_capacity_weight;
units::length max_containable_length;
std::string to_formatted_string( const bool compact = true ) {
std::string string_to_format;
if( compact ) {
string_to_format = _( "%s/%s : %s/%s : max %s" );
} else {
string_to_format = _( "(remains %s, %s) max length %s" );
}
return string_format( string_to_format,
unit_to_string( total_capacity - actual_capacity, true, true ),
unit_to_string( total_capacity_weight - actual_capacity_weight, true, true ),
unit_to_string( max_containable_length, true ) );
}
};
static int contained_offset( const item_location &loc )
{
if( loc.where() != item_location::type::container ) {
return 0;
}
return 2 + contained_offset( loc.parent_item() );
}
bool inventory_entry::operator==( const inventory_entry &other ) const
{
return get_category_ptr() == other.get_category_ptr() && locations == other.locations;
}
class selection_column_preset : public inventory_selector_preset
{
public:
selection_column_preset() = default;
std::string get_caption( const inventory_entry &entry ) const override {
std::string res;
const size_t available_count = entry.get_available_count();
const item_location &item = entry.any_item();
if( entry.chosen_count > 0 && entry.chosen_count < available_count ) {
//~ %1$d: chosen count, %2$d: available count
res += string_format( pgettext( "count", "%1$d of %2$d" ), entry.chosen_count,
available_count ) + " ";
} else if( available_count != 1 ) {
res += string_format( "%d ", available_count );
}
if( item->is_money() ) {
cata_assert( available_count == entry.get_stack_size() );
if( entry.chosen_count > 0 && entry.chosen_count < available_count ) {
res += item->display_money( available_count, item->ammo_remaining(),
entry.get_selected_charges() );
} else {
res += item->display_money( available_count, item->ammo_remaining() );
}
} else {
res += item->display_name( available_count );
}
return res;
}
nc_color get_color( const inventory_entry &entry ) const override {
Character &player_character = get_player_character();
if( entry.is_item() ) {
if( &*entry.any_item() == &player_character.get_wielded_item() ) {
return c_light_blue;
} else if( player_character.is_worn( *entry.any_item() ) ) {
return c_cyan;
}
}
return inventory_selector_preset::get_color( entry );
}
};
static const selection_column_preset selection_preset{};
bool inventory_entry::is_hidden() const
{
if( !is_item() ) {
return false;
}
item_location it = locations.front();
bool hidden = false;
if( topmost_parent != nullptr ) {
while( it.has_parent() ) {
item_location const prnt = it.parent_item();
hidden |= prnt.get_item()->contained_where( *it )->settings.is_collapsed();
if( prnt.get_item() == topmost_parent ) {
break;
}
it = prnt;
}
}
return hidden;
}
int inventory_entry::get_total_charges() const
{
int result = 0;
for( const item_location &location : locations ) {
result += location->charges;
}
return result;
}
int inventory_entry::get_selected_charges() const
{
cata_assert( chosen_count <= locations.size() );
int result = 0;
for( size_t i = 0; i < chosen_count; ++i ) {
const item_location &location = locations[i];
result += location->charges;
}
return result;
}
size_t inventory_entry::get_available_count() const
{
if( locations.size() == 1 ) {
return any_item()->count();
} else {
return locations.size();
}
}
int inventory_entry::get_invlet() const
{
if( custom_invlet != INT_MIN ) {
return custom_invlet;
}
if( !is_item() ) {
return '\0';
}
return any_item()->invlet;
}
nc_color inventory_entry::get_invlet_color() const
{
if( !is_selectable() ) {
return c_dark_gray;
} else if( get_player_character().inv->assigned_invlet.count( get_invlet() ) ) {
return c_yellow;
} else {
return c_white;
}
}
void inventory_entry::update_cache()
{
cached_name = any_item()->tname( 1, false );
cached_name_full = any_item()->tname();
}
const item_category *inventory_entry::get_category_ptr() const
{
if( custom_category != nullptr ) {
return custom_category;
}
if( !is_item() ) {
return nullptr;
}
return &any_item()->get_category_of_contents();
}
bool inventory_column::activatable() const
{
return std::any_of( entries.begin(), entries.end(), [this]( const inventory_entry & e ) {
return e.is_highlightable( skip_unselectable );
} );
}
inventory_entry *inventory_column::find_by_invlet( int invlet ) const
{
for( const auto &elem : entries ) {
if( elem.is_item() && elem.get_invlet() == invlet ) {
return const_cast<inventory_entry *>( &elem );
}
}
return nullptr;
}
inventory_entry *inventory_column::find_by_location( item_location &loc ) const
{
for( const inventory_entry &elem : entries ) {
if( elem.is_item() ) {
for( const item_location &it : elem.locations ) {
if( it == loc ) {
return const_cast<inventory_entry *>( &elem );
}
}
}
}
return nullptr;
}
size_t inventory_column::get_width() const
{
return std::max( get_cells_width(), reserved_width );
}
size_t inventory_column::get_height() const
{
return std::min( entries.size(), height );
}
void inventory_column::toggle_skip_unselectable( const bool skip )
{
skip_unselectable = skip;
}
inventory_selector_preset::inventory_selector_preset()
{
append_cell(
std::function<std::string( const inventory_entry & )>( [ this ]( const inventory_entry & entry ) {
return get_caption( entry );
} ) );
}
bool inventory_selector_preset::sort_compare( const inventory_entry &lhs,
const inventory_entry &rhs ) const
{
auto const sort_key = []( inventory_entry const & e ) {
return std::make_tuple( e.cached_name, e.cached_name_full, e.generation );
};
return localized_compare( sort_key( lhs ), sort_key( rhs ) );
}
bool inventory_selector_preset::cat_sort_compare( const inventory_entry &lhs,
const inventory_entry &rhs ) const
{
return *lhs.get_category_ptr() < *rhs.get_category_ptr();
}
nc_color inventory_selector_preset::get_color( const inventory_entry &entry ) const
{
return entry.is_item() ? entry.any_item()->color_in_inventory() : c_magenta;
}
std::function<bool( const inventory_entry & )> inventory_selector_preset::get_filter(
const std::string &filter ) const
{
auto item_filter = basic_item_filter( filter );
return [item_filter]( const inventory_entry & e ) {
return item_filter( *e.any_item() );
};
}
std::string inventory_selector_preset::get_caption( const inventory_entry &entry ) const
{
const size_t count = entry.get_stack_size();
std::string disp_name;
if( entry.any_item()->is_money() ) {
disp_name = entry.any_item()->display_money( count, entry.any_item()->ammo_remaining() );
} else {
disp_name = entry.any_item()->display_name( count );
}
return ( count > 1 ) ? string_format( "%d %s", count, disp_name ) : disp_name;
}
std::string inventory_selector_preset::get_denial( const inventory_entry &entry ) const
{
return entry.is_item() ? get_denial( entry.any_item() ) : std::string();
}
std::string inventory_selector_preset::get_cell_text( const inventory_entry &entry,
size_t cell_index ) const
{
if( cell_index >= cells.size() ) {
debugmsg( "Invalid cell index %d.", cell_index );
return "it's a bug!";
}
if( !entry ) {
return std::string();
} else if( entry.is_item() ) {
std::string text = cells[cell_index].get_text( entry );
const item &actual_item = *entry.locations.front();
const std::string info_display = get_option<std::string>( "DETAILED_CONTAINERS" );
// if we want no additional info skip this
if( info_display != "NONE" ) {
// if we want additional info for all items or it is worn then add the additional info
if( info_display == "ALL" || ( info_display == "WORN" &&
entry.get_category_ptr()->get_id() == item_category_ITEMS_WORN &&
actual_item.is_worn_by_player() ) ) {
if( cell_index == 0 && !text.empty() &&
actual_item.is_container() && actual_item.has_unrestricted_pockets() ) {
const units::volume total_capacity = actual_item.get_total_capacity( true );
const units::mass total_capacity_weight = actual_item.get_total_weight_capacity( true );
const units::length max_containable_length = actual_item.max_containable_length( true );
const units::volume actual_capacity = actual_item.get_total_contained_volume( true );
const units::mass actual_capacity_weight = actual_item.get_total_contained_weight( true );
container_data container_data = {
actual_capacity,
total_capacity,
actual_capacity_weight,
total_capacity_weight,
max_containable_length
};
std::string formatted_string = container_data.to_formatted_string( false );
text = text + string_format( " %s", formatted_string );
}
}
}
return text;
} else if( cell_index != 0 ) {
return replace_colors( cells[cell_index].title );
} else {
return entry.get_category_ptr()->name();
}
}
bool inventory_selector_preset::is_stub_cell( const inventory_entry &entry,
size_t cell_index ) const
{
if( !entry.is_item() ) {
return false;
}
const std::string &text = get_cell_text( entry, cell_index );
return text.empty() || text == cells[cell_index].stub;
}
void inventory_selector_preset::append_cell( const
std::function<std::string( const item_location & )> &func,
const std::string &title, const std::string &stub )
{
// Don't capture by reference here. The func should be able to die earlier than the object itself
append_cell( std::function<std::string( const inventory_entry & )>( [ func ](
const inventory_entry & entry ) {
return func( entry.any_item() );
} ), title, stub );
}
void inventory_selector_preset::append_cell( const
std::function<std::string( const inventory_entry & )> &func,
const std::string &title, const std::string &stub )
{
const auto iter = std::find_if( cells.begin(), cells.end(), [ &title ]( const cell_t &cell ) {
return cell.title == title;
} );
if( iter != cells.end() ) {
debugmsg( "Tried to append a duplicate cell \"%s\": ignored.", title.c_str() );
return;
}
cells.emplace_back( func, title, stub );
}
std::string inventory_selector_preset::cell_t::get_text( const inventory_entry &entry ) const
{
return replace_colors( func( entry ) );
}
bool inventory_holster_preset::is_shown( const item_location &contained ) const
{
if( contained.eventually_contains( holster ) ) {
return false;
}
if( contained.where() != item_location::type::container
&& contained->made_of( phase_id::LIQUID ) ) {
// spilt liquid cannot be picked up
return false;
}
item item_copy( *contained );
item_copy.charges = 1;
if( !holster->can_contain( item_copy ).success() ) {
return false;
}
//only hide if it is in the toplevel of holster (to allow shuffling of items inside a bag)
for( const item *it : holster->all_items_top() ) {
if( it == contained.get_item() ) {
return false;
}
}
if( contained->is_bucket_nonempty() ) {
return false;
}
if( !holster->all_pockets_rigid() &&
!holster.parents_can_contain_recursive( &item_copy ) ) {
return false;
}
return true;
}
void inventory_column::highlight( size_t new_index, scroll_direction dir )
{
if( new_index < entries.size() ) {
if( !entries[new_index].is_highlightable( skip_unselectable ) ) {
new_index = next_highlightable_index( new_index, dir );
}
highlighted_index = new_index;
page_offset = ( new_index == static_cast<size_t>( -1 ) ) ?
0 : highlighted_index - highlighted_index % entries_per_page;
}
}
size_t inventory_column::next_highlightable_index( size_t index, scroll_direction dir ) const
{
if( entries.empty() ) {
return index;
}
// limit index to the space of the size of entries
index = index % entries.size();
size_t new_index = index;
do {
// 'new_index' incremented by 'dir' using division remainder (number of entries) to loop over the entries.
// Negative step '-k' (backwards) is equivalent to '-k + N' (forward), where:
// N = entries.size() - number of elements,
// k = |step| - absolute step (k <= N).
new_index = ( new_index + static_cast<int>( dir ) + entries.size() ) % entries.size();
} while( new_index != index &&
!entries[new_index].is_highlightable( skip_unselectable ) );
if( !entries[new_index].is_highlightable( skip_unselectable ) ) {
return static_cast<size_t>( -1 );
}
return new_index;
}
void inventory_column::move_selection( scroll_direction dir )
{
size_t index = highlighted_index;
do {
index = next_highlightable_index( index, dir );
} while( index != highlighted_index && is_selected_by_category( entries[index] ) );
highlight( index, dir );
}
void inventory_column::move_selection_page( scroll_direction dir )
{
size_t index = highlighted_index;
do {
const size_t next_index = next_highlightable_index( index, dir );
const bool flipped = next_index == highlighted_index ||
( next_index > highlighted_index ) != ( static_cast<int>( dir ) > 0 );
if( flipped && page_of( next_index ) == page_index() ) {
break; // If flipped and still on the same page - no need to flip
}
index = next_index;
} while( page_of( next_highlightable_index( index, dir ) ) == page_index() );
highlight( index, dir );
}
size_t inventory_column::get_entry_cell_width( size_t index, size_t cell_index ) const
{
size_t res = utf8_width( get_entry_cell_cache( index ).text[cell_index], true );
if( cell_index == 0 ) {
res += get_entry_indent( entries[index] );
}
return res;
}
size_t inventory_column::get_entry_cell_width( const inventory_entry &entry,
size_t cell_index ) const
{
size_t res = utf8_width( preset.get_cell_text( entry, cell_index ), true );
if( cell_index == 0 ) {
res += get_entry_indent( entry );
}
return res;
}
size_t inventory_column::get_cells_width() const
{
return std::accumulate( cells.begin(), cells.end(), static_cast<size_t>( 0 ), []( size_t lhs,
const cell_t &cell ) {
return lhs + cell.current_width;
} );
}
void inventory_column::set_filter( const std::string &filter )
{
entries_cell_cache.clear();
paging_is_valid = false;
prepare_paging( filter );
}
void selection_column::set_filter( const std::string & )
{
// always show all selected items
inventory_column::set_filter( std::string() );
}
inventory_column::entry_cell_cache_t inventory_column::make_entry_cell_cache(
const inventory_entry &entry ) const
{
entry_cell_cache_t result;
result.assigned = true;
result.color = preset.get_color( entry );
result.denial = preset.get_denial( entry );
result.text.resize( preset.get_cells_count() );
for( size_t i = 0, n = preset.get_cells_count(); i < n; ++i ) {
result.text[i] = preset.get_cell_text( entry, i );
}
return result;
}
const inventory_column::entry_cell_cache_t &inventory_column::get_entry_cell_cache(
size_t index ) const
{
cata_assert( index < entries.size() );
if( entries_cell_cache.size() < entries.size() ) {
entries_cell_cache.resize( entries.size() );
}
if( !entries_cell_cache[index].assigned ) {
entries_cell_cache[index] = make_entry_cell_cache( entries[index] );
}
return entries_cell_cache[index];
}
void inventory_column::set_width( const size_t new_width,
const std::vector<inventory_column *> &all_columns )
{
reset_width( all_columns );
int width_gap = get_width() - new_width;
// Now adjust the width if we must
while( width_gap != 0 ) {
const int step = width_gap > 0 ? -1 : 1;
// Should return true when lhs < rhs
const auto cmp_for_expansion = []( const cell_t &lhs, const cell_t &rhs ) {
return lhs.visible() && lhs.gap() < rhs.gap();
};
// Should return true when lhs < rhs
const auto cmp_for_shrinking = []( const cell_t &lhs, const cell_t &rhs ) {
if( !lhs.visible() ) {
return false;
}
if( rhs.gap() <= min_cell_gap ) {
return lhs.current_width < rhs.current_width;
} else {
return lhs.gap() < rhs.gap();
}
};
const auto &cell = step > 0
? std::min_element( cells.begin(), cells.end(), cmp_for_expansion )
: std::max_element( cells.begin(), cells.end(), cmp_for_shrinking );
if( cell == cells.end() || !cell->visible() ) {
break; // This is highly unlikely to happen, but just in case
}
cell->current_width += step;
width_gap += step;
}
reserved_width = new_width;
}
void inventory_column::set_height( size_t new_height )
{
if( height != new_height ) {
if( new_height <= 1 ) {
debugmsg( "Unable to assign height <= 1 (was %zd).", new_height );
return;
}
height = new_height;
entries_per_page = new_height;
paging_is_valid = false;
}
}
void inventory_column::expand_to_fit( const inventory_entry &entry )
{
if( !entry ) {
return;
}
// Don't use cell cache here since the entry may not yet be placed into the vector of entries.
const std::string denial = preset.get_denial( entry );
for( size_t i = 0, num = denial.empty() ? cells.size() : 1; i < num; ++i ) {
auto &cell = cells[i];
cell.real_width = std::max( cell.real_width, get_entry_cell_width( entry, i ) );
// Don't reveal the cell for headers and stubs
if( cell.visible() || ( entry.is_item() && !preset.is_stub_cell( entry, i ) ) ) {
const size_t cell_gap = i > 0 ? normal_cell_gap : 0;
cell.current_width = std::max( cell.current_width, cell_gap + cell.real_width );
}
}
if( !denial.empty() ) {
reserved_width = std::max( get_entry_cell_width( entry, 0 ) + min_denial_gap + utf8_width( denial,
true ),
reserved_width );
}
}
void inventory_column::reset_width( const std::vector<inventory_column *> & )
{
for( auto &elem : cells ) {
elem = cell_t();
}
reserved_width = 0;
for( auto &elem : entries ) {
expand_to_fit( elem );
}
}
size_t inventory_column::page_of( size_t index ) const
{
cata_assert( entries_per_page ); // To appease static analysis
// NOLINTNEXTLINE(clang-analyzer-core.DivideZero)
return index / entries_per_page;
}
size_t inventory_column::page_of( const inventory_entry &entry ) const
{
return page_of( std::distance( entries.begin(), std::find( entries.begin(), entries.end(),
entry ) ) );
}
bool inventory_column::has_available_choices() const
{
if( !allows_selecting() || !activatable() ) {
return false;
}
for( size_t i = 0; i < entries.size(); ++i ) {
if( entries[i].is_item() && get_entry_cell_cache( i ).denial.empty() ) {
return true;
}
}
return false;
}
bool inventory_column::is_selected( const inventory_entry &entry ) const
{
return entry == get_highlighted() || ( multiselect && is_selected_by_category( entry ) );
}
bool inventory_column::is_selected_by_category( const inventory_entry &entry ) const
{
return entry.is_item() && mode == navigation_mode::CATEGORY
&& entry.get_category_ptr() == get_highlighted().get_category_ptr()
&& page_of( entry ) == page_index();
}
const inventory_entry &inventory_column::get_highlighted() const
{
if( highlighted_index >= entries.size() || !entries[highlighted_index].is_item() ) {
// clang complains if we use the default constructor here
static const inventory_entry dummy( nullptr );
return dummy;
}
return entries[highlighted_index];
}
inventory_entry &inventory_column::get_highlighted()
{
return const_cast<inventory_entry &>( const_cast<const inventory_column *>
( this )->get_highlighted() );
}
std::vector<inventory_entry *> inventory_column::get_all_selected() const
{
const auto filter_to_selected = [&]( const inventory_entry & entry ) {
return is_selected( entry );
};
return get_entries( filter_to_selected );
}
void inventory_column::_get_entries( get_entries_t *res, entries_t const &ent,
const ffilter_t &filter_func ) const
{
if( allows_selecting() ) {
for( const auto &elem : ent ) {
if( filter_func( elem ) ) {
res->push_back( const_cast<inventory_entry *>( &elem ) );
}
}
}
}
inventory_column::get_entries_t inventory_column::get_entries( const ffilter_t &filter_func,
bool include_hidden ) const
{
get_entries_t res;
if( allows_selecting() ) {
_get_entries( &res, entries, filter_func );
if( include_hidden ) {
_get_entries( &res, entries_hidden, filter_func );
}
}
return res;
}
void inventory_column::set_stack_favorite( std::vector<item_location> &locations,
const bool favorite )
{
for( item_location &loc : locations ) {
loc->set_favorite( favorite );
}
}
void inventory_column::set_collapsed( inventory_entry &entry, const bool collapse )
{
std::vector<item_location> &locations = entry.locations;
bool collapsed = false;
for( item_location &loc : locations ) {
if( loc.get_item()->is_container() ) {
for( item_pocket *pocket : loc->get_all_contained_pockets().value() ) {
pocket->settings.set_collapse( collapse );
collapsed = true;
}
}
}
if( collapsed ) {
entry.collapsed = collapse;
paging_is_valid = false;
// recache cells in case the name changed
std::size_t const index =
std::distance( entries.begin(), std::find( entries.begin(), entries.end(), entry ) );
entries_cell_cache[index].assigned = false;
entries_cell_cache[index] = make_entry_cell_cache( entry );
}
}
void inventory_column::on_input( const inventory_input &input )
{
if( !empty() && active ) {
if( input.action == "DOWN" ) {
move_selection( scroll_direction::FORWARD );
} else if( input.action == "UP" ) {
move_selection( scroll_direction::BACKWARD );
} else if( input.action == "PAGE_DOWN" ) {
move_selection_page( scroll_direction::FORWARD );
} else if( input.action == "PAGE_UP" ) {
move_selection_page( scroll_direction::BACKWARD );
} else if( input.action == "HOME" ) {
highlight( 0, scroll_direction::FORWARD );
} else if( input.action == "END" ) {
highlight( entries.size() - 1, scroll_direction::BACKWARD );
} else if( input.action == "TOGGLE_FAVORITE" ) {
inventory_entry &selected = get_highlighted();
set_stack_favorite( selected.locations, !selected.any_item()->is_favorite );
} else if( input.action == "HIDE_CONTENTS" ) {
inventory_entry &selected = get_highlighted();
set_collapsed( selected, true );
} else if( input.action == "SHOW_CONTENTS" ) {
inventory_entry &selected = get_highlighted();
set_collapsed( selected, false );
}
}
if( input.action == "TOGGLE_FAVORITE" ) {
// Favoriting items in one column may change item names in another column
// if that column contains an item that contains the favorited item. So
// we invalidate every column on TOGGLE_FAVORITE action.
paging_is_valid = false;
}
}
void inventory_column::on_change( const inventory_entry &/* entry */ )
{
// stub
}
void inventory_column::add_entry( const inventory_entry &entry )
{
if( std::find( entries.begin(), entries.end(), entry ) != entries.end() ) {
debugmsg( "Tried to add a duplicate entry." );
return;
}
bool has_loc = false;
if( entry.is_item() && entry.locations.front().where() == item_location::type::container ) {
item_location entry_item = entry.locations.front();
auto entry_with_loc = std::find_if( entries.begin(),
entries.end(), [&entry_item]( const inventory_entry & entry ) {
if( !entry.is_item() ) {
return false;
}
item_location found_entry_item = entry.locations.front();
return found_entry_item.where() == item_location::type::container &&
entry_item->display_stacked_with( *found_entry_item ) &&
found_entry_item.has_parent() &&
entry_item.parent_item() == found_entry_item.parent_item();
} );
if( entry_with_loc != entries.end() ) {
has_loc = true;
std::vector<item_location> locations = entry_with_loc->locations;
locations.insert( locations.end(), entry.locations.begin(), entry.locations.end() );
inventory_entry nentry( locations, entry.get_category_ptr() );
nentry.topmost_parent = entry_with_loc->topmost_parent;
nentry.generation = entry_with_loc->generation;
entries.erase( entry_with_loc );
add_entry( nentry );
}
}
if( !has_loc ) {
entries.emplace_back( entry );
}
entries_cell_cache.clear();
expand_to_fit( entry );
paging_is_valid = false;
}
void inventory_column::_move_entries_to( entries_t const &ent, inventory_column &dest )
{
for( const auto &elem : ent ) {
if( elem.is_item() &&
// this column already has this entry, no need to try to add it again
std::find( dest.entries.begin(), dest.entries.end(), elem ) == dest.entries.end() ) {
dest.add_entry( elem );
}
}
}
void inventory_column::move_entries_to( inventory_column &dest )
{
_move_entries_to( entries, dest );
_move_entries_to( entries_hidden, dest );
dest.prepare_paging();
clear();
}
bool inventory_column::sort_compare( inventory_entry const &lhs, inventory_entry const &rhs )
{
if( lhs.is_selectable() != rhs.is_selectable() ) {
return lhs.is_selectable(); // Disabled items always go last
}
Character &player_character = get_player_character();
// Place favorite items and items with an assigned inventory letter first,
// since the player cared enough to assign them
const bool left_has_invlet =
player_character.inv->assigned_invlet.count( lhs.any_item()->invlet ) != 0;
const bool right_has_invlet =
player_character.inv->assigned_invlet.count( rhs.any_item()->invlet ) != 0;
if( left_has_invlet != right_has_invlet ) {
return left_has_invlet;
}
const bool left_fav = lhs.any_item()->is_favorite;
const bool right_fav = rhs.any_item()->is_favorite;
if( left_fav != right_fav ) {
return left_fav;