-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDVGKeyframedAnimationScene.m
More file actions
132 lines (117 loc) · 4.2 KB
/
DVGKeyframedAnimationScene.m
File metadata and controls
132 lines (117 loc) · 4.2 KB
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
#import "DVGOglEffectBase.h"
#import <Foundation/Foundation.h>
#import "DVGKeyframedAnimationScene.h"
#include "DVGEasing.h"
CGFloat kDefaultValuesForKeys[kDVGVITimelineKeyLast] = {0,0,0,1,1,1,0};
@implementation DVGKeyframedAnimationSceneObject
+(DVGKeyframedAnimationSceneObject *)sceneObjectWithImage:(UIImage*)image relativeSize:(CGSize)size
{
DVGKeyframedAnimationSceneObject *sceneObject = [DVGKeyframedAnimationSceneObject new];
sceneObject.objectImage = image;
sceneObject.relativeSize = size;
return sceneObject;
}
@end
@implementation DVGKeyframedAnimationTimelineKeyframe
+(DVGKeyframedAnimationTimelineKeyframe *)keyframeWithTime:(CGFloat)time value:(CGFloat)value easing:(DVGVITimelineKeyInterpolationType)easing
{
DVGKeyframedAnimationTimelineKeyframe *keyframe = [DVGKeyframedAnimationTimelineKeyframe new];
keyframe.time = time;
keyframe.value = value;
keyframe.easing = easing;
return keyframe;
}
@end
@implementation DVGKeyframedAnimationTimeline
-(CGFloat)getValueForTime:(CGFloat)time {
NSInteger keyframes_count = [self.keyframes count];
if(keyframes_count == 0){
return kDefaultValuesForKeys[self.timeline_key];
}
for(NSInteger i=keyframes_count-1; i>= 0; i--){
DVGKeyframedAnimationTimelineKeyframe* keyframe = [self.keyframes objectAtIndex:i];
if(time < keyframe.time){
continue;
}
// We found it!
if(i == keyframes_count-1){
// No interpolation,just value
break;
}
DVGKeyframedAnimationTimelineKeyframe* keyframe_next = [self.keyframes objectAtIndex:i+1];
CGFloat timedist = keyframe_next.time-keyframe.time;
if(timedist <= 0.0001){
// nextvalue
return keyframe_next.value;
}
CGFloat needle = (time-keyframe.time)/timedist;
// applying easing
switch(keyframe.easing){
case kDVGVITimelineEasingIn:
needle = DVGCubicEaseIn(needle);
break;
case kDVGVITimelineEasingOut:
needle = DVGCubicEaseOut(needle);
break;
case kDVGVITimelineEasingInOut:
needle = DVGCubicEaseInOut(needle);
break;
case kDVGVITimelineInterpolationNone:
needle = 0;
default:
break;
}
CGFloat res = keyframe.value+(keyframe_next.value - keyframe.value)*needle;
if(isnan(res)){
res = kDefaultValuesForKeys[self.timeline_key];
}
return res;
}
DVGKeyframedAnimationTimelineKeyframe* keyframe_last = [self.keyframes objectAtIndex:keyframes_count-1];
CGFloat res = keyframe_last.value;
if(isnan(res)){
res = kDefaultValuesForKeys[self.timeline_key];
}
return res;
}
+(DVGKeyframedAnimationTimeline *)timelineWithKey:(DVGVITimelineKeyType)key objectIndex:(NSInteger)objectIndex keyFrames:(NSArray<DVGKeyframedAnimationTimelineKeyframe *>*)keyframes;
{
DVGKeyframedAnimationTimeline *timeline = [DVGKeyframedAnimationTimeline new];
timeline.timeline_key = key;
timeline.sceneobject_index = objectIndex;
timeline.keyframes = keyframes;
return timeline;
}
@end
//==========================
@implementation DVGKeyframedAnimationScene
-(instancetype)init {
if (self = [super init]) {
self.timeSpeed = 1.0f;
self.timeShift = 0.0f;
}
return self;
}
-(void)dealloc {
self.objects = nil;
self.timelines = nil;
}
-(void)fetchKeyedValues:(CGFloat*)values atTime:(CGFloat)time
{
[self fetchKeyedValues:values atTime:time forObject:-1];
}
-(void)fetchKeyedValues:(CGFloat*)values atTime:(CGFloat)timeOrigin forObject:(NSInteger)objectIndex
{
CGFloat time = timeOrigin*self.timeSpeed + self.timeShift;
memcpy(values,kDefaultValuesForKeys,sizeof(CGFloat)*kDVGVITimelineKeyLast);
for(int i=kDVGVITimelineUnknownKey+1; i<kDVGVITimelineKeyLast; i++){
for(DVGKeyframedAnimationTimeline* tl in self.timelines){
if(tl.timeline_key == i && (objectIndex<0 || tl.sceneobject_index == objectIndex)){
values[i] = [tl getValueForTime:time];
break;
}
}
}
return;
}
@end