mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-08-25 21:17:03 +00:00
Previously, Cappuccino was using preprocessor macros internally for the CGPoint/Size/Rect/Inset/Affine functions, as well as for CPRange. These macros had the same name as the corresponding function, but began with _. The functions were actually defined using the macros.
The motivation behind using macros was to increase performance by reducing function calls. However, there were a number of problems with this approach:
- There was an artificial dichotomy between _CG macros and the corresponding CG functions. We never completely replaced CG function calls with _CG macros. In fact, they were often mixed up in the same file. There was an extra burden on the programmer to remember to use the macro instead of the function.
- If a method call was passed as an argument to a macro, performance could actually be significantly *worse* than a function call. For example, _CGGetRectMakeCopy([view frame]) would expand to `{ origin:{ x:[view frame].origin.x, y:[view frame].origin.y }, size:{ width:[view frame].size.width, height:[view frame].size.height } }`. So instead of a single objj_msgSend and a single simple function call, we ended up with 4 objj_msgSend calls, which are way more expensive than simple function calls.
- Because of this expansion problem, to use macros efficiently required us to remember to use variables for all macro parameters. This didn't happen, and shouldn't have to happen.
- Finally, with modern Javascript engines, function call overhead is so small that it really isn't worth using the macros.
This commit eliminates the _CGGeometry, CGAffineTransformation and CPRange macros and replaces them with function calls.
BREAKING CHANGE:
The macros are no longer available. They could only be used with compiled code, but if there is any user code that used them, they will have to be replaced with the corresponding functions.
235 lines
7.2 KiB
Plaintext
235 lines
7.2 KiB
Plaintext
/*
|
|
* CPViewAnimation.j
|
|
* AppKit
|
|
*
|
|
* Created by Klaas Pieter Annema on September 3, 2009.
|
|
* Copyright 2009, Sofa BV
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
@import "CPAnimation.j"
|
|
|
|
@class CPWindow
|
|
|
|
CPViewAnimationTargetKey = @"CPViewAnimationTargetKey";
|
|
CPViewAnimationStartFrameKey = @"CPViewAnimationStartFrameKey";
|
|
CPViewAnimationEndFrameKey = @"CPViewAnimationEndFrameKey";
|
|
CPViewAnimationEffectKey = @"CPViewAnimationEffectKey";
|
|
|
|
CPViewAnimationFadeInEffect = @"CPViewAnimationFadeInEffect";
|
|
CPViewAnimationFadeOutEffect = @"CPViewAnimationFadeOutEffect";
|
|
|
|
/*!
|
|
@class CPViewAnimation
|
|
|
|
CPViewAnimation is a subclass of CPAnimation that makes it easy to do
|
|
basic animations on views.
|
|
*/
|
|
|
|
@implementation CPViewAnimation : CPAnimation
|
|
{
|
|
CPArray _viewAnimations;
|
|
}
|
|
|
|
/*!
|
|
Designated initializer.
|
|
|
|
This method takes an array of CPDictionaries. Each dictionary should
|
|
contain values for the following keys:
|
|
|
|
<pre>
|
|
CPViewAnimationTargetKey - (Required) The view to animate.
|
|
CPViewAnimationStartFrameKey - (Optional) The start frame of the target.
|
|
CPViewAnimationEndFrameKey - (Optional) The end frame of the target.
|
|
CPViewAnimationEffectKey - (Optional) a fade effect to use for the animation.
|
|
</pre>
|
|
|
|
For example:
|
|
<pre>
|
|
var animation = @{
|
|
CPViewAnimationTargetKey: myViewToAnimate,
|
|
CPViewAnimationStartFrameKey: aStartFrame,
|
|
CPViewAnimationEndFrameKey: anEndFrame,
|
|
CPViewAnimationEffectKey: CPViewAnimationFadeInEffect,
|
|
};
|
|
</pre>
|
|
|
|
If you pass nil instead of an array of dictionaries you should later call setViewAnimations:.
|
|
|
|
@param viewAnimations - An array of CPDictionaries for each animation.
|
|
*/
|
|
- (id)initWithViewAnimations:(CPArray)viewAnimations
|
|
{
|
|
if (self = [super initWithDuration:0.5 animationCurve:CPAnimationLinear])
|
|
{
|
|
[self setViewAnimations:viewAnimations];
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (void)startAnimation
|
|
{
|
|
var animationIndex = [_viewAnimations count];
|
|
while (animationIndex--)
|
|
{
|
|
var dictionary = [_viewAnimations objectAtIndex:animationIndex],
|
|
view = [self _targetView:dictionary],
|
|
startFrame = [self _startFrame:dictionary];
|
|
|
|
[view setFrame:startFrame];
|
|
|
|
var effect = [self _effect:dictionary];
|
|
if (effect === CPViewAnimationFadeInEffect)
|
|
{
|
|
[view setAlphaValue:0.0];
|
|
[self _targetView:view setHidden:NO];
|
|
}
|
|
else if (effect === CPViewAnimationFadeOutEffect)
|
|
[view setAlphaValue:1.0];
|
|
}
|
|
|
|
[super startAnimation];
|
|
}
|
|
|
|
- (void)setCurrentProgress:(CPAnimationProgress)progress
|
|
{
|
|
[super setCurrentProgress:progress];
|
|
|
|
var animationIndex = [_viewAnimations count];
|
|
while (animationIndex--)
|
|
{
|
|
var dictionary = [_viewAnimations objectAtIndex:animationIndex],
|
|
view = [self _targetView:dictionary],
|
|
startFrame = [self _startFrame:dictionary],
|
|
endFrame = [self _endFrame:dictionary],
|
|
differenceFrame = CGRectMakeZero(),
|
|
value = [super currentValue];
|
|
|
|
differenceFrame.origin.x = endFrame.origin.x - startFrame.origin.x;
|
|
differenceFrame.origin.y = endFrame.origin.y - startFrame.origin.y;
|
|
differenceFrame.size.width = endFrame.size.width - startFrame.size.width;
|
|
differenceFrame.size.height = endFrame.size.height - startFrame.size.height;
|
|
|
|
var intermediateFrame = CGRectMakeZero();
|
|
intermediateFrame.origin.x = startFrame.origin.x + differenceFrame.origin.x * value;
|
|
intermediateFrame.origin.y = startFrame.origin.y + differenceFrame.origin.y * value;
|
|
intermediateFrame.size.width = startFrame.size.width + differenceFrame.size.width * value;
|
|
intermediateFrame.size.height = startFrame.size.height + differenceFrame.size.height * value;
|
|
|
|
[view setFrame:intermediateFrame];
|
|
|
|
// Update the view's alpha value
|
|
var effect = [self _effect:dictionary];
|
|
if (effect === CPViewAnimationFadeInEffect)
|
|
[view setAlphaValue:1.0 * value];
|
|
else if (effect === CPViewAnimationFadeOutEffect)
|
|
[view setAlphaValue:1.0 + ( 0.0 - 1.0 ) * value];
|
|
|
|
if (progress === 1.0)
|
|
[self _targetView:view setHidden:CGRectIsEmpty(endFrame) || [view alphaValue] === 0.0];
|
|
}
|
|
}
|
|
|
|
- (void)stopAnimation
|
|
{
|
|
var animationIndex = [_viewAnimations count];
|
|
while (animationIndex--)
|
|
{
|
|
var dictionary = [_viewAnimations objectAtIndex:animationIndex],
|
|
view = [self _targetView:dictionary],
|
|
endFrame = [self _endFrame:dictionary];
|
|
|
|
[view setFrame:endFrame];
|
|
|
|
var effect = [self _effect:dictionary];
|
|
if (effect === CPViewAnimationFadeInEffect)
|
|
[view setAlphaValue:1.0];
|
|
else if (effect === CPViewAnimationFadeOutEffect)
|
|
[view setAlphaValue:0.0];
|
|
|
|
[self _targetView:view setHidden:CGRectIsEmpty(endFrame) || [view alphaValue] === 0.0];
|
|
}
|
|
|
|
[super stopAnimation];
|
|
}
|
|
|
|
- (void)_targetView:(id)theView setHidden:(BOOL)isHidden
|
|
{
|
|
if ([theView isKindOfClass:[CPWindow class]])
|
|
{
|
|
if (isHidden)
|
|
[theView orderOut:self];
|
|
else
|
|
[theView orderFront:self];
|
|
}
|
|
else
|
|
[theView setHidden:isHidden];
|
|
}
|
|
|
|
- (id)_targetView:(CPDictionary)dictionary
|
|
{
|
|
var targetView = [dictionary valueForKey:CPViewAnimationTargetKey];
|
|
if (!targetView)
|
|
[CPException raise:CPInternalInconsistencyException reason:[CPString stringWithFormat:@"view animation: %@ does not have a target view", [dictionary description]]];
|
|
|
|
return targetView;
|
|
}
|
|
|
|
- (CGRect)_startFrame:(CPDictionary)dictionary
|
|
{
|
|
var startFrame = [dictionary valueForKey:CPViewAnimationStartFrameKey];
|
|
if (!startFrame)
|
|
return [[self _targetView:dictionary] frame];
|
|
|
|
return startFrame;
|
|
}
|
|
|
|
- (CGRect)_endFrame:(CPDictionary)dictionary
|
|
{
|
|
var endFrame = [dictionary valueForKey:CPViewAnimationEndFrameKey];
|
|
if (!endFrame)
|
|
return [[self _targetView:dictionary] frame];
|
|
|
|
return endFrame;
|
|
}
|
|
|
|
- (CPString)_effect:(CPDictionary)dictionary
|
|
{
|
|
return [dictionary valueForKey:CPViewAnimationEffectKey];
|
|
}
|
|
|
|
- (CPArray)viewAnimations
|
|
{
|
|
return _viewAnimations;
|
|
}
|
|
|
|
/*!
|
|
Takes an array of CPDictionaries as documented in initWithViewAnimations:.
|
|
|
|
@param viewAnimations - An array of dictionaries describing the animation.
|
|
*/
|
|
- (void)setViewAnimations:(CPArray)viewAnimations
|
|
{
|
|
if (viewAnimations != _viewAnimations)
|
|
{
|
|
[self stopAnimation];
|
|
_viewAnimations = [viewAnimations copy];
|
|
}
|
|
}
|
|
|
|
@end
|