Loading...
Searching...
No Matches
EditableList.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith.
2// Malena Framework — Licensed under PolyForm Noncommercial 1.0.0; commercial use requires a paid license. See LICENSE.
3
4#ifndef MALENA_EDITABLELIST_H
5#define MALENA_EDITABLELIST_H
6
7#pragma once
8
10#include <Malena/Core/Core.h>
14#include <functional>
15#include <memory>
16#include <string>
17#include <vector>
18
19namespace ml
20{
56 {
57 public:
58 enum class SelectionMode { None, Single, Multi };
59
62 using ContentFactory = std::function<std::unique_ptr<Core>()>;
63
65 ~EditableList() override; // out-of-line for the pimpl-style Row
66
67 // ── Configuration ──────────────────────────────────────────────────────
70 void setPlaceholder(const std::string& text); // default TextInput content
71 void setShowActions(bool show); // per-row delete button
72 void setShowAddButton(bool show);
73 void setAddButtonLabel(const std::string& label);
74 void setMinRows(int n);
75 void setMaxRows(int n);
76 void setRowHeight(float h);
77
78 // ── Rows ────────────────────────────────────────────────────────────────
79 int addRow(); // append; returns its index
80 void removeRow(int index);
81 void clearRows();
82 [[nodiscard]] int rowCount() const;
83 [[nodiscard]] Core* contentAt(int index) const; // the row's content widget
84
85 // ── Selection ─────────────────────────────────────────────────────────--
86 void setSelected(int index, bool selected);
87 [[nodiscard]] std::vector<int> selectedIndices() const;
88
89 // ── Text convenience (default TextInput content) ─────────────────────────
91 void setValues(const std::vector<std::string>& values,
92 const std::vector<int>& selected = {});
94 [[nodiscard]] std::vector<std::string> values() const;
95
96 // ── Callbacks ─────────────────────────────────────────────────────────--
97 void onChange(std::function<void()> cb);
98
99 // ── Layout ────────────────────────────────────────────────────────────--
100 void setPosition(const sf::Vector2f& pos) override;
101 void setSize(const sf::Vector2f& size);
102 void setVisible(bool visible) override;
103
104 protected:
105 // Rows live in the ScrollPane (not this Panel's child list), so mirror the
106 // enabled state onto them here — fires on both direct and cascade paths.
107 void onEnabledChanged(bool enabled) override;
108
109 private:
110 struct Row; // defined in the .cpp
111
112 std::unique_ptr<Core> makeContent() const; // factory or default TextInput
113 void relayout();
114 void onSelectorClicked(Row* row);
115 void refreshSelectors();
116 int indexOf(const Row* row) const;
117 void fireChange() const;
118
119 ScrollPane _scroll{ 10.f, 10.f };
120 RectangleButton _addBtn;
121 std::vector<std::unique_ptr<Row>> _rows;
122
123 SelectionMode _mode = SelectionMode::Single;
124 ContentFactory _contentFactory;
125 std::string _placeholder;
126 std::string _addLabel = "+ Add";
127 bool _showActions = true;
128 bool _showAdd = true;
129 int _minRows = 0;
130 int _maxRows = 100;
131 float _rowHeight = 34.f;
132
133 std::function<void()> _onChange;
134 };
135
136} // namespace ml
137
138#endif // MALENA_EDITABLELIST_H
Virtual base class for all Malena framework objects.
Definition Core.h:97
void onChange(std::function< void()> cb)
void removeRow(int index)
void setContentFactory(ContentFactory factory)
std::vector< int > selectedIndices() const
~EditableList() override
void setPosition(const sf::Vector2f &pos) override
Set the world-space position immediately (no animation).
void setSize(const sf::Vector2f &size)
void setMaxRows(int n)
std::vector< std::string > values() const
Text of every row, in order (includes empty rows — caller filters).
std::function< std::unique_ptr< Core >()> ContentFactory
void setSelected(int index, bool selected)
void setValues(const std::vector< std::string > &values, const std::vector< int > &selected={})
Replace all rows with one per value; selected marks the chosen rows.
void setPlaceholder(const std::string &text)
int rowCount() const
void setSelectionMode(SelectionMode mode)
void setShowAddButton(bool show)
void setShowActions(bool show)
void onEnabledChanged(bool enabled) override
void setAddButtonLabel(const std::string &label)
void setMinRows(int n)
void setVisible(bool visible) override
void setRowHeight(float h)
Core * contentAt(int index) const
A rectangular button with a centered text label.
#define MALENA_API
Definition Component.h:22
Vector2< float > Vector2f