forked from overtake/telegram
-
Notifications
You must be signed in to change notification settings - Fork 6
/
POPAnimationInternal.h
executable file
·480 lines (397 loc) · 11 KB
/
POPAnimationInternal.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/**
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 <QuartzCore/CAMediaTimingFunction.h>
#import "POPAnimation.h"
#import "POPAnimationRuntime.h"
#import "POPAnimationTracerInternal.h"
#import "POPSpringSolver.h"
#import "POPAction.h"
using namespace POP;
/**
Enumeration of supported animation types.
*/
enum POPAnimationType
{
kPOPAnimationSpring,
kPOPAnimationDecay,
kPOPAnimationBasic,
kPOPAnimationCustom,
};
typedef struct
{
CGFloat progress;
bool reached;
} POPProgressMarker;
typedef void (^POPAnimationCompletionBlock)(POPAnimation *anim, BOOL finished);
@interface POPAnimation()
- (instancetype)_init;
@property (assign, nonatomic) SpringSolver4d *solver;
@property (readonly, nonatomic) POPAnimationType type;
/**
The current animation value, updated while animation is progressing.
*/
@property (copy, nonatomic, readonly) id currentValue;
/**
An array of optional progress markers. For each marker specified, the animation delegate will be informed when progress meets or exceeds the value specified. Specifying values outside of the [0, 1] range will give undefined results.
*/
@property (copy, nonatomic) NSArray *progressMarkers;
/**
Return YES to indicate animation should continue animating.
*/
- (BOOL)_advance:(id)object currentTime:(CFTimeInterval)currentTime elapsedTime:(CFTimeInterval)elapsedTime;
/**
Subclass override point to append animation description.
*/
- (void)_appendDescription:(NSMutableString *)s debug:(BOOL)debug;
@end
NS_INLINE NSString *describe(VectorConstRef vec)
{
return NULL == vec ? @"null" : vec->toString();
}
NS_INLINE Vector4r vector4(VectorConstRef vec)
{
return NULL == vec ? Vector4r::Zero() : vec->vector4r();
}
NS_INLINE Vector4d vector4d(VectorConstRef vec)
{
if (NULL == vec) {
return Vector4d::Zero();
} else {
return vec->vector4r().cast<double>();
}
}
NS_INLINE bool vec_equal(VectorConstRef v1, VectorConstRef v2)
{
if (v1 == v2) {
return true;
}
if (!v1 || !v2) {
return false;
}
return *v1 == *v2;
}
NS_INLINE CGFloat * vec_data(VectorRef vec)
{
return NULL == vec ? NULL : vec->data();
}
template<class T>
struct ComputeProgressFunctor {
CGFloat operator()(const T &value, const T &start, const T &end) const {
return 0;
}
};
template<>
struct ComputeProgressFunctor<Vector4r> {
CGFloat operator()(const Vector4r &value, const Vector4r &start, const Vector4r &end) const {
CGFloat s = (value - start).squaredNorm(); // distance from start
CGFloat e = (value - end).squaredNorm(); // distance from end
CGFloat d = (end - start).squaredNorm(); // distance from start to end
if (0 == d) {
return 1;
} else if (s > e) {
// s -------- p ---- e OR s ------- e ---- p
return sqrtr(s/d);
} else {
// s --- p --------- e OR p ---- s ------- e
return 1 - sqrtr(e/d);
}
}
};
struct _POPAnimationState;
struct _POPDecayAnimationState;
struct _POPPropertyAnimationState;
extern _POPAnimationState *POPAnimationGetState(POPAnimation *a);
#define FB_FLAG_GET(stype, flag, getter) \
- (BOOL)getter { \
return ((stype *)_state)->flag; \
}
#define FB_FLAG_SET(stype, flag, mutator) \
- (void)mutator (BOOL)value { \
if (value == ((stype *)_state)->flag) \
return; \
((stype *)_state)->flag = value; \
}
#define DEFINE_RW_FLAG(stype, flag, getter, mutator) \
FB_FLAG_GET (stype, flag, getter) \
FB_FLAG_SET (stype, flag, mutator)
#define FB_PROPERTY_GET(stype, property, ctype) \
- (ctype)property { \
return ((stype *)_state)->property; \
}
#define FB_PROPERTY_SET(stype, property, mutator, ctype, ...) \
- (void)mutator (ctype)value { \
if (value == ((stype *)_state)->property) \
return; \
((stype *)_state)->property = value; \
__VA_ARGS__ \
}
#define FB_PROPERTY_SET_OBJ_COPY(stype, property, mutator, ctype, ...) \
- (void)mutator (ctype)value { \
if (value == ((stype *)_state)->property) \
return; \
((stype *)_state)->property = [value copy]; \
__VA_ARGS__ \
}
#define DEFINE_RW_PROPERTY(stype, flag, mutator, ctype, ...) \
FB_PROPERTY_GET (stype, flag, ctype) \
FB_PROPERTY_SET (stype, flag, mutator, ctype, __VA_ARGS__)
#define DEFINE_RW_PROPERTY_OBJ(stype, flag, mutator, ctype, ...) \
FB_PROPERTY_GET (stype, flag, ctype) \
FB_PROPERTY_SET (stype, flag, mutator, ctype, __VA_ARGS__)
#define DEFINE_RW_PROPERTY_OBJ_COPY(stype, flag, mutator, ctype, ...) \
FB_PROPERTY_GET (stype, flag, ctype) \
FB_PROPERTY_SET_OBJ_COPY (stype, flag, mutator, ctype, __VA_ARGS__)
/**
Internal delegate definition.
*/
@interface NSObject (POPAnimationDelegateInternal)
- (void)pop_animation:(POPAnimation *)anim didReachProgress:(CGFloat)progress;
@end
struct _POPAnimationState
{
id __unsafe_unretained self;
POPAnimationType type;
NSString *name;
NSUInteger ID;
CFTimeInterval beginTime;
CFTimeInterval startTime;
CFTimeInterval lastTime;
id __weak delegate;
POPAnimationCompletionBlock completionBlock;
NSMutableDictionary *dict;
POPAnimationTracer *tracer;
CGFloat progress;
NSInteger repeatCount;
bool active:1;
bool paused:1;
bool removedOnCompletion:1;
bool delegateDidStart:1;
bool delegateDidStop:1;
bool delegateDidProgress:1;
bool delegateDidApply:1;
bool delegateDidReachToValue:1;
bool additive:1;
bool didReachToValue:1;
bool tracing:1; // corresponds to tracer started
bool userSpecifiedDynamics:1;
bool autoreverses:1;
bool repeatForever:1;
bool customFinished:1;
_POPAnimationState(id __unsafe_unretained anim) :
self(anim),
type((POPAnimationType)0),
name(nil),
ID(0),
beginTime(0),
startTime(0),
lastTime(0),
delegate(nil),
completionBlock(nil),
dict(nil),
tracer(nil),
progress(0),
repeatCount(0),
active(false),
paused(true),
removedOnCompletion(true),
delegateDidStart(false),
delegateDidStop(false),
delegateDidProgress(false),
delegateDidApply(false),
delegateDidReachToValue(false),
additive(false),
didReachToValue(false),
tracing(false),
userSpecifiedDynamics(false),
autoreverses(false),
repeatForever(false),
customFinished(false) {}
virtual ~_POPAnimationState()
{
name = nil;
dict = nil;
tracer = nil;
completionBlock = NULL;
}
bool isCustom() {
return kPOPAnimationCustom == type;
}
bool isStarted() {
return 0 != startTime;
}
id getDelegate() {
return delegate;
}
void setDelegate(id d) {
if (d != delegate) {
delegate = d;
delegateDidStart = [d respondsToSelector:@selector(pop_animationDidStart:)];
delegateDidStop = [d respondsToSelector:@selector(pop_animationDidStop:finished:)];
delegateDidProgress = [d respondsToSelector:@selector(pop_animation:didReachProgress:)];
delegateDidApply = [d respondsToSelector:@selector(pop_animationDidApply:)];
delegateDidReachToValue = [d respondsToSelector:@selector(pop_animationDidReachToValue:)];
}
}
bool getPaused() {
return paused;
}
void setPaused(bool f) {
if (f != paused) {
paused = f;
if (!paused) {
reset(false);
}
}
}
CGFloat getProgress() {
return progress;
}
/* returns true if started */
bool startIfNeeded(id obj, CFTimeInterval time, CFTimeInterval offset)
{
bool started = false;
// detect start based on time
if (0 == startTime && time >= beginTime + offset) {
// activate & unpause
active = true;
setPaused(false);
// note start time
startTime = lastTime = time;
started = true;
}
// ensure values for running animation
bool running = active && !paused;
if (running) {
willRun(started, obj);
}
// handle start
if (started) {
handleDidStart();
}
return started;
}
void stop(bool removing, bool done) {
if (active)
{
// delegate progress one last time
if (done) {
delegateProgress();
}
if (removing) {
active = false;
}
handleDidStop(done);
} else {
// stopped before even started
// delegate start and stop regardless; matches CA behavior
if (!isStarted()) {
handleDidStart();
handleDidStop(false);
}
}
setPaused(true);
}
virtual void handleDidStart()
{
if (delegateDidStart) {
ActionEnabler enabler;
[delegate pop_animationDidStart:self];
}
if (tracing) {
[tracer didStart];
}
}
void handleDidStop(BOOL done)
{
if (delegateDidStop) {
ActionEnabler enabler;
[delegate pop_animationDidStop:self finished:done];
}
// add another strong reference to completion block before callout
POPAnimationCompletionBlock block = completionBlock;
if (block != NULL) {
ActionEnabler enabler;
block(self, done);
}
if (tracing) {
[tracer didStop:done];
}
}
/* virtual functions */
virtual bool isDone() {
if (isCustom()) {
return customFinished;
}
return false;
}
bool advanceTime(CFTimeInterval time, id obj) {
bool advanced = false;
bool computedProgress = false;
CFTimeInterval dt = time - lastTime;
switch (type) {
case kPOPAnimationSpring:
advanced = advance(time, dt, obj);
break;
case kPOPAnimationDecay:
advanced = advance(time, dt, obj);
break;
case kPOPAnimationBasic: {
advanced = advance(time, dt, obj);
computedProgress = true;
break;
}
case kPOPAnimationCustom: {
customFinished = [self _advance:obj currentTime:time elapsedTime:dt] ? false : true;
advanced = true;
break;
}
default:
break;
}
if (advanced) {
// estimate progress
if (!computedProgress) {
computeProgress();
}
// delegate progress
delegateProgress();
// update time
lastTime = time;
}
return advanced;
}
virtual void willRun(bool started, id obj) {}
virtual bool advance(CFTimeInterval time, CFTimeInterval dt, id obj) { return false; }
virtual void computeProgress() {}
virtual void delegateProgress() {}
virtual void delegateApply() {
if (delegateDidApply) {
ActionEnabler enabler;
[delegate pop_animationDidApply:self];
}
}
virtual void reset(bool all) {
startTime = 0;
lastTime = 0;
}
};
typedef struct _POPAnimationState POPAnimationState;
@interface POPAnimation ()
{
@protected
struct _POPAnimationState *_state;
}
@end
// NSProxy extensions, for testing pursposes
@interface NSProxy (POP)
- (void)pop_addAnimation:(POPAnimation *)anim forKey:(NSString *)key;
- (void)pop_removeAllAnimations;
- (void)pop_removeAnimationForKey:(NSString *)key;
- (NSArray *)pop_animationKeys;
- (POPAnimation *)pop_animationForKey:(NSString *)key;
@end