blob: 432f8ba82afb660d347208e7f8fcad3290bf12c4 [file] [log] [blame]
Doris Liu766431a2016-02-04 22:17:11 +00001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include "VectorDrawable.h"
20
21#include <SkColor.h>
22
23namespace android {
24namespace uirenderer {
25
26/**
27 * PropertyValues holder contains data needed to change a property of a Vector Drawable object.
28 * When a fraction in [0f, 1f] is provided, the holder will calculate an interpolated value based
29 * on its start and end value, and set the new value on the VectorDrawble's corresponding property.
30 */
31class ANDROID_API PropertyValuesHolder {
32public:
33 virtual void setFraction(float fraction) = 0;
Doris Liua6b967c2016-06-02 16:20:18 -070034 virtual ~PropertyValuesHolder() {}
Doris Liu766431a2016-02-04 22:17:11 +000035};
36
Doris Liua6b967c2016-06-02 16:20:18 -070037template <typename T>
38class Evaluator {
39public:
40 virtual void evaluate(T* out, const T& from, const T& to, float fraction) const {};
41 virtual ~Evaluator() {}
42};
43
44class FloatEvaluator : public Evaluator<float> {
45public:
46 virtual void evaluate(float* out, const float& from, const float& to, float fraction)
47 const override {
48 *out = from * (1 - fraction) + to * fraction;
49 }
50};
51
52class ANDROID_API ColorEvaluator : public Evaluator<SkColor> {
53public:
54 virtual void evaluate(SkColor* outColor, const SkColor& from, const SkColor& to,
55 float fraction) const override;
56};
57
58class ANDROID_API PathEvaluator : public Evaluator<PathData> {
59 virtual void evaluate(PathData* out, const PathData& from, const PathData& to, float fraction)
60 const override;
61};
62
63template <typename T>
64class ANDROID_API PropertyValuesHolderImpl : public PropertyValuesHolder {
65public:
66 PropertyValuesHolderImpl(const T& startValue, const T& endValue)
67 : mStartValue(startValue)
68 , mEndValue(endValue) {}
69 void setPropertyDataSource(T* dataSource, int length) {
70 mDataSource.insert(mDataSource.begin(), dataSource, dataSource + length);
71 }
72 // Calculate the animated value from the data source.
73 const T getValueFromData(float fraction) const;
74 // Convenient method to favor getting animated value from data source. If no data source is set
75 // fall back to linear interpolation.
76 const T calculateAnimatedValue(float fraction) const;
77protected:
78 std::unique_ptr<Evaluator<T>> mEvaluator = nullptr;
79 // This contains uniformly sampled data throughout the animation duration. The first element
80 // should be the start value and the last should be the end value of the animation. When the
81 // data source is set, we'll favor data source over the linear interpolation of start/end value
82 // for calculation of animated value.
83 std::vector<T> mDataSource;
84 T mStartValue;
85 T mEndValue;
86};
87
88class ANDROID_API GroupPropertyValuesHolder : public PropertyValuesHolderImpl<float> {
Doris Liu766431a2016-02-04 22:17:11 +000089public:
90 GroupPropertyValuesHolder(VectorDrawable::Group* ptr, int propertyId, float startValue,
91 float endValue)
Doris Liua6b967c2016-06-02 16:20:18 -070092 : PropertyValuesHolderImpl(startValue, endValue)
93 , mGroup(ptr)
94 , mPropertyId(propertyId) {
95 mEvaluator.reset(new FloatEvaluator());
Doris Liu766431a2016-02-04 22:17:11 +000096 }
97 void setFraction(float fraction) override;
98private:
99 VectorDrawable::Group* mGroup;
100 int mPropertyId;
Doris Liu766431a2016-02-04 22:17:11 +0000101};
102
Doris Liua6b967c2016-06-02 16:20:18 -0700103class ANDROID_API FullPathColorPropertyValuesHolder : public PropertyValuesHolderImpl<SkColor> {
Doris Liu766431a2016-02-04 22:17:11 +0000104public:
Doris Liua6b967c2016-06-02 16:20:18 -0700105 FullPathColorPropertyValuesHolder(VectorDrawable::FullPath* ptr, int propertyId,
106 SkColor startValue, SkColor endValue)
107 : PropertyValuesHolderImpl(startValue, endValue)
108 , mFullPath(ptr)
109 , mPropertyId(propertyId) {
110 mEvaluator.reset(new ColorEvaluator());
111 }
Doris Liu766431a2016-02-04 22:17:11 +0000112 void setFraction(float fraction) override;
113 static SkColor interpolateColors(SkColor fromColor, SkColor toColor, float fraction);
114private:
115 VectorDrawable::FullPath* mFullPath;
116 int mPropertyId;
Doris Liu766431a2016-02-04 22:17:11 +0000117};
118
Doris Liua6b967c2016-06-02 16:20:18 -0700119class ANDROID_API FullPathPropertyValuesHolder : public PropertyValuesHolderImpl<float> {
Doris Liu766431a2016-02-04 22:17:11 +0000120public:
121 FullPathPropertyValuesHolder(VectorDrawable::FullPath* ptr, int propertyId, float startValue,
122 float endValue)
Doris Liua6b967c2016-06-02 16:20:18 -0700123 : PropertyValuesHolderImpl(startValue, endValue)
124 , mFullPath(ptr)
125 , mPropertyId(propertyId) {
126 mEvaluator.reset(new FloatEvaluator());
127 };
Doris Liu766431a2016-02-04 22:17:11 +0000128 void setFraction(float fraction) override;
129private:
130 VectorDrawable::FullPath* mFullPath;
131 int mPropertyId;
Doris Liu766431a2016-02-04 22:17:11 +0000132};
133
Doris Liua6b967c2016-06-02 16:20:18 -0700134class ANDROID_API PathDataPropertyValuesHolder : public PropertyValuesHolderImpl<PathData> {
Doris Liu766431a2016-02-04 22:17:11 +0000135public:
136 PathDataPropertyValuesHolder(VectorDrawable::Path* ptr, PathData* startValue,
137 PathData* endValue)
Doris Liua6b967c2016-06-02 16:20:18 -0700138 : PropertyValuesHolderImpl(*startValue, *endValue)
139 , mPath(ptr) {
140 mEvaluator.reset(new PathEvaluator());
141 };
Doris Liu766431a2016-02-04 22:17:11 +0000142 void setFraction(float fraction) override;
143private:
144 VectorDrawable::Path* mPath;
145 PathData mPathData;
Doris Liu766431a2016-02-04 22:17:11 +0000146};
147
Doris Liua6b967c2016-06-02 16:20:18 -0700148class ANDROID_API RootAlphaPropertyValuesHolder : public PropertyValuesHolderImpl<float> {
Doris Liu766431a2016-02-04 22:17:11 +0000149public:
150 RootAlphaPropertyValuesHolder(VectorDrawable::Tree* tree, float startValue, float endValue)
Doris Liua6b967c2016-06-02 16:20:18 -0700151 : PropertyValuesHolderImpl(startValue, endValue)
152 , mTree(tree) {
153 mEvaluator.reset(new FloatEvaluator());
154 }
Doris Liu766431a2016-02-04 22:17:11 +0000155 void setFraction(float fraction) override;
156private:
157 VectorDrawable::Tree* mTree;
Doris Liu766431a2016-02-04 22:17:11 +0000158};
159}
160}