-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathscroll.cpp
256 lines (202 loc) · 7.65 KB
/
scroll.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "scroll.h"
#include "anim.h"
namespace Karm::Ui {
// MARK: Scroll ----------------------------------------------------------------
struct Scroll : public ProxyNode<Scroll> {
static constexpr isize SCROLL_BAR_WIDTH = 4;
bool _mouseIn = false;
bool _animated = false;
Math::Orien _orient{};
Math::Recti _bound{};
Math::Vec2f _scroll{};
Math::Vec2f _targetScroll{};
Easedf _scrollOpacity;
Scroll(Child child, Math::Orien orient)
: ProxyNode(child), _orient(orient) {}
void scroll(Math::Vec2i s) {
auto childBound = child().bound();
_targetScroll.x = clamp(s.x, -(childBound.width - min(childBound.width, bound().width)), 0);
_targetScroll.y = clamp(s.y, -(childBound.height - min(childBound.height, bound().height)), 0);
if (_scroll.dist(_targetScroll) < 0.5) {
_scroll = _targetScroll;
_animated = false;
} else {
_animated = true;
}
}
bool canHScroll() {
return (_orient == Math::Orien::HORIZONTAL or _orient == Math::Orien::BOTH) and child().bound().width > bound().width;
}
Math::Recti hTrack() {
return Math::Recti{bound().start(), bound().bottom() - SCROLL_BAR_WIDTH, bound().width, SCROLL_BAR_WIDTH};
}
bool canVScroll() {
return (_orient == Math::Orien::VERTICAL or _orient == Math::Orien::BOTH) and child().bound().height > bound().height;
}
Math::Recti vTrack() {
return Math::Recti{bound().end() - SCROLL_BAR_WIDTH, bound().top(), SCROLL_BAR_WIDTH, bound().height};
}
void paint(Gfx::Canvas& g, Math::Recti r) override {
g.push();
g.clip(_bound);
g.origin(_scroll);
r.xy = r.xy - _scroll.cast<isize>();
child().paint(g, r);
g.pop();
// draw scroll bar
g.push();
g.clip(_bound);
auto childBound = child().bound();
if (canHScroll()) {
auto scrollBarWidth = (bound().width) * bound().width / childBound.width;
auto scrollBarX = bound().start() + (-_scroll.x * bound().width / childBound.width);
g.fillStyle(Gfx::GRAY500.withOpacity(0.5 * clamp01(_scrollOpacity.value())));
g.fill(Math::Recti{(isize)scrollBarX, bound().bottom() - SCROLL_BAR_WIDTH, scrollBarWidth, SCROLL_BAR_WIDTH});
}
if (canVScroll()) {
auto scrollBarHeight = (bound().height) * bound().height / childBound.height;
auto scrollBarY = bound().top() + (-_scroll.y * bound().height / childBound.height);
g.fillStyle(Ui::GRAY500.withOpacity(0.5 * clamp01(_scrollOpacity.value())));
g.fill(Math::Recti{bound().end() - SCROLL_BAR_WIDTH, (isize)scrollBarY, SCROLL_BAR_WIDTH, scrollBarHeight});
}
g.pop();
}
void event(App::Event& e) override {
if (_scrollOpacity.needRepaint(*this, e)) {
if (canHScroll())
shouldRepaint(*parent(), hTrack());
if (canVScroll())
shouldRepaint(*parent(), vTrack());
}
if (auto me = e.is<App::MouseEvent>()) {
if (bound().contains(me->pos)) {
_mouseIn = true;
me->pos = me->pos - _scroll.cast<isize>();
ProxyNode<Scroll>::event(e);
me->pos = me->pos + _scroll.cast<isize>();
if (not e.accepted()) {
if (me->type == App::MouseEvent::SCROLL) {
scroll((_scroll + me->scroll * 128).cast<isize>());
shouldAnimate(*this);
_scrollOpacity.delay(0).animate(*this, 1, 0.3);
}
}
} else if (_mouseIn) {
_mouseIn = false;
mouseLeave(*_child);
}
} else if (e.is<Node::AnimateEvent>() and _animated) {
shouldRepaint(*parent(), bound());
auto delta = _targetScroll - _scroll;
_scroll = _scroll + delta * (e.unwrap<Node::AnimateEvent>().dt * 12);
if (_scroll.dist(_targetScroll) < 0.5) {
_scroll = _targetScroll;
_animated = false;
_scrollOpacity.delay(1.0).animate(*this, 0, 0.3);
} else {
shouldAnimate(*this);
}
ProxyNode<Scroll>::event(e);
} else {
ProxyNode<Scroll>::event(e);
}
}
void bubble(App::Event& e) override {
if (auto pe = e.is<Node::PaintEvent>()) {
pe->bound.xy = pe->bound.xy + _scroll.cast<isize>();
pe->bound = pe->bound.clipTo(bound());
}
ProxyNode::bubble(e);
}
void layout(Math::Recti r) override {
_bound = r;
auto childSize = child().size(_bound.size(), Hint::MAX);
if (_orient == Math::Orien::HORIZONTAL) {
childSize.height = r.height;
} else if (_orient == Math::Orien::VERTICAL) {
childSize.width = r.width;
}
// Make sure the child is at least as big as the parent
childSize.width = max(childSize.width, r.width);
childSize.height = max(childSize.height, r.height);
r.wh = childSize;
child().layout(r);
scroll(_scroll.cast<isize>());
}
Math::Vec2i size(Math::Vec2i s, Hint hint) override {
auto childSize = child().size(s, hint);
if (hint == Hint::MIN) {
if (_orient == Math::Orien::HORIZONTAL) {
childSize.x = min(childSize.x, s.x);
} else if (_orient == Math::Orien::VERTICAL) {
childSize.y = min(childSize.y, s.y);
} else {
childSize = childSize.min(s);
}
}
return childSize;
}
Math::Recti bound() override {
return _bound;
}
};
Child vhscroll(Child child) {
return makeRc<Scroll>(child, Math::Orien::BOTH);
}
Child hscroll(Child child) {
return makeRc<Scroll>(child, Math::Orien::HORIZONTAL);
}
Child vscroll(Child child) {
return makeRc<Scroll>(child, Math::Orien::VERTICAL);
}
// MARK: Clip ------------------------------------------------------------------
struct Clip : public ProxyNode<Clip> {
static constexpr isize SCROLL_BAR_WIDTH = 4;
Math::Orien _orient{};
Math::Recti _bound{};
Clip(Child child, Math::Orien orient)
: ProxyNode(child), _orient(orient) {}
void paint(Gfx::Canvas& g, Math::Recti r) override {
g.push();
g.clip(_bound);
child().paint(g, r);
g.pop();
}
void layout(Math::Recti r) override {
_bound = r;
auto childSize = child().size(_bound.size(), Hint::MAX);
if (_orient == Math::Orien::HORIZONTAL) {
childSize.height = r.height;
} else if (_orient == Math::Orien::VERTICAL) {
childSize.width = r.width;
}
r.wh = childSize;
child().layout(r);
}
Math::Vec2i size(Math::Vec2i s, Hint hint) override {
auto childSize = child().size(s, hint);
if (hint == Hint::MIN) {
if (_orient == Math::Orien::HORIZONTAL) {
childSize.x = min(childSize.x, s.x);
} else if (_orient == Math::Orien::VERTICAL) {
childSize.y = min(childSize.y, s.y);
} else {
childSize = childSize.min(s);
}
}
return childSize;
}
Math::Recti bound() override {
return _bound;
}
};
Child vhclip(Child child) {
return makeRc<Clip>(child, Math::Orien::BOTH);
}
Child hclip(Child child) {
return makeRc<Clip>(child, Math::Orien::HORIZONTAL);
}
Child vclip(Child child) {
return makeRc<Clip>(child, Math::Orien::VERTICAL);
}
} // namespace Karm::Ui