| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#pragma once |
| 18 |
|
| 19 |
#include "VectorDrawable.h" |
| 20 |
|
| 21 |
#include <SkColor.h> |
| 22 |
|
| 23 |
namespace android { |
| 24 |
namespace uirenderer { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class PropertyValuesHolder { |
| 32 |
public: |
| 33 |
virtual void setFraction(float fraction) = 0; |
| 34 |
virtual ~PropertyValuesHolder() {} |
| 35 |
}; |
| 36 |
|
| 37 |
template <typename T> |
| 38 |
class Evaluator { |
| 39 |
public: |
| 40 |
virtual void evaluate(T* out, const T& from, const T& to, float fraction) const {}; |
| 41 |
virtual ~Evaluator() {} |
| 42 |
}; |
| 43 |
|
| 44 |
class FloatEvaluator : public Evaluator<float> { |
| 45 |
public: |
| 46 |
virtual void evaluate(float* out, const float& from, const float& to, |
| 47 |
float fraction) const override { |
| 48 |
*out = from * (1 - fraction) + to * fraction; |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
class ColorEvaluator : public Evaluator<SkColor> { |
| 53 |
public: |
| 54 |
virtual void evaluate(SkColor* outColor, const SkColor& from, const SkColor& to, |
| 55 |
float fraction) const override; |
| 56 |
}; |
| 57 |
|
| 58 |
class PathEvaluator : public Evaluator<PathData> { |
| 59 |
virtual void evaluate(PathData* out, const PathData& from, const PathData& to, |
| 60 |
float fraction) const override; |
| 61 |
}; |
| 62 |
|
| 63 |
template <typename T> |
| 64 |
class PropertyValuesHolderImpl : public PropertyValuesHolder { |
| 65 |
public: |
| 66 |
PropertyValuesHolderImpl(const T& startValue, const T& endValue) |
| 67 |
: mStartValue(startValue), mEndValue(endValue) {} |
| 68 |
void setPropertyDataSource(T* dataSource, int length) { |
| 69 |
mDataSource.insert(mDataSource.begin(), dataSource, dataSource + length); |
| 70 |
} |
| 71 |
|
| 72 |
const T getValueFromData(float fraction) const; |
| 73 |
|
| 74 |
|
| 75 |
const T calculateAnimatedValue(float fraction) const; |
| 76 |
|
| 77 |
protected: |
| 78 |
std::unique_ptr<Evaluator<T>> mEvaluator = nullptr; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
std::vector<T> mDataSource; |
| 84 |
T mStartValue; |
| 85 |
T mEndValue; |
| 86 |
}; |
| 87 |
|
| 88 |
class GroupPropertyValuesHolder : public PropertyValuesHolderImpl<float> { |
| 89 |
public: |
| 90 |
GroupPropertyValuesHolder(VectorDrawable::Group* ptr, int propertyId, float startValue, |
| 91 |
float endValue) |
| 92 |
: PropertyValuesHolderImpl(startValue, endValue), mGroup(ptr), mPropertyId(propertyId) { |
| 93 |
mEvaluator.reset(new FloatEvaluator()); |
| 94 |
} |
| 95 |
void setFraction(float fraction) override; |
| 96 |
|
| 97 |
private: |
| 98 |
VectorDrawable::Group* mGroup; |
| 99 |
int mPropertyId; |
| 100 |
}; |
| 101 |
|
| 102 |
class FullPathColorPropertyValuesHolder : public PropertyValuesHolderImpl<SkColor> { |
| 103 |
public: |
| 104 |
FullPathColorPropertyValuesHolder(VectorDrawable::FullPath* ptr, int propertyId, |
| 105 |
SkColor startValue, SkColor endValue) |
| 106 |
: PropertyValuesHolderImpl(startValue, endValue) |
| 107 |
, mFullPath(ptr) |
| 108 |
, mPropertyId(propertyId) { |
| 109 |
mEvaluator.reset(new ColorEvaluator()); |
| 110 |
} |
| 111 |
void setFraction(float fraction) override; |
| 112 |
static SkColor interpolateColors(SkColor fromColor, SkColor toColor, float fraction); |
| 113 |
|
| 114 |
private: |
| 115 |
VectorDrawable::FullPath* mFullPath; |
| 116 |
int mPropertyId; |
| 117 |
}; |
| 118 |
|
| 119 |
class FullPathPropertyValuesHolder : public PropertyValuesHolderImpl<float> { |
| 120 |
public: |
| 121 |
FullPathPropertyValuesHolder(VectorDrawable::FullPath* ptr, int propertyId, float startValue, |
| 122 |
float endValue) |
| 123 |
: PropertyValuesHolderImpl(startValue, endValue) |
| 124 |
, mFullPath(ptr) |
| 125 |
, mPropertyId(propertyId) { |
| 126 |
mEvaluator.reset(new FloatEvaluator()); |
| 127 |
}; |
| 128 |
void setFraction(float fraction) override; |
| 129 |
|
| 130 |
private: |
| 131 |
VectorDrawable::FullPath* mFullPath; |
| 132 |
int mPropertyId; |
| 133 |
}; |
| 134 |
|
| 135 |
class PathDataPropertyValuesHolder : public PropertyValuesHolderImpl<PathData> { |
| 136 |
public: |
| 137 |
PathDataPropertyValuesHolder(VectorDrawable::Path* ptr, PathData* startValue, |
| 138 |
PathData* endValue) |
| 139 |
: PropertyValuesHolderImpl(*startValue, *endValue), mPath(ptr) { |
| 140 |
mEvaluator.reset(new PathEvaluator()); |
| 141 |
}; |
| 142 |
void setFraction(float fraction) override; |
| 143 |
|
| 144 |
private: |
| 145 |
VectorDrawable::Path* mPath; |
| 146 |
PathData mPathData; |
| 147 |
}; |
| 148 |
|
| 149 |
class RootAlphaPropertyValuesHolder : public PropertyValuesHolderImpl<float> { |
| 150 |
public: |
| 151 |
RootAlphaPropertyValuesHolder(VectorDrawable::Tree* tree, float startValue, float endValue) |
| 152 |
: PropertyValuesHolderImpl(startValue, endValue), mTree(tree) { |
| 153 |
mEvaluator.reset(new FloatEvaluator()); |
| 154 |
} |
| 155 |
void setFraction(float fraction) override; |
| 156 |
|
| 157 |
private: |
| 158 |
VectorDrawable::Tree* mTree; |
| 159 |
}; |
| 160 |
} |
| 161 |
} |