2017-12-30 17:27:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
#include "SkottieSlide.h"
|
2017-12-30 17:27:00 +00:00
|
|
|
|
|
|
|
#include "SkAnimTimer.h"
|
2018-01-02 19:37:37 +00:00
|
|
|
#include "SkCanvas.h"
|
2018-01-16 22:04:30 +00:00
|
|
|
#include "Skottie.h"
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
SkottieSlide::SkottieSlide(const SkString& name, const SkString& path)
|
2017-12-30 17:27:00 +00:00
|
|
|
: fPath(path) {
|
|
|
|
fName = name;
|
|
|
|
}
|
|
|
|
|
2018-02-09 16:15:32 +00:00
|
|
|
void SkottieSlide::load(SkScalar w, SkScalar h) {
|
2018-02-23 16:10:22 +00:00
|
|
|
fAnimation = skottie::Animation::MakeFromFile(fPath.c_str());
|
|
|
|
fWinSize = SkSize::Make(w, h);
|
|
|
|
fTimeBase = 0; // force a time reset
|
2017-12-30 17:27:00 +00:00
|
|
|
|
|
|
|
if (fAnimation) {
|
2018-01-09 20:51:21 +00:00
|
|
|
fAnimation->setShowInval(fShowAnimationInval);
|
2017-12-30 17:27:00 +00:00
|
|
|
SkDebugf("loaded Bodymovin animation v: %s, size: [%f %f], fr: %f\n",
|
|
|
|
fAnimation->version().c_str(),
|
|
|
|
fAnimation->size().width(),
|
|
|
|
fAnimation->size().height(),
|
|
|
|
fAnimation->frameRate());
|
|
|
|
} else {
|
|
|
|
SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
void SkottieSlide::unload() {
|
2017-12-30 17:27:00 +00:00
|
|
|
fAnimation.reset();
|
|
|
|
}
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
SkISize SkottieSlide::getDimensions() const {
|
2018-02-09 16:15:32 +00:00
|
|
|
// We always scale to fill the window.
|
|
|
|
return fWinSize.toCeil();
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
void SkottieSlide::draw(SkCanvas* canvas) {
|
2017-12-30 17:27:00 +00:00
|
|
|
if (fAnimation) {
|
2018-01-02 19:37:37 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
2018-02-09 16:15:32 +00:00
|
|
|
const auto dstR = SkRect::MakeSize(fWinSize);
|
2018-01-08 13:25:27 +00:00
|
|
|
fAnimation->render(canvas, &dstR);
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
bool SkottieSlide::animate(const SkAnimTimer& timer) {
|
2017-12-30 17:27:00 +00:00
|
|
|
if (fTimeBase == 0) {
|
|
|
|
// Reset the animation time.
|
|
|
|
fTimeBase = timer.msec();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fAnimation) {
|
|
|
|
auto t = timer.msec() - fTimeBase;
|
|
|
|
fAnimation->animationTick(t);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
bool SkottieSlide::onChar(SkUnichar c) {
|
2017-12-30 17:27:00 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'I':
|
|
|
|
if (fAnimation) {
|
|
|
|
fShowAnimationInval = !fShowAnimationInval;
|
|
|
|
fAnimation->setShowInval(fShowAnimationInval);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return INHERITED::onChar(c);
|
|
|
|
}
|
2018-02-20 21:49:20 +00:00
|
|
|
|
|
|
|
bool SkottieSlide::onMouse(SkScalar x, SkScalar y, sk_app::Window::InputState state, uint32_t) {
|
|
|
|
switch (state) {
|
|
|
|
case sk_app::Window::kUp_InputState:
|
|
|
|
fShowAnimationInval = !fShowAnimationInval;
|
|
|
|
fAnimation->setShowInval(fShowAnimationInval);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-02-21 18:03:41 +00:00
|
|
|
return false;
|
2018-02-20 21:49:20 +00:00
|
|
|
}
|