-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
3067 lines (2714 loc) · 109 KB
/
Menu.cpp
File metadata and controls
3067 lines (2714 loc) · 109 KB
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
/*
Menu.cpp
Author: [Your Name]
Roll Number: [Your Roll Number]
Project Title: Xonix Game - DSA Project
Description:
This file contains the implementation of the Menu class methods.
*/
#include "Menu.h"
#include<iostream>
Menu::Menu(RenderWindow* window, Auth* auth) : window(window), auth(auth) {
currentState = MenuState::MAIN_MENU;
selectedItem = 0;
isLoggedIn = false;
cursorBlinkTime = 0.0f;
showCursor = true;
showPassword = false;
game = nullptr;
errorTimer = 0.0f;
passwordDisplay = "";
confirmPasswordDisplay = "";
isUsernameSelected = false;
isPasswordSelected = false;
isConfirmPasswordSelected = false;
// Initialize save/load game variables
selectedSaveIndex = 0;
showConfirmationDialog = false;
confirmationMessage = "";
isSaveOperation = false;
// Load system font
if (!font.loadFromFile("C:/Windows/Fonts/arial.ttf")) {
if (!font.loadFromFile("C:/Windows/Fonts/calibri.ttf")) {
if (!font.loadFromFile("C:/Windows/Fonts/consolas.ttf")) {
if (!font.loadFromFile("C:/Windows/Fonts/times.ttf")) {
font.loadFromFile("C:/Windows/Fonts/msmincho.ttc");
}
}
}
}
// Initialize cursor
cursor.setFont(font);
cursor.setString("|");
cursor.setCharacterSize(30);
cursor.setFillColor(Color::Black);
// Initialize text objects
usernameText.setFont(font);
passwordText.setFont(font);
confirmPasswordText.setFont(font);
usernameText.setCharacterSize(30);
passwordText.setCharacterSize(30);
confirmPasswordText.setCharacterSize(30);
usernameText.setFillColor(Color::Black);
passwordText.setFillColor(Color::Black);
confirmPasswordText.setFillColor(Color::Black);
// Initialize the main menu
initializeMainMenu();
}
Menu::~Menu() {
// Clean up any resources
}
void Menu::initialize() {
initializeMainMenu();
}
void Menu::initializeMainMenu() {
menuItems.clear();
buttons.clear();
// Create title
Text title;
title.setFont(font);
title.setString("Xonix Game");
title.setCharacterSize(50);
title.setFillColor(Color::White);
title.setPosition(250, 50);
menuItems.push_back(title);
// Create menu items
std::string items[] = {
"Single Player",
"Multi Player",
"Level Select",
"Load Game", // Added Load Game option
"Leaderboard",
"Profile",
"Options",
"Exit"
};
for (int i = 0; i < 8; i++) { // Updated to 8 items
Text text;
text.setFont(font);
text.setString(items[i]);
text.setCharacterSize(30);
text.setFillColor(Color::White);
text.setPosition(300, 150 + i * 40);
menuItems.push_back(text);
RectangleShape button;
button.setSize(Vector2f(200, 40));
button.setPosition(290, 150 + i * 40);
button.setFillColor(Color::Transparent);
button.setOutlineThickness(2);
button.setOutlineColor(Color::White);
buttons.push_back(button);
}
}
void Menu::initializeLoginMenu() {
menuItems.clear();
buttons.clear();
hints.clear();
// Create title
Text title;
title.setFont(font);
title.setString("Login");
title.setCharacterSize(50);
title.setFillColor(Color::White);
title.setPosition(300, 50);
menuItems.push_back(title);
// Create input fields with more visible labels
std::string fields[] = { "Username:", "Password:" };
for (int i = 0; i < 2; i++) {
// Create label text
Text labelText;
labelText.setFont(font);
labelText.setString(fields[i]);
labelText.setCharacterSize(30);
labelText.setFillColor(Color::White);
labelText.setPosition(200, 150 + i * 60);
menuItems.push_back(labelText);
// Create input field
RectangleShape field;
field.setSize(Vector2f(300, 40));
field.setPosition(200, 200 + i * 60);
field.setFillColor(Color::White);
field.setOutlineThickness(2);
field.setOutlineColor(Color::Black);
buttons.push_back(field);
// Add hint text below the field
Text hintText;
hintText.setFont(font);
hintText.setString(i == 0 ? "Enter your username" : "Enter your password");
hintText.setCharacterSize(15);
hintText.setFillColor(Color(200, 200, 200)); // Light gray color
hintText.setPosition(200, 245 + i * 60);
hints.push_back(hintText);
}
// Create login button with more visible text
Text loginText;
loginText.setFont(font);
loginText.setString("Click Here to Login");
loginText.setCharacterSize(30);
loginText.setFillColor(Color::White);
loginText.setPosition(250, 350);
menuItems.push_back(loginText);
RectangleShape loginButton;
loginButton.setSize(Vector2f(300, 50)); // Made button larger
loginButton.setPosition(240, 350);
loginButton.setFillColor(Color(0, 100, 0)); // Dark green color
loginButton.setOutlineThickness(2);
loginButton.setOutlineColor(Color::White);
buttons.push_back(loginButton);
// Create register button with more visible text
Text registerText;
registerText.setFont(font);
registerText.setString("New User? Click Here to Register");
registerText.setCharacterSize(20);
registerText.setFillColor(Color::White);
registerText.setPosition(250, 420);
menuItems.push_back(registerText);
RectangleShape registerButton;
registerButton.setSize(Vector2f(300, 40));
registerButton.setPosition(240, 420);
registerButton.setFillColor(Color::Transparent);
registerButton.setOutlineThickness(2);
registerButton.setOutlineColor(Color::White);
buttons.push_back(registerButton);
// Store input boxes for reference
usernameBox = buttons[0];
passwordBox = buttons[1];
// Reset input fields
username = "";
password = "";
passwordDisplay = "";
usernameText.setString("");
passwordText.setString("");
isUsernameSelected = false;
isPasswordSelected = false;
// Set up text display positions with black color for visibility
usernameText.setFont(font);
usernameText.setCharacterSize(30);
usernameText.setFillColor(Color::Black); // Changed to black
usernameText.setPosition(210, 205);
passwordText.setFont(font);
passwordText.setCharacterSize(30);
passwordText.setFillColor(Color::Black); // Changed to black
passwordText.setPosition(210, 265);
}
void Menu::processInput(Event& event) {
switch (currentState) {
case MenuState::MAIN_MENU:
handleMainMenuInput(event);
break;
case MenuState::LOGIN:
handleLoginInput(event);
break;
case MenuState::REGISTER:
handleRegisterInput(event);
break;
case MenuState::LEVEL_SELECT:
handleLevelSelectInput(event);
break;
case MenuState::LEADERBOARD:
handleLeaderboardInput(event);
break;
case MenuState::PROFILE:
handleProfileInput(event);
break;
case MenuState::OPTIONS:
handleOptionsInput(event);
break;
case MenuState::SAVE_GAME:
handleSaveGameInput(event);
break;
case MenuState::LOAD_GAME:
handleLoadGameInput(event);
break;
case MenuState::MATCHMAKING:
handleMatchmakingInput(event);
break;
default:
break;
}
}
void Menu::handleMainMenuInput(Event& event) {
if (event.type == Event::KeyPressed) {
switch (event.key.code) {
case Keyboard::Up:
if (selectedItem > 1) { // Start from 1 since we've defined menu items to start at index 1
selectedItem--;
std::cout << "Selected menu item: " << selectedItem << std::endl;
}
else if (selectedItem == 0) {
// Wrap around to the bottom if at the top
selectedItem = 8; // Updated to 8 total items
std::cout << "Selected menu item: " << selectedItem << std::endl;
}
break;
case Keyboard::Down:
if (selectedItem < 8) { // Updated to 8 total items
selectedItem++;
std::cout << "Selected menu item: " << selectedItem << std::endl;
}
else if (selectedItem == 8) { // Updated to 8 total items
// Wrap around to the top if at the bottom
selectedItem = 1; // First selectable item
std::cout << "Selected menu item: " << selectedItem << std::endl;
}
break;
case Keyboard::Return: // Enter key
std::cout << "Selected menu item: " << selectedItem << std::endl;
switch (selectedItem) {
case 1: // Single Player
if (!isLoggedIn) {
setState(MenuState::LOGIN);
}
else {
// Initialize and start single player game
game = new Game();
game->initialize();
game->setCurrentUsername(currentUser);
game->run();
updateLeaderboard(currentUser, game->getFinalScore());
delete game;
game = nullptr;
setState(MenuState::MAIN_MENU);
}
break;
case 2: // Multi Player
if (!isLoggedIn) {
setState(MenuState::LOGIN);
}
else {
// Enter matchmaking queue with player's score
int playerScore = auth->getPlayerScore(currentUser);
enterMatchmakingQueue(currentUser, playerScore);
}
break;
case 3: // Level Select
if (!isLoggedIn) {
setState(MenuState::LOGIN);
}
else {
setState(MenuState::LEVEL_SELECT);
}
break;
case 4: // Load Game
if (!isLoggedIn) {
setState(MenuState::LOGIN);
}
else {
setState(MenuState::LOAD_GAME);
}
break;
case 5: // Leaderboard
setState(MenuState::LEADERBOARD);
break;
case 6: // Profile
if (!isLoggedIn) {
setState(MenuState::LOGIN);
}
else {
setState(MenuState::PROFILE);
}
break;
case 7: // Options
setState(MenuState::OPTIONS);
break;
case 8: // Exit
window->close();
break;
}
break;
}
}
else if (event.type == Event::MouseWheelScrolled) {
if (event.mouseWheelScroll.wheel == Mouse::VerticalWheel) {
// Scroll up
if (event.mouseWheelScroll.delta > 0) {
if (selectedItem > 1) {
selectedItem--;
std::cout << "Mouse wheel scrolled up, selected item: " << selectedItem << std::endl;
}
else if (selectedItem <= 1) {
// Wrap around to the bottom if at the top
selectedItem = 8;
std::cout << "Mouse wheel scrolled up, selected item: " << selectedItem << std::endl;
}
}
// Scroll down
else if (event.mouseWheelScroll.delta < 0) {
if (selectedItem < 8) {
selectedItem++;
std::cout << "Mouse wheel scrolled down, selected item: " << selectedItem << std::endl;
}
else if (selectedItem >= 8) {
// Wrap around to the top if at the bottom
selectedItem = 1;
std::cout << "Mouse wheel scrolled down, selected item: " << selectedItem << std::endl;
}
}
}
}
// Add mouse click support for menu items
// Add mouse click support for menu items
if (event.type == Event::MouseButtonPressed && event.mouseButton.button == Mouse::Left) {
Vector2f mousePos = window->mapPixelToCoords(Vector2i(event.mouseButton.x, event.mouseButton.y));
// Calculate the vertical spacing based on available space
float windowWidth = static_cast<float>(window->getSize().x);
float windowHeight = static_cast<float>(window->getSize().y);
int totalItems = 8; // Number of menu items
float menuItemHeight = 40;
float availableHeight = windowHeight - 120; // Total height minus header
float menuSpacing = min(10.0f, max(3.0f, (availableHeight - totalItems * menuItemHeight) / (totalItems - 1)));
float menuStartY = 120; // Start just below the divider
float menuWidth = 300;
for (int i = 0; i < 8; i++) { // Updated to 8 items
float itemY = menuStartY + i * (menuItemHeight + menuSpacing);
FloatRect menuItemBounds(
(windowWidth - menuWidth) / 2,
itemY,
menuWidth,
menuItemHeight
);
if (menuItemBounds.contains(mousePos)) {
// Update selected item and trigger selection
selectedItem = i + 1;
// Simulate Enter key press by executing the same code
Event enterEvent;
enterEvent.type = Event::KeyPressed;
enterEvent.key.code = Keyboard::Return;
handleMainMenuInput(enterEvent);
break;
}
}
}
}
void Menu::display() {
window->clear(Color(50, 50, 50)); // Dark gray background
switch (currentState) {
case MenuState::MAIN_MENU:
displayMainMenu();
break;
case MenuState::LOGIN:
displayLoginMenu();
break;
case MenuState::REGISTER:
displayRegisterMenu();
break;
case MenuState::LEVEL_SELECT:
displayLevelSelect();
break;
case MenuState::LEADERBOARD:
displayLeaderboard();
break;
case MenuState::PROFILE:
displayProfile();
break;
case MenuState::OPTIONS:
displayOptions();
break;
case MenuState::SAVE_GAME:
displaySaveGame();
break;
case MenuState::LOAD_GAME:
displayLoadGame();
break;
case MenuState::MATCHMAKING:
displayMatchmaking();
break;
default:
break;
}
// Display confirmation dialog over other elements if active
if (showConfirmationDialog) {
displayConfirmationDialog();
}
window->display();
}
void Menu::displayMainMenu() {
// Get current window dimensions
float windowWidth = static_cast<float>(window->getSize().x);
float windowHeight = static_cast<float>(window->getSize().y);
// Draw a nice background with gradient effect
RectangleShape background(Vector2f(windowWidth, windowHeight));
background.setFillColor(Color(30, 30, 40)); // Darker background
window->draw(background);
// Add a decorative header bar
RectangleShape headerBar(Vector2f(windowWidth, 80));
headerBar.setFillColor(Color(50, 50, 70));
window->draw(headerBar);
// Draw title with shadow effect for depth
Text shadowText;
shadowText.setFont(font);
shadowText.setString("Xonix Game");
shadowText.setCharacterSize(50);
shadowText.setFillColor(Color(20, 20, 30, 200));
FloatRect shadowBounds = shadowText.getLocalBounds();
shadowText.setPosition((windowWidth - shadowBounds.width) / 2 + 3, 37); // Position slightly higher
window->draw(shadowText);
// Main title with glow effect
Text titleText;
titleText.setFont(font);
titleText.setString("Xonix Game");
titleText.setCharacterSize(50);
titleText.setFillColor(Color(220, 220, 50)); // Bright yellow
titleText.setStyle(Text::Bold);
FloatRect titleBounds = titleText.getLocalBounds();
titleText.setPosition((windowWidth - titleBounds.width) / 2, 34); // Position slightly higher
window->draw(titleText);
// Draw decorative line beneath title
RectangleShape divider(Vector2f(400, 2));
divider.setFillColor(Color(220, 220, 50, 150)); // Semi-transparent yellow
divider.setPosition((windowWidth - 400) / 2, 100); // Position slightly higher
window->draw(divider);
// Calculate the vertical spacing based on available space
// Calculate how much space we need for all menu items
int totalItems = 8; // Number of menu items
float requiredHeight = totalItems * 40 + (totalItems - 1) * 10; // Menu item height (40) + spacing (10)
float availableHeight = windowHeight - 120; // Total height minus header
// Adjust spacing based on available height
float menuItemHeight = 40;
float menuSpacing = min(10.0f, max(3.0f, (availableHeight - totalItems * menuItemHeight) / (totalItems - 1)));
// Start menu items higher
float menuStartY = 120; // Start just below the divider
float menuWidth = 300;
// Draw menu items with better spacing and visual appeal
for (int i = 0; i < 8; i++) { // Updated to 8 items
// Calculate position - compact layout
float itemY = menuStartY + i * (menuItemHeight + menuSpacing);
// Create menu box
RectangleShape menuBox(Vector2f(menuWidth, menuItemHeight));
menuBox.setPosition((windowWidth - menuWidth) / 2, itemY);
// Style based on selection
if (i + 1 == selectedItem) {
// Selected item styling
menuBox.setFillColor(Color(80, 80, 100, 200));
menuBox.setOutlineThickness(2);
menuBox.setOutlineColor(Color(220, 220, 50)); // Yellow border
// Add highlight effects
RectangleShape glow(Vector2f(menuWidth + 10, menuItemHeight + 10));
glow.setPosition((windowWidth - menuWidth - 10) / 2, itemY - 5);
glow.setFillColor(Color(220, 220, 50, 30)); // Very light yellow glow
window->draw(glow);
}
else {
// Unselected item styling
menuBox.setFillColor(Color(60, 60, 80, 150));
menuBox.setOutlineThickness(1);
menuBox.setOutlineColor(Color(150, 150, 150, 100));
}
window->draw(menuBox);
// Create menu text
Text menuText;
menuText.setFont(font);
// Use menu items from the fixed array instead of dynamic menuItems
std::string items[] = {
"Single Player",
"Multi Player",
"Level Select",
"Load Game", // Added Load Game option
"Leaderboard",
"Profile",
"Options",
"Exit"
};
menuText.setString(items[i]);
menuText.setCharacterSize(i + 1 == selectedItem ? 30 : 28);
menuText.setFillColor(i + 1 == selectedItem ? Color(255, 255, 100) : Color(220, 220, 220));
menuText.setStyle(i + 1 == selectedItem ? Text::Bold : Text::Regular);
// Center text in box
FloatRect textBounds = menuText.getLocalBounds();
menuText.setPosition(
(windowWidth - textBounds.width) / 2,
itemY + (menuItemHeight - textBounds.height) / 2 - textBounds.top
);
window->draw(menuText);
}
// Draw login status with a more attractive box
RectangleShape statusBox(Vector2f(200, 30));
statusBox.setPosition(windowWidth - 220, 10);
statusBox.setFillColor(isLoggedIn ? Color(40, 100, 40, 200) : Color(100, 40, 40, 200));
statusBox.setOutlineThickness(1);
statusBox.setOutlineColor(Color(200, 200, 200, 100));
window->draw(statusBox);
Text loginStatus;
loginStatus.setFont(font);
loginStatus.setString(isLoggedIn ? "Logged in as: " + currentUser : "Not logged in");
loginStatus.setCharacterSize(16);
loginStatus.setFillColor(Color(240, 240, 240));
FloatRect statusBounds = loginStatus.getLocalBounds();
loginStatus.setPosition(
windowWidth - 220 + (200 - statusBounds.width) / 2,
10 + (30 - statusBounds.height) / 2 - statusBounds.top
);
window->draw(loginStatus);
}
void Menu::setState(MenuState newState) {
currentState = newState;
selectedItem = 0; // Reset selection when changing states
switch (newState) {
case MenuState::MAIN_MENU:
initializeMainMenu();
break;
case MenuState::LOGIN:
initializeLoginMenu();
break;
case MenuState::REGISTER:
initializeRegisterMenu();
break;
case MenuState::LEVEL_SELECT:
initializeLevelSelect();
break;
case MenuState::LEADERBOARD:
initializeLeaderboard();
break;
case MenuState::PROFILE:
initializeProfile();
break;
case MenuState::OPTIONS:
initializeOptions();
break;
case MenuState::SAVE_GAME:
initializeSaveGame();
break;
case MenuState::LOAD_GAME:
initializeLoadGame();
break;
case MenuState::MATCHMAKING:
initializeMatchmaking();
break;
}
}
bool Menu::getIsLoggedIn() const {
return isLoggedIn;
}
std::string Menu::getCurrentUser() const {
return currentUser;
}
// Initialize other menu states
void Menu::initializeRegisterMenu() {
menuItems.clear();
buttons.clear();
hints.clear();
// Create back button
Text backText;
backText.setFont(font);
backText.setString("Back to Login");
backText.setCharacterSize(20);
backText.setFillColor(Color::White);
backText.setPosition(20, 20);
menuItems.push_back(backText);
RectangleShape backButton;
backButton.setSize(Vector2f(150, 30));
backButton.setPosition(20, 20);
backButton.setFillColor(Color::Transparent);
backButton.setOutlineThickness(2);
backButton.setOutlineColor(Color::White);
buttons.push_back(backButton);
// Create title
Text title;
title.setFont(font);
title.setString("Register");
title.setCharacterSize(50);
title.setFillColor(Color::White);
title.setPosition(280, 50);
menuItems.push_back(title);
// Create input fields
std::string fields[] = { "Username:", "Password:", "Confirm Password:" };
for (int i = 0; i < 3; i++) {
Text text;
text.setFont(font);
text.setString(fields[i]);
text.setCharacterSize(30);
text.setFillColor(Color::White);
text.setPosition(200, 150 + i * 60);
menuItems.push_back(text);
RectangleShape field;
field.setSize(Vector2f(300, 40));
field.setPosition(200, 200 + i * 60);
field.setFillColor(Color::White);
field.setOutlineThickness(2);
field.setOutlineColor(Color::Black);
buttons.push_back(field);
}
// Add field hints
addFieldHint("Enter your username (3-20 characters)", 200, 245);
addFieldHint("Password Requirements:", 200, 305);
addFieldHint("• At least 6 characters long", 220, 325);
addFieldHint("• At least one uppercase letter (A-Z)", 220, 345);
addFieldHint("• At least one lowercase letter (a-z)", 220, 365);
addFieldHint("• At least one number (0-9)", 220, 385);
addFieldHint("• At least one special character (!@#$%^&* etc.)", 220, 405);
addFieldHint("Re-enter your password to confirm", 200, 425);
// Create show password button
Text showPasswordText;
showPasswordText.setFont(font);
showPasswordText.setString("Show Password");
showPasswordText.setCharacterSize(20);
showPasswordText.setFillColor(Color::White);
showPasswordText.setPosition(520, 200);
menuItems.push_back(showPasswordText);
RectangleShape showPasswordButton;
showPasswordButton.setSize(Vector2f(150, 30));
showPasswordButton.setPosition(520, 200);
showPasswordButton.setFillColor(Color::Transparent);
showPasswordButton.setOutlineThickness(2);
showPasswordButton.setOutlineColor(Color::White);
buttons.push_back(showPasswordButton);
// Create register button
RectangleShape registerButton;
registerButton.setSize(Vector2f(500, 80));
registerButton.setPosition(150, 430);
registerButton.setFillColor(Color(255, 0, 0));
registerButton.setOutlineThickness(5);
registerButton.setOutlineColor(Color::Yellow);
buttons.push_back(registerButton);
Text registerText;
registerText.setFont(font);
registerText.setString("CLICK HERE TO REGISTER");
registerText.setCharacterSize(40);
registerText.setFillColor(Color::White);
registerText.setPosition(180, 445);
menuItems.push_back(registerText);
// Initialize text input fields
username = "";
password = "";
confirmPassword = "";
isUsernameSelected = false;
isPasswordSelected = false;
isConfirmPasswordSelected = false;
showPassword = false;
// Set up text display with black color for visibility
usernameText.setFont(font);
usernameText.setCharacterSize(30);
usernameText.setFillColor(Color::Black); // Changed to black
usernameText.setPosition(210, 205);
passwordText.setFont(font);
passwordText.setCharacterSize(30);
passwordText.setFillColor(Color::Black); // Changed to black
passwordText.setPosition(210, 265);
confirmPasswordText.setFont(font);
confirmPasswordText.setCharacterSize(30);
confirmPasswordText.setFillColor(Color::Black); // Changed to black
confirmPasswordText.setPosition(210, 325);
// Store input boxes for reference
usernameBox = buttons[1];
passwordBox = buttons[2];
confirmPasswordBox = buttons[3];
}
void Menu::handleRegisterInput(Event& event) {
if (event.type == Event::MouseButtonPressed) {
Vector2f mousePos = window->mapPixelToCoords(Vector2i(event.mouseButton.x, event.mouseButton.y));
// Check username box
if (usernameBox.getGlobalBounds().contains(mousePos)) {
std::cout << "Username field selected" << std::endl;
isUsernameSelected = true;
isPasswordSelected = false;
isConfirmPasswordSelected = false;
}
// Check password box
else if (passwordBox.getGlobalBounds().contains(mousePos)) {
std::cout << "Password field selected" << std::endl;
isUsernameSelected = false;
isPasswordSelected = true;
isConfirmPasswordSelected = false;
}
// Check confirm password box
else if (confirmPasswordBox.getGlobalBounds().contains(mousePos)) {
std::cout << "Confirm password field selected" << std::endl;
isUsernameSelected = false;
isPasswordSelected = false;
isConfirmPasswordSelected = true;
}
// Check register button (index 5)
else if (buttons[5].getGlobalBounds().contains(mousePos)) {
std::cout << "\n=== Registration Attempt ===" << std::endl;
std::cout << "Username: " << username << std::endl;
std::cout << "Password length: " << password.length() << std::endl;
std::cout << "Confirm password length: " << confirmPassword.length() << std::endl;
if (username.empty() || password.empty() || confirmPassword.empty()) {
std::cout << "Registration failed: Please fill all fields" << std::endl;
errorMessage = "Please fill all fields";
errorTimer = 3.0f;
}
else if (password != confirmPassword) {
std::cout << "Registration failed: Passwords do not match" << std::endl;
errorMessage = "Passwords do not match";
errorTimer = 3.0f;
}
else if (!validatePassword(password)) {
std::cout << "Registration failed: Password does not meet requirements" << std::endl;
errorMessage = "Password does not meet requirements";
errorTimer = 3.0f;
}
else if (auth->registerUser(username, password)) {
std::cout << "Registration successful!" << std::endl;
isLoggedIn = true;
currentUser = username;
// Initialize and start single player game
game = new Game();
game->initialize();
game->setCurrentUsername(currentUser); // Set username before running game
game->run();
updateLeaderboard(currentUser, game->getFinalScore());
delete game;
game = nullptr;
setState(MenuState::MAIN_MENU);
}
else {
std::cout << "Registration failed: Username already exists" << std::endl;
errorMessage = "Username already exists";
errorTimer = 3.0f;
}
}
// Check back button (index 0)
else if (buttons[0].getGlobalBounds().contains(mousePos)) {
std::cout << "Returning to login screen" << std::endl;
setState(MenuState::LOGIN);
}
// Check show password button (index 4)
else if (buttons[4].getGlobalBounds().contains(mousePos)) {
std::cout << "Toggling password visibility" << std::endl;
togglePasswordVisibility();
}
else {
isUsernameSelected = false;
isPasswordSelected = false;
isConfirmPasswordSelected = false;
}
}
else if (event.type == Event::TextEntered) {
if (isUsernameSelected) {
if (event.text.unicode == '\b' && !username.empty()) {
username.pop_back();
std::cout << "Username backspace: " << username << std::endl;
}
else if (event.text.unicode < 128 && username.length() < MAX_USERNAME_LENGTH) {
username += static_cast<char>(event.text.unicode);
std::cout << "Username updated: " << username << std::endl;
}
usernameText.setString(username);
usernameText.setFillColor(Color::Black);
usernameText.setPosition(210, 205);
}
else if (isPasswordSelected) {
if (event.text.unicode == '\b' && !password.empty()) {
password.pop_back();
passwordDisplay.pop_back();
std::cout << "Password length: " << password.length() << std::endl;
}
else if (event.text.unicode < 128 && password.length() < MAX_PASSWORD_LENGTH) {
password += static_cast<char>(event.text.unicode);
passwordDisplay += '*';
std::cout << "Password length: " << password.length() << std::endl;
}
passwordText.setString(showPassword ? password : passwordDisplay);
passwordText.setFillColor(Color::Black);
passwordText.setPosition(210, 265);
}
else if (isConfirmPasswordSelected) {
if (event.text.unicode == '\b' && !confirmPassword.empty()) {
confirmPassword.pop_back();
confirmPasswordDisplay.pop_back();
std::cout << "Confirm password length: " << confirmPassword.length() << std::endl;
}
else if (event.text.unicode < 128 && confirmPassword.length() < MAX_PASSWORD_LENGTH) {
confirmPassword += static_cast<char>(event.text.unicode);
confirmPasswordDisplay += '*';
std::cout << "Confirm password length: " << confirmPassword.length() << std::endl;
}
confirmPasswordText.setString(showPassword ? confirmPassword : confirmPasswordDisplay);
confirmPasswordText.setFillColor(Color::Black);
confirmPasswordText.setPosition(210, 325);
}
}
}
void Menu::togglePasswordVisibility() {
showPassword = !showPassword;
if (showPassword) {
passwordText.setString(password);
confirmPasswordText.setString(confirmPassword);
}
else {
passwordText.setString(std::string(password.length(), '*'));
confirmPasswordText.setString(std::string(confirmPassword.length(), '*'));
}
}
void Menu::showErrorMessage(const std::string& message) {
Text errorText;
errorText.setFont(font);
errorText.setString(message);
errorText.setCharacterSize(20);
errorText.setFillColor(Color::Red);
errorText.setPosition(200, 450);
menuItems.push_back(errorText);
}
void Menu::showSuccessMessage(const std::string& message) {
// Create success message text
Text successText;
successText.setFont(font);
successText.setString(message);
successText.setCharacterSize(20);
successText.setFillColor(Color::Green);
successText.setPosition(250, 450);
menuItems.push_back(successText);
// Display the message
display();
window->display();
}
void Menu::initializeLevelSelect() {
// Create level selection menu
}
void Menu::initializeLeaderboard() {
menuItems.clear();
buttons.clear();
// Create back button
Text backText;
backText.setFont(font);
backText.setString("Back to Main Menu");
backText.setCharacterSize(20);
backText.setFillColor(Color::White);
backText.setPosition(20, 20);
menuItems.push_back(backText);
RectangleShape backButton;
backButton.setSize(Vector2f(200, 40));
backButton.setPosition(20, 20);
backButton.setFillColor(Color::Transparent);
backButton.setOutlineThickness(2);
backButton.setOutlineColor(Color::White);
buttons.push_back(backButton);
// Create title
Text title;
title.setFont(font);
title.setString("Leaderboard");
title.setCharacterSize(50);
title.setFillColor(Color::White);
title.setPosition(280, 50);
menuItems.push_back(title);
// Fetch scores from leaderboard
int count;
PlayerScore* scores = leaderboard.getTopScores(count);
std::cout << "Displaying leaderboard with " << count << " scores" << std::endl;
// Add all scores to the debug output
for (int i = 0; i < count; i++) {
std::cout << " Leaderboard entry " << i + 1 << ": " << scores[i].username << " - " << scores[i].score << std::endl;
}
// Display column headers
Text headerRank;
headerRank.setFont(font);
headerRank.setString("Rank");
headerRank.setCharacterSize(24);
headerRank.setFillColor(Color::Yellow);
headerRank.setPosition(150, 120);
menuItems.push_back(headerRank);
Text headerName;
headerName.setFont(font);
headerName.setString("Player");
headerName.setCharacterSize(24);