Loading...
Searching...
No Matches
Carousel.h
Go to the documentation of this file.
1//
2// Carousel.h
3//
4
5#ifndef CAROUSEL_H
6#define CAROUSEL_H
7
8#include <Malena/Core/Core.h>
13#include <list>
14#include <optional>
17
18namespace ml
19{
29 {
30 public:
39 enum class Flag { SCROLLING, SHOW_ARROWS };
40
51
61 enum class ArrowPlacement { SIDES, TOP, OVERLAY };
62
64 enum class ArrowSide { LEFT, RIGHT };
65
68 };
69
111 class Carousel : public ComponentWith<CarouselManifest>
112 {
113 public:
124
125 private:
126 std::list<ml::Core*> _components;
127 sf::RenderTexture _renderTexture;
128 sf::Sprite _sprite;
129 sf::View _carouselView;
130
131 // Shader
132 CarouselManifest::Style _currentStyle = CarouselManifest::Style::DEFAULT;
133 sf::Shader* _shader = nullptr;
134 std::unique_ptr<sf::Shader> _ownedShader;
135 std::function<void(sf::Shader&)> _shaderUniformSetter;
136 float _shaderTime = 0.f;
137 ShaderLibrary::Params _shaderParams;
138
139 // Scroll state
140 float _spacing = 10.f;
141 float _speed = 1.f;
142 float _viewX = 0.f;
143 float _targetViewX = 0.f;
144 int _previewCount = 5;
145
146 sf::Vector2f _position = {0.f, 0.f};
147
148 // Arrow controls
149 ml::Rectangle _leftArrow;
150 ml::Rectangle _rightArrow;
151 sf::Text _leftLabel;
152 sf::Text _rightLabel;
153 ArrowPlacement _arrowPlacement = ArrowPlacement::SIDES;
154 float _arrowSize = 40.f;
155 sf::Color _arrowColor = sf::Color(80, 80, 80, 200);
156 sf::Color _arrowHoverColor = sf::Color(120, 120, 120, 230);
157 std::optional<sf::Texture> _leftArrowTexture;
158 std::optional<sf::Texture> _rightArrowTexture;
159 std::optional<sf::Vector2f> _leftArrowManualPos;
160 std::optional<sf::Vector2f> _rightArrowManualPos;
161
162 void updateView();
163 void wrapComponents();
164 void updateRenderTextureSize();
165 void updateArrowPositions();
166 void drawTexture();
167 void loadShaderForStyle(Style style);
168 void initArrows();
169 bool isOnLeftArrow(sf::Vector2f screenPos) const;
170 bool isOnRightArrow(sf::Vector2f screenPos) const;
171
172 protected:
173 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
174
175 public:
177
178 // ── Item management ──────────────────────────────────────────────────
179
188 void add(ml::Core& component);
189
196 void remove(ml::Core& component);
197
198 // ── Positionable overrides ───────────────────────────────────────────
199
200 void setPosition(const sf::Vector2f& position) override;
201 sf::Vector2f getPosition() const override;
204
205 // ── Navigation ───────────────────────────────────────────────────────
206
213 void next();
214
221 void previous();
222
223 // ── Content settings ─────────────────────────────────────────────────
224
229 void setSpacing(float spacing);
230
232 float getSpacing() const;
233
238 void setSpeed(float speed);
239
241 float getSpeed() const;
242
247 void setPreviewCount(int count);
248
250 int getPreviewCount() const;
251
252 // ── Shader settings ──────────────────────────────────────────────────
253
262 void setStyle(Style style);
263
266
278 void setShader(sf::Shader* shader, std::function<void(sf::Shader&)> uniformSetter);
279
285
291
292 // ── Arrow settings ───────────────────────────────────────────────────
293
298 void showArrows(bool show);
299
305
310 void setArrowSize(float size);
311
317
323
330
341
347 };
348
349} // namespace ml
350
351#endif // CAROUSEL_H
Virtual base class for all Malena framework objects.
Definition Core.h:67
Base class for all Malena manifests.
Definition Manifest.h:81
A framework-integrated rectangle with optional rounded corners.
Definition Rectangle.h:44
Style
Available built-in visual styles.
Component< M, Traits... > ComponentWith
Alias for Component<M, Traits...>.
Definition Component.h:150
Definition Component.h:18
Rect< float > FloatRect
Vector2< float > Vector2f
All uniform parameters shared across the built-in shaders.