Loading...
Searching...
No Matches
SideMenu.h
Go to the documentation of this file.
1//
2// SideMenu.h
3//
4
5#ifndef MALENA_SIDEMENU_H
6#define MALENA_SIDEMENU_H
7
8#pragma once
9
12#include <Malena/Core/Core.h>
21#include <functional>
22#include <type_traits>
23
24namespace ml
25{
27 {
28 public:
29 enum class Flag { OPEN };
30 enum class State { CLOSED, OPEN, ANIMATING };
31 };
32
70 class MALENA_API SideMenu : public ComponentWith<SideMenuManifest>,
71 public SideMenuSettings,
72 public SideMenuTheme,
73 public Themeable
74 {
75 public:
80
81 private:
82 // ── Content ───────────────────────────────────────────────────────────
83 ml::List _defaultList;
84 ml::Core* _content;
85
86 // ── Visual ────────────────────────────────────────────────────────────
87 sf::RectangleShape _panel;
88 sf::RectangleShape _backdrop;
89 sf::Vector2f _hamburgerPos = {8.f, 8.f};
90
91 // ── Animation ─────────────────────────────────────────────────────────
92 float _currentX = 0.f;
93 float _targetX = 0.f;
94 bool _animating = false;
95
96 // ── Callbacks ─────────────────────────────────────────────────────────
97 std::function<void()> _onOpenCb;
98 std::function<void()> _onCloseCb;
99
100 // ── Internal ──────────────────────────────────────────────────────────
101 float closedX() const;
102 float openX() const;
103 float resolvedHeight() const;
104 void updatePanelTransform();
105 void drawHamburger(sf::RenderTarget& target,
106 const sf::RenderStates& states) const;
107
108 protected:
109 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
110 void onThemeApplied(const Theme& theme) override;
111
112 public:
113 explicit SideMenu(Mode mode = Mode::OVERLAY,
114 Anchor anchor = Anchor::LEFT,
116
117 // ── Apply ─────────────────────────────────────────────────────────────
118
119 template<typename S>
120 void applySettings(const S& s)
121 {
122 static_assert(std::is_base_of_v<SideMenuSettings, S>,
123 "applySettings() requires a type derived from SideMenuSettings");
124 static_cast<SideMenuSettings&>(*this) = s;
125 updatePanelTransform();
126 }
127
128 template<typename T>
129 void applyTheme(const T& t)
130 {
131 static_assert(std::is_base_of_v<SideMenuTheme, T>,
132 "applyTheme() requires a type derived from SideMenuTheme");
133 static_cast<SideMenuTheme&>(*this) = t;
134 _panel.setFillColor(panelBg);
135 _backdrop.setFillColor(backdropColor);
136 }
137
138 template<typename St>
139 void applyStyle(const St& s)
140 {
141 static_assert(std::is_base_of_v<SideMenuSettings, St> &&
142 std::is_base_of_v<SideMenuTheme, St>,
143 "applyStyle() requires SideMenuSettings and SideMenuTheme");
144 static_cast<SideMenuSettings&>(*this) = s;
145 static_cast<SideMenuTheme&>(*this) = s;
146 _panel.setFillColor(panelBg);
147 _backdrop.setFillColor(backdropColor);
148 updatePanelTransform();
149 }
150
151 // ── Content ───────────────────────────────────────────────────────────
152
160
168 void setContent(ml::Core& component);
169
170 // ── Control ───────────────────────────────────────────────────────────
171
172 void open();
173 void close();
174 void toggle();
175 [[nodiscard]] bool isOpen() const;
176
177 // ── Callbacks ─────────────────────────────────────────────────────────
178
180 void onOpen(std::function<void()> cb);
182 void onClose(std::function<void()> cb);
183
184 // ── Sizing / position ─────────────────────────────────────────────────
185
187 [[nodiscard]] float getPanelWidth() const { return panelWidth; }
188
189 void setPosition(const sf::Vector2f& position) override;
190 sf::Vector2f getPosition() const override;
192 };
193
194 template<typename MANIFEST>
195 class SideMenuWith : public SideMenu, public Customizable<MANIFEST>
196 { public: using SideMenu::SideMenu; };
197
198} // namespace ml
199#endif // MALENA_SIDEMENU_H
Virtual base class for all Malena framework objects.
Definition Core.h:69
static const sf::Font & getDefault()
Return the built-in Arial font.
A vertically stacked list of rows with optional dividers and background.
Definition List.h:104
Base class for all Malena manifests.
Definition Manifest.h:51
SideMenuSettings::Anchor Anchor
Definition SideMenu.h:79
void applyStyle(const St &s)
Definition SideMenu.h:139
SideMenu(Mode mode=Mode::OVERLAY, Anchor anchor=Anchor::LEFT, const sf::Font &font=FontManager<>::getDefault())
void onThemeApplied(const Theme &theme) override
Called by ThemeManager when the active theme changes.
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
float getPanelWidth() const
Definition SideMenu.h:187
bool isOpen() const
ml::List & getList()
Return the internal List for configuration.
void setHamburgerPosition(const sf::Vector2f &pos)
void setPosition(const sf::Vector2f &position) override
void onClose(std::function< void()> cb)
Called when the panel finishes closing.
SideMenuManifest::State State
Definition SideMenu.h:77
void onOpen(std::function< void()> cb)
Called when the panel finishes opening.
sf::FloatRect getGlobalBounds() const override
void applySettings(const S &s)
Definition SideMenu.h:120
SideMenuManifest::Flag Flag
Definition SideMenu.h:76
void setContent(ml::Core &component)
Replace the internal list with any component.
SideMenuSettings::Mode Mode
Definition SideMenu.h:78
void applyTheme(const T &t)
Definition SideMenu.h:129
sf::Vector2f getPosition() const override
SideMenu(Mode mode=Mode::OVERLAY, Anchor anchor=Anchor::LEFT, const sf::Font &font=FontManager<>::getDefault())
Component< M, Traits... > ComponentWith
Alias for Component<M, Traits...>.
Definition Component.h:299
#define MALENA_API
Definition Component.h:22
Rect< float > FloatRect
Vector2< float > Vector2f
const sf::Font * font
void setFillColor(const sf::Color &c)
sf::Color backdropColor
Universal design token set applied across all Themeable components.
Definition Theme.h:70