Skip to content

Commit 7b0fe6f

Browse files
tinyjinhermet
andcommitted
dexamples: add asset resolver in LottieExtensions
- Add a dedicated AssetResolver example animation to LottieExtension.cpp, a showcase for the new asset resolution functionality. The resolver loads image assets dynamically during runtime. - neat code++ Co-Authored-By: Hermet Park <[email protected]>
1 parent 90572e7 commit 7b0fe6f

File tree

2 files changed

+38
-72
lines changed

2 files changed

+38
-72
lines changed

examples/LottieExtension.cpp

Lines changed: 37 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/************************************************************************/
2626

2727
#define NUM_PER_ROW 4
28-
#define NUM_PER_COL 3
28+
#define NUM_PER_COL 4
2929

3030
struct UserExample : tvgexam::Example
3131
{
@@ -41,103 +41,62 @@ struct UserExample : tvgexam::Example
4141
unique_ptr<tvg::LottieAnimation> slot9;
4242
unique_ptr<tvg::LottieAnimation> slot10;
4343
unique_ptr<tvg::LottieAnimation> marker;
44+
unique_ptr<tvg::LottieAnimation> resolver;
4445
uint32_t slotId1, slotId2, slotId3, slotId4, slotId6, slotId7, slotId8, slotId9, slotId10;
4546
uint32_t w, h;
4647
uint32_t size;
4748

4849
void sizing(tvg::Picture* picture, uint32_t counter)
4950
{
51+
picture->origin(0.5f, 0.5f);
52+
5053
//image scaling preserving its aspect ratio
51-
float scale;
52-
float shiftX = 0.0f, shiftY = 0.0f;
5354
float w, h;
5455
picture->size(&w, &h);
55-
56-
if (w > h) {
57-
scale = size / w;
58-
shiftY = (size - h * scale) * 0.5f;
59-
} else {
60-
scale = size / h;
61-
shiftX = (size - w * scale) * 0.5f;
62-
}
63-
64-
picture->scale(scale);
65-
picture->translate((counter % NUM_PER_ROW) * size + shiftX, (counter / NUM_PER_ROW) * (this->h / NUM_PER_COL) + shiftY);
56+
picture->scale((w > h) ? size / w : size / h);
57+
picture->translate((counter % NUM_PER_ROW) * size + size / 2, (counter / NUM_PER_ROW) * (this->h / NUM_PER_COL) + size / 2);
6658
}
6759

6860
bool update(tvg::Canvas* canvas, uint32_t elapsed) override
6961
{
7062
//default slot
71-
{
72-
auto progress = tvgexam::progress(elapsed, slot0->duration());
73-
slot0->frame(slot0->totalFrame() * progress);
74-
}
63+
slot0->frame(slot0->totalFrame() * tvgexam::progress(elapsed, slot0->duration()));
7564

7665
//gradient slot
77-
{
78-
auto progress = tvgexam::progress(elapsed, slot1->duration());
79-
slot1->frame(slot1->totalFrame() * progress);
80-
}
66+
slot1->frame(slot1->totalFrame() * tvgexam::progress(elapsed, slot1->duration()));
8167

8268
//solid fill slot
83-
{
84-
auto progress = tvgexam::progress(elapsed, slot2->duration());
85-
slot2->frame(slot2->totalFrame() * progress);
86-
}
69+
slot2->frame(slot2->totalFrame() * tvgexam::progress(elapsed, slot2->duration()));
8770

8871
//image slot
89-
{
90-
auto progress = tvgexam::progress(elapsed, slot3->duration());
91-
slot3->frame(slot3->totalFrame() * progress);
92-
}
72+
slot3->frame(slot3->totalFrame() * tvgexam::progress(elapsed, slot3->duration()));
9373

9474
//overriden default slot
95-
{
96-
auto progress = tvgexam::progress(elapsed, slot4->duration());
97-
slot4->frame(slot4->totalFrame() * progress);
98-
}
75+
slot4->frame(slot4->totalFrame() * tvgexam::progress(elapsed, slot4->duration()));
9976

10077
//duplicate slot
101-
{
102-
auto progress = tvgexam::progress(elapsed, slot5->duration());
103-
slot5->frame(slot5->totalFrame() * progress);
104-
}
78+
slot5->frame(slot5->totalFrame() * tvgexam::progress(elapsed, slot5->duration()));
10579

10680
//position slot
107-
{
108-
auto progress = tvgexam::progress(elapsed, slot6->duration());
109-
slot6->frame(slot6->totalFrame() * progress);
110-
}
81+
slot6->frame(slot6->totalFrame() * tvgexam::progress(elapsed, slot6->duration()));
11182

11283
//scale slot
113-
{
114-
auto progress = tvgexam::progress(elapsed, slot7->duration());
115-
slot7->frame(slot7->totalFrame() * progress);
116-
}
84+
slot7->frame(slot7->totalFrame() * tvgexam::progress(elapsed, slot7->duration()));
11785

11886
//rotation slot
119-
{
120-
auto progress = tvgexam::progress(elapsed, slot8->duration());
121-
slot8->frame(slot8->totalFrame() * progress);
122-
}
87+
slot8->frame(slot8->totalFrame() * tvgexam::progress(elapsed, slot8->duration()));
12388

12489
//opacity slot
125-
{
126-
auto progress = tvgexam::progress(elapsed, slot9->duration());
127-
slot9->frame(slot9->totalFrame() * progress);
128-
}
90+
slot9->frame(slot9->totalFrame() * tvgexam::progress(elapsed, slot9->duration()));
12991

13092
//expression slot
131-
{
132-
auto progress = tvgexam::progress(elapsed, slot10->duration());
133-
slot10->frame(slot10->totalFrame() * progress);
134-
}
93+
slot10->frame(slot10->totalFrame() * tvgexam::progress(elapsed, slot10->duration()));
13594

13695
//marker
137-
{
138-
auto progress = tvgexam::progress(elapsed, marker->duration());
139-
marker->frame(marker->totalFrame() * progress);
140-
}
96+
marker->frame(marker->totalFrame() * tvgexam::progress(elapsed, marker->duration()));
97+
98+
//asset resolver
99+
resolver->frame(resolver->totalFrame() * tvgexam::progress(elapsed, resolver->duration()));
141100

142101
canvas->update();
143102

@@ -163,7 +122,6 @@ struct UserExample : tvgexam::Example
163122
if (!tvgexam::verify(picture->load(EXAMPLE_DIR"/lottie/extensions/slot0.json"))) return false;
164123

165124
sizing(picture, 0);
166-
167125
canvas->push(picture);
168126
}
169127

@@ -178,7 +136,6 @@ struct UserExample : tvgexam::Example
178136
if (!tvgexam::verify(slot1->apply(slotId1))) return false;
179137

180138
sizing(picture, 1);
181-
182139
canvas->push(picture);
183140
}
184141

@@ -193,7 +150,6 @@ struct UserExample : tvgexam::Example
193150
if (!tvgexam::verify(slot2->apply(slotId2))) return false;
194151

195152
sizing(picture, 2);
196-
197153
canvas->push(picture);
198154
}
199155

@@ -208,7 +164,6 @@ struct UserExample : tvgexam::Example
208164
if (!tvgexam::verify(slot3->apply(slotId3))) return false;
209165

210166
sizing(picture, 3);
211-
212167
canvas->push(picture);
213168
}
214169

@@ -223,7 +178,6 @@ struct UserExample : tvgexam::Example
223178
if (!tvgexam::verify(slot4->apply(slotId4))) return false;
224179

225180
sizing(picture, 4);
226-
227181
canvas->push(picture);
228182
}
229183

@@ -234,7 +188,6 @@ struct UserExample : tvgexam::Example
234188
if (!tvgexam::verify(picture->load(EXAMPLE_DIR"/lottie/extensions/slot5.json"))) return false;
235189

236190
sizing(picture, 5);
237-
238191
canvas->push(picture);
239192
}
240193

@@ -249,7 +202,6 @@ struct UserExample : tvgexam::Example
249202
if (!tvgexam::verify(slot6->apply(slotId6))) return false;
250203

251204
sizing(picture, 6);
252-
253205
canvas->push(picture);
254206
}
255207

