API 0.9.5
AppKit/CPViewAnimation.j
Go to the documentation of this file.
00001 /*
00002  * CPViewAnimation.j
00003  * AppKit
00004  *
00005  * Created by Klaas Pieter Annema on September 3, 2009.
00006  * Copyright 2009, Sofa BV
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 
00024 
00025 CPViewAnimationTargetKey = @"CPViewAnimationTargetKey";
00026 CPViewAnimationStartFrameKey = @"CPViewAnimationStartFrameKey";
00027 CPViewAnimationEndFrameKey = @"CPViewAnimationEndFrameKey";
00028 CPViewAnimationEffectKey = @"CPViewAnimationEffectKey";
00029 
00030 CPViewAnimationFadeInEffect = @"CPViewAnimationFadeInEffect";
00031 CPViewAnimationFadeOutEffect = @"CPViewAnimationFadeOutEffect";
00032 
00040 @implementation CPViewAnimation : CPAnimation
00041 {
00042     CPArray _viewAnimations;
00043 }
00044 
00068 - (id)initWithViewAnimations:(CPArray)viewAnimations
00069 {
00070     if (self = [super initWithDuration:0.5 animationCurve:CPAnimationLinear])
00071     {
00072         [self setViewAnimations:viewAnimations];
00073     }
00074 
00075     return self;
00076 }
00077 
00078 - (void)startAnimation
00079 {
00080     var animationIndex = [_viewAnimations count];
00081     while (animationIndex--)
00082     {
00083         var dictionary = [_viewAnimations objectAtIndex:animationIndex],
00084             view = [self _targetView:dictionary],
00085             startFrame = [self _startFrame:dictionary];
00086 
00087         [view setFrame:startFrame];
00088 
00089         var effect = [self _effect:dictionary];
00090         if (effect === CPViewAnimationFadeInEffect)
00091         {
00092             [view setAlphaValue:0.0];
00093             [self _targetView:view setHidden:NO];
00094         }
00095         else if (effect === CPViewAnimationFadeOutEffect)
00096             [view setAlphaValue:1.0];
00097     }
00098 
00099     [super startAnimation];
00100 }
00101 
00102 - (void)setCurrentProgress:(CPAnimationProgress)progress
00103 {
00104     [super setCurrentProgress:progress];
00105 
00106     var animationIndex = [_viewAnimations count];
00107     while (animationIndex--)
00108     {
00109         var dictionary = [_viewAnimations objectAtIndex:animationIndex],
00110             view = [self _targetView:dictionary],
00111             startFrame = [self _startFrame:dictionary],
00112             endFrame = [self _endFrame:dictionary],
00113             differenceFrame = _CGRectMakeZero(),
00114             value = [super currentValue];
00115 
00116         differenceFrame.origin.x = endFrame.origin.x - startFrame.origin.x;
00117         differenceFrame.origin.y = endFrame.origin.y - startFrame.origin.y;
00118         differenceFrame.size.width = endFrame.size.width - startFrame.size.width;
00119         differenceFrame.size.height = endFrame.size.height - startFrame.size.height;
00120 
00121         var intermediateFrame = _CGRectMakeZero();
00122         intermediateFrame.origin.x = startFrame.origin.x + differenceFrame.origin.x * value;
00123         intermediateFrame.origin.y = startFrame.origin.y + differenceFrame.origin.y * value;
00124         intermediateFrame.size.width = startFrame.size.width + differenceFrame.size.width * value;
00125         intermediateFrame.size.height = startFrame.size.height + differenceFrame.size.height * value;
00126 
00127         [view setFrame:intermediateFrame];
00128 
00129         // Update the view's alpha value
00130         var effect = [self _effect:dictionary];
00131         if (effect === CPViewAnimationFadeInEffect)
00132             [view setAlphaValue:1.0 * value];
00133         else if (effect === CPViewAnimationFadeOutEffect)
00134             [view setAlphaValue:1.0 + ( 0.0 - 1.0 ) * value];
00135 
00136         if (progress === 1.0)
00137             [self _targetView:view setHidden:_CGRectIsNull(endFrame) || [view alphaValue] === 0.0];
00138     }
00139 }
00140 
00141 - (void)stopAnimation
00142 {
00143     var animationIndex = [_viewAnimations count];
00144     while (animationIndex--)
00145     {
00146         var dictionary = [_viewAnimations objectAtIndex:animationIndex],
00147             view = [self _targetView:dictionary],
00148             endFrame = [self _endFrame:dictionary];
00149 
00150         [view setFrame:endFrame];
00151 
00152         var effect = [self _effect:dictionary];
00153         if (effect === CPViewAnimationFadeInEffect)
00154             [view setAlphaValue:1.0];
00155         else if (effect === CPViewAnimationFadeOutEffect)
00156             [view setAlphaValue:0.0];
00157 
00158         [self _targetView:view setHidden:_CGRectIsNull(endFrame) || [view alphaValue] === 0.0];
00159     }
00160 
00161     [super stopAnimation];
00162 }
00163 
00164 - (void)_targetView:(id)theView setHidden:(BOOL)isHidden
00165 {
00166     if ([theView isKindOfClass:[CPWindow class]])
00167     {
00168         if (isHidden)
00169             [theView orderOut:self];
00170         else
00171             [theView orderFront:self];
00172     }
00173     else
00174         [theView setHidden:isHidden];
00175 }
00176 
00177 - (id)_targetView:(CPDictionary)dictionary
00178 {
00179     var targetView = [dictionary valueForKey:CPViewAnimationTargetKey];
00180     if (!targetView)
00181         [CPException raise:CPInternalInconsistencyException reason:[CPString stringWithFormat:@"view animation: %@ does not have a target view", [dictionary description]]];
00182 
00183     return targetView;
00184 }
00185 
00186 - (CGRect)_startFrame:(CPDictionary)dictionary
00187 {
00188     var startFrame = [dictionary valueForKey:CPViewAnimationStartFrameKey];
00189     if (!startFrame)
00190         return [[self _targetView:dictionary] frame];
00191 
00192     return startFrame;
00193 }
00194 
00195 - (CGRect)_endFrame:(CPDictionary)dictionary
00196 {
00197     var endFrame = [dictionary valueForKey:CPViewAnimationEndFrameKey];
00198     if (!endFrame)
00199         return [[self _targetView:dictionary] frame];
00200 
00201     return endFrame;
00202 }
00203 
00204 - (CPString)_effect:(CPDictionary)dictionary
00205 {
00206     return [dictionary valueForKey:CPViewAnimationEffectKey];
00207 }
00208 
00209 - (CPArray)viewAnimations
00210 {
00211     return _viewAnimations;
00212 }
00213 
00219 - (void)setViewAnimations:(CPArray)viewAnimations
00220 {
00221     if (viewAnimations != _viewAnimations)
00222     {
00223         [self stopAnimation];
00224         _viewAnimations = [viewAnimations copy];
00225     }
00226 }
00227 
00228 @end
 All Classes Files Functions Variables Defines