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, DISABLED };
31 enum class State { IDLE, HOVERED, CHECKED, DISABLED };
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
69 public:
70 explicit Checkbox(const std::string& label = "",
72
73 // ── Apply ─────────────────────────────────────────────────────────────
74
75 template<typename S>
76 void applySettings(const S& s)
77 {
78 static_assert(std::is_base_of_v<CheckboxSettings, S>,
79 "applySettings() requires a type derived from CheckboxSettings");
80 static_cast<CheckboxSettings&>(*this) = s;
81 layout();
82 }
83
84 template<typename T>
85 void applyTheme(const T& t)
86 {
87 static_assert(std::is_base_of_v<CheckboxTheme, T>,
88 "applyTheme() requires a type derived from CheckboxTheme");
89 static_cast<CheckboxTheme&>(*this) = t;
90 applyVisualState();
91 }
92
93 template<typename St>
94 void applyStyle(const St& s)
95 {
96 static_assert(std::is_base_of_v<CheckboxSettings, St> &&
97 std::is_base_of_v<CheckboxTheme, St>,
98 "applyStyle() requires a type derived from both CheckboxSettings and CheckboxTheme");
99 static_cast<CheckboxSettings&>(*this) = s;
100 static_cast<CheckboxTheme&>(*this) = s;
101 layout();
102 }
103
104 // ── Checked state ─────────────────────────────────────────────────────
105
106 void check();
107 void uncheck();
108 void toggle();
109 [[nodiscard]] bool isChecked() const;
110
111 // ── Enabled / disabled ────────────────────────────────────────────────
112
113 void setEnabled(bool enabled);
114 [[nodiscard]] bool isEnabled() const;
115
116 // ── Label ─────────────────────────────────────────────────────────────
117
118 void setLabel(const std::string& label);
119 [[nodiscard]] std::string getLabel() const;
120
121 // ── Positionable ──────────────────────────────────────────────────────
122
123 void setPosition(const sf::Vector2f& position) override;
124 sf::Vector2f getPosition() const override;
126 };
127
128 template<typename MANIFEST>
129 class CheckboxWith : public Checkbox, public Customizable<MANIFEST>
130 { public: using Checkbox::Checkbox; };
131
132} // namespace ml
133
134#endif // MALENA_CHECKBOX_H
sf::Vector2f getPosition() const override
Checkbox(const std::string &label="", const sf::Font &font=FontManager<>::getDefault())
void setEnabled(bool enabled)
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
bool isChecked() const
void uncheck()
bool isEnabled() const
CheckboxManifest::Flag Flag
Definition Checkbox.h:51
sf::FloatRect getGlobalBounds() const override
void applyStyle(const St &s)
Definition Checkbox.h:94
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 applySettings(const S &s)
Definition Checkbox.h:76
void applyTheme(const T &t)
Definition Checkbox.h:85
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:299
@ 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