blob: 432b307a5e15590423a2d07bee77595ca0a4e026 [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"
13#include "SkStream.h"
14
15SkottySlide::SkottySlide(const SkString& name, const SkString& path)
16 : fPath(path) {
17 fName = name;
18}
19
20void SkottySlide::load(SkScalar, SkScalar) {
21 auto stream = SkStream::MakeFromFile(fPath.c_str());
22 fAnimation = skotty::Animation::Make(stream.get());
23 fTimeBase = 0; // force a time reset
24
25 if (fAnimation) {
26 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
36void SkottySlide::unload() {
37 fAnimation.reset();
38}
39
40SkISize SkottySlide::getDimensions() const {
41 return fAnimation? fAnimation->size().toCeil() : SkISize::Make(0, 0);
42}
43
44void SkottySlide::draw(SkCanvas* canvas) {
45 if (fAnimation) {
Florin Malitaaa4dc622018-01-02 14:37:37 -050046 SkAutoCanvasRestore acr(canvas, true);
47 const auto animationBounds = SkRect::Make(fAnimation->size().toCeil());
48 canvas->concat(SkMatrix::MakeRectToRect(animationBounds,
49 SkRect::Make(canvas->imageInfo().bounds()),
50 SkMatrix::kCenter_ScaleToFit));
51 canvas->clipRect(animationBounds);
Florin Malita094ccde2017-12-30 12:27:00 -050052 fAnimation->render(canvas);
53 }
54}
55
56bool SkottySlide::animate(const SkAnimTimer& timer) {
57 if (fTimeBase == 0) {
58 // Reset the animation time.
59 fTimeBase = timer.msec();
60 }
61
62 if (fAnimation) {
63 auto t = timer.msec() - fTimeBase;
64 fAnimation->animationTick(t);
65 }
66 return true;
67}
68
69bool SkottySlide::onChar(SkUnichar c) {
70 switch (c) {
71 case 'I':
72 if (fAnimation) {
73 fShowAnimationInval = !fShowAnimationInval;
74 fAnimation->setShowInval(fShowAnimationInval);
75 }
76 break;
77 default:
78 break;
79 }
80
81 return INHERITED::onChar(c);
82}