skia2/tools/skottie_ios_app/SkAnimationDraw.h
Hal Canary efb4ed86cf SkMetalDeviceToGrContext: no longer needs any Skia headers
Also: mv experimental/skottie_ios tools/skottie_ios_app

Motivation:  make using SkMetalViewBridge that much easier for clients.

Change-Id: I985930ae0751d218e89c48c57b69d85ad7a1e703
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/259279
Reviewed-by: Hal Canary <halcanary@google.com>
Commit-Queue: Hal Canary <halcanary@google.com>
2019-12-16 18:09:40 +00:00

62 lines
2.2 KiB
C++

// Copyright 2019 Google LLC.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#ifndef SkAnimationDraw_DEFINED
#define SkAnimationDraw_DEFINED
#include "include/core/SkCanvas.h"
#include "include/core/SkPaint.h"
#include "modules/skottie/include/Skottie.h"
class SkAnimationDraw {
public:
SkAnimationDraw() = default;
~SkAnimationDraw() = default;
explicit operator bool() const { return fAnimation != nullptr; }
void draw(SkSize size, SkCanvas* canvas) {
if (size.width() != fSize.width() || size.height() != fSize.height()) {
// Cache the current matrix; change only if size changes.
if (fAnimationSize.width() > 0 && fAnimationSize.height() > 0) {
float scale = std::min(size.width() / fAnimationSize.width(),
size.height() / fAnimationSize.height());
fMatrix.setScaleTranslate(
scale, scale,
(size.width() - fAnimationSize.width() * scale) * 0.5f,
(size.height() - fAnimationSize.height() * scale) * 0.5f);
} else {
fMatrix = SkMatrix();
}
fSize = size;
}
canvas->concat(fMatrix);
SkRect rect = {0, 0, fAnimationSize.width(), fAnimationSize.height()};
canvas->drawRect(rect, SkPaint(SkColors::kWhite));
fAnimation->render(canvas);
}
void load(const void* data, size_t length) {
skottie::Animation::Builder builder;
fAnimation = builder.make((const char*)data, (size_t)length);
fSize = {0, 0};
fAnimationSize = fAnimation ? fAnimation->size() : SkSize{0, 0};
}
void seek(double time) { if (fAnimation) { fAnimation->seekFrameTime(time, nullptr); } }
float duration() { return fAnimation ? fAnimation->duration() : 0; }
SkSize size() { return fAnimationSize; }
private:
sk_sp<skottie::Animation> fAnimation; // owner
SkSize fSize;
SkSize fAnimationSize;
SkMatrix fMatrix;
SkAnimationDraw(const SkAnimationDraw&) = delete;
SkAnimationDraw& operator=(const SkAnimationDraw&) = delete;
};
#endif // SkAnimationDraw_DEFINED