blob: 6e3608c9047d7abfffbd5ba4e03e6598f48f07a9 [file] [log] [blame]
Florin Malita094ccde2017-12-30 12:27:00 -05001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkottySlide.h"
9
10#include "SkAnimTimer.h"
Florin Malitaaa4dc622018-01-02 14:37:37 -050011#include "SkCanvas.h"
Florin Malita094ccde2017-12-30 12:27:00 -050012#include "Skotty.h"
13#include "SkStream.h"
14
15SkottySlide::SkottySlide(const SkString& name, const SkString& path)
16 : fPath(path) {
17 fName = name;
18}
19
20void SkottySlide::load(SkScalar, SkScalar) {
21 auto stream = SkStream::MakeFromFile(fPath.c_str());
22 fAnimation = skotty::Animation::Make(stream.get());
23 fTimeBase = 0; // force a time reset
24
25 if (fAnimation) {
26 SkDebugf("loaded Bodymovin animation v: %s, size: [%f %f], fr: %f\n",
27 fAnimation->version().c_str(),
28 fAnimation->size().width(),
29 fAnimation->size().height(),
30 fAnimation->frameRate());
31 } else {
32 SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str());
33 }
34}
35
36void SkottySlide::unload() {
37 fAnimation.reset();
38}
39
40SkISize SkottySlide::getDimensions() const {
41 return fAnimation? fAnimation->size().toCeil() : SkISize::Make(0, 0);
42}
43
44void SkottySlide::draw(SkCanvas* canvas) {
45 if (fAnimation) {
Florin Malitaaa4dc622018-01-02 14:37:37 -050046 SkAutoCanvasRestore acr(canvas, true);
Mike Reed29859872018-01-08 08:25:27 -050047 const SkRect dstR = SkRect::Make(canvas->imageInfo().bounds());
48 fAnimation->render(canvas, &dstR);
Florin Malita094ccde2017-12-30 12:27:00 -050049 }
50}
51
52bool SkottySlide::animate(const SkAnimTimer& timer) {
53 if (fTimeBase == 0) {
54 // Reset the animation time.
55 fTimeBase = timer.msec();
56 }
57
58 if (fAnimation) {
59 auto t = timer.msec() - fTimeBase;
60 fAnimation->animationTick(t);
61 }
62 return true;
63}
64
65bool SkottySlide::onChar(SkUnichar c) {
66 switch (c) {
67 case 'I':
68 if (fAnimation) {
69 fShowAnimationInval = !fShowAnimationInval;
70 fAnimation->setShowInval(fShowAnimationInval);
71 }
72 break;
73 default:
74 break;
75 }
76
77 return INHERITED::onChar(c);
78}