Florin Malita | a831655 | 2018-11-09 16:19:44 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "modules/skottie/utils/SkottieUtils.h" |
Florin Malita | a831655 | 2018-11-09 16:19:44 -0500 | [diff] [blame] | 9 | |
Florin Malita | a831655 | 2018-11-09 16:19:44 -0500 | [diff] [blame] | 10 | namespace skottie_utils { |
| 11 | |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 12 | class CustomPropertyManager::PropertyInterceptor final : public skottie::PropertyObserver { |
| 13 | public: |
| 14 | explicit PropertyInterceptor(CustomPropertyManager* mgr) : fMgr(mgr) {} |
Florin Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 15 | |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 16 | 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 Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 22 | } |
Florin Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 23 | |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 24 | 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 Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 30 | } |
Florin Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 31 | |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 32 | 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 Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 38 | } |
Florin Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 39 | |
Florin Malita | 7c7cd30 | 2020-01-16 18:39:44 -0500 | [diff] [blame] | 40 | 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 Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 48 | private: |
| 49 | CustomPropertyManager* fMgr; |
| 50 | }; |
| 51 | |
| 52 | class CustomPropertyManager::MarkerInterceptor final : public skottie::MarkerObserver { |
| 53 | public: |
| 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 | |
| 63 | private: |
| 64 | CustomPropertyManager* fMgr; |
| 65 | }; |
| 66 | |
| 67 | CustomPropertyManager::CustomPropertyManager() |
| 68 | : fPropertyInterceptor(sk_make_sp<PropertyInterceptor>(this)) |
| 69 | , fMarkerInterceptor(sk_make_sp<MarkerInterceptor>(this)) {} |
Florin Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 70 | |
| 71 | CustomPropertyManager::~CustomPropertyManager() = default; |
| 72 | |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 73 | sk_sp<skottie::PropertyObserver> CustomPropertyManager::getPropertyObserver() const { |
| 74 | return fPropertyInterceptor; |
| 75 | } |
| 76 | |
| 77 | sk_sp<skottie::MarkerObserver> CustomPropertyManager::getMarkerObserver() const { |
| 78 | return fMarkerInterceptor; |
| 79 | } |
| 80 | |
Florin Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 81 | template <typename T> |
| 82 | std::vector<CustomPropertyManager::PropKey> |
| 83 | CustomPropertyManager::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 | |
| 94 | template <typename V, typename T> |
| 95 | V 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 | |
| 103 | template <typename V, typename T> |
| 104 | bool 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 | |
| 118 | std::vector<CustomPropertyManager::PropKey> |
| 119 | CustomPropertyManager::getColorProps() const { |
| 120 | return this->getProps(fColorMap); |
| 121 | } |
| 122 | |
| 123 | skottie::ColorPropertyValue CustomPropertyManager::getColor(const PropKey& key) const { |
| 124 | return this->get<skottie::ColorPropertyValue>(key, fColorMap); |
| 125 | } |
| 126 | |
| 127 | bool CustomPropertyManager::setColor(const PropKey& key, const skottie::ColorPropertyValue& c) { |
| 128 | return this->set(key, c, fColorMap); |
| 129 | } |
| 130 | |
| 131 | std::vector<CustomPropertyManager::PropKey> |
| 132 | CustomPropertyManager::getOpacityProps() const { |
| 133 | return this->getProps(fOpacityMap); |
| 134 | } |
| 135 | |
| 136 | skottie::OpacityPropertyValue CustomPropertyManager::getOpacity(const PropKey& key) const { |
| 137 | return this->get<skottie::OpacityPropertyValue>(key, fOpacityMap); |
| 138 | } |
| 139 | |
| 140 | bool CustomPropertyManager::setOpacity(const PropKey& key, const skottie::OpacityPropertyValue& o) { |
| 141 | return this->set(key, o, fOpacityMap); |
| 142 | } |
| 143 | |
| 144 | std::vector<CustomPropertyManager::PropKey> |
| 145 | CustomPropertyManager::getTransformProps() const { |
| 146 | return this->getProps(fTransformMap); |
| 147 | } |
| 148 | |
Florin Malita | 7c7cd30 | 2020-01-16 18:39:44 -0500 | [diff] [blame] | 149 | skottie::TransformPropertyValue CustomPropertyManager::getTransform(const PropKey& key) const { |
| 150 | return this->get<skottie::TransformPropertyValue>(key, fTransformMap); |
| 151 | } |
| 152 | |
Florin Malita | 8ac81b7 | 2018-11-28 11:39:39 -0500 | [diff] [blame] | 153 | bool CustomPropertyManager::setTransform(const PropKey& key, |
| 154 | const skottie::TransformPropertyValue& t) { |
| 155 | return this->set(key, t, fTransformMap); |
| 156 | } |
| 157 | |
Florin Malita | 7c7cd30 | 2020-01-16 18:39:44 -0500 | [diff] [blame] | 158 | std::vector<CustomPropertyManager::PropKey> |
| 159 | CustomPropertyManager::getTextProps() const { |
| 160 | return this->getProps(fTextMap); |
| 161 | } |
| 162 | |
| 163 | skottie::TextPropertyValue CustomPropertyManager::getText(const PropKey& key) const { |
| 164 | return this->get<skottie::TextPropertyValue>(key, fTextMap); |
| 165 | } |
| 166 | |
| 167 | bool CustomPropertyManager::setText(const PropKey& key, const skottie::TextPropertyValue& o) { |
| 168 | return this->set(key, o, fTextMap); |
| 169 | } |
| 170 | |
Florin Malita | a831655 | 2018-11-09 16:19:44 -0500 | [diff] [blame] | 171 | } // namespace skottie_utils |