API  0.9.7
 All Classes Files Functions Variables Macros Groups Pages
CPViewAnimation.j
Go to the documentation of this file.
1 /*
2  * CPViewAnimation.j
3  * AppKit
4  *
5  * Created by Klaas Pieter Annema on September 3, 2009.
6  * Copyright 2009, Sofa BV
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 @class CPWindow
25 
26 CPViewAnimationTargetKey = @"CPViewAnimationTargetKey";
27 CPViewAnimationStartFrameKey = @"CPViewAnimationStartFrameKey";
28 CPViewAnimationEndFrameKey = @"CPViewAnimationEndFrameKey";
29 CPViewAnimationEffectKey = @"CPViewAnimationEffectKey";
30 
31 CPViewAnimationFadeInEffect = @"CPViewAnimationFadeInEffect";
32 CPViewAnimationFadeOutEffect = @"CPViewAnimationFadeOutEffect";
33 
41 @implementation CPViewAnimation : CPAnimation
42 {
43  CPArray _viewAnimations;
44 }
45 
73 - (id)initWithViewAnimations:(CPArray)viewAnimations
74 {
75  if (self = [super initWithDuration:0.5 animationCurve:CPAnimationLinear])
76  {
77  [self setViewAnimations:viewAnimations];
78  }
79 
80  return self;
81 }
82 
83 - (void)startAnimation
84 {
85  var animationIndex = [_viewAnimations count];
86  while (animationIndex--)
87  {
88  var dictionary = [_viewAnimations objectAtIndex:animationIndex],
89  view = [self _targetView:dictionary],
90  startFrame = [self _startFrame:dictionary];
91 
92  [view setFrame:startFrame];
93 
94  var effect = [self _effect:dictionary];
95  if (effect === CPViewAnimationFadeInEffect)
96  {
97  [view setAlphaValue:0.0];
98  [self _targetView:view setHidden:NO];
99  }
100  else if (effect === CPViewAnimationFadeOutEffect)
101  [view setAlphaValue:1.0];
102  }
103 
104  [super startAnimation];
105 }
106 
107 - (void)setCurrentProgress:(float)progress
108 {
109  [super setCurrentProgress:progress];
110 
111  var animationIndex = [_viewAnimations count];
112  while (animationIndex--)
113  {
114  var dictionary = [_viewAnimations objectAtIndex:animationIndex],
115  view = [self _targetView:dictionary],
116  startFrame = [self _startFrame:dictionary],
117  endFrame = [self _endFrame:dictionary],
118  differenceFrame = CGRectMakeZero(),
119  value = [super currentValue];
120 
121  differenceFrame.origin.x = endFrame.origin.x - startFrame.origin.x;
122  differenceFrame.origin.y = endFrame.origin.y - startFrame.origin.y;
123  differenceFrame.size.width = endFrame.size.width - startFrame.size.width;
124  differenceFrame.size.height = endFrame.size.height - startFrame.size.height;
125 
126  var intermediateFrame = CGRectMakeZero();
127  intermediateFrame.origin.x = startFrame.origin.x + differenceFrame.origin.x * value;
128  intermediateFrame.origin.y = startFrame.origin.y + differenceFrame.origin.y * value;
129  intermediateFrame.size.width = startFrame.size.width + differenceFrame.size.width * value;
130  intermediateFrame.size.height = startFrame.size.height + differenceFrame.size.height * value;
131 
132  [view setFrame:intermediateFrame];
133 
134  // Update the view's alpha value
135  var effect = [self _effect:dictionary];
136  if (effect === CPViewAnimationFadeInEffect)
137  [view setAlphaValue:1.0 * value];
138  else if (effect === CPViewAnimationFadeOutEffect)
139  [view setAlphaValue:1.0 + ( 0.0 - 1.0 ) * value];
140 
141  if (progress === 1.0)
142  [self _targetView:view setHidden:CGRectIsEmpty(endFrame) || [view alphaValue] === 0.0];
143  }
144 }
145 
146 - (void)stopAnimation
147 {
148  var animationIndex = [_viewAnimations count];
149  while (animationIndex--)
150  {
151  var dictionary = [_viewAnimations objectAtIndex:animationIndex],
152  view = [self _targetView:dictionary],
153  endFrame = [self _endFrame:dictionary];
154 
155  [view setFrame:endFrame];
156 
157  var effect = [self _effect:dictionary];
158  if (effect === CPViewAnimationFadeInEffect)
159  [view setAlphaValue:1.0];
160  else if (effect === CPViewAnimationFadeOutEffect)
161  [view setAlphaValue:0.0];
162 
163  [self _targetView:view setHidden:CGRectIsEmpty(endFrame) || [view alphaValue] === 0.0];
164  }
165 
166  [super stopAnimation];
167 }
168 
169 - (void)_targetView:(id)theView setHidden:(BOOL)isHidden
170 {
171  if ([theView isKindOfClass:[CPWindow class]])
172  {
173  if (isHidden)
174  [theView orderOut:self];
175  else
176  [theView orderFront:self];
177  }
178  else
179  [theView setHidden:isHidden];
180 }
181 
182 - (id)_targetView:(CPDictionary)dictionary
183 {
184  var targetView = [dictionary valueForKey:CPViewAnimationTargetKey];
185  if (!targetView)
186  [CPException raise:CPInternalInconsistencyException reason:[CPString stringWithFormat:@"view animation: %@ does not have a target view", [dictionary description]]];
187 
188  return targetView;
189 }
190 
191 - (CGRect)_startFrame:(CPDictionary)dictionary
192 {
193  var startFrame = [dictionary valueForKey:CPViewAnimationStartFrameKey];
194  if (!startFrame)
195  return [[self _targetView:dictionary] frame];
196 
197  return startFrame;
198 }
199 
200 - (CGRect)_endFrame:(CPDictionary)dictionary
201 {
202  var endFrame = [dictionary valueForKey:CPViewAnimationEndFrameKey];
203  if (!endFrame)
204  return [[self _targetView:dictionary] frame];
205 
206  return endFrame;
207 }
208 
209 - (CPString)_effect:(CPDictionary)dictionary
210 {
211  return [dictionary valueForKey:CPViewAnimationEffectKey];
212 }
213 
214 - (CPArray)viewAnimations
215 {
216  return _viewAnimations;
217 }
218 
224 - (void)setViewAnimations:(CPArray)viewAnimations
225 {
226  if (viewAnimations != _viewAnimations)
227  {
228  [self stopAnimation];
229  _viewAnimations = [viewAnimations copy];
230  }
231 }
232 
233 @end