Player12
An open source football management simulator
Loading...
Searching...
No Matches
ButtonManager Class Reference

Manages a collection of UI buttons. More...

#include <button_manager.h>

Public Member Functions

 ButtonManager (SDL_Renderer *renderer_ptr, TTF_Font *font_ptr)
 Constructs a new ButtonManager.
 
 ~ButtonManager ()
 Destroys the ButtonManager.
 
 ButtonManager (const ButtonManager &)=delete
 
ButtonManager & operator= (const ButtonManager &)=delete
 
int getButtonCount () const
 Gets the total number of buttons managed.
 
int addButton (const SDL_FRect &rect, std::string_view label, std::function< void()> callback)
 Adds a new button.
 
int addButton (const SDL_FRect &rect, std::string_view label, const ButtonStyle &style, std::function< void()> callback)
 Adds a new button with a specific style.
 
int addButton (float x, float y, float w, float h, std::string_view label, std::function< void()> callback)
 Adds a new button using individual coordinates.
 
int addButton (float x, float y, float w, float h, std::string_view label, const ButtonStyle &style, std::function< void()> callback)
 Adds a new button with a specific style using individual coordinates.
 
void removeButton (int buttonId)
 Removes a button by ID.
 
void clearButtons ()
 Clears all managed buttons.
 
void setButtonStyle (int buttonId, const ButtonStyle &style)
 Sets the style of a specific button.
 
void setButtonVisible (int buttonId, bool visible)
 Sets the visibility of a specific button.
 
void setButtonEnabled (int buttonId, bool enabled)
 Sets the enabled state of a specific button.
 
void setButtonText (int buttonId, std::string_view text)
 Sets the text label of a specific button.
 
void setButtonSelected (int buttonId, bool selected)
 Sets whether a specific button is selected.
 
void handleMouseMove (float x, float y)
 Handles mouse movement events for buttons.
 
bool handleMouseClick (float x, float y) const
 Handles mouse click events for buttons.
 
void render ()
 Renders all visible buttons.
 
void setFont (TTF_Font *newFont)
 Sets the font used for all buttons.
 
TTF_Font * getFont () const
 Gets the font currently used for buttons.
 
void setDefaultStyle (const ButtonStyle &style)
 Sets the default style applied to new buttons.
 
const std::vector< Button > & getButtons () const
 Gets a copy of the list of buttons.
 
void recreateTextures ()
 Recreates textures for all buttons (e.g. on text change).
 
void updateButtonPosition (size_t buttonIndex, SDL_FRect newRect)
 Updates a button's position by its index.
 
void updateButtonPositionById (int buttonId, SDL_FRect newRect)
 Updates a button's position by its ID.
 

Private Member Functions

void renderButton (const Button &button)
 
bool isPointInButton (float x, float y, const Button &button) const
 
void createButtonTexture (Button &btn)
 

Private Attributes

SDL_Renderer * renderer
 
TTF_Font * font
 
std::vector< Button > buttons
 
ButtonStyle defaultStyle
 
int nextButtonId = 0
 

Detailed Description

Manages a collection of UI buttons.

Constructor & Destructor Documentation

◆ ButtonManager() [1/2]

ButtonManager::ButtonManager ( SDL_Renderer *  renderer_ptr,
TTF_Font *  font_ptr 
)

Constructs a new ButtonManager.

Parameters
renderer_ptrThe SDL renderer used for drawing buttons.
font_ptrThe font used for button text.

◆ ~ButtonManager()

ButtonManager::~ButtonManager ( )

Destroys the ButtonManager.

◆ ButtonManager() [2/2]

ButtonManager::ButtonManager ( const ButtonManager &  )
delete

Member Function Documentation

◆ addButton() [1/4]

int ButtonManager::addButton ( const SDL_FRect &  rect,
std::string_view  label,
const ButtonStyle &  style,
std::function< void()>  callback 
)

Adds a new button with a specific style.

Parameters
rectThe rectangle defining the button's position and size.
labelThe text displayed on the button.
styleThe style to apply to the button.
callbackThe function to call when the button is clicked.
Returns
The ID of the newly created button.

◆ addButton() [2/4]

int ButtonManager::addButton ( const SDL_FRect &  rect,
std::string_view  label,
std::function< void()>  callback 
)

Adds a new button.

Parameters
rectThe rectangle defining the button's position and size.
labelThe text displayed on the button.
callbackThe function to call when the button is clicked.
Returns
The ID of the newly created button.

◆ addButton() [3/4]

int ButtonManager::addButton ( float  x,
float  y,
float  w,
float  h,
std::string_view  label,
const ButtonStyle &  style,
std::function< void()>  callback 
)

Adds a new button with a specific style using individual coordinates.

Parameters
xThe x-coordinate.
yThe y-coordinate.
wThe width.
hThe height.
labelThe text displayed on the button.
styleThe style to apply to the button.
callbackThe function to call when the button is clicked.
Returns
The ID of the newly created button.

