Home | History | Annotate | Download | only in include
      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 SkottieProperty_DEFINED
      9 #define SkottieProperty_DEFINED
     10 
     11 #include "SkColor.h"
     12 #include "SkPoint.h"
     13 #include "SkRefCnt.h"
     14 
     15 #include <functional>
     16 
     17 class SkMatrix;
     18 
     19 namespace sksg {
     20 
     21 class Color;
     22 class OpacityEffect;
     23 
     24 } // namespace sksg
     25 
     26 namespace skottie {
     27 
     28 using ColorPropertyValue   = SkColor;
     29 using OpacityPropertyValue = float;
     30 
     31 struct TransformPropertyValue {
     32     SkPoint  fAnchorPoint,
     33              fPosition;
     34     SkVector fScale;
     35     SkScalar fRotation,
     36              fSkew,
     37              fSkewAxis;
     38 
     39     bool operator==(const TransformPropertyValue& other) const;
     40     bool operator!=(const TransformPropertyValue& other) const;
     41 };
     42 
     43 namespace internal { class AnimationBuilder; }
     44 
     45 /**
     46  * Property handles are adapters between user-facing AE model/values
     47  * and the internal scene-graph representation.
     48  */
     49 template <typename ValueT, typename NodeT>
     50 class SK_API PropertyHandle final {
     51 public:
     52     ~PropertyHandle();
     53 
     54     ValueT get() const;
     55     void set(const ValueT&);
     56 
     57 private:
     58     explicit PropertyHandle(sk_sp<NodeT> node) : fNode(std::move(node)) {}
     59 
     60     friend class skottie::internal::AnimationBuilder;
     61 
     62     const sk_sp<NodeT> fNode;
     63 };
     64 
     65 class TransformAdapter2D;
     66 
     67 using ColorPropertyHandle     = PropertyHandle<ColorPropertyValue    , sksg::Color         >;
     68 using OpacityPropertyHandle   = PropertyHandle<OpacityPropertyValue  , sksg::OpacityEffect >;
     69 using TransformPropertyHandle = PropertyHandle<TransformPropertyValue, TransformAdapter2D  >;
     70 
     71 /**
     72  * A PropertyObserver can be used to track and manipulate certain properties of "interesting"
     73  * Lottie nodes.
     74  *
     75  * When registered with an animation builder, PropertyObserver receives notifications for
     76  * various properties of layer and shape nodes.  The |node_name| argument corresponds to the
     77  * name ("nm") node property.
     78  */
     79 class SK_API PropertyObserver : public SkRefCnt {
     80 public:
     81     template <typename T>
     82     using LazyHandle = std::function<std::unique_ptr<T>()>;
     83 
     84     virtual void onColorProperty    (const char node_name[],
     85                                      const LazyHandle<ColorPropertyHandle>&);
     86     virtual void onOpacityProperty  (const char node_name[],
     87                                      const LazyHandle<OpacityPropertyHandle>&);
     88     virtual void onTransformProperty(const char node_name[],
     89                                      const LazyHandle<TransformPropertyHandle>&);
     90 };
     91 
     92 } // namespace skottie
     93 
     94 #endif // SkottieProperty_DEFINED
     95