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#ifndef SkottieProperty_DEFINED
9#define SkottieProperty_DEFINED
10
11#include "include/core/SkColor.h"
12#include "include/core/SkPoint.h"
13#include "include/core/SkRefCnt.h"
14#include "include/core/SkTypeface.h"
15#include "include/utils/SkTextUtils.h"
16#include "modules/skottie/src/text/SkottieShaper.h"
17
18#include <functional>
19
20class SkMatrix;
21
22namespace sksg {
23
24class Color;
25class OpacityEffect;
26
27} // namespace sksg
28
29namespace skottie {
30
31using ColorPropertyValue = SkColor;
32using OpacityPropertyValue = float;
33
34struct TextPropertyValue {
35 sk_sp<SkTypeface> fTypeface;
36 SkString fText;
37 float fTextSize = 0,
38 fStrokeWidth = 0,
39 fLineHeight = 0,
40 fAscent = 0;
41 SkTextUtils::Align fHAlign = SkTextUtils::kLeft_Align;
42 Shaper::VAlign fVAlign = Shaper::VAlign::kTop;
43 Shaper::ResizePolicy fResize = Shaper::ResizePolicy::kNone;
44 SkRect fBox = SkRect::MakeEmpty();
45 SkColor fFillColor = SK_ColorTRANSPARENT,
46 fStrokeColor = SK_ColorTRANSPARENT;
47 bool fHasFill = false,
48 fHasStroke = false;
49
50 bool operator==(const TextPropertyValue& other) const;
51 bool operator!=(const TextPropertyValue& other) const;
52};
53
54struct TransformPropertyValue {
55 SkPoint fAnchorPoint,
56 fPosition;
57 SkVector fScale;
58 SkScalar fRotation,
59 fSkew,
60 fSkewAxis;
61
62 bool operator==(const TransformPropertyValue& other) const;
63 bool operator!=(const TransformPropertyValue& other) const;
64};
65
66namespace internal { class AnimationBuilder; }
67
68/**
69 * Property handles are adapters between user-facing AE model/values
70 * and the internal scene-graph representation.
71 */
72template <typename ValueT, typename NodeT>
73class SK_API PropertyHandle final {
74public:
75 explicit PropertyHandle(sk_sp<NodeT> node) : fNode(std::move(node)) {}
76 ~PropertyHandle();
77
78 ValueT get() const;
79 void set(const ValueT&);
80
81private:
82 const sk_sp<NodeT> fNode;
83};
84
85namespace internal {
86
87class TextAdapter;
88class TransformAdapter2D;
89
90} // namespace internal
91
92using ColorPropertyHandle = PropertyHandle<ColorPropertyValue,
93 sksg::Color>;
94using OpacityPropertyHandle = PropertyHandle<OpacityPropertyValue,
95 sksg::OpacityEffect>;
96using TextPropertyHandle = PropertyHandle<TextPropertyValue,
97 internal::TextAdapter>;
98using TransformPropertyHandle = PropertyHandle<TransformPropertyValue,
99 internal::TransformAdapter2D>;
100
101/**
102 * A PropertyObserver can be used to track and manipulate certain properties of "interesting"
103 * Lottie nodes.
104 *
105 * When registered with an animation builder, PropertyObserver receives notifications for
106 * various properties of layer and shape nodes. The |node_name| argument corresponds to the
107 * name ("nm") node property.
108 */
109class SK_API PropertyObserver : public SkRefCnt {
110public:
111 template <typename T>
112 using LazyHandle = std::function<std::unique_ptr<T>()>;
113
114 virtual void onColorProperty (const char node_name[],
115 const LazyHandle<ColorPropertyHandle>&);
116 virtual void onOpacityProperty (const char node_name[],
117 const LazyHandle<OpacityPropertyHandle>&);
118 virtual void onTextProperty (const char node_name[],
119 const LazyHandle<TextPropertyHandle>&);
120 virtual void onTransformProperty(const char node_name[],
121 const LazyHandle<TransformPropertyHandle>&);
122 virtual void onEnterNode(const char node_name[]);
123 virtual void onLeavingNode(const char node_name[]);
124};
125
126} // namespace skottie
127
128#endif // SkottieProperty_DEFINED
129