You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.7 KiB

/*
* Copyright 2020 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "tools/viewer/SkRiveSlide.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkStream.h"
#if defined(SK_ENABLE_SKRIVE)
SkRiveSlide::SkRiveSlide(const SkString& name, const SkString& path)
: fPath(path) {
fName = name;
}
SkRiveSlide::~SkRiveSlide() = default;
void SkRiveSlide::load(SkScalar w, SkScalar h) {
fWinSize = {w , h};
fRive = skrive::SkRive::Builder().make(SkFILEStream::Make(fPath.c_str()));
fRiveBounds = SkRect::MakeEmpty();
if (fRive) {
SkDebugf("Loaded Rive animation: %zu artboards\n", fRive->artboards().size());
for (const auto& ab : fRive->artboards()) {
const auto& pos = ab->getTranslation();
const auto& size = ab->getSize();
fRiveBounds.join(SkRect::MakeXYWH(pos.x, pos.y, size.x, size.y));
}
} else {
SkDebugf("Failed to load Rive animation: %s\n", fPath.c_str());
}
}
void SkRiveSlide::unload() {
fRive.reset();
}
void SkRiveSlide::resize(SkScalar w, SkScalar h) {
fWinSize = {w , h};
}
SkISize SkRiveSlide::getDimensions() const {
// We always scale to fill the window.
return fWinSize.toCeil();
}
void SkRiveSlide::draw(SkCanvas* canvas) {
if (!fRive) {
return;
}
// Scale the Rive artboards to fill our window.
SkAutoCanvasRestore acr(canvas, true);
canvas->concat(SkMatrix::RectToRect(fRiveBounds, SkRect::MakeSize(fWinSize),
SkMatrix::kCenter_ScaleToFit));
for (const auto& ab : fRive->artboards()) {
ab->render(canvas);
}
}
#endif // defined(SK_ENABLE_SKRIVE)