1/*
2 * Copyright 2017 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 SkottieProperties_DEFINED
9#define SkottieProperties_DEFINED
10
11#include "SkColor.h"
12#include "SkPath.h"
13#include "SkPoint.h"
14#include "SkSize.h"
15#include "SkRefCnt.h"
16#include "SkTArray.h"
17#include "SkTypes.h"
18
19#include <memory>
20#include <vector>
21
22namespace sksg {
23class Color;
24class Gradient;
25class LinearGradient;
26class Matrix;
27class Path;
28class RadialGradient;
29class RRect;
30class RenderNode;;
31}
32
33namespace Json { class Value; }
34
35namespace  skottie {
36
37template <typename T>
38struct ValueTraits {
39    static size_t Cardinality(const T&);
40
41    template <typename U>
42    static U As(const T&);
43};
44
45using ScalarValue = SkScalar;
46using VectorValue = std::vector<ScalarValue>;
47using ShapeValue  = SkPath;
48
49// Composite properties.
50
51#define COMPOSITE_PROPERTY(p_name, p_type, p_default) \
52    void set##p_name(const p_type& p) {               \
53        if (p == f##p_name) return;                   \
54        f##p_name = p;                                \
55        this->apply();                                \
56    }                                                 \
57  private:                                            \
58    p_type f##p_name = p_default;                     \
59  public:
60
61class CompositeRRect final : public SkRefCnt {
62public:
63    explicit CompositeRRect(sk_sp<sksg::RRect>);
64
65    COMPOSITE_PROPERTY(Position, SkPoint , SkPoint::Make(0, 0))
66    COMPOSITE_PROPERTY(Size    , SkSize  , SkSize::Make(0, 0))
67    COMPOSITE_PROPERTY(Radius  , SkSize  , SkSize::Make(0, 0))
68
69private:
70    void apply();
71
72    sk_sp<sksg::RRect> fRRectNode;
73
74    using INHERITED = SkRefCnt;
75};
76
77class CompositePolyStar final : public SkRefCnt {
78public:
79    enum class Type {
80        kStar, kPoly,
81    };
82
83    CompositePolyStar(sk_sp<sksg::Path>, Type);
84
85    COMPOSITE_PROPERTY(Position      , SkPoint , SkPoint::Make(0, 0))
86    COMPOSITE_PROPERTY(PointCount    , SkScalar, 0)
87    COMPOSITE_PROPERTY(InnerRadius   , SkScalar, 0)
88    COMPOSITE_PROPERTY(OuterRadius   , SkScalar, 0)
89    COMPOSITE_PROPERTY(InnerRoundness, SkScalar, 0)
90    COMPOSITE_PROPERTY(OuterRoundness, SkScalar, 0)
91    COMPOSITE_PROPERTY(Rotation      , SkScalar, 0)
92
93private:
94    void apply();
95
96    sk_sp<sksg::Path> fPathNode;
97    Type              fType;
98
99    using INHERITED = SkRefCnt;
100};
101
102class CompositeTransform final : public SkRefCnt {
103public:
104    explicit CompositeTransform(sk_sp<sksg::Matrix>);
105
106    COMPOSITE_PROPERTY(AnchorPoint, SkPoint , SkPoint::Make(0, 0))
107    COMPOSITE_PROPERTY(Position   , SkPoint , SkPoint::Make(0, 0))
108    COMPOSITE_PROPERTY(Scale      , SkVector, SkPoint::Make(100, 100))
109    COMPOSITE_PROPERTY(Rotation   , SkScalar, 0)
110    COMPOSITE_PROPERTY(Skew       , SkScalar, 0)
111    COMPOSITE_PROPERTY(SkewAxis   , SkScalar, 0)
112
113private:
114    void apply();
115
116    sk_sp<sksg::Matrix> fMatrixNode;
117
118    using INHERITED = SkRefCnt;
119};
120
121class CompositeGradient : public SkRefCnt {
122public:
123    COMPOSITE_PROPERTY(StartPoint, SkPoint              , SkPoint::Make(0, 0)    )
124    COMPOSITE_PROPERTY(EndPoint  , SkPoint              , SkPoint::Make(0, 0)    )
125    COMPOSITE_PROPERTY(ColorStops, std::vector<SkScalar>, std::vector<SkScalar>())
126
127protected:
128    CompositeGradient(sk_sp<sksg::Gradient>, size_t stopCount);
129
130    const SkPoint& startPoint() const { return fStartPoint; }
131    const SkPoint& endPoint()   const { return fEndPoint;   }
132
133    sk_sp<sksg::Gradient> fGradient;
134    size_t                fStopCount;
135
136    virtual void onApply() = 0;
137
138private:
139    void apply();
140
141    using INHERITED = SkRefCnt;
142};
143
144class CompositeLinearGradient final : public CompositeGradient {
145public:
146    CompositeLinearGradient(sk_sp<sksg::LinearGradient>, size_t stopCount);
147
148private:
149    void onApply() override;
150
151    using INHERITED = CompositeGradient;
152};
153
154class CompositeRadialGradient final : public CompositeGradient {
155public:
156    CompositeRadialGradient(sk_sp<sksg::RadialGradient>, size_t stopCount);
157
158private:
159    void onApply() override;
160
161    using INHERITED = CompositeGradient;
162};
163
164#undef COMPOSITE_PROPERTY
165
166} // namespace skottie
167
168#endif // SkottieProperties_DEFINED
169