forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_menu.cpp
1118 lines (1021 loc) · 41.1 KB
/
main_menu.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 "main_menu.h"
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <exception>
#include <functional>
#include <istream>
#include <memory>
#include <string>
#include "auto_pickup.h"
#include "avatar.h"
#include "cata_utility.h"
#include "catacharset.h"
#include "character_id.h"
#include "color.h"
#include "debug.h"
#include "enums.h"
#include "filesystem.h"
#include "game.h"
#include "gamemode.h"
#include "get_version.h"
#include "help.h"
#include "loading_ui.h"
#include "localized_comparator.h"
#include "mapbuffer.h"
#include "mapsharing.h"
#include "messages.h"
#include "music.h"
#include "optional.h"
#include "options.h"
#include "output.h"
#include "overmapbuffer.h"
#include "path_info.h"
#include "popup.h"
#include "safemode_ui.h"
#include "scenario.h"
#include "sdlsound.h"
#include "sounds.h"
#include "string_formatter.h"
#include "text_snippets.h"
#include "translations.h"
#include "ui_manager.h"
#include "wcwidth.h"
#include "worldfactory.h"
enum class main_menu_opts : int {
MOTD = 0,
NEWCHAR = 1,
LOADCHAR = 2,
WORLD = 3,
SPECIAL = 4,
SETTINGS = 5,
HELP = 6,
CREDITS = 7,
QUIT = 8
};
static constexpr int max_menu_opts = 8;
static int getopt( main_menu_opts o )
{
return static_cast<int>( o );
}
void main_menu::on_move() const
{
sfx::play_variant_sound( "menu_move", "default", 100 );
}
void main_menu::on_error()
{
sfx::play_variant_sound( "menu_error", "default", 100 );
}
//CJK characters have a width of 2, etc
static int utf8_width_notags( const char *s )
{
int len = strlen( s );
const char *ptr = s;
int w = 0;
bool inside_tag = false;
while( len > 0 ) {
uint32_t ch = UTF8_getch( &ptr, &len );
if( ch == UNKNOWN_UNICODE ) {
continue;
}
if( ch == '<' ) {
inside_tag = true;
} else if( ch == '>' ) {
inside_tag = false;
continue;
}
if( inside_tag ) {
continue;
}
w += mk_wcwidth( ch );
}
return w;
}
std::vector<int> main_menu::print_menu_items( const catacurses::window &w_in,
const std::vector<std::string> &vItems,
size_t iSel, point offset, int spacing, bool main )
{
const point win_offset( getbegx( w_in ), getbegy( w_in ) );
std::vector<int> ret;
std::string text;
for( size_t i = 0; i < vItems.size(); ++i ) {
if( i > 0 ) {
text += std::string( spacing, ' ' );
}
ret.push_back( utf8_width_notags( text.c_str() ) );
std::string temp = shortcut_text( iSel == i ? hilite( c_yellow ) : c_yellow, vItems[i] );
text += string_format( "[%s]", colorize( temp, iSel == i ? hilite( c_white ) : c_white ) );
}
int text_width = utf8_width_notags( text.c_str() );
if( text_width > getmaxx( w_in ) ) {
offset.y -= std::ceil( text_width / getmaxx( w_in ) );
}
std::vector<std::string> menu_txt = foldstring( text, getmaxx( w_in ), ']' );
int y_off = 0;
int sel_opt = 0;
for( const std::string &txt : menu_txt ) {
trim_and_print( w_in, offset + point( 0, y_off ), getmaxx( w_in ), c_white, txt );
if( !main ) {
y_off++;
continue;
}
std::vector<std::string> tmp_chars = utf8_display_split( remove_color_tags( txt ) );
for( int x = 0; static_cast<size_t>( x ) < tmp_chars.size(); x++ ) {
if( tmp_chars.at( x ) == "[" ) {
for( int x2 = x; static_cast<size_t>( x2 ) < tmp_chars.size(); x2++ ) {
if( tmp_chars.at( x2 ) == "]" ) {
inclusive_rectangle<point> rec( win_offset + offset + point( x, y_off ),
win_offset + offset + point( x2, y_off ) );
main_menu_button_map.emplace_back( rec, sel_opt++ );
break;
}
}
}
}
y_off++;
}
return ret;
}
void main_menu::display_sub_menu( int sel, const point &bottom_left, int sel_line )
{
main_menu_sub_button_map.clear();
std::vector<std::string> sub_opts;
int xlen = 0;
main_menu_opts sel_o = static_cast<main_menu_opts>( sel );
switch( sel_o ) {
case main_menu_opts::CREDITS:
display_text( mmenu_credits, _( "Credits" ), sel_line );
return;
case main_menu_opts::MOTD:
//~ Message Of The Day
display_text( mmenu_motd, _( "MOTD" ), sel_line );
return;
case main_menu_opts::SPECIAL:
for( int i = 1; i < static_cast<int>( special_game_type::NUM_SPECIAL_GAME_TYPES ); i++ ) {
std::string spec_name = special_game_name( static_cast<special_game_type>( i ) );
nc_color clr = i == sel2 ? hilite( c_yellow ) : c_yellow;
sub_opts.push_back( shortcut_text( clr, spec_name ) );
int len = utf8_width( shortcut_text( clr, spec_name ), true );
if( len > xlen ) {
xlen = len;
}
}
break;
case main_menu_opts::SETTINGS:
for( int i = 0; static_cast<size_t>( i ) < vSettingsSubItems.size(); ++i ) {
nc_color clr = i == sel2 ? hilite( c_yellow ) : c_yellow;
sub_opts.push_back( shortcut_text( clr, vSettingsSubItems[i] ) );
int len = utf8_width( shortcut_text( clr, vSettingsSubItems[i] ), true );
if( len > xlen ) {
xlen = len;
}
}
break;
case main_menu_opts::NEWCHAR:
for( int i = 0; static_cast<size_t>( i ) < vNewGameSubItems.size(); i++ ) {
nc_color clr = i == sel2 ? hilite( c_yellow ) : c_yellow;
sub_opts.push_back( shortcut_text( clr, vNewGameSubItems[i] ) );
int len = utf8_width( shortcut_text( clr, vNewGameSubItems[i] ), true );
if( len > xlen ) {
xlen = len;
}
}
break;
case main_menu_opts::LOADCHAR:
case main_menu_opts::WORLD: {
const bool extra_opt = sel == getopt( main_menu_opts::WORLD );
if( extra_opt ) {
sub_opts.emplace_back( colorize( _( "Create World" ), sel2 == 0 ? hilite( c_yellow ) : c_yellow ) );
xlen = utf8_width( sub_opts.back(), true );
}
std::vector<std::string> all_worldnames = world_generator->all_worldnames();
for( int i = 0; static_cast<size_t>( i ) < all_worldnames.size(); i++ ) {
int savegames_count = world_generator->get_world( all_worldnames[i] )->world_saves.size();
nc_color clr = c_white;
if( all_worldnames[i] == "TUTORIAL" || all_worldnames[i] == "DEFENSE" ) {
clr = c_light_cyan;
}
sub_opts.push_back( colorize( string_format( "%s (%d)", all_worldnames[i], savegames_count ),
( sel2 == i + ( extra_opt ? 1 : 0 ) ) ? hilite( clr ) : clr ) );
int len = utf8_width( sub_opts.back(), true );
if( len > xlen ) {
xlen = len;
}
}
}
break;
case main_menu_opts::HELP:
case main_menu_opts::QUIT:
default:
return;
}
if( sub_opts.empty() ) {
return;
}
const point top_left( bottom_left + point( 0, -( sub_opts.size() + 1 ) ) );
catacurses::window w_sub = catacurses::newwin( sub_opts.size() + 2, xlen + 4, top_left );
werase( w_sub );
draw_border( w_sub, c_white );
for( int y = 0; static_cast<size_t>( y ) < sub_opts.size(); y++ ) {
std::string opt = ( sel2 == y ? "» " : " " ) + sub_opts[y];
int padding = ( xlen + 2 ) - utf8_width( opt, true );
opt.append( padding, ' ' );
nc_color clr = sel2 == y ? hilite( c_white ) : c_white;
trim_and_print( w_sub, point( 1, y + 1 ), xlen + 2, clr, opt );
inclusive_rectangle<point> rec( top_left + point( 1, y + 1 ), top_left + point( xlen + 2, y + 1 ) );
main_menu_sub_button_map.emplace_back( rec, std::pair<int, int> { sel, y } );
}
wnoutrefresh( w_sub );
}
void main_menu::print_menu( const catacurses::window &w_open, int iSel, const point &offset,
int sel_line )
{
main_menu_button_map.clear();
// Clear Lines
werase( w_open );
// Define window size
int window_width = getmaxx( w_open );
int window_height = getmaxy( w_open );
// Draw horizontal line
for( int i = 1; i < window_width - 1; ++i ) {
mvwputch( w_open, point( i, window_height - 4 ), c_white, LINE_OXOX );
}
if( iSel == getopt( main_menu_opts::NEWCHAR ) ) {
center_print( w_open, window_height - 2, c_yellow, vNewGameHints[sel2] );
} else {
center_print( w_open, window_height - 2, c_red,
_( "Bugs? Suggestions? Use links in MOTD to report them." ) );
}
center_print( w_open, window_height - 1, c_light_cyan, string_format( _( "Tip of the day: %s" ),
vdaytip ) );
int iLine = 0;
const int iOffsetX = ( window_width - FULL_SCREEN_WIDTH ) / 2;
if( get_option<bool>( "SEASONAL_TITLE" ) ) {
switch( current_holiday ) {
case holiday::new_year:
case holiday::easter:
break;
case holiday::halloween:
fold_and_print_from( w_open, point_zero, 30, 0, c_white, halloween_spider() );
fold_and_print_from( w_open, point( getmaxx( w_open ) - 25, offset.y - 8 ),
25, 0, c_white, halloween_graves() );
break;
case holiday::thanksgiving:
case holiday::christmas:
case holiday::none:
case holiday::num_holiday:
default:
break;
}
}
if( mmenu_title.size() > 1 ) {
for( const std::string &i_title : mmenu_title ) {
nc_color cur_color = c_white;
nc_color base_color = c_white;
print_colored_text( w_open, point( iOffsetX, iLine++ ), cur_color, base_color, i_title );
}
} else {
center_print( w_open, iLine++, c_light_cyan, mmenu_title[0] );
}
iLine++;
center_print( w_open, iLine, c_light_blue, string_format( _( "Version: %s" ),
getVersionString() ) );
int menu_length = 0;
for( size_t i = 0; i < vMenuItems.size(); ++i ) {
menu_length += utf8_width_notags( vMenuItems[i].c_str() ) + 2;
if( !vMenuHotkeys[i].empty() ) {
menu_length += utf8_width( vMenuHotkeys[i][0] );
}
}
const int free_space = std::max( 0, window_width - menu_length - offset.x );
const int spacing = free_space / ( static_cast<int>( vMenuItems.size() ) + 1 );
const int width_of_spacing = spacing * ( vMenuItems.size() + 1 );
const int adj_offset = std::max( 0, ( free_space - width_of_spacing ) / 2 );
const int final_offset = offset.x + adj_offset + spacing;
std::vector<int> offsets =
print_menu_items( w_open, vMenuItems, iSel, point( final_offset, offset.y ), spacing, true );
wnoutrefresh( w_open );
const point p_offset( catacurses::getbegx( w_open ), catacurses::getbegy( w_open ) );
display_sub_menu( iSel, p_offset + point( offsets[iSel], offset.y - 2 ), sel_line );
}
std::vector<std::string> main_menu::load_file( const std::string &path,
const std::string &alt_text ) const
{
std::vector<std::string> result;
read_from_file_optional( path, [&result]( std::istream & fin ) {
std::string line;
while( std::getline( fin, line ) ) {
if( !line.empty() && line[0] == '#' ) {
continue;
}
result.push_back( line );
}
} );
if( result.empty() ) {
result.push_back( alt_text );
}
return result;
}
holiday main_menu::get_holiday_from_time()
{
return ::get_holiday_from_time( 0, true );
}
void main_menu::init_windows()
{
if( LAST_TERM == point( TERMX, TERMY ) ) {
return;
}
// main window should also expand to use available display space.
// expanding to evenly use up half of extra space, for now.
extra_w = ( ( TERMX - FULL_SCREEN_WIDTH ) / 2 ) - 1;
int extra_h = ( ( TERMY - FULL_SCREEN_HEIGHT ) / 2 ) - 1;
extra_w = ( extra_w > 0 ? extra_w : 0 );
extra_h = ( extra_h > 0 ? extra_h : 0 );
const int total_w = FULL_SCREEN_WIDTH + extra_w;
const int total_h = FULL_SCREEN_HEIGHT + extra_h;
// position of window within main display
const point p0( ( TERMX - total_w ) / 2, ( TERMY - total_h ) / 2 );
w_open = catacurses::newwin( total_h, total_w, p0 );
menu_offset.y = total_h - 3;
// note: if iMenuOffset is changed,
// please update MOTD and credits to indicate how long they can be.
LAST_TERM = point( TERMX, TERMY );
}
void main_menu::init_strings()
{
// ASCII Art
mmenu_title = load_file( PATH_INFO::title( current_holiday ), _( "Cataclysm: Dark Days Ahead" ) );
// MOTD
auto motd = load_file( PATH_INFO::motd(), _( "No message today." ) );
mmenu_motd.clear();
for( const std::string &line : motd ) {
mmenu_motd += ( line.empty() ? " " : line ) + "\n";
}
mmenu_motd = colorize( mmenu_motd, c_light_red );
mmenu_motd_len = foldstring( mmenu_motd, FULL_SCREEN_WIDTH - 2 ).size();
// Credits
mmenu_credits.clear();
read_from_file_optional( PATH_INFO::credits(), [&]( std::istream & stream ) {
std::string line;
while( std::getline( stream, line ) ) {
if( line[0] != '#' ) {
mmenu_credits += ( line.empty() ? " " : line ) + "\n";
}
}
} );
if( mmenu_credits.empty() ) {
mmenu_credits = _( "No credits information found." );
}
mmenu_credits_len = foldstring( mmenu_credits, FULL_SCREEN_WIDTH - 2 ).size();
// fill menu with translated menu items
vMenuItems.clear();
vMenuItems.emplace_back( pgettext( "Main Menu", "<M|m>OTD" ) );
vMenuItems.emplace_back( pgettext( "Main Menu", "<N|n>ew Game" ) );
vMenuItems.emplace_back( pgettext( "Main Menu", "Lo<a|A>d" ) );
vMenuItems.emplace_back( pgettext( "Main Menu", "<W|w>orld" ) );
vMenuItems.emplace_back( pgettext( "Main Menu", "<S|s>pecial" ) );
vMenuItems.emplace_back( pgettext( "Main Menu", "Se<t|T>tings" ) );
vMenuItems.emplace_back( pgettext( "Main Menu", "H<e|E|?>lp" ) );
vMenuItems.emplace_back( pgettext( "Main Menu", "<C|c>redits" ) );
vMenuItems.emplace_back( pgettext( "Main Menu", "<Q|q>uit" ) );
// new game menu items
vNewGameSubItems.clear();
vNewGameSubItems.emplace_back( pgettext( "Main Menu|New Game", "C<u|U>stom Character" ) );
vNewGameSubItems.emplace_back( pgettext( "Main Menu|New Game", "<P|p>reset Character" ) );
vNewGameSubItems.emplace_back( pgettext( "Main Menu|New Game", "<R|r>andom Character" ) );
if( !MAP_SHARING::isSharing() ) { // "Play Now" function doesn't play well together with shared maps
vNewGameSubItems.emplace_back( pgettext( "Main Menu|New Game",
"Play Now! (<D|d>efault Scenario)" ) );
vNewGameSubItems.emplace_back( pgettext( "Main Menu|New Game", "Play N<o|O>w!" ) );
}
vNewGameHints.clear();
vNewGameHints.emplace_back(
_( "Allows you to fully customize points pool, scenario, and character's profession, stats, traits, skills and other parameters." ) );
vNewGameHints.emplace_back( _( "Select from one of previously created character templates." ) );
vNewGameHints.emplace_back(
_( "Creates random character, but lets you preview the generated character and the scenario and change character and/or scenario if needed." ) );
vNewGameHints.emplace_back(
_( "Puts you right in the game, randomly choosing character's traits, profession, skills and other parameters. Scenario is fixed to Evacuee." ) );
vNewGameHints.emplace_back(
_( "Puts you right in the game, randomly choosing scenario and character's traits, profession, skills and other parameters." ) );
vNewGameHotkeys.clear();
vNewGameHotkeys.reserve( vNewGameSubItems.size() );
for( const std::string &item : vNewGameSubItems ) {
vNewGameHotkeys.push_back( get_hotkeys( item ) );
}
// determine hotkeys from translated menu item text
vMenuHotkeys.clear();
for( const std::string &item : vMenuItems ) {
vMenuHotkeys.push_back( get_hotkeys( item ) );
}
vWorldSubItems.clear();
vWorldSubItems.emplace_back( pgettext( "Main Menu|World", "<D|d>elete World" ) );
vWorldSubItems.emplace_back( pgettext( "Main Menu|World", "<R|r>eset World" ) );
vWorldSubItems.emplace_back( pgettext( "Main Menu|World", "Sh<o|O>w World Mods" ) );
vWorldSubItems.emplace_back( pgettext( "Main Menu|World", "Copy World Sett<i|I>ngs" ) );
vWorldSubItems.emplace_back( pgettext( "Main Menu|World", "Character to Tem<p|P>late" ) );
vWorldHotkeys.clear();
for( const std::string &item : vWorldSubItems ) {
vWorldHotkeys.push_back( get_hotkeys( item ) );
}
vSettingsSubItems.clear();
vSettingsSubItems.emplace_back( pgettext( "Main Menu|Settings", "<O|o>ptions" ) );
vSettingsSubItems.emplace_back( pgettext( "Main Menu|Settings", "Ke<y|Y>bindings" ) );
vSettingsSubItems.emplace_back( pgettext( "Main Menu|Settings", "A<u|U>topickup" ) );
vSettingsSubItems.emplace_back( pgettext( "Main Menu|Settings", "Sa<f|F>emode" ) );
vSettingsSubItems.emplace_back( pgettext( "Main Menu|Settings", "Colo<r|R>s" ) );
vSettingsHotkeys.clear();
for( const std::string &item : vSettingsSubItems ) {
vSettingsHotkeys.push_back( get_hotkeys( item ) );
}
loading_ui ui( false );
g->load_core_data( ui );
vdaytip = SNIPPET.random_from_category( "tip" ).value_or( translation() ).translated();
}
void main_menu::display_text( const std::string &text, const std::string &title, int &selected )
{
const int w_open_height = getmaxy( w_open );
const int b_height = FULL_SCREEN_HEIGHT - clamp( ( FULL_SCREEN_HEIGHT - w_open_height ) + 4, 0, 4 );
const int vert_off = clamp( ( w_open_height - FULL_SCREEN_HEIGHT ) / 2, getbegy( w_open ), TERMY );
catacurses::window w_border = catacurses::newwin( b_height, FULL_SCREEN_WIDTH,
point( clamp( ( TERMX - FULL_SCREEN_WIDTH ) / 2, 0, TERMX ), vert_off ) );
catacurses::window w_text = catacurses::newwin( b_height - 2, FULL_SCREEN_WIDTH - 2,
point( 1 + clamp( ( TERMX - FULL_SCREEN_WIDTH ) / 2, 0, TERMX ), 1 + vert_off ) );
draw_border( w_border, BORDER_COLOR, title );
int width = FULL_SCREEN_WIDTH - 2;
int height = b_height - 2;
const auto vFolded = foldstring( text, width );
int iLines = vFolded.size();
fold_and_print_from( w_text, point_zero, width, selected, c_light_gray, text );
draw_scrollbar( w_border, selected, height, iLines, point_south, BORDER_COLOR, true );
wnoutrefresh( w_border );
wnoutrefresh( w_text );
}
void main_menu::load_char_templates()
{
templates.clear();
for( std::string path : get_files_from_path( ".template", PATH_INFO::templatedir(), false,
true ) ) {
path.erase( path.find( ".template" ), std::string::npos );
path.erase( 0, path.find_last_of( "\\/" ) + 1 );
templates.push_back( path );
}
std::sort( templates.begin(), templates.end(), localized_compare );
std::reverse( templates.begin(), templates.end() );
}
bool main_menu::opening_screen()
{
// set holiday based on local system time
current_holiday = get_holiday_from_time();
if( music::get_music_id() != music::music_id::title ) {
music::deactivate_music_id_all();
} else {
play_music( music::get_music_id_string() );
}
world_generator->set_active_world( nullptr );
world_generator->init();
init_strings();
load_char_templates();
ctxt.register_cardinal();
ctxt.register_action( "NEXT_TAB" );
ctxt.register_action( "PREV_TAB" );
ctxt.register_action( "PAGE_UP" );
ctxt.register_action( "PAGE_DOWN" );
ctxt.register_action( "CONFIRM" );
ctxt.register_action( "QUIT" );
// for mouse selection
ctxt.register_action( "SELECT" );
ctxt.register_action( "MOUSE_MOVE" );
ctxt.register_action( "SCROLL_UP" );
ctxt.register_action( "SCROLL_DOWN" );
// for the menu shortcuts
ctxt.register_action( "ANY_INPUT" );
bool start = false;
avatar &player_character = get_avatar();
player_character = avatar();
int sel_line = 0;
size_t last_world_pos = 0;
// Make [Load Game] the default cursor position if there's game save available
if( !world_generator->all_worldnames().empty() ) {
std::vector<std::string> worlds = world_generator->all_worldnames();
last_world_pos = std::find( worlds.begin(), worlds.end(),
world_generator->last_world_name ) - worlds.begin();
if( last_world_pos >= worlds.size() ) {
last_world_pos = 0;
}
sel1 = getopt( main_menu_opts::LOADCHAR );
sel2 = last_world_pos;
}
background_pane background;
ui_adaptor ui;
ui.on_redraw( [&]( const ui_adaptor & ) {
print_menu( w_open, sel1, menu_offset, sel_line );
} );
ui.on_screen_resize( [this]( ui_adaptor & ui ) {
init_windows();
ui.position_from_window( w_open );
} );
ui.mark_resize();
while( !start ) {
ui_manager::redraw();
std::string action = ctxt.handle_input();
input_event sInput = ctxt.get_raw_input();
// check automatic menu shortcuts
for( int i = 0; static_cast<size_t>( i ) < vMenuHotkeys.size(); ++i ) {
for( const std::string &hotkey : vMenuHotkeys[i] ) {
if( sInput.text == hotkey && sel1 != i ) {
sel1 = i;
sel2 = i == getopt( main_menu_opts::LOADCHAR ) ? last_world_pos : 0;
sel_line = 0;
if( i == getopt( main_menu_opts::HELP ) ) {
action = "CONFIRM";
} else if( i == getopt( main_menu_opts::QUIT ) ) {
action = "QUIT";
}
}
}
}
if( sel1 == getopt( main_menu_opts::SETTINGS ) ) {
for( int i = 0; static_cast<size_t>( i ) < vSettingsSubItems.size(); ++i ) {
for( const std::string &hotkey : vSettingsHotkeys[i] ) {
if( sInput.text == hotkey ) {
sel2 = i;
action = "CONFIRM";
}
}
}
}
if( sel1 == getopt( main_menu_opts::NEWCHAR ) ) {
for( int i = 0; static_cast<size_t>( i ) < vNewGameSubItems.size(); ++i ) {
for( const std::string &hotkey : vNewGameHotkeys[i] ) {
if( sInput.text == hotkey ) {
sel2 = i;
action = "CONFIRM";
}
}
}
}
// handle mouse click
if( action == "SELECT" || action == "MOUSE_MOVE" ) {
cata::optional<point> coord = ctxt.get_coordinates_text( catacurses::stdscr );
for( const auto &it : main_menu_button_map ) {
if( coord.has_value() && it.first.contains( coord.value() ) ) {
if( sel1 != it.second ) {
sel1 = it.second;
sel2 = sel1 == getopt( main_menu_opts::LOADCHAR ) ? last_world_pos : 0;
sel_line = 0;
on_move();
}
if( action == "SELECT" &&
( sel1 == getopt( main_menu_opts::HELP ) || sel1 == getopt( main_menu_opts::QUIT ) ) ) {
action = "CONFIRM";
}
ui_manager::redraw();
break;
}
}
for( const auto &it : main_menu_sub_button_map ) {
if( coord.has_value() && it.first.contains( coord.value() ) ) {
if( sel1 != it.second.first || sel2 != it.second.second ) {
on_move();
}
sel1 = it.second.first;
sel2 = it.second.second;
sel_line = 0;
if( action == "SELECT" ) {
action = "CONFIRM";
}
ui_manager::redraw();
break;
}
}
}
// also check special keys
if( action == "QUIT" ) {
if( query_yn( _( "Really quit?" ) ) ) {
return false;
}
} else if( action == "LEFT" || action == "PREV_TAB" ) {
sel_line = 0;
if( sel1 > 0 ) {
sel1--;
} else {
sel1 = max_menu_opts;
}
sel2 = sel1 == getopt( main_menu_opts::LOADCHAR ) ? last_world_pos : 0;
on_move();
} else if( action == "RIGHT" || action == "NEXT_TAB" ) {
sel_line = 0;
if( sel1 < max_menu_opts ) {
sel1++;
} else {
sel1 = 0;
}
sel2 = sel1 == getopt( main_menu_opts::LOADCHAR ) ? last_world_pos : 0;
on_move();
} else if( action == "UP" || action == "DOWN" ||
action == "PAGE_UP" || action == "PAGE_DOWN" ||
action == "SCROLL_UP" || action == "SCROLL_DOWN" ) {
int max_item_count = 0;
int min_item_val = 0;
main_menu_opts opt = static_cast<main_menu_opts>( sel1 );
switch( opt ) {
case main_menu_opts::MOTD:
case main_menu_opts::CREDITS:
if( action == "UP" || action == "PAGE_UP" || action == "SCROLL_UP" ) {
if( sel_line > 0 ) {
sel_line--;
}
} else if( action == "DOWN" || action == "PAGE_DOWN" || action == "SCROLL_DOWN" ) {
int effective_height = sel_line + FULL_SCREEN_HEIGHT - 2;
if( ( opt == main_menu_opts::CREDITS && effective_height < mmenu_credits_len ) ||
( opt == main_menu_opts::MOTD && effective_height < mmenu_motd_len ) ) {
sel_line++;
}
}
break;
case main_menu_opts::LOADCHAR:
max_item_count = world_generator->all_worldnames().size();
break;
case main_menu_opts::WORLD:
// extra 1 = "Create New World"
max_item_count = world_generator->all_worldnames().size() + 1;
break;
case main_menu_opts::NEWCHAR:
max_item_count = vNewGameSubItems.size();
break;
case main_menu_opts::SETTINGS:
max_item_count = vSettingsSubItems.size();
break;
case main_menu_opts::SPECIAL:
max_item_count = static_cast<int>( special_game_type::NUM_SPECIAL_GAME_TYPES ) - 1;
break;
case main_menu_opts::HELP:
case main_menu_opts::QUIT:
default:
break;
}
if( max_item_count > 0 ) {
if( action == "UP" || action == "PAGE_UP" || action == "SCROLL_UP" ) {
sel2--;
if( sel2 < min_item_val ) {
sel2 = max_item_count - 1;
}
} else if( action == "DOWN" || action == "PAGE_DOWN" || action == "SCROLL_DOWN" ) {
sel2++;
if( sel2 >= max_item_count ) {
sel2 = min_item_val;
}
}
on_move();
}
} else if( action == "CONFIRM" ) {
switch( static_cast<main_menu_opts>( sel1 ) ) {
case main_menu_opts::HELP:
get_help().display_help();
break;
case main_menu_opts::QUIT:
return false;
case main_menu_opts::SPECIAL:
if( MAP_SHARING::isSharing() ) {
on_error();
popup( _( "Special games don't work with shared maps." ) );
} else if( sel2 >= 0 && sel2 < static_cast<int>( special_game_type::NUM_SPECIAL_GAME_TYPES ) - 1 ) {
on_out_of_scope cleanup( [&player_character]() {
g->gamemode.reset();
player_character = avatar();
world_generator->set_active_world( nullptr );
} );
g->gamemode = get_special_game( static_cast<special_game_type>( sel2 + 1 ) );
// check world
WORLD *world = world_generator->make_new_world( static_cast<special_game_type>( sel2 + 1 ) );
if( world == nullptr ) {
break;
}
world_generator->set_active_world( world );
try {
g->setup();
} catch( const std::exception &err ) {
debugmsg( "Error: %s", err.what() );
break;
}
if( !g->gamemode->init() ) {
break;
}
cleanup.cancel();
start = true;
}
break;
case main_menu_opts::SETTINGS:
if( sel2 == 0 ) { /// Options
get_options().show( false );
// The language may have changed- gracefully handle this.
init_strings();
} else if( sel2 == 1 ) { /// Keybindings
input_context ctxt_default = get_default_mode_input_context();
ctxt_default.display_menu();
} else if( sel2 == 2 ) { /// Autopickup
get_auto_pickup().show();
} else if( sel2 == 3 ) { /// Safemode
get_safemode().show();
} else if( sel2 == 4 ) { /// Colors
all_colors.show_gui();
}
break;
case main_menu_opts::WORLD:
world_tab( sel2 > 0 ? world_generator->all_worldnames().at( sel2 - 1 ) : "" );
break;
case main_menu_opts::LOADCHAR:
if( static_cast<std::size_t>( sel2 ) < world_generator->all_worldnames().size() ) {
start = load_character_tab( world_generator->all_worldnames().at( sel2 ) );
} else {
popup( _( "No world to load." ) );
}
break;
case main_menu_opts::NEWCHAR:
start = new_character_tab();
break;
case main_menu_opts::MOTD:
case main_menu_opts::CREDITS:
default:
break;
}
}
}
return true;
}
bool main_menu::new_character_tab()
{
avatar &pc = get_avatar();
// Preset character templates
if( sel2 == 1 ) {
if( templates.empty() ) {
on_error();
popup( _( "No templates found!" ) );
return false;
}
while( true ) {
uilist mmenu( _( "Choose a preset character template" ), {} );
mmenu.border_color = c_white;
int opt_val = 0;
for( const std::string &tmpl : templates ) {
mmenu.entries.emplace_back( opt_val++, true, MENU_AUTOASSIGN, tmpl );
}
mmenu.entries.emplace_back( opt_val, true, 'q', _( "<- Back to Main Menu" ), c_yellow, c_yellow );
mmenu.query();
opt_val = mmenu.ret;
if( opt_val < 0 || static_cast<size_t>( opt_val ) >= templates.size() ) {
return false;
}
std::string res = query_popup()
.context( "LOAD_DELETE_CANCEL" ).default_color( c_white )
.message( _( "What to do with template \"%s\"?" ), templates[opt_val] )
.option( "LOAD" ).option( "DELETE" ).option( "CANCEL" ).cursor( 0 )
.query().action;
if( res == "DELETE" &&
query_yn( _( "Are you sure you want to delete %s?" ), templates[opt_val] ) ) {
const auto path = PATH_INFO::templatedir() + templates[opt_val] + ".template";
if( !remove_file( path ) ) {
popup( _( "Sorry, something went wrong." ) );
} else {
templates.erase( templates.begin() + opt_val );
}
} else if( res == "LOAD" ) {
on_out_of_scope cleanup( [&pc]() {
pc = avatar();
world_generator->set_active_world( nullptr );
} );
g->gamemode = nullptr;
WORLD *world = world_generator->pick_world();
if( world == nullptr ) {
continue;
}
world_generator->set_active_world( world );
try {
g->setup();
} catch( const std::exception &err ) {
debugmsg( "Error: %s", err.what() );
continue;
}
if( !pc.create( character_type::TEMPLATE, templates[opt_val] ) ) {
load_char_templates();
MAPBUFFER.clear();
overmap_buffer.clear();
return false;
}
if( !g->start_game() ) {
return false;
}
cleanup.cancel();
return true;
}
if( templates.empty() ) {
return false;
}
}
} else { ///Non-template options
on_out_of_scope cleanup( [&pc]() {
pc = avatar();
world_generator->set_active_world( nullptr );
} );
g->gamemode = nullptr;
// First load the mods, this is done by
// loading the world.
// Pick a world, suppressing prompts if it's "play now" mode.
WORLD *world = world_generator->pick_world( true, sel2 == 3 || sel2 == 4 );
if( world == nullptr ) {
return false;
}
world_generator->set_active_world( world );
try {
g->setup();
} catch( const std::exception &err ) {
debugmsg( "Error: %s", err.what() );
return false;
}
character_type play_type = character_type::CUSTOM;
switch( sel2 ) {
case 0:
play_type = character_type::CUSTOM;
break;
case 2:
play_type = character_type::RANDOM;
break;
case 3:
play_type = character_type::NOW;
break;
case 4:
play_type = character_type::FULL_RANDOM;
break;
}
if( !pc.create( play_type ) ) {
load_char_templates();
MAPBUFFER.clear();
overmap_buffer.clear();
return false;
}
if( !g->start_game() ) {
return false;
}
cleanup.cancel();
return true;
}
return false;
}
bool main_menu::load_character_tab( const std::string &worldname )
{
savegames = world_generator->get_world( worldname )->world_saves;
if( MAP_SHARING::isSharing() ) {
auto new_end = std::remove_if( savegames.begin(), savegames.end(), []( const save_t &str ) {
return str.decoded_name() != MAP_SHARING::getUsername();
} );
savegames.erase( new_end, savegames.end() );
}
if( savegames.empty() ) {
on_error();
//~ %s = world name
popup( _( "%s has no characters to load!" ), worldname );
return false;
}
uilist mmenu( string_format( _( "Load character from \"%s\"" ), worldname ), {} );
mmenu.border_color = c_white;
int opt_val = 0;
for( const save_t &s : savegames ) {
mmenu.entries.emplace_back( opt_val++, true, MENU_AUTOASSIGN, s.decoded_name() );
}
mmenu.entries.emplace_back( opt_val, true, 'q', _( "<- Back to Main Menu" ), c_yellow, c_yellow );
mmenu.query();
opt_val = mmenu.ret;
if( opt_val < 0 || static_cast<size_t>( opt_val ) >= savegames.size() ) {
return false;
}
avatar &pc = get_avatar();
on_out_of_scope cleanup( [&pc]() {
pc = avatar();
world_generator->set_active_world( nullptr );
} );
g->gamemode = nullptr;
WORLD *world = world_generator->get_world( worldname );
world_generator->last_world_name = world->world_name;
world_generator->last_character_name = savegames[opt_val].decoded_name();
world_generator->save_last_world_info();
world_generator->set_active_world( world );
try {
g->setup();
} catch( const std::exception &err ) {
debugmsg( "Error: %s", err.what() );
return false;
}
if( g->load( savegames[opt_val] ) ) {