Loading...
Searching...
No Matches
RadioButton.h
Go to the documentation of this file.
1//
2// RadioButton.h
3//
4
5#ifndef MALENA_RADIOBUTTON_H
6#define MALENA_RADIOBUTTON_H
7
8#pragma once
9
21#include <string>
22#include <type_traits>
23
24namespace ml
25{
27 {
28 public:
29 enum class Flag { SELECTED, DISABLED };
30 enum class State { IDLE, HOVERED, SELECTED, DISABLED };
31 };
32
42 class MALENA_API RadioButton : public ComponentWith<RadioButtonManifest>,
44 public RadioButtonTheme,
45 public Themeable,
46 public Selectable
47 {
48 public:
51
52 private:
53 sf::CircleShape _ring;
54 sf::CircleShape _dot;
55 sf::Text _label;
56 sf::Vector2f _position = {0.f, 0.f};
57 std::string _labelStr;
58
59 void layout();
60 void applyVisualState();
61
62 protected:
63 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
64 void onThemeApplied(const Theme& theme) override;
65
66 public:
67 explicit RadioButton(const std::string& label = "",
69
70 // ── Apply ─────────────────────────────────────────────────────────────
71
72 template<typename S>
73 void applySettings(const S& s)
74 {
75 static_assert(std::is_base_of_v<RadioButtonSettings, S>,
76 "applySettings() requires a type derived from RadioButtonSettings");
77 static_cast<RadioButtonSettings&>(*this) = s;
78 layout();
79 }
80
81 template<typename T>
82 void applyTheme(const T& t)
83 {
84 static_assert(std::is_base_of_v<RadioButtonTheme, T>,
85 "applyTheme() requires a type derived from RadioButtonTheme");
86 static_cast<RadioButtonTheme&>(*this) = t;
87 applyVisualState();
88 }
89
90 template<typename St>
91 void applyStyle(const St& s)
92 {
93 static_assert(std::is_base_of_v<RadioButtonSettings, St> &&
94 std::is_base_of_v<RadioButtonTheme, St>,
95 "applyStyle() requires RadioButtonSettings and RadioButtonTheme");
96 static_cast<RadioButtonSettings&>(*this) = s;
97 static_cast<RadioButtonTheme&>(*this) = s;
98 layout();
99 }
100
101 // ── Selection ─────────────────────────────────────────────────────────
102
103 void select();
104 void deselect();
105 [[nodiscard]] bool isSelected() const;
106
107 // ── Enabled / disabled ────────────────────────────────────────────────
108
109 void setEnabled(bool enabled);
110 [[nodiscard]] bool isEnabled() const;
111
112 // ── Label ─────────────────────────────────────────────────────────────
113
114 void setLabel(const std::string& label);
115 [[nodiscard]] std::string getLabel() const;
116
117 // ── Positionable ──────────────────────────────────────────────────────
118
119 void setPosition(const sf::Vector2f& position) override;
120 sf::Vector2f getPosition() const override;
122 };
123
124 template<typename MANIFEST>
125 class RadioButtonWith : public RadioButton, public Customizable<MANIFEST>
126 { public: using RadioButton::RadioButton; };
127
128} // namespace ml
129
130#endif // MALENA_RADIOBUTTON_H
static const sf::Font & getDefault()
Return the built-in Arial font.
Base class for all Malena manifests.
Definition Manifest.h:51
void setEnabled(bool enabled)
void applyTheme(const T &t)
Definition RadioButton.h:82
sf::Vector2f getPosition() const override
void applyStyle(const St &s)
Definition RadioButton.h:91
bool isSelected() const
void setPosition(const sf::Vector2f &position) override
bool isEnabled() const
std::string getLabel() const
void applySettings(const S &s)
Definition RadioButton.h:73
void onThemeApplied(const Theme &theme) override
Called by ThemeManager when the active theme changes.
void setLabel(const std::string &label)
RadioButtonManifest::State State
Definition RadioButton.h:50
RadioButton(const std::string &label="", const sf::Font &font=FontManager<>::getDefault())
RadioButtonManifest::Flag Flag
Definition RadioButton.h:49
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
sf::FloatRect getGlobalBounds() const override
RadioButton(const std::string &label="", const sf::Font &font=FontManager<>::getDefault())
Trait that adds selection and deselection callbacks to any Core object.
Definition Selectable.h:62
Component< M, Traits... > ComponentWith
Alias for Component<M, Traits...>.
Definition Component.h:299
@ SELECTED
Component was programmatically selected.
Definition Event.h:60
@ HOVERED
Mouse is currently over this component (set by HoverableDispatcher).
Definition Flag.h:66
#define MALENA_API
Definition Component.h:22
Rect< float > FloatRect
Vector2< float > Vector2f
const sf::Font * font
Layout and behaviour settings for RadioButton.
Color and font tokens for RadioButton.
Universal design token set applied across all Themeable components.
Definition Theme.h:70