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 | #ifndef SkSGScene_DEFINED |
| 9 | #define SkSGScene_DEFINED |
| 10 | |
| 11 | #include "SkRefCnt.h" |
| 12 | #include "SkTypes.h" |
| 13 | |
| 14 | #include <memory> |
| 15 | #include <vector> |
| 16 | |
| 17 | class SkCanvas; |
Florin Malita | eb46bd8 | 2019-02-12 09:33:21 -0500 | [diff] [blame] | 18 | struct SkPoint; |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 19 | |
| 20 | namespace sksg { |
| 21 | |
| 22 | class RenderNode; |
| 23 | |
| 24 | /** |
| 25 | * Base class for animators. |
| 26 | * |
| 27 | */ |
Ben Wagner | d5148e3 | 2018-07-16 17:44:06 -0400 | [diff] [blame] | 28 | class Animator { |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 29 | public: |
| 30 | virtual ~Animator(); |
Ben Wagner | d5148e3 | 2018-07-16 17:44:06 -0400 | [diff] [blame] | 31 | Animator(const Animator&) = delete; |
| 32 | Animator& operator=(const Animator&) = delete; |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 33 | |
| 34 | void tick(float t); |
| 35 | |
| 36 | protected: |
| 37 | Animator(); |
| 38 | |
| 39 | virtual void onTick(float t) = 0; |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 40 | }; |
| 41 | |
Florin Malita | cca86f3 | 2018-01-29 10:49:49 -0500 | [diff] [blame] | 42 | using AnimatorList = std::vector<std::unique_ptr<Animator>>; |
| 43 | |
| 44 | class GroupAnimator : public Animator { |
| 45 | protected: |
| 46 | explicit GroupAnimator(AnimatorList&&); |
| 47 | |
| 48 | void onTick(float t) override; |
| 49 | |
| 50 | private: |
| 51 | const AnimatorList fAnimators; |
| 52 | |
| 53 | using INHERITED = Animator; |
| 54 | }; |
| 55 | |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 56 | /** |
| 57 | * Holds a scene root and a list of animators. |
| 58 | * |
| 59 | * Provides high-level mehods for driving rendering and animations. |
| 60 | * |
| 61 | */ |
Ben Wagner | d5148e3 | 2018-07-16 17:44:06 -0400 | [diff] [blame] | 62 | class Scene final { |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 63 | public: |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 64 | static std::unique_ptr<Scene> Make(sk_sp<RenderNode> root, AnimatorList&& animators); |
| 65 | ~Scene(); |
Ben Wagner | d5148e3 | 2018-07-16 17:44:06 -0400 | [diff] [blame] | 66 | Scene(const Scene&) = delete; |
| 67 | Scene& operator=(const Scene&) = delete; |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 68 | |
| 69 | void render(SkCanvas*) const; |
| 70 | void animate(float t); |
Florin Malita | eb46bd8 | 2019-02-12 09:33:21 -0500 | [diff] [blame] | 71 | const RenderNode* nodeAt(const SkPoint&) const; |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 72 | |
| 73 | void setShowInval(bool show) { fShowInval = show; } |
| 74 | |
| 75 | private: |
| 76 | Scene(sk_sp<RenderNode> root, AnimatorList&& animators); |
| 77 | |
| 78 | const sk_sp<RenderNode> fRoot; |
| 79 | const AnimatorList fAnimators; |
| 80 | |
| 81 | bool fShowInval = false; |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | } // namespace sksg |
| 85 | |
| 86 | #endif // SkSGScene_DEFINED |