Loading...
Searching...
No Matches
ml::Button< T, S > Class Template Reference

Generic button template that combines a shape with a text label. More...

#include <Button.h>

Inheritance diagram for ml::Button< T, S >:

Public Member Functions

 Button (const sf::Font &font=FontManager<>::getDefault(), std::optional< S > buttonSize=std::nullopt, const std::string &text="", unsigned int charSize=30)
 Construct a button with an optional size, label, and font.
void draw (sf::RenderTarget &target, sf::RenderStates states) const override
 Draw the button shape and its label to a render target.
sf::Vector2f findCharacterPos (std::size_t index) const
 Return the world-space position of a character in the label.
unsigned int getCharacterSize () const
 Return the current label character size.
const sf::FontgetFont () const
 Return the current label font.
float getLetterSpacing () const
 Return the current label letter spacing factor.
float getLineSpacing () const
 Return the current label line spacing factor.
const sf::StringgetString () const
 Return the current label string.
std::uint32_t getStyle () const
 Return the current label text style bitmask.
sf::Color getTextColor () const
 Return the current label fill color.
sf::Color getTextOutlineColor () const
 Return the current label outline color.
float getTextOutlineThickness () const
 Return the current label outline thickness.
void move (sf::Vector2f offset)
 Translate both shape and label by offset.
void rotate (sf::Angle angle)
 Rotate both shape and label by angle.
void scale (sf::Vector2f scale)
 Apply a multiplicative scale to both shape and label.
void setCharacterSize (unsigned int size)
 Set the label character size.
void setFont (const sf::Font &&font)=delete
 Deleted — rvalue font references are not supported.
void setFont (const sf::Font &font)
 Set the label font.
void setLetterSpacing (float spacingFactor)
 Set the label letter spacing factor.
void setLineSpacing (float spacingFactor)
 Set the label line spacing factor.
void setPosition (const sf::Vector2f &position) override
 Set the world-space position of both the shape and the label.
void setRotation (sf::Angle angle)
 Set the absolute rotation of both shape and label.
void setScale (sf::Vector2f scale)
 Scale both shape and label.
void setString (const sf::String &text)
 Set the label string.
void setStyle (std::uint32_t style)
 Set the label text style (bold, italic, underline, etc.).
void setTextColor (const sf::Color &color)
 Set the label fill color.
void setTextOutlineColor (const sf::Color &color)
 Set the label outline color.
void setTextOutlineThickness (float thickness)
 Set the label outline thickness in pixels.

Detailed Description

template<typename T, typename S>
class ml::Button< T, S >

Generic button template that combines a shape with a text label.

Button<T, S> layers an sf::Text label on top of any framework shape type T, exposing a unified API for positioning, styling, and interacting with both the background shape and its label together.

T must derive from ml::Core — this is enforced at compile time via static_assert. S is the size type that T's constructor accepts (e.g., sf::Vector2f for Rectangle, float for Circle, std::size_t for Convex).

Click and hover subscriptions are inherited from T (which inherits ComponentBase and therefore Subscribable). The label text is automatically centered over the shape on every setPosition call.

Typical use via concrete subclasses

Most code uses the pre-built button types rather than Button directly:

btn.setSize({200.f, 50.f});
btn.setPosition({100.f, 200.f});
btn.setString("Play");
btn.onClick([]{ startGame(); });
addComponent(btn);
void setString(const sf::String &text)
Set the label string.
void setPosition(const sf::Vector2f &position) override
Set the world-space position of both the shape and the label.
void setTextColor(const sf::Color &color)
Set the label fill color.
void onClick(std::function< void()> callback)
Register a no-argument callback invoked when this component is clicked.
A rectangular button with a centered text label.
void setSize(const sf::Vector2f &size)
Set the width and height of the rectangle.
static const Color White

With a manifest

RectangleButton with an attached manifest.
Template Parameters
TShape type — must derive from ml::Core. Typically ml::Rectangle, ml::Circle, or ml::Convex.
SSize parameter type accepted by T's constructor.
See also
RectangleButton, CircleButton, ConvexButton, Graphic

Button.

Definition at line 57 of file Button.h.

Constructor & Destructor Documentation

◆ Button()

template<typename T, typename S>
ml::Button< T, S >::Button ( const sf::Font & font = FontManager<>::getDefault(),
std::optional< S > buttonSize = std::nullopt,
const std::string & text = "",
unsigned int charSize = 30 )

Construct a button with an optional size, label, and font.

Parameters
fontFont for the label text. Defaults to the built-in Arial font via FontManager<>::getDefault().
buttonSizeOptional initial size passed to T's constructor. If std::nullopt, the shape is default-constructed.
textInitial label string. Empty by default.
charSizeLabel character size in points. Defaults to 30.

Member Function Documentation

◆ draw()

template<typename T, typename S>
void ml::Button< T, S >::draw ( sf::RenderTarget & target,
sf::RenderStates states ) const
override

Draw the button shape and its label to a render target.

Draws T first, then the centered sf::Text label on top.

Parameters
targetSFML render target.
statesCurrent render states.

◆ findCharacterPos()

