Loading...
Searching...
No Matches
Button.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith. All rights reserved.
2// Malena Framework — Proprietary Software. See LICENSE for terms.
3
4#ifndef MALENA_BUTTON_H
5#define MALENA_BUTTON_H
6
7#pragma once
8
11#include <Malena/Core/Core.h>
13#include <string>
14#include <optional>
15namespace ml
16{
57 template<typename T, typename S>
58 class Button : public T
59 {
60 static_assert(std::is_base_of_v<Core, T>,
61 "Button<T, S>: T must be derived from ml::Core.");
62
63 sf::Text _text;
64
65 public:
77 std::optional<S> buttonSize = std::nullopt,
78 const std::string& text = "",
79 unsigned int charSize = 30);
80
88 void setPosition(const sf::Vector2f& position) override;
89
90 // ── Label text ───────────────────────────────────────────────────────
91
96 void setString(const sf::String& text);
97
102 [[nodiscard]] const sf::String& getString() const;
103
108 void setCharacterSize(unsigned int size);
109
114 [[nodiscard]] unsigned int getCharacterSize() const;
115
121 void setFont(const sf::Font& font);
122
124 void setFont(const sf::Font&& font) = delete;
125
130 [[nodiscard]] const sf::Font& getFont() const;
131
132 // ── Label styling ─────────────────────────────────────────────────────
133
138 void setTextColor(const sf::Color& color);
139
144 [[nodiscard]] sf::Color getTextColor() const;
145
150 void setTextOutlineColor(const sf::Color& color);
151
156 [[nodiscard]] sf::Color getTextOutlineColor() const;
157
162 void setTextOutlineThickness(float thickness);
163
168 [[nodiscard]] float getTextOutlineThickness() const;
169
174 void setLetterSpacing(float spacingFactor);
175
180 [[nodiscard]] float getLetterSpacing() const;
181
186 void setLineSpacing(float spacingFactor);
187
192 [[nodiscard]] float getLineSpacing() const;
193
198 void setStyle(std::uint32_t style);
199
204 [[nodiscard]] std::uint32_t getStyle() const;
205
211 [[nodiscard]] sf::Vector2f findCharacterPos(std::size_t index) const;
212
213 // ── Transform ────────────────────────────────────────────────────────
214
217
220
222 void move(sf::Vector2f offset);
223
225 void rotate(sf::Angle angle);
226
229
230 // ── Draw ─────────────────────────────────────────────────────────────
231
240 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
241
242 // ── Forwarded from T ─────────────────────────────────────────────────
243 using T::getPosition;
244 using T::setFillColor;
245 using T::setOutlineColor;
246 using T::setOutlineThickness;
247 using T::getGlobalBounds;
248 using T::getLocalBounds;
249 using T::getOrigin;
250 using T::getTransform;
251 using T::getInverseTransform;
252 using T::setOrigin;
253 using T::getGeometricCenter;
254 using T::getPoint;
255 using T::getPointCount;
256 };
257
258} // namespace ml
259
260#include "../../../../src/Graphics/Base/Button.tpp"
261#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.
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