2007-04-05 08:52:05 +00:00
|
|
|
/*
|
|
|
|
* GSAnimator.m
|
|
|
|
*
|
|
|
|
* Author: Xavier Glattard (xgl) <xavier.glattard@online.fr>
|
|
|
|
*
|
|
|
|
* 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 library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; see the file COPYING.LIB.
|
|
|
|
* If not, write to the Free Software Foundation,
|
|
|
|
* 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <GNUstepGUI/GSAnimator.h>
|
|
|
|
|
|
|
|
#include <Foundation/NSTimer.h>
|
|
|
|
#include <Foundation/NSRunLoop.h>
|
|
|
|
#include <Foundation/NSThread.h>
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
#include <Foundation/NSSet.h>
|
|
|
|
#include <Foundation/NSNotification.h>
|
|
|
|
|
|
|
|
#include <AppKit/NSEvent.h>
|
|
|
|
|
2007-04-12 17:01:23 +00:00
|
|
|
#include <Foundation/NSDebug.h>
|
|
|
|
|
2007-04-05 08:52:05 +00:00
|
|
|
typedef enum {
|
|
|
|
NullEvent,
|
|
|
|
GSAnimationNextFrameEvent,
|
|
|
|
GSAnimationEventTypeNumber
|
|
|
|
} GSAnimationEventType;
|
|
|
|
|
|
|
|
@interface GSAnimator(private)
|
|
|
|
- (void) _animationBegin;
|
|
|
|
- (void) _animationLoop;
|
|
|
|
- (void) _animationEnd;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSTimerBasedAnimator : GSAnimator
|
|
|
|
{ }
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSPerformerBasedAnimator : GSAnimator
|
|
|
|
{ }
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSAnimator
|
|
|
|
|
|
|
|
+ (GSAnimator*) animatorWithAnimation: (id<GSAnimation>)anAnimation
|
|
|
|
frameRate: (float)fps
|
|
|
|
zone: (NSZone*)aZone
|
|
|
|
{
|
|
|
|
GSAnimator* animator;
|
2007-04-12 17:01:23 +00:00
|
|
|
animator = [[GSTimerBasedAnimator allocWithZone: aZone]
|
|
|
|
initWithAnimation: anAnimation
|
|
|
|
frameRate: fps];
|
2007-04-05 08:52:05 +00:00
|
|
|
AUTORELEASE(animator);
|
|
|
|
return animator;
|
|
|
|
}
|
|
|
|
|
2007-04-12 17:01:23 +00:00
|
|
|
+ (GSAnimator*) animatorWithAnimation: (id<GSAnimation>)anAnimation
|
|
|
|
frameRate: (float)fps
|
|
|
|
{
|
|
|
|
return [self animatorWithAnimation: anAnimation
|
|
|
|
frameRate: fps
|
|
|
|
zone: NULL];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (GSAnimator*) animatorWithAnimation: (id<GSAnimation>)anAnimation
|
|
|
|
{
|
|
|
|
return [self animatorWithAnimation: anAnimation
|
|
|
|
frameRate: 0.0];
|
|
|
|
}
|
|
|
|
|
2007-04-05 08:52:05 +00:00
|
|
|
- (GSAnimator*) initWithAnimation: (id<GSAnimation>)anAnimation
|
2007-04-12 17:01:23 +00:00
|
|
|
frameRate: (float)fps
|
2007-04-05 08:52:05 +00:00
|
|
|
{
|
|
|
|
if((self = [super init]))
|
2007-04-12 17:01:23 +00:00
|
|
|
{
|
|
|
|
_running = NO;
|
|
|
|
|
|
|
|
_animation = anAnimation; TEST_RETAIN(_animation);
|
|
|
|
_runLoopModes = [NSArray arrayWithObject: NSDefaultRunLoopMode];
|
|
|
|
RETAIN(_runLoopModes);
|
|
|
|
_timerInterval = (fps==0.0)?0.0:(1.0/fps);
|
2007-04-05 08:52:05 +00:00
|
|
|
|
2007-04-12 17:01:23 +00:00
|
|
|
[self resetCounters];
|
|
|
|
}
|
2007-04-05 08:52:05 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (GSAnimator*) initWithAnimation: (id<GSAnimation>)anAnimation
|
|
|
|
{
|
|
|
|
return [self initWithAnimation: anAnimation
|
2007-04-12 17:01:23 +00:00
|
|
|
frameRate: 0.0];
|
2007-04-05 08:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[self stopAnimation];
|
|
|
|
TEST_RELEASE(_animation);
|
|
|
|
TEST_RELEASE(_startTime);
|
2007-04-12 17:01:23 +00:00
|
|
|
TEST_RELEASE(_runLoopModes);
|
2007-04-05 08:52:05 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned int) frameCount
|
|
|
|
{ return _frameCount; }
|
|
|
|
|
|
|
|
- (void) resetCounters
|
|
|
|
{
|
|
|
|
_elapsed = 0.0;
|
|
|
|
_frameCount = 0;
|
|
|
|
_lastFrame = [NSDate timeIntervalSinceReferenceDate];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (float) frameRate
|
|
|
|
{ return ((float)[self frameCount]) / ((float)_elapsed); }
|
|
|
|
|
|
|
|
- (NSArray*) runLoopModesForAnimating
|
2007-04-12 17:01:23 +00:00
|
|
|
{ return _runLoopModes; }
|
|
|
|
|
|
|
|
- (void) setRunLoopModesForAnimating: (NSArray*)modes
|
|
|
|
{ ASSIGN (_runLoopModes,modes); }
|
2007-04-05 08:52:05 +00:00
|
|
|
|
|
|
|
- (void) startAnimation
|
|
|
|
{
|
|
|
|
if(!_running)
|
2007-04-12 17:01:23 +00:00
|
|
|
{
|
|
|
|
_running = YES;
|
|
|
|
[self resetCounters];
|
|
|
|
[_animation animatorDidStart];
|
|
|
|
[self _animationBegin];
|
|
|
|
[self _animationLoop];
|
|
|
|
NSDebugFLLog (@"GSAnimator",@"%@ Started !",self);
|
|
|
|
}
|
2007-04-05 08:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) stopAnimation
|
|
|
|
{
|
|
|
|
if(_running)
|
2007-04-12 17:01:23 +00:00
|
|
|
{
|
|
|
|
_running = NO;
|
|
|
|
[self _animationEnd];
|
|
|
|
[_animation animatorDidStop];
|
|
|
|
}
|
2007-04-05 08:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) startStopAnimation
|
|
|
|
{
|
|
|
|
if(_running)
|
|
|
|
[self stopAnimation];
|
|
|
|
else
|
|
|
|
[self startAnimation];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isAnimationRunning
|
|
|
|
{ return _running; }
|
|
|
|
|
|
|
|
- (void) _animationBegin
|
|
|
|
{ [self subclassResponsibility: _cmd]; }
|
|
|
|
|
|
|
|
- (void) _animationLoop
|
|
|
|
{ [self subclassResponsibility: _cmd]; }
|
|
|
|
|
|
|
|
- (void) _animationEnd
|
|
|
|
{ [self subclassResponsibility: _cmd]; }
|
|
|
|
|
|
|
|
- (void) stepAnimation
|
|
|
|
{
|
|
|
|
NSTimeInterval thisFrame = [NSDate timeIntervalSinceReferenceDate];
|
|
|
|
NSTimeInterval sinceLastFrame = (thisFrame - _lastFrame);
|
|
|
|
_elapsed += sinceLastFrame;
|
|
|
|
_lastFrame = thisFrame;
|
|
|
|
|
|
|
|
[_animation animatorStep: _elapsed];
|
|
|
|
_frameCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) animationLoopEvent: (NSEvent*) e
|
|
|
|
{ [self subclassResponsibility: _cmd]; }
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
static NSTimer* _GSTimerBasedAnimator_timer = nil;
|
|
|
|
static NSMutableSet* _GSTimerBasedAnimator_animators = nil;
|
|
|
|
static GSTimerBasedAnimator* _GSTimerBasedAnimator_the_one_animator = nil;
|
|
|
|
static int _GSTimerBasedAnimator_animator_count = 0;
|
|
|
|
|
|
|
|
@implementation GSTimerBasedAnimator
|
|
|
|
|
|
|
|
+ (void) loopsAnimators
|
|
|
|
{
|
|
|
|
switch(_GSTimerBasedAnimator_animator_count)
|
2007-04-12 17:01:23 +00:00
|
|
|
{
|
2007-04-05 08:52:05 +00:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
[_GSTimerBasedAnimator_the_one_animator _animationLoop];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: @"GSTimerBasedAnimator_loop" object: self];
|
2007-04-12 17:01:23 +00:00
|
|
|
}
|
2007-04-05 08:52:05 +00:00
|
|
|
}
|
|
|
|
|
2007-04-12 17:01:23 +00:00
|
|
|
+ (void) registerAnimator: (GSTimerBasedAnimator*)anAnimator
|
2007-04-05 08:52:05 +00:00
|
|
|
{
|
2007-04-12 17:01:23 +00:00
|
|
|
NSTimer* newTimer = nil;
|
|
|
|
|
2007-04-05 08:52:05 +00:00
|
|
|
if(anAnimator->_timerInterval == 0.0)
|
|
|
|
{
|
2007-04-12 17:01:23 +00:00
|
|
|
NSDebugFLLog (@"GSAnimator",@"%@ AFAP",anAnimator);
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver: anAnimator
|
|
|
|
selector: @selector(_animationLoop)
|
|
|
|
name: @"GSTimerBasedAnimator_loop"
|
|
|
|
object: self];
|
|
|
|
|
|
|
|
if(!_GSTimerBasedAnimator_animator_count++)
|
|
|
|
_GSTimerBasedAnimator_the_one_animator = anAnimator;
|
|
|
|
|
|
|
|
if(nil==_GSTimerBasedAnimator_animators)
|
|
|
|
_GSTimerBasedAnimator_animators = [[NSMutableSet alloc] initWithCapacity: 5];
|
|
|
|
[_GSTimerBasedAnimator_animators addObject: anAnimator];
|
|
|
|
|
|
|
|
if(nil==_GSTimerBasedAnimator_timer)
|
|
|
|
{
|
|
|
|
newTimer =
|
|
|
|
_GSTimerBasedAnimator_timer = [NSTimer
|
|
|
|
timerWithTimeInterval: 0.0
|
|
|
|
target: self
|
|
|
|
selector: @selector(loopsAnimators)
|
|
|
|
userInfo: nil
|
|
|
|
repeats: YES
|
|
|
|
];
|
|
|
|
}
|
2007-04-05 08:52:05 +00:00
|
|
|
}
|
|
|
|
else
|
2007-04-12 17:01:23 +00:00
|
|
|
{
|
|
|
|
NSDebugFLLog (@"GSAnimator",@"%@ Fixed frame rate",anAnimator);
|
|
|
|
newTimer =
|
|
|
|
anAnimator->_timer = [NSTimer
|
|
|
|
timerWithTimeInterval: anAnimator->_timerInterval
|
|
|
|
target: anAnimator
|
|
|
|
selector: @selector(_animationLoop)
|
|
|
|
userInfo: nil
|
|
|
|
repeats: YES
|
|
|
|
];
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2007-04-05 08:52:05 +00:00
|
|
|
}
|
|
|
|
|
2007-04-12 17:01:23 +00:00
|
|
|
+ (void) unregisterAnimator: (GSTimerBasedAnimator*)anAnimator
|
2007-04-05 08:52:05 +00:00
|
|
|
{
|
|
|
|
if(anAnimator->_timerInterval == 0.0)
|
|
|
|
{
|
2007-04-12 17:01:23 +00:00
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
removeObserver: anAnimator
|
|
|
|
name: @"GSTimerBasedAnimator_loop"
|
|
|
|
object: self];
|
|
|
|
|
|
|
|
[_GSTimerBasedAnimator_animators removeObject: anAnimator];
|
|
|
|
|
|
|
|
if(!--_GSTimerBasedAnimator_animator_count)
|
|
|
|
{
|
|
|
|
[_GSTimerBasedAnimator_timer invalidate];
|
|
|
|
DESTROY(_GSTimerBasedAnimator_timer);
|
|
|
|
_GSTimerBasedAnimator_the_one_animator = nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if(_GSTimerBasedAnimator_the_one_animator==anAnimator)
|
|
|
|
_GSTimerBasedAnimator_the_one_animator
|
|
|
|
= [_GSTimerBasedAnimator_animators anyObject];
|
2007-04-05 08:52:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-04-12 17:01:23 +00:00
|
|
|
if(anAnimator->_timer != nil)
|
|
|
|
{
|
|
|
|
[anAnimator->_timer invalidate];
|
|
|
|
DESTROY(anAnimator->_timer);
|
|
|
|
}
|
2007-04-05 08:52:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _animationBegin
|
|
|
|
{
|
2007-04-12 17:01:23 +00:00
|
|
|
NSDebugFLLog (@"GSAnimator",@"%@",self);
|
2007-04-05 08:52:05 +00:00
|
|
|
[[self class] registerAnimator: self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _animationLoop
|
|
|
|
{
|
2007-04-12 17:01:23 +00:00
|
|
|
NSDebugFLLog (@"GSAnimator",@"%@",self);
|
2007-04-05 08:52:05 +00:00
|
|
|
[self stepAnimation];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _animationEnd
|
|
|
|
{
|
2007-04-12 17:01:23 +00:00
|
|
|
NSDebugFLLog (@"GSAnimator",@"%@",self);
|
2007-04-05 08:52:05 +00:00
|
|
|
[[self class] unregisterAnimator: self];
|
|
|
|
}
|
|
|
|
|
2007-04-12 17:01:23 +00:00
|
|
|
@end // implementation GSTimerBasedAnimator
|
2007-04-05 08:52:05 +00:00
|
|
|
|
|
|
|
static void _sendAnimationPerformer( GSAnimator* animator )
|
|
|
|
{
|
2007-04-12 17:01:23 +00:00
|
|
|
[[NSRunLoop currentRunLoop]
|
2007-04-05 08:52:05 +00:00
|
|
|
performSelector: @selector(_animationLoop)
|
|
|
|
target: animator
|
|
|
|
argument: nil
|
|
|
|
order: 1000000
|
|
|
|
modes: [animator runLoopModesForAnimating]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _cancelAnimationPerformer( GSAnimator* animator )
|
|
|
|
{
|
2007-04-12 17:01:23 +00:00
|
|
|
[[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget: animator];
|
2007-04-05 08:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@implementation GSPerformerBasedAnimator
|
|
|
|
|
|
|
|
- (void) _animationBegin
|
|
|
|
{ [self _animationLoop]; }
|
|
|
|
|
|
|
|
- (void) _animationLoop
|
|
|
|
{
|
|
|
|
[self stepAnimation];
|
|
|
|
if(_running)
|
|
|
|
_sendAnimationPerformer(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _animationEnd
|
|
|
|
{ _cancelAnimationPerformer(self); }
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|