2018-11-09 21:19:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "modules/skottie/utils/SkottieUtils.h"
|
2018-11-09 21:19:44 +00:00
|
|
|
|
|
|
|
namespace skottie_utils {
|
|
|
|
|
2018-11-30 21:46:45 +00:00
|
|
|
class CustomPropertyManager::PropertyInterceptor final : public skottie::PropertyObserver {
|
|
|
|
public:
|
|
|
|
explicit PropertyInterceptor(CustomPropertyManager* mgr) : fMgr(mgr) {}
|
|
|
|
|
|
|
|
void onColorProperty(const char node_name[],
|
|
|
|
const LazyHandle<skottie::ColorPropertyHandle>& c) override {
|
2020-08-20 18:51:41 +00:00
|
|
|
const auto key = fMgr->acceptKey(node_name, ".Color");
|
|
|
|
if (!key.empty()) {
|
|
|
|
fMgr->fColorMap[key].push_back(c());
|
|
|
|
}
|
2018-11-30 21:46:45 +00:00
|
|
|
}
|
2018-11-28 16:39:39 +00:00
|
|
|
|
2018-11-30 21:46:45 +00:00
|
|
|
void onOpacityProperty(const char node_name[],
|
|
|
|
const LazyHandle<skottie::OpacityPropertyHandle>& o) override {
|
2020-08-20 18:51:41 +00:00
|
|
|
const auto key = fMgr->acceptKey(node_name, ".Opacity");
|
|
|
|
if (!key.empty()) {
|
|
|
|
fMgr->fOpacityMap[key].push_back(o());
|
|
|
|
}
|
2018-11-28 16:39:39 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 21:46:45 +00:00
|
|
|
void onTransformProperty(const char node_name[],
|
|
|
|
const LazyHandle<skottie::TransformPropertyHandle>& t) override {
|
2020-08-20 18:51:41 +00:00
|
|
|
const auto key = fMgr->acceptKey(node_name, ".Transform");
|
|
|
|
if (!key.empty()) {
|
|
|
|
fMgr->fTransformMap[key].push_back(t());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onTextProperty(const char node_name[],
|
|
|
|
const LazyHandle<skottie::TextPropertyHandle>& t) override {
|
|
|
|
const auto key = fMgr->acceptKey(node_name, ".Text");
|
|
|
|
if (!key.empty()) {
|
|
|
|
fMgr->fTextMap[key].push_back(t());
|
|
|
|
}
|
2020-02-11 07:48:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 21:12:46 +00:00
|
|
|
void onEnterNode(const char node_name[], PropertyObserver::NodeType node_type) override {
|
|
|
|
if (node_name == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-11 07:48:50 +00:00
|
|
|
fMgr->fCurrentNode =
|
|
|
|
fMgr->fCurrentNode.empty() ? node_name : fMgr->fCurrentNode + "." + node_name;
|
|
|
|
}
|
|
|
|
|
2021-06-09 21:12:46 +00:00
|
|
|
void onLeavingNode(const char node_name[], PropertyObserver::NodeType node_type) override {
|
|
|
|
if (node_name == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-11 07:48:50 +00:00
|
|
|
auto length = strlen(node_name);
|
|
|
|
fMgr->fCurrentNode =
|
|
|
|
fMgr->fCurrentNode.length() > length
|
|
|
|
? fMgr->fCurrentNode.substr(
|
|
|
|
0, fMgr->fCurrentNode.length() - strlen(node_name) - 1)
|
|
|
|
: "";
|
2018-11-28 16:39:39 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 21:46:45 +00:00
|
|
|
private:
|
|
|
|
CustomPropertyManager* fMgr;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CustomPropertyManager::MarkerInterceptor final : public skottie::MarkerObserver {
|
|
|
|
public:
|
|
|
|
explicit MarkerInterceptor(CustomPropertyManager* mgr) : fMgr(mgr) {}
|
|
|
|
|
|
|
|
void onMarker(const char name[], float t0, float t1) override {
|
2020-08-20 18:51:41 +00:00
|
|
|
// collect all markers
|
|
|
|
fMgr->fMarkers.push_back({ std::string(name), t0, t1 });
|
2018-11-28 16:39:39 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 21:46:45 +00:00
|
|
|
private:
|
|
|
|
CustomPropertyManager* fMgr;
|
|
|
|
};
|
|
|
|
|
2020-08-20 18:51:41 +00:00
|
|
|
CustomPropertyManager::CustomPropertyManager(Mode mode, const char* prefix)
|
|
|
|
: fMode(mode)
|
|
|
|
, fPrefix(prefix ? prefix : "$")
|
|
|
|
, fPropertyInterceptor(sk_make_sp<PropertyInterceptor>(this))
|
2018-11-30 21:46:45 +00:00
|
|
|
, fMarkerInterceptor(sk_make_sp<MarkerInterceptor>(this)) {}
|
2018-11-28 16:39:39 +00:00
|
|
|
|
|
|
|
CustomPropertyManager::~CustomPropertyManager() = default;
|
|
|
|
|
2020-08-20 18:51:41 +00:00
|
|
|
std::string CustomPropertyManager::acceptKey(const char* name, const char* suffix) const {
|
|
|
|
if (!SkStrStartsWith(name, fPrefix.c_str())) {
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
return fMode == Mode::kCollapseProperties
|
|
|
|
? std::string(name)
|
|
|
|
: fCurrentNode + suffix;
|
|
|
|
}
|
|
|
|
|
2018-11-30 21:46:45 +00:00
|
|
|
sk_sp<skottie::PropertyObserver> CustomPropertyManager::getPropertyObserver() const {
|
|
|
|
return fPropertyInterceptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<skottie::MarkerObserver> CustomPropertyManager::getMarkerObserver() const {
|
|
|
|
return fMarkerInterceptor;
|
|
|
|
}
|
|
|
|
|
2018-11-28 16:39:39 +00:00
|
|
|
template <typename T>
|
|
|
|
std::vector<CustomPropertyManager::PropKey>
|
|
|
|
CustomPropertyManager::getProps(const PropMap<T>& container) const {
|
|
|
|
std::vector<PropKey> props;
|
|
|
|
|
|
|
|
for (const auto& prop_list : container) {
|
|
|
|
SkASSERT(!prop_list.second.empty());
|
|
|
|
props.push_back(prop_list.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename V, typename T>
|
|
|
|
V CustomPropertyManager::get(const PropKey& key, const PropMap<T>& container) const {
|
|
|
|
auto prop_group = container.find(key);
|
|
|
|
|
|
|
|
return prop_group == container.end()
|
|
|
|
? V()
|
|
|
|
: prop_group->second.front()->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename V, typename T>
|
|
|
|
bool CustomPropertyManager::set(const PropKey& key, const V& val, const PropMap<T>& container) {
|
|
|
|
auto prop_group = container.find(key);
|
|
|
|
|
|
|
|
if (prop_group == container.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& handle : prop_group->second) {
|
|
|
|
handle->set(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CustomPropertyManager::PropKey>
|
|
|
|
CustomPropertyManager::getColorProps() const {
|
|
|
|
return this->getProps(fColorMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
skottie::ColorPropertyValue CustomPropertyManager::getColor(const PropKey& key) const {
|
|
|
|
return this->get<skottie::ColorPropertyValue>(key, fColorMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CustomPropertyManager::setColor(const PropKey& key, const skottie::ColorPropertyValue& c) {
|
|
|
|
return this->set(key, c, fColorMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CustomPropertyManager::PropKey>
|
|
|
|
CustomPropertyManager::getOpacityProps() const {
|
|
|
|
return this->getProps(fOpacityMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
skottie::OpacityPropertyValue CustomPropertyManager::getOpacity(const PropKey& key) const {
|
|
|
|
return this->get<skottie::OpacityPropertyValue>(key, fOpacityMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CustomPropertyManager::setOpacity(const PropKey& key, const skottie::OpacityPropertyValue& o) {
|
|
|
|
return this->set(key, o, fOpacityMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CustomPropertyManager::PropKey>
|
|
|
|
CustomPropertyManager::getTransformProps() const {
|
|
|
|
return this->getProps(fTransformMap);
|
|
|
|
}
|
|
|
|
|
2020-01-16 23:39:44 +00:00
|
|
|
skottie::TransformPropertyValue CustomPropertyManager::getTransform(const PropKey& key) const {
|
|
|
|
return this->get<skottie::TransformPropertyValue>(key, fTransformMap);
|
|
|
|
}
|
|
|
|
|
2018-11-28 16:39:39 +00:00
|
|
|
bool CustomPropertyManager::setTransform(const PropKey& key,
|
|
|
|
const skottie::TransformPropertyValue& t) {
|
|
|
|
return this->set(key, t, fTransformMap);
|
|
|
|
}
|
|
|
|
|
2020-01-16 23:39:44 +00:00
|
|
|
std::vector<CustomPropertyManager::PropKey>
|
|
|
|
CustomPropertyManager::getTextProps() const {
|
|
|
|
return this->getProps(fTextMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
skottie::TextPropertyValue CustomPropertyManager::getText(const PropKey& key) const {
|
|
|
|
return this->get<skottie::TextPropertyValue>(key, fTextMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CustomPropertyManager::setText(const PropKey& key, const skottie::TextPropertyValue& o) {
|
|
|
|
return this->set(key, o, fTextMap);
|
|
|
|
}
|
|
|
|
|
2020-05-06 19:55:18 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ExternalAnimationLayer final : public skottie::ExternalLayer {
|
|
|
|
public:
|
|
|
|
ExternalAnimationLayer(sk_sp<skottie::Animation> anim, const SkSize& size)
|
|
|
|
: fAnimation(std::move(anim))
|
|
|
|
, fSize(size) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void render(SkCanvas* canvas, double t) override {
|
|
|
|
fAnimation->seekFrameTime(t);
|
|
|
|
|
|
|
|
const auto dst_rect = SkRect::MakeSize(fSize);
|
|
|
|
fAnimation->render(canvas, &dst_rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
const sk_sp<skottie::Animation> fAnimation;
|
|
|
|
const SkSize fSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
ExternalAnimationPrecompInterceptor::ExternalAnimationPrecompInterceptor(
|
|
|
|
sk_sp<skresources::ResourceProvider> rprovider,
|
|
|
|
const char prefixp[])
|
|
|
|
: fResourceProvider(std::move(rprovider))
|
|
|
|
, fPrefix(prefixp) {}
|
|
|
|
|
|
|
|
ExternalAnimationPrecompInterceptor::~ExternalAnimationPrecompInterceptor() = default;
|
|
|
|
|
|
|
|
sk_sp<skottie::ExternalLayer> ExternalAnimationPrecompInterceptor::onLoadPrecomp(
|
|
|
|
const char[], const char name[], const SkSize& size) {
|
2020-08-16 03:22:53 +00:00
|
|
|
if (0 != strncmp(name, fPrefix.c_str(), fPrefix.size())) {
|
2020-05-06 19:55:18 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto data = fResourceProvider->load("", name + fPrefix.size());
|
|
|
|
if (!data) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto anim = skottie::Animation::Builder()
|
|
|
|
.setPrecompInterceptor(sk_ref_sp(this))
|
|
|
|
.make(static_cast<const char*>(data->data()), data->size());
|
|
|
|
|
|
|
|
return anim ? sk_make_sp<ExternalAnimationLayer>(std::move(anim), size)
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-09 21:19:44 +00:00
|
|
|
} // namespace skottie_utils
|