forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.cpp
1028 lines (928 loc) · 31.8 KB
/
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 "ui.h"
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cstdlib>
#include <iterator>
#include <memory>
#include "avatar.h"
#include "cata_utility.h"
#include "catacharset.h"
#include "debug.h"
#include "game.h"
#include "ime.h"
#include "input.h"
#include "output.h"
#include "player.h"
#include "string_input_popup.h"
#include "string_utils.h"
#include "ui_manager.h"
#if defined(__ANDROID__)
#include <SDL_keyboard.h>
#include "options.h"
#endif
catacurses::window new_centered_win( int nlines, int ncols )
{
int height = std::min( nlines, TERMY );
int width = std::min( ncols, TERMX );
point pos( ( TERMX - width ) / 2, ( TERMY - height ) / 2 );
return catacurses::newwin( height, width, pos );
}
/**
* \defgroup UI "The UI Menu."
* @{
*/
uilist::size_scalar &uilist::size_scalar::operator=( auto_assign )
{
fun = nullptr;
return *this;
}
uilist::size_scalar &uilist::size_scalar::operator=( const int val )
{
fun = [val]() -> int {
return val;
};
return *this;
}
uilist::size_scalar &uilist::size_scalar::operator=( const std::function<int()> &fun )
{
this->fun = fun;
return *this;
}
uilist::pos_scalar &uilist::pos_scalar::operator=( auto_assign )
{
fun = nullptr;
return *this;
}
uilist::pos_scalar &uilist::pos_scalar::operator=( const int val )
{
fun = [val]( int ) -> int {
return val;
};
return *this;
}
uilist::pos_scalar &uilist::pos_scalar::operator=( const std::function<int( int )> &fun )
{
this->fun = fun;
return *this;
}
uilist::uilist()
{
init();
}
uilist::uilist( const std::string &hotkeys_override )
{
init();
if( !hotkeys_override.empty() ) {
hotkeys = hotkeys_override;
}
}
uilist::uilist( const std::string &msg, const std::vector<uilist_entry> &opts )
{
init();
text = msg;
entries = opts;
query();
}
uilist::uilist( const std::string &msg, const std::vector<std::string> &opts )
{
init();
text = msg;
for( const std::string &opt : opts ) {
entries.emplace_back( opt );
}
query();
}
uilist::uilist( const std::string &msg, std::initializer_list<const char *const> opts )
{
init();
text = msg;
for( const char *const opt : opts ) {
entries.emplace_back( opt );
}
query();
}
uilist::~uilist()
{
shared_ptr_fast<ui_adaptor> current_ui = ui.lock();
if( current_ui ) {
current_ui->reset();
}
}
void uilist::color_error( const bool report )
{
if( report ) {
_color_error = report_color_error::yes;
} else {
_color_error = report_color_error::no;
}
}
/*
* Enables oneshot construction -> running -> exit
*/
uilist::operator int() const
{
return ret;
}
/**
* Sane defaults on initialization
*/
void uilist::init()
{
if( test_mode ) {
debugmsg( "uilist must not be used in test mode" );
return;
}
w_x_setup = pos_scalar::auto_assign {};
w_y_setup = pos_scalar::auto_assign {};
w_width_setup = size_scalar::auto_assign {};
w_height_setup = size_scalar::auto_assign {};
w_x = 0;
w_y = 0;
w_width = 0;
w_height = 0;
ret = UILIST_WAIT_INPUT;
text.clear(); // header text, after (maybe) folding, populates:
textformatted.clear(); // folded to textwidth
textwidth = MENU_AUTOASSIGN; // if unset, folds according to w_width
title.clear(); // Makes use of the top border, no folding, sets min width if w_width is auto
keypress = 0; // last keypress from (int)getch()
window = catacurses::window(); // our window
keymap.clear(); // keymap[int] == index, for entries[index]
selected = 0; // current highlight, for entries[index]
entries.clear(); // uilist_entry(int returnval, bool enabled, int keycode, std::string text, ... TODO: submenu stuff)
started = false; // set to true when width and key calculations are done, and window is generated.
pad_left_setup = 0;
pad_right_setup = 0;
pad_left = 0; // make a blank space to the left
pad_right = 0; // or right
desc_enabled = false; // don't show option description by default
desc_lines_hint = 6; // default number of lines for description
desc_lines = 6;
footer_text.clear(); // takes precedence over per-entry descriptions.
border_color = c_magenta; // border color
text_color = c_light_gray; // text color
title_color = c_green; // title color
hilight_color = h_white; // highlight for up/down selection bar
hotkey_color = c_light_green; // hotkey text to the right of menu entry's text
disabled_color = c_dark_gray; // disabled menu entry
allow_disabled = false; // disallow selecting disabled options
allow_anykey = false; // do not return on unbound keys
allow_cancel = true; // allow canceling with "QUIT" action
allow_additional = false; // do not return on unhandled additional actions
hilight_disabled =
false; // if false, hitting 'down' onto a disabled entry will advance downward to the first enabled entry
vshift = 0; // scrolling menu offset
vmax = 0; // max entries area rows
callback = nullptr; // * uilist_callback
filter.clear(); // filter string. If "", show everything
fentries.clear(); // fentries is the actual display after filtering, and maps displayed entry number to actual entry number
fselected = 0; // fentries[selected]
filtering = true; // enable list display filtering via '/' or '.'
filtering_igncase = true; // ignore case when filtering
max_entry_len = 0;
max_column_len = 0; // for calculating space for second column
_color_error = report_color_error::yes;
hotkeys = DEFAULT_HOTKEYS;
input_category = "UILIST";
additional_actions.clear();
}
void uilist::filterlist()
{
bool filtering = ( this->filtering && !filter.empty() );
// TODO: && is_all_lc( filter )
bool ignore_case = filtering_igncase;
fentries.clear();
fselected = -1;
int f = 0;
int num_entries = entries.size();
// check if string begin by " and finish by ". If that's the case, we only return a result if it matches it exactly
bool exact_match_only = !filter.empty() && filter.front() == '\"' && filter.back() == '\"';
if( exact_match_only ) {
filter.erase( std::remove( filter.begin(), filter.end(), '\"' ), filter.end() );
}
for( int i = 0; i < num_entries; i++ ) {
if( filtering ) {
if( exact_match_only ) {
if( !( entries[i].txt == filter ) ) {
continue;
}
} else if( ignore_case ) {
if( !lcmatch( entries[i].txt, filter ) ) {
continue;
}
} else if( entries[i].txt.find( filter ) == std::string::npos ) {
continue;
}
}
fentries.push_back( i );
if( i == selected && ( hilight_disabled || entries[i].enabled ) ) {
fselected = f;
} else if( i > selected && fselected == -1 && ( hilight_disabled || entries[i].enabled ) ) {
// Past the previously selected entry, which has been filtered out,
// choose another nearby entry instead.
fselected = f;
}
f++;
}
if( fselected == -1 ) {
fselected = 0;
vshift = 0;
if( fentries.empty() ) {
selected = -1;
} else {
selected = fentries [ 0 ];
}
} else if( fselected < static_cast<int>( fentries.size() ) ) {
selected = fentries[fselected];
} else {
fselected = selected = -1;
}
// scroll to top of screen if all remaining entries fit the screen.
if( static_cast<int>( fentries.size() ) <= vmax ) {
vshift = 0;
}
if( callback != nullptr ) {
callback->select( this );
}
}
void uilist::clear_filter()
{
filter.clear();
filterlist();
}
void uilist::set_filter( const std::string &fstr )
{
filter = fstr;
filterlist();
}
void uilist::inputfilter()
{
input_context ctxt( input_category );
ctxt.register_updown();
ctxt.register_action( "PAGE_UP" );
ctxt.register_action( "PAGE_DOWN" );
ctxt.register_action( "SCROLL_UP" );
ctxt.register_action( "SCROLL_DOWN" );
ctxt.register_action( "ANY_INPUT" );
filter_popup = std::make_unique<string_input_popup>();
filter_popup->context( ctxt ).text( filter )
.ignore_custom_actions( false )
.max_length( 256 )
.window( window, point( 4, w_height - 1 ), w_width - 4 );
ime_sentry sentry;
do {
ui_manager::redraw();
filter = filter_popup->query_string( false );
if( !filter_popup->canceled() ) {
const std::string action = ctxt.input_to_action( ctxt.get_raw_input() );
if( filter_popup->handled() || !scrollby( scroll_amount_from_action( action ) ) ) {
filterlist();
}
}
} while( !filter_popup->confirmed() && !filter_popup->canceled() );
if( filter_popup->canceled() ) {
filterlist();
}
filter_popup.reset();
}
bool uilist::set_selected( int sel )
{
if( sel < 0 || sel >= static_cast<int>( entries.size() ) ) {
// Shortcut
return false;
}
for( size_t i = 0; i < fentries.size(); i++ ) {
if( fentries[i] == sel ) {
selected = sel;
fselected = static_cast<int>( i );
return true;
}
}
return false;
}
/**
* Find the minimum width between max( min_width, 1 ) and
* max( max_width, min_width, 1 ) to fold the string to no more than max_lines,
* or no more than the minimum number of lines possible, assuming that
* foldstring( width ).size() decreases monotonously with width.
**/
static int find_minimum_fold_width( const std::string &str, int max_lines,
int min_width, int max_width )
{
if( str.empty() ) {
return std::max( min_width, 1 );
}
min_width = std::max( min_width, 1 );
// max_width could be further limited by the string width, but utf8_width is
// not handling linebreaks properly.
if( min_width < max_width ) {
// If with max_width the string still folds to more than max_lines, find the
// minimum width that folds the string to such number of lines instead.
max_lines = std::max<int>( max_lines, foldstring( str, max_width ).size() );
while( min_width < max_width ) {
int width = ( min_width + max_width ) / 2;
// width may equal min_width, but will always be less than max_width.
int lines = foldstring( str, width ).size();
// If the current width folds the string to no more than max_lines
if( lines <= max_lines ) {
// The minimum width is between min_width and width.
max_width = width;
} else {
// The minimum width is between width + 1 and max_width.
min_width = width + 1;
}
// The new interval will always be smaller than the previous one,
// so the loop is guaranteed to end.
}
}
return min_width;
}
/**
* Calculate sizes, populate arrays, initialize window
*/
void uilist::setup()
{
bool w_auto = !w_width_setup.fun;
// Space for a line between text and entries. Only needed if there is actually text.
const int text_separator_line = text.empty() ? 0 : 1;
if( w_auto ) {
w_width = 4;
if( !title.empty() ) {
w_width = utf8_width( title ) + 5;
}
} else {
w_width = w_width_setup.fun();
}
const int max_desc_width = w_auto ? TERMX - 4 : w_width - 4;
bool h_auto = !w_height_setup.fun;
if( h_auto ) {
w_height = 4;
} else {
w_height = w_height_setup.fun();
}
max_entry_len = 0;
max_column_len = 0;
desc_lines = desc_lines_hint;
std::vector<int> autoassign;
pad_left = pad_left_setup.fun ? pad_left_setup.fun() : 0;
pad_right = pad_right_setup.fun ? pad_right_setup.fun() : 0;
int pad = pad_left + pad_right + 2;
int descwidth_final = 0; // for description width guard
for( size_t i = 0; i < entries.size(); i++ ) {
int txtwidth = utf8_width( remove_color_tags( entries[i].txt ) );
int ctxtwidth = utf8_width( remove_color_tags( entries[i].ctxt ) );
if( txtwidth > max_entry_len ) {
max_entry_len = txtwidth;
}
if( ctxtwidth > max_column_len ) {
max_column_len = ctxtwidth;
}
int clen = ( ctxtwidth > 0 ) ? ctxtwidth + 2 : 0;
if( entries[ i ].enabled ) {
if( entries[ i ].hotkey > 0 ) {
keymap[ entries[ i ].hotkey ] = i;
} else if( entries[ i ].hotkey == -1 && i < 100 ) {
autoassign.push_back( i );
}
if( entries[ i ].retval == -1 ) {
entries[ i ].retval = i;
}
if( w_auto && w_width < txtwidth + pad + 4 + clen ) {
w_width = txtwidth + pad + 4 + clen;
}
} else {
if( w_auto && w_width < txtwidth + pad + 4 + clen ) {
// TODO: or +5 if header
w_width = txtwidth + pad + 4 + clen;
}
}
if( desc_enabled ) {
const int min_desc_width = std::min( max_desc_width, std::max( w_width, descwidth_final ) - 4 );
int descwidth = find_minimum_fold_width( footer_text.empty() ? entries[i].desc : footer_text,
desc_lines, min_desc_width, max_desc_width );
descwidth += 4; // 2x border + 2x ' ' pad
if( descwidth_final < descwidth ) {
descwidth_final = descwidth;
}
}
if( entries[ i ].text_color == c_red_red ) {
entries[ i ].text_color = text_color;
}
}
size_t next_free_hotkey = 0;
for( auto it = autoassign.begin(); it != autoassign.end() &&
next_free_hotkey < hotkeys.size(); ++it ) {
while( next_free_hotkey < hotkeys.size() ) {
const int setkey = hotkeys[next_free_hotkey];
next_free_hotkey++;
if( keymap.count( setkey ) == 0 ) {
entries[*it].hotkey = setkey;
keymap[setkey] = *it;
break;
}
}
}
if( desc_enabled ) {
if( descwidth_final > TERMX ) {
desc_enabled = false; // give up
} else if( descwidth_final > w_width ) {
w_width = descwidth_final;
}
}
if( !text.empty() ) {
int twidth = utf8_width( remove_color_tags( text ) );
bool formattxt = true;
int realtextwidth = 0;
if( textwidth == -1 ) {
if( !w_auto ) {
realtextwidth = w_width - 4;
} else {
realtextwidth = twidth;
if( twidth + 4 > w_width ) {
if( realtextwidth + 4 > TERMX ) {
realtextwidth = TERMX - 4;
}
textformatted = foldstring( text, realtextwidth );
formattxt = false;
realtextwidth = 10;
for( auto &l : textformatted ) {
const int w = utf8_width( remove_color_tags( l ) );
if( w > realtextwidth ) {
realtextwidth = w;
}
}
if( realtextwidth + 4 > w_width ) {
w_width = realtextwidth + 4;
}
}
}
} else if( textwidth != -1 ) {
realtextwidth = textwidth;
if( realtextwidth + 4 > w_width ) {
w_width = realtextwidth + 4;
}
}
if( formattxt ) {
textformatted = foldstring( text, realtextwidth );
}
}
// shrink-to-fit
if( desc_enabled ) {
desc_lines = 0;
for( const uilist_entry &ent : entries ) {
// -2 for borders, -2 for padding
desc_lines = std::max<int>( desc_lines, foldstring( footer_text.empty() ? ent.desc : footer_text,
w_width - 4 ).size() );
}
if( desc_lines <= 0 ) {
desc_enabled = false;
}
}
if( w_auto && w_width > TERMX ) {
w_width = TERMX;
}
vmax = entries.size();
int additional_lines = 2 + text_separator_line + // add two for top & bottom borders
static_cast<int>( textformatted.size() );
if( desc_enabled ) {
additional_lines += desc_lines + 1; // add one for description separator line
}
if( h_auto ) {
w_height = vmax + additional_lines;
}
if( w_height > TERMY ) {
w_height = TERMY;
}
if( vmax + additional_lines > w_height ) {
vmax = w_height - additional_lines;
}
if( !w_x_setup.fun ) {
w_x = static_cast<int>( ( TERMX - w_width ) / 2 );
} else {
w_x = w_x_setup.fun( w_width );
}
if( !w_y_setup.fun ) {
w_y = static_cast<int>( ( TERMY - w_height ) / 2 );
} else {
w_y = w_y_setup.fun( w_height );
}
window = catacurses::newwin( w_height, w_width, point( w_x, w_y ) );
if( !window ) {
abort();
}
if( !started ) {
filterlist();
}
started = true;
}
void uilist::reposition( ui_adaptor &ui )
{
setup();
if( filter_popup ) {
filter_popup->window( window, point( 4, w_height - 1 ), w_width - 4 );
}
ui.position_from_window( window );
}
void uilist::apply_scrollbar()
{
int sbside = ( pad_left <= 0 ? 0 : w_width - 1 );
int estart = textformatted.size();
if( estart > 0 ) {
estart += 2;
} else {
estart = 1;
}
scrollbar()
.offset_x( sbside )
.offset_y( estart )
.content_size( fentries.size() )
.viewport_pos( vshift )
.viewport_size( vmax )
.border_color( border_color )
.arrow_color( border_color )
.slot_color( c_light_gray )
.bar_color( c_cyan_cyan )
.scroll_to_last( false )
.apply( window );
}
/**
* Generate and refresh output
*/
void uilist::show()
{
if( !started ) {
setup();
}
werase( window );
draw_border( window, border_color );
if( !title.empty() ) {
// NOLINTNEXTLINE(cata-use-named-point-constants)
mvwprintz( window, point( 1, 0 ), border_color, "< " );
wprintz( window, title_color, title );
wprintz( window, border_color, " >" );
}
const int text_lines = textformatted.size();
int estart = 1;
if( !textformatted.empty() ) {
for( int i = 0; i < text_lines; i++ ) {
trim_and_print( window, point( 2, 1 + i ), getmaxx( window ) - 4,
text_color, _color_error, "%s", textformatted[i] );
}
mvwputch( window, point( 0, text_lines + 1 ), border_color, LINE_XXXO );
for( int i = 1; i < w_width - 1; ++i ) {
mvwputch( window, point( i, text_lines + 1 ), border_color, LINE_OXOX );
}
mvwputch( window, point( w_width - 1, text_lines + 1 ), border_color, LINE_XOXX );
estart += text_lines + 1; // +1 for the horizontal line.
}
calcStartPos( vshift, fselected, vmax, fentries.size() );
const int pad_size = std::max( 0, w_width - 2 - pad_left - pad_right );
const std::string padspaces = std::string( pad_size, ' ' );
for( int fei = vshift, si = 0; si < vmax; fei++, si++ ) {
if( fei < static_cast<int>( fentries.size() ) ) {
int ei = fentries [ fei ];
nc_color co;
nc_color hk_co;
if( ei == selected ) {
if( entries[ei].override_hilite_color ) {
co = entries[ei].hilite_color;
} else {
co = hilight_color;
}
hk_co = co;
} else {
if( entries[ei].enabled || entries[ei].force_color ) {
co = entries[ ei ].text_color;
} else {
co = disabled_color;
}
hk_co = entries[ei].enabled ? hotkey_color : co;
}
mvwprintz( window, point( pad_left + 1, estart + si ), co, padspaces );
if( entries[ ei ].hotkey >= 33 && entries[ ei ].hotkey < 126 ) {
mvwprintz( window, point( pad_left + 2, estart + si ), hk_co,
"%c", entries[ ei ].hotkey );
}
if( pad_size > 3 ) {
// pad_size indicates the maximal width of the entry, it is used above to
// activate the highlighting, it is used to override previous text there, but in both
// cases printing starts at pad_left+1, here it starts at pad_left+4, so 3 cells less
// to be used.
const auto entry = utf8_wrapper( ei == selected ? remove_color_tags( entries[ ei ].txt ) :
entries[ ei ].txt );
trim_and_print( window, point( pad_left + 4, estart + si ),
max_entry_len, co, _color_error, "%s", entry.c_str() );
if( max_column_len && !entries[ ei ].ctxt.empty() ) {
const auto centry = utf8_wrapper( ei == selected ? remove_color_tags( entries[ ei ].ctxt ) :
entries[ ei ].ctxt );
trim_and_print( window, point( getmaxx( window ) - max_column_len - 2, estart + si ),
max_column_len, co, _color_error, "%s", centry.str() );
}
}
mvwzstr menu_entry_extra_text = entries[ei].extratxt;
if( !menu_entry_extra_text.txt.empty() ) {
mvwprintz( window, point( pad_left + 1 + menu_entry_extra_text.left, estart + si ),
menu_entry_extra_text.color, menu_entry_extra_text.txt );
}
if( menu_entry_extra_text.sym != 0 ) {
mvwputch( window, point( pad_left + 1 + menu_entry_extra_text.left, estart + si ),
menu_entry_extra_text.color, menu_entry_extra_text.sym );
}
} else {
mvwprintz( window, point( pad_left + 1, estart + si ), c_light_gray, padspaces );
}
}
if( desc_enabled ) {
// draw border
mvwputch( window, point( 0, w_height - desc_lines - 2 ), border_color, LINE_XXXO );
for( int i = 1; i < w_width - 1; ++i ) {
mvwputch( window, point( i, w_height - desc_lines - 2 ), border_color, LINE_OXOX );
}
mvwputch( window, point( w_width - 1, w_height - desc_lines - 2 ), border_color, LINE_XOXX );
// clear previous desc the ugly way
for( int y = desc_lines + 1; y > 1; --y ) {
for( int x = 2; x < w_width - 2; ++x ) {
mvwputch( window, point( x, w_height - y ), text_color, " " );
}
}
if( static_cast<size_t>( selected ) < entries.size() ) {
fold_and_print( window, point( 2, w_height - desc_lines - 1 ), w_width - 4, text_color,
footer_text.empty() ? entries[selected].desc : footer_text );
}
}
if( filter_popup ) {
mvwprintz( window, point( 2, w_height - 1 ), border_color, "< " );
mvwprintz( window, point( w_width - 3, w_height - 1 ), border_color, " >" );
filter_popup->query( /*loop=*/false, /*draw_only=*/true );
} else {
if( !filter.empty() ) {
mvwprintz( window, point( 2, w_height - 1 ), border_color, "< %s >", filter );
mvwprintz( window, point( 4, w_height - 1 ), text_color, filter );
}
}
apply_scrollbar();
wnoutrefresh( window );
if( callback != nullptr ) {
callback->refresh( this );
}
}
int uilist::scroll_amount_from_action( const std::string &action )
{
if( action == "UP" ) {
return -1;
} else if( action == "PAGE_UP" ) {
return ( -vmax + 1 );
} else if( action == "SCROLL_UP" ) {
return -3;
} else if( action == "DOWN" ) {
return 1;
} else if( action == "PAGE_DOWN" ) {
return vmax - 1;
} else if( action == "SCROLL_DOWN" ) {
return +3;
} else {
return 0;
}
}
/**
* check for valid scrolling keypress and handle. return false if invalid keypress
*/
bool uilist::scrollby( const int scrollby )
{
if( scrollby == 0 ) {
return false;
}
bool looparound = ( scrollby == -1 || scrollby == 1 );
bool backwards = ( scrollby < 0 );
fselected += scrollby;
if( !looparound ) {
if( backwards && fselected < 0 ) {
fselected = 0;
} else if( fselected >= static_cast<int>( fentries.size() ) ) {
fselected = fentries.size() - 1;
}
}
if( backwards ) {
if( fselected < 0 ) {
fselected = fentries.size() - 1;
}
for( size_t i = 0; i < fentries.size(); ++i ) {
if( hilight_disabled || entries[ fentries [ fselected ] ].enabled ) {
break;
}
--fselected;
if( fselected < 0 ) {
fselected = fentries.size() - 1;
}
}
} else {
if( fselected >= static_cast<int>( fentries.size() ) ) {
fselected = 0;
}
for( size_t i = 0; i < fentries.size(); ++i ) {
if( hilight_disabled || entries[ fentries [ fselected ] ].enabled ) {
break;
}
++fselected;
if( fselected >= static_cast<int>( fentries.size() ) ) {
fselected = 0;
}
}
}
if( static_cast<size_t>( fselected ) < fentries.size() ) {
selected = fentries [ fselected ];
if( callback != nullptr ) {
callback->select( this );
}
}
return true;
}
shared_ptr_fast<ui_adaptor> uilist::create_or_get_ui_adaptor()
{
shared_ptr_fast<ui_adaptor> current_ui = ui.lock();
if( !current_ui ) {
ui = current_ui = make_shared_fast<ui_adaptor>();
current_ui->on_redraw( [this]( const ui_adaptor & ) {
show();
} );
current_ui->on_screen_resize( [this]( ui_adaptor & ui ) {
reposition( ui );
} );
current_ui->mark_resize();
}
return current_ui;
}
/**
* Handle input and update display
*
*/
void uilist::query( bool loop, int timeout )
{
if( test_mode ) {
debugmsg( "Tried to open UI in test mode" );
ret = UILIST_ERROR;
return;
}
keypress = 0;
if( entries.empty() ) {
ret = UILIST_ERROR;
return;
}
ret = UILIST_WAIT_INPUT;
input_context ctxt( input_category );
ctxt.register_updown();
ctxt.register_action( "PAGE_UP" );
ctxt.register_action( "PAGE_DOWN" );
ctxt.register_action( "SCROLL_UP" );
ctxt.register_action( "SCROLL_DOWN" );
if( allow_cancel ) {
ctxt.register_action( "QUIT" );
}
ctxt.register_action( "CONFIRM" );
ctxt.register_action( "FILTER" );
ctxt.register_action( "ANY_INPUT" );
ctxt.register_action( "HELP_KEYBINDINGS" );
for( const auto &additional_action : additional_actions ) {
ctxt.register_action( additional_action.first, additional_action.second );
}
hotkeys = ctxt.get_available_single_char_hotkeys( hotkeys );
shared_ptr_fast<ui_adaptor> ui = create_or_get_ui_adaptor();
ui_manager::redraw();
#if defined(__ANDROID__)
for( const auto &entry : entries ) {
if( entry.hotkey > 0 && entry.enabled ) {
ctxt.register_manual_key( entry.hotkey, entry.txt );
}
}
#endif
do {
ret_act = ctxt.handle_input( timeout );
const auto event = ctxt.get_raw_input();
keypress = event.get_first_input();
const auto iter = keymap.find( keypress );
if( scrollby( scroll_amount_from_action( ret_act ) ) ) {
/* nothing */
} else if( filtering && ret_act == "FILTER" ) {
inputfilter();
} else if( iter != keymap.end() ) {
selected = iter->second;
if( entries[ selected ].enabled ) {
ret = entries[ selected ].retval; // valid
} else if( allow_disabled ) {
ret = entries[selected].retval; // disabled
}
if( callback != nullptr ) {
callback->select( this );
}
} else if( !fentries.empty() && ret_act == "CONFIRM" ) {
if( entries[ selected ].enabled ) {
ret = entries[ selected ].retval; // valid
} else if( allow_disabled ) {
// disabled
ret = entries[selected].retval;
}
} else if( allow_cancel && ret_act == "QUIT" ) {
ret = UILIST_CANCEL;
} else if( ret_act == "TIMEOUT" ) {
ret = UILIST_TIMEOUT;
} else {
// including HELP_KEYBINDINGS, in case the caller wants to refresh their contents
bool unhandled = callback == nullptr || !callback->key( ctxt, event, selected, this );
if( unhandled && allow_anykey ) {
ret = UILIST_UNBOUND;
} else if( unhandled && allow_additional ) {
for( const auto &it : additional_actions ) {
if( it.first == ret_act ) {
ret = UILIST_ADDITIONAL;
break;
}
}
}
}
ui_manager::redraw();
} while( loop && ret == UILIST_WAIT_INPUT );
}
///@}
/**
* cleanup
*/
void uilist::reset()
{
window = catacurses::window();
init();
}
void uilist::addentry( const std::string &str )
{
entries.emplace_back( str );
}
void uilist::addentry( int r, bool e, int k, const std::string &str )
{
entries.emplace_back( r, e, k, str );
}
void uilist::addentry_desc( const std::string &str, const std::string &desc )
{
entries.emplace_back( str, desc );
}
void uilist::addentry_desc( int r, bool e, int k, const std::string &str, const std::string &desc )
{
entries.emplace_back( r, e, k, str, desc );
}
void uilist::addentry_col( int r, bool e, int k, const std::string &str, const std::string &column,
const std::string &desc )
{
entries.emplace_back( r, e, k, str, desc, column );
}
void uilist::settext( const std::string &str )
{
text = str;
}
struct pointmenu_cb::impl_t {
const std::vector< tripoint > &points;
int last; // to suppress redrawing
tripoint last_view; // to reposition the view after selecting
shared_ptr_fast<game::draw_callback_t> terrain_draw_cb;
impl_t( const std::vector<tripoint> &pts );
~impl_t();
void select( uilist *menu );
};
pointmenu_cb::impl_t::impl_t( const std::vector<tripoint> &pts ) : points( pts )
{
last = INT_MIN;
last_view = g->u.view_offset;
terrain_draw_cb = make_shared_fast<game::draw_callback_t>( [this]() {
if( last >= 0 && static_cast<size_t>( last ) < points.size() ) {
g->draw_trail_to_square( g->u.view_offset, true );
}
} );
g->add_draw_callback( terrain_draw_cb );
}
pointmenu_cb::impl_t::~impl_t()
{
g->u.view_offset = last_view;
}