Loading...
Searching...
No Matches
ToggleGroup.h
Go to the documentation of this file.
1//
2// ToggleGroup.h
3//
4
5#ifndef MALENA_TOGGLEGROUP_H
6#define MALENA_TOGGLEGROUP_H
7
8#pragma once
9
19#include <functional>
20#include <string>
21#include <vector>
22#include <memory>
23
24namespace ml
25{
27 {
28 public:
29 enum class Flag {};
30 enum class State { IDLE };
31 };
32
60 class MALENA_API ToggleGroup : public ComponentWith<ToggleGroupManifest>
61 {
62 public:
65
66 private:
67 enum class ItemType { PILL, SEGMENT, BUTTON };
68
69 struct Item
70 {
71 ItemType type;
72 std::string key;
73 PillToggle* pill = nullptr;
74 SegmentToggle* segment = nullptr;
75 ButtonToggle* button = nullptr;
76 bool owned = false;
77
78 bool isOn() const;
79 void setOn(bool on);
80 sf::Vector2f pos() const;
81 void setPos(const sf::Vector2f& p);
82 sf::FloatRect bounds() const;
83 };
84
85 // unique_ptr<Item> keeps Items at stable heap addresses
86 std::vector<std::unique_ptr<Item>> _items;
87 std::vector<std::unique_ptr<PillToggle>> _ownedPills;
88 std::vector<std::unique_ptr<SegmentToggle>> _ownedSegments;
89 std::vector<std::unique_ptr<ButtonToggle>> _ownedButtons;
90
91 sf::Vector2f _position = {0.f, 0.f};
92 float _spacing = 12.f;
93
94 // Default styles applied to newly created owned toggles
95 PillSettings _defaultPillSettings;
96 PillTheme _defaultPillTheme;
97 SegmentSettings _defaultSegmentSettings;
98 SegmentTheme _defaultSegmentTheme;
99 ButtonSettings _defaultButtonSettings;
100 ButtonTheme _defaultButtonTheme;
101
102 std::function<void(const std::string&, bool)> _onAnyToggled;
103
104 void relayout();
105 void attach(Item& item);
106
107 protected:
108 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
109
110 public:
111 explicit ToggleGroup();
113
114 ToggleGroup(const ToggleGroup&) = delete;
116
117 // ── Adding owned toggles ──────────────────────────────────────────────
118
125 PillToggle& addToggle(const std::string& label,
126 const std::string& key,
127 bool on = false);
128
133 SegmentToggle& addSegmentToggle(const std::string& offLabel,
134 const std::string& onLabel,
135 const std::string& key,
136 bool on = false);
137
142 ButtonToggle& addButtonToggle(const std::string& label,
143 const std::string& key,
144 bool on = false);
145
146 // ── Adding external toggles ───────────────────────────────────────────
147
148 void add(PillToggle& toggle, const std::string& key);
149 void add(SegmentToggle& toggle, const std::string& key);
150 void add(ButtonToggle& toggle, const std::string& key);
151
153
154 // ── State ─────────────────────────────────────────────────────────────
155
156 [[nodiscard]] bool isOn(const std::string& key) const;
157 void setOn(const std::string& key, bool on);
158 void setAllOn();
159 void setAllOff();
160 [[nodiscard]] std::vector<std::string> getOnKeys() const;
161
162 // ── Callbacks ─────────────────────────────────────────────────────────
163
164 void onAnyToggled(std::function<void(const std::string&, bool)> callback);
165
166 // ── Default styles ────────────────────────────────────────────────────
167
172
177
182
183 // ── Bulk apply to owned toggles ───────────────────────────────────────
184
188
192
196
197 // ── Convenience ───────────────────────────────────────────────────────
198
200 void setFont(const sf::Font& font);
201 void setFont(const sf::Font&&) = delete;
202
204 void setCharacterSize(unsigned int size);
205
206 void setSpacing(float spacing);
207
208 // ── Positionable ──────────────────────────────────────────────────────
209
210 void setPosition(const sf::Vector2f& position) override;
211 sf::Vector2f getPosition() const override;
213 };
214
215 template<typename MANIFEST>
216 class ToggleGroupWith : public ToggleGroup, public Customizable<MANIFEST>
217 { public: using ToggleGroup::ToggleGroup; };
218
219} // namespace ml
220
221#endif // MALENA_TOGGLEGROUP_H
Rectangular button that changes color and label on toggle.
Base class for all Malena manifests.
Definition Manifest.h:51
iOS-style oval pill switch with animated sliding thumb.
Definition PillToggle.h:70
Two-option segmented selector with animated sliding pill.
void applyStyleToAll(const ButtonStyle &s)
sf::FloatRect getGlobalBounds() const override
void setDefaultStyle(const ButtonStyle &s)
void setFont(const sf::Font &&)=delete
SegmentToggle & addSegmentToggle(const std::string &offLabel, const std::string &onLabel, const std::string &key, bool on=false)
Create and add a new SegmentToggle.
void onAnyToggled(std::function< void(const std::string &, bool)> callback)
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
void setFont(const sf::Font &font)
Set font on the default pill theme and all owned pills.
void setOn(const std::string &key, bool on)
void applyThemeToAll(const ButtonTheme &t)
void setPosition(const sf::Vector2f &position) override
void setCharacterSize(unsigned int size)
Set character size on the default pill theme and all owned pills.
void setSpacing(float spacing)
void add(PillToggle &toggle, const std::string &key)
void setDefaultStyle(const PillStyle &s)
Set default settings and theme together.
void add(SegmentToggle &toggle, const std::string &key)
ToggleGroup(const ToggleGroup &)=delete
void setDefaultTheme(const SegmentTheme &t)
void applySettingsToAll(const SegmentSettings &s)
void setDefaultTheme(const PillTheme &t)
Set the default theme applied to new owned PillToggles.
void applyStyleToAll(const SegmentStyle &s)
void setDefaultSettings(const SegmentSettings &s)
sf::Vector2f getPosition() const override
ToggleGroupManifest::Flag Flag
Definition ToggleGroup.h:63
bool isOn(const std::string &key) const
void applySettingsToAll(const PillSettings &s)
void applySettingsToAll(const ButtonSettings &s)
ToggleGroupManifest::State State
Definition ToggleGroup.h:64
ToggleGroup & operator=(const ToggleGroup &)=delete
PillToggle & addToggle(const std::string &label, const std::string &key, bool on=false)
Create and add a new PillToggle with a right label.
void applyThemeToAll(const PillTheme &t)
void add(ButtonToggle &toggle, const std::string &key)
void setDefaultTheme(const ButtonTheme &t)
void applyThemeToAll(const SegmentTheme &t)
void setDefaultSettings(const PillSettings &s)
Set the default settings applied to new owned PillToggles.
void setDefaultStyle(const SegmentStyle &s)
void setDefaultSettings(const ButtonSettings &s)
void applyStyleToAll(const PillStyle &s)
ButtonToggle & addButtonToggle(const std::string &label, const std::string &key, bool on=false)
Create and add a new ButtonToggle.
std::vector< std::string > getOnKeys() const
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
Layout and behaviour settings for ButtonToggle.
Complete style descriptor for ButtonToggle.
Definition ButtonStyle.h:25
Theme tokens for the ButtonToggle (rectangular toggle button).
Definition ButtonTheme.h:28
Layout and behaviour settings for PillToggle.
Complete style descriptor for PillToggle.
Definition PillStyle.h:59
Theme tokens for the PillToggle (iOS-style oval switch).
Definition PillTheme.h:48
Layout and behaviour settings for SegmentToggle.
Complete style descriptor for SegmentToggle.
Theme tokens for the SegmentToggle (Hotels/Apartments style).