-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflickable.cpp
More file actions
73 lines (62 loc) · 2.52 KB
/
flickable.cpp
File metadata and controls
73 lines (62 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "flickable.h"
#include "nanovg.h"
Flickable::Flickable(Item *parent, float x, float y, float width, float hieght)
: Item (parent, x, y, width, hieght) {
}
bool Flickable::mouseDragEvent(float mx, float my, float rx, float ry,
int button, int modifiers) {
if (!mChildren.empty() && mChildPreferredHeight > hieght()) {
float scrollh = hieght() * std::min(1.0f, hieght() / mChildPreferredHeight);
mScroll = std::max(0.0f, std::min(1.0f, mScroll + ry / (hieght() - 8 - scrollh)));
onScrolled();
return true;
} else {
return Item::mouseDragEvent(mx, my, rx, ry, button, modifiers);
}
}
bool Flickable::scrollEvent(float mx, float my, float rx, float ry) {
if (!mChildren.empty() && mChildPreferredHeight > hieght()) {
float scrollAmount = ry * (hieght() / 20.0f);
float scrollh = hieght() * std::min(1.0f, hieght() / mChildPreferredHeight);
mScroll = std::max(0.0f, std::min(1.0f, mScroll - scrollAmount / (hieght() - 8 - scrollh)));
onScrolled();
return true;
} else {
return Item::scrollEvent(mx, my, rx, ry);
}
}
void Flickable::draw(NVGcontext *ctx) {
if (mChildren.empty())
return;
Item *child = mChildren[0];
child->performLayout();
child->y = -mScroll*(mChildPreferredHeight - hieght());
mChildPreferredHeight = child->hieght();
nvgSave(ctx);
nvgTranslate(ctx, x(), y());
nvgIntersectScissor(ctx, 0, 0, width(), hieght());
if (child->visible())
child->draw(ctx);
nvgRestore(ctx);
if (mChildPreferredHeight <= hieght() || !mMouseFocus)
return;
float scrollh = hieght() * std::min(1.0f, hieght() / mChildPreferredHeight);
/*NVGpaint paint = nvgBoxGradient(
ctx, x() + width() - 12 + 1, y() + 4 + 1, 8,
hieght() - 8, 3, 4, nvgRGBA(0, 0, 0, 32), nvgRGBA(0, 0, 0, 92));
nvgBeginPath(ctx);
nvgRoundedRect(ctx, x() + width() - 12, y() + 4, 8,
hieght() - 8, 3);
nvgFillPaint(ctx, paint);
nvgFill(ctx);*/
auto paint = nvgBoxGradient(
ctx, x() + width() - 12 - 1,
y() + 4 + (hieght() - 8 - scrollh) * mScroll - 1, 8, scrollh,
3, 4, nvgRGBA(220, 220, 220, 200), nvgRGBA(128, 128, 128, 200));
nvgBeginPath(ctx);
nvgRoundedRect(ctx, x() + width() - 12 + 1,
y() + 4 + 1 + (hieght() - 8 - scrollh) * mScroll, 8 - 2,
scrollh - 2, 2);
nvgFillPaint(ctx, paint);
nvgFill(ctx);
}