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
|
|
|
|
2018-05-26 13:49:28 +00:00
|
|
|
#if defined(SK_ENABLE_SKOTTIE)
|
|
|
|
|
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-05-29 17:46:54 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2018-08-23 00:37:04 +00:00
|
|
|
static void draw_stats_box(SkCanvas* canvas, const skottie::Animation::Builder::Stats& stats) {
|
2018-04-30 14:32:18 +00:00
|
|
|
static constexpr SkRect kR = { 10, 10, 280, 120 };
|
|
|
|
static constexpr SkScalar kTextSize = 20;
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(0xffeeeeee);
|
|
|
|
paint.setTextSize(kTextSize);
|
|
|
|
|
|
|
|
canvas->drawRect(kR, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
|
|
|
|
const auto json_size = SkStringPrintf("Json size: %lu bytes",
|
|
|
|
stats.fJsonSize);
|
|
|
|
canvas->drawText(json_size.c_str(),
|
|
|
|
json_size.size(), kR.x() + 10, kR.y() + kTextSize * 1, paint);
|
|
|
|
const auto animator_count = SkStringPrintf("Animator count: %lu",
|
|
|
|
stats.fAnimatorCount);
|
|
|
|
canvas->drawText(animator_count.c_str(),
|
|
|
|
animator_count.size(), kR.x() + 10, kR.y() + kTextSize * 2, paint);
|
|
|
|
const auto json_parse_time = SkStringPrintf("Json parse time: %.3f ms",
|
|
|
|
stats.fJsonParseTimeMS);
|
|
|
|
canvas->drawText(json_parse_time.c_str(),
|
|
|
|
json_parse_time.size(), kR.x() + 10, kR.y() + kTextSize * 3, paint);
|
|
|
|
const auto scene_parse_time = SkStringPrintf("Scene build time: %.3f ms",
|
|
|
|
stats.fSceneParseTimeMS);
|
|
|
|
canvas->drawText(scene_parse_time.c_str(),
|
|
|
|
scene_parse_time.size(), kR.x() + 10, kR.y() + kTextSize * 4, paint);
|
|
|
|
const auto total_load_time = SkStringPrintf("Total load time: %.3f ms",
|
|
|
|
stats.fTotalLoadTimeMS);
|
|
|
|
canvas->drawText(total_load_time.c_str(),
|
|
|
|
total_load_time.size(), kR.x() + 10, kR.y() + kTextSize * 5, paint);
|
|
|
|
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawRect(kR, paint);
|
|
|
|
}
|
|
|
|
|
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-08-23 00:37:04 +00:00
|
|
|
skottie::Animation::Builder builder;
|
|
|
|
fAnimation = builder.makeFromFile(fPath.c_str());
|
|
|
|
fAnimationStats = builder.getStats();
|
|
|
|
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);
|
2018-05-31 20:45:29 +00:00
|
|
|
SkDebugf("loaded Bodymovin animation v: %s, size: [%f %f]\n",
|
2017-12-30 17:27:00 +00:00
|
|
|
fAnimation->version().c_str(),
|
|
|
|
fAnimation->size().width(),
|
2018-05-31 20:45:29 +00:00
|
|
|
fAnimation->size().height());
|
2017-12-30 17:27:00 +00:00
|
|
|
} 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);
|
2018-04-30 14:32:18 +00:00
|
|
|
|
|
|
|
if (fShowAnimationStats) {
|
|
|
|
draw_stats_box(canvas, fAnimationStats);
|
|
|
|
}
|
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) {
|
2018-05-29 17:46:54 +00:00
|
|
|
const auto t = timer.msec() - fTimeBase;
|
|
|
|
const auto d = fAnimation->duration() * 1000;
|
|
|
|
fAnimation->seek(std::fmod(t, d) / d);
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
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':
|
2018-04-30 14:32:18 +00:00
|
|
|
fShowAnimationStats = !fShowAnimationStats;
|
2017-12-30 17:27:00 +00:00
|
|
|
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;
|
2018-04-30 14:32:18 +00:00
|
|
|
fShowAnimationStats = !fShowAnimationStats;
|
2018-02-20 21:49:20 +00:00
|
|
|
fAnimation->setShowInval(fShowAnimationInval);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-02-21 18:03:41 +00:00
|
|
|
return false;
|
2018-02-20 21:49:20 +00:00
|
|
|
}
|
2018-05-26 13:49:28 +00:00
|
|
|
|
|
|
|
#endif // SK_ENABLE_SKOTTIE
|