Loading...
Searching...
No Matches
Checkbox.h
Go to the documentation of this file.
1//
2// Checkbox.h
3//
4
5#ifndef MALENA_CHECKBOX_H
6#define MALENA_CHECKBOX_H
7
8#pragma once
9
21#include <functional>
22#include <string>
23#include <type_traits>
24
25namespace ml
26{
28 {
29 public:
30 enum class Flag { CHECKED };
31 enum class State { IDLE, HOVERED, CHECKED }; // disabled derived from ml::Flag::ENABLED
32 };
33
44 class MALENA_API Checkbox : public ComponentWith<CheckboxManifest>,
45 public CheckboxSettings,
46 public CheckboxTheme,
47 public Themeable,
48 public Selectable
49 {
50 public:
53
54 private:
56 sf::VertexArray _checkmark;
57 sf::Text _label;
58 sf::Vector2f _position = {0.f, 0.f};
59 std::string _labelStr;
60
61 void layout();
62 void buildCheckmark();
63 void applyVisualState();
64
65 protected:
66 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
67 void onThemeApplied(const Theme& theme) override;
68 void onEnabledChanged(bool enabled) override;
69
70 public:
71 explicit Checkbox(const std::string& label = "",
73
74 // ── Apply ─────────────────────────────────────────────────────────────
75
76 template<typename S>
77 void applySettings(const S& s)
78 {
79 static_assert(std::is_base_of_v<CheckboxSettings, S>,
80 "applySettings() requires a type derived from CheckboxSettings");
81 static_cast<CheckboxSettings&>(*this) = s;
82 layout();
83 }
84
85 template<typename T>
86 void applyTheme(const T& t)
87 {
88 static_assert(std::is_base_of_v<CheckboxTheme, T>,
89 "applyTheme() requires a type derived from CheckboxTheme");
90 static_cast<CheckboxTheme&>(*this) = t;
91 applyVisualState();
92 }
93
94 template<typename St>
95 void applyStyle(const St& s)
96 {
97 static_assert(std::is_base_of_v<CheckboxSettings, St> &&
98 std::is_base_of_v<CheckboxTheme, St>,
99 "applyStyle() requires a type derived from both CheckboxSettings and CheckboxTheme");
100 static_cast<CheckboxSettings&>(*this) = s;
101 static_cast<CheckboxTheme&>(*this) = s;
102 layout();
103 }
104
105 // ── Checked state ─────────────────────────────────────────────────────
106
107 void check();
108 void uncheck();
109 void toggle();
110 void setChecked(bool checked);
111 [[nodiscard]] bool isChecked() const;
112
113 // ── Label ─────────────────────────────────────────────────────────────
114
115 void setLabel(const std::string& label);
116 [[nodiscard]] std::string getLabel() const;
117
118 // ── Positionable ──────────────────────────────────────────────────────
119
120 void setPosition(const sf::Vector2f& position) override;
121 sf::Vector2f getPosition() const override;
123 };
124
125 template<typename MANIFEST>
126 class CheckboxWith : public Checkbox, public Customizable<MANIFEST>
127 { public: using Checkbox::Checkbox; };
128
129} // namespace ml
130
131#endif // MALENA_CHECKBOX_H
sf::Vector2f getPosition() const override
void onEnabledChanged(bool enabled) override
Checkbox(const std::string &label="", const sf::Font &font=FontManager<>::getDefault())
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
bool isChecked() const
void uncheck()
CheckboxManifest::Flag Flag
Definition Checkbox.h:51
sf::FloatRect getGlobalBounds() const override
void applyStyle(const St &s)
Definition Checkbox.h:95
std::string getLabel() const
void setPosition(const sf::Vector2f &position) override
void onThemeApplied(const Theme &theme) override
Called by ThemeManager when the active theme changes.
void setLabel(const std::string &label)
void setChecked(bool checked)
void applySettings(const S &s)
Definition Checkbox.h:77
void applyTheme(const T &t)
Definition Checkbox.h:86
CheckboxManifest::State State
Definition Checkbox.h:52
Checkbox(const std::string &label="", const sf::Font &font=FontManager<>::getDefault())
static const sf::Font & getDefault()
Return the built-in Arial font.
Base class for all Malena manifests.
Definition Manifest.h:51
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:316
@ 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
Layout and behaviour settings for Checkbox.
Color and font tokens for Checkbox.
const sf::Font * font
Universal design token set applied across all Themeable components.
Definition Theme.h:70