blob: d65b7ecbc1b163f88fc279f71632f1ca9451b777 [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"
11#include "Skotty.h"
12#include "SkStream.h"
13
14SkottySlide::SkottySlide(const SkString& name, const SkString& path)
15 : fPath(path) {
16 fName = name;
17}
18
19void SkottySlide::load(SkScalar, SkScalar) {
20 auto stream = SkStream::MakeFromFile(fPath.c_str());
21 fAnimation = skotty::Animation::Make(stream.get());
22 fTimeBase = 0; // force a time reset
23
24 if (fAnimation) {
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 SkottySlide::unload() {
36 fAnimation.reset();
37}
38
39SkISize SkottySlide::getDimensions() const {
40 return fAnimation? fAnimation->size().toCeil() : SkISize::Make(0, 0);
41}
42
43void SkottySlide::draw(SkCanvas* canvas) {
44 if (fAnimation) {
45 fAnimation->render(canvas);
46 }
47}
48
49bool SkottySlide::animate(const SkAnimTimer& timer) {
50 if (fTimeBase == 0) {
51 // Reset the animation time.
52 fTimeBase = timer.msec();
53 }
54
55 if (fAnimation) {
56 auto t = timer.msec() - fTimeBase;
57 fAnimation->animationTick(t);
58 }
59 return true;
60}
61
62bool SkottySlide::onChar(SkUnichar c) {
63 switch (c) {
64 case 'I':
65 if (fAnimation) {
66 fShowAnimationInval = !fShowAnimationInval;
67 fAnimation->setShowInval(fShowAnimationInval);
68 }
69 break;
70 default:
71 break;
72 }
73
74 return INHERITED::onChar(c);
75}