//*****************************************************************************
//
//	Animator.m  
//
//		Controls app Icon animation
//
//			by	Felipe A. Rodriguez		
//
//	This code is supplied "as is" the author makes no warranty as to its 
//	suitability for any purpose.  This code is free and may be distributed 
//	in accordance with the terms of the:
//		
//			GNU GENERAL PUBLIC LICENSE
//			Version 2, June 1991
//			copyright (C) 1989, 1991 Free Software Foundation, Inc.
// 			675 Mass Ave, Cambridge, MA 02139, USA
//
//*****************************************************************************

#import <appkit/appkit.h>

#import "Animator.h"
#import "IconView.h"




@interface Animator (Private)


static void Animate();					// called by timed entry events
	static DPSTimedEntry aniTimeTag;


@end
@implementation Animator

//*****************************************************************************
//
// 			standard init
//
//*****************************************************************************

- init
{
NXRect	appIconFrame;
NXRect	lineViewRect;
id appIcon;

    [super init];
							// setup appIcon for animation
	appIcon = [NXApp appIcon];					// get appIcon window
    [appIcon getFrame:&appIconFrame];			// get frame rect
    [Window getContentRect:&lineViewRect 		// get content view rect appIco
						forFrameRect:&appIconFrame style:[appIcon style]];
	NX_X(&lineViewRect) = 1;					// reduce the size and width/ht 
	NX_Y(&lineViewRect) = 1;					// by a border of 1
	NX_WIDTH(&lineViewRect) -= 1 ;
	NX_HEIGHT(&lineViewRect) -= 1;
	theIconView = [[IconView alloc] initFrame:&lineViewRect];
	[[appIcon contentView] addSubview:theIconView];
			
	imageList = [[List alloc] init];		// produce an image objects array
	[imageList insertObject: [NXImage findImageNamed: "g20"] at: 0];	// open
	[imageList insertObject: [NXImage findImageNamed: "g21"] at: 1];
	[imageList insertObject: [NXImage findImageNamed: "g22"] at: 2];
	[imageList insertObject: [NXImage findImageNamed: "g23"] at: 3];
	[imageList insertObject: [NXImage findImageNamed: "g24"] at: 4];	// clos
	[imageList insertObject: [NXImage findImageNamed: "g10"] at: 5];	// dial
	[imageList insertObject: [NXImage findImageNamed: "g11"] at: 6];
	[imageList insertObject: [NXImage findImageNamed: "g12"] at: 7];
	[imageList insertObject: [NXImage findImageNamed: "g13"] at: 8];	// dial
	[imageList insertObject: [NXImage findImageNamed: "GateKeeper"] at: 9];

	index = 9;

    return self;
}
//*****************************************************************************
//
// 		setup to do open gate anim 
//
//*****************************************************************************

- startAnimTimer
{
	if(!aniTimeTag)  
		aniTimeTag = DPSAddTimedEntry(			// register function Animate
			0.2, 								// to be called every period of 		
			(DPSTimedEntryProc)Animate, 		// arg0
			(id)self, 
			NX_BASETHRESHOLD);

	return self;
}
//*****************************************************************************
//
// 		icon animation
//
//*****************************************************************************
 
- _animate
{
	if(standBy) 						// app running but not dialing
		{
		index--;
		if(index < 0)					// anim seq completed
			{
			[self removeTimedEntry];
			standBy = NO;
			index = 9;
			}
		}
	if(dialing) 						// app dialing but not pppup
		{
		index++;
		if(index > 8)
			index = 5;
		}

	[theIconView setImage:[imageList objectAt:index]]; 
			
	return self;
}
//*****************************************************************************
//
// 		animate should stop icon sequence
//
//*****************************************************************************
 
- pppup
{
	[self removeTimedEntry];
	index = 4;
	dialing = NO;
	standBy = NO;
		
	return self;
}
//*****************************************************************************
//
// 		animate should do open gate icon sequence
//
//*****************************************************************************
 
- dialing
{
	standBy = NO;
	reverse = NO;
	if(!dialing)
		{
		index = 0;
		dialing = YES;
		[self startAnimTimer];					// cycle thru animation
		}
		
	return self;
}
//*****************************************************************************
//
// 		animate should do open standby icon seq
//
//*****************************************************************************
 
- standBy
{
	dialing = NO;
	if(!standBy && index != 9)
		{
		standBy = YES;
		[self startAnimTimer];					// cycle thru animation
		}
		
	return self;
}
//*****************************************************************************
//
// 		remove the timed entry when connection is made or we exit
//
//*****************************************************************************
 
- removeTimedEntry
{
	if (aniTimeTag)  
		DPSRemoveTimedEntry (aniTimeTag);
	aniTimeTag = 0;
		
	return self;
}
//*****************************************************************************
//
// 		return the app icon view 
//
//*****************************************************************************

- iconView
{
	return theIconView;
}
//*****************************************************************************
//
// 	free gets rid of everything we created
//
//*****************************************************************************

- free
{
	if(imageList)
		[imageList free];

    return [super free];
}

@end
@implementation Animator (Private)

//*****************************************************************************
//
//		This fucntion is registered by DPSaddtimedentry.
//		It is subsequently called every period t as registered  
//		in arg 0 of DPSaddtimedentry.
//
//*****************************************************************************
 
static void Animate(DPSTimedEntry time_tag, double now, id self)
{
	[self _animate];
}

@end
