R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
BaseMenu.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2026
3** r-type
4** File description:
5** BaseMenu - Base class for all menus to reduce code duplication
6*/
7
8#include "Menu/BaseMenu.hpp"
10
11namespace Game {
12 BaseMenu::BaseMenu(UI::IUIFactory &uiFactory) : _uiFactory(uiFactory) {
14 }
15
17 if (_menu) {
18 _menu->Update();
19 }
20 }
21
23 if (_menu) {
24 _menu->Render();
25 }
26 }
27
29 if (_menu) {
30 _menu->SetVisible(true);
31 }
32 }
33
35 if (_menu) {
36 _menu->SetVisible(false);
37 }
38 }
39
40 bool BaseMenu::IsVisible() const {
41 return _menu && _menu->IsVisible();
42 }
43
45 _soundService = soundService;
46 }
47
48 std::function<void()> BaseMenu::WrapWithClickSound(std::function<void()> callback) {
49 if (!callback) {
50 return callback;
51 }
52 return [this, cb = std::move(callback)]() {
53 if (_soundService) {
55 }
56 cb();
57 };
58 }
59
60 std::shared_ptr<UI::IButton> BaseMenu::CreateCenteredButton(const char *label, float offsetY, float width,
61 float height, unsigned int backgroundColor,
62 unsigned int hoverColor,
63 std::function<void()> callback) {
64 auto button = _uiFactory.CreateButton();
65 button->SetSize(width, height);
66 button->SetAlign(UI::Align::CENTER_BOTH);
67 button->ApplyAlignment();
68
69 float x = 0.0f;
70 float y = 0.0f;
71 button->GetPosition(x, y);
72 button->SetPosition(x, y + offsetY);
73
74 button->SetBackgroundColor(backgroundColor);
75 button->SetHoverColor(hoverColor);
76 button->SetText(label);
77 button->SetTextSize(18);
78 button->SetTextColor(0xFFFFFFFF);
79 button->SetFont(-1);
80 button->SetCallback(WrapWithClickSound(std::move(callback)));
81 return button;
82 }
83} // namespace Game
Interface for sound effect management in menus.
virtual void PlayClickSound()=0
Play the button click sound effect.
virtual void Show()
Show the menu.
Definition BaseMenu.cpp:28
virtual void Update()
Update menu state (should be called every frame).
Definition BaseMenu.cpp:16
BaseMenu(UI::IUIFactory &uiFactory)
Constructor with UI factory reference.
Definition BaseMenu.cpp:12
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
virtual void Render()
Render menu (should be called every frame).
Definition BaseMenu.cpp:22
void SetSoundEffectService(Audio::ISoundEffectService *soundService)
Set the sound effect service for playing UI sounds.
Definition BaseMenu.cpp:44
virtual void Hide()
Hide the menu.
Definition BaseMenu.cpp:34
std::function< void()> WrapWithClickSound(std::function< void()> callback)
Wrap a callback to play click sound before executing.
Definition BaseMenu.cpp:48
virtual bool IsVisible() const
Check if menu is currently visible.
Definition BaseMenu.cpp:40
UI::IUIFactory & _uiFactory
Definition BaseMenu.hpp:100
Abstract factory interface for creating UI elements.
virtual std::unique_ptr< IMenu > CreateMenu()=0
Create a menu instance.
virtual std::unique_ptr< IButton > CreateButton()=0
Create a button instance.
@ CENTER_BOTH
Center on both axes.