R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
MainMenu.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** r-type
4** File description:
5** MainMenu - Main menu for the game (business logic)
6*/
7
8#include "Menu/MainMenu.hpp"
9#include "../common/Logger/Logger.hpp"
10
11namespace Game {
12 MainMenu::MainMenu(UI::IUIFactory &uiFactory) : BaseMenu(uiFactory) {}
13
14 void MainMenu::SetOnQuit(std::function<void()> onQuit) {
15 _onQuit = std::move(onQuit);
16 }
17
18 void MainMenu::SetOnSettings(std::function<void()> onSettings) {
19 _onSettings = std::move(onSettings);
20 }
21
22 void MainMenu::SetOnPlay(std::function<void()> onPlay) {
23 _onPlay = std::move(onPlay);
24 }
25
26 void MainMenu::SetOnProfile(std::function<void()> onProfile) {
27 _onProfile = std::move(onProfile);
28 }
29
30 void MainMenu::SetOnSelectServer(std::function<void()> onSelectServer) {
31 _onSelectServer = std::move(onSelectServer);
32 }
33
34 void MainMenu::SetProfileName(const std::string &name) {
35 if (_profileButton) {
36 _profileButton->SetText(name);
37 }
38 }
39
40 void MainMenu::SetScreenSize(float width, float height) {
41 _screenWidth = width;
42 _screenHeight = height;
43 }
44
51
53 if (!_menu) {
54 return;
55 }
56
57 const float buttonWidth = 200.0f;
58 const float buttonHeight = 50.0f;
59 const float spacing = 20.0f;
60
61 // 4 buttons stack (Play, Settings, Select Server, Quit)
62 const float totalHeight = (buttonHeight * 4.0f) + (spacing * 3.0f);
63 const float offsets[4] = {
64 -(totalHeight / 2.0f) + (buttonHeight / 2.0f), // Play
65 -(totalHeight / 2.0f) + (buttonHeight / 2.0f) + buttonHeight + spacing, // Settings
66 (totalHeight / 2.0f) - (buttonHeight / 2.0f) - buttonHeight - spacing, // Select Server
67 (totalHeight / 2.0f) - (buttonHeight / 2.0f) // Quit
68 };
69
70 _menu->Clear();
71 _menu->AddButton(CreateCenteredButton("PLAY", offsets[0], buttonWidth, buttonHeight, 0xFF4CAF50,
72 0xFF66BB6A, [this]() { OnPlayClicked(); }));
73 _menu->AddButton(CreateCenteredButton("SETTINGS", offsets[1], buttonWidth, buttonHeight, 0xFF424242,
74 0xFF616161, [this]() { OnSettingsClicked(); }));
75 _menu->AddButton(CreateCenteredButton("SELECT SERVER", offsets[2], buttonWidth, buttonHeight,
76 0xFF2196F3, 0xFF64B5F6, [this]() { OnSelectServerClicked(); }));
77 _menu->AddButton(CreateCenteredButton("QUIT", offsets[3], buttonWidth, buttonHeight, 0xFFF44336,
78 0xFFE57373, [this]() { OnQuitClicked(); }));
79
80 // Profile Button (Top Left)
81 const float profileBtnWidth = 120.0f;
82 const float profileBtnHeight = 40.0f;
83 const float margin = 20.0f;
84
86 _profileButton->SetSize(profileBtnWidth, profileBtnHeight);
87 _profileButton->SetPosition(margin, margin);
88 _profileButton->SetText("GUEST"); // Default
89 _profileButton->SetBackgroundColor(0xFF2196F3); // Blue
90 _profileButton->SetHoverColor(0xFF64B5F6);
91 _profileButton->SetTextColor(0xFFFFFFFF);
92 _profileButton->SetCallback(WrapWithClickSound([this]() { OnProfileClicked(); }));
93
94 _menu->AddButton(_profileButton);
95 }
96
98 LOG_INFO("hello world");
99
100 if (_onPlay) {
101 _onPlay();
102 }
103 }
104
106 LOG_INFO("[MainMenu] Settings button clicked!");
107 if (_onSettings) {
108 _onSettings();
109 }
110 }
111
113 LOG_INFO("[MainMenu] Quit button clicked!");
114 if (_onQuit) {
115 _onQuit();
116 } else {
117 LOG_WARNING("[MainMenu] Quit requested but no onQuit callback was set");
118 }
119 }
120
122 LOG_INFO("[MainMenu] Profile button clicked!");
123 if (_onProfile) {
124 _onProfile();
125 }
126 }
127
129 LOG_INFO("[MainMenu] Select Server button clicked!");
130 if (_onSelectServer) {
132 }
133 }
134} // namespace Game
#define LOG_INFO(...)
Definition Logger.hpp:181
#define LOG_WARNING(...)
Definition Logger.hpp:182
virtual void PlayMainMenuOpenSound()=0
Play the main menu open sound effect.
Base class for all menu implementations.
Definition BaseMenu.hpp:26
virtual void Show()
Show the menu.
Definition BaseMenu.cpp:28
std::shared_ptr< UI::IMenu > _menu
Definition BaseMenu.hpp:101
std::shared_ptr< UI::IButton > CreateCenteredButton(const char *label, float offsetY, float width, float height, unsigned int backgroundColor, unsigned int hoverColor, std::function< void()> callback)
Create a button with standard styling and positioning.
Definition BaseMenu.cpp:60
Audio::ISoundEffectService * _soundService
Definition BaseMenu.hpp:102
std::function< void()> WrapWithClickSound(std::function< void()> callback)
Wrap a callback to play click sound before executing.
Definition BaseMenu.cpp:48
UI::IUIFactory & _uiFactory
Definition BaseMenu.hpp:100
void SetOnSettings(std::function< void()> onSettings)
Set callback triggered when the Settings button is clicked.
Definition MainMenu.cpp:18
float _screenWidth
Definition MainMenu.hpp:87
void SetScreenSize(float width, float height)
Set screen size for layout calculations.
Definition MainMenu.cpp:40
std::function< void()> _onQuit
Definition MainMenu.hpp:79
void SetProfileName(const std::string &name)
Update the text of the Profile button (e.g. "GUEST" -> "PlayerName").
Definition MainMenu.cpp:34
void SetOnSelectServer(std::function< void()> onSelectServer)
Set callback triggered when the Select Server button is clicked.
Definition MainMenu.cpp:30
void Show() override
Show the menu and play the open sound effect.
Definition MainMenu.cpp:45
std::function< void()> _onPlay
Definition MainMenu.hpp:78
std::shared_ptr< UI::IButton > _profileButton
Definition MainMenu.hpp:84
void OnProfileClicked()
Definition MainMenu.cpp:121
MainMenu(UI::IUIFactory &uiFactory)
Definition MainMenu.cpp:12
void OnQuitClicked()
Definition MainMenu.cpp:112
void OnSelectServerClicked()
Definition MainMenu.cpp:128
std::function< void()> _onProfile
Definition MainMenu.hpp:81
std::function< void()> _onSettings
Definition MainMenu.hpp:80
void SetOnPlay(std::function< void()> onPlay)
Set callback triggered when the Play button is clicked.
Definition MainMenu.cpp:22
void SetOnQuit(std::function< void()> onQuit)
Set callback triggered when the Quit button is clicked.
Definition MainMenu.cpp:14
void OnPlayClicked()
Definition MainMenu.cpp:97
void Initialize() override
Initialize menu (must be implemented by derived classes).
Definition MainMenu.cpp:52
void SetOnProfile(std::function< void()> onProfile)
Set callback triggered when the Profile/Guest button is clicked.
Definition MainMenu.cpp:26
float _screenHeight
Definition MainMenu.hpp:88
void OnSettingsClicked()
Definition MainMenu.cpp:105
std::function< void()> _onSelectServer
Definition MainMenu.hpp:82
Abstract factory interface for creating UI elements.
virtual std::unique_ptr< IButton > CreateButton()=0
Create a button instance.