◆ addButton() [4/4]

int ButtonManager::addButton ( float  x,
float  y,
float  w,
float  h,
std::string_view  label,
std::function< void()>  callback 
)

Adds a new button using individual coordinates.

Parameters
xThe x-coordinate.
yThe y-coordinate.
wThe width.
hThe height.
labelThe text displayed on the button.
callbackThe function to call when the button is clicked.
Returns
The ID of the newly created button.

◆ clearButtons()

void ButtonManager::clearButtons ( )

Clears all managed buttons.

◆ createButtonTexture()

void ButtonManager::createButtonTexture ( Button &  btn)
private

◆ getButtonCount()

int ButtonManager::getButtonCount ( ) const
inline

Gets the total number of buttons managed.

Returns
The number of buttons.

◆ getButtons()

const std::vector< Button > & ButtonManager::getButtons ( ) const
inline

Gets a copy of the list of buttons.

Returns
A vector of buttons.

◆ getFont()

TTF_Font * ButtonManager::getFont ( ) const
inline

Gets the font currently used for buttons.

Returns
The font.

◆ handleMouseClick()

bool ButtonManager::handleMouseClick ( float  x,
float  y 
) const

Handles mouse click events for buttons.

Parameters
xThe mouse x-coordinate.
yThe mouse y-coordinate.
Returns
True if a button was clicked, false otherwise.

◆ handleMouseMove()

void ButtonManager::handleMouseMove ( float  x,
float  y 
)

Handles mouse movement events for buttons.

Parameters
xThe mouse x-coordinate.
yThe mouse y-coordinate.

◆ isPointInButton()

bool ButtonManager::isPointInButton ( float  x,
float  y,
const Button &  button 
) const
private

◆ operator=()

ButtonManager & ButtonManager::operator= ( const ButtonManager &  )
delete

◆ recreateTextures()

void ButtonManager::recreateTextures ( )

Recreates textures for all buttons (e.g. on text change).

◆ removeButton()

void ButtonManager::removeButton ( int  buttonId)

Removes a button by ID.

Parameters
buttonIdThe ID of the button to remove.

◆ render()

void ButtonManager::render ( )

Renders all visible buttons.

◆ renderButton()

void ButtonManager::renderButton ( const Button &  button)
private

◆ setButtonEnabled()

void ButtonManager::setButtonEnabled ( int  buttonId,
bool  enabled 
)

Sets the enabled state of a specific button.

Parameters
buttonIdThe ID of the button.
enabledTrue to enable it, false to disable it.

◆ setButtonSelected()

void ButtonManager::setButtonSelected ( int  buttonId,
bool  selected 
)

Sets whether a specific button is selected.

Parameters
buttonIdThe ID of the button.
selectedTrue if selected, false otherwise.

◆ setButtonStyle()

void ButtonManager::setButtonStyle ( int  buttonId,
const ButtonStyle &  style 
)

Sets the style of a specific button.

Parameters
buttonIdThe ID of the button.
styleThe new style to apply.

◆ setButtonText()

void ButtonManager::setButtonText ( int  buttonId,
std::string_view  text 
)

Sets the text label of a specific button.

Parameters
buttonIdThe ID of the button.
textThe new text for the button.

◆ setButtonVisible()

void ButtonManager::setButtonVisible ( int  buttonId,
bool  visible 
)

Sets the visibility of a specific button.

Parameters
buttonIdThe ID of the button.
visibleTrue to make it visible, false to hide it.

◆ setDefaultStyle()

void ButtonManager::setDefaultStyle ( const ButtonStyle &  style)
inline

Sets the default style applied to new buttons.

Parameters
styleThe default style.

◆ setFont()

void ButtonManager::setFont ( TTF_Font *  newFont)
inline

Sets the font used for all buttons.

Parameters
newFontThe new font to use.

◆ updateButtonPosition()

void ButtonManager::updateButtonPosition ( size_t  buttonIndex,
SDL_FRect  newRect 
)

Updates a button's position by its index.

Parameters
buttonIndexThe index of the button in the internal list.
newRectThe new rectangle.

◆ updateButtonPositionById()

void ButtonManager::updateButtonPositionById ( int  buttonId,
SDL_FRect  newRect 
)

Updates a button's position by its ID.

Parameters
buttonIdThe ID of the button.
newRectThe new rectangle.

Member Data Documentation

◆ buttons

std::vector<Button> ButtonManager::buttons
private

◆ defaultStyle

ButtonStyle ButtonManager::defaultStyle
private

◆ font

TTF_Font* ButtonManager::font
private

◆ nextButtonId

int ButtonManager::nextButtonId = 0
private

◆ renderer

SDL_Renderer* ButtonManager::renderer
private

The documentation for this class was generated from the following files: