-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathdrag.cpp
188 lines (153 loc) · 5.63 KB
/
drag.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
#include <karm-ui/drag.h>
namespace Karm::Ui {
// MARK: Dismisable ------------------------------------------------------------
struct Dismisable :
public ProxyNode<Dismisable> {
OnDismis _onDismis;
DismisDir _dir;
f64 _threshold;
Eased2f _drag{};
Math::Vec2i _last{};
bool _dismissed{};
Dismisable(OnDismis onDismis, DismisDir dir, f64 threshold, Ui::Child child)
: ProxyNode(child),
_onDismis(std::move(onDismis)),
_dir(dir),
_threshold(threshold) {}
void reconcile(Dismisable& o) override {
_onDismis = std::move(o._onDismis);
_dir = o._dir;
_threshold = o._threshold;
ProxyNode<Dismisable>::reconcile(o);
}
Math::Vec2i drag() const {
return _drag.value().cast<isize>();
}
void paint(Gfx::Canvas& g, Math::Recti r) override {
g.push();
g.clip(bound());
g.origin(drag().cast<f64>());
r.xy = r.xy - drag();
child().paint(g, r);
g.pop();
}
void event(App::Event& e) override {
if (auto me = e.is<App::MouseEvent>()) {
me->pos = me->pos - drag();
child().event(e);
me->pos = me->pos + drag();
} else if (e.is<Node::AnimateEvent>() and _dismissed and _drag.reached()) {
_onDismis(*this);
_dismissed = false;
Ui::ProxyNode<Dismisable>::event(e);
} else if (_drag.needRepaint(*this, e)) {
auto childBound = child().bound();
auto repaintBound =
bound()
.clipTo(childBound
.offset(_last)
.mergeWith(childBound.offset(drag())));
_last = drag();
Ui::shouldRepaint(*this, repaintBound);
Ui::ProxyNode<Dismisable>::event(e);
} else {
Ui::ProxyNode<Dismisable>::event(e);
}
}
void bubble(App::Event& e) override {
if (auto de = e.is<DragEvent>()) {
if (de->type == DragEvent::DRAG) {
auto d = _drag.target() + de->delta;
d.x = clamp(
d.x,
(bool)(_dir & DismisDir::LEFT) ? -bound().width : 0,
(bool)(_dir & DismisDir::RIGHT) ? bound().width : 0
);
d.y = clamp(
d.y,
(bool)(_dir & DismisDir::TOP) ? -bound().height : 0,
(bool)(_dir & DismisDir::DOWN) ? bound().height : 0
);
_drag.set(*this, d);
e.accept();
}
if (de->type == DragEvent::END) {
if ((bool)(_dir & DismisDir::HORIZONTAL)) {
if (Math::abs(_drag.targetX()) / (f64)bound().width > _threshold) {
_drag.animate(*this, {bound().width * (_drag.targetX() < 0.0 ? -1.0 : 1), 0}, 0.25, Math::Easing::cubicOut);
_dismissed = true;
} else {
_drag.animate(*this, {0, _drag.targetY()}, 0.25, Math::Easing::exponentialOut);
}
e.accept();
}
if ((bool)(_dir & DismisDir::VERTICAL)) {
if (Math::abs(_drag.targetY()) / (f64)bound().height > _threshold) {
_drag.animate(*this, {0, bound().height * (_drag.targetY() < 0.0 ? -1.0 : 1)}, 0.25, Math::Easing::cubicOut);
_dismissed = true;
} else {
_drag.animate(*this, {_drag.targetX(), 0}, 0.25, Math::Easing::exponentialOut);
}
e.accept();
}
}
}
Ui::ProxyNode<Dismisable>::bubble(e);
}
};
Child dismisable(OnDismis onDismis, DismisDir dir, f64 threshold, Ui::Child child) {
return makeRc<Dismisable>(std::move(onDismis), dir, threshold, std::move(child));
}
// MARK: Drag Region -----------------------------------------------------------
struct DragRegion : public ProxyNode<DragRegion> {
bool _grabbed{};
Math::Vec2i _dir;
DragRegion(Child child, Math::Vec2i dir)
: ProxyNode(child),
_dir(dir) {}
using ProxyNode::ProxyNode;
void event(App::Event& event) override {
ProxyNode::event(event);
if (event.accepted())
return;
auto e = event.is<App::MouseEvent>();
if (not e)
return;
if (not bound().contains(e->pos) and not _grabbed)
return;
if (e->type == App::MouseEvent::PRESS) {
_grabbed = true;
bubble<DragEvent>(*this, DragEvent::START);
event.accept();
} else if (e->type == App::MouseEvent::RELEASE) {
_grabbed = false;
bubble<DragEvent>(*this, DragEvent::END);
event.accept();
} else if (e->type == App::MouseEvent::MOVE and _grabbed) {
bubble<DragEvent>(*this, DragEvent::DRAG, e->delta * _dir);
event.accept();
}
}
};
Child dragRegion(Child child, Math::Vec2i dir) {
return makeRc<DragRegion>(child, dir);
}
// MARK: Handle ----------------------------------------------------------------
Child handle() {
return empty({128, 4}) |
box({
.borderRadii = 999,
.backgroundFill = GRAY50,
}) |
insets(12) |
center() |
minSize({UNCONSTRAINED, 48});
}
Child dragHandle() {
return handle() | dragRegion();
}
Child buttonHandle(OnPress press) {
return handle() |
button(std::move(press), Ui::ButtonStyle::none());
}
} // namespace Karm::Ui