Loading...
Searching...
No Matches
Table.h
Go to the documentation of this file.
1//
2// Table.h
3//
4
5#ifndef MALENA_TABLE_H
6#define MALENA_TABLE_H
7
8#pragma once
9
12#include <Malena/Core/Core.h>
20#include <string>
21#include <vector>
22#include <functional>
23#include <unordered_map>
24
25namespace ml
26{
28 {
29 public:
30 enum class Flag {};
31 enum class State {};
32 };
33
62 class MALENA_API Table : public ComponentWith<TableManifest>, public Themeable
63 {
64 public:
67
68 private:
69 struct Column
70 {
71 std::string header;
72 float width; // 0 = flex
73 };
74 using Row = std::vector<std::string>;
75
76 std::vector<Column> _columns;
77 std::vector<Row> _rows;
78 std::size_t _maxRows = 0; // 0 = unlimited
79 std::unordered_map<std::size_t, sf::Color> _rowColors;
80
81 const sf::Font* _font;
82 sf::Vector2f _position = {0.f, 0.f};
83 sf::Vector2f _size = {400.f, 300.f};
84 float _rowHeight = 28.f;
85 float _headerHeight= 32.f;
86 float _padding = 8.f;
87 unsigned int _fontSize = 13u;
88 unsigned int _headerFontSize = 13u;
89
90 sf::Color _headerBgColor = sf::Color(55, 55, 65);
91 sf::Color _headerTxtColor = sf::Color(200, 200, 210);
92 sf::Color _rowBgColor = sf::Color(30, 30, 30);
93 sf::Color _altRowBgColor = sf::Color(38, 38, 45);
94 sf::Color _textColor = sf::Color(220, 220, 220);
95 sf::Color _dividerColor = sf::Color(70, 70, 80);
96 sf::Color _borderColor = sf::Color(70, 70, 80);
97
98 std::vector<float> resolveWidths() const;
99
100 void drawHeader (sf::RenderTarget&, const sf::RenderStates&,
101 const std::vector<float>& widths) const;
102 void drawDataRow (sf::RenderTarget&, const sf::RenderStates&,
103 const Row&, float y, bool alt,
104 const std::vector<float>& widths,
105 const sf::Color* bgOverride = nullptr) const;
106
107 static std::string clampText(const sf::Font&, unsigned charSize,
108 const std::string& text, float maxWidth);
109
110 protected:
111 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
112 void onThemeApplied(const Theme& theme) override;
113
114 public:
115 explicit Table(const sf::Font& font = FontManager<>::getDefault());
116
117 Table(const Table&) = delete;
118 Table& operator=(const Table&) = delete;
119
120 // ── Column / row management ───────────────────────────────────────────
121
127 void addColumn(const std::string& header, float width = 0.f);
128
136 void addRow(const std::vector<std::string>& cells);
137
139 bool removeRow(std::size_t index);
140
142 void clear();
143
145 void reset();
146
156 void setRowColor(std::size_t rowIndex, sf::Color color);
157
159 void clearRowColor(std::size_t rowIndex);
160
165 void setMaxRows(std::size_t max);
166
167 [[nodiscard]] std::size_t rowCount() const { return _rows.size(); }
168 [[nodiscard]] std::size_t columnCount() const { return _columns.size(); }
169
170 // ── Appearance ────────────────────────────────────────────────────────
171
172 void setRowHeight (float h) { _rowHeight = h; }
173 void setHeaderHeight(float h) { _headerHeight = h; }
174 void setFontSize (unsigned int s) { _fontSize = s; }
175 void setPadding (float p) { _padding = p; }
176
177 void setHeaderBgColor (const sf::Color& c) { _headerBgColor = c; }
178 void setHeaderTextColor(const sf::Color& c) { _headerTxtColor = c; }
179 void setRowBgColor (const sf::Color& c) { _rowBgColor = c; }
180 void setAltRowBgColor (const sf::Color& c) { _altRowBgColor = c; }
181 void setTextColor (const sf::Color& c) { _textColor = c; }
182 void setDividerColor (const sf::Color& c) { _dividerColor = c; }
183 void setBorderColor (const sf::Color& c) { _borderColor = c; }
184
185 // ── Size / position ───────────────────────────────────────────────────
186
187 void setSize(const sf::Vector2f& size) { _size = size; }
188 sf::Vector2f getSize() const { return _size; }
189
190 void setPosition(const sf::Vector2f& pos) override;
191 sf::Vector2f getPosition() const override;
193 };
194
195 template<typename MANIFEST>
196 class TableWith : public Table, public Customizable<MANIFEST>
197 { public: using Table::Table; };
198
199} // namespace ml
200
201#endif // MALENA_TABLE_H
static const sf::Font & getDefault()
Return the built-in Arial font.
Base class for all Malena manifests.
Definition Manifest.h:51
void onThemeApplied(const Theme &theme) override
Called by ThemeManager when the active theme changes.
void setHeaderBgColor(const sf::Color &c)
Definition Table.h:177
void setHeaderTextColor(const sf::Color &c)
Definition Table.h:178
TableManifest::State State
Definition Table.h:66
void setMaxRows(std::size_t max)
Cap the number of stored rows; oldest are dropped on overflow. Pass 0 to disable the limit.
void setHeaderHeight(float h)
Definition Table.h:173
void setDividerColor(const sf::Color &c)
Definition Table.h:182
void setSize(const sf::Vector2f &size)
Definition Table.h:187
sf::FloatRect getGlobalBounds() const override
void setRowBgColor(const sf::Color &c)
Definition Table.h:179
void addColumn(const std::string &header, float width=0.f)
Define a column. Call before adding rows.
sf::Vector2f getPosition() const override
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
std::size_t rowCount() const
Definition Table.h:167
void setPadding(float p)
Definition Table.h:175
void reset()
Remove all data rows AND all column definitions.
void setRowColor(std::size_t rowIndex, sf::Color color)
Override the background colour of a specific row.
TableManifest::Flag Flag
Definition Table.h:65
bool removeRow(std::size_t index)
Remove a row by index (0 = oldest).
void setTextColor(const sf::Color &c)
Definition Table.h:181
Table(const sf::Font &font=FontManager<>::getDefault())
void addRow(const std::vector< std::string > &cells)
Append a data row.
void setAltRowBgColor(const sf::Color &c)
Definition Table.h:180
sf::Vector2f getSize() const
Definition Table.h:188
void clearRowColor(std::size_t rowIndex)
Remove the colour override for a specific row.
void setFontSize(unsigned int s)
Definition Table.h:174
std::size_t columnCount() const
Definition Table.h:168
void setRowHeight(float h)
Definition Table.h:172
Table(const Table &)=delete
void setPosition(const sf::Vector2f &pos) override
void clear()
Remove all data rows (columns are kept).
Table & operator=(const Table &)=delete
void setBorderColor(const sf::Color &c)
Definition Table.h:183
Table(const sf::Font &font=FontManager<>::getDefault())
Component< M, Traits... > ComponentWith
Alias for Component<M, Traits...>.
Definition Component.h:316
#define MALENA_API
Definition Component.h:22
Rect< float > FloatRect
Vector2< float > Vector2f
Universal design token set applied across all Themeable components.
Definition Theme.h:70