forked from overtake/telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
POPPropertyAnimationInternal.h
executable file
·353 lines (292 loc) · 8.65 KB
/
POPPropertyAnimationInternal.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
Copyright (c) 2014-present, Facebook, Inc.
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
*/
#import "POPAnimationInternal.h"
#import "POPPropertyAnimation.h"
static void clampValue(CGFloat &value, CGFloat fromValue, CGFloat toValue, NSUInteger clamp)
{
BOOL increasing = (toValue > fromValue);
// Clamp start of animation.
if ((kPOPAnimationClampStart & clamp) &&
((increasing && (value < fromValue)) || (!increasing && (value > fromValue)))) {
value = fromValue;
}
// Clamp end of animation.
if ((kPOPAnimationClampEnd & clamp) &&
((increasing && (value > toValue)) || (!increasing && (value < toValue)))) {
value = toValue;
}
}
struct _POPPropertyAnimationState : _POPAnimationState
{
POPAnimatableProperty *property;
POPValueType valueType;
NSUInteger valueCount;
VectorRef fromVec;
VectorRef toVec;
VectorRef currentVec;
VectorRef previousVec;
VectorRef previous2Vec;
VectorRef velocityVec;
VectorRef originalVelocityVec;
VectorRef distanceVec;
CGFloat roundingFactor;
NSUInteger clampMode;
NSArray *progressMarkers;
POPProgressMarker *progressMarkerState;
NSUInteger progressMarkerCount;
NSUInteger nextProgressMarkerIdx;
CGFloat dynamicsThreshold;
_POPPropertyAnimationState(id __unsafe_unretained anim) : _POPAnimationState(anim),
property(nil),
valueType((POPValueType)0),
valueCount(0),
fromVec(nullptr),
toVec(nullptr),
currentVec(nullptr),
previousVec(nullptr),
previous2Vec(nullptr),
velocityVec(nullptr),
originalVelocityVec(nullptr),
distanceVec(nullptr),
roundingFactor(0),
clampMode(0),
progressMarkers(nil),
progressMarkerState(nil),
progressMarkerCount(0),
nextProgressMarkerIdx(0),
dynamicsThreshold(0)
{
type = kPOPAnimationBasic;
}
~_POPPropertyAnimationState()
{
if (progressMarkerState) {
free(progressMarkerState);
progressMarkerState = NULL;
}
}
bool canProgress() {
return hasValue();
}
bool shouldRound() {
return 0 != roundingFactor;
}
bool hasValue() {
return 0 != valueCount;
}
bool isDone() {
// inherit done
if (_POPAnimationState::isDone()) {
return true;
}
// consider an animation with no values done
if (!hasValue() && !isCustom()) {
return true;
}
return false;
}
// returns a copy of the currentVec, rounding if needed
VectorRef currentValue() {
VectorRef vec = VectorRef(Vector::new_vector(currentVec.get()));
if (shouldRound()) {
vec->subRound(1 / roundingFactor);
}
return vec;
}
void resetProgressMarkerState()
{
for (NSUInteger idx = 0; idx < progressMarkerCount; idx++)
progressMarkerState[idx].reached = false;
nextProgressMarkerIdx = 0;
}
void updatedProgressMarkers()
{
if (progressMarkerState) {
free(progressMarkerState);
progressMarkerState = NULL;
}
progressMarkerCount = progressMarkers.count;
if (0 != progressMarkerCount) {
progressMarkerState = (POPProgressMarker *)malloc(progressMarkerCount * sizeof(POPProgressMarker));
[progressMarkers enumerateObjectsUsingBlock:^(NSNumber *progressMarker, NSUInteger idx, BOOL *stop) {
progressMarkerState[idx].reached = false;
progressMarkerState[idx].progress = [progressMarker floatValue];
}];
}
nextProgressMarkerIdx = 0;
}
virtual void updatedDynamicsThreshold()
{
dynamicsThreshold = property.threshold;
}
void finalizeProgress()
{
progress = 1.0;
NSUInteger count = valueCount;
VectorRef outVec(Vector::new_vector(count, NULL));
if (outVec && toVec) {
*outVec = *toVec;
}
currentVec = outVec;
clampCurrentValue();
delegateProgress();
}
void computeProgress() {
if (!canProgress()) {
return;
}
static ComputeProgressFunctor<Vector4r> func;
Vector4r v = vector4(currentVec);
Vector4r f = vector4(fromVec);
Vector4r t = vector4(toVec);
progress = func(v, f, t);
}
void delegateProgress() {
if (!canProgress()) {
return;
}
if (delegateDidProgress && progressMarkerState) {
while (nextProgressMarkerIdx < progressMarkerCount) {
if (progress < progressMarkerState[nextProgressMarkerIdx].progress)
break;
if (!progressMarkerState[nextProgressMarkerIdx].reached) {
ActionEnabler enabler;
[delegate pop_animation:self didReachProgress:progressMarkerState[nextProgressMarkerIdx].progress];
progressMarkerState[nextProgressMarkerIdx].reached = true;
}
nextProgressMarkerIdx++;
}
}
if (!didReachToValue) {
bool didReachToValue = false;
if (0 == valueCount) {
didReachToValue = true;
} else {
Vector4r distance = toVec->vector4r();
distance -= currentVec->vector4r();
if (0 == distance.squaredNorm()) {
didReachToValue = true;
} else {
// components
if (distanceVec) {
didReachToValue = true;
const CGFloat *distanceValues = distanceVec->data();
for (NSUInteger idx = 0; idx < valueCount; idx++) {
didReachToValue &= (signbit(distance[idx]) != signbit(distanceValues[idx]));
}
}
}
}
if (didReachToValue) {
handleDidReachToValue();
}
}
}
void handleDidReachToValue() {
didReachToValue = true;
if (delegateDidReachToValue) {
ActionEnabler enabler;
[delegate pop_animationDidReachToValue:self];
}
if (tracing) {
[tracer didReachToValue:POPBox(currentValue(), valueType, true)];
}
}
void readObjectValue(VectorRef *ptrVec, id obj)
{
// use current object value as from value
pop_animatable_read_block read = property.readBlock;
if (NULL != read) {
Vector4r vec = read_values(read, obj, valueCount);
*ptrVec = VectorRef(Vector::new_vector(valueCount, vec));
if (tracing) {
[tracer readPropertyValue:POPBox(*ptrVec, valueType, true)];
}
}
}
virtual void willRun(bool started, id obj) {
// ensure from value initialized
if (NULL == fromVec) {
readObjectValue(&fromVec, obj);
}
// ensure to value initialized
if (NULL == toVec) {
// compute decay to value
if (kPOPAnimationDecay == type) {
[self toValue];
} else {
// read to value
readObjectValue(&toVec, obj);
}
}
// handle one time value initialization on start
if (started) {
// initialize current vec
if (!currentVec) {
currentVec = VectorRef(Vector::new_vector(valueCount, NULL));
// initialize current value with from value
// only do this on initial creation to avoid overwriting current value
// on paused animation continuation
if (currentVec && fromVec) {
*currentVec = *fromVec;
}
}
// ensure velocity values
if (!velocityVec) {
velocityVec = VectorRef(Vector::new_vector(valueCount, NULL));
}
if (!originalVelocityVec) {
originalVelocityVec = VectorRef(Vector::new_vector(valueCount, NULL));
}
}
// ensure distance value initialized
// depends on current value set on one time start
if (NULL == distanceVec) {
// not yet started animations may not have current value
VectorRef fromVec2 = NULL != currentVec ? currentVec : fromVec;
if (fromVec2 && toVec) {
Vector4r distance = toVec->vector4r();
distance -= fromVec2->vector4r();
if (0 != distance.squaredNorm()) {
distanceVec = VectorRef(Vector::new_vector(valueCount, distance));
}
}
}
}
virtual void reset(bool all) {
_POPAnimationState::reset(all);
if (all) {
currentVec = NULL;
previousVec = NULL;
previous2Vec = NULL;
}
progress = 0;
resetProgressMarkerState();
didReachToValue = false;
distanceVec = NULL;
}
void clampCurrentValue(NSUInteger clamp)
{
if (kPOPAnimationClampNone == clamp)
return;
// Clamp all vector values
CGFloat *currentValues = currentVec->data();
const CGFloat *fromValues = fromVec->data();
const CGFloat *toValues = toVec->data();
for (NSUInteger idx = 0; idx < valueCount; idx++) {
clampValue(currentValues[idx], fromValues[idx], toValues[idx], clamp);
}
}
void clampCurrentValue()
{
clampCurrentValue(clampMode);
}
};
typedef struct _POPPropertyAnimationState POPPropertyAnimationState;
@interface POPPropertyAnimation ()
@end