R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
AccessibilityMenu.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2026
3** r-type
4** File description:
5** AccessibilityMenu - Accessibility features menu implementation
6*/
7
9#include <algorithm>
10#include <cstring>
11#include "../common/Logger/Logger.hpp"
12
13namespace Game {
15 : BaseMenu(uiFactory), _graphics(graphics) {}
16
18 if (!_menu) {
19 return;
20 }
21
22 _menu->Clear();
23
24 const float buttonWidth = 400.0f;
25 const float buttonHeight = 50.0f;
26 const float spacing = 18.0f;
27
28 const bool showMainMenuButton = (_mode == Mode::OVERLAY);
29 const int buttonCount = showMainMenuButton ? 4 : 3;
30
31 // Center buttons vertically as a stack
32 const float totalHeight = (buttonHeight * static_cast<float>(buttonCount)) +
33 (spacing * static_cast<float>(buttonCount - 1));
34
35 auto offsetForIndex = [&](int idx) {
36 const float start = -(totalHeight / 2.0f) + (buttonHeight / 2.0f);
37 return start + (buttonHeight + spacing) * static_cast<float>(idx);
38 };
39
40 // Colorblind Filter button (cycles through filter types)
41 _menu->AddButton(CreateCenteredButton(
42 "COLORBLIND: NONE", offsetForIndex(0), buttonWidth, buttonHeight, 0xFF424242, 0xFF616161,
44
45 // Key Bindings configuration button
46 _menu->AddButton(CreateCenteredButton("CONFIGURE KEY BINDINGS", offsetForIndex(1), buttonWidth,
47 buttonHeight, 0xFF5E35B1, 0xFF7E57C2,
48 [this]() { OpenKeyBindingsConfig(); }));
49
50 // Back button
51 _menu->AddButton(CreateCenteredButton("BACK", offsetForIndex(2), buttonWidth, buttonHeight,
52 0xFF1976D2, 0xFF1E88E5, [this]() {
53 if (_onBack) {
54 _onBack();
55 } else {
56 Hide();
57 }
58 }));
59
60 // Main menu (only in overlay mode)
61 if (showMainMenuButton) {
62 _menu->AddButton(CreateCenteredButton("MAIN MENU", offsetForIndex(3), buttonWidth, buttonHeight,
63 0xFF5D4037, 0xFF6D4C41, [this]() {
64 if (_onMainMenu) {
66 }
67 }));
68 }
69
71 }
72
76
78 if (!_menu || !_menu->IsVisible()) {
79 return;
80 }
81
82 // Draw title
83 int screenWidth = _graphics.GetScreenWidth();
84 const char *title = "ACCESSIBILITY SETTINGS";
85 int titleFontSize = 24;
86 int titleWidth = static_cast<int>(strlen(title) * titleFontSize * 0.6f);
87 int titleX = (screenWidth - titleWidth) / 2;
88 int titleY = 80;
89 _graphics.DrawText(title, titleX, titleY, titleFontSize, 0xFF4CAF50);
90
91 // Draw dim overlay if in overlay mode
92 if (_mode == Mode::OVERLAY) {
93 // Background dim is handled by the caller
94 }
95
97 }
98
99 // --- Display Mode ---
101 if (_mode != mode) {
102 _mode = mode;
103 Initialize(); // Rebuild menu to show/hide Main Menu button
104 }
105 }
106
110
111 void AccessibilityMenu::SetOverlayDimColor(unsigned int color) {
112 _overlayDimColor = color;
113 }
114
116 return _overlayDimColor;
117 }
118
120 return (_mode == Mode::OVERLAY) && IsVisible();
121 }
122
123 // --- Colorblind Filters ---
125 if (_colorblindFilter != filter) {
126 _colorblindFilter = filter;
128 LOG_INFO("[AccessibilityMenu] Colorblind filter changed: ", ColorblindFilterToString(filter));
131 }
132 }
133 }
134
138
140 _onColorblindFilterChanged = std::move(callback);
141 }
142
147
148 // --- Game Speed ---
150 float clampedSpeed = ClampGameSpeed(speed);
151 if (_gameSpeed != clampedSpeed) {
152 _gameSpeed = clampedSpeed;
153 LOG_INFO("[AccessibilityMenu] Game speed set to: ", static_cast<int>(_gameSpeed * 100), "%");
156 }
157 }
158 }
159
161 return _gameSpeed;
162 }
163
164 void AccessibilityMenu::SetOnGameSpeedChanged(std::function<void(float)> callback) {
165 _onGameSpeedChanged = std::move(callback);
166 }
167
169 _gameSpeed = ClampGameSpeed(speed);
170 }
171
172 // --- Key Bindings ---
174 LOG_INFO("[AccessibilityMenu] Opening key bindings configuration");
177 } else {
178 LOG_INFO("[AccessibilityMenu] No key bindings configuration callback set");
179 // TODO: Implement key bindings configuration dialog
180 }
181 }
182
183 void AccessibilityMenu::SetOnConfigureKeyBindings(std::function<void()> callback) {
184 _onConfigureKeyBindings = std::move(callback);
185 }
186
187 // --- Callbacks ---
188 void AccessibilityMenu::SetOnBack(std::function<void()> callback) {
189 _onBack = std::move(callback);
190 }
191
192 void AccessibilityMenu::SetOnMainMenu(std::function<void()> callback) {
193 _onMainMenu = std::move(callback);
194 }
195
196 // --- Visual Updates ---
200
202 if (!_menu) {
203 return;
204 }
205
206 const char *filterStr = ColorblindFilterToString(_colorblindFilter);
207 char buttonText[64];
208 snprintf(buttonText, sizeof(buttonText), "COLORBLIND: %s", filterStr);
209
210 if (_menu->GetButtonCount() > COLORBLIND_FILTER_INDEX) {
211 auto button = _menu->GetButton(COLORBLIND_FILTER_INDEX);
212 button->SetText(buttonText);
213 }
214 }
215
216 // --- Helper Methods ---
234
236 switch (filter) {
238 return "NONE";
240 return "PROTANOPIA";
242 return "DEUTERANOPIA";
244 return "TRITANOPIA";
246 return "MONOCHROMACY";
247 default:
248 return "UNKNOWN";
249 }
250 }
251
252 float AccessibilityMenu::ClampGameSpeed(float speed) const {
253 return std::max(0.25f, std::min(1.0f, speed));
254 }
255} // namespace Game
#define LOG_INFO(...)
Definition Logger.hpp:181
void SetGameSpeedSilent(float speed)
Set game speed without emitting callbacks.
ColorblindFilter
Colorblind filter types.
@ MONOCHROMACY
Complete color blindness.
void SetOnColorblindFilterChanged(std::function< void(ColorblindFilter)> callback)
Set callback invoked when colorblind filter changes.
std::function< void()> _onBack
void SetGameSpeed(float speed)
Set game speed multiplier (0.5 = 50% speed, 1.0 = normal)
std::function< void(float)> _onGameSpeedChanged
AccessibilityMenu(UI::IUIFactory &uiFactory, Graphics::IGraphics &graphics)
Construct a new AccessibilityMenu.
void RefreshVisuals()
Refresh button labels/colors to match current state.
void SetColorblindFilter(ColorblindFilter filter)
Set the colorblind filter type.
void SetOverlayDimColor(unsigned int color)
void Initialize() override
Initialize UI elements.
void OpenKeyBindingsConfig()
Open key bindings configuration dialog.
Graphics::IGraphics & _graphics
float ClampGameSpeed(float speed) const
static constexpr size_t COLORBLIND_FILTER_INDEX
void SetOnMainMenu(std::function< void()> callback)
Set callback invoked when "Main Menu" is clicked (overlay mode)
void Update() override
Update menu state (should be called every frame).
ColorblindFilter GetColorblindFilter() const
Get the current colorblind filter.
void SetOnBack(std::function< void()> callback)
Set callback invoked when Back is clicked.
unsigned int GetOverlayDimColor() const
std::function< void(ColorblindFilter)> _onColorblindFilterChanged
const char * ColorblindFilterToString(ColorblindFilter filter) const
void Render() override
Render menu (should be called every frame).
ColorblindFilter _colorblindFilter
void SetOnGameSpeedChanged(std::function< void(float)> callback)
Set callback invoked when game speed changes.
std::function< void()> _onConfigureKeyBindings
float GetGameSpeed() const
Get current game speed multiplier.
void SetOnConfigureKeyBindings(std::function< void()> callback)
Set callback invoked when user wants to configure key bindings.
std::function< void()> _onMainMenu
ColorblindFilter NextColorblindFilter(ColorblindFilter current) const
Mode
Display mode for the accessibility menu.
@ OVERLAY
Displays over the game with a dimmed background.
void SetColorblindFilterSilent(ColorblindFilter filter)
Set colorblind filter without emitting callbacks.
Base class for all menu implementations.
Definition BaseMenu.hpp:26
virtual void Update()
Update menu state (should be called every frame).
Definition BaseMenu.cpp:16
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
virtual void Render()
Render menu (should be called every frame).
Definition BaseMenu.cpp:22
virtual void Hide()
Hide the menu.
Definition BaseMenu.cpp:34
virtual bool IsVisible() const
Check if menu is currently visible.
Definition BaseMenu.cpp:40
Abstract interface for graphics rendering operations.
Definition IGraphics.hpp:32
virtual void DrawText(int fontHandle, const char *text, int x, int y, int fontSize, unsigned int color)=0
Draw text using a loaded font.
virtual int GetScreenWidth() const =0
Get the screen width (same as window width)
Abstract factory interface for creating UI elements.