Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 1 | /* |
| 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 Malita | aa4dc62 | 2018-01-02 14:37:37 -0500 | [diff] [blame^] | 11 | #include "SkCanvas.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 12 | #include "Skotty.h" |
| 13 | #include "SkStream.h" |
| 14 | |
| 15 | SkottySlide::SkottySlide(const SkString& name, const SkString& path) |
| 16 | : fPath(path) { |
| 17 | fName = name; |
| 18 | } |
| 19 | |
| 20 | void 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 | |
| 36 | void SkottySlide::unload() { |
| 37 | fAnimation.reset(); |
| 38 | } |
| 39 | |
| 40 | SkISize SkottySlide::getDimensions() const { |
| 41 | return fAnimation? fAnimation->size().toCeil() : SkISize::Make(0, 0); |
| 42 | } |
| 43 | |
| 44 | void SkottySlide::draw(SkCanvas* canvas) { |
| 45 | if (fAnimation) { |
Florin Malita | aa4dc62 | 2018-01-02 14:37:37 -0500 | [diff] [blame^] | 46 | 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 Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 52 | fAnimation->render(canvas); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | bool 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 | |
| 69 | bool 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 | } |