00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import <Foundation/CPObject.j>
00024
00025 @import "CAMediaTimingFunction.j"
00026
00027
00028
00029
00030
00031
00032 CPAnimationEaseInOut = 0;
00033
00034
00035
00036
00037 CPAnimationEaseIn = 1;
00038
00039
00040
00041
00042 CPAnimationEaseOut = 2;
00043
00044
00045
00046
00047 CPAnimationLinear = 3;
00048
00049 ACTUAL_FRAME_RATE = 0;
00050
00080 @implementation CPAnimation : CPObject
00081 {
00082 CPTimeInterval _lastTime;
00083 CPTimeInterval _duration;
00084
00085 CPAnimationCurve _animationCurve;
00086 CAMediaTimingFunction _timingFunction;
00087
00088 float _frameRate;
00089 float _progress;
00090
00091 id _delegate;
00092 CPTimer _timer;
00093 }
00094
00101 - (id)initWithDuration:(float)aDuration animationCurve:(CPAnimationCurve)anAnimationCurve
00102 {
00103 self = [super init];
00104
00105 if (self)
00106 {
00107 _progress = 0.0;
00108 _duration = MAX(0.0, aDuration);
00109 _animationCurve = anAnimationCurve;
00110 _frameRate = 60.0;
00111 }
00112
00113 return self;
00114 }
00115
00121 - (void)setAnimationCurve:(CPAnimationCurve)anAnimationCurve
00122 {
00123 switch (_animationCurve)
00124 {
00125 case CPAnimationEaseInOut: timingFunctionName = kCAMediaTimingFunctionEaseInEaseOut;
00126 break;
00127
00128 case CPAnimationEaseIn: timingFunctionName = kCAMediaTimingFunctionEaseIn;
00129 break;
00130
00131 case CPAnimationEaseOut: timingFunctionName = kCAMediaTimingFunctionEaseOut;
00132 break;
00133
00134 case CPAnimationLinear: timingFunctionName = kCAMediaTimingFunctionLinear;
00135 break;
00136
00137 default: [CPException raise:CPInvalidArgumentException
00138 reason:"Invalid value provided for animation curve"];
00139 break;
00140 }
00141
00142 _animationCurve = anAnimationCurve;
00143 _timingFunction = [CAMediaTimingFunction functionWithName:timingFunctionName];
00144 }
00145
00149 - (CPAnimationCurve)animationCurve
00150 {
00151 return _animationCurve;
00152 }
00153
00159 - (void)setDuration:(CPTimeInterval)aDuration
00160 {
00161 if (aDuration < 0)
00162 [CPException raise:CPInvalidArgumentException reason:"aDuration can't be negative"];
00163
00164 _duration = aDuration;
00165 }
00166
00170 - (CPTimeInterval)duration
00171 {
00172 return _duration;
00173 }
00174
00180 - (void)setFrameRate:(float)frameRate
00181 {
00182 if (frameRate < 0)
00183 [CPException raise:CPInvalidArgumentException reason:"frameRate can't be negative"];
00184
00185 _frameRate = frameRate;
00186 }
00187
00191 - (float)frameRate
00192 {
00193 return _frameRate;
00194 }
00195
00199 - (id)delegate
00200 {
00201 return _delegate;
00202 }
00203
00208 - (void)setDelegate:(id)aDelegate
00209 {
00210 _delegate = aDelegate;
00211 }
00212
00218 - (void)startAnimation
00219 {
00220
00221 if (_timer || _delegate && [_delegate respondsToSelector:@selector(animationShouldStart)] && ![_delegate animationShouldStart:self])
00222 return;
00223
00224 if (_progress === 1.0)
00225 _progress = 0.0;
00226
00227 ACTUAL_FRAME_RATE = 0;
00228 _lastTime = new Date();
00229
00230 _timer = [CPTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(animationTimerDidFire:) userInfo:nil repeats:YES];
00231 }
00232
00233
00234
00235
00236 - (void)animationTimerDidFire:(CPTimer)aTimer
00237 {
00238 var currentTime = new Date(),
00239 progress = MIN(1.0, [self currentProgress] + (currentTime - _lastTime) / (_duration * 1000.0));
00240
00241 _lastTime = currentTime;
00242
00243 ++ACTUAL_FRAME_RATE;
00244
00245 [self setCurrentProgress:progress];
00246
00247 if (progress === 1.0)
00248 {
00249 [_timer invalidate];
00250 _timer = nil;
00251
00252 if ([_delegate respondsToSelector:@selector(animationDidEnd:)])
00253 [_delegate animationDidEnd:self];
00254 }
00255 }
00256
00260 - (void)stopAnimation
00261 {
00262 if (!_timer)
00263 return;
00264
00265 [_timer invalidate];
00266 _timer = nil;
00267
00268 if ([_delegate respondsToSelector:@selector(animationDidStop:)])
00269 [_delegate animationDidStop:self];
00270 }
00271
00276 - (BOOL)isAnimating
00277 {
00278 return _timer;
00279 }
00280
00285 - (void)setCurrentProgress:(float)aProgress
00286 {
00287 _progress = aProgress;
00288 }
00289
00293 - (float)currentProgress
00294 {
00295 return _progress;
00296 }
00297
00301 - (float)currentValue
00302 {
00303 if ([_delegate respondsToSelector:@selector(animation:valueForProgress:)])
00304 return [_delegate animation:self valueForProgress:_progress];
00305
00306 if (_animationCurve == CPAnimationLinear)
00307 return _progress;
00308
00309 alert("IMPLEMENT ANIMATION CURVES!!!");
00310 }
00311
00312 @end