Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkSGScene.h" |
| 9 | |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkMatrix.h" |
| 12 | #include "SkPaint.h" |
| 13 | #include "SkSGInvalidationController.h" |
| 14 | #include "SkSGRenderNode.h" |
| 15 | |
| 16 | namespace sksg { |
| 17 | |
| 18 | Animator::Animator() = default; |
| 19 | Animator::~Animator() = default; |
| 20 | |
| 21 | void Animator::tick(float t) { |
| 22 | this->onTick(t); |
| 23 | } |
| 24 | |
Florin Malita | cca86f3 | 2018-01-29 10:49:49 -0500 | [diff] [blame] | 25 | GroupAnimator::GroupAnimator(AnimatorList&& animators) |
| 26 | : fAnimators(std::move(animators)) {} |
| 27 | |
| 28 | void GroupAnimator::onTick(float t) { |
| 29 | for (const auto& a : fAnimators) { |
| 30 | a->tick(t); |
| 31 | } |
| 32 | } |
| 33 | |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 34 | std::unique_ptr<Scene> Scene::Make(sk_sp<RenderNode> root, AnimatorList&& anims) { |
| 35 | return root ? std::unique_ptr<Scene>(new Scene(std::move(root), std::move(anims))) : nullptr; |
| 36 | } |
| 37 | |
| 38 | Scene::Scene(sk_sp<RenderNode> root, AnimatorList&& animators) |
| 39 | : fRoot(std::move(root)) |
| 40 | , fAnimators(std::move(animators)) {} |
| 41 | |
| 42 | Scene::~Scene() = default; |
| 43 | |
| 44 | void Scene::render(SkCanvas* canvas) const { |
Florin Malita | ef26fcb | 2019-02-10 12:54:20 -0500 | [diff] [blame] | 45 | // TODO: externalize the inval controller. |
| 46 | // TODO: relocate the revalidation to tick()? |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 47 | InvalidationController ic; |
Florin Malita | ef26fcb | 2019-02-10 12:54:20 -0500 | [diff] [blame] | 48 | fRoot->revalidate(fShowInval ? &ic : nullptr, SkMatrix::I()); |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 49 | fRoot->render(canvas); |
| 50 | |
| 51 | if (fShowInval) { |
| 52 | SkPaint fill, stroke; |
| 53 | fill.setAntiAlias(true); |
| 54 | fill.setColor(0x40ff0000); |
| 55 | stroke.setAntiAlias(true); |
| 56 | stroke.setColor(0xffff0000); |
| 57 | stroke.setStyle(SkPaint::kStroke_Style); |
| 58 | |
| 59 | for (const auto& r : ic) { |
| 60 | canvas->drawRect(r, fill); |
| 61 | canvas->drawRect(r, stroke); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void Scene::animate(float t) { |
| 67 | for (const auto& anim : fAnimators) { |
| 68 | anim->tick(t); |
| 69 | } |
| 70 | } |
| 71 | |
Florin Malita | eb46bd8 | 2019-02-12 09:33:21 -0500 | [diff] [blame] | 72 | const RenderNode* Scene::nodeAt(const SkPoint& p) const { |
| 73 | return fRoot->nodeAt(p); |
| 74 | } |
| 75 | |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 76 | } // namespace sksg |