blob: 9261cf4c96d270f726d16d77327b016ea1bcca96 [file] [log] [blame]
Florin Malita35efaa82018-01-22 12:57:06 -05001/*
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
17class SkCanvas;
Florin Malitaeb46bd82019-02-12 09:33:21 -050018struct SkPoint;
Florin Malita35efaa82018-01-22 12:57:06 -050019
20namespace sksg {
21
22class RenderNode;
23
24/**
25 * Base class for animators.
26 *
27 */
Ben Wagnerd5148e32018-07-16 17:44:06 -040028class Animator {
Florin Malita35efaa82018-01-22 12:57:06 -050029public:
30 virtual ~Animator();
Ben Wagnerd5148e32018-07-16 17:44:06 -040031 Animator(const Animator&) = delete;
32 Animator& operator=(const Animator&) = delete;
Florin Malita35efaa82018-01-22 12:57:06 -050033
34 void tick(float t);
35
36protected:
37 Animator();
38
39 virtual void onTick(float t) = 0;
Florin Malita35efaa82018-01-22 12:57:06 -050040};
41
Florin Malitacca86f32018-01-29 10:49:49 -050042using AnimatorList = std::vector<std::unique_ptr<Animator>>;
43
44class GroupAnimator : public Animator {
45protected:
46 explicit GroupAnimator(AnimatorList&&);
47
48 void onTick(float t) override;
49
50private:
51 const AnimatorList fAnimators;
52
53 using INHERITED = Animator;
54};
55
Florin Malita35efaa82018-01-22 12:57:06 -050056/**
57 * Holds a scene root and a list of animators.
58 *
59 * Provides high-level mehods for driving rendering and animations.
60 *
61 */
Ben Wagnerd5148e32018-07-16 17:44:06 -040062class Scene final {
Florin Malita35efaa82018-01-22 12:57:06 -050063public:
Florin Malita35efaa82018-01-22 12:57:06 -050064 static std::unique_ptr<Scene> Make(sk_sp<RenderNode> root, AnimatorList&& animators);
65 ~Scene();
Ben Wagnerd5148e32018-07-16 17:44:06 -040066 Scene(const Scene&) = delete;
67 Scene& operator=(const Scene&) = delete;
Florin Malita35efaa82018-01-22 12:57:06 -050068
69 void render(SkCanvas*) const;
70 void animate(float t);
Florin Malitaeb46bd82019-02-12 09:33:21 -050071 const RenderNode* nodeAt(const SkPoint&) const;
Florin Malita35efaa82018-01-22 12:57:06 -050072
73 void setShowInval(bool show) { fShowInval = show; }
74
75private:
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 Malita35efaa82018-01-22 12:57:06 -050082};
83
84} // namespace sksg
85
86#endif // SkSGScene_DEFINED