Loading...
Searching...
No Matches
Carousel.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith. All rights reserved.
2// Malena Framework — Proprietary Software. See LICENSE for terms.
3
4//
5// Carousel.h
6//
7
8#ifndef MALENA_CAROUSEL_H
9#define MALENA_CAROUSEL_H
10
12#include <Malena/Core/Core.h>
17#include <list>
18#include <optional>
21
22namespace ml
23{
33 {
34 public:
43 enum class Flag { SCROLLING, SHOW_ARROWS };
44
54 enum class State { SCROLL_IDLE, SCROLL_LEFT, SCROLL_RIGHT };
55
65 enum class ArrowPlacement { SIDES, TOP, OVERLAY };
66
68 enum class ArrowSide { LEFT, RIGHT };
69
72 };
73
115 class MALENA_API Carousel : public ComponentWith<CarouselManifest>
116 {
117 public:
128
129 private:
130 std::list<ml::Core*> _components;
131 sf::RenderTexture _renderTexture;
132 sf::Sprite _sprite;
133 sf::View _carouselView;
134
135 // Shader
136 CarouselManifest::Style _currentStyle = CarouselManifest::Style::DEFAULT;
137 sf::Shader* _shader = nullptr;
138 std::unique_ptr<sf::Shader> _ownedShader;
139 std::function<void(sf::Shader&)> _shaderUniformSetter;
140 float _shaderTime = 0.f;
141 ShaderLibrary::Params _shaderParams;
142
143 // Scroll state
144 float _spacing = 10.f;
145 float _speed = 1.f;
146 float _viewX = 0.f;
147 float _targetViewX = 0.f;
148 int _previewCount = 5;
149
150 sf::Vector2f _position = {0.f, 0.f};
151
152 // Arrow controls
153 ml::Rectangle _leftArrow;
154 ml::Rectangle _rightArrow;
155 sf::Text _leftLabel;
156 sf::Text _rightLabel;
157 ArrowPlacement _arrowPlacement = ArrowPlacement::SIDES;
158 float _arrowSize = 40.f;
159 sf::Color _arrowColor = sf::Color(80, 80, 80, 200);
160 sf::Color _arrowHoverColor = sf::Color(120, 120, 120, 230);
161 std::optional<sf::Texture> _leftArrowTexture;
162 std::optional<sf::Texture> _rightArrowTexture;
163 std::optional<sf::Vector2f> _leftArrowManualPos;
164 std::optional<sf::Vector2f> _rightArrowManualPos;
165
166 void updateView();
167 void wrapComponents();
168 void updateRenderTextureSize();
169 void updateArrowPositions();
170 void drawTexture();
171 void loadShaderForStyle(Style style);
172 void initArrows();
173 bool isOnLeftArrow(sf::Vector2f screenPos) const;
174 bool isOnRightArrow(sf::Vector2f screenPos) const;
175
176 protected:
177 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
178
179 public:
181
182 Carousel(const Carousel&) = delete;
183 Carousel& operator=(const Carousel&) = delete;
184
185 // ── Item management ──────────────────────────────────────────────────
186
195 void add(ml::Core& component);
196
203 void remove(ml::Core& component);
204
205 // ── Positionable overrides ───────────────────────────────────────────
206
207 void setPosition(const sf::Vector2f& position) override;
208 sf::Vector2f getPosition() const override;
211
212 // ── Navigation ───────────────────────────────────────────────────────
213
220 void next();
221
228 void previous();
229
230 // ── Content settings ─────────────────────────────────────────────────
231
236 void setSpacing(float spacing);
237
239 float getSpacing() const;
240
245 void setSpeed(float speed);
246
248 float getSpeed() const;
249
254 void setPreviewCount(int count);
255
257 int getPreviewCount() const;
258
259 // ── Shader settings ──────────────────────────────────────────────────
260
269 void setStyle(Style style);
270
273
285 void setShader(sf::Shader* shader, std::function<void(sf::Shader&)> uniformSetter);
286
292
298
299 // ── Arrow settings ───────────────────────────────────────────────────
300
305 void showArrows(bool show);
306
312
317 void setArrowSize(float size);
318
324
330
337
348
354 };
355
356} // namespace ml
357
358#endif // MALENA_CAROUSEL_H
Virtual base class for all Malena framework objects.
Definition Core.h:69
Base class for all Malena manifests.
Definition Manifest.h:51
A framework-integrated rectangle with optional rounded corners.
Definition Rectangle.h:48
Style
Available built-in visual styles.
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
All uniform parameters shared across the built-in shaders.