-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathanim.cpp
170 lines (133 loc) · 4.13 KB
/
anim.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
#include "anim.h"
namespace Karm::Ui {
// MARK: Slide In --------------------------------------------------------------
struct SlideIn : public ProxyNode<SlideIn> {
SlideFrom _from;
Easedf _slide{};
SlideIn(SlideFrom from, Ui::Child child)
: ProxyNode(std::move(child)),
_from(from) {
}
Math::Vec2f outside() {
switch (_from) {
case SlideFrom::START:
return {(f64)-bound().width, 0};
case SlideFrom::END:
return {(f64)bound().width, 0};
case SlideFrom::TOP:
return {0, (f64)-bound().height};
case SlideFrom::BOTTOM:
return {0, (f64)bound().height};
}
}
auto translation() {
return lerp(outside(), Math::Vec2f{}, _slide.value()).cast<isize>();
}
void paint(Gfx::Canvas& g, Math::Recti r) override {
g.push();
g.clip(bound());
auto anim = translation();
g.origin(anim.cast<f64>());
r.xy = r.xy - anim;
child().paint(g, r);
g.pop();
}
void event(App::Event& e) override {
if (_slide.needRepaint(*this, e)) {
auto repaintBound =
bound().clipTo(
child().bound().offset(translation())
);
Ui::shouldRepaint(*this, repaintBound);
}
Ui::ProxyNode<SlideIn>::event(e);
}
void attach(Node* parent) override {
Ui::ProxyNode<SlideIn>::attach(parent);
_slide.animate(*this, 1.0, 0.25, Math::Easing::cubicOut);
}
};
Child slideIn(SlideFrom from, Ui::Child child) {
return makeRc<SlideIn>(from, std::move(child));
}
// MARK: Scale In --------------------------------------------------------------
struct ScaleIn : public ProxyNode<ScaleIn> {
Easedf _scale{};
ScaleIn(Ui::Child child)
: ProxyNode(std::move(child)) {
}
Math::Vec2f scale() {
return Math::Vec2f{0.9} + Math::Vec2f{_scale.value() * 0.1};
}
void paint(Gfx::Canvas& g, Math::Recti r) override {
g.push();
g.clip(bound());
g.origin(bound().center().cast<f64>());
g.scale(scale());
g.origin(-bound().center().cast<f64>());
child().paint(g, r);
g.pop();
}
void event(App::Event& e) override {
if (_scale.needRepaint(*this, e)) {
Ui::shouldRepaint(*this, bound());
}
Ui::ProxyNode<ScaleIn>::event(e);
}
void attach(Node* parent) override {
Ui::ProxyNode<ScaleIn>::attach(parent);
_scale.animate(*this, 1.0, 0.25, Math::Easing::cubicOut);
}
};
Child scaleIn(Child child) {
return makeRc<ScaleIn>(std::move(child));
}
// MARK: Carousel --------------------------------------------------------------
struct Carousel : public GroupNode<Carousel> {
usize _selected;
Math::Flow _flow;
Easedf _slide{};
Carousel(usize selected, Children children, Math::Flow flow)
: GroupNode(children), _selected(selected), _flow(flow) {
}
void reconcile(Carousel& o) override {
GroupNode::reconcile(o);
if (_selected != o._selected) {
_selected = o._selected;
_slide.animate(*this, _selected, 0.3, Math::Easing::cubicOut);
}
}
Math::Vec2i translation() {
return {
(int)(-_slide.value() * bound().width),
0,
};
}
void paint(Gfx::Canvas& g, Math::Recti r) override {
g.push();
g.clip(bound());
auto anim = translation();
g.origin(anim.cast<f64>());
for (auto& child : children()) {
child->paint(g, r);
}
g.pop();
}
void event(App::Event& e) override {
if (_slide.needRepaint(*this, e)) {
Ui::shouldRepaint(*this, bound());
}
GroupNode::event(e);
}
void layout(Math::Recti r) override {
_bound = r;
for (auto& child : children()) {
child->layout(r);
r = r.offset({r.width, 0});
}
}
};
Child carousel(usize selected, Children children, Math::Flow flow) {
return makeRc<Carousel>(selected, std::move(children), flow);
}
} // namespace Karm::Ui