template<typename T, typename S>
sf::Vector2f ml::Button< T, S >::findCharacterPos ( std::size_t index) const
nodiscard

Return the world-space position of a character in the label.

Parameters
indexZero-based character index.
Returns
World-space position of the character's origin.

◆ getCharacterSize()

template<typename T, typename S>
unsigned int ml::Button< T, S >::getCharacterSize ( ) const
nodiscard

Return the current label character size.

Returns
Character size in points.

◆ getFont()

template<typename T, typename S>
const sf::Font & ml::Button< T, S >::getFont ( ) const
nodiscard

Return the current label font.

Returns
Const reference to the active sf::Font.

◆ getLetterSpacing()

template<typename T, typename S>
float ml::Button< T, S >::getLetterSpacing ( ) const
nodiscard

Return the current label letter spacing factor.

Returns
Letter spacing multiplier.

◆ getLineSpacing()

template<typename T, typename S>
float ml::Button< T, S >::getLineSpacing ( ) const
nodiscard

Return the current label line spacing factor.

Returns
Line spacing multiplier.

◆ getString()

template<typename T, typename S>
const sf::String & ml::Button< T, S >::getString ( ) const
nodiscard

Return the current label string.

Returns
Const reference to the current sf::String.

◆ getStyle()

template<typename T, typename S>
std::uint32_t ml::Button< T, S >::getStyle ( ) const
nodiscard

Return the current label text style bitmask.

Returns
sf::Text::Style bitmask.

◆ getTextColor()

template<typename T, typename S>
sf::Color ml::Button< T, S >::getTextColor ( ) const
nodiscard

Return the current label fill color.

Returns
Current text sf::Color.

◆ getTextOutlineColor()

template<typename T, typename S>
sf::Color ml::Button< T, S >::getTextOutlineColor ( ) const
nodiscard

Return the current label outline color.

Returns
Current text outline sf::Color.

◆ getTextOutlineThickness()

template<typename T, typename S>
float ml::Button< T, S >::getTextOutlineThickness ( ) const
nodiscard

Return the current label outline thickness.

Returns
Outline thickness in pixels.

◆ move()

template<typename T, typename S>
void ml::Button< T, S >::move ( sf::Vector2f offset)

Translate both shape and label by offset.

◆ rotate()

template<typename T, typename S>
void ml::Button< T, S >::rotate ( sf::Angle angle)

Rotate both shape and label by angle.

◆ scale()

template<typename T, typename S>
void ml::Button< T, S >::scale ( sf::Vector2f scale)

Apply a multiplicative scale to both shape and label.

◆ setCharacterSize()

template<typename T, typename S>
void ml::Button< T, S >::setCharacterSize ( unsigned int size)

Set the label character size.

Parameters
sizeCharacter size in points.

◆ setFont() [1/2]

template<typename T, typename S>
void ml::Button< T, S >::setFont ( const sf::Font && font)
delete

Deleted — rvalue font references are not supported.

◆ setFont() [2/2]

template<typename T, typename S>
void ml::Button< T, S >::setFont ( const sf::Font & font)

Set the label font.

Parameters
fontConst reference to the font to use.

◆ setLetterSpacing()

template<typename T, typename S>
void ml::Button< T, S >::setLetterSpacing ( float spacingFactor)

Set the label letter spacing factor.

Parameters
spacingFactorMultiplier applied to the default letter spacing.

◆ setLineSpacing()

template<typename T, typename S>
void ml::Button< T, S >::setLineSpacing ( float spacingFactor)

Set the label line spacing factor.

Parameters
spacingFactorMultiplier applied to the default line height.

◆ setPosition()

template<typename T, typename S>
void ml::Button< T, S >::setPosition ( const sf::Vector2f & position)
override

Set the world-space position of both the shape and the label.

Repositions the background shape and re-centers the text label.

Parameters
positionNew position in world coordinates.

◆ setRotation()

template<typename T, typename S>
void ml::Button< T, S >::setRotation ( sf::Angle angle)

Set the absolute rotation of both shape and label.

◆ setScale()

template<typename T, typename S>
void ml::Button< T, S >::setScale ( sf::Vector2f scale)

Scale both shape and label.

◆ setString()

template<typename T, typename S>
void ml::Button< T, S >::setString ( const sf::String & text)

Set the label string.

Parameters
textNew label text.

◆ setStyle()

template<typename T, typename S>
void ml::Button< T, S >::setStyle ( std::uint32_t style)

Set the label text style (bold, italic, underline, etc.).

Parameters
styleBitmask of sf::Text::Style flags.

◆ setTextColor()

template<typename T, typename S>
void ml::Button< T, S >::setTextColor ( const sf::Color & color)

Set the label fill color.

Parameters
colorNew text fill color.

◆ setTextOutlineColor()

template<typename T, typename S>
void ml::Button< T, S >::setTextOutlineColor ( const sf::Color & color)

Set the label outline color.

Parameters
colorNew text outline color.

◆ setTextOutlineThickness()

template<typename T, typename S>
void ml::Button< T, S >::setTextOutlineThickness ( float thickness)

Set the label outline thickness in pixels.

Parameters
thicknessOutline thickness. Pass 0 to remove the outline.

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