8#include "TinyGPU/Color/RGB565.h"
9#include "TinyGPU/Color/RGB666.h"
10#include "TinyGPU/Color/RGB888.h"
11#include "TinyMaterialDesign/Core/Widget.h"
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
49 using ItemCountFn = std::function<
int()>;
50 using ItemAtFn = std::function<
Widget<RGB_T>&(
int index)>;
54 : itemWidth_(itemWidth), gap_(gap) {
55 this->bounds = bounds;
65 item.bounds = itemRect(
static_cast<
int>(items_.size()));
66 items_.push_back(&item);
67 if (
this->theme_ !=
nullptr) item.setTheme(*
this->theme_);
72 itemCountFn_ = std::move(count);
73 itemAtFn_ = std::move(at);
77 Widget<RGB_T>::setTheme(theme);
78 for (Widget<RGB_T>* item : items_) {
79 if (item !=
nullptr) item->setTheme(theme);
86 return Bounds(index * (itemWidth_ + gap_), 0, itemWidth_,
this->bounds.h - kIndicatorHeight);
96 const int count = itemCount();
97 if (count == 0)
return;
98 index = std::min(std::max(index, 0), count - 1);
99 const bool changed = index != currentIndex_;
100 currentIndex_ = index;
101 targetOffsetX_ =
static_cast<
float>(index * (itemWidth_ + gap_));
102 if (!animate) offsetX_ = targetOffsetX_;
103 animating_ = animate && offsetX_ != targetOffsetX_;
104 if (changed && onPageChange) onPageChange(currentIndex_);
109 void draw(tinygpu::ISurface<RGB_T>& target)
override {
110 target.pushClipRect(toPx(
this->bounds.x), toPx(
this->bounds.y), toPx(
this->bounds.w),
111 toPx(
this->bounds.h));
112 const int count = itemCount();
113 for (
int i = 0; i < count; ++i) {
114 Widget<RGB_T>* item = itemAt(i);
115 if (!item->visible)
continue;
116 const int32_t contentX = item->bounds.x;
117 item->bounds.x =
this->bounds.x + contentX -
static_cast<int32_t>(offsetX_);
118 if (item->bounds.right() >
this->bounds.x && item->bounds.x <
this->bounds.right()) {
121 item->bounds.x = contentX;
123 target.popClipRect();
124 drawIndicator(target);
127 bool onGesture(
const tinygpu::GestureEvent& event)
override {
128 if (event.type == tinygpu::GestureType::kDrag) {
129 if (event.phase == tinygpu::GesturePhase::kBegan) animating_ =
false;
130 offsetX_ -= event.stepDeltaX;
132 if (event.phase == tinygpu::GesturePhase::kEnded) snapToNearest();
135 if (!isTapGesture(event.type))
return false;
137 const int32_t contentX = event.point.x -
this->bounds.x +
static_cast<int32_t>(offsetX_);
138 const int count = itemCount();
139 for (
int i = 0; i < count; ++i) {
140 Widget<RGB_T>* item = itemAt(i);
141 if (item->enabled && item->bounds.contains(contentX, event.point.y)) {
142 tinygpu::GestureEvent shifted = event;
143 shifted.point.x =
static_cast<int16_t>(contentX);
144 return item->onGesture(shifted);
151 bool changed =
false;
153 const float diff = targetOffsetX_ - offsetX_;
154 if (std::fabs(diff) < 1.0f) {
155 offsetX_ = targetOffsetX_;
158 offsetX_ += diff * 0.3f;
162 const int count = itemCount();
163 for (
int i = 0; i < count; ++i) changed |= itemAt(i)->update(nowMs);
168 static constexpr int32_t kIndicatorHeight = 16;
170 int32_t itemWidth_ = 140;
172 std::vector<Widget<RGB_T>*> items_;
173 ItemCountFn itemCountFn_;
176 float offsetX_ = 0.0f;
177 float targetOffsetX_ = 0.0f;
178 bool animating_ =
false;
179 int currentIndex_ = 0;
181 int itemCount()
const {
return itemCountFn_ ? itemCountFn_() :
static_cast<
int>(items_.size()); }
188 Widget<RGB_T>* itemAt(
int index)
const {
190 Widget<RGB_T>& item = itemAtFn_(index);
192 if (
this->theme_ !=
nullptr) item.setTheme(*
this->theme_);
195 return items_[
static_cast<size_t>(index)];
198 int32_t maxOffsetX()
const {
199 const int count = itemCount();
200 return count > 0 ? std::max(int32_t{0}, (count - 1) * (itemWidth_ + gap_)) : 0;
204 offsetX_ = std::min(
static_cast<
float>(maxOffsetX()), std::max(0.0f, offsetX_));
209 void snapToNearest() {
210 if (itemCount() == 0)
return;
211 const int32_t step = itemWidth_ + gap_;
212 const int index = step > 0 ?
static_cast<
int>(std::round(offsetX_ / step)) : 0;
216 void drawIndicator(tinygpu::ISurface<RGB_T>& target) {
217 const int count = itemCount();
218 if (count <= 1)
return;
219 constexpr int32_t kDotDiameter = 6;
220 constexpr int32_t kDotGap = 8;
221 const int32_t totalWidth = count * kDotDiameter + (count - 1) * kDotGap;
222 int32_t x =
this->bounds.centerX() - totalWidth / 2;
223 const int32_t y =
this->bounds.bottom() - kIndicatorHeight / 2;
225 for (
int i = 0; i < count; ++i) {
227 i == currentIndex_ ?
this->theme().colors.primary :
this->theme().colors.surfaceVariant;
228 target.fillCircle(toPx(x + kDotDiameter / 2), toPx(y), toPx(kDotDiameter / 2), color);
229 x += kDotDiameter + kDotGap;
Horizontally paged, drag-to-swipe row of items (typically MediaCard), with a snap-to-item animation a...
Definition: Carousel.h:47
bool isDraggable() const override
Definition: Carousel.h:107
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: Carousel.h:127
int currentIndex() const
Definition: Carousel.h:89
void setItemProvider(ItemCountFn count, ItemAtFn at)
Switches to callback-driven items - see the class comment.
Definition: Carousel.h:71
void addItem(Widget< RGB_T > &item)
Definition: Carousel.h:64
Carousel(Bounds bounds, int32_t itemWidth=140, int32_t gap=12)
Definition: Carousel.h:53
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Carousel.h:109
bool update(uint32_t nowMs) override
Definition: Carousel.h:150
int pageCount() const
Definition: Carousel.h:90
std::function< void(int)> onPageChange
Definition: Carousel.h:60
void setCurrentIndex(int index, bool animate=true)
Definition: Carousel.h:95
void setTheme(const MaterialTheme< RGB_T > &theme) override
Definition: Carousel.h:76
Bounds itemRect(int index) const
Definition: Carousel.h:85
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
Bundles a color scheme, shape tokens, typography, and spacing into one theme, passed to widgets so th...
Definition: MaterialTheme.h:50