@@ -264,7 +216,6 @@ struct UserExample : tvgexam::Example
264216
if (!tvgexam::verify(slot7->apply(slotId7))) return false;
265217

266218
sizing(picture, 7);
267-
268219
canvas->push(picture);
269220
}
270221

@@ -279,7 +230,6 @@ struct UserExample : tvgexam::Example
279230
if (!tvgexam::verify(slot8->apply(slotId8))) return false;
280231

281232
sizing(picture, 8);
282-
283233
canvas->push(picture);
284234
}
285235

@@ -309,7 +259,6 @@ struct UserExample : tvgexam::Example
309259
if (!tvgexam::verify(slot10->apply(slotId10))) return false;
310260

311261
sizing(picture, 10);
312-
313262
canvas->push(picture);
314263
}
315264

@@ -321,7 +270,23 @@ struct UserExample : tvgexam::Example
321270
if (!tvgexam::verify(marker->segment("sectionC"))) return false;
322271

323272
sizing(picture, 11);
273+
canvas->push(picture);
274+
}
275+
276+
//asset resolver
277+
{
278+
resolver = std::unique_ptr<tvg::LottieAnimation>(tvg::LottieAnimation::gen());
279+
auto picture = resolver->picture();
280+
281+
if (!tvgexam::verify(picture->resolver([](tvg::Paint* p, const char* src, void* data) {
282+
if (p->type() != tvg::Type::Picture) return false; //supposed to be a picture object
283+
auto ret = static_cast<tvg::Picture*>(p)->load(src); //load picture resources as demand
284+
return (ret == (tvg::Result) 0) ? true : false; //return true if resolving is successful
285+
}, nullptr))) return false;
286+
287+
if (!tvgexam::verify(picture->load(EXAMPLE_DIR"/lottie/extensions/resolver.json"))) return false;
324288

289+
sizing(picture, 12);
325290
canvas->push(picture);
326291
}
327292

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"v":"5.4.3","fr":60,"ip":0,"op":120,"w":800,"h":800,"nm":"AssetResolver Example","ddd":0,"assets":[{"id":"image_0","w":100,"h":100,"u":"images/","p":"logo.png","e":0}],"layers":[{"ddd":0,"ind":1,"ty":2,"nm":"Resolved Asset","refId":"image_0","sr":1,"ks":{"o":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":0,"s":[0]},{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":30,"s":[100]},{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":90,"s":[100]},{"t":120,"s":[0]}],"ix":11},"r":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":0,"s":[0]},{"t":120,"s":[360]}],"ix":10},"p":{"a":1,"k":[{"i":{"x":0.833,"y":0.833},"o":{"x":0.167,"y":0.167},"t":0,"s":[400,400]},{"i":{"x":0.833,"y":0.833},"o":{"x":0.167,"y":0.167},"t":60,"s":[500,300]},{"t":120,"s":[400,400]}],"ix":2},"a":{"a":0,"k":[100,100,0],"ix":1},"s":{"a":1,"k":[{"i":{"x":[0.833,0.833,0.833],"y":[0.833,0.833,0.833]},"o":{"x":[0.167,0.167,0.167],"y":[0.167,0.167,0.167]},"t":0,"s":[50,50,100]},{"i":{"x":[0.833,0.833,0.833],"y":[0.833,0.833,0.833]},"o":{"x":[0.167,0.167,0.167],"y":[0.167,0.167,0.167]},"t":60,"s":[150,150,100]},{"t":120,"s":[50,50,100]}],"ix":6}},"ao":0,"ip":0,"op":120,"st":0,"bm":0}],"markers":[]}

0 commit comments

Comments
 (0)