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
175 std::size_t addButton(const std::string& label, const sf::Texture& icon,
176 std::function<void()> action = {});
177
179 void setItemLabel(std::size_t index, const std::string& label);
180
182 void setItemEnabled(std::size_t index, bool enabled);
183
185 void setItemSelected(std::size_t index, bool selected);
186
188 void setItemBadge(std::size_t index, int count);
189
211 void add(ml::Core& component);
212
214 void add(const ml::Core& component);
215
218
220 void clear();
221
223 [[nodiscard]] std::size_t itemCount() const { return _items.size(); }
224
225 // ── Sizing ────────────────────────────────────────────────────────────
226
231 void setBarLength(float length);
232 [[nodiscard]] float getBarLength() const { return _barLength; }
233 [[nodiscard]] float getBarThickness() const;
234
241 [[nodiscard]] float getContentExtent() const;
242
243 // ── Positionable ──────────────────────────────────────────────────────
244
245 void setPosition(const sf::Vector2f& position) override;
246 sf::Vector2f getPosition() const override;
248 };
249
250 template<typename MANIFEST>
251 class ToolbarWith : public Toolbar, public Customizable<MANIFEST>
252 { public: using Toolbar::Toolbar; };
253
254} // namespace ml
255#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:223
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:232
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 setItemBadge(std::size_t index, int count)
Set a notification badge count on an owned button (0 hides it).
std::size_t addButton(const std::string &label, const sf::Texture &icon, std::function< void()> action={})
Add an icon + label button (icon left of the label).
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:323
#define MALENA_API
Definition Animate.h:25
Rect< float > FloatRect
Vector2< float > Vector2f
const sf::Font * font
Universal design token set applied across all Themeable components.
Definition Theme.h:70