summaryrefslogtreecommitdiff
path: root/ios/shared/ios_sharedlo/objective_c/utils/MLOAnimation.m
blob: ee7dee20b1fc5525f0929a603563ecb80c88e7a0 (plain)
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
// -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
//
// This file is part of the LibreOffice project.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#import "MLOAnimation.h"


typedef CGFloat (^MLOAnimationCurve)(CGFloat time);

@interface MLOAnimation ()
@property BOOL active,didPost,cancelled;
@property CGFloat frameCount;
@property NSDate * startDate;
@property MLOAnimationBehavior behavior;
@property MLOAnimationFractionType fractionType;
@property (nonatomic,strong) MLOAnimationBlock animation;
@property (nonatomic,strong) MLOAnimationCurve curve;
@end

@implementation MLOAnimation

-(void) linearCurve{
    static const MLOAnimationCurve LINEAR = ^(CGFloat completedFraction){
        return completedFraction;
    };
    [self setCurve:LINEAR name:@"LINEAR"];
}
-(void) easeOutCurve{
    static const MLOAnimationCurve EASE_OUT  = ^(CGFloat completedFraction){
        return 1 - completedFraction*completedFraction;
    };
    [self setCurve:EASE_OUT name:@"EASE_OUT"];
}

-(void) easeInCurve{
    static const MLOAnimationCurve EASE_IN  = ^(CGFloat completedFraction){
        return completedFraction*completedFraction;
    };
    
    [self setCurve:EASE_IN name:@"EASE_IN"];
}

-(void)setCurve:(MLOAnimationCurve) curve name:(NSString *) name{
    self.curve = curve;
    NSLog(@"MLOAnimation curve set to: %@",name);
}

-(void) cancel{
    if(_behavior == CANCELABLE){
        self.cancelled= YES;
        self.active = NO;
        [self doPost:nil];
        
        if(_startDate){
        
            NSLog(@"MLOAnimation cancelled after %f millis",[_startDate timeIntervalSinceNow]);
        }else{
            NSLog(@"MLOAnimation aborted");
        }
    }else{
        NSLog(@"CLOAnimation cannot be cancelled");
    }
}
-(BOOL) isCancelled{
    return self.cancelled;
}

-(id)initWithBehavior:(BOOL) behavior fractionType:(MLOAnimationFractionType) fractionType animation:(MLOAnimationBlock) animation{
    self = [super init];
    if(self){
        _active=YES;
        _duration = DEFAULT_MLO_ANIMATION_DURAION;
        _fps = DEFAULT_ANIMATION_FPS;
        _behavior=CANCELABLE;
        _fractionType = fractionType;
        _frameCount = -1.0f;
        _startDate = nil;
        _didPost =NO;
        _cancelled=NO;
        [self linearCurve];
        self.animation = animation;
    }
    return self;
}

-(void)animate{

    if(!_startDate){
        self.startDate = [NSDate date];
        _frameCount = _duration *_fps;
        
        if(_frameCount>0){
            CGFloat frameDuration = 1.0f/_fps;
            
            NSLog(@"MLOAnimation: duration=%f frameCount=%f fps=%f frameDuration=%f fractionType=%@",_duration,_frameCount,_fps,frameDuration,[self fractionTypeAsString]);
            
            for (CGFloat i = 1; i <= _frameCount; i++) {
                [self performSelector:@selector(doFrame:) withObject:[NSNumber numberWithFloat:i] afterDelay: i*frameDuration];
            }
        [self performSelector:@selector(doPost:) withObject:nil afterDelay:_duration + frameDuration];
        }else{
            NSLog(@"MLOAnimation cannot run (zero frames)");
        }
    }
}

-(void) doPost:(NSObject *) niller{
    if(!_didPost){
        _didPost = YES;
        if(_endBlock){
            _endBlock();
        }
    }
}

-(NSString *)fractionTypeAsString{
    switch (_fractionType) {
        case DELTA_ONLY:
            return @"DELTA_ONLY";
        case FULL_FRACTION:
            return @"FULL_FRACTION";
    }
}

-(void)doFrame:(NSNumber *) frame{
    if(_active){
        
        CGFloat fFrame = [frame floatValue];
        
        CGFloat currentFraction = _curve(fFrame/_frameCount);
        
        if(_fractionType == DELTA_ONLY){
            currentFraction -= _curve( (fFrame-1.0f) /_frameCount);
        }
        
        _animation(_curve(currentFraction));
        
        if(fFrame ==_frameCount){
        
            _behavior =MANDATORY;
        }
    }
}

@end