Generic button template that combines a shape with a text label. More...
#include <Button.h>
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::Font & | getFont () 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::String & | getString () 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. | |
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.
Most code uses the pre-built button types rather than Button directly:
| T | Shape type — must derive from ml::Core. Typically ml::Rectangle, ml::Circle, or ml::Convex. |
| S | Size parameter type accepted by T's constructor. |
| 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.
| font | Font for the label text. Defaults to the built-in Arial font via FontManager<>::getDefault(). |
| buttonSize | Optional initial size passed to T's constructor. If std::nullopt, the shape is default-constructed. |
| text | Initial label string. Empty by default. |
| charSize | Label character size in points. Defaults to 30. |
|
override |
Draw the button shape and its label to a render target.
Draws T first, then the centered sf::Text label on top.
| target | SFML render target. |
| states | Current render states. |
|
nodiscard |
Return the world-space position of a character in the label.
| index | Zero-based character index. |
|
nodiscard |
Return the current label character size.
|
nodiscard |
Return the current label font.
sf::Font.
|
nodiscard |
Return the current label letter spacing factor.
|
nodiscard |
Return the current label line spacing factor.
|
nodiscard |
Return the current label string.
sf::String.
|
nodiscard |
Return the current label text style bitmask.
sf::Text::Style bitmask.
|
nodiscard |
Return the current label fill color.
sf::Color.
|
nodiscard |
Return the current label outline color.
sf::Color.
|
nodiscard |
Return the current label outline thickness.
| void ml::Button< T, S >::move | ( | sf::Vector2f | offset | ) |
Translate both shape and label by offset.
| void ml::Button< T, S >::rotate | ( | sf::Angle | angle | ) |
Rotate both shape and label by angle.
| void ml::Button< T, S >::scale | ( | sf::Vector2f | scale | ) |
Apply a multiplicative scale to both shape and label.
| void ml::Button< T, S >::setCharacterSize | ( | unsigned int | size | ) |
Set the label character size.
| size | Character size in points. |
|
delete |
Deleted — rvalue font references are not supported.
| void ml::Button< T, S >::setFont | ( | const sf::Font & | font | ) |
Set the label font.
| font | Const reference to the font to use. |
| void ml::Button< T, S >::setLetterSpacing | ( | float | spacingFactor | ) |
Set the label letter spacing factor.
| spacingFactor | Multiplier applied to the default letter spacing. |
| void ml::Button< T, S >::setLineSpacing | ( | float | spacingFactor | ) |
Set the label line spacing factor.
| spacingFactor | Multiplier applied to the default line height. |
|
override |
Set the world-space position of both the shape and the label.
Repositions the background shape and re-centers the text label.
| position | New position in world coordinates. |
| void ml::Button< T, S >::setRotation | ( | sf::Angle | angle | ) |
Set the absolute rotation of both shape and label.
| void ml::Button< T, S >::setScale | ( | sf::Vector2f | scale | ) |
Scale both shape and label.
| void ml::Button< T, S >::setString | ( | const sf::String & | text | ) |
Set the label string.
| text | New label text. |
| void ml::Button< T, S >::setStyle | ( | std::uint32_t | style | ) |
Set the label text style (bold, italic, underline, etc.).
| style | Bitmask of sf::Text::Style flags. |
| void ml::Button< T, S >::setTextColor | ( | const sf::Color & | color | ) |
Set the label fill color.
| color | New text fill color. |
| void ml::Button< T, S >::setTextOutlineColor | ( | const sf::Color & | color | ) |
Set the label outline color.
| color | New text outline color. |
| void ml::Button< T, S >::setTextOutlineThickness | ( | float | thickness | ) |
Set the label outline thickness in pixels.
| thickness | Outline thickness. Pass 0 to remove the outline. |