Loading...
Searching...
No Matches
PillToggle.h
Go to the documentation of this file.
1//
2// PillToggle.h
3//
4
5#ifndef MALENA_PILLTOGGLE_H
6#define MALENA_PILLTOGGLE_H
7
8#pragma once
9
18#include <SFML/System/Clock.hpp>
19#include <functional>
20#include <type_traits>
21
22namespace ml
23{
25 {
26 public:
27 enum class Flag { ON, DISABLED };
28 enum class State { IDLE, HOVERED, ON, DISABLED };
29 };
30
66 class MALENA_API PillToggle : public ComponentWith<PillToggleManifest>,
67 public PillSettings,
68 public PillTheme,
69 public Themeable
70 {
71 public:
74
75 private:
76 sf::CircleShape _thumb;
77 float _thumbX = 0.f;
78 float _thumbTarget = 0.f;
79 mutable sf::Clock _animClock;
80 sf::Vector2f _position = {0.f, 0.f};
81
82 std::function<void(bool)> _onToggled;
83
84 void syncFromSettings();
85 void updateThumbPosition();
86 void drawCapsule(sf::RenderTarget&, const sf::RenderStates&,
87 sf::Vector2f, sf::Vector2f, float, sf::Color) const;
88
89 protected:
90 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
91 void onThemeApplied(const Theme& theme) override;
92
93 public:
94 explicit PillToggle();
95
96 // ── Apply ─────────────────────────────────────────────────────────────
97
99 template<typename S>
100 void applySettings(const S& s)
101 {
102 static_assert(std::is_base_of_v<PillSettings, S>,
103 "applySettings() requires a type derived from PillSettings");
104 static_cast<PillSettings&>(*this) = s;
105 syncFromSettings();
106 }
107
109 template<typename T>
110 void applyTheme(const T& t)
111 {
112 static_assert(std::is_base_of_v<PillTheme, T>,
113 "applyTheme() requires a type derived from PillTheme");
114 static_cast<PillTheme&>(*this) = t;
115 syncFromSettings();
116 }
117
119 template<typename St>
120 void applyStyle(const St& s)
121 {
122 static_assert(std::is_base_of_v<PillSettings, St> &&
123 std::is_base_of_v<PillTheme, St>,
124 "applyStyle() requires a type derived from both PillSettings and PillTheme");
125 static_cast<PillSettings&>(*this) = s;
126 static_cast<PillTheme&>(*this) = s;
127 syncFromSettings();
128 }
129
130 // ── State ─────────────────────────────────────────────────────────────
131
132 void setOn(bool on);
133 void toggle();
134 [[nodiscard]] bool isOn() const;
135 void setEnabled(bool enabled);
136 [[nodiscard]] bool isEnabled() const;
137
138 // ── Convenience ───────────────────────────────────────────────────────
139
141 void setCharacterSize(unsigned int size) { setFontSize(size); }
142
144 [[nodiscard]] unsigned int getCharacterSize() const { return getFontSize(); }
145
146 // ── Callback ─────────────────────────────────────────────────────────
147
148 void onToggled(std::function<void(bool)> callback);
149
150 // ── Positionable ──────────────────────────────────────────────────────
151
152 void setPosition(const sf::Vector2f& position) override;
153 sf::Vector2f getPosition() const override;
155 };
156
157 template<typename MANIFEST>
158 class PillToggleWith : public PillToggle, public Customizable<MANIFEST>
159 { public: using PillToggle::PillToggle; };
160
161} // namespace ml
162
163#endif // MALENA_PILLTOGGLE_H
Base class for all Malena manifests.
Definition Manifest.h:51
unsigned int getCharacterSize() const
Alias for getFontSize() — matches SFML naming convention.
Definition PillToggle.h:144
void setOn(bool on)
PillToggleManifest::Flag Flag
Definition PillToggle.h:72
void setPosition(const sf::Vector2f &position) override
void applySettings(const S &s)
Apply layout/behaviour settings. Theme layer is unaffected.
Definition PillToggle.h:100
sf::FloatRect getGlobalBounds() const override
void setEnabled(bool enabled)
void onThemeApplied(const Theme &theme) override
Called by ThemeManager when the active theme changes.
void onToggled(std::function< void(bool)> callback)
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
sf::Vector2f getPosition() const override
void applyStyle(const St &s)
Apply settings and theme in one call.
Definition PillToggle.h:120
PillToggleManifest::State State
Definition PillToggle.h:73
void applyTheme(const T &t)
Apply color/font theme. Settings layer is unaffected.
Definition PillToggle.h:110
void setCharacterSize(unsigned int size)
Alias for setFontSize() — matches SFML naming convention.
Definition PillToggle.h:141
bool isOn() const
bool isEnabled() const
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
void setFontSize(unsigned int s)
unsigned int getFontSize() const
Layout and behaviour settings for PillToggle.
Theme tokens for the PillToggle (iOS-style oval switch).
Definition PillTheme.h:48
Universal design token set applied across all Themeable components.
Definition Theme.h:70