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