Loading...
Searching...
No Matches
List.h
Go to the documentation of this file.
1//
2// List.h
3//
4
5#ifndef MALENA_LIST_H
6#define MALENA_LIST_H
7
8#pragma once
9
12#include <Malena/Core/Core.h>
21#include <functional>
22#include <memory>
23#include <string>
24#include <vector>
25#include <type_traits>
26
27namespace ml
28{
29 // ── ListManifest ──────────────────────────────────────────────────────────
30
32 {
33 public:
34 enum class Flag {};
35 enum class State {};
36 };
37
38 // ── List ──────────────────────────────────────────────────────────────────
39
100 class MALENA_API List : public ComponentWith<ListManifest>,
101 public ListSettings,
102 public ListTheme,
103 public Themeable
104 {
105 public:
108
109 private:
110 // ── Row storage ───────────────────────────────────────────────────────
111 struct Row
112 {
113 ml::Core* component = nullptr;
114 std::unique_ptr<ListItem> owned;
115 };
116
117 std::vector<Row> _rows;
118 std::vector<sf::RectangleShape> _dividers;
119
120 // ── Layout ────────────────────────────────────────────────────────────
121 sf::RectangleShape _background;
122 sf::Vector2f _position = {0.f, 0.f};
123 float _width = 300.f;
124 float _indent = 0.f;
125
126 // ── Internal helpers ──────────────────────────────────────────────────
127 void layout();
128 void rebuildDividers();
129 void applyThemeToOwnedItems();
130 void applySettingsToOwnedItems();
131
132 protected:
133 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
134 void onThemeApplied(const Theme& theme) override;
135
136 public:
138
139 List(const List&) = delete;
140 List& operator=(const List&) = delete;
141
142 // ── Apply ─────────────────────────────────────────────────────────────
143
144 template<typename S>
145 void applySettings(const S& s)
146 {
147 static_assert(std::is_base_of_v<ListSettings, S>,
148 "applySettings() requires a type derived from ListSettings");
149 static_cast<ListSettings&>(*this) = s;
150 applySettingsToOwnedItems();
151 layout();
152 }
153
154 template<typename T>
155 void applyTheme(const T& t)
156 {
157 static_assert(std::is_base_of_v<ListTheme, T>,
158 "applyTheme() requires a type derived from ListTheme");
159 static_cast<ListTheme&>(*this) = t;
160 applyThemeToOwnedItems();
161 layout();
162 }
163
164 template<typename St>
165 void applyStyle(const St& s)
166 {
167 static_assert(std::is_base_of_v<ListSettings, St> &&
168 std::is_base_of_v<ListTheme, St>,
169 "applyStyle() requires ListSettings and ListTheme");
170 static_cast<ListSettings&>(*this) = s;
171 static_cast<ListTheme&>(*this) = s;
172 applyThemeToOwnedItems();
173 applySettingsToOwnedItems();
174 layout();
175 }
176
177 // ── Adding rows ───────────────────────────────────────────────────────
178
190 ListItem& addItem(const std::string& label,
191 const std::string& description = "");
192
202 void add(ml::Core& component);
203
205 void clear();
206
208 [[nodiscard]] std::size_t rowCount() const { return _rows.size(); }
209
210 // ── Sizing ────────────────────────────────────────────────────────────
211
220 void setWidth(float width);
221 [[nodiscard]] float getWidth() const { return _width; }
222 [[nodiscard]] float getTotalHeight() const;
223
224 // ── Indent (for nested lists) ─────────────────────────────────────────
225
230 void setIndentOffset(float offset) { _indent = offset; layout(); }
231
232 // ── Positionable ──────────────────────────────────────────────────────
233
234 void setPosition(const sf::Vector2f& position) override;
235 sf::Vector2f getPosition() const override;
237 };
238
239 // ── ListWith ──────────────────────────────────────────────────────────────
240
241 template<typename MANIFEST>
242 class ListWith : public List, public Customizable<MANIFEST>
243 { public: using List::List; };
244
245} // namespace ml
246
247#endif // MALENA_LIST_H
Virtual base class for all Malena framework objects.
Definition Core.h:69
static const sf::Font & getDefault()
Return the built-in Arial font.
sf::Vector2f getPosition() const override
void applyTheme(const T &t)
Definition List.h:155
float getWidth() const
Definition List.h:221
std::size_t rowCount() const
Return the number of rows.
Definition List.h:208
float getTotalHeight() const
void applyStyle(const St &s)
Definition List.h:165
void setIndentOffset(float offset)
Set the current indentation offset in pixels. Called automatically by a parent List when nesting.
Definition List.h:230
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
void clear()
Remove all rows. Owned ListItem objects are destroyed.
void setPosition(const sf::Vector2f &position) override
List & operator=(const List &)=delete
ListManifest::Flag Flag
Definition List.h:106
void setWidth(float width)
Set the total width of the list.
sf::FloatRect getGlobalBounds() const override
void add(ml::Core &component)
Add any ml::Core component as a row.
List(const List &)=delete
ListManifest::State State
Definition List.h:107
void applySettings(const S &s)
Definition List.h:145
void onThemeApplied(const Theme &theme) override
Called by ThemeManager when the active theme changes.
ListItem & addItem(const std::string &label, const std::string &description="")
Create and own a ListItem with a label.
List(const sf::Font &font=FontManager<>::getDefault())
A single row in a List with start, content and end slots.
Definition ListItem.h:81
List(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
#define MALENA_API
Definition Component.h:22
Rect< float > FloatRect
Vector2< float > Vector2f
const sf::Font * font
Layout and behaviour settings for List.
Color tokens for List.
Definition ListTheme.h:24
Universal design token set applied across all Themeable components.
Definition Theme.h:70