00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import <AppKit/CPAnimation.j>
00024
00025 CPViewAnimationTargetKey = @"CPViewAnimationTarget";
00026 CPViewAnimationStartFrameKey = @"CPViewAnimationStartFrame";
00027 CPViewAnimationEndFrameKey = @"CPViewAnimationEndFrame";
00028 CPViewAnimationEffectKey = @"CPViewAnimationEffect";
00029
00030 CPViewAnimationFadeInEffect = @"CPViewAnimationFadeIn";
00031 CPViewAnimationFadeOutEffect = @"CPViewAnimationFadeOut";
00032
00033 @implementation CPViewAnimation : CPAnimation
00034 {
00035 CPArray _viewAnimations;
00036 }
00037
00038 - (id)initWithViewAnimations:(CPArray)viewAnimations
00039 {
00040 if (self = [super initWithDuration:0.5 animationCurve:CPAnimationLinear])
00041 {
00042 [self setViewAnimations:viewAnimations];
00043 }
00044
00045 return self;
00046 }
00047
00048 - (void)startAnimation
00049 {
00050 var animationIndex = [_viewAnimations count];
00051 while (animationIndex--)
00052 {
00053 var dictionary = [_viewAnimations objectAtIndex:animationIndex],
00054 view = [self _targetView:dictionary],
00055 startFrame = [self _startFrame:dictionary];
00056
00057 [view setFrame:startFrame];
00058
00059 var effect = [self _effect:dictionary];
00060 if (effect === CPViewAnimationFadeInEffect)
00061 {
00062 [view setAlphaValue:0.0];
00063 [view setHidden:NO];
00064 }
00065 else if (effect === CPViewAnimationFadeOutEffect)
00066 [view setAlphaValue:1.0];
00067 }
00068
00069 [super startAnimation];
00070 }
00071
00072 - (void)setCurrentProgress:(NSAnimationProgress)progress
00073 {
00074 [super setCurrentProgress:progress];
00075
00076 var animationIndex = [_viewAnimations count];
00077 while (animationIndex--)
00078 {
00079 var dictionary = [_viewAnimations objectAtIndex:animationIndex],
00080 view = [self _targetView:dictionary]
00081 startFrame = [self _startFrame:dictionary]
00082 endFrame = [self _endFrame:dictionary]
00083 differenceFrame = CPRectMakeZero();
00084
00085 differenceFrame.origin.x = endFrame.origin.x - startFrame.origin.x;
00086 differenceFrame.origin.y = endFrame.origin.y - startFrame.origin.y;
00087 differenceFrame.size.width = endFrame.size.width - startFrame.size.width;
00088 differenceFrame.size.height = endFrame.size.height - startFrame.size.height;
00089
00090 var intermediateFrame = CPRectMakeZero();
00091 intermediateFrame.origin.x = startFrame.origin.x + differenceFrame.origin.x * progress;
00092 intermediateFrame.origin.y = startFrame.origin.y + differenceFrame.origin.y * progress;
00093 intermediateFrame.size.width = startFrame.size.width + differenceFrame.size.width * progress;
00094 intermediateFrame.size.height = startFrame.size.height + differenceFrame.size.height * progress;
00095
00096 [view setFrame:intermediateFrame];
00097
00098
00099 var effect = [self _effect:dictionary];
00100 if (effect === CPViewAnimationFadeInEffect)
00101 [view setAlphaValue:1.0 * progress];
00102 else if (effect === CPViewAnimationFadeOutEffect)
00103 [view setAlphaValue:1.0 + ( 0.0 - 1.0 ) * progress];
00104
00105 if (progress === 1.0)
00106 [view setHidden:CPRectIsNull(endFrame) || [view alphaValue] === 0.0];
00107 }
00108 }
00109
00110 - (void)stopAnimation
00111 {
00112 var animationIndex = [_viewAnimations count];
00113 while (animationIndex--)
00114 {
00115 var dictionary = [_viewAnimations objectAtIndex:animationIndex],
00116 view = [self _targetView:dictionary],
00117 endFrame = [self _endFrame:dictionary];
00118
00119 [view setFrame:endFrame];
00120
00121 var effect = [self _effect:dictionary];
00122 if (effect === CPViewAnimationFadeInEffect)
00123 [view setAlphaValue:1.0];
00124 else if (effect === CPViewAnimationFadeOutEffect)
00125 [view setAlphaValue:0.0];
00126
00127 [view setHidden:CPRectIsNull(endFrame) || [view alphaValue] === 0.0];
00128 }
00129
00130 [super stopAnimation];
00131 }
00132
00133 - (id)_targetView:(CPDictionary)dictionary
00134 {
00135 var targetView = [dictionary valueForKey:CPViewAnimationTargetKey];
00136 if (!targetView)
00137 [CPException raise:CPInternalInconsistencyException reason:[CPString stringWithFormat:@"view animation: %@ does not have a target view", [dictionary description]]];
00138
00139 return targetView;
00140 }
00141
00142 - (CPRect)_startFrame:(CPDictionary)dictionary
00143 {
00144 var startFrame = [dictionary valueForKey:CPViewAnimationStartFrameKey];
00145 if (!startFrame)
00146 return [[self _targetView:dictionary] frame];
00147
00148 return startFrame;
00149 }
00150
00151 - (CPRect)_endFrame:(CPDictionary)dictionary
00152 {
00153 var endFrame = [dictionary valueForKey:CPViewAnimationEndFrameKey];
00154 if (!endFrame)
00155 return [[self _targetView:dictionary] frame];
00156
00157 return endFrame;
00158 }
00159
00160 - (CPString)_effect:(CPDictionary)dictionary
00161 {
00162 return [dictionary valueForKey:CPViewAnimationEffectKey];
00163 }
00164
00165 - (CPArray)viewAnimations
00166 {
00167 return _viewAnimations;
00168 }
00169
00170 - (void)setViewAnimations:(CPArray)viewAnimations
00171 {
00172 if (viewAnimations != _viewAnimations)
00173 {
00174 [self stopAnimation];
00175 _viewAnimations = [viewAnimations copy];
00176 }
00177 }
00178
00179 @end