[skottie] Add a property setter sample/GM
skottie_colorize: press 'c' to cycle through stroke/fill colors.
Change-Id: I7fa9dcd67822df37e97cafabe088c4b9ded9c622
TBR=
Reviewed-on: https://skia-review.googlesource.com/156882
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@chromium.org>
diff --git a/modules/skottie/gm/SkottieGM.cpp b/modules/skottie/gm/SkottieGM.cpp
index f326a12..b99c337 100644
--- a/modules/skottie/gm/SkottieGM.cpp
+++ b/modules/skottie/gm/SkottieGM.cpp
@@ -8,9 +8,14 @@
#include "gm.h"
#include "Resources.h"
#include "SkAnimTimer.h"
+#include "SkColor.h"
#include "Skottie.h"
+#include "SkottieProperty.h"
#include <cmath>
+#include <vector>
+
+using namespace skottie;
namespace {
@@ -18,7 +23,7 @@
static constexpr char kSkottieResource[] = "skottie/skottie_sample_webfont.json";
// Dummy web font loader which serves a single local font (checked in under resources/).
-class FakeWebFontProvider final : public skottie::ResourceProvider {
+class FakeWebFontProvider final : public ResourceProvider {
public:
FakeWebFontProvider() : fFontData(GetResourceAsData(kWebFontResource)) {}
@@ -29,7 +34,7 @@
private:
sk_sp<SkData> fFontData;
- using INHERITED = skottie::ResourceProvider;
+ using INHERITED = ResourceProvider;
};
} // namespace
@@ -47,7 +52,7 @@
void onOnceBeforeDraw() override {
if (auto stream = GetResourceAsStream(kSkottieResource)) {
- fAnimation = skottie::Animation::Builder()
+ fAnimation = Animation::Builder()
.setResourceProvider(sk_make_sp<FakeWebFontProvider>())
.make(stream.get());
}
@@ -75,9 +80,96 @@
private:
static constexpr SkScalar kSize = 800;
- sk_sp<skottie::Animation> fAnimation;
+ sk_sp<Animation> fAnimation;
using INHERITED = skiagm::GM;
};
DEF_GM(return new SkottieWebFontGM;)
+
+class SkottieColorizeGM : public skiagm::GM {
+public:
+protected:
+ SkString onShortName() override {
+ return SkString("skottie_colorize");
+ }
+
+ SkISize onISize() override {
+ return SkISize::Make(kSize, kSize);
+ }
+
+ void onOnceBeforeDraw() override {
+ if (auto stream = GetResourceAsStream("skottie/skottie_sample_search.json")) {
+ fColorizer = sk_make_sp<Colorizer>();
+ fAnimation = Animation::Builder()
+ .setPropertyObserver(fColorizer)
+ .make(stream.get());
+ }
+ }
+
+ void onDraw(SkCanvas* canvas) override {
+ if (!fAnimation) {
+ return;
+ }
+
+ auto dest = SkRect::MakeWH(kSize, kSize);
+ fAnimation->render(canvas, &dest);
+ }
+
+ bool onAnimate(const SkAnimTimer& timer) override {
+ if (!fAnimation) {
+ return false;
+ }
+
+ const auto duration = fAnimation->duration();
+ fAnimation->seek(std::fmod(timer.secs(), duration) / duration);
+ return true;
+ }
+
+protected:
+ bool onHandleKey(SkUnichar uni) override {
+ static constexpr SkColor kColors[] = {
+ SK_ColorBLACK,
+ SK_ColorRED,
+ SK_ColorGREEN,
+ SK_ColorYELLOW,
+ SK_ColorCYAN,
+ };
+
+ if (uni == 'c') {
+ fColorIndex = (fColorIndex + 1) % SK_ARRAY_COUNT(kColors);
+ fColorizer->colorize(kColors[fColorIndex]);
+ return true;
+ }
+
+ return false;
+ }
+
+private:
+ class Colorizer final : public PropertyObserver {
+ public:
+ void onColorProperty(const char node_name[],
+ const PropertyObserver::LazyHandle<ColorPropertyHandle>& lh) override {
+ fColorHandles.push_back(lh());
+ }
+
+ void colorize(SkColor c) {
+ for (const auto& handle : fColorHandles) {
+ handle->setColor(c);
+ }
+ }
+
+ private:
+ std::vector<std::unique_ptr<skottie::ColorPropertyHandle>> fColorHandles;
+ };
+
+ static constexpr SkScalar kSize = 800;
+
+ sk_sp<Animation> fAnimation;
+ sk_sp<Colorizer> fColorizer;
+ size_t fColorIndex = 0;
+
+ using INHERITED = skiagm::GM;
+};
+
+DEF_GM(return new SkottieColorizeGM;)
diff --git a/resources/skottie/skottie_sample_search.json b/resources/skottie/skottie_sample_search.json
new file mode 100644
index 0000000..dddb766
--- /dev/null
+++ b/resources/skottie/skottie_sample_search.json
@@ -0,0 +1,442 @@
+{
+ "assets": [],
+ "ddd": 0,
+ "fr": 24,
+ "h": 24,
+ "ip": 0,
+ "layers": [
+ {
+ "ao": 0,
+ "bm": 0,
+ "ddd": 0,
+ "ind": 1,
+ "ip": 0,
+ "ks": {
+ "a": {
+ "a": 0,
+ "ix": 1,
+ "k": [
+ 0,
+ 0,
+ 0
+ ]
+ },
+ "o": {
+ "a": 0,
+ "ix": 11,
+ "k": 100
+ },
+ "p": {
+ "a": 0,
+ "ix": 2,
+ "k": [
+ 10.375,
+ 9.875,
+ 0
+ ]
+ },
+ "r": {
+ "a": 0,
+ "ix": 10,
+ "k": 0
+ },
+ "s": {
+ "a": 0,
+ "ix": 6,
+ "k": [
+ 100,
+ 100,
+ 100
+ ]
+ }
+ },
+ "nm": "Shape Layer 1",
+ "op": 54,
+ "shapes": [
+ {
+ "cix": 2,
+ "hd": false,
+ "it": [
+ {
+ "hd": false,
+ "ind": 0,
+ "ix": 1,
+ "ks": {
+ "a": 0,
+ "ix": 2,
+ "k": {
+ "c": false,
+ "i": [
+ [
+ 0,
+ 0
+ ],
+ [
+ 0,
+ 0
+ ]
+ ],
+ "o": [
+ [
+ 0,
+ 0
+ ],
+ [
+ 0,
+ 0
+ ]
+ ],
+ "v": [
+ [
+ 3.75,
+ 5
+ ],
+ [
+ 10.75,
+ 12
+ ]
+ ]
+ }
+ },
+ "mn": "ADBE Vector Shape - Group",
+ "nm": "Path 1",
+ "ty": "sh"
+ },
+ {
+ "c": {
+ "a": 0,
+ "ix": 3,
+ "k": [
+ 0,
+ 0,
+ 0,
+ 1
+ ]
+ },
+ "hd": false,
+ "lc": 2,
+ "lj": 2,
+ "mn": "ADBE Vector Graphic - Stroke",
+ "nm": "Stroke 1",
+ "o": {
+ "a": 0,
+ "ix": 4,
+ "k": 56
+ },
+ "ty": "st",
+ "w": {
+ "a": 0,
+ "ix": 5,
+ "k": 2
+ }
+ },
+ {
+ "c": {
+ "a": 0,
+ "ix": 4,
+ "k": [
+ 1,
+ 1,
+ 1,
+ 1
+ ]
+ },
+ "hd": false,
+ "mn": "ADBE Vector Graphic - Fill",
+ "nm": "Fill 1",
+ "o": {
+ "a": 0,
+ "ix": 5,
+ "k": 100
+ },
+ "r": 1,
+ "ty": "fl"
+ },
+ {
+ "a": {
+ "a": 0,
+ "ix": 1,
+ "k": [
+ 0,
+ 0
+ ]
+ },
+ "nm": "Transform",
+ "o": {
+ "a": 0,
+ "ix": 7,
+ "k": 100
+ },
+ "p": {
+ "a": 0,
+ "ix": 2,
+ "k": [
+ 0,
+ 0
+ ]
+ },
+ "r": {
+ "a": 0,
+ "ix": 6,
+ "k": 0
+ },
+ "s": {
+ "a": 0,
+ "ix": 3,
+ "k": [
+ 100,
+ 100
+ ]
+ },
+ "sa": {
+ "a": 0,
+ "ix": 5,
+ "k": 0
+ },
+ "sk": {
+ "a": 0,
+ "ix": 4,
+ "k": 0
+ },
+ "ty": "tr"
+ }
+ ],
+ "ix": 1,
+ "mn": "ADBE Vector Group",
+ "nm": "Shape 1",
+ "np": 3,
+ "ty": "gr"
+ },
+ {
+ "cix": 2,
+ "hd": false,
+ "it": [
+ {
+ "d": 1,
+ "hd": false,
+ "mn": "ADBE Vector Shape - Ellipse",
+ "nm": "Ellipse Path 1",
+ "p": {
+ "a": 0,
+ "ix": 3,
+ "k": [
+ 0,
+ 0
+ ]
+ },
+ "s": {
+ "a": 0,
+ "ix": 2,
+ "k": [
+ 13.992,
+ 13.992
+ ]
+ },
+ "ty": "el"
+ },
+ {
+ "c": {
+ "a": 0,
+ "ix": 3,
+ "k": [
+ 0,
+ 0,
+ 0,
+ 1
+ ]
+ },
+ "hd": false,
+ "lc": 1,
+ "lj": 2,
+ "mn": "ADBE Vector Graphic - Stroke",
+ "nm": "Stroke 1",
+ "o": {
+ "a": 0,
+ "ix": 4,
+ "k": 56
+ },
+ "ty": "st",
+ "w": {
+ "a": 0,
+ "ix": 5,
+ "k": 2
+ }
+ },
+ {
+ "a": {
+ "a": 0,
+ "ix": 1,
+ "k": [
+ 0,
+ 0
+ ]
+ },
+ "nm": "Transform",
+ "o": {
+ "a": 0,
+ "ix": 7,
+ "k": 100
+ },
+ "p": {
+ "a": 0,
+ "ix": 2,
+ "k": [
+ -0.504,
+ -0.254
+ ]
+ },
+ "r": {
+ "a": 0,
+ "ix": 6,
+ "k": 0
+ },
+ "s": {
+ "a": 0,
+ "ix": 3,
+ "k": [
+ 100,
+ 100
+ ]
+ },
+ "sa": {
+ "a": 0,
+ "ix": 5,
+ "k": 0
+ },
+ "sk": {
+ "a": 0,
+ "ix": 4,
+ "k": 0
+ },
+ "ty": "tr"
+ }
+ ],
+ "ix": 2,
+ "mn": "ADBE Vector Group",
+ "nm": "Ellipse 1",
+ "np": 2,
+ "ty": "gr"
+ },
+ {
+ "e": {
+ "a": 0,
+ "ix": 2,
+ "k": 100
+ },
+ "hd": false,
+ "ix": 3,
+ "m": 1,
+ "mn": "ADBE Vector Filter - Trim",
+ "nm": "Trim Paths 1",
+ "o": {
+ "a": 0,
+ "ix": 3,
+ "k": 0
+ },
+ "s": {
+ "a": 1,
+ "ix": 1,
+ "k": [
+ {
+ "e": [
+ 100
+ ],
+ "i": {
+ "x": [
+ 0.58
+ ],
+ "y": [
+ 1
+ ]
+ },
+ "n": [
+ "0p58_1_0p42_0"
+ ],
+ "o": {
+ "x": [
+ 0.42
+ ],
+ "y": [
+ 0
+ ]
+ },
+ "s": [
+ 0
+ ],
+ "t": 0
+ },
+ {
+ "e": [
+ 100
+ ],
+ "i": {
+ "x": [
+ 0.833
+ ],
+ "y": [
+ 1
+ ]
+ },
+ "n": [
+ "0p833_1_0p42_0"
+ ],
+ "o": {
+ "x": [
+ 0.42
+ ],
+ "y": [
+ 0
+ ]
+ },
+ "s": [
+ 100
+ ],
+ "t": 12
+ },
+ {
+ "e": [
+ 0
+ ],
+ "i": {
+ "x": [
+ 0.833
+ ],
+ "y": [
+ 1
+ ]
+ },
+ "n": [
+ "0p833_1_0p167_0"
+ ],
+ "o": {
+ "x": [
+ 0.167
+ ],
+ "y": [
+ 0
+ ]
+ },
+ "s": [
+ 100
+ ],
+ "t": 24
+ },
+ {
+ "t": 36
+ }
+ ]
+ },
+ "ty": "tm"
+ }
+ ],
+ "sr": 1,
+ "st": 0,
+ "ty": 4
+ }
+ ],
+ "markers": [],
+ "nm": "search",
+ "op": 54,
+ "v": "5.0.4",
+ "w": 24
+}