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;
18 struct SkPoint;
19 
20 namespace sksg {
21 
22 class RenderNode;
23 
24 /**
25  * Base class for animators.
26  *
27  */
28 class Animator {
29 public:
30     virtual ~Animator();
31     Animator(const Animator&) = delete;
32     Animator& operator=(const Animator&) = delete;
33 
34     void tick(float t);
35 
36 protected:
37     Animator();
38 
39     virtual void onTick(float t) = 0;
40 };
41 
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 
56 /**
57  * Holds a scene root and a list of animators.
58  *
59  * Provides high-level mehods for driving rendering and animations.
60  *
61  */
62 class Scene final {
63 public:
64     static std::unique_ptr<Scene> Make(sk_sp<RenderNode> root, AnimatorList&& animators);
65     ~Scene();
66     Scene(const Scene&) = delete;
67     Scene& operator=(const Scene&) = delete;
68 
69     void render(SkCanvas*) const;
70     void animate(float t);
71     const RenderNode* nodeAt(const SkPoint&) const;
72 
setShowInval(bool show)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;
82 };
83 
84 } // namespace sksg
85 
86 #endif // SkSGScene_DEFINED
87