1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2018 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#ifndef SkottieUtils_DEFINED 9cb93a386Sopenharmony_ci#define SkottieUtils_DEFINED 10cb93a386Sopenharmony_ci 11cb93a386Sopenharmony_ci#include "modules/skottie/include/ExternalLayer.h" 12cb93a386Sopenharmony_ci#include "modules/skottie/include/Skottie.h" 13cb93a386Sopenharmony_ci#include "modules/skottie/include/SkottieProperty.h" 14cb93a386Sopenharmony_ci 15cb93a386Sopenharmony_ci#include <memory> 16cb93a386Sopenharmony_ci#include <string> 17cb93a386Sopenharmony_ci#include <unordered_map> 18cb93a386Sopenharmony_ci#include <vector> 19cb93a386Sopenharmony_ci 20cb93a386Sopenharmony_cinamespace skottie_utils { 21cb93a386Sopenharmony_ci 22cb93a386Sopenharmony_ci/** 23cb93a386Sopenharmony_ci * CustomPropertyManager implements a property management scheme where color/opacity/transform 24cb93a386Sopenharmony_ci * attributes are grouped and manipulated by name (one-to-many mapping). 25cb93a386Sopenharmony_ci * 26cb93a386Sopenharmony_ci * - setters apply the value to all properties in a named group 27cb93a386Sopenharmony_ci * 28cb93a386Sopenharmony_ci * - getters return all the managed property groups, and the first value within each of them 29cb93a386Sopenharmony_ci * (unchecked assumption: all properties within the same group have the same value) 30cb93a386Sopenharmony_ci * 31cb93a386Sopenharmony_ci * Attach to an Animation::Builder using the utility methods below to intercept properties and 32cb93a386Sopenharmony_ci * markers at build time. 33cb93a386Sopenharmony_ci */ 34cb93a386Sopenharmony_ciclass CustomPropertyManager final { 35cb93a386Sopenharmony_cipublic: 36cb93a386Sopenharmony_ci enum class Mode { 37cb93a386Sopenharmony_ci kCollapseProperties, // keys ignore the ancestor chain and are 38cb93a386Sopenharmony_ci // grouped based on the local node name 39cb93a386Sopenharmony_ci kNamespacedProperties, // keys include the ancestor node names (no grouping) 40cb93a386Sopenharmony_ci }; 41cb93a386Sopenharmony_ci 42cb93a386Sopenharmony_ci explicit CustomPropertyManager(Mode = Mode::kNamespacedProperties, 43cb93a386Sopenharmony_ci const char* prefix = nullptr); 44cb93a386Sopenharmony_ci ~CustomPropertyManager(); 45cb93a386Sopenharmony_ci 46cb93a386Sopenharmony_ci using PropKey = std::string; 47cb93a386Sopenharmony_ci 48cb93a386Sopenharmony_ci std::vector<PropKey> getColorProps() const; 49cb93a386Sopenharmony_ci skottie::ColorPropertyValue getColor(const PropKey&) const; 50cb93a386Sopenharmony_ci bool setColor(const PropKey&, const skottie::ColorPropertyValue&); 51cb93a386Sopenharmony_ci 52cb93a386Sopenharmony_ci std::vector<PropKey> getOpacityProps() const; 53cb93a386Sopenharmony_ci skottie::OpacityPropertyValue getOpacity(const PropKey&) const; 54cb93a386Sopenharmony_ci bool setOpacity(const PropKey&, const skottie::OpacityPropertyValue&); 55cb93a386Sopenharmony_ci 56cb93a386Sopenharmony_ci std::vector<PropKey> getTransformProps() const; 57cb93a386Sopenharmony_ci skottie::TransformPropertyValue getTransform(const PropKey&) const; 58cb93a386Sopenharmony_ci bool setTransform(const PropKey&, const skottie::TransformPropertyValue&); 59cb93a386Sopenharmony_ci 60cb93a386Sopenharmony_ci std::vector<PropKey> getTextProps() const; 61cb93a386Sopenharmony_ci skottie::TextPropertyValue getText(const PropKey&) const; 62cb93a386Sopenharmony_ci bool setText(const PropKey&, const skottie::TextPropertyValue&); 63cb93a386Sopenharmony_ci 64cb93a386Sopenharmony_ci struct MarkerInfo { 65cb93a386Sopenharmony_ci std::string name; 66cb93a386Sopenharmony_ci float t0, t1; 67cb93a386Sopenharmony_ci }; 68cb93a386Sopenharmony_ci const std::vector<MarkerInfo>& markers() const { return fMarkers; } 69cb93a386Sopenharmony_ci 70cb93a386Sopenharmony_ci // Returns a property observer to be attached to an animation builder. 71cb93a386Sopenharmony_ci sk_sp<skottie::PropertyObserver> getPropertyObserver() const; 72cb93a386Sopenharmony_ci 73cb93a386Sopenharmony_ci // Returns a marker observer to be attached to an animation builder. 74cb93a386Sopenharmony_ci sk_sp<skottie::MarkerObserver> getMarkerObserver() const; 75cb93a386Sopenharmony_ci 76cb93a386Sopenharmony_ciprivate: 77cb93a386Sopenharmony_ci class PropertyInterceptor; 78cb93a386Sopenharmony_ci class MarkerInterceptor; 79cb93a386Sopenharmony_ci 80cb93a386Sopenharmony_ci std::string acceptKey(const char*, const char*) const; 81cb93a386Sopenharmony_ci 82cb93a386Sopenharmony_ci template <typename T> 83cb93a386Sopenharmony_ci using PropGroup = std::vector<std::unique_ptr<T>>; 84cb93a386Sopenharmony_ci 85cb93a386Sopenharmony_ci template <typename T> 86cb93a386Sopenharmony_ci using PropMap = std::unordered_map<PropKey, PropGroup<T>>; 87cb93a386Sopenharmony_ci 88cb93a386Sopenharmony_ci template <typename T> 89cb93a386Sopenharmony_ci std::vector<PropKey> getProps(const PropMap<T>& container) const; 90cb93a386Sopenharmony_ci 91cb93a386Sopenharmony_ci template <typename V, typename T> 92cb93a386Sopenharmony_ci V get(const PropKey&, const PropMap<T>& container) const; 93cb93a386Sopenharmony_ci 94cb93a386Sopenharmony_ci template <typename V, typename T> 95cb93a386Sopenharmony_ci bool set(const PropKey&, const V&, const PropMap<T>& container); 96cb93a386Sopenharmony_ci 97cb93a386Sopenharmony_ci const Mode fMode; 98cb93a386Sopenharmony_ci const SkString fPrefix; 99cb93a386Sopenharmony_ci 100cb93a386Sopenharmony_ci sk_sp<PropertyInterceptor> fPropertyInterceptor; 101cb93a386Sopenharmony_ci sk_sp<MarkerInterceptor> fMarkerInterceptor; 102cb93a386Sopenharmony_ci 103cb93a386Sopenharmony_ci PropMap<skottie::ColorPropertyHandle> fColorMap; 104cb93a386Sopenharmony_ci PropMap<skottie::OpacityPropertyHandle> fOpacityMap; 105cb93a386Sopenharmony_ci PropMap<skottie::TransformPropertyHandle> fTransformMap; 106cb93a386Sopenharmony_ci PropMap<skottie::TextPropertyHandle> fTextMap; 107cb93a386Sopenharmony_ci std::vector<MarkerInfo> fMarkers; 108cb93a386Sopenharmony_ci std::string fCurrentNode; 109cb93a386Sopenharmony_ci}; 110cb93a386Sopenharmony_ci 111cb93a386Sopenharmony_ci/** 112cb93a386Sopenharmony_ci * A sample PrecompInterceptor implementation. 113cb93a386Sopenharmony_ci * 114cb93a386Sopenharmony_ci * Attempts to substitute all precomp layers matching the given pattern (name prefix) 115cb93a386Sopenharmony_ci * with external Lottie animations. 116cb93a386Sopenharmony_ci */ 117cb93a386Sopenharmony_ciclass ExternalAnimationPrecompInterceptor final : public skottie::PrecompInterceptor { 118cb93a386Sopenharmony_cipublic: 119cb93a386Sopenharmony_ci ExternalAnimationPrecompInterceptor(sk_sp<skresources::ResourceProvider>, const char prefix[]); 120cb93a386Sopenharmony_ci ~ExternalAnimationPrecompInterceptor() override; 121cb93a386Sopenharmony_ci 122cb93a386Sopenharmony_ciprivate: 123cb93a386Sopenharmony_ci sk_sp<skottie::ExternalLayer> onLoadPrecomp(const char[], const char[], const SkSize&) override; 124cb93a386Sopenharmony_ci 125cb93a386Sopenharmony_ci const sk_sp<skresources::ResourceProvider> fResourceProvider; 126cb93a386Sopenharmony_ci const SkString fPrefix; 127cb93a386Sopenharmony_ci}; 128cb93a386Sopenharmony_ci 129cb93a386Sopenharmony_ci 130cb93a386Sopenharmony_ci} // namespace skottie_utils 131cb93a386Sopenharmony_ci 132cb93a386Sopenharmony_ci#endif // SkottieUtils_DEFINED 133