1/*
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 "SkottieSlide.h"
9
10#include "SkAnimTimer.h"
11#include "SkCanvas.h"
12#include "Skottie.h"
13
14SkottieSlide::SkottieSlide(const SkString& name, const SkString& path)
15    : fPath(path) {
16    fName = name;
17}
18
19void SkottieSlide::load(SkScalar, SkScalar) {
20    fAnimation  = skottie::Animation::MakeFromFile(fPath.c_str());
21    fTimeBase   = 0; // force a time reset
22
23    if (fAnimation) {
24        fAnimation->setShowInval(fShowAnimationInval);
25        SkDebugf("loaded Bodymovin animation v: %s, size: [%f %f], fr: %f\n",
26                 fAnimation->version().c_str(),
27                 fAnimation->size().width(),
28                 fAnimation->size().height(),
29                 fAnimation->frameRate());
30    } else {
31        SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str());
32    }
33}
34
35void SkottieSlide::unload() {
36    fAnimation.reset();
37}
38
39SkISize SkottieSlide::getDimensions() const {
40    return fAnimation? fAnimation->size().toCeil() : SkISize::Make(0, 0);
41}
42
43void SkottieSlide::draw(SkCanvas* canvas) {
44    if (fAnimation) {
45        SkAutoCanvasRestore acr(canvas, true);
46        const SkRect dstR = SkRect::Make(canvas->imageInfo().bounds());
47        fAnimation->render(canvas, &dstR);
48    }
49}
50
51bool SkottieSlide::animate(const SkAnimTimer& timer) {
52    if (fTimeBase == 0) {
53        // Reset the animation time.
54        fTimeBase = timer.msec();
55    }
56
57    if (fAnimation) {
58        auto t = timer.msec() - fTimeBase;
59        fAnimation->animationTick(t);
60    }
61    return true;
62}
63
64bool SkottieSlide::onChar(SkUnichar c) {
65    switch (c) {
66    case 'I':
67        if (fAnimation) {
68            fShowAnimationInval = !fShowAnimationInval;
69            fAnimation->setShowInval(fShowAnimationInval);
70        }
71        break;
72    default:
73        break;
74    }
75
76    return INHERITED::onChar(c);
77}
78