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
77 class MALENA_API Toolbar : public ComponentWith<ToolbarManifest>,
78 public ToolbarSettings,
79 public ToolbarTheme,
80 public Themeable
81 {
82 public:
87
88 private:
89 struct Item
90 {
91 ml::Core* component = nullptr;
92 std::unique_ptr<ml::Core> owned;
93 std::function<void()> action;
94 std::string label;
95 bool separator = false;
96 bool hovered = false;
97
98 Item() = default;
99 Item(Item&&) = default;
100 Item& operator=(Item&&) = default;
101 };
102
103 std::vector<Item> _items;
104 sf::Vector2f _position = {0.f, 0.f};
105 float _barLength = 0.f;
106 int _hoveredIdx = -1;
107
108 void layout();
109 void drawSeparator(sf::RenderTarget& target,
110 const sf::RenderStates& states,
111 const sf::Vector2f& pos) const;
112
113 protected:
114 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
115 void onThemeApplied(const Theme& theme) override;
116
117 public:
119
120 Toolbar(const Toolbar&) = delete;
121 Toolbar& operator=(const Toolbar&) = delete;
122
123 // ── Apply ─────────────────────────────────────────────────────────────
124
125 template<typename S>
126 void applySettings(const S& s)
127 {
128 static_assert(std::is_base_of_v<ToolbarSettings, S>,
129 "applySettings() requires ToolbarSettings");
130 static_cast<ToolbarSettings&>(*this) = s;
131 layout();
132 }
133
134 template<typename T>
135 void applyTheme(const T& t)
136 {
137 static_assert(std::is_base_of_v<ToolbarTheme, T>,
138 "applyTheme() requires ToolbarTheme");
139 static_cast<ToolbarTheme&>(*this) = t;
140 }
141
142 template<typename St>
143 void applyStyle(const St& s)
144 {
145 static_assert(std::is_base_of_v<ToolbarSettings, St> &&
146 std::is_base_of_v<ToolbarTheme, St>,
147 "applyStyle() requires ToolbarSettings and ToolbarTheme");
148 static_cast<ToolbarSettings&>(*this) = s;
149 static_cast<ToolbarTheme&>(*this) = s;
150 layout();
151 }
152
153 // ── Adding items ──────────────────────────────────────────────────────
154
156 void addButton(const std::string& label,
157 std::function<void()> action = {});
158
165 void add(ml::Core& component);
166
169
171 void clear();
172
174 [[nodiscard]] std::size_t itemCount() const { return _items.size(); }
175
176 // ── Sizing ────────────────────────────────────────────────────────────
177
182 void setBarLength(float length);
183 [[nodiscard]] float getBarLength() const { return _barLength; }
184 [[nodiscard]] float getBarThickness() const;
185
186 // ── Positionable ──────────────────────────────────────────────────────
187
188 void setPosition(const sf::Vector2f& position) override;
189 sf::Vector2f getPosition() const override;
191 };
192
193 template<typename MANIFEST>
194 class ToolbarWith : public Toolbar, public Customizable<MANIFEST>
195 { public: using Toolbar::Toolbar; };
196
197} // namespace ml
198#endif // MALENA_TOOLBAR_H
Virtual base class for all Malena framework objects.
Definition Core.h:69
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:174
sf::FloatRect getGlobalBounds() const override
ToolbarManifest::Flag Flag
Definition Toolbar.h:83
sf::Vector2f getPosition() const override
void applyStyle(const St &s)
Definition Toolbar.h:143
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
ToolbarSettings::Overflow Overflow
Definition Toolbar.h:86
Toolbar(const Toolbar &)=delete
void setPosition(const sf::Vector2f &position) override
ToolbarSettings::Orientation Orientation
Definition Toolbar.h:85
void addButton(const std::string &label, std::function< void()> action={})
Add a text-only button.
ToolbarManifest::State State
Definition Toolbar.h:84
void add(ml::Core &component)
Add a component as a toolbar item.
void clear()
Remove all items.
void applyTheme(const T &t)
Definition Toolbar.h:135
float getBarLength() const
Definition Toolbar.h:183
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 applySettings(const S &s)
Definition Toolbar.h:126
Toolbar & operator=(const Toolbar &)=delete
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:299
#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