Loading...
Searching...
No Matches
Toolbar.h
Go to the documentation of this file.
1//
2// Toolbar.h
3//
4
5#ifndef MALENA_TOOLBAR_H
6#define MALENA_TOOLBAR_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{
30 {
31 public:
32 enum class Flag {};
33 enum class State {};
34 };
35
79 class MALENA_API Toolbar : public ComponentWith<ToolbarManifest>,
80 public ToolbarSettings,
81 public ToolbarTheme,
82 public Themeable
83 {
84 public:
89
90 private:
91 struct Item
92 {
93 ml::Core* component = nullptr;
94 std::unique_ptr<ml::Core> owned;
95 std::function<void()> action;
96 std::string label;
97 bool separator = false;
98 bool hovered = false;
99 bool enabled = true;
100 bool selected = false;
101
102 Item() = default;
103 Item(Item&&) = default;
104 Item& operator=(Item&&) = default;
105 };
106
107 std::vector<Item> _items;
108 sf::Vector2f _position = {0.f, 0.f};
109 float _barLength = 0.f;
110 float _scrollOffsetX = 0.f;
111 float _totalItemsLen = 0.f;
112 mutable int _rowCount = 1;
113 int _hoveredIdx = -1;
114
115 void layout();
116 void drawSeparator(sf::RenderTarget& target,
117 const sf::RenderStates& states,
118 const sf::Vector2f& pos) const;
119
120 protected:
121 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
122 void onThemeApplied(const Theme& theme) override;
123
124 public:
126
127 Toolbar(const Toolbar&) = delete;
128 Toolbar& operator=(const Toolbar&) = delete;
129
130 // ── Apply ─────────────────────────────────────────────────────────────
131
132 template<typename S>
133 void applySettings(const S& s)
134 {
135 static_assert(std::is_base_of_v<ToolbarSettings, S>,
136 "applySettings() requires ToolbarSettings");
137 static_cast<ToolbarSettings&>(*this) = s;
138 layout();
139 }
140
141 template<typename T>
142 void applyTheme(const T& t)
143 {
144 static_assert(std::is_base_of_v<ToolbarTheme, T>,
145 "applyTheme() requires ToolbarTheme");
146 static_cast<ToolbarTheme&>(*this) = t;
147 }
148
149 template<typename St>
150 void applyStyle(const St& s)
151 {
152 static_assert(std::is_base_of_v<ToolbarSettings, St> &&
153 std::is_base_of_v<ToolbarTheme, St>,
154 "applyStyle() requires ToolbarSettings and ToolbarTheme");
155 static_cast<ToolbarSettings&>(*this) = s;
156 static_cast<ToolbarTheme&>(*this) = s;
157 layout();
158 }
159
160 // ── Adding items ──────────────────────────────────────────────────────
161
163 std::size_t addButton(const std::string& label,
164 std::function<void()> action = {});
165
167 void setItemLabel(std::size_t index, const std::string& label);
168
170 void setItemEnabled(std::size_t index, bool enabled);
171
173 void setItemSelected(std::size_t index, bool selected);
174
196 void add(ml::Core& component);
197
199 void add(const ml::Core& component);
200
203
205 void clear();
206
208 [[nodiscard]] std::size_t itemCount() const { return _items.size(); }
209
210 // ── Sizing ────────────────────────────────────────────────────────────
211
216 void setBarLength(float length);
217 [[nodiscard]] float getBarLength() const { return _barLength; }
218 [[nodiscard]] float getBarThickness() const;
219
226 [[nodiscard]] float getContentExtent() const;
227
228 // ── Positionable ──────────────────────────────────────────────────────
229
230 void setPosition(const sf::Vector2f& position) override;
231 sf::Vector2f getPosition() const override;
233 };
234
235 template<typename MANIFEST>
236 class ToolbarWith : public Toolbar, public Customizable<MANIFEST>
237 { public: using Toolbar::Toolbar; };
238
239} // namespace ml
240#endif // MALENA_TOOLBAR_H
Virtual base class for all Malena framework objects.
Definition Core.h:97
static const sf::Font & getDefault()
Return the built-in Arial font.
Base class for all Malena manifests.
Definition Manifest.h:51
std::size_t itemCount() const
Return the number of items (including separators).
Definition Toolbar.h:208
sf::FloatRect getGlobalBounds() const override
ToolbarManifest::Flag Flag
Definition Toolbar.h:85
sf::Vector2f getPosition() const override
void applyStyle(const St &s)
Definition Toolbar.h:150
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
ToolbarSettings::Overflow Overflow
Definition Toolbar.h:88
void setItemSelected(std::size_t index, bool selected)
Set or clear the persistent selected highlight on an owned button.
Toolbar(const Toolbar &)=delete
void setPosition(const sf::Vector2f &position) override
ToolbarSettings::Orientation Orientation
Definition Toolbar.h:87
float getContentExtent() const
Extent of the laid-out items along the bar (width for H, height for V), measured from the bar origin ...
ToolbarManifest::State State
Definition Toolbar.h:86
void add(ml::Core &component)
Embed any ml::Core as a positioned toolbar item.
void clear()
Remove all items.
void applyTheme(const T &t)
Definition Toolbar.h:142
void setItemLabel(std::size_t index, const std::string &label)
Update the label of an owned button by item index.
float getBarLength() const
Definition Toolbar.h:217
Toolbar(const sf::Font &font=FontManager<>::getDefault())
void setBarLength(float length)
Override the bar length (width for H, height for V). By default fills the window width.
void setItemEnabled(std::size_t index, bool enabled)
Enable or disable an owned button by item index.
void add(const ml::Core &component)
Const overload — forwards to the non-const add().
void applySettings(const S &s)
Definition Toolbar.h:133
Toolbar & operator=(const Toolbar &)=delete
std::size_t addButton(const std::string &label, std::function< void()> action={})
Add a text-only button. Returns the item index.
void onThemeApplied(const Theme &theme) override
Called by ThemeManager when the active theme changes.
float getBarThickness() const
height (H) or width (V)
void addSeparator()
Add a visual separator.
Toolbar(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
const sf::Font * font
Universal design token set applied across all Themeable components.
Definition Theme.h:70