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 | #include "modules/skottie/utils/SkottieUtils.h" |
9 | |
10 | namespace skottie_utils { |
11 | |
12 | class CustomPropertyManager::PropertyInterceptor final : public skottie::PropertyObserver { |
13 | public: |
14 | explicit PropertyInterceptor(CustomPropertyManager* mgr) : fMgr(mgr) {} |
15 | |
16 | void onColorProperty(const char node_name[], |
17 | const LazyHandle<skottie::ColorPropertyHandle>& c) override { |
18 | const auto markedKey = fMgr->acceptKey(node_name); |
19 | const auto key = markedKey.empty() ? markedKey : fMgr->fCurrentNode + ".Color" ; |
20 | fMgr->fColorMap[key].push_back(c()); |
21 | } |
22 | |
23 | void onOpacityProperty(const char node_name[], |
24 | const LazyHandle<skottie::OpacityPropertyHandle>& o) override { |
25 | const auto markedKey = fMgr->acceptKey(node_name); |
26 | const auto key = markedKey.empty() ? markedKey : fMgr->fCurrentNode + ".Opacity" ; |
27 | fMgr->fOpacityMap[key].push_back(o()); |
28 | } |
29 | |
30 | void onTransformProperty(const char node_name[], |
31 | const LazyHandle<skottie::TransformPropertyHandle>& t) override { |
32 | const auto markedKey = fMgr->acceptKey(node_name); |
33 | const auto key = markedKey.empty() ? markedKey : fMgr->fCurrentNode + ".Transform" ; |
34 | fMgr->fTransformMap[key].push_back(t()); |
35 | } |
36 | |
37 | void onEnterNode(const char node_name[]) override { |
38 | fMgr->fCurrentNode = |
39 | fMgr->fCurrentNode.empty() ? node_name : fMgr->fCurrentNode + "." + node_name; |
40 | } |
41 | |
42 | void onLeavingNode(const char node_name[]) override { |
43 | auto length = strlen(node_name); |
44 | fMgr->fCurrentNode = |
45 | fMgr->fCurrentNode.length() > length |
46 | ? fMgr->fCurrentNode.substr( |
47 | 0, fMgr->fCurrentNode.length() - strlen(node_name) - 1) |
48 | : "" ; |
49 | } |
50 | |
51 | void onTextProperty(const char node_name[], |
52 | const LazyHandle<skottie::TextPropertyHandle>& t) override { |
53 | const auto key = fMgr->acceptKey(node_name); |
54 | if (!key.empty()) { |
55 | fMgr->fTextMap[key].push_back(t()); |
56 | } |
57 | } |
58 | |
59 | private: |
60 | CustomPropertyManager* fMgr; |
61 | }; |
62 | |
63 | class CustomPropertyManager::MarkerInterceptor final : public skottie::MarkerObserver { |
64 | public: |
65 | explicit MarkerInterceptor(CustomPropertyManager* mgr) : fMgr(mgr) {} |
66 | |
67 | void onMarker(const char name[], float t0, float t1) override { |
68 | const auto key = fMgr->acceptKey(name); |
69 | if (!key.empty()) { |
70 | fMgr->fMarkers.push_back({ std::move(key), t0, t1 }); |
71 | } |
72 | } |
73 | |
74 | private: |
75 | CustomPropertyManager* fMgr; |
76 | }; |
77 | |
78 | CustomPropertyManager::CustomPropertyManager() |
79 | : fPropertyInterceptor(sk_make_sp<PropertyInterceptor>(this)) |
80 | , fMarkerInterceptor(sk_make_sp<MarkerInterceptor>(this)) {} |
81 | |
82 | CustomPropertyManager::~CustomPropertyManager() = default; |
83 | |
84 | sk_sp<skottie::PropertyObserver> CustomPropertyManager::getPropertyObserver() const { |
85 | return fPropertyInterceptor; |
86 | } |
87 | |
88 | sk_sp<skottie::MarkerObserver> CustomPropertyManager::getMarkerObserver() const { |
89 | return fMarkerInterceptor; |
90 | } |
91 | |
92 | template <typename T> |
93 | std::vector<CustomPropertyManager::PropKey> |
94 | CustomPropertyManager::getProps(const PropMap<T>& container) const { |
95 | std::vector<PropKey> props; |
96 | |
97 | for (const auto& prop_list : container) { |
98 | SkASSERT(!prop_list.second.empty()); |
99 | props.push_back(prop_list.first); |
100 | } |
101 | |
102 | return props; |
103 | } |
104 | |
105 | template <typename V, typename T> |
106 | V CustomPropertyManager::get(const PropKey& key, const PropMap<T>& container) const { |
107 | auto prop_group = container.find(key); |
108 | |
109 | return prop_group == container.end() |
110 | ? V() |
111 | : prop_group->second.front()->get(); |
112 | } |
113 | |
114 | template <typename V, typename T> |
115 | bool CustomPropertyManager::set(const PropKey& key, const V& val, const PropMap<T>& container) { |
116 | auto prop_group = container.find(key); |
117 | |
118 | if (prop_group == container.end()) { |
119 | return false; |
120 | } |
121 | |
122 | for (auto& handle : prop_group->second) { |
123 | handle->set(val); |
124 | } |
125 | |
126 | return true; |
127 | } |
128 | |
129 | std::vector<CustomPropertyManager::PropKey> |
130 | CustomPropertyManager::getColorProps() const { |
131 | return this->getProps(fColorMap); |
132 | } |
133 | |
134 | skottie::ColorPropertyValue CustomPropertyManager::getColor(const PropKey& key) const { |
135 | return this->get<skottie::ColorPropertyValue>(key, fColorMap); |
136 | } |
137 | |
138 | bool CustomPropertyManager::setColor(const PropKey& key, const skottie::ColorPropertyValue& c) { |
139 | return this->set(key, c, fColorMap); |
140 | } |
141 | |
142 | std::vector<CustomPropertyManager::PropKey> |
143 | CustomPropertyManager::getOpacityProps() const { |
144 | return this->getProps(fOpacityMap); |
145 | } |
146 | |
147 | skottie::OpacityPropertyValue CustomPropertyManager::getOpacity(const PropKey& key) const { |
148 | return this->get<skottie::OpacityPropertyValue>(key, fOpacityMap); |
149 | } |
150 | |
151 | bool CustomPropertyManager::setOpacity(const PropKey& key, const skottie::OpacityPropertyValue& o) { |
152 | return this->set(key, o, fOpacityMap); |
153 | } |
154 | |
155 | std::vector<CustomPropertyManager::PropKey> |
156 | CustomPropertyManager::getTransformProps() const { |
157 | return this->getProps(fTransformMap); |
158 | } |
159 | |
160 | skottie::TransformPropertyValue CustomPropertyManager::getTransform(const PropKey& key) const { |
161 | return this->get<skottie::TransformPropertyValue>(key, fTransformMap); |
162 | } |
163 | |
164 | bool CustomPropertyManager::setTransform(const PropKey& key, |
165 | const skottie::TransformPropertyValue& t) { |
166 | return this->set(key, t, fTransformMap); |
167 | } |
168 | |
169 | std::vector<CustomPropertyManager::PropKey> |
170 | CustomPropertyManager::getTextProps() const { |
171 | return this->getProps(fTextMap); |
172 | } |
173 | |
174 | skottie::TextPropertyValue CustomPropertyManager::getText(const PropKey& key) const { |
175 | return this->get<skottie::TextPropertyValue>(key, fTextMap); |
176 | } |
177 | |
178 | bool CustomPropertyManager::setText(const PropKey& key, const skottie::TextPropertyValue& o) { |
179 | return this->set(key, o, fTextMap); |
180 | } |
181 | |
182 | } // namespace skottie_utils |
183 | |