Loading...
Searching...
No Matches
Button.h
Go to the documentation of this file.
1#ifndef BUTTON_H
2#define BUTTON_H
3
4#pragma once
5
7#include <Malena/Core/Core.h>
9#include <string>
10
11namespace ml
12{
52 template<typename T, typename S>
57 class Button : public T
58 {
59 static_assert(std::is_base_of_v<Core, T>,
60 "Button<T, S>: T must be derived from ml::Core.");
61
62 sf::Text _text;
63
64 public:
76 std::optional<S> buttonSize = std::nullopt,
77 const std::string& text = "",
78 unsigned int charSize = 30);
79
87 void setPosition(const sf::Vector2f& position) override;
88
89 // ── Label text ───────────────────────────────────────────────────────
90
95 void setString(const sf::String& text);
96
101 [[nodiscard]] const sf::String& getString() const;
102
107 void setCharacterSize(unsigned int size);
108
113 [[nodiscard]] unsigned int getCharacterSize() const;
114
120 void setFont(const sf::Font& font);
121
123 void setFont(const sf::Font&& font) = delete;
124
129 [[nodiscard]] const sf::Font& getFont() const;
130
131 // ── Label styling ─────────────────────────────────────────────────────
132
137 void setTextColor(const sf::Color& color);
138
143 [[nodiscard]] sf::Color getTextColor() const;
144
149 void setTextOutlineColor(const sf::Color& color);
150
155 [[nodiscard]] sf::Color getTextOutlineColor() const;
156
161 void setTextOutlineThickness(float thickness);
162
167 [[nodiscard]] float getTextOutlineThickness() const;
168
173 void setLetterSpacing(float spacingFactor);
174
179 [[nodiscard]] float getLetterSpacing() const;
180
185 void setLineSpacing(float spacingFactor);
186
191 [[nodiscard]] float getLineSpacing() const;
192
197 void setStyle(std::uint32_t style);
198
203 [[nodiscard]] std::uint32_t getStyle() const;
204
210 [[nodiscard]] sf::Vector2f findCharacterPos(std::size_t index) const;
211
212 // ── Transform ────────────────────────────────────────────────────────
213
216
219
221 void move(sf::Vector2f offset);
222
224 void rotate(sf::Angle angle);
225
228
229 // ── Draw ─────────────────────────────────────────────────────────────
230
239 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
240
241 // ── Forwarded from T ─────────────────────────────────────────────────
242 using T::getPosition;
243 using T::setFillColor;
244 using T::setOutlineColor;
245 using T::setOutlineThickness;
246 using T::getGlobalBounds;
247 using T::getLocalBounds;
248 using T::getOrigin;
249 using T::getTransform;
250 using T::getInverseTransform;
251 using T::setOrigin;
252 using T::getGeometricCenter;
253 using T::getPoint;
254 using T::getPointCount;
255 };
256
257} // namespace ml
258
259#include "../../../../src/Graphics/Base/Button.tpp"
260#endif // 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.
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:18
Vector2< float > Vector2f