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 {}; // no per-instance flags; disabled derives from ml::Flag::ENABLED
33 enum class State { IDLE, HOVERED };
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 bool _selected = false;
107
108 // ── Internal ──────────────────────────────────────────────────────────
109 void layout();
110 void applyVisualState();
111 float contentHeight() const;
112
113 protected:
114 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
115 void onThemeApplied(const Theme& theme) override;
116 void onEnabledChanged(bool enabled) override;
117
118 public:
120
121 // ── Apply ─────────────────────────────────────────────────────────────
122
123 template<typename S>
124 void applySettings(const S& s)
125 {
126 static_assert(std::is_base_of_v<ListItemSettings, S>,
127 "applySettings() requires a type derived from ListItemSettings");
128 static_cast<ListItemSettings&>(*this) = s;
129 layout();
130 }
131
132 template<typename T>
133 void applyTheme(const T& t)
134 {
135 static_assert(std::is_base_of_v<ListItemTheme, T>,
136 "applyTheme() requires a type derived from ListItemTheme");
137 static_cast<ListItemTheme&>(*this) = t;
138 applyVisualState();
139 layout();
140 }
141
142 template<typename St>
143 void applyStyle(const St& s)
144 {
145 static_assert(std::is_base_of_v<ListItemSettings, St> &&
146 std::is_base_of_v<ListItemTheme, St>,
147 "applyStyle() requires ListItemSettings and ListItemTheme");
148 static_cast<ListItemSettings&>(*this) = s;
149 static_cast<ListItemTheme&>(*this) = s;
150 applyVisualState();
151 layout();
152 }
153
154 // ── Slots ─────────────────────────────────────────────────────────────
155
162 void setStart(ml::Core& component);
163
170 void setEnd(ml::Core& component);
171
178 void setContent(ml::Core& component);
179
180 // ── Built-in content ──────────────────────────────────────────────────
181
183 void setLabel(const std::string& text);
184 [[nodiscard]] std::string getLabel() const;
185
190 void setDescription(const std::string& text);
191 [[nodiscard]] std::string getDescription() const;
192
193 // ── Click callback ────────────────────────────────────────────────────
194
196 void onClick(std::function<void()> callback);
197
199 [[nodiscard]] bool isEndHit(const sf::Vector2f& point) const;
200
201 // ── State ─────────────────────────────────────────────────────────────
202
203
204 void setSelected(bool selected);
205 [[nodiscard]] bool isSelected() const { return _selected; }
206
207 // ── Width ─────────────────────────────────────────────────────────────
208
213 void setWidth(float width);
214 [[nodiscard]] float getWidth() const { return _width; }
215
216 // ── Positionable ──────────────────────────────────────────────────────
217
218 void setPosition(const sf::Vector2f& position) override;
219 sf::Vector2f getPosition() const override;
221 };
222
223 // ── ListItemWith ──────────────────────────────────────────────────────────
224
225 template<typename MANIFEST>
226 class ListItemWith : public ListItem, public Customizable<MANIFEST>
227 { public: using ListItem::ListItem; };
228
229} // namespace ml
230
231#endif // MALENA_LISTITEM_H
Virtual base class for all Malena framework objects.
Definition Core.h:97
static const sf::Font & getDefault()
Return the built-in Arial font.
bool isEndHit(const sf::Vector2f &point) const
Returns true if point is within the end slot component's bounds.
void onEnabledChanged(bool enabled) override
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.
void setSelected(bool selected)
void setWidth(float width)
Set the total row width. Called automatically by List when the item is added.
bool isSelected() const
Definition ListItem.h:205
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:133
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:124
void setContent(ml::Core &component)
Replace the built-in label/description with a custom component.
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:143
float getWidth() const
Definition ListItem.h:214
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: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
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