Loading...
Searching...
No Matches
RadioGroup.h
Go to the documentation of this file.
1//
2// RadioGroup.h
3//
4
5#ifndef MALENA_RADIOGROUP_H
6#define MALENA_RADIOGROUP_H
7
8#pragma once
9
20#include <functional>
21#include <string>
22#include <vector>
23#include <memory>
24#include <type_traits>
25
26namespace ml
27{
29 {
30 public:
31 enum class Flag { SHOW_BACKGROUND };
32 };
33
43 class MALENA_API RadioGroup : public ComponentWith<RadioGroupManifest>,
44 public RadioGroupSettings,
45 public RadioGroupTheme,
46 public Themeable
47 {
48 public:
50
51 private:
52 std::vector<std::unique_ptr<RadioButton>> _owned;
53 std::vector<RadioButton*> _buttons;
54 RadioButton* _selected = nullptr;
55
56 sf::Vector2f _position = {0.f, 0.f};
57 ml::Rectangle _background;
58
59 std::function<void(const std::string&, std::size_t)> _onSelectionChanged;
60
61 void layout();
62 void applyStylesToButton(RadioButton& btn);
63 void wire(RadioButton& btn);
64 void handleClick(RadioButton& clicked);
65
66 protected:
67 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
68 void onThemeApplied(const Theme& theme) override;
69
70 public:
72
73 RadioGroup(const RadioGroup&) = delete;
74 RadioGroup& operator=(const RadioGroup&) = delete;
75
76 // ── Apply ─────────────────────────────────────────────────────────────
77
78 template<typename S>
79 void applySettings(const S& s)
80 {
81 static_assert(std::is_base_of_v<RadioGroupSettings, S>,
82 "applySettings() requires a type derived from RadioGroupSettings");
83 static_cast<RadioGroupSettings&>(*this) = s;
84 for (auto* btn : _buttons) applyStylesToButton(*btn);
85 layout();
86 }
87
88 template<typename T>
89 void applyTheme(const T& t)
90 {
91 static_assert(std::is_base_of_v<RadioGroupTheme, T>,
92 "applyTheme() requires a type derived from RadioGroupTheme");
93 static_cast<RadioGroupTheme&>(*this) = t;
94 for (auto* btn : _buttons) applyStylesToButton(*btn);
95 }
96
97 template<typename St>
98 void applyStyle(const St& s)
99 {
100 static_assert(std::is_base_of_v<RadioGroupSettings, St> &&
101 std::is_base_of_v<RadioGroupTheme, St>,
102 "applyStyle() requires RadioGroupSettings and RadioGroupTheme");
103 static_cast<RadioGroupSettings&>(*this) = s;
104 static_cast<RadioGroupTheme&>(*this) = s;
105 for (auto* btn : _buttons) applyStylesToButton(*btn);
106 layout();
107 }
108
109 // ── Adding options ────────────────────────────────────────────────────
110
111 void addOption(const std::string& label);
112 void add(RadioButton& button);
114 [[nodiscard]] std::size_t optionCount() const;
115
116 // ── Selection ─────────────────────────────────────────────────────────
117
118 void select(std::size_t index);
120 [[nodiscard]] std::string getSelectedLabel() const;
121 [[nodiscard]] std::size_t getSelectedIndex() const;
122 [[nodiscard]] RadioButton* getSelected() const;
123
124 // ── Callback ─────────────────────────────────────────────────────────
125
127 std::function<void(const std::string&, std::size_t)> callback);
128
129 // ── Option enabled ────────────────────────────────────────────────────
130
131 void setOptionEnabled(std::size_t index, bool enabled);
132
133 // ── Positionable ──────────────────────────────────────────────────────
134
135 void setPosition(const sf::Vector2f& position) override;
136 sf::Vector2f getPosition() const override;
138 };
139
140 template<typename MANIFEST>
141 class RadioGroupWith : public RadioGroup, public Customizable<MANIFEST>
142 { public: using RadioGroup::RadioGroup; };
143
144} // namespace ml
145
146#endif // MALENA_RADIOGROUP_H
static const sf::Font & getDefault()
Return the built-in Arial font.
Base class for all Malena manifests.
Definition Manifest.h:51
A single radio button option with circular indicator and label.
Definition RadioButton.h:47
void clearOptions()
void setPosition(const sf::Vector2f &position) override
void applySettings(const S &s)
Definition RadioGroup.h:79
void selectFirst()
std::size_t getSelectedIndex() const
void applyTheme(const T &t)
Definition RadioGroup.h:89
void addOption(const std::string &label)
std::string getSelectedLabel() const
void add(RadioButton &button)
void applyStyle(const St &s)
Definition RadioGroup.h:98
RadioGroup & operator=(const RadioGroup &)=delete
sf::Vector2f getPosition() const override
void onThemeApplied(const Theme &theme) override
Called by ThemeManager when the active theme changes.
RadioGroup(const sf::Font &font=FontManager<>::getDefault())
void onSelectionChanged(std::function< void(const std::string &, std::size_t)> callback)
std::size_t optionCount() const
void select(std::size_t index)
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
RadioButton * getSelected() const
sf::FloatRect getGlobalBounds() const override
void setOptionEnabled(std::size_t index, bool enabled)
RadioGroup(const RadioGroup &)=delete
RadioGroupManifest::Flag Flag
Definition RadioGroup.h:49
RadioGroup(const sf::Font &font=FontManager<>::getDefault())
A framework-integrated rectangle with optional rounded corners.
Definition Rectangle.h:48
Component< M, Traits... > ComponentWith
Alias for Component<M, Traits...>.
Definition Component.h:299
#define MALENA_API
Definition Component.h:22
Rect< float > FloatRect
Vector2< float > Vector2f
const sf::Font * font
Layout and behaviour settings for RadioGroup.
Color tokens for RadioGroup.
Universal design token set applied across all Themeable components.
Definition Theme.h:70