Loading...
Searching...
No Matches
ListItem.h
Go to the documentation of this file.
1//
2// ListItem.h
3//
4
5#ifndef MALENA_LISTITEM_H
6#define MALENA_LISTITEM_H
7
8#pragma once
9
12#include <Malena/Core/Core.h>
21#include <functional>
22#include <string>
23#include <type_traits>
24
25namespace ml
26{
27 // ── ListItemManifest ──────────────────────────────────────────────────────
28
30 {
31 public:
32 enum class Flag { DISABLED };
33 enum class State { IDLE, HOVERED, DISABLED };
34 };
35
36 // ── ListItem ──────────────────────────────────────────────────────────────
37
77 class MALENA_API ListItem : public ComponentWith<ListItemManifest>,
78 public ListItemSettings,
79 public ListItemTheme,
80 public Themeable
81 {
82 public:
85
86 private:
87 // ── Slots — external references, never owned ──────────────────────────
88 ml::Core* _start = nullptr;
89 ml::Core* _content = nullptr;
90 ml::Core* _end = nullptr;
91
92 // ── Built-in content ──────────────────────────────────────────────────
93 sf::Text _label;
94 sf::Text _description;
95 bool _hasDescription = false;
96 bool _hasCustomContent = false;
97
98 // ── Layout ────────────────────────────────────────────────────────────
99 sf::RectangleShape _background;
100 sf::Vector2f _position = {0.f, 0.f};
101 float _width = 300.f;
102
103 // ── Callbacks ─────────────────────────────────────────────────────────
104 std::function<void()> _onClickCb;
105
106 // ── Internal ──────────────────────────────────────────────────────────
107 void layout();
108 void applyVisualState();
109 float contentHeight() const;
110
111 protected:
112 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
113 void onThemeApplied(const Theme& theme) override;
114
115 public:
117
118 // ── Apply ─────────────────────────────────────────────────────────────
119
120 template<typename S>
121 void applySettings(const S& s)
122 {
123 static_assert(std::is_base_of_v<ListItemSettings, S>,
124 "applySettings() requires a type derived from ListItemSettings");
125 static_cast<ListItemSettings&>(*this) = s;
126 layout();
127 }
128
129 template<typename T>
130 void applyTheme(const T& t)
131 {
132 static_assert(std::is_base_of_v<ListItemTheme, T>,
133 "applyTheme() requires a type derived from ListItemTheme");
134 static_cast<ListItemTheme&>(*this) = t;
135 applyVisualState();
136 layout();
137 }
138
139 template<typename St>
140 void applyStyle(const St& s)
141 {
142 static_assert(std::is_base_of_v<ListItemSettings, St> &&
143 std::is_base_of_v<ListItemTheme, St>,
144 "applyStyle() requires ListItemSettings and ListItemTheme");
145 static_cast<ListItemSettings&>(*this) = s;
146 static_cast<ListItemTheme&>(*this) = s;
147 applyVisualState();
148 layout();
149 }
150
151 // ── Slots ─────────────────────────────────────────────────────────────
152
159 void setStart(ml::Core& component);
160
167 void setEnd(ml::Core& component);
168
175 void setContent(ml::Core& component);
176
177 // ── Built-in content ──────────────────────────────────────────────────
178
180 void setLabel(const std::string& text);
181 [[nodiscard]] std::string getLabel() const;
182
187 void setDescription(const std::string& text);
188 [[nodiscard]] std::string getDescription() const;
189
190 // ── Click callback ────────────────────────────────────────────────────
191
193 void onClick(std::function<void()> callback);
194
195 // ── State ─────────────────────────────────────────────────────────────
196
197 void setEnabled(bool enabled);
198 [[nodiscard]] bool isEnabled() const;
199
200 // ── Width ─────────────────────────────────────────────────────────────
201
206 void setWidth(float width);
207 [[nodiscard]] float getWidth() const { return _width; }
208
209 // ── Positionable ──────────────────────────────────────────────────────
210
211 void setPosition(const sf::Vector2f& position) override;
212 sf::Vector2f getPosition() const override;
214 };
215
216 // ── ListItemWith ──────────────────────────────────────────────────────────
217
218 template<typename MANIFEST>
219 class ListItemWith : public ListItem, public Customizable<MANIFEST>
220 { public: using ListItem::ListItem; };
221
222} // namespace ml
223
224#endif // MALENA_LISTITEM_H
Virtual base class for all Malena framework objects.
Definition Core.h:69
static const sf::Font & getDefault()
Return the built-in Arial font.
void setEnd(ml::Core &component)
Assign a component to the end (right) slot.
void setStart(ml::Core &component)
Assign a component to the start (left) slot.
bool isEnabled() const
void setWidth(float width)
Set the total row width. Called automatically by List when the item is added.
sf::Vector2f getPosition() const override
std::string getLabel() const
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
void applyTheme(const T &t)
Definition ListItem.h:130
void onThemeApplied(const Theme &theme) override
Called by ThemeManager when the active theme changes.
void setDescription(const std::string &text)
Set an optional secondary description line below the label. Pass an empty string to remove.
ListItem(const sf::Font &font=FontManager<>::getDefault())
void setPosition(const sf::Vector2f &position) override
void onClick(std::function< void()> callback)
Register a callback fired when the row is clicked.
void setLabel(const std::string &text)
Set the primary label text.
void applySettings(const S &s)
Definition ListItem.h:121
void setContent(ml::Core &component)
Replace the built-in label/description with a custom component.
void setEnabled(bool enabled)
sf::FloatRect getGlobalBounds() const override
ListItemManifest::State State
Definition ListItem.h:84
ListItemManifest::Flag Flag
Definition ListItem.h:83
void applyStyle(const St &s)
Definition ListItem.h:140
float getWidth() const
Definition ListItem.h:207
std::string getDescription() const
ListItem(const sf::Font &font=FontManager<>::getDefault())
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
const sf::Font * font
Layout and behaviour settings for ListItem.
Color and font tokens for ListItem.
Universal design token set applied across all Themeable components.
Definition Theme.h:70