7#include "TinyGPU/Input/GestureDetector.h"
8#include "TinyMaterialDesign/Core/Widget.h"
9#include "TinyMaterialDesign/Core/OffsetSurface.h"
14
15
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
46
47
48
49
50
51
52
53
54
58 using Widget<RGB_T>::bounds;
59 using ChildCountFn = std::function<
int()>;
60 using ChildAtFn = std::function<
Widget<RGB_T>&(
int index)>;
69 children_.push_back(&child);
70 if (
this->theme_ !=
nullptr) child.setTheme(*
this->theme_);
76 for (size_t i = 0; i < children_.size(); ++i) {
77 if (children_[i] == &child) {
78 if (activeChild_ == &child) activeChild_ =
nullptr;
79 children_.erase(children_.begin() +
static_cast<
long>(i));
94 int childrenCount()
const {
return static_cast<
int>(children_.size()); }
114 childCountFn_ = std::move(count);
134 Widget<RGB_T>::setTheme(theme);
135 for (Widget<RGB_T>* child : children_) {
136 if (child !=
nullptr) child->setTheme(theme);
140 void draw(tinygpu::ISurface<RGB_T>& target)
override {
142 drawChildren(target);
143 drawScrollbar(target);
151 drawScrollbar(target);
155 bool changed =
false;
157 for (
int i = 0; i < count; ++i) {
159 if (child !=
nullptr) changed |= child->update(nowMs);
164 bool onGesture(
const tinygpu::GestureEvent& event)
override {
165 using tinygpu::GesturePhase;
166 using tinygpu::GestureType;
168 if (isContinuousType(event.type)) {
169 if (event.phase == GesturePhase::kBegan) {
170 scrollbarDragActive_ = isOnScrollbar(event.startPoint.x, event.startPoint.y);
172 if (event.phase == GesturePhase::kBegan && !scrollbarDragActive_) {
185 if ((event.type == GestureType::kPan || event.type == GestureType::kScroll) &&
186 contentHeight() > bounds.h) {
191 activeChild_ = hitTest(event.startPoint.x, event.startPoint.y);
195 bool handled =
false;
198 scrollToTrackY(event.point.y);
201 scrollOffset_ -= event.stepDeltaY;
205 handled = dispatch(*activeChild_, event);
208 if (event.phase == GesturePhase::kEnded) {
217 if (isTapGesture(event.type) && isOnScrollbar(event.point.x, event.point.y)) {
218 scrollToTrackY(event.point.y);
222 Widget<RGB_T>* hit = hitTest(event.point.x, event.point.y);
223 if (hit !=
nullptr && hit->enabled)
return dispatch(*hit, event);
244 Widget<RGB_T>* hit = hitTest(x, y);
245 if (hit ==
nullptr)
return false;
263 using tinygpu::GestureType;
265 case GestureType::kDrag:
266 case GestureType::kPan:
267 case GestureType::kScroll:
268 case GestureType::kPinchIn:
269 case GestureType::kPinchOut:
270 case GestureType::kRotate:
294 int32_t bottom = bounds.y;
295 for (
int i = 0; i < count; ++i) {
297 if (child !=
nullptr) bottom = std::max(bottom, child->bounds.bottom());
299 return bottom - bounds.y;
305 return std::max(int32_t(24), bounds.h * bounds.h / content);
320 const int32_t maxScroll = content - bounds.h;
321 if (maxScroll <= 0)
return;
323 const int32_t track = bounds.h - thumb;
324 scrollOffset_ = track > 0 ? (y - bounds.y - thumb / 2) * maxScroll / track : 0;
329 const int32_t maxScroll = std::max(int32_t{0},
contentHeight() - bounds.h);
337 return childCountFn_ ? childCountFn_() :
static_cast<
int>(children_.size());
347 if (
this->theme_ !=
nullptr) child.setTheme(*
this->theme_);
350 return children_[
static_cast<size_t>(index)];
368 bool intersectsBounds(
const Bounds& b)
const {
369 return b
.bottom() > bounds.y && b
.y < bounds.bottom() && b
.right() > bounds.x && b
.x < bounds.right();
376 void drawChildren(tinygpu::ISurface<RGB_T>& target) {
377 target.pushClipRect(toPx(bounds.x), toPx(bounds.y), toPx(bounds.w), toPx(bounds.h));
379 for (
int i = 0; i < count; ++i) {
381 if (child ==
nullptr || !child->visible)
continue;
383 if (intersectsBounds(child->bounds)) {
384 if (child->bounds.y < 0) {
385 drawAboveTop(target, *child);
392 target.popClipRect();
399 void drawAboveTop(tinygpu::ISurface<RGB_T>& target,
Widget<RGB_T>& child) {
400 const int32_t dy = -child.bounds.y;
401 OffsetSurface<RGB_T> shifted(target, dy, bounds.y, bounds.bottom());
402 child.bounds.y += dy;
404 child.bounds.y -= dy;
410 void drawScrollbar(tinygpu::ISurface<RGB_T>& target) {
412 if (content <= bounds.h)
return;
414 constexpr int32_t kBarWidth = 4;
415 const int32_t trackX = bounds.right() - kBarWidth;
416 target.fillRect(toPx(trackX), toPx(bounds.y), toPx(kBarWidth), toPx(bounds.h),
417 blend(
this->theme().colors.surface,
this->theme().colors.outline, 0.25f));
419 const int32_t maxScroll = content - bounds.h;
421 const int32_t thumbY =
422 maxScroll > 0 ? bounds.y + (
scrollOffset_ * (bounds.h - thumbHeight)) / maxScroll : bounds.y;
423 target.fillRoundRect(toPx(trackX), toPx(thumbY), toPx(kBarWidth), toPx(thumbHeight), toPx(kBarWidth / 2),
424 this->theme().colors.primary);
432 Widget<RGB_T>* hitTest(int32_t x, int32_t y)
const {
436 if (child !=
nullptr && child->visible && child->enabled && child->bounds.contains(x, contentY)) {
445 bool dispatch(
Widget<RGB_T>& child,
const tinygpu::GestureEvent& event) {
446 if (scrollOffset_ == 0)
return child.onGesture(event);
447 tinygpu::GestureEvent shifted = event;
448 shifted.point.y =
static_cast<int16_t>(shifted.point.y + scrollOffset_);
449 shifted.startPoint.y =
static_cast<int16_t>(shifted.startPoint.y + scrollOffset_);
450 return child.onGesture(shifted);
A widget that holds child widgets, draws/dispatches gestures to them, and scrolls its content vertica...
Definition: Container.h:56
void scrollToTrackY(int32_t y)
Definition: Container.h:318
ChildCountFn childCountFn_
Definition: Container.h:354
void clearChildren()
Removes all children (they are not deleted) and scrolls to the top.
Definition: Container.h:87
int32_t uniformItemHeight_
Definition: Container.h:356
bool isContainer() const override
Provides true if the widget is a container.
Definition: Container.h:277
bool isDraggableAt(int32_t x, int32_t y) const override
Definition: Container.h:241
int32_t contentHeight() const
Definition: Container.h:291
int32_t scrollThumbHeight(int32_t content) const
Definition: Container.h:304
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: Container.h:164
int32_t scrollOffset() const
Current vertical scroll offset (0 = content's natural top).
Definition: Container.h:232
bool isOnScrollbar(int32_t x, int32_t y) const
Definition: Container.h:311
void addChild(Widget< RGB_T > &child)
Definition: Container.h:68
int childrenCount() const
Number of children added with addChild().
Definition: Container.h:94
static constexpr int32_t kScrollbarTouchWidth
Definition: Container.h:256
Widget< RGB_T > * child(int index) override
Definition: Container.h:229
std::vector< Widget< RGB_T > * > children_
Definition: Container.h:353
void setChildProvider(ChildCountFn count, ChildAtFn at)
Definition: Container.h:113
bool scrollDragActive_
Definition: Container.h:358
Widget< RGB_T > * effectiveChild(int index) const
Definition: Container.h:344
void scrollToChild(const Widget< RGB_T > &child, int32_t margin=4)
Definition: Container.h:106
void setUniformItemHeight(int32_t height)
Definition: Container.h:127
int32_t scrollOffset_
Definition: Container.h:361
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Container.h:140
int effectiveCount() const
Definition: Container.h:336
Container(Bounds containerBounds)
Definition: Container.h:63
Widget< RGB_T > * activeChild_
Definition: Container.h:357
bool update(uint32_t nowMs) override
Definition: Container.h:154
int childCount() const override
Definition: Container.h:227
ChildAtFn childAtFn_
Definition: Container.h:355
bool removeChild(Widget< RGB_T > &child)
Definition: Container.h:75
void setTheme(const MaterialTheme< RGB_T > &theme) override
Definition: Container.h:133
void drawBackground(tinygpu::ISurface< RGB_T > &target) override
Definition: Container.h:149
bool scrollbarDragActive_
A continuous gesture started on the scrollbar (see isOnScrollbar()).
Definition: Container.h:360
void setScrollOffset(int32_t offset)
Definition: Container.h:98
static bool isContinuousType(tinygpu::GestureType type)
Definition: Container.h:262
void clampScroll()
Definition: Container.h:328
Draws into target moved up by dy pixels, only inside the rows [clipTop, clipBottom) of target.
Definition: OffsetSurface.h:22
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t right() const
Definition: Bounds.h:30
int32_t bottom() const
Definition: Bounds.h:31
int32_t y
Definition: Bounds.h:23
int32_t x
Definition: Bounds.h:22
Bundles a color scheme, shape tokens, typography, and spacing into one theme, passed to widgets so th...
Definition: MaterialTheme.h:50