Loading...
Searching...
No Matches
Button.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith.
2// Malena Framework — Licensed under PolyForm Noncommercial 1.0.0; commercial use requires a paid license. See LICENSE.
3
4#ifndef MALENA_BUTTON_H
5#define MALENA_BUTTON_H
6
7#pragma once
8
11#include <Malena/Core/Core.h>
15#include <string>
16#include <optional>
17namespace ml
18{
59 template<typename T, typename S>
60 class Button : public T, public Themeable
61 {
62 static_assert(std::is_base_of_v<Core, T>,
63 "Button<T, S>: T must be derived from ml::Core.");
64
65 sf::Text _text;
66
67 public:
79 std::optional<S> buttonSize = std::nullopt,
80 const std::string& text = "",
81 unsigned int charSize = 30);
82
90 void setPosition(const sf::Vector2f& position) override;
91
92 // ── Label text ───────────────────────────────────────────────────────
93
98 void setString(const sf::String& text);
99
104 [[nodiscard]] const sf::String& getString() const;
105
110 void setCharacterSize(unsigned int size);
111
116 [[nodiscard]] unsigned int getCharacterSize() const;
117
123 void setFont(const sf::Font& font);
124
126 void setFont(const sf::Font&& font) = delete;
127
132 [[nodiscard]] const sf::Font& getFont() const;
133
134 // ── Label styling ─────────────────────────────────────────────────────
135
140 void setTextColor(const sf::Color& color);
141
146 [[nodiscard]] sf::Color getTextColor() const;
147
152 void setTextOutlineColor(const sf::Color& color);
153
158 [[nodiscard]] sf::Color getTextOutlineColor() const;
159
164 void setTextOutlineThickness(float thickness);
165
170 [[nodiscard]] float getTextOutlineThickness() const;
171
176 void setLetterSpacing(float spacingFactor);
177
182 [[nodiscard]] float getLetterSpacing() const;
183
188 void setLineSpacing(float spacingFactor);
189
194 [[nodiscard]] float getLineSpacing() const;
195
200 void setStyle(std::uint32_t style);
201
206 [[nodiscard]] std::uint32_t getStyle() const;
207
213 [[nodiscard]] sf::Vector2f findCharacterPos(std::size_t index) const;
214
215 // ── Transform ────────────────────────────────────────────────────────
216
219
222
224 void move(sf::Vector2f offset);
225
227 void rotate(sf::Angle angle);
228
231
232 // ── Themeable ─────────────────────────────────────────────────────────
233
241 void onThemeApplied(const Theme& theme) override;
242
243 // ── Draw ─────────────────────────────────────────────────────────────
244
253 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
254
255 // ── Forwarded from T ─────────────────────────────────────────────────
256 using T::getPosition;
257 using T::setFillColor;
258 using T::setOutlineColor;
259 using T::setOutlineThickness;
260 using T::getGlobalBounds;
261 using T::getLocalBounds;
262 using T::getOrigin;
263 using T::getTransform;
264 using T::getInverseTransform;
265 using T::setOrigin;
266 using T::getGeometricCenter;
267 using T::getPoint;
268 using T::getPointCount;
269 };
270
271} // namespace ml
272
273#include "../../../../src/Graphics/Base/Button.tpp"
274#endif // MALENA_BUTTON_H
sf::Color getTextOutlineColor() const
Return the current label outline color.
void setLetterSpacing(float spacingFactor)
Set the label letter spacing factor.
void setFont(const sf::Font &font)
Set the label font.
void setString(const sf::String &text)
Set the label string.
void onThemeApplied(const Theme &theme) override
Re-style from the active theme.
std::uint32_t getStyle() const
Return the current label text style bitmask.
const sf::String & getString() const
Return the current label string.
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the button shape and its label to a render target.
void setScale(sf::Vector2f scale)
Scale both shape and label.
void scale(sf::Vector2f scale)
Apply a multiplicative scale to both shape and label.
sf::Color getTextColor() const
Return the current label fill color.
sf::Vector2f findCharacterPos(std::size_t index) const
Return the world-space position of a character in the label.
void setTextOutlineThickness(float thickness)
Set the label outline thickness in pixels.
float getLetterSpacing() const
Return the current label letter spacing factor.
void setLineSpacing(float spacingFactor)
Set the label line spacing factor.
void rotate(sf::Angle angle)
Rotate both shape and label by angle.
void setStyle(std::uint32_t style)
Set the label text style (bold, italic, underline, etc.).
unsigned int getCharacterSize() const
Return the current label character size.
void setPosition(const sf::Vector2f &position) override
Set the world-space position of both the shape and the label.
float getLineSpacing() const
Return the current label line spacing factor.
void setTextColor(const sf::Color &color)
Set the label fill color.
const sf::Font & getFont() const
Return the current label font.
void setTextOutlineColor(const sf::Color &color)
Set the label outline color.
void setRotation(sf::Angle angle)
Set the absolute rotation of both shape and label.
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 move(sf::Vector2f offset)
Translate both shape and label by offset.
float getTextOutlineThickness() const
Return the current label outline thickness.
void setCharacterSize(unsigned int size)
Set the label character size.
void setFont(const sf::Font &&font)=delete
Deleted — rvalue font references are not supported.
static const sf::Font & getDefault()
Return the built-in Arial font.
Definition Component.h:22
Vector2< float > Vector2f
Universal design token set applied across all Themeable components.
Definition Theme.h:70