Implements threaded and blocking NSAnimation modes and some other changes.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@24998 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
xgl 2007-04-12 17:01:23 +00:00
parent 5278c0cbe2
commit 9fe955c56d
5 changed files with 1076 additions and 751 deletions

View file

@ -1,3 +1,15 @@
2007-04-12 Xavier Glattard <xavier.glattard@online.fr>
* Headers/AppKit/NSAnimation.h
* Source/NSAnimation.m
* Headers/Additions/GNUstepGUI/GSAnimator.h
* Source/GSAnimator.m
- attempt to follow GNU coding standards
- implementation of NSAnimationBlockingMode and
NSAnimationNonBlockingThreaded modes
- fix some bugs and incompatibilities with Cocoa
- Add some more documentation
2007-04-11 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSView.m: When transforming a size ensure that the

View file

@ -5,8 +5,7 @@
*
* Copyright (c) 2007 Free Software Foundation, Inc.
*
* This file used to be part of the mySTEP Library.
* This file now is part of the GNUstep GUI Library.
* This file is part of the GNUstep GUI Library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@ -31,7 +30,7 @@
@class NSRunLoop;
@class NSEvent;
@class NSTimer;
@class NSThread;
@class NSString;
#include <Foundation/NSObject.h>
#include <Foundation/NSDate.h>
@ -42,8 +41,6 @@
* be animated by a GSAnimator.
*/
@protocol GSAnimation
/** Returns the run-loop modes useed to run the animation timer in. */
- (NSArray*) runLoopModesForAnimating;
/** Call back method indicating that the GSAnimator did start the
* animation loop. */
- (void) animatorDidStart;
@ -54,19 +51,9 @@
- (void) animatorStep: (NSTimeInterval)elapsedTime;
@end
typedef enum
{
GSTimerBasedAnimation,
GSPerformerBasedAnimation,
// Cocoa compatible animation modes :
GSBlockingCocoaAnimation,
GSNonblockingCocoaAnimation,
GSNonblockingCocoaThreadedAnimation
} GSAnimationBlockingMode;
/**
* GSAnimator is the front of a class cluster. Instances of a subclass of
* GSAnimator manages the timing of an animation.
* GSAnimator manage the timing of an animation.
*/
@interface GSAnimator : NSObject
{
@ -78,37 +65,39 @@ typedef enum
NSTimeInterval _lastFrame; // The time of the last animation loop
unsigned int _frameCount; // The number of loops since the start
NSRunLoop *_runLoop; // The run-loop used for looping
NSArray* _runLoopModes;
NSTimer* _timer; // Timer used for looping
NSTimeInterval _timerInterval;
}
/** Returns a GSAnimator object initialized with the specified object
* to be animated as fast as possible. */
+ (GSAnimator*) animatorWithAnimation: (id<GSAnimation>)anAnimation;
/** Returns a GSAnimator object initialized with the specified object
* to be animated. */
+ (GSAnimator*) animatorWithAnimation: (id<GSAnimation>)anAnimation
mode: (GSAnimationBlockingMode)aMode
frameRate: (float)fps;
/** Returns a GSAnimator object allocated in the given NSZone and
* initialized with the specified object to be animated. */
+ (GSAnimator*) animatorWithAnimation: (id<GSAnimation>)anAnimation
mode: (GSAnimationBlockingMode)aMode
frameRate: (float)fps
zone: (NSZone*)aZone;
/** Returns a GSAnimator object initialized with the specified object
* to be animated. */
* to be animated. The given NSRunLoop is used in NSDefaultRunLoopMode.*/
- (GSAnimator*) initWithAnimation: (id<GSAnimation>)anAnimation
frameRate: (float)aFrameRate
runLoop: (NSRunLoop*)aRunLoop;
frameRate: (float)aFrameRate;
- (GSAnimator*) initWithAnimation: (id<GSAnimation>)anAnimation;
- (unsigned int) frameCount;
- (void) resetCounters;
- (float) frameRate;
- (NSRunLoop*) runLoopForAnimating;
- (NSArray*) runLoopModesForAnimating;
- (void) setRunLoopModesForAnimating: (NSArray*)modes;
- (void) startAnimation;
- (void) stopAnimation;

View file

