|
9 | 9 | import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
|
10 | 10 | import com.facebook.react.bridge.ReadableArray;
|
11 | 11 | import com.facebook.react.bridge.ReadableMap;
|
| 12 | +import com.facebook.react.bridge.ReadableType; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.regex.Matcher; |
| 16 | +import java.util.regex.Pattern; |
| 17 | + |
12 | 18 | import javax.annotation.Nullable;
|
13 | 19 |
|
14 | 20 | /**
|
|
21 | 27 | public static final String EXTRAPOLATE_TYPE_IDENTITY = "identity";
|
22 | 28 | public static final String EXTRAPOLATE_TYPE_CLAMP = "clamp";
|
23 | 29 | public static final String EXTRAPOLATE_TYPE_EXTEND = "extend";
|
| 30 | + static final Pattern regex = Pattern.compile("[0-9.-]+"); |
24 | 31 |
|
25 | 32 | private static double[] fromDoubleArray(ReadableArray ary) {
|
26 | 33 | double[] res = new double[ary.size()];
|
@@ -105,13 +112,68 @@ private static int findRangeIndex(double value, double[] ranges) {
|
105 | 112 |
|
106 | 113 | private final double mInputRange[];
|
107 | 114 | private final double mOutputRange[];
|
| 115 | + private String mPattern; |
| 116 | + private double mOutputs[][]; |
| 117 | + private final boolean mHasStringOutput; |
| 118 | + private final Matcher mSOutputMatcher; |
108 | 119 | private final String mExtrapolateLeft;
|
109 | 120 | private final String mExtrapolateRight;
|
110 | 121 | private @Nullable ValueAnimatedNode mParent;
|
| 122 | + private boolean mShouldRound; |
| 123 | + private int mNumVals; |
111 | 124 |
|
112 | 125 | public InterpolationAnimatedNode(ReadableMap config) {
|
113 | 126 | mInputRange = fromDoubleArray(config.getArray("inputRange"));
|
114 |
| - mOutputRange = fromDoubleArray(config.getArray("outputRange")); |
| 127 | + ReadableArray output = config.getArray("outputRange"); |
| 128 | + mHasStringOutput = output.getType(0) == ReadableType.String; |
| 129 | + if (mHasStringOutput) { |
| 130 | + /* |
| 131 | + * Supports string shapes by extracting numbers so new values can be computed, |
| 132 | + * and recombines those values into new strings of the same shape. Supports |
| 133 | + * things like: |
| 134 | + * |
| 135 | + * rgba(123, 42, 99, 0.36) // colors |
| 136 | + * -45deg // values with units |
| 137 | + */ |
| 138 | + int size = output.size(); |
| 139 | + mOutputRange = new double[size]; |
| 140 | + mPattern = output.getString(0); |
| 141 | + mShouldRound = mPattern.startsWith("rgb"); |
| 142 | + mSOutputMatcher = regex.matcher(mPattern); |
| 143 | + ArrayList<ArrayList<Double>> mOutputRanges = new ArrayList<>(); |
| 144 | + for (int i = 0; i < size; i++) { |
| 145 | + String val = output.getString(i); |
| 146 | + Matcher m = regex.matcher(val); |
| 147 | + ArrayList<Double> outputRange = new ArrayList<>(); |
| 148 | + mOutputRanges.add(outputRange); |
| 149 | + while (m.find()) { |
| 150 | + Double parsed = Double.parseDouble(m.group()); |
| 151 | + outputRange.add(parsed); |
| 152 | + } |
| 153 | + mOutputRange[i] = outputRange.get(0); |
| 154 | + } |
| 155 | + |
| 156 | + // ['rgba(0, 100, 200, 0)', 'rgba(50, 150, 250, 0.5)'] |
| 157 | + // -> |
| 158 | + // [ |
| 159 | + // [0, 50], |
| 160 | + // [100, 150], |
| 161 | + // [200, 250], |
| 162 | + // [0, 0.5], |
| 163 | + // ] |
| 164 | + mNumVals = mOutputRanges.get(0).size(); |
| 165 | + mOutputs = new double[mNumVals][]; |
| 166 | + for (int j = 0; j < mNumVals; j++) { |
| 167 | + double[] arr = new double[size]; |
| 168 | + mOutputs[j] = arr; |
| 169 | + for (int i = 0; i < size; i++) { |
| 170 | + arr[i] = mOutputRanges.get(i).get(j); |
| 171 | + } |
| 172 | + } |
| 173 | + } else { |
| 174 | + mOutputRange = fromDoubleArray(output); |
| 175 | + mSOutputMatcher = null; |
| 176 | + } |
115 | 177 | mExtrapolateLeft = config.getString("extrapolateLeft");
|
116 | 178 | mExtrapolateRight = config.getString("extrapolateRight");
|
117 | 179 | }
|
@@ -142,6 +204,33 @@ public void update() {
|
142 | 204 | // unattached node.
|
143 | 205 | return;
|
144 | 206 | }
|
145 |
| - mValue = interpolate(mParent.getValue(), mInputRange, mOutputRange, mExtrapolateLeft, mExtrapolateRight); |
| 207 | + double value = mParent.getValue(); |
| 208 | + mValue = interpolate(value, mInputRange, mOutputRange, mExtrapolateLeft, mExtrapolateRight); |
| 209 | + if (mHasStringOutput) { |
| 210 | + // 'rgba(0, 100, 200, 0)' |
| 211 | + // -> |
| 212 | + // 'rgba(${interpolations[0](input)}, ${interpolations[1](input)}, ...' |
| 213 | + if (mNumVals > 1) { |
| 214 | + StringBuffer sb = new StringBuffer(mPattern.length()); |
| 215 | + int i = 0; |
| 216 | + mSOutputMatcher.reset(); |
| 217 | + while (mSOutputMatcher.find()) { |
| 218 | + double val = interpolate(value, mInputRange, mOutputs[i++], mExtrapolateLeft, mExtrapolateRight); |
| 219 | + if (mShouldRound) { |
| 220 | + // rgba requires that the r,g,b are integers.... so we want to round them, but we *dont* want to |
| 221 | + // round the opacity (4th column). |
| 222 | + boolean isAlpha = i == 4; |
| 223 | + int rounded = (int)Math.round(isAlpha ? val * 1000 : val); |
| 224 | + mSOutputMatcher.appendReplacement(sb, isAlpha ? String.valueOf((double)rounded / 1000) : String.valueOf(rounded)); |
| 225 | + } else { |
| 226 | + mSOutputMatcher.appendReplacement(sb, String.valueOf(val)); |
| 227 | + } |
| 228 | + } |
| 229 | + mSOutputMatcher.appendTail(sb); |
| 230 | + mAnimatedObject = sb.toString(); |
| 231 | + } else { |
| 232 | + mAnimatedObject = mSOutputMatcher.replaceFirst(String.valueOf(mValue)); |
| 233 | + } |
| 234 | + } |
146 | 235 | }
|
147 | 236 | }
|
0 commit comments