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" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 13 | |
| 14 | SkottySlide::SkottySlide(const SkString& name, const SkString& path) |
| 15 | : fPath(path) { |
| 16 | fName = name; |
| 17 | } |
| 18 | |
| 19 | void SkottySlide::load(SkScalar, SkScalar) { |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 20 | fAnimation = skotty::Animation::MakeFromFile(fPath.c_str()); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 21 | 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 | |
| 34 | void SkottySlide::unload() { |
| 35 | fAnimation.reset(); |
| 36 | } |
| 37 | |
| 38 | SkISize SkottySlide::getDimensions() const { |
| 39 | return fAnimation? fAnimation->size().toCeil() : SkISize::Make(0, 0); |
| 40 | } |
| 41 | |
| 42 | void SkottySlide::draw(SkCanvas* canvas) { |
| 43 | if (fAnimation) { |
Florin Malita | aa4dc62 | 2018-01-02 14:37:37 -0500 | [diff] [blame] | 44 | SkAutoCanvasRestore acr(canvas, true); |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 45 | const SkRect dstR = SkRect::Make(canvas->imageInfo().bounds()); |
| 46 | fAnimation->render(canvas, &dstR); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
| 50 | bool 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 | |
| 63 | bool 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 | } |