Player12
An open source football management simulator
Loading...
Searching...
No Matches
button_manager.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2// Football Management Project
3// Copyright (c) 2025 - 2026 Flavio Milinanni. All Rights Reserved.
4//
5// This file is part of the Football Management Project.
6// See the LICENSE file in the project root.
7// -----------------------------------------------------------------------------
8
9#pragma once
10#include <SDL3/SDL.h>
11#include <SDL3_ttf/SDL_ttf.h>
12
13#include <cstddef>
14#include <functional>
15#include <string>
16#include <string_view>
17#include <vector>
18
23{
24 SDL_Color backgroundColor = {70, 70, 150, 255};
25 SDL_Color borderColor = {100, 100, 200, 255};
26 SDL_Color textColor = {255, 255, 255, 255};
27 SDL_Color hoverBackgroundColor = {90, 90, 170, 255};
28 SDL_Color hoverBorderColor = {120, 120, 220, 255};
29 SDL_Color selectedBackgroundColor = {120, 120, 220, 255};
30 SDL_Color selectedBorderColor = {150, 150, 250, 255};
31 bool hasBorder = true;
32 int borderWidth = 1;
33};
34
38struct Button
39{
40 SDL_FRect rect{};
41 SDL_Texture* textTexture = nullptr;
42 SDL_FRect textRect{};
43 std::string label;
44 std::function<void()> onClick;
45 bool isHovered = false;
46 bool isVisible = true;
47 bool isSelected = false;
49 int id = -1;
50};
51
56{
57 public:
63 ButtonManager(SDL_Renderer* renderer_ptr, TTF_Font* font_ptr);
64
69 ButtonManager(const ButtonManager&) = delete;
71
76 int getButtonCount() const { return static_cast<int>(buttons.size()); }
77 // Button creation and management
78
86 int addButton(const SDL_FRect& rect, std::string_view label,
87 std::function<void()> callback);
88
97 int addButton(const SDL_FRect& rect, std::string_view label,
98 const ButtonStyle& style, std::function<void()> callback);
99
110 int addButton(float x, float y, float w, float h, std::string_view label,
111 std::function<void()> callback);
112
125 int addButton(float x, float y, float w, float h, std::string_view label,
126 const ButtonStyle& style, std::function<void()> callback);
127
132 void removeButton(int buttonId);
133
137 void clearButtons();
138
139 // Button modification
145 void setButtonStyle(int buttonId, const ButtonStyle& style);
146
152 void setButtonVisible(int buttonId, bool visible);
153
159 void setButtonEnabled(int buttonId, bool enabled);
160
166 void setButtonText(int buttonId, std::string_view text);
167
173 void setButtonSelected(int buttonId, bool selected);
174
175 // Event handling
181 void handleMouseMove(float x, float y);
182
189 bool handleMouseClick(float x, float y) const;
190
191 // Rendering
195 void render();
196
197 // Utility
202 void setFont(TTF_Font* newFont) { font = newFont; }
203
208 TTF_Font* getFont() const { return font; }
209
214 void setDefaultStyle(const ButtonStyle& style) { defaultStyle = style; }
215
220 const std::vector<Button>& getButtons() const { return buttons; }
221
225 void recreateTextures();
226
232 void updateButtonPosition(size_t buttonIndex, SDL_FRect newRect);
233
239 void updateButtonPositionById(int buttonId, SDL_FRect newRect);
240
241 private:
242 void renderButton(const Button& button);
243 bool isPointInButton(float x, float y, const Button& button) const;
244 void createButtonTexture(Button& btn);
245
246 SDL_Renderer* renderer;
247 TTF_Font* font;
248 std::vector<Button> buttons;
251};
Manages a collection of UI buttons.
Definition button_manager.h:56
void recreateTextures()
Recreates textures for all buttons (e.g. on text change).
Definition button_manager.cpp:250
void setButtonText(int buttonId, std::string_view text)
Sets the text label of a specific button.
Definition button_manager.cpp:123
void setButtonStyle(int buttonId, const ButtonStyle &style)
Sets the style of a specific button.
Definition button_manager.cpp:103
void setButtonEnabled(int buttonId, bool enabled)
Sets the enabled state of a specific button.
ButtonManager(const ButtonManager &)=delete
bool isPointInButton(float x, float y, const Button &button) const
Definition button_manager.cpp:222
const std::vector< Button > & getButtons() const
Gets a copy of the list of buttons.
Definition button_manager.h:220
void renderButton(const Button &button)
Definition button_manager.cpp:176
bool handleMouseClick(float x, float y) const
Handles mouse click events for buttons.
Definition button_manager.cpp:143
void handleMouseMove(float x, float y)
Handles mouse movement events for buttons.
Definition button_manager.cpp:134
int addButton(const SDL_FRect &rect, std::string_view label, std::function< void()> callback)
Adds a new button.
Definition button_manager.cpp:32
~ButtonManager()
Destroys the ButtonManager.
Definition button_manager.cpp:20
std::vector< Button > buttons
Definition button_manager.h:248
SDL_Renderer * renderer
Definition button_manager.h:246
void updateButtonPosition(size_t buttonIndex, SDL_FRect newRect)
Updates a button's position by its index.
Definition button_manager.cpp:255
void removeButton(int buttonId)
Removes a button by ID.
Definition button_manager.cpp:80
void clearButtons()
Clears all managed buttons.
Definition button_manager.cpp:90
ButtonStyle defaultStyle
Definition button_manager.h:249
TTF_Font * getFont() const
Gets the font currently used for buttons.
Definition button_manager.h:208
int getButtonCount() const
Gets the total number of buttons managed.
Definition button_manager.h:76
void render()
Renders all visible buttons.
Definition button_manager.cpp:155
void setFont(TTF_Font *newFont)
Sets the font used for all buttons.
Definition button_manager.h:202
void createButtonTexture(Button &btn)
Definition button_manager.cpp:229
void updateButtonPositionById(int buttonId, SDL_FRect newRect)
Updates a button's position by its ID.
Definition button_manager.cpp:266
TTF_Font * font
Definition button_manager.h:247
void setDefaultStyle(const ButtonStyle &style)
Sets the default style applied to new buttons.
Definition button_manager.h:214
int nextButtonId
Definition button_manager.h:250
void setButtonVisible(int buttonId, bool visible)
Sets the visibility of a specific button.
Definition button_manager.cpp:113
void setButtonSelected(int buttonId, bool selected)
Sets whether a specific button is selected.
Definition button_manager.cpp:166
ButtonManager & operator=(const ButtonManager &)=delete
const char * label
Definition input_actions.cpp:27
Style configuration for buttons.
Definition button_manager.h:23
SDL_Color hoverBorderColor
Definition button_manager.h:28
int borderWidth
Definition button_manager.h:32
SDL_Color backgroundColor
Definition button_manager.h:24
SDL_Color textColor
Definition button_manager.h:26
SDL_Color borderColor
Definition button_manager.h:25
SDL_Color hoverBackgroundColor
Definition button_manager.h:27
SDL_Color selectedBorderColor
Definition button_manager.h:30
SDL_Color selectedBackgroundColor
Definition button_manager.h:29
bool hasBorder
Definition button_manager.h:31
Represents a single UI button.
Definition button_manager.h:39
std::function< void()> onClick
Definition button_manager.h:44
bool isHovered
Definition button_manager.h:45
ButtonStyle style
Definition button_manager.h:48
SDL_Texture * textTexture
Definition button_manager.h:41
bool isVisible
Definition button_manager.h:46
SDL_FRect textRect
Definition button_manager.h:42
SDL_FRect rect
Definition button_manager.h:40
std::string label
Definition button_manager.h:43
bool isSelected
Definition button_manager.h:47