Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkottieUtils.h
Go to the documentation of this file.
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 SkottieUtils_DEFINED
9#define SkottieUtils_DEFINED
10
15
16#include <cstddef>
17#include <memory>
18#include <string>
19#include <unordered_map>
20#include <vector>
21
22struct SkSize;
23
24namespace skottie {
25class MarkerObserver;
26}
27
28namespace skresources {
30}
31
32namespace skottie_utils {
33
34/**
35 * CustomPropertyManager implements a property management scheme where color/opacity/transform
36 * attributes are grouped and manipulated by name (one-to-many mapping).
37 *
38 * - setters apply the value to all properties in a named group
39 *
40 * - getters return all the managed property groups, and the first value within each of them
41 * (unchecked assumption: all properties within the same group have the same value)
42 *
43 * Attach to an Animation::Builder using the utility methods below to intercept properties and
44 * markers at build time.
45 */
47public:
48 enum class Mode {
49 kCollapseProperties, // keys ignore the ancestor chain and are
50 // grouped based on the local node name
51 kNamespacedProperties, // keys include the ancestor node names (no grouping)
52 };
53
55 const char* prefix = nullptr);
57
58 using PropKey = std::string;
59
60 std::vector<PropKey> getColorProps() const;
62 std::unique_ptr<skottie::ColorPropertyHandle> getColorHandle(const PropKey&, size_t) const;
63 bool setColor(const PropKey&, const skottie::ColorPropertyValue&);
64
65 std::vector<PropKey> getOpacityProps() const;
67 std::unique_ptr<skottie::OpacityPropertyHandle> getOpacityHandle(const PropKey&, size_t) const;
69
70 std::vector<PropKey> getTransformProps() const;
72 std::unique_ptr<skottie::TransformPropertyHandle> getTransformHandle(const PropKey&,
73 size_t) const;
75
76 std::vector<PropKey> getTextProps() const;
78 std::unique_ptr<skottie::TextPropertyHandle> getTextHandle(const PropKey&, size_t index) const;
79 bool setText(const PropKey&, const skottie::TextPropertyValue&);
80
81 struct MarkerInfo {
82 std::string name;
83 float t0, t1;
84 };
85 const std::vector<MarkerInfo>& markers() const { return fMarkers; }
86
87 // Returns a property observer to be attached to an animation builder.
89
90 // Returns a marker observer to be attached to an animation builder.
92
93private:
94 class PropertyInterceptor;
95 class MarkerInterceptor;
96
97 std::string acceptKey(const char*, const char*) const;
98
99 template <typename T>
100 using PropGroup = std::vector<std::unique_ptr<T>>;
101
102 template <typename T>
103 using PropMap = std::unordered_map<PropKey, PropGroup<T>>;
104
105 template <typename T>
106 std::vector<PropKey> getProps(const PropMap<T>& container) const;
107
108 template <typename V, typename T>
109 V get(const PropKey&, const PropMap<T>& container) const;
110
111 template <typename T>
112 std::unique_ptr<T> getHandle(const PropKey&, size_t, const PropMap<T>& container) const;
113
114 template <typename V, typename T>
115 bool set(const PropKey&, const V&, const PropMap<T>& container);
116
117 const Mode fMode;
118 const SkString fPrefix;
119
120 sk_sp<PropertyInterceptor> fPropertyInterceptor;
121 sk_sp<MarkerInterceptor> fMarkerInterceptor;
122
123 PropMap<skottie::ColorPropertyHandle> fColorMap;
124 PropMap<skottie::OpacityPropertyHandle> fOpacityMap;
125 PropMap<skottie::TransformPropertyHandle> fTransformMap;
126 PropMap<skottie::TextPropertyHandle> fTextMap;
127 std::vector<MarkerInfo> fMarkers;
128 std::string fCurrentNode;
129};
130
131/**
132 * A sample PrecompInterceptor implementation.
133 *
134 * Attempts to substitute all precomp layers matching the given pattern (name prefix)
135 * with external Lottie animations.
136 */
138public:
141
142private:
143 sk_sp<skottie::ExternalLayer> onLoadPrecomp(const char[], const char[], const SkSize&) override;
144
145 const sk_sp<skresources::ResourceProvider> fResourceProvider;
146 const SkString fPrefix;
147};
148
149} // namespace skottie_utils
150
151#endif // SkottieUtils_DEFINED
sk_sp< skottie::MarkerObserver > getMarkerObserver() const
bool setOpacity(const PropKey &, const skottie::OpacityPropertyValue &)
skottie::ColorPropertyValue getColor(const PropKey &) const
bool setTransform(const PropKey &, const skottie::TransformPropertyValue &)
std::unique_ptr< skottie::OpacityPropertyHandle > getOpacityHandle(const PropKey &, size_t) const
bool setColor(const PropKey &, const skottie::ColorPropertyValue &)
bool setText(const PropKey &, const skottie::TextPropertyValue &)
std::vector< PropKey > getColorProps() const
std::unique_ptr< skottie::TextPropertyHandle > getTextHandle(const PropKey &, size_t index) const
skottie::TransformPropertyValue getTransform(const PropKey &) const
std::unique_ptr< skottie::TransformPropertyHandle > getTransformHandle(const PropKey &, size_t) const
std::unique_ptr< skottie::ColorPropertyHandle > getColorHandle(const PropKey &, size_t) const
std::vector< PropKey > getTextProps() const
const std::vector< MarkerInfo > & markers() const
skottie::TextPropertyValue getText(const PropKey &) const
std::vector< PropKey > getOpacityProps() const
sk_sp< skottie::PropertyObserver > getPropertyObserver() const
std::vector< PropKey > getTransformProps() const
skottie::OpacityPropertyValue getOpacity(const PropKey &) const
sk_sp< skottie::ExternalLayer > onLoadPrecomp(const char[], const char[], const SkSize &) override
T __attribute__((ext_vector_type(N))) V
float OpacityPropertyValue
SkColor ColorPropertyValue
SkBlendMode fMode
Definition xfermodes.cpp:52