Loading...
Searching...
No Matches
CheckboxGroup.h
Go to the documentation of this file.
1//
2// CheckboxGroup.h
3//
4
5#ifndef MALENA_CHECKBOXGROUP_H
6#define MALENA_CHECKBOXGROUP_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
45 class MALENA_API CheckboxGroup : public ComponentWith<CheckboxGroupManifest>,
47 public CheckboxGroupTheme,
48 public Themeable
49 {
50 public:
52
53 private:
54 std::vector<std::unique_ptr<Checkbox>> _owned;
55 std::vector<Checkbox*> _checkboxes;
56
57 sf::Vector2f _position = {0.f, 0.f};
58 ml::Rectangle _background;
59
60 std::function<void(const std::vector<std::string>&)> _onSelectionChanged;
61
62 void layout();
63 void applyStylesToCheckbox(Checkbox& cb);
64 void wire(Checkbox& cb);
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 CheckboxGroup(const CheckboxGroup&) = delete;
75
76 // ── Apply ─────────────────────────────────────────────────────────────
77
78 template<typename S>
79 void applySettings(const S& s)
80 {
81 static_assert(std::is_base_of_v<CheckboxGroupSettings, S>,
82 "applySettings() requires a type derived from CheckboxGroupSettings");
83 static_cast<CheckboxGroupSettings&>(*this) = s;
84 for (auto* cb : _checkboxes) applyStylesToCheckbox(*cb);
85 layout();
86 }
87
88 template<typename T>
89 void applyTheme(const T& t)
90 {
91 static_assert(std::is_base_of_v<CheckboxGroupTheme, T>,
92 "applyTheme() requires a type derived from CheckboxGroupTheme");
93 static_cast<CheckboxGroupTheme&>(*this) = t;
94 for (auto* cb : _checkboxes) applyStylesToCheckbox(*cb);
95 }
96
97 template<typename St>
98 void applyStyle(const St& s)
99 {
100 static_assert(std::is_base_of_v<CheckboxGroupSettings, St> &&
101 std::is_base_of_v<CheckboxGroupTheme, St>,
102 "applyStyle() requires CheckboxGroupSettings and CheckboxGroupTheme");
103 static_cast<CheckboxGroupSettings&>(*this) = s;
104 static_cast<CheckboxGroupTheme&>(*this) = s;
105 for (auto* cb : _checkboxes) applyStylesToCheckbox(*cb);
106 layout();
107 }
108
109 // ── Adding options ────────────────────────────────────────────────────
110
111 void addOption(const std::string& label, bool checked = false);
112 void add(Checkbox& checkbox);
114 [[nodiscard]] std::size_t optionCount() const;
115
116 // ── State ─────────────────────────────────────────────────────────────
117
118 [[nodiscard]] bool isChecked(std::size_t index) const;
119 [[nodiscard]] std::vector<std::string> getCheckedLabels() const;
120 [[nodiscard]] std::vector<std::size_t> getCheckedIndices() const;
121
122 void check(std::size_t index);
123 void uncheck(std::size_t index);
124 void checkAll();
126
127 // ── Callback ─────────────────────────────────────────────────────────
128
130 std::function<void(const std::vector<std::string>&)> callback);
131
132 // ── Option enabled ────────────────────────────────────────────────────
133
134 void setOptionEnabled(std::size_t index, bool enabled);
135
136 // ── Positionable ──────────────────────────────────────────────────────
137
138 void setPosition(const sf::Vector2f& position) override;
139 sf::Vector2f getPosition() const override;
141 };
142
143 template<typename MANIFEST>
144 class CheckboxGroupWith : public CheckboxGroup, public Customizable<MANIFEST>
145 { public: using CheckboxGroup::CheckboxGroup; };
146
147} // namespace ml
148
149#endif // MALENA_CHECKBOXGROUP_H
bool isChecked(std::size_t index) const
void uncheck(std::size_t index)
void onThemeApplied(const Theme &theme) override
Called by ThemeManager when the active theme changes.
std::vector< std::size_t > getCheckedIndices() const
CheckboxGroup(const CheckboxGroup &)=delete
void applyStyle(const St &s)
CheckboxGroup(const sf::Font &font=FontManager<>::getDefault())
void setOptionEnabled(std::size_t index, bool enabled)
void applyTheme(const T &t)
std::vector< std::string > getCheckedLabels() const
void setPosition(const sf::Vector2f &position) override
CheckboxGroupManifest::Flag Flag
void check(std::size_t index)
void onSelectionChanged(std::function< void(const std::vector< std::string > &)> callback)
void addOption(const std::string &label, bool checked=false)
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
sf::FloatRect getGlobalBounds() const override
sf::Vector2f getPosition() const override
CheckboxGroup & operator=(const CheckboxGroup &)=delete
void add(Checkbox &checkbox)
void applySettings(const S &s)
std::size_t optionCount() const
CheckboxGroup(const sf::Font &font=FontManager<>::getDefault())
A toggleable checkbox with a square indicator and text label.
Definition Checkbox.h:49
static const sf::Font & getDefault()
Return the built-in Arial font.
Base class for all Malena manifests.
Definition Manifest.h:51
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
Layout and behaviour settings for CheckboxGroup.
Color tokens for CheckboxGroup.
const sf::Font * font
Universal design token set applied across all Themeable components.
Definition Theme.h:70