Loading...
Searching...
No Matches
SegmentToggle.h
Go to the documentation of this file.
1//
2// SegmentToggle.h
3//
4
5#ifndef MALENA_SEGMENTTOGGLE_H
6#define MALENA_SEGMENTTOGGLE_H
7
8#pragma once
9
17#include <SFML/System/Clock.hpp>
18#include <functional>
19#include <type_traits>
20
21namespace ml
22{
24 {
25 public:
26 enum class Flag { ON, DISABLED };
27 enum class State { IDLE, HOVERED, ON, DISABLED };
28 };
29
50 class MALENA_API SegmentToggle : public ComponentWith<SegmentToggleManifest>,
51 public SegmentSettings,
52 public SegmentTheme,
53 public Themeable
54 {
55 public:
58
59 private:
60 sf::Vector2f _position = {0.f, 0.f};
61 float _thumbX = 0.f;
62 float _thumbTarget = 0.f;
63 mutable sf::Clock _animClock;
64 std::function<void(bool)> _onToggled;
65
66 void syncFromSettings();
67 void drawCapsule(sf::RenderTarget&, const sf::RenderStates&,
68 sf::Vector2f, sf::Vector2f, float, sf::Color) const;
69
70 protected:
71 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
72 void onThemeApplied(const Theme& theme) override;
73
74 public:
75 explicit SegmentToggle();
76
77 template<typename S>
78 void applySettings(const S& s)
79 {
80 static_assert(std::is_base_of_v<SegmentSettings, S>,
81 "applySettings() requires a type derived from SegmentSettings");
82 static_cast<SegmentSettings&>(*this) = s;
83 syncFromSettings();
84 }
85
86 template<typename T>
87 void applyTheme(const T& t)
88 {
89 static_assert(std::is_base_of_v<SegmentTheme, T>,
90 "applyTheme() requires a type derived from SegmentTheme");
91 static_cast<SegmentTheme&>(*this) = t;
92 syncFromSettings();
93 }
94
95 template<typename St>
96 void applyStyle(const St& s)
97 {
98 static_assert(std::is_base_of_v<SegmentSettings, St> &&
99 std::is_base_of_v<SegmentTheme, St>,
100 "applyStyle() requires a type derived from both SegmentSettings and SegmentTheme");
101 static_cast<SegmentSettings&>(*this) = s;
102 static_cast<SegmentTheme&>(*this) = s;
103 syncFromSettings();
104 }
105
106 void setOn(bool on);
107 void toggle();
108 [[nodiscard]] bool isOn() const;
109 void setEnabled(bool enabled);
110 [[nodiscard]] bool isEnabled() const;
111
112 void onToggled(std::function<void(bool)> callback);
113
114 void setPosition(const sf::Vector2f& position) override;
115 sf::Vector2f getPosition() const override;
117 };
118
119 template<typename MANIFEST>
120 class SegmentToggleWith : public SegmentToggle, public Customizable<MANIFEST>
121 { public: using SegmentToggle::SegmentToggle; };
122
123} // namespace ml
124
125#endif // MALENA_SEGMENTTOGGLE_H
Base class for all Malena manifests.
Definition Manifest.h:51
bool isOn() const
sf::Vector2f getPosition() const override
sf::FloatRect getGlobalBounds() const override
SegmentToggleManifest::State State
bool isEnabled() const
void setOn(bool on)
void applyTheme(const T &t)
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
void setPosition(const sf::Vector2f &position) override
void onToggled(std::function< void(bool)> callback)
void applySettings(const S &s)
void setEnabled(bool enabled)
SegmentToggleManifest::Flag Flag
void onThemeApplied(const Theme &theme) override
Called by ThemeManager when the active theme changes.
void applyStyle(const St &s)
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 SegmentToggle.
Theme tokens for the SegmentToggle (Hotels/Apartments style).
Universal design token set applied across all Themeable components.
Definition Theme.h:70