aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/viewer/SkottySlide.cpp
blob: d65b7ecbc1b163f88fc279f71632f1ca9451b777 (plain)
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
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkottySlide.h"

#include "SkAnimTimer.h"
#include "Skotty.h"
#include "SkStream.h"

SkottySlide::SkottySlide(const SkString& name, const SkString& path)
    : fPath(path) {
    fName = name;
}

void SkottySlide::load(SkScalar, SkScalar) {
    auto stream = SkStream::MakeFromFile(fPath.c_str());
    fAnimation  = skotty::Animation::Make(stream.get());
    fTimeBase   = 0; // force a time reset

    if (fAnimation) {
        SkDebugf("loaded Bodymovin animation v: %s, size: [%f %f], fr: %f\n",
                 fAnimation->version().c_str(),
                 fAnimation->size().width(),
                 fAnimation->size().height(),
                 fAnimation->frameRate());
    } else {
        SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str());
    }
}

void SkottySlide::unload() {
    fAnimation.reset();
}

SkISize SkottySlide::getDimensions() const {
    return fAnimation? fAnimation->size().toCeil() : SkISize::Make(0, 0);
}

void SkottySlide::draw(SkCanvas* canvas) {
    if (fAnimation) {
        fAnimation->render(canvas);
    }
}

bool SkottySlide::animate(const SkAnimTimer& timer) {
    if (fTimeBase == 0) {
        // Reset the animation time.
        fTimeBase = timer.msec();
    }

    if (fAnimation) {
        auto t = timer.msec() - fTimeBase;
        fAnimation->animationTick(t);
    }
    return true;
}

bool SkottySlide::onChar(SkUnichar c) {
    switch (c) {
    case 'I':
        if (fAnimation) {
            fShowAnimationInval = !fShowAnimationInval;
            fAnimation->setShowInval(fShowAnimationInval);
        }
        break;
    default:
        break;
    }

    return INHERITED::onChar(c);
}