blob: 063330e925cf57c7c526f863b99d45477d859b49 [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
Florin Malita54f65c42018-01-16 17:04:30 -05008#include "SkottieSlide.h"
Florin Malita094ccde2017-12-30 12:27:00 -05009
10#include "SkAnimTimer.h"
Florin Malitaaa4dc622018-01-02 14:37:37 -050011#include "SkCanvas.h"
Florin Malita54f65c42018-01-16 17:04:30 -050012#include "Skottie.h"
Florin Malita094ccde2017-12-30 12:27:00 -050013
Florin Malita54f65c42018-01-16 17:04:30 -050014SkottieSlide::SkottieSlide(const SkString& name, const SkString& path)
Florin Malita094ccde2017-12-30 12:27:00 -050015 : fPath(path) {
16 fName = name;
17}
18
Florin Malitac378fdc2018-02-09 11:15:32 -050019void SkottieSlide::load(SkScalar w, SkScalar h) {
Florin Malita54f65c42018-01-16 17:04:30 -050020 fAnimation = skottie::Animation::MakeFromFile(fPath.c_str());
Florin Malitac378fdc2018-02-09 11:15:32 -050021 fWinSize = SkSize::Make(w, h);
Florin Malita094ccde2017-12-30 12:27:00 -050022 fTimeBase = 0; // force a time reset
23
24 if (fAnimation) {
Florin Malitadf2713c2018-01-09 15:51:21 -050025 fAnimation->setShowInval(fShowAnimationInval);
Florin Malita094ccde2017-12-30 12:27:00 -050026 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
Florin Malita54f65c42018-01-16 17:04:30 -050036void SkottieSlide::unload() {
Florin Malita094ccde2017-12-30 12:27:00 -050037 fAnimation.reset();
38}
39
Florin Malita54f65c42018-01-16 17:04:30 -050040SkISize SkottieSlide::getDimensions() const {
Florin Malitac378fdc2018-02-09 11:15:32 -050041 // We always scale to fill the window.
42 return fWinSize.toCeil();
Florin Malita094ccde2017-12-30 12:27:00 -050043}
44
Florin Malita54f65c42018-01-16 17:04:30 -050045void SkottieSlide::draw(SkCanvas* canvas) {
Florin Malita094ccde2017-12-30 12:27:00 -050046 if (fAnimation) {
Florin Malitaaa4dc622018-01-02 14:37:37 -050047 SkAutoCanvasRestore acr(canvas, true);
Florin Malitac378fdc2018-02-09 11:15:32 -050048 const auto dstR = SkRect::MakeSize(fWinSize);
Mike Reed29859872018-01-08 08:25:27 -050049 fAnimation->render(canvas, &dstR);
Florin Malita094ccde2017-12-30 12:27:00 -050050 }
51}
52
Florin Malita54f65c42018-01-16 17:04:30 -050053bool SkottieSlide::animate(const SkAnimTimer& timer) {
Florin Malita094ccde2017-12-30 12:27:00 -050054 if (fTimeBase == 0) {
55 // Reset the animation time.
56 fTimeBase = timer.msec();
57 }
58
59 if (fAnimation) {
60 auto t = timer.msec() - fTimeBase;
61 fAnimation->animationTick(t);
62 }
63 return true;
64}
65
Florin Malita54f65c42018-01-16 17:04:30 -050066bool SkottieSlide::onChar(SkUnichar c) {
Florin Malita094ccde2017-12-30 12:27:00 -050067 switch (c) {
68 case 'I':
69 if (fAnimation) {
70 fShowAnimationInval = !fShowAnimationInval;
71 fAnimation->setShowInval(fShowAnimationInval);
72 }
73 break;
74 default:
75 break;
76 }
77
78 return INHERITED::onChar(c);
79}