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 "include/core/SkColor.h" 12 #include "include/core/SkPoint.h" 13 #include "include/core/SkRefCnt.h" 14 #include "include/core/SkTypeface.h" 15 #include "include/utils/SkTextUtils.h" 16 #include "modules/skottie/src/text/SkottieShaper.h" 17 18 #include <functional> 19 20 class SkMatrix; 21 22 namespace sksg { 23 24 class Color; 25 class OpacityEffect; 26 27 } // namespace sksg 28 29 namespace skottie { 30 31 using ColorPropertyValue = SkColor; 32 using OpacityPropertyValue = float; 33 34 struct TextPropertyValue { 35 sk_sp<SkTypeface> fTypeface; 36 SkString fText; 37 float fTextSize = 0, 38 fStrokeWidth = 0, 39 fLineHeight = 0, 40 fAscent = 0; 41 SkTextUtils::Align fHAlign = SkTextUtils::kLeft_Align; 42 Shaper::VAlign fVAlign = Shaper::VAlign::kTop; 43 SkRect fBox = SkRect::MakeEmpty(); 44 SkColor fFillColor = SK_ColorTRANSPARENT, 45 fStrokeColor = SK_ColorTRANSPARENT; 46 bool fHasFill : 1, 47 fHasStroke : 1; 48 49 bool operator==(const TextPropertyValue& other) const; 50 bool operator!=(const TextPropertyValue& other) const; 51 }; 52 53 struct TransformPropertyValue { 54 SkPoint fAnchorPoint, 55 fPosition; 56 SkVector fScale; 57 SkScalar fRotation, 58 fSkew, 59 fSkewAxis; 60 61 bool operator==(const TransformPropertyValue& other) const; 62 bool operator!=(const TransformPropertyValue& other) const; 63 }; 64 65 namespace internal { class AnimationBuilder; } 66 67 /** 68 * Property handles are adapters between user-facing AE model/values 69 * and the internal scene-graph representation. 70 */ 71 template <typename ValueT, typename NodeT> 72 class SK_API PropertyHandle final { 73 public: PropertyHandle(sk_sp<NodeT> node)74 explicit PropertyHandle(sk_sp<NodeT> node) : fNode(std::move(node)) {} 75 ~PropertyHandle(); 76 77 ValueT get() const; 78 void set(const ValueT&); 79 80 private: 81 const sk_sp<NodeT> fNode; 82 }; 83 84 namespace internal { class TextAdapter; } 85 class TransformAdapter2D; 86 87 using ColorPropertyHandle = PropertyHandle<ColorPropertyValue , sksg::Color >; 88 using OpacityPropertyHandle = PropertyHandle<OpacityPropertyValue , sksg::OpacityEffect >; 89 using TextPropertyHandle = PropertyHandle<TextPropertyValue , internal::TextAdapter >; 90 using TransformPropertyHandle = PropertyHandle<TransformPropertyValue, TransformAdapter2D >; 91 92 /** 93 * A PropertyObserver can be used to track and manipulate certain properties of "interesting" 94 * Lottie nodes. 95 * 96 * When registered with an animation builder, PropertyObserver receives notifications for 97 * various properties of layer and shape nodes. The |node_name| argument corresponds to the 98 * name ("nm") node property. 99 */ 100 class SK_API PropertyObserver : public SkRefCnt { 101 public: 102 template <typename T> 103 using LazyHandle = std::function<std::unique_ptr<T>()>; 104 105 virtual void onColorProperty (const char node_name[], 106 const LazyHandle<ColorPropertyHandle>&); 107 virtual void onOpacityProperty (const char node_name[], 108 const LazyHandle<OpacityPropertyHandle>&); 109 virtual void onTextProperty (const char node_name[], 110 const LazyHandle<TextPropertyHandle>&); 111 virtual void onTransformProperty(const char node_name[], 112 const LazyHandle<TransformPropertyHandle>&); 113 }; 114 115 } // namespace skottie 116 117 #endif // SkottieProperty_DEFINED 118