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 };
28 enum class State { IDLE, HOVERED, ON }; // disabled derives from ml::Flag::ENABLED
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 void onEnabledChanged(bool enabled) override;
93
94 public:
95 explicit PillToggle();
96
97 // ── Apply ─────────────────────────────────────────────────────────────
98
100 template<typename S>
101 void applySettings(const S& s)
102 {
103 static_assert(std::is_base_of_v<PillSettings, S>,
104 "applySettings() requires a type derived from PillSettings");
105 static_cast<PillSettings&>(*this) = s;
106 syncFromSettings();
107 }
108
110 template<typename T>
111 void applyTheme(const T& t)
112 {
113 static_assert(std::is_base_of_v<PillTheme, T>,
114 "applyTheme() requires a type derived from PillTheme");
115 static_cast<PillTheme&>(*this) = t;
116 syncFromSettings();
117 }
118
120 template<typename St>
121 void applyStyle(const St& s)
122 {
123 static_assert(std::is_base_of_v<PillSettings, St> &&
124 std::is_base_of_v<PillTheme, St>,
125 "applyStyle() requires a type derived from both PillSettings and PillTheme");
126 static_cast<PillSettings&>(*this) = s;
127 static_cast<PillTheme&>(*this) = s;
128 syncFromSettings();
129 }
130
131 // ── State ─────────────────────────────────────────────────────────────
132
133 void setOn(bool on);
134 void toggle();
135 [[nodiscard]] bool isOn() const;
136
137 // ── Convenience ───────────────────────────────────────────────────────
138
140 void setCharacterSize(unsigned int size) { setFontSize(size); }
141
143 [[nodiscard]] unsigned int getCharacterSize() const { return getFontSize(); }
144
145 // ── Callback ─────────────────────────────────────────────────────────
146
147 void onToggled(std::function<void(bool)> callback);
148
149 // ── Positionable ──────────────────────────────────────────────────────
150
151 void setPosition(const sf::Vector2f& position) override;
152 sf::Vector2f getPosition() const override;
154 };
155
156 template<typename MANIFEST>
157 class PillToggleWith : public PillToggle, public Customizable<MANIFEST>
158 { public: using PillToggle::PillToggle; };
159
160} // namespace ml
161
162#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:143
void onEnabledChanged(bool enabled) override
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:101
sf::FloatRect getGlobalBounds() const override
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:121
PillToggleManifest::State State
Definition PillToggle.h:73
void applyTheme(const T &t)
Apply color/font theme. Settings layer is unaffected.
Definition PillToggle.h:111
void setCharacterSize(unsigned int size)
Alias for setFontSize() — matches SFML naming convention.
Definition PillToggle.h:140
bool isOn() const
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
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