@ -39,109 +39,10 @@
#include <Foundation/NSString.h>
#include <Foundation/NSArray.h>
// Bezier curve parameters
typedef struct __GSBezierDesc
{
float p[4]; // control points
BOOL areCoefficientsComputed;
float a[4]; // coefficients
} _GSBezierDesc;
static inline void
_GSBezierComputeCoefficients( _GSBezierDesc *b )
{
b->a[0] = b->p[0];
b->a[1] =-3.0*b->p[0]+3.0*b->p[1];
b->a[2] = 3.0*b->p[0]-6.0*b->p[1]+3.0*b->p[2];
b->a[3] =- b->p[0]+3.0*b->p[1]-3.0*b->p[2]+b->p[3];
b->areCoefficientsComputed = YES;
}
static inline float
_GSBezierEval( _GSBezierDesc *b, float t )
{
if(!b->areCoefficientsComputed)
_GSBezierComputeCoefficients(b);
return b->a[0]+t*(b->a[1]+t*(b->a[2]+t*b->a[3]));
}
static inline float
_GSBezierDerivEval( _GSBezierDesc *b, float t )
{
if(!b->areCoefficientsComputed)
_GSBezierComputeCoefficients(b);
return b->a[1]+t*(2.0*b->a[2]+t*3.0*b->a[3]);
}
// Rational Bezier curve parameters
typedef struct __GSRationalBezierDesc
{
float w[4]; // weights
float p[4]; // control points
BOOL areBezierDescComputed;
_GSBezierDesc n; // numerator
_GSBezierDesc d; // denumerator
} _GSRationalBezierDesc;
static inline void
_GSRationalBezierComputeBezierDesc( _GSRationalBezierDesc *rb )
{
unsigned i;
for(i=0;i<4;i++)
rb->n.p[i] = (rb->d.p[i] = rb->w[i]) * rb->p[i];
_GSBezierComputeCoefficients(&rb->n);
_GSBezierComputeCoefficients(&rb->d);
rb->areBezierDescComputed = YES;
}
static inline float
_GSRationalBezierEval( _GSRationalBezierDesc *rb, float t)
{
if(!rb->areBezierDescComputed)
_GSRationalBezierComputeBezierDesc(rb);
return _GSBezierEval(&(rb->n),t)/_GSBezierEval(&(rb->d),t);
}
static inline float
_GSRationalBezierDerivEval( _GSRationalBezierDesc *rb, float t)
{
if(!rb->areBezierDescComputed)
_GSRationalBezierComputeBezierDesc(rb);
float h = _GSBezierEval(&(rb->d),t);
return ( _GSBezierDerivEval(&(rb->n),t) * h
- _GSBezierEval (&(rb->n),t) * _GSBezierDerivEval(&(rb->d),t) )
/ (h*h);
}
typedef struct __NSAnimationCurveDesc
{
float s,e; // start & end values
float sg,eg; // start & end gradients
_GSRationalBezierDesc rb;
BOOL isRBezierComputed;
} _NSAnimationCurveDesc;
extern
_NSAnimationCurveDesc *_gs_animationCurveDesc;
static inline float
_gs_animationValueForCurve( _NSAnimationCurveDesc *c, float t, float t0 )
{
if(!c->isRBezierComputed)
{
c->rb.p[0] = c->s;
c->rb.p[1] = c->s + (c->sg*c->rb.w[0])/(3*c->rb.w[1]);
c->rb.p[2] = c->e - (c->eg*c->rb.w[3])/(3*c->rb.w[2]);
c->rb.p[3] = c->e;
_GSRationalBezierComputeBezierDesc(&c->rb);
c->isRBezierComputed = YES;
}
return _GSRationalBezierEval( &(c->rb),(t-t0)/(1.0-t0) );
}
@class NSString;
@class NSArray;
@class NSNumber;
@class NSRecursiveLock;
/** These constants describe the curve of an animation—that is, the relative speed of an animation from start to finish. */
typedef enum _NSAnimationCurve
@ -156,16 +57,46 @@ typedef enum _NSAnimationCurve
/** These constants indicate the blocking mode of an NSAnimation object when it is running. */
typedef enum _NSAnimationBlockingMode
{
NSAnimationBlocking = GSBlockingCocoaAnimation,
NSAnimationNonblocking = GSNonblockingCocoaAnimation,
NSAnimationNonblockingThreaded = GSNonblockingCocoaThreadedAnimation
NSAnimationBlocking,
NSAnimationNonblocking,
NSAnimationNonblockingThreaded
} NSAnimationBlockingMode;
typedef float NSAnimationProgress;
// Bezier curve parameters
typedef struct __GSBezierDesc
{
float p[4]; // control points
BOOL areCoefficientsComputed;
float a[4]; // coefficients
} _GSBezierDesc;
// Rational Bezier curve parameters
typedef struct __GSRationalBezierDesc
{
float w[4]; // weights
float p[4]; // control points
BOOL areBezierDescComputed;
_GSBezierDesc n; // numerator
_GSBezierDesc d; // denumerator
} _GSRationalBezierDesc;
// Animation curve parameters
typedef struct __NSAnimationCurveDesc
{
float s,e; // start & end values
float sg,eg; // start & end gradients
_GSRationalBezierDesc rb;
BOOL isRBezierComputed;
} _NSAnimationCurveDesc;
/** Posted when the current progress of a running animation reaches one of its progress marks. */
APPKIT_EXPORT NSString *NSAnimationProgressMarkNotification;
/** Key used in the [NSNotification-userInfo] disctionary to access the current progress mark. */
APPKIT_EXPORT NSString *NSAnimationProgressMark;
/**
* Objects of the NSAnimation class manage the timing and progress of
* animations in the user interface. The class also lets you link together
@ -203,6 +134,10 @@ APPKIT_EXPORT NSString *NSAnimationProgressMarkNotification;
void (*_delegate_animationDidEnd )(id,SEL,NSAnimation*);
void (*_delegate_animationDidStop )(id,SEL,NSAnimation*);
BOOL (*_delegate_animationShouldStart )(id,SEL,NSAnimation*);
id _currentDelegate; // The delegate when the animation is running
BOOL _isThreaded;
NSRecursiveLock *_isAnimatingLock;
}
/** Adds the progress mark to the receiver. */
@ -245,44 +180,63 @@ APPKIT_EXPORT NSString *NSAnimationProgressMarkNotification;
/** Returns the receivers progress marks. */
- (NSArray*) progressMarks;
/** Removes progress mark from the receiver. */
/** Removes a progress mark from the receiver.
A value that does not correspond to a progress mark is ignored.*/
- (void) removeProgressMark: (NSAnimationProgress)progress;
/** Overridden to return the run-loop modes that the receiver uses to run the animation timer in. */
/** Overridden to return the run-loop modes that the receiver uses to run the
animation timer in. */
- (NSArray*) runLoopModesForAnimating;
/** Sets the blocking mode of the receiver. */
/** Sets the blocking mode of the receiver.
The new blocking mode takes effect the next time the receiver is started. */
- (void) setAnimationBlockingMode: (NSAnimationBlockingMode)mode;
/** Sets the receivers animation curve. */
/** Sets the receivers animation curve.
The new value affects the animation already in progress : the actual
curve smoothly changes from the old curve to the new one. */
- (void) setAnimationCurve: (NSAnimationCurve)curve;
/** Sets the current progress of the receiver. */
/** Sets the current progress of the receiver.
In the case of a forward jump the marks between the previous progress value
and the new (excluded) progress value are ignored. In the case of a
backward jump (rewind) the marks will be reached again. */
- (void) setCurrentProgress: (NSAnimationProgress)progress;
/** Sets the delegate of the receiver. */
/** Sets the delegate of the receiver.
The new delegate takes effect the next time the receiver is started. */
- (void) setDelegate: (id)delegate;
/** Sets the duration of the animation to a specified number of seconds. */
/** Sets the duration of the animation to a specified number of seconds.
If the duration is changed while the animation is running the <i>speed</i>
of the animation is not changed but the current progress value is
(see [-setCurrentprogress]). The new value takes effect at the next
frame. */
- (void) setDuration: (NSTimeInterval)duration;
/** Sets the frame rate of the receiver. */
/** Sets the frame rate of the receiver.
The new frame rate takes effect at the next frame. */
- (void) setFrameRate: (float)fps;
/** Sets the receivers progress marks to the values specified in the passed-in array. */
/** Sets the receivers progress marks to the values specified in the
passed-in array. The new marks are t*/
- (void) setProgressMarks: (NSArray*)progress;
/** Starts the animation represented by the receiver. */
/** Starts the animation represented by the receiver.
If the animation is already running the method has no effect.*/
- (void) startAnimation;
/** Starts running the animation represented by the receiver when another animation reaches a specific progress mark. */
/** Starts running the animation represented by the receiver when another
animation reaches a specific progress mark. */
- (void) startWhenAnimation: (NSAnimation*)animation
reachesProgress: (NSAnimationProgress)start;
/** Stops the animation represented by the receiver. */
/** Stops the animation represented by the receiver.
If the animation is not running the method has no effect.*/
- (void) stopAnimation;
/** Stops running the animation represented by the receiver when another animation reaches a specific progress mark. */
/** Stops running the animation represented by the receiver when another
animation reaches a specific progress mark. */
- (void) stopWhenAnimation: (NSAnimation*)animation
reachesProgress: (NSAnimationProgress)stop;

View file

