diff options
author | Florin Malita <fmalita@chromium.org> | 2018-01-31 17:06:59 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2018-01-31 22:36:05 +0000 |
commit | 51012ce332016cca5160b5a63b91a04b6cb56899 (patch) | |
tree | 5ad31e213c9d4adacae77c16c648f98d3f34739b /tools | |
parent | 28a110c68e8c6f96dc699011762d02b4e4c5b8e5 (diff) |
[sksg] Initial text support
Use the new node type for SkottieSlide2 labels.
TBR=
Change-Id: Icd6a4faf1c281bd83a2331c0072d1a6ed71acc09
Reviewed-on: https://skia-review.googlesource.com/102441
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@chromium.org>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/viewer/SkottieSlide.h | 2 | ||||
-rw-r--r-- | tools/viewer/SkottieSlide2.cpp | 89 |
2 files changed, 51 insertions, 40 deletions
diff --git a/tools/viewer/SkottieSlide.h b/tools/viewer/SkottieSlide.h index f573c2ed09..44929d7be5 100644 --- a/tools/viewer/SkottieSlide.h +++ b/tools/viewer/SkottieSlide.h @@ -50,13 +50,13 @@ public: void draw(SkCanvas*) override; bool animate(const SkAnimTimer&) override; bool onMouse(SkScalar x, SkScalar y, sk_app::Window::InputState, uint32_t modifiers) override; + private: class AnimationWrapper; struct Rec { sk_sp<AnimationWrapper> fWrapper; SkMSec fTimeBase = 0; - SkString fName; bool fShowAnimationInval = false; explicit Rec(sk_sp<AnimationWrapper>); diff --git a/tools/viewer/SkottieSlide2.cpp b/tools/viewer/SkottieSlide2.cpp index 7a9854031d..7b2c7c25a1 100644 --- a/tools/viewer/SkottieSlide2.cpp +++ b/tools/viewer/SkottieSlide2.cpp @@ -12,11 +12,17 @@ #include "Skottie.h" #include "SkOSFile.h" #include "SkOSPath.h" +#include "SkSGColor.h" +#include "SkSGDraw.h" #include "SkSGGroup.h" #include "SkSGRenderNode.h" #include "SkSGScene.h" +#include "SkSGText.h" #include "SkSGTransform.h" #include "SkStream.h" +#include "SkTypeface.h" + +#include <cmath> static constexpr int CELL_WIDTH = 240; static constexpr int CELL_HEIGHT = 160; @@ -65,38 +71,64 @@ SkottieSlide2::SkottieSlide2(const SkString& path) fName.set("skottie-dir"); } +// Build a global scene using tranformed animation fragments: +// +// [Group(root)] +// [Transform] +// [Group] +// [AnimationWrapper] +// [Draw] +// [Text] +// [Color] +// [Transform] +// [Group] +// [AnimationWrapper] +// [Draw] +// [Text] +// [Color] +// ... +// +// Note: for now animation wrappers are also tracked externally in fAnims, for tick dispatching. + +static sk_sp<sksg::RenderNode> MakeLabel(const SkString& txt, + const SkRect& src, + const SkMatrix& dstXform) { + auto text = sksg::Text::Make(nullptr, txt); + text->setFlags(SkPaint::kAntiAlias_Flag); + text->setSize(12 / std::sqrt(dstXform.getScaleX() * dstXform.getScaleY())); + text->setAlign(SkPaint::kCenter_Align); + text->setPosition(SkPoint::Make(src.width() / 2, src.height() + text->getSize())); + + return sksg::Draw::Make(std::move(text), sksg::Color::Make(SK_ColorBLACK)); +} + 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); + const auto src = SkRect::MakeSize(anim->size()), + dst = SkRect::MakeXYWH(MARGIN + x * (CELL_WIDTH + SPACER_X), + MARGIN + y * (CELL_HEIGHT + SPACER_Y), + CELL_WIDTH, CELL_HEIGHT); + const auto m = SkMatrix::MakeRectToRect(src, dst, SkMatrix::kCenter_ScaleToFit); + 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)); + + auto group = sksg::Group::Make(); + group->addChild(wrapper); + group->addChild(MakeLabel(name, src, m)); + + auto xform = sksg::Transform::Make(std::move(group), m); scene_root->addChild(xform); - fAnims.emplace_back(std::move(wrapper)).fName = name; + fAnims.emplace_back(std::move(wrapper)); if (++x == COL_COUNT) { x = 0; @@ -123,27 +155,6 @@ SkISize SkottieSlide2::getDimensions() const { 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) { |