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