aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/viewer/SkottieSlide2.cpp
blob: 7a9854031dc3a399f38771d09df74afdb291e720 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkottieSlide.h"

#include "SkAnimTimer.h"
#include "SkCanvas.h"
#include "Skottie.h"
#include "SkOSFile.h"
#include "SkOSPath.h"
#include "SkSGGroup.h"
#include "SkSGRenderNode.h"
#include "SkSGScene.h"
#include "SkSGTransform.h"
#include "SkStream.h"

static constexpr int CELL_WIDTH  = 240;
static constexpr int CELL_HEIGHT = 160;
static constexpr int COL_COUNT   = 4;
static constexpr int SPACER_X    = 12;
static constexpr int SPACER_Y    = 24;
static constexpr int MARGIN      = 8;

class SkottieSlide2::AnimationWrapper final : public sksg::RenderNode {
public:
    explicit AnimationWrapper(std::unique_ptr<skottie::Animation> anim)
        : fAnimation(std::move(anim)) {
        SkASSERT(fAnimation);
    }

    void tick(SkMSec t) {
        fAnimation->animationTick(t);
        this->invalidate();
    }

    void setShowInval(bool show) { fAnimation->setShowInval(show); }

protected:
    SkRect onRevalidate(sksg::InvalidationController* ic, const SkMatrix& ctm) override {
        return SkRect::MakeSize(fAnimation->size());
    }

    void onRender(SkCanvas* canvas) const override {
        fAnimation->render(canvas);
    }

private:
    const std::unique_ptr<skottie::Animation> fAnimation;

    using INHERITED = sksg::RenderNode;
};

SkottieSlide2::Rec::Rec(sk_sp<AnimationWrapper> wrapper)
    : fWrapper(std::move(wrapper)) {}

SkottieSlide2::Rec::Rec(Rec&& o) = default;

SkottieSlide2::SkottieSlide2(const SkString& path)
    : fPath(path)
{
    fName.set("skottie-dir");
}

void SkottieSlide2::load(SkScalar, SkScalar) {
    SkString name;
    SkOSFile::Iter iter(fPath.c_str(), "json");

    int x = 0, y = 0;

    // Build a global scene using tranformed animation fragments:
    //
    // [Group]
    //     [Transform]
    //         [AnimationWrapper]
    //     [Transform]
    //         [AnimationWrapper]
    //     ...
    //
    // Note: for now animation wrappers are also tracked externally in fAnims, for tick dispatching.
    auto scene_root = sksg::Group::Make();

    while (iter.next(&name)) {
        SkString path = SkOSPath::Join(fPath.c_str(), name.c_str());
        if (auto anim  = skottie::Animation::MakeFromFile(path.c_str())) {
            const SkRect src = SkRect::MakeSize(anim->size()),
                         dst = SkRect::MakeXYWH(MARGIN + x * (CELL_WIDTH + SPACER_X),
                                                MARGIN + y * (CELL_HEIGHT + SPACER_Y),
                                                CELL_WIDTH, CELL_HEIGHT);
            auto wrapper = sk_make_sp<AnimationWrapper>(std::move(anim));
            auto matrix  = sksg::Matrix::Make(
                SkMatrix::MakeRectToRect(src, dst, SkMatrix::kCenter_ScaleToFit));
            auto xform   = sksg::Transform::Make(wrapper, std::move(matrix));

            scene_root->addChild(xform);
            fAnims.emplace_back(std::move(wrapper)).fName = name;

            if (++x == COL_COUNT) {
                x = 0;
                y += 1;
            }
        }
    }

    fScene = sksg::Scene::Make(std::move(scene_root), sksg::AnimatorList());
}

void SkottieSlide2::unload() {
    fAnims.reset();
    fScene.reset();
}

SkISize SkottieSlide2::getDimensions() const {
    const int rows = (fAnims.count() + COL_COUNT - 1) / COL_COUNT;
    return {
        MARGIN + (COL_COUNT - 1) * SPACER_X + COL_COUNT * CELL_WIDTH + MARGIN,
        MARGIN + (rows - 1) * SPACER_Y + rows * CELL_HEIGHT + MARGIN,
    };
}

void SkottieSlide2::draw(SkCanvas* canvas) {
    fScene->render(canvas);

    // TODO: this is all only to draw labels; replace with sksg::Text nodes, when available.
    SkPaint paint;
    paint.setTextSize(12);
    paint.setAntiAlias(true);
    paint.setTextAlign(SkPaint::kCenter_Align);

    const SkRect dst = SkRect::MakeIWH(CELL_WIDTH, CELL_HEIGHT);
    int x = 0, y = 0;

    canvas->translate(MARGIN, MARGIN);
    for (const auto& rec : fAnims) {
        SkAutoCanvasRestore acr(canvas, true);
        canvas->translate(x * (CELL_WIDTH + SPACER_X), y * (CELL_HEIGHT + SPACER_Y));
        canvas->drawText(rec.fName.c_str(), rec.fName.size(),
                         dst.centerX(), dst.bottom() + paint.getTextSize(), paint);
        if (++x == COL_COUNT) {
            x = 0;
            y += 1;
        }
    }
}

bool SkottieSlide2::animate(const SkAnimTimer& timer) {
    for (auto& rec : fAnims) {
        if (rec.fTimeBase == 0) {
            // Reset the animation time.
            rec.fTimeBase = timer.msec();
        }
        rec.fWrapper->tick(timer.msec() - rec.fTimeBase);
    }
    return true;
}

bool SkottieSlide2::onMouse(SkScalar x, SkScalar y, sk_app::Window::InputState state,
                           uint32_t modifiers) {
    if (fTrackingCell < 0 && state == sk_app::Window::kDown_InputState) {
        fTrackingCell = this->findCell(x, y);
    }
    if (fTrackingCell >= 0 && state == sk_app::Window::kUp_InputState) {
        int index = this->findCell(x, y);
        if (fTrackingCell == index) {
            fAnims[index].fShowAnimationInval = !fAnims[index].fShowAnimationInval;
            fAnims[index].fWrapper->setShowInval(fAnims[index].fShowAnimationInval);
        }
        fTrackingCell = -1;
    }
    return fTrackingCell >= 0;
}

int SkottieSlide2::findCell(float x, float y) const {
    x -= MARGIN;
    y -= MARGIN;
    int index = -1;
    if (x >= 0 && y >= 0) {
        int ix = (int)x;
        int iy = (int)y;
        int col = ix / (CELL_WIDTH + SPACER_X);
        int row = iy / (CELL_HEIGHT + SPACER_Y);
        index = row * COL_COUNT + col;
        if (index >= fAnims.count()) {
            index = -1;
        }
    }
    return index;
}