@ -36,6 +36,8 @@
#include <AppKit/NSEvent.h>
#include <Foundation/NSDebug.h>
typedef enum {
NullEvent,
GSAnimationNextFrameEvent,
@ -56,61 +58,45 @@ typedef enum {
{ }
@end
@interface GSThreadedTimerBasedAnimator : GSAnimator
{
NSThread* _thread;
}
@end
@implementation GSAnimator
+ (GSAnimator*) animatorWithAnimation: (id<GSAnimation>)anAnimation
mode: (GSAnimationBlockingMode)aMode
frameRate: (float)fps
zone: (NSZone*)aZone
{
GSAnimator* animator;
animator = [[GSTimerBasedAnimator allocWithZone: aZone]
initWithAnimation: anAnimation
frameRate: fps];
AUTORELEASE(animator);
return animator;
}
+ (GSAnimator*) animatorWithAnimation: (id<GSAnimation>)anAnimation
frameRate: (float)fps
{
return [self
animatorWithAnimation: anAnimation
mode: aMode
return [self animatorWithAnimation: anAnimation
frameRate: fps
zone: NULL];
}
+ (GSAnimator*) animatorWithAnimation: (id<GSAnimation>)anAnimation
mode: (GSAnimationBlockingMode)aMode
frameRate: (float)fps
zone: (NSZone*)aZone
{
GSAnimator* animator;
NSRunLoop* runLoop;
switch(aMode)
{//FIXME
case GSTimerBasedAnimation:
case GSPerformerBasedAnimation:
case GSBlockingCocoaAnimation:
case GSNonblockingCocoaAnimation:
case GSNonblockingCocoaThreadedAnimation:
runLoop = [NSRunLoop currentRunLoop];
animator = [[GSTimerBasedAnimator allocWithZone: aZone]
initWithAnimation: anAnimation
frameRate: fps
runLoop: runLoop];
}
AUTORELEASE(animator);
return animator;
return [self animatorWithAnimation: anAnimation
frameRate: 0.0];
}
- (GSAnimator*) initWithAnimation: (id<GSAnimation>)anAnimation
frameRate: (float)aFrameRate
runLoop: (NSRunLoop*)aRunLoop
frameRate: (float)fps
{
if((self = [super init]))
{
_running = NO;
_animation = anAnimation; TEST_RETAIN(_animation);
_runLoop = aRunLoop; TEST_RETAIN(_runLoop);
_timerInterval = (aFrameRate==0.0)?0.0:(1.0/aFrameRate);
_runLoopModes = [NSArray arrayWithObject: NSDefaultRunLoopMode];
RETAIN(_runLoopModes);
_timerInterval = (fps==0.0)?0.0:(1.0/fps);
[self resetCounters];
}
@ -120,16 +106,15 @@ typedef enum {
- (GSAnimator*) initWithAnimation: (id<GSAnimation>)anAnimation
{
return [self initWithAnimation: anAnimation
frameRate: 0.0
runLoop: [NSRunLoop currentRunLoop]];
frameRate: 0.0];
}
- (void) dealloc
{
[self stopAnimation];
TEST_RELEASE(_animation);
TEST_RELEASE(_runLoop);
TEST_RELEASE(_startTime);
TEST_RELEASE(_runLoopModes);
[super dealloc];
}
@ -146,11 +131,11 @@ typedef enum {
- (float) frameRate
{ return ((float)[self frameCount]) / ((float)_elapsed); }
- (NSRunLoop*) runLoopForAnimating
{ return _runLoop; }
- (NSArray*) runLoopModesForAnimating
{ return [_animation runLoopModesForAnimating]; }
{ return _runLoopModes; }
- (void) setRunLoopModesForAnimating: (NSArray*)modes
{ ASSIGN (_runLoopModes,modes); }
- (void) startAnimation
{
@ -161,6 +146,7 @@ typedef enum {
[_animation animatorDidStart];
[self _animationBegin];
[self _animationLoop];
NSDebugFLLog (@"GSAnimator",@"%@ Started !",self);
}
}
@ -234,8 +220,11 @@ static int _GSTimerBasedAnimator_animator_count = 0;
+ (void) registerAnimator: (GSTimerBasedAnimator*)anAnimator
{
NSTimer* newTimer = nil;
if(anAnimator->_timerInterval == 0.0)
{
NSDebugFLLog (@"GSAnimator",@"%@ AFAP",anAnimator);
[[NSNotificationCenter defaultCenter]
addObserver: anAnimator
selector: @selector(_animationLoop)
@ -251,26 +240,37 @@ static int _GSTimerBasedAnimator_animator_count = 0;
if(nil==_GSTimerBasedAnimator_timer)
{
newTimer =
_GSTimerBasedAnimator_timer = [NSTimer
scheduledTimerWithTimeInterval: 0.0
timerWithTimeInterval: 0.0
target: self
selector: @selector(loopsAnimators)
userInfo: nil
repeats: YES
];
TEST_RETAIN(_GSTimerBasedAnimator_timer);
}
}
else
{
NSDebugFLLog (@"GSAnimator",@"%@ Fixed frame rate",anAnimator);
newTimer =
anAnimator->_timer = [NSTimer
scheduledTimerWithTimeInterval: anAnimator->_timerInterval
timerWithTimeInterval: anAnimator->_timerInterval
target: anAnimator
selector: @selector(_animationLoop)
userInfo: nil
repeats: YES
];
TEST_RETAIN(anAnimator->_timer);
}
if(newTimer!=nil)
{
unsigned i,c;
TEST_RETAIN(newTimer);
for (i=0,c=[anAnimator->_runLoopModes count]; i<c; i++)
[[NSRunLoop currentRunLoop]
addTimer: newTimer
forMode: [anAnimator->_runLoopModes objectAtIndex:i]];
NSDebugFLLog (@"GSAnimator",@"%@ addTimer in %d mode(s)",anAnimator,c);
}
}
@ -308,24 +308,27 @@ static int _GSTimerBasedAnimator_animator_count = 0;
- (void) _animationBegin
{
NSDebugFLLog (@"GSAnimator",@"%@",self);
[[self class] registerAnimator: self];
}
- (void) _animationLoop
{
NSDebugFLLog (@"GSAnimator",@"%@",self);
[self stepAnimation];
}
- (void) _animationEnd
{
NSDebugFLLog (@"GSAnimator",@"%@",self);
[[self class] unregisterAnimator: self];
}
@end
@end // implementation GSTimerBasedAnimator
static void _sendAnimationPerformer( GSAnimator* animator )
{
[[animator runLoopForAnimating]
[[NSRunLoop currentRunLoop]
performSelector: @selector(_animationLoop)
target: animator
argument: nil
@ -336,7 +339,7 @@ static void _sendAnimationPerformer( GSAnimator* animator )
static void _cancelAnimationPerformer( GSAnimator* animator )
{
[[animator runLoopForAnimating] cancelPerformSelectorsWithTarget: animator];
[[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget: animator];
}
@implementation GSPerformerBasedAnimator

View file

@ -31,6 +31,11 @@
#include <Foundation/NSValue.h>
#include <Foundation/NSException.h>
#include <Foundation/NSRunLoop.h>
#include <Foundation/NSThread.h>
#include <Foundation/NSLock.h>
#include <Foundation/NSDate.h>
#include <Foundation/NSAutoReleasePool.h>
#include <AppKit/NSApplication.h>
// needed by NSViewAnimation
#include <AppKit/NSWindow.h>
@ -38,13 +43,19 @@
#include <Foundation/NSDebug.h>
/*
* NSAnimation class
*/
/*===================*
* NSAnimation class *
*===================*/
NSString* NSAnimationProgressMarkNotification
= @"NSAnimationProgressMarkNotification";
NSString *NSAnimationProgressMark
= @"NSAnimationProgressMark";
NSString* NSAnimationBlockingRunLoopMode
= @"NSAnimationBlockingRunLoopMode";
#define GSI_ARRAY_NO_RETAIN
#define GSI_ARRAY_NO_RELEASE
#define GSIArrayItem NSAnimationProgress
@ -55,7 +66,66 @@ NSString *NSAnimationProgressMarkNotification
// 'reasonable value' ?
#define GS_ANIMATION_DEFAULT_FRAME_RATE 25.0
_NSAnimationCurveDesc __gs_animationCurveDesc[] =
static NSArray* _NSAnimationDefaultRunLoopModes;
static inline void
_GSBezierComputeCoefficients ( _GSBezierDesc *b )
{
b->a[0] = b->p[0];
b->a[1] =-3.0*b->p[0] + 3.0*b->p[1];
b->a[2] = 3.0*b->p[0] - 6.0*b->p[1] + 3.0*b->p[2];
b->a[3] =- b->p[0] + 3.0*b->p[1] - 3.0*b->p[2] + b->p[3];
b->areCoefficientsComputed = YES;
}
static inline float
_GSBezierEval (_GSBezierDesc *b, float t )
{
if (!b->areCoefficientsComputed)
_GSBezierComputeCoefficients (b);
return b->a[0] + t * (b->a[1] + t * (b->a[2] + t * b->a[3]));
}
static inline float
_GSBezierDerivEval (_GSBezierDesc *b, float t )
{
if (!b->areCoefficientsComputed)
_GSBezierComputeCoefficients (b);
return b->a[1] + t * (2.0 * b->a[2] + t * 3.0 * b->a[3]);
}
static inline void
_GSRationalBezierComputeBezierDesc (_GSRationalBezierDesc *rb )
{
unsigned i;
for (i=0; i<4; i++)
rb->n.p[i] = (rb->d.p[i] = rb->w[i]) * rb->p[i];
_GSBezierComputeCoefficients (&rb->n);
_GSBezierComputeCoefficients (&rb->d);
rb->areBezierDescComputed = YES;
}
static inline float
_GSRationalBezierEval (_GSRationalBezierDesc *rb, float t)
{
if (!rb->areBezierDescComputed)
_GSRationalBezierComputeBezierDesc (rb);
return _GSBezierEval(&(rb->n),t) / _GSBezierEval(&(rb->d),t);
}
static inline float
_GSRationalBezierDerivEval (_GSRationalBezierDesc *rb, float t)
{
if (!rb->areBezierDescComputed)
_GSRationalBezierComputeBezierDesc (rb);
float h = _GSBezierEval (&(rb->d),t);
return ( _GSBezierDerivEval(&(rb->n),t) * h
- _GSBezierEval (&(rb->n),t) * _GSBezierDerivEval(&(rb->d),t) )
/ (h*h);
}
static
_NSAnimationCurveDesc _gs_animationCurveDesc[] =
{
// easeInOut : endGrad = startGrad & startGrad <= 1/3
{ 0.0,1.0, 1.0/3,1.0/3 , {{2.0,2.0/3,2.0/3,2.0}} },
@ -69,8 +139,23 @@ _NSAnimationCurveDesc __gs_animationCurveDesc[] =
{ 0.0,1.0, 3.0 ,3.0 , {{2.0/3,2.0,2.0,2.0/3}} }
};
_NSAnimationCurveDesc *_gs_animationCurveDesc
= __gs_animationCurveDesc;
/* Translate the NSAnimationCurveDesc data (start/end points and start/end
* gradients) to GSRBezier data (4 control points), then evaluate it.
*/
static inline float
_gs_animationValueForCurve ( _NSAnimationCurveDesc *c, float t, float t0 )
{
if (!c->isRBezierComputed)
{
c->rb.p[0] = c->s;
c->rb.p[1] = c->s + (c->sg*c->rb.w[0]) / (3*c->rb.w[1]);
c->rb.p[2] = c->e - (c->eg*c->rb.w[3]) / (3*c->rb.w[2]);
c->rb.p[3] = c->e;
_GSRationalBezierComputeBezierDesc (&c->rb);
c->isRBezierComputed = YES;
}
return _GSRationalBezierEval ( &(c->rb), (t-t0) / (1.0-t0) );
}
@interface NSAnimation (PrivateNotificationCallbacks)
- (void) _gs_startAnimationReachesProgressMark: (NSNotification*)notification;
@ -79,6 +164,8 @@ _NSAnimationCurveDesc *_gs_animationCurveDesc
@interface NSAnimation (Private)
- (void) _gs_didReachProgressMark: (NSAnimationProgress)progress;
- (void) _gs_startAnimationInOwnLoop;
- (void) _gs_startThreadedAnimation;
- (_NSAnimationCurveDesc*) _gs_curveDesc;
- (NSAnimationProgress) _gs_curveShift;
@end
@ -90,6 +177,25 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
return (NSComparisonResult)(diff / fabs (diff));
}
#define _NSANIMATION_LOCK \
BOOL __gs_isLocked = NO; \
if (_isThreaded) \
{ \
__gs_isLocked = YES; \
NSDebugFLLog(@"NSAnimationLock",\
@"%@ LOCK %@",self,[NSThread currentThread]);\
[_isAnimatingLock lock]; \
}
#define _NSANIMATION_UNLOCK \
if (__gs_isLocked) \
{ \
__gs_isLocked = YES; \
NSDebugFLLog(@"NSAnimationLock",\
@"%@ UNLOCK %@",self,[NSThread currentThread]);\
[_isAnimatingLock unlock]; \
}
@implementation NSAnimation
+ (void) initialize
@ -97,6 +203,12 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
unsigned i;
for (i=0; i<5; i++) // compute Bezier curve parameters...
_gs_animationValueForCurve (&_gs_animationCurveDesc[i],0.0,0.0);
_NSAnimationDefaultRunLoopModes
= [[NSArray alloc] initWithObjects:
NSDefaultRunLoopMode,
NSModalPanelRunLoopMode,
NSEventTrackingRunLoopMode,
nil];
}
- (void) addProgressMark: (NSAnimationProgress)progress
@ -104,104 +216,157 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
if (progress < 0.0) progress = 0.0;
if (progress > 1.0) progress = 1.0;
_NSANIMATION_LOCK;
if (GSIArrayCount(_progressMarks) == 0)
{ // First mark
GSIArrayAddItem (_progressMarks,progress);
NSDebugFLLog(@"NSAnimationMark",@"%@ Insert 1st mark for %f (next:#%d)",self,progress,_nextMark);
NSDebugFLLog (@"NSAnimationMark",
@"%@ Insert 1st mark for %f (next:#%d)",
self,progress,_nextMark);
_nextMark = (progress >= [self currentProgress])? 0 : 1;
}
else
{
unsigned index;
index = GSIArrayInsertionPosition(_progressMarks,progress,&nsanimation_progressMarkSorter);
index = GSIArrayInsertionPosition (_progressMarks,
progress,
&nsanimation_progressMarkSorter);
if (_nextMark < GSIArrayCount(_progressMarks))
if(index <= _nextMark && progress < GSIArrayItemAtIndex(_progressMarks,_nextMark))
if (index <= _nextMark
&& progress < GSIArrayItemAtIndex(_progressMarks,_nextMark))
_nextMark++;
GSIArrayInsertItem (_progressMarks,progress,index);
NSDebugFLLog(@"NSAnimationMark",@"%@ Insert mark #%d/%d for %f (next:#%d)",self,index,GSIArrayCount(_progressMarks),progress,_nextMark);
NSDebugFLLog (@"NSAnimationMark",
@"%@ Insert mark #%d/%d for %f (next:#%d)",
self,index,GSIArrayCount(_progressMarks),progress,_nextMark);
}
_isCachedProgressMarkNumbersValid = NO;
_NSANIMATION_UNLOCK;
}
- (NSAnimationBlockingMode) animationBlockingMode
{
return _blockingMode;
NSAnimationBlockingMode m;
_NSANIMATION_LOCK;
m = _blockingMode;
_NSANIMATION_UNLOCK;
return m;
}
- (NSAnimationCurve) animationCurve
{
return _curve;
NSAnimationCurve c;
_NSANIMATION_LOCK;
c = _curve;
_NSANIMATION_UNLOCK;
return c;
}
- (void) clearStartAnimation
{
_NSANIMATION_LOCK;
[[NSNotificationCenter defaultCenter]
removeObserver: self
name: NSAnimationProgressMarkNotification
object: _startAnimation];
[_startAnimation removeProgressMark: _startMark];
_startAnimation = nil;
_NSANIMATION_UNLOCK;
}
- (void) clearStopAnimation
{
_NSANIMATION_LOCK;
[[NSNotificationCenter defaultCenter]
removeObserver: self
name: NSAnimationProgressMarkNotification
object: _stopAnimation];
[_stopAnimation removeProgressMark: _stopMark];
_stopAnimation = nil;
_NSANIMATION_UNLOCK;
}
- (NSAnimationProgress) currentProgress
{
return _currentProgress;
NSAnimationProgress p;
_NSANIMATION_LOCK;
p = _currentProgress;
_NSANIMATION_UNLOCK;
return p;
}
- (float) currentValue
{
float value;
id delegate;
delegate = GS_GC_UNHIDE(_delegate);
if(_delegate_animationValueForProgress) // method is cached (the animation is running)
{
NSDebugFLLog(@"NSAnimationDelegate",@"%@ [delegate animationValueForProgress] (cached)",self);
value = (*_delegate_animationValueForProgress)(delegate,@selector(animation:valueForProgress:),self,_currentProgress);
_NSANIMATION_LOCK;
if (_delegate_animationValueForProgress)
{ // method is cached (the animation is running)
NSDebugFLLog (@"NSAnimationDelegate",
@"%@ [delegate animationValueForProgress] (cached)",self);
value = (*_delegate_animationValueForProgress)
(GS_GC_UNHIDE (_currentDelegate),
@selector (animation:valueForProgress:),
self, _currentProgress);
}
else // method is not cached (the animation did not start yet)
if ( _delegate != nil
&& [delegate respondsToSelector: @selector(animation:valueForProgress:)] )
&& [GS_GC_UNHIDE (_delegate) respondsToSelector:
@selector (animation:valueForProgress:)] )
{
NSDebugFLLog(@"NSAnimationDelegate",@"%@ [delegate animationValueForProgress]",self);
value = [GS_GC_UNHIDE(_delegate) animation: self valueForProgress: _currentProgress];
NSDebugFLLog (@"NSAnimationDelegate",
@"%@ [delegate animationValueForProgress]",self);
value = [GS_GC_UNHIDE (_delegate) animation: self
valueForProgress: _currentProgress];
}
else // default -- FIXME
else // default -- FIXME ??
/* switch (_curve)
{
case NSAnimationEaseInOut:
case NSAnimationEaseIn:
case NSAnimationEaseOut:
case NSAnimationSpeedInOut:*/
value = _gs_animationValueForCurve( &_curveDesc,_currentProgress,_curveProgressShift );
value = _gs_animationValueForCurve (
&_curveDesc, _currentProgress, _curveProgressShift
);
/* break;
case NSAnimationLinear:
value = _currentProgress; break;
}*/
_NSANIMATION_UNLOCK;
return value;
}
- (id) delegate
{
return (_delegate == nil)? nil : GS_GC_UNHIDE(_delegate);
id d;
_NSANIMATION_LOCK;
d = (_delegate == nil)? nil : GS_GC_UNHIDE (_delegate);
_NSANIMATION_UNLOCK;
return d;
}
- (NSTimeInterval) duration
{
return _duration;
NSTimeInterval d;
_NSANIMATION_LOCK;
d = _duration;
_NSANIMATION_UNLOCK;
return d;
}
- (float) frameRate
{
return _frameRate;
float f;
_NSANIMATION_LOCK;
f = _frameRate;
_NSANIMATION_UNLOCK;
return f;
}
- (id) initWithDuration: (NSTimeInterval)duration
@ -209,6 +374,9 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
{
if ((self = [super init]))
{
if (duration<=0.0)
[NSException raise: NSInvalidArgumentException
format: @"%@ Duration must be > 0.0 (passed: %f)",self,duration];
_duration = duration;
_frameRate = GS_ANIMATION_DEFAULT_FRAME_RATE;
_curve = curve;
@ -241,6 +409,9 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
(void (*)(id,SEL,NSAnimation*)) NULL;
_delegate_animationShouldStart =
(BOOL (*)(id,SEL,NSAnimation*)) NULL;
_isThreaded = NO;
_isAnimatingLock = [[NSRecursiveLock alloc] init];
}
return self;
}
@ -252,6 +423,8 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
- (void) dealloc
{
[self stopAnimation];
GSIArrayEmpty (_progressMarks);
NSZoneFree ([self zone], _progressMarks);
if (_cachedProgressMarkNumbers != NULL)
@ -262,21 +435,31 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
NSZoneFree ([self zone], _cachedProgressMarkNumbers);
}
if( _startAnimation != nil || _stopAnimation != nil)
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self clearStartAnimation];
[self clearStopAnimation];
TEST_RELEASE (_animator);
RELEASE(_isAnimatingLock);
[super dealloc];
}
- (BOOL) isAnimating
{
return (_animator != nil)? [_animator isAnimationRunning] : NO;
BOOL f;
_NSANIMATION_LOCK;
f = (_animator != nil)? [_animator isAnimationRunning] : NO;
_NSANIMATION_UNLOCK;
return f;
}
- (NSArray*) progressMarks
{
NSNumber **cpmn;
_NSANIMATION_LOCK;
unsigned count = GSIArrayCount (_progressMarks);
if (!_isCachedProgressMarkNumbersValid)
@ -303,12 +486,17 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
_cachedProgressMarkNumberCount = count;
_isCachedProgressMarkNumbersValid = YES;
}
return [NSArray arrayWithObjects: _cachedProgressMarkNumbers
count: count];
cpmn = _cachedProgressMarkNumbers;
_NSANIMATION_UNLOCK;
return [NSArray arrayWithObjects: cpmn count: count];
}
- (void) removeProgressMark: (NSAnimationProgress)progress
{
_NSANIMATION_LOCK;
unsigned index = GSIArraySearch (_progressMarks,progress,nsanimation_progressMarkSorter);
if ( index < GSIArrayCount(_progressMarks)
&& progress == GSIArrayItemAtIndex (_progressMarks,index) )
@ -320,6 +508,8 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
}
else
NSWarnFLog (@"%@ Unexistent progress mark",self);
_NSANIMATION_UNLOCK;
}
- (NSArray*) runLoopModesForAnimating
@ -329,12 +519,16 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
- (void) setAnimationBlockingMode: (NSAnimationBlockingMode)mode
{
_NSANIMATION_LOCK;
_isANewAnimatorNeeded |= (_blockingMode != mode);
_blockingMode = mode;
_NSANIMATION_UNLOCK;
}
- (void) setAnimationCurve: (NSAnimationCurve)curve
{
_NSANIMATION_LOCK;
if (_currentProgress <= 0.0f || _currentProgress >= 1.0f)
{
_curveDesc = _gs_animationCurveDesc[curve];
@ -388,6 +582,8 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
_curveDesc.isRBezierComputed = YES;
}
_curve = curve;
_NSANIMATION_UNLOCK;
}
- (void) setCurrentProgress: (NSAnimationProgress)progress
@ -398,6 +594,8 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
if (progress < 0.0) progress = 0.0;
if (progress > 1.0) progress = 1.0;
_NSANIMATION_LOCK;
// NOTE: In the case of a forward jump the marks between the
// previous progress value and the new (excluded) progress
// value are never reached.
@ -433,16 +631,25 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
if (progress >= 1.0 && _animator != nil)
[_animator stopAnimation];
_NSANIMATION_UNLOCK;
}
- (void) setDelegate: (id)delegate
{
_NSANIMATION_LOCK;
_delegate = (delegate == nil)? nil : GS_GC_HIDE (delegate);
_NSANIMATION_UNLOCK;
}
- (void) setDuration: (NSTimeInterval)duration
{
if (duration<=0.0)
[NSException raise: NSInvalidArgumentException
format: @"%@ Duration must be > 0.0 (passed: %f)",self,duration];
_NSANIMATION_LOCK;
_duration = duration;
_NSANIMATION_UNLOCK;
}
- (void) setFrameRate: (float)fps
@ -450,13 +657,23 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
if (fps<0.0)
[NSException raise: NSInvalidArgumentException
format: @"%@ Framerate must be >= 0.0 (passed: %f)",self,fps];
_NSANIMATION_LOCK;
_isANewAnimatorNeeded |= (_frameRate != fps);
if ( _frameRate != fps && [self isAnimating] )
{ // a new animator is needed *now*
// FIXME : should I have been smarter ?
[self stopAnimation];
[self startAnimation];
}
_frameRate = fps;
_NSANIMATION_UNLOCK;
}
- (void) setProgressMarks: (NSArray*)marks
{
_NSANIMATION_LOCK;
GSIArrayEmpty (_progressMarks);
_nextMark = 0;
if (marks != nil)
{
unsigned i, count=[marks count];
@ -464,6 +681,7 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
[self addProgressMark:[(NSNumber*)[marks objectAtIndex:i] floatValue]];
}
_isCachedProgressMarkNumbersValid = NO;
_NSANIMATION_UNLOCK;
}
- (void) startAnimation
@ -475,7 +693,9 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
unsigned i;
for (i=0; i<GSIArrayCount(_progressMarks); i++)
NSDebugFLLog(@"NSAnimationMark",@"%@ Mark #%d : %f",self,i,GSIArrayItemAtIndex(_progressMarks,i));
NSDebugFLLog (@"NSAnimationMark",
@"%@ Mark #%d : %f",
self,i,GSIArrayItemAtIndex(_progressMarks,i));
if ([self currentProgress] >= 1.0)
{
@ -488,7 +708,8 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
if (_delegate != nil)
{
NSDebugFLLog(@"NSAnimationDelegate",@"%@ Cache delegation methods",self);
NSDebugFLLog (@"NSAnimationDelegate",
@"%@ Cache delegation methods",self);
// delegation methods are cached while the animation is running
id delegate;
delegate = GS_GC_UNHIDE (_delegate);
@ -517,16 +738,19 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
(BOOL (*)(id,SEL,NSAnimation*))
[delegate methodForSelector: @selector (animationShouldStart:)]
: NULL;
NSDebugFLLog(@"NSAnimationDelegate",@"%@ Delegation methods : %x %x %x %x %x", self,
NSDebugFLLog (@"NSAnimationDelegate",
@"%@ Delegation methods : %x %x %x %x %x", self,
_delegate_animationDidReachProgressMark,
_delegate_animationValueForProgress,
_delegate_animationDidEnd,
_delegate_animationDidStop,
_delegate_animationShouldStart);
_currentDelegate = _delegate;
}
else
{
NSDebugFLLog(@"NSAnimationDelegate",@"%@ No delegate : clear delegation methods",self);
NSDebugFLLog (@"NSAnimationDelegate",
@"%@ No delegate : clear delegation methods",self);
_delegate_animationDidReachProgressMark =
(void (*)(id,SEL,NSAnimation*,NSAnimationProgress)) NULL;
_delegate_animationValueForProgress =
@ -537,28 +761,55 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
(void (*)(id,SEL,NSAnimation*)) NULL;
_delegate_animationShouldStart =
(BOOL (*)(id,SEL,NSAnimation*)) NULL;
_currentDelegate = nil;
}
if (_animator==nil || _isANewAnimatorNeeded)
{
TEST_RELEASE (_animator);
_animator = [GSAnimator
animatorWithAnimation: self
mode: _blockingMode
frameRate: _frameRate
zone: [self zone]];
NSAssert (_animator,@"Can not create a GSAnimator");
RETAIN (_animator);
NSDebugFLLog (@"NSAnimationAnimator",@"%@ New GSAnimator: %@", self,[_animator class]);
_isANewAnimatorNeeded = NO;
}
NSDebugFLLog(@"NSAnimationAnimator",@"%@ Start animator %@...",self,_animator);
switch (_blockingMode)
{
case NSAnimationBlocking:
[self _gs_startAnimationInOwnLoop];
//[_animator setRunLoopModesForAnimating:
// [NSArray arrayWithObject: NSAnimationBlockingRunLoopMode]];
//[_animator startAnimation];
break;
case NSAnimationNonblocking:
{
NSArray* runLoopModes;
runLoopModes = [self runLoopModesForAnimating];
if (runLoopModes == nil)
runLoopModes = _NSAnimationDefaultRunLoopModes;
[_animator setRunLoopModesForAnimating: runLoopModes];
}
[_animator startAnimation];
break;
case NSAnimationNonblockingThreaded:
_isThreaded = YES;
[NSThread
detachNewThreadSelector: @selector (_gs_startThreadedAnimation)
toTarget: self
withObject: nil];
}
}
- (void) startWhenAnimation: (NSAnimation*)animation
reachesProgress: (NSAnimationProgress)start
{
_NSANIMATION_LOCK;
_startAnimation = animation;
_startMark = start;
@ -569,17 +820,25 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
selector: @selector (_gs_startAnimationReachesProgressMark:)
name: NSAnimationProgressMarkNotification
object: _startAnimation];
_NSANIMATION_UNLOCK;
}
- (void) stopAnimation
{
if ([self isAnimating])
{
_NSANIMATION_LOCK;
[_animator stopAnimation];
_NSANIMATION_UNLOCK;
}
}
- (void) stopWhenAnimation: (NSAnimation*)animation
reachesProgress: (NSAnimationProgress)stop
{
_NSANIMATION_LOCK;
_stopAnimation = animation;
_stopMark = stop;
@ -590,6 +849,8 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
selector: @selector (_gs_stopAnimationReachesProgressMark:)
name: NSAnimationProgressMarkNotification
object: _stopAnimation];
_NSANIMATION_UNLOCK;
}
- (void) encodeWithCoder: (NSCoder*)coder
@ -609,8 +870,11 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
- (void) animatorDidStart
{
NSDebugFLLog (@"NSAnimationAnimator",@"%@",self);
_NSANIMATION_LOCK;
id delegate;
delegate = GS_GC_UNHIDE(_delegate);
delegate = GS_GC_UNHIDE (_currentDelegate);
if (_delegate_animationShouldStart) // method is cached (the animation is running)
{
@ -618,13 +882,18 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
_delegate_animationShouldStart (delegate,@selector(animationShouldStart:),self);
}
RETAIN (self);
_NSANIMATION_UNLOCK;
}
- (void) animatorDidStop
{
NSDebugFLLog (@"NSAnimationAnimator",@"%@ Progress = %f",self,_currentProgress);
_NSANIMATION_LOCK;
id delegate;
delegate = GS_GC_UNHIDE(_delegate);
delegate = GS_GC_UNHIDE (_currentDelegate);
if (_currentProgress < 1.0)
{
if (_delegate_animationDidStop) // method is cached (the animation is running)
@ -642,11 +911,16 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
}
}
RELEASE (self);
_NSANIMATION_UNLOCK;
}
- (void) animatorStep: (NSTimeInterval) elapsedTime;
{
NSDebugFLLog (@"NSAnimationAnimator",@"%@ Elapsed time : %f",self,elapsedTime);
_NSANIMATION_LOCK;
NSAnimationProgress progress = (elapsedTime / _duration);
{ // have some marks been passed ?
@ -654,8 +928,7 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
// treated in [-setCurrentProgress]
unsigned count = GSIArrayCount (_progressMarks);
NSAnimationProgress markedProgress;
while(
_nextMark < count
while ( _nextMark < count
&& progress > (markedProgress = GSIArrayItemAtIndex (_progressMarks,_nextMark)) ) // is a mark reached ?
{
[self _gs_didReachProgressMark: markedProgress];
@ -663,6 +936,8 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
}
[self setCurrentProgress: progress];
_NSANIMATION_UNLOCK;
}
@end //implementation NSAnimation
@ -671,24 +946,40 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
- (void) _gs_startAnimationReachesProgressMark: (NSNotification*)notification
{
NSDebugFLLog(@"NSAnimationMark",@"%@",self);
NSAnimation *animation = [notification object];
if( animation == _startAnimation && [_startAnimation currentProgress] >= _startMark)
NSAnimationProgress mark
= [[[notification userInfo] objectForKey: NSAnimationProgressMark] floatValue];
NSDebugFLLog (@"NSAnimationMark",
@"%@ Start Animation %@ reaches %f",self,animation,mark);
_NSANIMATION_LOCK;
if ( animation == _startAnimation && mark == _startMark)
{
// [self clearStartAnimation];
[self startAnimation];
}
_NSANIMATION_UNLOCK;
}
- (void) _gs_stopAnimationReachesProgressMark: (NSNotification*)notification
{
NSDebugFLLog(@"NSAnimationMark",@"%@",self);
NSAnimation *animation = [notification object];
if( animation == _stopAnimation && [_stopAnimation currentProgress] >= _stopMark)
NSAnimationProgress mark
= [[[notification userInfo] objectForKey: NSAnimationProgressMark] floatValue];
NSDebugFLLog (@"NSAnimationMark",
@"%@ Stop Animation %@ reaches %f",self,animation,mark);
_NSANIMATION_LOCK;
if ( animation == _stopAnimation && mark == _stopMark)
{
// [self clearStopAnimation];
[self stopAnimation];
}
_NSANIMATION_UNLOCK;
}
@end // implementation NSAnimation (PrivateNotificationCallbacks)
@ -698,38 +989,77 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
- (void) _gs_didReachProgressMark: (NSAnimationProgress) progress
{
NSDebugFLLog (@"NSAnimationMark",@"%@ progress %f",self, progress);
_NSANIMATION_LOCK;
// calls delegate's method
if (_delegate_animationDidReachProgressMark) // method is cached (the animation is running)
{
NSDebugFLLog(@"NSAnimationDelegate",@"%@ [delegate animationdidReachProgressMark] (cached)",self);
_delegate_animationDidReachProgressMark(GS_GC_UNHIDE(_delegate),@selector(animation:didReachProgressMark:),self,progress);
NSDebugFLLog (@"NSAnimationDelegate",
@"%@ [delegate animationdidReachProgressMark] (cached)",self);
_delegate_animationDidReachProgressMark (GS_GC_UNHIDE(_currentDelegate),
@selector(animation:didReachProgressMark:),
self,progress);
}
else // method is not cached (the animation did not start yet)
if ( _delegate != nil
&& [GS_GC_UNHIDE(_delegate) respondsToSelector: @selector(animation:didReachProgressMark:)] )
&& [GS_GC_UNHIDE (_delegate)
respondsToSelector: @selector(animation:didReachProgressMark:)] )
{
NSDebugFLLog(@"NSAnimationDelegate",@"%@ [delegate animationdidReachProgressMark]",self);
NSDebugFLLog (@"NSAnimationDelegate",
@"%@ [delegate animationdidReachProgressMark]",self);
[GS_GC_UNHIDE (_delegate) animation: self didReachProgressMark: progress];
}
// posts a notification
NSDebugFLLog(@"NSAnimationNotification",@"%@ Post NSAnimationProgressMarkNotification : %f",self,progress);
NSDebugFLLog (@"NSAnimationNotification",
@"%@ Post NSAnimationProgressMarkNotification : %f",self,progress);
[[NSNotificationCenter defaultCenter]
postNotificationName: NSAnimationProgressMarkNotification
object: self
userInfo: [NSDictionary
dictionaryWithObject: [NSNumber numberWithFloat: progress]
forKey: @"NSAnimationProgressMark"
forKey: NSAnimationProgressMark
]
];
// skips marks with the same progress value
while (
(++_nextMark) < GSIArrayCount (_progressMarks)
&& GSIArrayItemAtIndex(_progressMarks,_nextMark) == progress)
&& GSIArrayItemAtIndex (_progressMarks,_nextMark) == progress
)
;
NSDebugFLLog(@"NSAnimationMark",@"%@ Next mark #%d for %f",self,_nextMark,GSIArrayItemAtIndex(_progressMarks,_nextMark));
_NSANIMATION_UNLOCK;
NSDebugFLLog (@"NSAnimationMark",
@"%@ Next mark #%d for %f",
self,_nextMark,GSIArrayItemAtIndex(_progressMarks,_nextMark));
}
- (void) _gs_startThreadedAnimation
{
// NSAssert(_isThreaded);
CREATE_AUTORELEASE_POOL (pool);
NSDebugFLLog (@"NSAnimationThread",
@"%@ Start of %@",self,[NSThread currentThread]);
[self _gs_startAnimationInOwnLoop];
NSDebugFLLog (@"NSAnimationThread",
@"%@ End of %@",self,[NSThread currentThread]);
RELEASE (pool);
_isThreaded = NO;
}
- (void) _gs_startAnimationInOwnLoop
{
[_animator setRunLoopModesForAnimating:
[NSArray arrayWithObject: NSAnimationBlockingRunLoopMode]];
[_animator startAnimation];
while ( [[NSRunLoop currentRunLoop]
runMode: NSAnimationBlockingRunLoopMode
beforeDate: [NSDate distantFuture]] )
/* do nothing */;
}
- (_NSAnimationCurveDesc*) _gs_curveDesc
@ -743,19 +1073,35 @@ nsanimation_progressMarkSorter( NSAnimationProgress first,NSAnimationProgress se
@implementation NSAnimation (GNUstep)
- (unsigned int) frameCount
{ return (_animator != nil)? [_animator frameCount] : 0; }
{
unsigned c;
_NSANIMATION_LOCK;
c = (_animator != nil)? [_animator frameCount] : 0;
_NSANIMATION_UNLOCK;
return c;
}
- (void) resetCounters
{ if(_animator != nil) [_animator resetCounters]; }
{
_NSANIMATION_LOCK;
if (_animator != nil) [_animator resetCounters];
_NSANIMATION_UNLOCK;
}
- (float) actualFrameRate;
{ return (_animator != nil)? [_animator frameRate] : 0.0; }
{
float r;
_NSANIMATION_LOCK;
r = (_animator != nil)? [_animator frameRate] : 0.0;
_NSANIMATION_UNLOCK;
return r;
}
@end
/*
* NSViewAnimation class
*/
/*=======================*
* NSViewAnimation class *
*=======================*/
NSString *NSViewAnimationTargetKey = @"NSViewAnimationTargetKey";
NSString *NSViewAnimationStartFrameKey = @"NSViewAnimationStartFrameKey";
@ -969,18 +1315,25 @@ NSString *NSViewAnimationFadeOutEffect = @"NSViewAnimationFadeOutEffect";
- (void) setViewAnimations: (NSArray*)animations
{
_NSANIMATION_LOCK;
if (_viewAnimations != animations)
DESTROY (_viewAnimationDesc);
ASSIGN (_viewAnimations, animations) ;
_NSANIMATION_UNLOCK;
}
- (NSArray*) viewAnimations
{
return _viewAnimations;
NSArray *a;
_NSANIMATION_LOCK;
a = _viewAnimations;
_NSANIMATION_UNLOCK;
return a;
}
- (void) startAnimation
{
_NSANIMATION_LOCK;
if (_viewAnimationDesc == nil)
{
unsigned i,c;
@ -994,24 +1347,38 @@ NSString *NSViewAnimationFadeOutEffect = @"NSViewAnimationFadeOutEffect";
];
}
[super startAnimation];
_NSANIMATION_UNLOCK;
}
- (void) stopAnimation
{
_NSANIMATION_LOCK;
[super stopAnimation];
[self setCurrentProgress: 1.0];
_NSANIMATION_UNLOCK;
}
- (void) setCurrentProgress: (NSAnimationProgress)progress
- (void) _gs_updateViewsWithValue: (NSNumber*) value
{
// Runs in main thread : must not call any NSAnimation method to avoid a deadlock
unsigned i,c;
float v;
[super setCurrentProgress: progress];
v = [self currentValue];
v = [value floatValue];
if (_viewAnimationDesc != nil)
for (i=0, c=[_viewAnimationDesc count];i<c;i++)
[[_viewAnimationDesc objectAtIndex: i] setCurrentProgress: v];
}
- (void) setCurrentProgress: (NSAnimationProgress)progress
{
_NSANIMATION_LOCK;
[super setCurrentProgress: progress];
[self performSelectorOnMainThread: @selector (_gs_updateViewsWithValue:)
withObject: [NSNumber numberWithFloat:[self currentValue]]
waitUntilDone: YES];
_NSANIMATION_UNLOCK;
}
@end // implementation NSViewAnimation