blob: 9c9ba565edf361064fb35460e47d21df57f7495a [file] [log] [blame]
Florin Malitaa8316552018-11-09 16:19:44 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "modules/skottie/utils/SkottieUtils.h"
Florin Malitaa8316552018-11-09 16:19:44 -05009
Florin Malitaa8316552018-11-09 16:19:44 -050010namespace skottie_utils {
11
Florin Malita91af8d82018-11-30 16:46:45 -050012class CustomPropertyManager::PropertyInterceptor final : public skottie::PropertyObserver {
13public:
14 explicit PropertyInterceptor(CustomPropertyManager* mgr) : fMgr(mgr) {}
Florin Malita8ac81b72018-11-28 11:39:39 -050015
Florin Malita91af8d82018-11-30 16:46:45 -050016 void onColorProperty(const char node_name[],
17 const LazyHandle<skottie::ColorPropertyHandle>& c) override {
18 const auto key = fMgr->acceptKey(node_name);
19 if (!key.empty()) {
20 fMgr->fColorMap[key].push_back(c());
21 }
Florin Malita8ac81b72018-11-28 11:39:39 -050022 }
Florin Malita8ac81b72018-11-28 11:39:39 -050023
Florin Malita91af8d82018-11-30 16:46:45 -050024 void onOpacityProperty(const char node_name[],
25 const LazyHandle<skottie::OpacityPropertyHandle>& o) override {
26 const auto key = fMgr->acceptKey(node_name);
27 if (!key.empty()) {
28 fMgr->fOpacityMap[key].push_back(o());
29 }
Florin Malita8ac81b72018-11-28 11:39:39 -050030 }
Florin Malita8ac81b72018-11-28 11:39:39 -050031
Florin Malita91af8d82018-11-30 16:46:45 -050032 void onTransformProperty(const char node_name[],
33 const LazyHandle<skottie::TransformPropertyHandle>& t) override {
34 const auto key = fMgr->acceptKey(node_name);
35 if (!key.empty()) {
36 fMgr->fTransformMap[key].push_back(t());
37 }
Florin Malita8ac81b72018-11-28 11:39:39 -050038 }
Florin Malita8ac81b72018-11-28 11:39:39 -050039
Florin Malita7c7cd302020-01-16 18:39:44 -050040 void onTextProperty(const char node_name[],
41 const LazyHandle<skottie::TextPropertyHandle>& t) override {
42 const auto key = fMgr->acceptKey(node_name);
43 if (!key.empty()) {
44 fMgr->fTextMap[key].push_back(t());
45 }
46 }
47
Florin Malita91af8d82018-11-30 16:46:45 -050048private:
49 CustomPropertyManager* fMgr;
50};
51
52class CustomPropertyManager::MarkerInterceptor final : public skottie::MarkerObserver {
53public:
54 explicit MarkerInterceptor(CustomPropertyManager* mgr) : fMgr(mgr) {}
55
56 void onMarker(const char name[], float t0, float t1) override {
57 const auto key = fMgr->acceptKey(name);
58 if (!key.empty()) {
59 fMgr->fMarkers.push_back({ std::move(key), t0, t1 });
60 }
61 }
62
63private:
64 CustomPropertyManager* fMgr;
65};
66
67CustomPropertyManager::CustomPropertyManager()
68 : fPropertyInterceptor(sk_make_sp<PropertyInterceptor>(this))
69 , fMarkerInterceptor(sk_make_sp<MarkerInterceptor>(this)) {}
Florin Malita8ac81b72018-11-28 11:39:39 -050070
71CustomPropertyManager::~CustomPropertyManager() = default;
72
Florin Malita91af8d82018-11-30 16:46:45 -050073sk_sp<skottie::PropertyObserver> CustomPropertyManager::getPropertyObserver() const {
74 return fPropertyInterceptor;
75}
76
77sk_sp<skottie::MarkerObserver> CustomPropertyManager::getMarkerObserver() const {
78 return fMarkerInterceptor;
79}
80
Florin Malita8ac81b72018-11-28 11:39:39 -050081template <typename T>
82std::vector<CustomPropertyManager::PropKey>
83CustomPropertyManager::getProps(const PropMap<T>& container) const {
84 std::vector<PropKey> props;
85
86 for (const auto& prop_list : container) {
87 SkASSERT(!prop_list.second.empty());
88 props.push_back(prop_list.first);
89 }
90
91 return props;
92}
93
94template <typename V, typename T>
95V CustomPropertyManager::get(const PropKey& key, const PropMap<T>& container) const {
96 auto prop_group = container.find(key);
97
98 return prop_group == container.end()
99 ? V()
100 : prop_group->second.front()->get();
101}
102
103template <typename V, typename T>
104bool CustomPropertyManager::set(const PropKey& key, const V& val, const PropMap<T>& container) {
105 auto prop_group = container.find(key);
106
107 if (prop_group == container.end()) {
108 return false;
109 }
110
111 for (auto& handle : prop_group->second) {
112 handle->set(val);
113 }
114
115 return true;
116}
117
118std::vector<CustomPropertyManager::PropKey>
119CustomPropertyManager::getColorProps() const {
120 return this->getProps(fColorMap);
121}
122
123skottie::ColorPropertyValue CustomPropertyManager::getColor(const PropKey& key) const {
124 return this->get<skottie::ColorPropertyValue>(key, fColorMap);
125}
126
127bool CustomPropertyManager::setColor(const PropKey& key, const skottie::ColorPropertyValue& c) {
128 return this->set(key, c, fColorMap);
129}
130
131std::vector<CustomPropertyManager::PropKey>
132CustomPropertyManager::getOpacityProps() const {
133 return this->getProps(fOpacityMap);
134}
135
136skottie::OpacityPropertyValue CustomPropertyManager::getOpacity(const PropKey& key) const {
137 return this->get<skottie::OpacityPropertyValue>(key, fOpacityMap);
138}
139
140bool CustomPropertyManager::setOpacity(const PropKey& key, const skottie::OpacityPropertyValue& o) {
141 return this->set(key, o, fOpacityMap);
142}
143
144std::vector<CustomPropertyManager::PropKey>
145CustomPropertyManager::getTransformProps() const {
146 return this->getProps(fTransformMap);
147}
148
Florin Malita7c7cd302020-01-16 18:39:44 -0500149skottie::TransformPropertyValue CustomPropertyManager::getTransform(const PropKey& key) const {
150 return this->get<skottie::TransformPropertyValue>(key, fTransformMap);
151}
152
Florin Malita8ac81b72018-11-28 11:39:39 -0500153bool CustomPropertyManager::setTransform(const PropKey& key,
154 const skottie::TransformPropertyValue& t) {
155 return this->set(key, t, fTransformMap);
156}
157
Florin Malita7c7cd302020-01-16 18:39:44 -0500158std::vector<CustomPropertyManager::PropKey>
159CustomPropertyManager::getTextProps() const {
160 return this->getProps(fTextMap);
161}
162
163skottie::TextPropertyValue CustomPropertyManager::getText(const PropKey& key) const {
164 return this->get<skottie::TextPropertyValue>(key, fTextMap);
165}
166
167bool CustomPropertyManager::setText(const PropKey& key, const skottie::TextPropertyValue& o) {
168 return this->set(key, o, fTextMap);
169}
170
Florin Malitaa8316552018-11-09 16:19:44 -0500171} // namespace skottie_utils