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