forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.cpp
2838 lines (2512 loc) · 93.8 KB
/
output.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 "output.h"
#include <cctype>
// IWYU pragma: no_include <sys/errno.h>
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <new>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#include "cached_options.h" // IWYU pragma: keep
#include "cata_utility.h"
#include "catacharset.h"
#include "color.h"
#include "cursesport.h" // IWYU pragma: keep
#include "cursesdef.h"
#include "game_constants.h"
#include "input.h"
#include "item.h"
#include "line.h"
#include "name.h"
#include "options.h"
#include "point.h"
#include "popup.h"
#include "rng.h"
#include "sdltiles.h" // IWYU pragma: keep
#include "string_formatter.h"
#include "string_input_popup.h"
#include "ui_manager.h"
#include "unicode.h"
#include "units_utility.h"
#include "wcwidth.h"
#if defined(__ANDROID__)
#include <jni.h>
#include <SDL_keyboard.h>
#endif
// Display data
int TERMX;
int TERMY;
int POSX;
int POSY;
int TERRAIN_WINDOW_WIDTH;
int TERRAIN_WINDOW_HEIGHT;
int TERRAIN_WINDOW_TERM_WIDTH;
int TERRAIN_WINDOW_TERM_HEIGHT;
int FULL_SCREEN_WIDTH;
int FULL_SCREEN_HEIGHT;
int OVERMAP_WINDOW_HEIGHT;
int OVERMAP_WINDOW_WIDTH;
int OVERMAP_WINDOW_TERM_WIDTH;
int OVERMAP_WINDOW_TERM_HEIGHT;
int OVERMAP_LEGEND_WIDTH;
scrollingcombattext SCT;
std::string string_from_int( const catacurses::chtype ch )
{
catacurses::chtype charcode = ch;
// LINE_NESW - X for on, O for off
switch( ch ) {
case LINE_XOXO:
charcode = LINE_XOXO_C;
break;
case LINE_OXOX:
charcode = LINE_OXOX_C;
break;
case LINE_XXOO:
charcode = LINE_XXOO_C;
break;
case LINE_OXXO:
charcode = LINE_OXXO_C;
break;
case LINE_OOXX:
charcode = LINE_OOXX_C;
break;
case LINE_XOOX:
charcode = LINE_XOOX_C;
break;
case LINE_XXOX:
charcode = LINE_XXOX_C;
break;
case LINE_XXXO:
charcode = LINE_XXXO_C;
break;
case LINE_XOXX:
charcode = LINE_XOXX_C;
break;
case LINE_OXXX:
charcode = LINE_OXXX_C;
break;
case LINE_XXXX:
charcode = LINE_XXXX_C;
break;
default:
break;
}
char buffer[2] = { static_cast<char>( charcode ), '\0' };
return buffer;
}
// utf8 version
std::vector<std::string> foldstring( const std::string &str, int width, const char split )
{
std::vector<std::string> lines;
if( width < 1 ) {
lines.push_back( str );
return lines;
}
std::stringstream sstr( str );
std::string strline;
std::vector<std::string> tags;
while( std::getline( sstr, strline, '\n' ) ) {
if( strline.empty() ) {
// Special case empty lines as std::getline() sets failbit immediately
// if the line is empty.
lines.emplace_back();
} else {
std::string wrapped = word_rewrap( strline, width, split );
std::stringstream swrapped( wrapped );
std::string wline;
while( std::getline( swrapped, wline, '\n' ) ) {
// Ensure that each line is independently color-tagged
// Re-add tags closed in the previous line
const std::string rawwline = wline;
if( !tags.empty() ) {
std::string swline;
for( const std::string &tag : tags ) {
swline += tag;
}
swline += wline;
wline = swline;
}
// Process the additional tags in the current line
const std::vector<size_t> tags_pos = get_tag_positions( rawwline );
for( const size_t tag_pos : tags_pos ) {
if( tag_pos + 1 < rawwline.size() && rawwline[tag_pos + 1] == '/' ) {
if( !tags.empty() ) {
tags.pop_back();
}
} else {
auto tag_end = rawwline.find( '>', tag_pos );
if( tag_end != std::string::npos ) {
tags.emplace_back( rawwline.substr( tag_pos, tag_end + 1 - tag_pos ) );
}
}
}
// Close any unclosed tags
if( !tags.empty() ) {
std::string swline;
swline += wline;
for( auto it = tags.rbegin(); it != tags.rend(); ++it ) {
// currently the only closing tag is </color>
swline += "</color>";
}
wline = swline;
}
// The resulting line can be printed independently and have the correct color
lines.emplace_back( wline );
}
}
}
return lines;
}
std::vector<std::string> split_by_color( const std::string &s )
{
std::vector<std::string> ret;
std::vector<size_t> tag_positions = get_tag_positions( s );
size_t last_pos = 0;
for( size_t tag_position : tag_positions ) {
ret.push_back( s.substr( last_pos, tag_position - last_pos ) );
last_pos = tag_position;
}
// and the last (or only) one
ret.push_back( s.substr( last_pos, std::string::npos ) );
return ret;
}
std::string remove_color_tags( const std::string &s )
{
std::string ret;
std::vector<size_t> tag_positions = get_tag_positions( s );
if( tag_positions.empty() ) {
return s;
}
size_t next_pos = 0;
for( size_t tag_position : tag_positions ) {
ret += s.substr( next_pos, tag_position - next_pos );
next_pos = s.find( ">", tag_position, 1 ) + 1;
}
ret += s.substr( next_pos, std::string::npos );
return ret;
}
color_tag_parse_result::tag_type update_color_stack(
std::stack<nc_color> &color_stack, const std::string &seg,
const report_color_error color_error )
{
color_tag_parse_result tag = get_color_from_tag( seg, color_error );
switch( tag.type ) {
case color_tag_parse_result::open_color_tag:
color_stack.push( tag.color );
break;
case color_tag_parse_result::close_color_tag:
if( !color_stack.empty() ) {
color_stack.pop();
}
break;
case color_tag_parse_result::non_color_tag:
// Do nothing
break;
}
return tag.type;
}
void print_colored_text( const catacurses::window &w, const point &p, nc_color &color,
const nc_color &base_color, const std::string &text,
const report_color_error color_error )
{
if( p.y > -1 && p.x > -1 ) {
wmove( w, p );
}
const auto color_segments = split_by_color( text );
std::stack<nc_color> color_stack;
color_stack.push( color );
for( auto seg : color_segments ) {
if( seg.empty() ) {
continue;
}
if( seg[0] == '<' ) {
const color_tag_parse_result::tag_type type = update_color_stack(
color_stack, seg, color_error );
if( type != color_tag_parse_result::non_color_tag ) {
seg = rm_prefix( seg );
}
}
color = color_stack.empty() ? base_color : color_stack.top();
wprintz( w, color, seg );
}
}
void trim_and_print( const catacurses::window &w, const point &begin,
const int width, const nc_color &base_color,
const std::string &text,
const report_color_error color_error )
{
std::string sText = trim_by_length( text, width );
nc_color dummy = base_color;
print_colored_text( w, begin, dummy, base_color, sText, color_error );
}
std::string trim_by_length( const std::string &text, int width )
{
std::string sText;
if( utf8_width( remove_color_tags( text ) ) > width ) {
int iLength = 0;
std::string sTempText;
std::string sColor;
const auto color_segments = split_by_color( text );
for( const std::string &seg : color_segments ) {
sColor.clear();
if( !seg.empty() && ( seg.substr( 0, 7 ) == "<color_" || seg.substr( 0, 7 ) == "</color" ) ) {
sTempText = rm_prefix( seg );
if( seg.substr( 0, 7 ) == "<color_" ) {
sColor = seg.substr( 0, seg.find( '>' ) + 1 );
}
} else {
sTempText = seg;
}
const int iTempLen = utf8_width( sTempText );
iLength += iTempLen;
if( iLength > width ) {
int pos = 0;
cursorx_to_position( sTempText.c_str(), iTempLen - ( iLength - width ) - 1, &pos, -1 );
sTempText = sTempText.substr( 0, pos ) + "\u2026";
}
sText += sColor + sTempText;
if( !sColor.empty() ) {
sText += "</color>";
}
if( iLength > width ) {
break;
}
}
} else {
sText = text;
}
return sText;
}
int print_scrollable( const catacurses::window &w, int begin_line, const std::string &text,
const nc_color &base_color, const std::string &scroll_msg )
{
const size_t wwidth = getmaxx( w );
const auto text_lines = foldstring( text, wwidth );
size_t wheight = getmaxy( w );
const bool print_scroll_msg = text_lines.size() > wheight;
if( print_scroll_msg && !scroll_msg.empty() ) {
// keep the last line free for a message to the player
wheight--;
}
if( begin_line < 0 || text_lines.size() <= wheight ) {
begin_line = 0;
} else if( begin_line + wheight >= text_lines.size() ) {
begin_line = text_lines.size() - wheight;
}
nc_color color = base_color;
for( size_t i = 0; i + begin_line < text_lines.size() && i < wheight; ++i ) {
print_colored_text( w, point( 0, i ), color, base_color, text_lines[i + begin_line] );
}
if( print_scroll_msg && !scroll_msg.empty() ) {
color = c_white;
print_colored_text( w, point( 0, wheight ), color, color, scroll_msg );
}
return std::max<int>( 0, text_lines.size() - wheight );
}
// returns number of printed lines
int fold_and_print( const catacurses::window &w, const point &begin, int width,
const nc_color &base_color, const std::string &text, const char split )
{
nc_color color = base_color;
std::vector<std::string> textformatted = foldstring( text, width, split );
for( int line_num = 0; static_cast<size_t>( line_num ) < textformatted.size(); line_num++ ) {
print_colored_text( w, begin + point( 0, line_num ), color, base_color,
textformatted[line_num] );
}
return textformatted.size();
}
int fold_and_print_from( const catacurses::window &w, const point &begin, int width,
int begin_line, const nc_color &base_color, const std::string &text )
{
const int iWinHeight = getmaxy( w );
std::stack<nc_color> color_stack;
std::vector<std::string> textformatted = foldstring( text, width );
for( int line_num = 0; static_cast<size_t>( line_num ) < textformatted.size(); line_num++ ) {
if( line_num + begin.y - begin_line == iWinHeight ) {
break;
}
if( line_num >= begin_line ) {
wmove( w, begin + point( 0, -begin_line + line_num ) );
}
// split into colorable sections
std::vector<std::string> color_segments = split_by_color( textformatted[line_num] );
// for each section, get the color, and print it
std::vector<std::string>::iterator it;
for( it = color_segments.begin(); it != color_segments.end(); ++it ) {
color_tag_parse_result::tag_type type = color_tag_parse_result::non_color_tag;
if( !it->empty() && it->at( 0 ) == '<' ) {
type = update_color_stack( color_stack, *it );
}
if( line_num >= begin_line ) {
std::string l = *it;
if( type != color_tag_parse_result::non_color_tag ) {
l = rm_prefix( l );
}
if( l != "--" ) { // -- is a separation line!
nc_color color = color_stack.empty() ? base_color : color_stack.top();
wprintz( w, color, l );
} else {
for( int i = 0; i < width; i++ ) {
wputch( w, c_dark_gray, LINE_OXOX );
}
}
}
}
}
return textformatted.size();
}
void scrollable_text( const std::function<catacurses::window()> &init_window,
const std::string &title, const std::string &text )
{
// NOLINTNEXTLINE(cata-use-named-point-constants)
constexpr point text2( 1, 1 );
input_context ctxt( "SCROLLABLE_TEXT" );
ctxt.register_action( "UP" );
ctxt.register_action( "DOWN" );
ctxt.register_action( "PAGE_UP" );
ctxt.register_action( "PAGE_DOWN" );
ctxt.register_action( "CONFIRM" );
ctxt.register_action( "QUIT" );
ctxt.register_action( "HELP_KEYBINDINGS" );
// mouse
ctxt.register_action( "SCROLL_UP" );
ctxt.register_action( "SCROLL_DOWN" );
catacurses::window w;
int width = 0;
int height = 0;
int text_w = 0;
int text_h = 0;
std::vector<std::string> lines;
int beg_line = 0;
int max_beg_line = 0;
ui_adaptor ui;
const auto screen_resize_cb = [&]( ui_adaptor & ui ) {
w = init_window();
width = getmaxx( w );
height = getmaxy( w );
text_w = std::max( 0, width - 2 );
text_h = std::max( 0, height - 2 );
lines = foldstring( text, text_w );
max_beg_line = std::max( 0, static_cast<int>( lines.size() ) - text_h );
ui.position_from_window( w );
};
screen_resize_cb( ui );
ui.on_screen_resize( screen_resize_cb );
ui.on_redraw( [&]( const ui_adaptor & ) {
werase( w );
draw_border( w, BORDER_COLOR, title );
for( int line = beg_line, pos_y = text2.y; line < std::min<int>( beg_line + text_h, lines.size() );
++line, ++pos_y ) {
nc_color dummy = c_white;
print_colored_text( w, point( text2.x, pos_y ), dummy, dummy, lines[line] );
}
scrollbar().offset_x( width - 1 ).offset_y( text2.y ).content_size( lines.size() )
.viewport_pos( std::min( beg_line, max_beg_line ) ).viewport_size( text_h ).apply( w );
wnoutrefresh( w );
} );
std::string action;
do {
ui_manager::redraw();
action = ctxt.handle_input();
if( action == "UP" || action == "SCROLL_UP" ) {
if( beg_line > 0 ) {
--beg_line;
}
} else if( action == "DOWN" || action == "SCROLL_DOWN" ) {
if( beg_line < max_beg_line ) {
++beg_line;
}
} else if( action == "PAGE_UP" ) {
if( beg_line > text_h ) {
beg_line -= text_h;
} else {
beg_line = 0;
}
} else if( action == "PAGE_DOWN" ) {
// always scroll an entire page's length
if( beg_line < max_beg_line ) {
beg_line += text_h;
}
}
} while( action != "CONFIRM" && action != "QUIT" );
}
/*If name and value fit in field_width, should add a single string equivalent to trimmed_name_and_value
*If the name would not fit, it is folded, preserving an empty column for values, and
*The resulting vector of strings is returned
*/
void add_folded_name_and_value( std::vector<std::string> ¤t_vector, const std::string &name,
const std::string &value, const int field_width )
{
const int name_width = utf8_width( name );
const int value_width = utf8_width( value );
if( name_width + value_width + 1 >= field_width ) {
if( value_width >= field_width ) {
debugmsg( "Unable to fit name (%s) and value (%s) in available space (%i)", name, value,
field_width );
} else {
std::vector<std::string> new_lines;
new_lines = foldstring( name, field_width - value_width );
const int spacing = field_width - utf8_width( new_lines.back() ) - value_width;
new_lines.back() = new_lines.back() + std::string( spacing, ' ' ) + value;
current_vector.insert( current_vector.end(), new_lines.begin(), new_lines.end() );
}
} else {
const int spacing = field_width - name_width - value_width;
current_vector.emplace_back( name + std::string( spacing, ' ' ) + value );
}
}
void add_folded_name_and_value( std::vector<std::string> ¤t_vector, const std::string &name,
const int value, const int field_width )
{
add_folded_name_and_value( current_vector, name, string_format( "%d", value ), field_width );
}
// returns single string with left aligned name and right aligned value
std::string trimmed_name_and_value( const std::string &name, const std::string &value,
const int field_width )
{
const int text_width = utf8_width( name ) + utf8_width( value );
if( text_width >= field_width ) {
//Since it's easier to abbreviate a string than a number, try to preserve the value
std::string trimmed_name = trim_by_length( name, field_width - utf8_width( value ) - 1 );
return trimmed_name + " " + value;
} else {
const int spacing = field_width - text_width;
return name + std::string( spacing, ' ' ) + value;
}
}
std::string trimmed_name_and_value( const std::string &name, int const value,
const int field_width )
{
return trimmed_name_and_value( name, string_format( "%d", value ), field_width );
}
void center_print( const catacurses::window &w, const int y, const nc_color &FG,
const std::string &text )
{
int window_width = getmaxx( w );
int string_width = utf8_width( text, true );
int x;
if( string_width >= window_width ) {
x = 0;
} else {
x = ( window_width - string_width ) / 2;
}
const int available_width = std::max( 1, window_width - x );
trim_and_print( w, point( x, y ), available_width, FG, text );
}
int right_print( const catacurses::window &w, const int line, const int right_indent,
const nc_color &FG, const std::string &text )
{
const int available_width = std::max( 1, getmaxx( w ) - right_indent );
const int x = std::max( 0, available_width - utf8_width( text, true ) );
trim_and_print( w, point( x, line ), available_width, FG, text );
return x;
}
void wputch( const catacurses::window &w, nc_color FG, int ch )
{
wattron( w, FG );
waddch( w, ch );
wattroff( w, FG );
}
void mvwputch( const catacurses::window &w, const point &p, nc_color FG, int ch )
{
wattron( w, FG );
mvwaddch( w, p, ch );
wattroff( w, FG );
}
void mvwputch( const catacurses::window &w, const point &p, nc_color FG, const std::string &ch )
{
wattron( w, FG );
mvwprintw( w, p, ch );
wattroff( w, FG );
}
void mvwputch_inv( const catacurses::window &w, const point &p, nc_color FG, int ch )
{
nc_color HC = invert_color( FG );
wattron( w, HC );
mvwaddch( w, p, ch );
wattroff( w, HC );
}
void mvwputch_inv( const catacurses::window &w, const point &p, nc_color FG,
const std::string &ch )
{
nc_color HC = invert_color( FG );
wattron( w, HC );
mvwprintw( w, p, ch );
wattroff( w, HC );
}
void mvwputch_hi( const catacurses::window &w, const point &p, nc_color FG, int ch )
{
nc_color HC = hilite( FG );
wattron( w, HC );
mvwaddch( w, p, ch );
wattroff( w, HC );
}
void mvwputch_hi( const catacurses::window &w, const point &p, nc_color FG, const std::string &ch )
{
nc_color HC = hilite( FG );
wattron( w, HC );
mvwprintw( w, p, ch );
wattroff( w, HC );
}
void draw_custom_border(
const catacurses::window &w, const catacurses::chtype ls, const catacurses::chtype rs,
const catacurses::chtype ts, const catacurses::chtype bs, const catacurses::chtype tl,
const catacurses::chtype tr, const catacurses::chtype bl, const catacurses::chtype br,
const nc_color &FG, const point &pos, int height, int width )
{
wattron( w, FG );
height = ( height == 0 ) ? getmaxy( w ) - pos.y : height;
width = ( width == 0 ) ? getmaxx( w ) - pos.x : width;
for( int j = pos.y; j < height + pos.y - 1; j++ ) {
if( ls > 0 ) {
mvwputch( w, point( pos.x, j ), c_light_gray, ( ls > 1 ) ? ls : LINE_XOXO ); // |
}
if( rs > 0 ) {
mvwputch( w, point( pos.x + width - 1, j ), c_light_gray, ( rs > 1 ) ? rs : LINE_XOXO ); // |
}
}
for( int j = pos.x; j < width + pos.x - 1; j++ ) {
if( ts > 0 ) {
mvwputch( w, point( j, pos.y ), c_light_gray, ( ts > 1 ) ? ts : LINE_OXOX ); // --
}
if( bs > 0 ) {
mvwputch( w, point( j, pos.y + height - 1 ), c_light_gray, ( bs > 1 ) ? bs : LINE_OXOX ); // --
}
}
if( tl > 0 ) {
mvwputch( w, pos, c_light_gray, ( tl > 1 ) ? tl : LINE_OXXO ); // |^
}
if( tr > 0 ) {
mvwputch( w, pos + point( -1 + width, 0 ), c_light_gray, ( tr > 1 ) ? tr : LINE_OOXX ); // ^|
}
if( bl > 0 ) {
mvwputch( w, pos + point( 0, -1 + height ), c_light_gray, ( bl > 1 ) ? bl : LINE_XXOO ); // |_
}
if( br > 0 ) {
mvwputch( w, pos + point( -1 + width, -1 + height ), c_light_gray,
( br > 1 ) ? br : LINE_XOOX ); // _|
}
wattroff( w, FG );
}
void draw_border( const catacurses::window &w, nc_color border_color, const std::string &title,
nc_color title_color )
{
wattron( w, border_color );
wborder( w, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
wattroff( w, border_color );
if( !title.empty() ) {
center_print( w, 0, title_color, title );
}
}
void draw_border_below_tabs( const catacurses::window &w, nc_color border_color )
{
int width = getmaxx( w );
int height = getmaxy( w );
for( int i = 1; i < width - 1; i++ ) {
mvwputch( w, point( i, height - 1 ), border_color, LINE_OXOX );
}
for( int i = 3; i < height - 1; i++ ) {
mvwputch( w, point( 0, i ), border_color, LINE_XOXO );
mvwputch( w, point( width - 1, i ), border_color, LINE_XOXO );
}
mvwputch( w, point( 0, height - 1 ), border_color, LINE_XXOO ); // |_
mvwputch( w, point( width - 1, height - 1 ), border_color, LINE_XOOX ); // _|
}
border_helper::border_info::border_info( border_helper &helper )
: helper( helper )
{
}
void border_helper::border_info::set( const point &pos, const point &size )
{
this->pos = pos;
this->size = size;
helper.get().border_connection_map.reset();
}
border_helper::border_info &border_helper::add_border()
{
border_info_list.emplace_front( border_info( *this ) );
return border_info_list.front();
}
void border_helper::draw_border( const catacurses::window &win, nc_color border_color )
{
if( !border_connection_map.has_value() ) {
border_connection_map.emplace();
for( const border_info &info : border_info_list ) {
const point beg = info.pos;
const point end = info.pos + info.size;
for( int x = beg.x; x < end.x; ++x ) {
const point ptop( x, beg.y );
const point pbottom( x, end.y - 1 );
if( x > beg.x ) {
border_connection_map.value()[ptop].left =
border_connection_map.value()[pbottom].left = true;
}
if( x < end.x - 1 ) {
border_connection_map.value()[ptop].right =
border_connection_map.value()[pbottom].right = true;
}
}
for( int y = beg.y; y < end.y; ++y ) {
const point pleft( beg.x, y );
const point pright( end.x - 1, y );
if( y > beg.y ) {
border_connection_map.value()[pleft].top =
border_connection_map.value()[pright].top = true;
}
if( y < end.y - 1 ) {
border_connection_map.value()[pleft].bottom =
border_connection_map.value()[pright].bottom = true;
}
}
}
}
const point win_beg( getbegx( win ), getbegy( win ) );
const point win_end = win_beg + point( getmaxx( win ), getmaxy( win ) );
for( const std::pair<const point, border_connection> &conn : border_connection_map.value() ) {
if( conn.first.x >= win_beg.x && conn.first.x < win_end.x &&
conn.first.y >= win_beg.y && conn.first.y < win_end.y ) {
mvwputch( win, conn.first - win_beg, border_color, conn.second.as_curses_line() );
}
}
}
int border_helper::border_connection::as_curses_line() const
{
constexpr int t = 0x8;
constexpr int r = 0x4;
constexpr int b = 0x2;
constexpr int l = 0x1;
const int conn = ( top ? t : 0 ) | ( right ? r : 0 ) | ( bottom ? b : 0 ) | ( left ? l : 0 );
switch( conn ) {
default:
return '?';
case 0x3:
return LINE_OOXX;
case 0x5:
return LINE_OXOX;
case 0x6:
return LINE_OXXO;
case 0x7:
return LINE_OXXX;
case 0x9:
return LINE_XOOX;
case 0xA:
return LINE_XOXO;
case 0xB:
return LINE_XOXX;
case 0xC:
return LINE_XXOO;
case 0xD:
return LINE_XXOX;
case 0xE:
return LINE_XXXO;
case 0xF:
return LINE_XXXX;
}
}
bool query_yn( const std::string &text )
{
#if defined(__ANDROID__)
if( get_option<bool>( "ANDROID_NATIVE_UI" ) ) {
JNIEnv *env = ( JNIEnv * )SDL_AndroidGetJNIEnv();
jobject activity = ( jobject )SDL_AndroidGetActivity();
jclass clazz( env->GetObjectClass( activity ) );
jmethodID get_nativeui_method_id = env->GetMethodID( clazz, "getNativeUI",
"()Lcom/cleverraven/cataclysmdda/NativeUI;" );
jobject native_ui_obj = env->CallObjectMethod( activity, get_nativeui_method_id );
jclass native_ui_cls( env->GetObjectClass( native_ui_obj ) );
jmethodID queryYN_method_id = env->GetMethodID( native_ui_cls, "queryYN", "(Ljava/lang/String;)Z" );
jstring jstr = env->NewStringUTF( text.c_str() );
bool result = env->CallBooleanMethod( native_ui_obj, queryYN_method_id, jstr );
env->DeleteLocalRef( jstr );
env->DeleteLocalRef( native_ui_cls );
env->DeleteLocalRef( native_ui_obj );
env->DeleteLocalRef( clazz );
env->DeleteLocalRef( activity );
return result;
}
#endif // defined(__ANDROID__)
const bool force_uc = get_option<bool>( "FORCE_CAPITAL_YN" );
const auto &allow_key = force_uc ? input_context::disallow_lower_case_or_non_modified_letters
: input_context::allow_all_keys;
return query_popup()
.preferred_keyboard_mode( keyboard_mode::keycode )
.context( "YESNO" )
.message( force_uc && !is_keycode_mode_supported() ?
pgettext( "query_yn", "%s (Case Sensitive)" ) :
pgettext( "query_yn", "%s" ), text )
.option( "YES", allow_key )
.option( "NO", allow_key )
.cursor( 1 )
.default_color( c_light_red )
.query()
.action == "YES";
}
bool query_int( int &result, const std::string &text )
{
string_input_popup popup;
popup.title( text );
popup.text( "" ).only_digits( true );
int temp = popup.query_int();
if( popup.canceled() ) {
return false;
}
result = temp;
return true;
}
std::vector<std::string> get_hotkeys( const std::string &s )
{
std::vector<std::string> hotkeys;
size_t start = s.find_first_of( '<' );
size_t end = s.find_first_of( '>' );
if( start != std::string::npos && end != std::string::npos ) {
// hotkeys separated by '|' inside '<' and '>', for example "<e|E|?>"
size_t lastsep = start;
size_t sep = s.find_first_of( '|', start );
while( sep < end ) {
hotkeys.push_back( s.substr( lastsep + 1, sep - lastsep - 1 ) );
lastsep = sep;
sep = s.find_first_of( '|', sep + 1 );
}
hotkeys.push_back( s.substr( lastsep + 1, end - lastsep - 1 ) );
}
return hotkeys;
}
int popup( const std::string &text, PopupFlags flags )
{
#if defined(__ANDROID__)
if( get_option<bool>( "ANDROID_NATIVE_UI" ) && flags == PF_NONE ) {
JNIEnv *env = ( JNIEnv * )SDL_AndroidGetJNIEnv();
jobject activity = ( jobject )SDL_AndroidGetActivity();
jclass clazz( env->GetObjectClass( activity ) );
jmethodID get_nativeui_method_id = env->GetMethodID( clazz, "getNativeUI",
"()Lcom/cleverraven/cataclysmdda/NativeUI;" );
jobject native_ui_obj = env->CallObjectMethod( activity, get_nativeui_method_id );
jclass native_ui_cls( env->GetObjectClass( native_ui_obj ) );
jmethodID queryYN_method_id = env->GetMethodID( native_ui_cls, "popup", "(Ljava/lang/String;)V" );
jstring jstr = env->NewStringUTF( remove_color_tags( text ).c_str() );
env->CallVoidMethod( native_ui_obj, queryYN_method_id, jstr );
env->DeleteLocalRef( jstr );
env->DeleteLocalRef( native_ui_cls );
env->DeleteLocalRef( native_ui_obj );
env->DeleteLocalRef( clazz );
env->DeleteLocalRef( activity );
return UNKNOWN_UNICODE;
}
#endif
query_popup pop;
pop.preferred_keyboard_mode( keyboard_mode::keychar );
pop.message( "%s", text );
if( flags & PF_GET_KEY ) {
pop.allow_anykey( true );
} else {
pop.allow_cancel( true );
}
if( flags & PF_FULLSCREEN ) {
pop.full_screen( true );
} else if( flags & PF_ON_TOP ) {
pop.on_top( true );
}
pop.context( "POPUP_WAIT" );
const query_popup::result &res = pop.query();
if( res.evt.type == input_event_t::keyboard_char ) {
return res.evt.get_first_input();
} else {
return UNKNOWN_UNICODE;
}
}
//note that passing in iteminfo instances with sType == "DESCRIPTION" does special things
//all this should probably be cleaned up at some point, rather than using a function for things it wasn't meant for
// well frack, half the game uses it so: optional (int)selected argument causes entry highlight, and enter to return entry's key. Also it now returns int
//@param without_getch don't wait getch, return = (int)' ';
input_event draw_item_info( const int iLeft, const int iWidth, const int iTop, const int iHeight,
item_info_data &data )
{
catacurses::window win =
catacurses::newwin( iHeight, iWidth,
point( iLeft, iTop ) );
#if defined(TILES)
clear_window_area( win );
#endif // TILES
wclear( win );
wnoutrefresh( win );
const input_event result = draw_item_info( win, data );
return result;
}
std::string string_replace( std::string text, const std::string &before, const std::string &after )
{
// Check if there's something to replace (mandatory) and it's necessary (optional)
// Second condition assumes that text is much longer than both &before and &after.
if( before.empty() || before == after ) {
return text;
}
const size_t before_len = before.length();
const size_t after_len = after.length();
size_t pos = 0;
while( ( pos = text.find( before, pos ) ) != std::string::npos ) {
text.replace( pos, before_len, after );
pos += after_len;
}
return text;
}
std::string replace_colors( std::string text )
{
static const std::vector<std::pair<std::string, std::string>> info_colors = {
{"info", get_all_colors().get_name( c_cyan )},
{"stat", get_all_colors().get_name( c_light_blue )},
{"header", get_all_colors().get_name( c_magenta )},
{"bold", get_all_colors().get_name( c_white )},
{"dark", get_all_colors().get_name( c_dark_gray )},
{"good", get_all_colors().get_name( c_green )},
{"bad", get_all_colors().get_name( c_red )},
{"neutral", get_all_colors().get_name( c_yellow )}
};
for( const auto &elem : info_colors ) {
text = string_replace( text, "<" + elem.first + ">", "<color_" + elem.second + ">" );
text = string_replace( text, "</" + elem.first + ">", "</color>" );
}
return text;
}
static const std::array<translation, 3> item_filter_rule_intros {
to_translation( "Type part of an item's name to filter it." ),
to_translation( "Type part of an item's name to move nearby items to the bottom." ),
to_translation( "Type part of an item's name to move nearby items to the top." )
};
std::string item_filter_rule_string( const item_filter_type type )
{
std::ostringstream str;
const int tab_idx = static_cast<int>( type ) - static_cast<int>( item_filter_type::FIRST );
str << item_filter_rule_intros[tab_idx];
// NOLINTNEXTLINE(cata-text-style): literal comma
str << "\n\n" << _( "Separate multiple items with [<color_yellow>,</color>]." );
//~ An example of how to separate multiple items with a comma when filtering items.
str << "\n" << _( "Example: back,flash,aid, ,band" ); // NOLINT(cata-text-style): literal comma
str << "\n\n" << _( "Search [<color_yellow>c</color>]ategory, [<color_yellow>m</color>]aterial, "
"[<color_yellow>q</color>]uality, [<color_yellow>n</color>]otes, "
"[<color_yellow>s</color>]skill taught by books, "
"[<color_yellow>d</color>]isassembled components, or "
"items satisfying [<color_yellow>b</color>]oth conditions." );
//~ An example of how to filter items based on category or material.
str << "\n" << _( "Example: c:food,m:iron,q:hammering,n:toolshelf,d:pipe,s:devices,b:mre;sealed" );
str << "\n\n" << _( "To exclude items, place [<color_yellow>-</color>] in front. "
"Place [<color_yellow>--</color>] in front to include only matching items." );
//~ An example of how to exclude items with - when filtering items.
str << "\n" << _( "Example: steel,-chunk,--c:spare parts" );
return str.str();
}
void draw_item_filter_rules( const catacurses::window &win, const int starty, const int height,
const item_filter_type type )
{
// Clear every row, but the leftmost/rightmost pixels intact.
const int len = getmaxx( win ) - 2;