Velvet 0.1a
A beginner-friendly C++ GUI framework built on SFML.
Loading...
Searching...
No Matches
Button Class Reference

Clickable button widget with text label. More...

#include <Button.hpp>

+ Inheritance diagram for Button:

Public Member Functions

void render (sf::RenderWindow &window) override
 Render the button.
 
void handleEvent (const sf::Event &event, sf::RenderWindow &window) override
 Process hover/press input events.
 
 Button (float width, float height, std::string label, std::unordered_map< std::string, std::variant< unsigned int, float, std::string > > styling={}, unsigned int borderColor=0x000000FFu, float borderThickness=1.f)
 Create a button.
 
 Button (const Button &other)
 Copy constructor.
 
void setPosition (float x, float y) override
 Set top-left position of the button.
 
sf::Vector2< float > getDimensions () override
 Get button dimensions.
 
void overrideStyling (std::unordered_map< std::string, std::variant< unsigned int, float, std::string > > styling)
 Apply/override style properties.
 
- Public Member Functions inherited from Widget
 Widget ()
 Construct a widget with default font loading.
 

Public Attributes

std::function< void()> onclick
 Called when the button is pressed with left mouse button while hovered.
 
- Public Attributes inherited from Widget
sf::Font font
 Shared font loaded by the base widget.
 

Additional Inherited Members

- Protected Attributes inherited from Widget
float x
 
float y
 

Detailed Description

Clickable button widget with text label.

Assign a lambda to onclick to react to left-click presses.

Supported style keys (std::unordered_map<std::string, std::variant<unsigned int, float, std::string>>):

  • fontPath (std::string)
  • fontSize (float)
  • letterSpacing (float)
  • fontColor (unsigned int, RGBA hex like 0xRRGGBBAA)
  • hoverFontColor (unsigned int)
  • outlineColor (unsigned int)
  • outlineThickness (float)
  • backgroundColor (unsigned int)
  • hoverBackgroundColor (unsigned int)
  • width (float)
  • height (float)

Constructor & Destructor Documentation

◆ Button() [1/2]

Button::Button ( float  width,
float  height,
std::string  label,
std::unordered_map< std::string, std::variant< unsigned int, float, std::string > >  styling = {},
unsigned int  borderColor = 0x000000FFu,
float  borderThickness = 1.f 
)

Create a button.

Parameters
widthButton width in pixels.
heightButton height in pixels.
labelText displayed in the center.
stylingOptional style map that overrides default visual properties such as font, colors, and size (see class documentation for supported keys).
borderColorBorder color (maps to the outlineColor style key).
borderThicknessBorder thickness (maps to the outlineThickness style key).

◆ Button() [2/2]

Button::Button ( const Button other)

Copy constructor.

Member Function Documentation

◆ getDimensions()

sf::Vector2< float > Button::getDimensions ( )
overridevirtual

Get button dimensions.

Implements Widget.

◆ handleEvent()

void Button::handleEvent ( const sf::Event &  event,
sf::RenderWindow &  window 
)
overridevirtual

Process hover/press input events.

Reimplemented from Widget.

◆ overrideStyling()

void Button::overrideStyling ( std::unordered_map< std::string, std::variant< unsigned int, float, std::string > >  styling)

Apply/override style properties.

Unknown keys are ignored with a warning.

◆ render()

void Button::render ( sf::RenderWindow &  window)
overridevirtual

Render the button.

Reimplemented from Widget.

◆ setPosition()

void Button::setPosition ( float  x,
float  y 
)
overridevirtual

Set top-left position of the button.

Implements Widget.

Member Data Documentation

◆ onclick

std::function<void()> Button::onclick

Called when the button is pressed with left mouse button while hovered.


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