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