2 * Copyright 2018 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkottieUtils_DEFINED
9 #define SkottieUtils_DEFINED
11 #include "modules/skottie/include/ExternalLayer.h"
12 #include "modules/skottie/include/Skottie.h"
13 #include "modules/skottie/include/SkottieProperty.h"
17 #include <unordered_map>
20 namespace skottie_utils {
23 * CustomPropertyManager implements a property management scheme where color/opacity/transform
24 * attributes are grouped and manipulated by name (one-to-many mapping).
26 * - setters apply the value to all properties in a named group
28 * - getters return all the managed property groups, and the first value within each of them
29 * (unchecked assumption: all properties within the same group have the same value)
31 * Attach to an Animation::Builder using the utility methods below to intercept properties and
32 * markers at build time.
34 class CustomPropertyManager final {
37 kCollapseProperties, // keys ignore the ancestor chain and are
38 // grouped based on the local node name
39 kNamespacedProperties, // keys include the ancestor node names (no grouping)
42 explicit CustomPropertyManager(Mode = Mode::kNamespacedProperties,
43 const char* prefix = nullptr);
44 ~CustomPropertyManager();
46 using PropKey = std::string;
48 std::vector<PropKey> getColorProps() const;
49 skottie::ColorPropertyValue getColor(const PropKey&) const;
50 bool setColor(const PropKey&, const skottie::ColorPropertyValue&);
52 std::vector<PropKey> getOpacityProps() const;
53 skottie::OpacityPropertyValue getOpacity(const PropKey&) const;
54 bool setOpacity(const PropKey&, const skottie::OpacityPropertyValue&);
56 std::vector<PropKey> getTransformProps() const;
57 skottie::TransformPropertyValue getTransform(const PropKey&) const;
58 bool setTransform(const PropKey&, const skottie::TransformPropertyValue&);
60 std::vector<PropKey> getTextProps() const;
61 skottie::TextPropertyValue getText(const PropKey&) const;
62 bool setText(const PropKey&, const skottie::TextPropertyValue&);
68 const std::vector<MarkerInfo>& markers() const { return fMarkers; }
70 // Returns a property observer to be attached to an animation builder.
71 sk_sp<skottie::PropertyObserver> getPropertyObserver() const;
73 // Returns a marker observer to be attached to an animation builder.
74 sk_sp<skottie::MarkerObserver> getMarkerObserver() const;
77 class PropertyInterceptor;
78 class MarkerInterceptor;
80 std::string acceptKey(const char*, const char*) const;
83 using PropGroup = std::vector<std::unique_ptr<T>>;
86 using PropMap = std::unordered_map<PropKey, PropGroup<T>>;
89 std::vector<PropKey> getProps(const PropMap<T>& container) const;
91 template <typename V, typename T>
92 V get(const PropKey&, const PropMap<T>& container) const;
94 template <typename V, typename T>
95 bool set(const PropKey&, const V&, const PropMap<T>& container);
98 const SkString fPrefix;
100 sk_sp<PropertyInterceptor> fPropertyInterceptor;
101 sk_sp<MarkerInterceptor> fMarkerInterceptor;
103 PropMap<skottie::ColorPropertyHandle> fColorMap;
104 PropMap<skottie::OpacityPropertyHandle> fOpacityMap;
105 PropMap<skottie::TransformPropertyHandle> fTransformMap;
106 PropMap<skottie::TextPropertyHandle> fTextMap;
107 std::vector<MarkerInfo> fMarkers;
108 std::string fCurrentNode;
112 * A sample PrecompInterceptor implementation.
114 * Attempts to substitute all precomp layers matching the given pattern (name prefix)
115 * with external Lottie animations.
117 class ExternalAnimationPrecompInterceptor final : public skottie::PrecompInterceptor {
119 ExternalAnimationPrecompInterceptor(sk_sp<skresources::ResourceProvider>, const char prefix[]);
120 ~ExternalAnimationPrecompInterceptor() override;
123 sk_sp<skottie::ExternalLayer> onLoadPrecomp(const char[], const char[], const SkSize&) override;
125 const sk_sp<skresources::ResourceProvider> fResourceProvider;
126 const SkString fPrefix;
130 } // namespace skottie_utils
132 #endif // SkottieUtils_DEFINED