forked from CleverRaven/Cataclysm-DDA
/
output.cpp
1237 lines (1115 loc) · 33.6 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 <string>
#include <vector>
#include <cstdarg>
#include <cstring>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <algorithm>
#include "output.h"
#include "color.h"
#include "input.h"
#include "output.h"
#include "rng.h"
#include "keypress.h"
#include "options.h"
#include "cursesdef.h"
#include "catacharset.h"
#include "debug.h"
#include "uistate.h"
// Display data
int TERMX;
int TERMY;
int POSX;
int POSY;
int VIEW_OFFSET_X;
int VIEW_OFFSET_Y;
int TERRAIN_WINDOW_WIDTH;
int TERRAIN_WINDOW_HEIGHT;
int FULL_SCREEN_WIDTH;
int FULL_SCREEN_HEIGHT;
// utf8 version
std::vector<std::string> foldstring ( std::string str, int width ) {
std::vector<std::string> lines;
if ( width < 1 ) {
lines.push_back( str );
return lines;
}
std::stringstream sstr(str);
std::string strline;
while (std::getline(sstr, strline, '\n')) {
std::string wrapped = word_rewrap(strline, width);
std::stringstream swrapped(wrapped);
std::string wline;
while (std::getline(swrapped, wline, '\n')) {
lines.push_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;
std::vector<size_t>::iterator it;
for (it = tag_positions.begin(); it != tag_positions.end(); ++it) {
ret.push_back(s.substr(last_pos, *it-last_pos));
last_pos = *it;
}
// and the last (or only) one
ret.push_back(s.substr(last_pos, std::string::npos));
return ret;
}
// returns number of printed lines
int fold_and_print(WINDOW* w, int begin_y, int begin_x, int width, nc_color base_color, const char *mes, ...)
{
va_list ap;
va_start(ap,mes);
char buff[6000]; //TODO replace Magic Number
vsprintf(buff, mes, ap);
va_end(ap);
nc_color color = base_color;
std::vector<std::string> textformatted;
textformatted = foldstring(buff, width);
for (int line_num=0; line_num<textformatted.size(); line_num++)
{
wmove(w, line_num+begin_y, begin_x);
// split into colourable sections
std::vector<std::string> color_segments = split_by_color(textformatted[line_num]);
// for each section, get the colour, and print it
std::vector<std::string>::iterator it;
for (it = color_segments.begin(); it != color_segments.end(); ++it) {
if (!it->empty() && it->at(0) == '<') {
color = get_color_from_tag(*it, base_color);
}
wprintz(w, color, "%s", rm_prefix(*it).c_str());
}
}
return textformatted.size();
};
void center_print(WINDOW *w, int y, nc_color FG, const char *mes, ...)
{
va_list ap;
va_start(ap,mes);
char buff[6000]; //TODO replace Magic Number
vsprintf(buff, mes, ap);
va_end(ap);
int window_width = getmaxx(w);
int string_width = utf8_width(buff);
int x;
if (string_width >= window_width) {
x = 0;
} else {
x = (window_width - string_width) / 2;
}
mvwprintz(w, y, x, FG, buff);
}
void mvputch(int y, int x, nc_color FG, long ch)
{
attron(FG);
mvaddch(y, x, ch);
attroff(FG);
}
void wputch(WINDOW* w, nc_color FG, long ch)
{
wattron(w, FG);
waddch(w, ch);
wattroff(w, FG);
}
void mvwputch(WINDOW *w, int y, int x, nc_color FG, long ch)
{
wattron(w, FG);
mvwaddch(w, y, x, ch);
wattroff(w, FG);
}
void mvputch_inv(int y, int x, nc_color FG, long ch)
{
nc_color HC = invert_color(FG);
attron(HC);
mvaddch(y, x, ch);
attroff(HC);
}
void mvwputch_inv(WINDOW* w, int y, int x, nc_color FG, long ch)
{
nc_color HC = invert_color(FG);
wattron(w, HC);
mvwaddch(w, y, x, ch);
wattroff(w, HC);
}
void mvputch_hi(int y, int x, nc_color FG, long ch)
{
nc_color HC = hilite(FG);
attron(HC);
mvaddch(y, x, ch);
attroff(HC);
}
void mvwputch_hi(WINDOW* w, int y, int x, nc_color FG, long ch)
{
nc_color HC = hilite(FG);
wattron(w, HC);
mvwaddch(w, y, x, ch);
wattroff(w, HC);
}
void mvprintz(int y, int x, nc_color FG, const char *mes, ...)
{
va_list ap;
va_start(ap, mes);
char buff[6000];
vsprintf(buff, mes, ap);
va_end(ap);
attron(FG);
mvprintw(y, x, "%s", buff);
attroff(FG);
}
void mvwprintz(WINDOW* w, int y, int x, nc_color FG, const char *mes, ...)
{
va_list ap;
va_start(ap, mes);
// char buff[4096];
// vsprintf(buff, mes, ap);
char buff[6000];
vsprintf(buff, mes, ap);
va_end(ap);
wattron(w, FG);
// wmove(w, y, x);
mvwprintw(w, y, x, "%s", buff);
wattroff(w, FG);
}
void printz(nc_color FG, const char *mes, ...)
{
va_list ap;
va_start(ap,mes);
char buff[6000];
vsprintf(buff, mes, ap);
va_end(ap);
attron(FG);
printw("%s", buff);
attroff(FG);
}
void wprintz(WINDOW *w, nc_color FG, const char *mes, ...)
{
va_list ap;
va_start(ap,mes);
char buff[6000];
vsprintf(buff, mes, ap);
va_end(ap);
wattron(w, FG);
wprintw(w, "%s", buff);
wattroff(w, FG);
}
void draw_tabs(WINDOW *w, int active_tab, ...)
{
int win_width;
win_width = getmaxx(w);
std::vector<std::string> labels;
va_list ap;
va_start(ap, active_tab);
char *tmp;
while ((tmp = va_arg(ap, char *)))
labels.push_back((std::string)(tmp));
va_end(ap);
// Draw the line under the tabs
for (int x = 0; x < win_width; x++)
mvwputch(w, 2, x, c_white, LINE_OXOX);
int total_width = 0;
for (int i = 0; i < labels.size(); i++)
total_width += labels[i].length() + 6; // "< |four| >"
if (total_width > win_width) {
//debugmsg("draw_tabs not given enough space! %s", labels[0]);
return;
}
// Extra "buffer" space per each side of each tab
double buffer_extra = (win_width - total_width) / (labels.size() * 2);
int buffer = int(buffer_extra);
// Set buffer_extra to (0, 1); the "extra" whitespace that builds up
buffer_extra = buffer_extra - buffer;
int xpos = 0;
double savings = 0;
for (int i = 0; i < labels.size(); i++) {
int length = labels[i].length();
xpos += buffer + 2;
savings += buffer_extra;
if (savings > 1) {
savings--;
xpos++;
}
mvwputch(w, 0, xpos, c_white, LINE_OXXO);
mvwputch(w, 1, xpos, c_white, LINE_XOXO);
mvwputch(w, 0, xpos + length + 1, c_white, LINE_OOXX);
mvwputch(w, 1, xpos + length + 1, c_white, LINE_XOXO);
if (i == active_tab) {
mvwputch(w, 1, xpos - 2, h_white, '<');
mvwputch(w, 1, xpos + length + 3, h_white, '>');
mvwputch(w, 2, xpos, c_white, LINE_XOOX);
mvwputch(w, 2, xpos + length + 1, c_white, LINE_XXOO);
mvwprintz(w, 1, xpos + 1, h_white, labels[i].c_str());
for (int x = xpos + 1; x <= xpos + length; x++) {
mvwputch(w, 0, x, c_white, LINE_OXOX);
mvwputch(w, 2, x, c_black, 'x');
}
} else {
mvwputch(w, 2, xpos, c_white, LINE_XXOX);
mvwputch(w, 2, xpos + length + 1, c_white, LINE_XXOX);
mvwprintz(w, 1, xpos + 1, c_white, labels[i].c_str());
for (int x = xpos + 1; x <= xpos + length; x++)
mvwputch(w, 0, x, c_white, LINE_OXOX);
}
xpos += length + 1 + buffer;
}
}
void realDebugmsg(const char* filename, const char* line, const char *mes, ...)
{
va_list ap;
va_start(ap, mes);
char buff[1024];
vsprintf(buff, mes, ap);
va_end(ap);
fold_and_print(stdscr, 0, 0, getmaxx(stdscr), c_red, "DEBUG: %s\n Press spacebar...", buff);
std::ofstream fout;
fout.open("debug.log", std::ios_base::app | std::ios_base::out);
fout << filename << "[" << line << "]: " << buff << "\n";
fout.close();
while(getch() != ' ') ;
;
}
bool query_yn(const char *mes, ...)
{
bool force_uc = OPTIONS["FORCE_CAPITAL_YN"];
va_list ap;
va_start(ap, mes);
char buff[1024];
vsprintf(buff, mes, ap);
va_end(ap);
int win_width = utf8_width(buff) + 26;
WINDOW *w = newwin(3, win_width, (TERMY-3)/2, (TERMX > win_width) ? (TERMX-win_width)/2 : 0);
wborder(w, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
mvwprintz(w, 1, 1, c_ltred, (force_uc ? _("%s (Y/N - Case Sensitive)") : _("%s (y/n)")), buff);
wrefresh(w);
char ch;
do
ch = getch();
while (ch != '\n' && ch != ' ' && ch != KEY_ESCAPE && ch != 'Y' && ch != 'N' && (force_uc || (ch != 'y' && ch != 'n')));
werase(w);
wrefresh(w);
delwin(w);
refresh();
if (ch == 'Y' || ch == 'y')
return true;
return false;
}
int query_int(const char *mes, ...)
{
va_list ap;
va_start(ap, mes);
char buff[1024];
vsprintf(buff, mes, ap);
va_end(ap);
int win_width = utf8_width(buff) + 10;
WINDOW *w = newwin(3, win_width, (TERMY-3)/2, 11+((TERMX > win_width) ? (TERMX-win_width)/2 : 0));
wborder(w, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
mvwprintz(w, 1, 1, c_ltred, _("%s (0-9)"), buff);
wrefresh(w);
int temp;
do
temp = getch();
while ((temp-48)<0 || (temp-48)>9);
werase(w);
wrefresh(w);
delwin(w);
refresh();
return temp-48;
}
std::string string_input_popup(std::string title, int width, std::string input, std::string desc, std::string identifier, int max_length ) {
nc_color title_color = c_ltred;
nc_color desc_color = c_green;
std::vector<std::string> descformatted;
int titlesize = utf8_width(title.c_str());
int startx = titlesize + 2;
if ( max_length == 0 ) max_length = width;
int w_height=3;
int iPopupWidth = (width == 0) ? FULL_SCREEN_WIDTH : width + titlesize + 4;
if (iPopupWidth > FULL_SCREEN_WIDTH) {
iPopupWidth = FULL_SCREEN_WIDTH;
}
if ( desc.size() > 0 ) {
int twidth = utf8_width(desc.c_str());
if ( twidth > iPopupWidth-4 ) {
twidth=iPopupWidth-4;
}
descformatted = foldstring(desc, twidth);
w_height+=descformatted.size();
}
int starty=1+descformatted.size();
if ( max_length == 0 ) max_length = 1024;
int w_y=(TERMY-w_height)/2;
int w_x=((TERMX > iPopupWidth) ? (TERMX-iPopupWidth)/2 : 0);
WINDOW *w = newwin(w_height, iPopupWidth, w_y,
((TERMX > iPopupWidth) ? (TERMX-iPopupWidth)/2 : 0));
wborder(w, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
int endx=iPopupWidth-3;
for(int i=0; i < descformatted.size(); i++ ) {
mvwprintz(w, 1+i, 1, desc_color, "%s", descformatted[i].c_str() );
}
mvwprintz(w, starty, 1, title_color, "%s", title.c_str() );
long key=0;
int pos = -1;
std::string ret = string_input_win(w, input, max_length, startx, starty, endx, true, key, pos, identifier, w_x, w_y, true );
werase(w);
wrefresh(w);
delwin(w);
refresh();
return ret;
}
std::string string_input_win(WINDOW * w, std::string input, int max_length, int startx, int starty, int endx, bool loop, long & ch, int & pos, std::string identifier, int w_x, int w_y, bool dorefresh ) {
std::string ret = input;
nc_color string_color = c_magenta;
nc_color cursor_color = h_ltgray;
nc_color underscore_color = c_ltgray;
if ( pos == -1 ) {
pos = utf8_width(input.c_str());
}
int lastpos=pos;
int scrmax=endx-startx;
int shift = 0;
int lastshift=shift;
bool redraw=true;
do {
if ( pos < 0 ) pos=0;
if ( pos < shift ) {
shift=pos;
} else if ( pos > shift+scrmax ) {
shift=pos-scrmax;
}
if (shift < 0 ) shift=0;
if( redraw || lastshift != shift ) {
redraw=false;
for ( int reti=shift, scri=0; scri <= scrmax; reti++,scri++ ) {
if( reti < ret.size() ) {
mvwputch(w, starty, startx+scri, (reti == pos ? cursor_color : string_color ), ret[reti] );
} else {
mvwputch(w, starty, startx+scri, (reti == pos ? cursor_color : underscore_color ), '_');
}
}
} else if ( lastpos != pos ) {
if ( lastpos >= shift && lastpos <= shift+scrmax ) {
if ( lastpos-shift >= 0 && lastpos-shift < ret.size() ) {
mvwputch(w, starty, startx+lastpos, string_color, ret[lastpos-shift]);
} else {
mvwputch(w, starty, startx+lastpos, underscore_color, '_' );
}
}
if (pos < ret.size() ) {
mvwputch(w, starty, startx+pos, cursor_color, ret[pos-shift]);
} else {
mvwputch(w, starty, startx+pos, cursor_color, '_' );
}
}
lastpos=pos;
lastshift=shift;
if (dorefresh) wrefresh(w);
ch = getch();
if (ch == 27) { // Escape
return "";
} else if (ch == '\n') {
if(identifier.size() > 0 && ret.size() > 0 ) {
std::vector<std::string> * hist=uistate.gethistory(identifier);
if( hist!=NULL ) {
if ( hist->size() == 0 || (*hist)[hist->size()-1] != ret ) {
hist->push_back(ret);
}
}
}
return ret;
} else if (ch == KEY_UP ) {
if(identifier.size() > 0) {
std::vector<std::string> * hist=uistate.gethistory(identifier);
if(hist!=NULL) {
uimenu hmenu;
hmenu.w_height=3+hist->size();
if (w_y-hmenu.w_height < 0 ) {
hmenu.w_y=0;
hmenu.w_height = ( w_y-hmenu.w_y < 4 ? 4 : w_y-hmenu.w_y );
} else {
hmenu.w_y = w_y - hmenu.w_height;
}
hmenu.w_x = w_x;
hmenu.title = _("d: delete history");
hmenu.return_invalid = true;
for(int h=0; h < hist->size(); h++) {
hmenu.addentry(h,true,-2,(*hist)[h].c_str());
}
if ( ret.size() > 0 && ( hmenu.entries.size() == 0 || hmenu.entries[hist->size()-1].txt != ret ) ) {
hmenu.addentry(hist->size(),true,-2,ret);
hmenu.selected = hist->size();
} else {
hmenu.selected = hist->size()-1;
}
hmenu.query();
if ( hmenu.ret >= 0 && hmenu.entries[hmenu.ret].txt != ret ) {
ret=hmenu.entries[hmenu.ret].txt;
if( hmenu.ret < hist->size() ) {
hist->erase(hist->begin()+hmenu.ret);
hist->push_back(ret);
}
pos=ret.size();
redraw=true;
} else if ( hmenu.keypress == 'd' ) {
hist->clear();
}
}
}
} else if (ch == KEY_DOWN || ch == KEY_NPAGE || ch == KEY_PPAGE ) {
/* absolutely nothing */
} else if (ch == KEY_RIGHT ) {
if( pos+1 <= ret.size() ) {
pos++;
}
redraw=true;
} else if (ch == KEY_LEFT ) {
if ( pos > 0 ) {
pos--;
}
redraw=true;
} else if (ch == 0x15 ) { // ctrl-u: delete all the things
pos = 0;
ret.erase(0);
redraw = true;
} else if (ch == KEY_BACKSPACE || ch == 127) { // Move the cursor back and re-draw it
if( pos > 0 && pos <= ret.size() ) { // but silently drop input if we're at 0, instead of adding '^'
pos--; //TODO: it is safe now since you only input ascii chars
ret.erase(pos,1);
redraw = true;
}
} else if(ch==KEY_F(2)) {
std::string tmp = get_input_string_from_file();
int tmplen = utf8_width(tmp.c_str());
if(tmplen>0 && (tmplen+utf8_width(ret.c_str())<=max_length||max_length==0)) {
ret.append(tmp);
}
} else if( ch != 0 && ch != ERR && (ret.size() < max_length || max_length == 0) ) {
if ( pos == ret.size() ) {
ret += ch;
} else {
ret.insert(pos, 1, ch);
}
redraw = true;
pos++;
}
} while ( loop==true );
return ret;
}
char popup_getkey(const char *mes, ...)
{
va_list ap;
va_start(ap, mes);
char buff[8192];
vsprintf(buff, mes, ap);
va_end(ap);
std::string tmp = buff;
int width = 0;
int height = 2;
std::vector<std::string> folded = foldstring(tmp, 99999);
height += folded.size();
for(int i=0; i<folded.size(); i++) {
int cw = utf8_width(folded[i].c_str());
if(cw>width) {
width = cw;
}
}
width += 2;
if (height > FULL_SCREEN_HEIGHT)
height = FULL_SCREEN_HEIGHT;
WINDOW *w = newwin(height+1, width, (TERMY-(height+1))/2, (TERMX > width) ? (TERMX-width)/2 : 0);
wborder(w, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
for(int i=0; i<folded.size(); i++) {
mvwprintz(w, i+1, 1, c_white, folded[i].c_str());
}
wrefresh(w);
char ch = getch();;
werase(w);
wrefresh(w);
delwin(w);
refresh();
return ch;
}
int menu_vec(bool cancelable, const char *mes, std::vector<std::string> options) { // compatibility stub for uimenu(cancelable, mes, options)
return (int)uimenu(cancelable, mes, options);
}
int menu(bool cancelable, const char *mes, ...) { // compatibility stub for uimenu(cancelable, mes, ...)
va_list ap;
va_start(ap, mes);
char* tmp;
std::vector<std::string> options;
bool done = false;
while (!done) {
tmp = va_arg(ap, char*);
if (tmp != NULL) {
std::string strtmp = tmp;
options.push_back(strtmp);
} else {
done = true;
}
}
return (uimenu(cancelable, mes, options));
}
void popup_top(const char *mes, ...)
{
va_list ap;
va_start(ap, mes);
char buff[4096];
vsprintf(buff, mes, ap);
va_end(ap);
std::string tmp = buff;
int width = 0;
int height = 2;
std::vector<std::string> folded = foldstring(tmp, 99999);
height += folded.size();
for(int i=0; i<folded.size(); i++) {
int cw = utf8_width(folded[i].c_str());
if(cw>width) {
width = cw;
}
}
width += 2;
WINDOW *w = newwin(height, width, 0, (TERMX > width) ? (TERMX-width)/2 : 0);
wborder(w, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
for(int i=0; i<folded.size(); i++) {
mvwprintz(w, i+1, 1, c_white, folded[i].c_str());
}
wrefresh(w);
char ch;
do
ch = getch();
while(ch != ' ' && ch != '\n' && ch != KEY_ESCAPE);
werase(w);
wrefresh(w);
delwin(w);
refresh();
}
void popup(const char *mes, ...)
{
va_list ap;
va_start(ap, mes);
char buff[4096];
vsprintf(buff, mes, ap);
va_end(ap);
std::string tmp = buff;
int width = 0;
int height = 2;
std::vector<std::string> folded = foldstring(tmp, 99999);
height += folded.size();
for(int i=0; i<folded.size(); i++) {
int cw = utf8_width(folded[i].c_str());
if(cw>width) {
width = cw;
}
}
width += 2;
if (height > FULL_SCREEN_HEIGHT)
height = FULL_SCREEN_HEIGHT;
WINDOW *w = newwin(height, width, (TERMY-(height+1))/2, (TERMX > width) ? (TERMX-width)/2 : 0);
wborder(w, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
for(int i=0; i<folded.size(); i++) {
mvwprintz(w, i+1, 1, c_white, folded[i].c_str());
}
wrefresh(w);
char ch;
do
ch = getch();
while(ch != ' ' && ch != '\n' && ch != KEY_ESCAPE);
werase(w);
wrefresh(w);
delwin(w);
refresh();
}
void popup_nowait(const char *mes, ...)
{
va_list ap;
va_start(ap, mes);
char buff[4096];
vsprintf(buff, mes, ap);
va_end(ap);
std::string tmp = buff;
int width = 0;
int height = 2;
std::vector<std::string> folded = foldstring(tmp, 99999);
height += folded.size();
for(int i=0; i<folded.size(); i++) {
int cw = utf8_width(folded[i].c_str());
if(cw>width) {
width = cw;
}
}
width += 2;
if (height > FULL_SCREEN_HEIGHT)
height = FULL_SCREEN_HEIGHT;
WINDOW *w = newwin(height, width, (TERMY-(height+1))/2, (TERMX > width) ? (TERMX-width)/2 : 0);
wborder(w, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
for(int i=0; i<folded.size(); i++) {
mvwprintz(w, i+1, 1, c_white, folded[i].c_str());
}
wrefresh(w);
delwin(w);
refresh();
}
void full_screen_popup(const char* mes, ...)
{
va_list ap;
va_start(ap, mes);
char buff[8192];
vsprintf(buff, mes, ap);
va_end(ap);
std::string tmp = buff;
int width = 0;
int height = 2;
std::vector<std::string> folded = foldstring(tmp, FULL_SCREEN_WIDTH-3);
height += folded.size();
for(int i=0; i<folded.size(); i++) {
int cw = utf8_width(folded[i].c_str());
if(cw>width) {
width = cw;
}
}
width += 2;
WINDOW *w = newwin(FULL_SCREEN_HEIGHT, FULL_SCREEN_WIDTH,
(TERMY > FULL_SCREEN_HEIGHT) ? (TERMY-FULL_SCREEN_HEIGHT)/2 : 0,
(TERMX > FULL_SCREEN_WIDTH) ? (TERMX-FULL_SCREEN_WIDTH)/2 : 0);
wborder(w, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
for(int i=0; i<folded.size(); i++) {
mvwprintz(w, i+1, 2, c_white, folded[i].c_str());
}
wrefresh(w);
char ch;
do
ch = getch();
while(ch != ' ' && ch != '\n' && ch != KEY_ESCAPE);
werase(w);
wrefresh(w);
delwin(w);
refresh();
}
//note that passing in iteminfo instances with sType == "MENU" or "DESCRIPTION" does special things
//if sType == "MENU", sFmt == "iOffsetY" or "iOffsetX" also do special things
//otherwise if sType == "MENU", dValue can be used to control color
//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
int compare_split_screen_popup(int iLeft, int iWidth, int iHeight, std::string sItemName, std::vector<iteminfo> vItemDisplay, std::vector<iteminfo> vItemCompare, int selected)
{
WINDOW* w = newwin(iHeight, iWidth, VIEW_OFFSET_Y, iLeft + VIEW_OFFSET_X);
mvwprintz(w, 1, 2, c_white, sItemName.c_str());
int line_num = 3;
int iStartX = 0;
bool bStartNewLine = true;
int selected_ret='\n';
std::string spaces(iWidth-2, ' ');
for (int i = 0; i < vItemDisplay.size(); i++) {
if (vItemDisplay[i].sType == "MENU") {
if (vItemDisplay[i].sFmt == "iOffsetY") {
line_num += int(vItemDisplay[i].dValue);
} else if (vItemDisplay[i].sFmt == "iOffsetX") {
iStartX = int(vItemDisplay[i].dValue);
} else {
nc_color nameColor = c_ltgreen; //pre-existing behavior, so make it the default
//patched to allow variable "name" coloring, e.g. for item examining
nc_color bgColor = c_white; //yes the name makes no sense
if (vItemDisplay[i].dValue >= 0) {
if (vItemDisplay[i].dValue < .1 && vItemDisplay[i].dValue > -.1){
nameColor = c_ltgray;
} else {
nameColor = c_ltred;
}
}
if ( i == selected && vItemDisplay[i].sName != "" ) {
bgColor = h_white;
selected_ret=(int)vItemDisplay[i].sName.c_str()[0]; // fixme: sanity check(?)
}
mvwprintz(w, line_num, 1, bgColor, "%s", spaces.c_str() );
shortcut_print(w, line_num, iStartX, bgColor, nameColor, vItemDisplay[i].sFmt.c_str());
line_num++;
}
} else if (vItemDisplay[i].sType == "DESCRIPTION") {
std::string sText = word_rewrap(vItemDisplay[i].sName, iWidth-4);
std::stringstream ss(sText);
std::string l;
while (std::getline(ss, l, '\n')) {
line_num++;
mvwprintz(w, line_num, 2, c_white, l.c_str());
}
} else {
if (bStartNewLine) {
mvwprintz(w, line_num, 2, c_white, "%s", (vItemDisplay[i].sName).c_str());
bStartNewLine = false;
} else {
wprintz(w, c_white, "%s", (vItemDisplay[i].sName).c_str());
}
std::string sPlus = vItemDisplay[i].sPlus;
std::string sFmt = vItemDisplay[i].sFmt;
std::string sNum = " ";
std::string sPost = "";
//A bit tricky, find %d and split the string
size_t pos = sFmt.find("<num>");
if(pos != std::string::npos)
{
wprintz(w, c_white, sFmt.substr(0, pos).c_str());
sPost = sFmt.substr(pos+5);
}
else
{
wprintz(w, c_white, sFmt.c_str());
}
if (vItemDisplay[i].sValue != "-999") {
nc_color thisColor = c_white;
for (int k = 0; k < vItemCompare.size(); k++) {
if (vItemCompare[k].sValue != "-999") {
if (vItemDisplay[i].sName == vItemCompare[k].sName) {
if (vItemDisplay[i].dValue > vItemCompare[k].dValue - .1 &&
vItemDisplay[i].dValue < vItemCompare[k].dValue + .1) {
thisColor = c_white;
} else if (vItemDisplay[i].dValue > vItemCompare[k].dValue) {
if (vItemDisplay[i].bLowerIsBetter) {
thisColor = c_ltred;
} else {
thisColor = c_ltgreen;
}
} else if (vItemDisplay[i].dValue < vItemCompare[k].dValue) {
if (vItemDisplay[i].bLowerIsBetter) {
thisColor = c_ltgreen;
} else {
thisColor = c_ltred;
}
}
break;
}
}
}
if (vItemDisplay[i].is_int == true) {
wprintz(w, thisColor, "%s%.0f", sPlus.c_str(), vItemDisplay[i].dValue);
} else {
wprintz(w, thisColor, "%s%.1f", sPlus.c_str(), vItemDisplay[i].dValue);
}
}
wprintz(w, c_white, sPost.c_str());
if (vItemDisplay[i].bNewLine) {
line_num++;
bStartNewLine = true;
}
}
}
wborder(w, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
int ch = (int)' ';
wrefresh(w);
if (iLeft > 0) {
ch = (int)getch();
if ( selected > 0 && ( ch == '\n' || ch == KEY_RIGHT ) && selected_ret != 0 ) {
ch=selected_ret;
} else if ( selected == KEY_LEFT ) {
ch=(int)' ';
}
//werase(w);
//wrefresh(w);
delwin(w);
//refresh();
}
return ch;
}
char rand_char()
{
switch (rng(0, 9)) {
case 0: return '|';
case 1: return '-';
case 2: return '#';
case 3: return '?';
case 4: return '&';
case 5: return '.';
case 6: return '%';
case 7: return '{';
case 8: return '*';
case 9: return '^';
}
return '?';
}
// this translates symbol y, u, n, b to NW, NE, SE, SW lines correspondingly
// h, j, c to horizontal, vertical, cross correspondingly
long special_symbol (long sym)
{
switch (sym)
{
case 'j': return LINE_XOXO;
case 'h': return LINE_OXOX;
case 'c': return LINE_XXXX;
case 'y': return LINE_OXXO;
case 'u': return LINE_OOXX;
case 'n': return LINE_XOOX;
case 'b': return LINE_XXOO;
default: return sym;
}
}
// find the position of each non-printing tag in a string
std::vector<size_t> get_tag_positions(const std::string &s)
{
std::vector<size_t> ret;
size_t pos = s.find("<color_", 0, 7);
while (pos != std::string::npos) {
ret.push_back(pos);
pos = s.find("<color_", pos+1, 7);
}
pos = s.find("</color>", 0, 8);
while (pos != std::string::npos) {
ret.push_back(pos);
pos = s.find("</color>", pos+1, 8);
}
std::sort(ret.begin(), ret.end());
return ret;
}
// utf-8 version
std::string word_rewrap (const std::string &ins, int width){
std::ostringstream o;
std::string in = ins;
std::replace(in.begin(), in.end(), '\n', ' ');
// find non-printing tags
std::vector<size_t> tag_positions = get_tag_positions(in);
int lastwb = 0; //last word break
int lastout = 0;
const char *instr = in.c_str();
bool skipping_tag = false;
for (int j=0, x=0; j<in.size(); ) {
const char* ins = instr+j;
int len = ANY_LENGTH;
unsigned uc = UTF8_getch(&ins, &len);
if (uc == '<') { // maybe skip non-printing tag
std::vector<size_t>::iterator it;
for (it = tag_positions.begin(); it != tag_positions.end(); ++it) {
if (*it == j) {
skipping_tag = true;
break;
}
}
}
j += ANY_LENGTH-len;
if (skipping_tag) {
if (uc == '>') {
skipping_tag = false;
}
continue;
}
x += mk_wcwidth(uc);
if (x >= width) {
if (lastwb == lastout) {
lastwb = j;
}
for(int k=lastout; k<lastwb; k++) {
o << in[k];
}
o << '\n';