Home | History | Annotate | Download | only in skottie
      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 
     22 namespace sksg {
     23 class Color;
     24 class Gradient;
     25 class LinearGradient;
     26 class Matrix;
     27 class Path;
     28 class RadialGradient;
     29 class RRect;
     30 class RenderNode;;
     31 }
     32 
     33 namespace Json { class Value; }
     34 
     35 namespace  skottie {
     36 
     37 template <typename T>
     38 struct ValueTraits {
     39     static size_t Cardinality(const T&);
     40 
     41     template <typename U>
     42     static U As(const T&);
     43 };
     44 
     45 using ScalarValue = SkScalar;
     46 using VectorValue = std::vector<ScalarValue>;
     47 using 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 
     61 class CompositeRRect final : public SkRefCnt {
     62 public:
     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 
     69 private:
     70     void apply();
     71 
     72     sk_sp<sksg::RRect> fRRectNode;
     73 
     74     using INHERITED = SkRefCnt;
     75 };
     76 
     77 class CompositePolyStar final : public SkRefCnt {
     78 public:
     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 
     93 private:
     94     void apply();
     95 
     96     sk_sp<sksg::Path> fPathNode;
     97     Type              fType;
     98 
     99     using INHERITED = SkRefCnt;
    100 };
    101 
    102 class CompositeTransform final : public SkRefCnt {
    103 public:
    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 
    113 private:
    114     void apply();
    115 
    116     sk_sp<sksg::Matrix> fMatrixNode;
    117 
    118     using INHERITED = SkRefCnt;
    119 };
    120 
    121 class CompositeGradient : public SkRefCnt {
    122 public:
    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 
    127 protected:
    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 
    138 private:
    139     void apply();
    140 
    141     using INHERITED = SkRefCnt;
    142 };
    143 
    144 class CompositeLinearGradient final : public CompositeGradient {
    145 public:
    146     CompositeLinearGradient(sk_sp<sksg::LinearGradient>, size_t stopCount);
    147 
    148 private:
    149     void onApply() override;
    150 
    151     using INHERITED = CompositeGradient;
    152 };
    153 
    154 class CompositeRadialGradient final : public CompositeGradient {
    155 public:
    156     CompositeRadialGradient(sk_sp<sksg::RadialGradient>, size_t stopCount);
    157 
    158 private:
    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