//*****************************************************************************
//
//	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"


		// used in producing timed events
static void Animate();
	static DPSTimedEntry aniTimeTag;


@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 array of objects
	[imageList insertObject: [NXImage findImageNamed: "g20"] at: 0];
	[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];
	[imageList insertObject: [NXImage findImageNamed: "g10"] at: 5];
	[imageList insertObject: [NXImage findImageNamed: "g11"] at: 6];
	[imageList insertObject: [NXImage findImageNamed: "g12"] at: 7];
	[imageList insertObject: [NXImage findImageNamed: "g13"] at: 8];
	[imageList insertObject: [NXImage findImageNamed: "GateKeeper"] at: 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
{
static int Index = 0;
static BOOL reverse = NO;

	if(standbyGate && Index < 0) 		// app running but not dialing
		{
		[self removeTimedEntry];
		Index = 9;
		}
	if(Index < 0)						// restarted before standby was reached
		{
		reverse = NO;
		Index++;
		}
	if(openGate)						// app stops dialing
		{
		Index = 4;
		reverse = YES;
		openGate = NO;
		}
	[theIconView setImage:[imageList objectAt:Index]]; 
	if(Index >= 8)
		{
		if(reverse)
			Index = -1;
		else
			Index = 5;
		}
	else
		{
		if(reverse)
			Index--;
		else
			Index++;
		}
			
	return self;
}
//*****************************************************************************
//
// 		animate should do open gate icon sequence
//
//*****************************************************************************
 
- setOpenGate:(BOOL)doSeq
{
	openGate = doSeq;
		
	return self;
}
//*****************************************************************************
//
// 		animate should do open standby icon seq
//
//*****************************************************************************
 
- setStandbyGate:(BOOL)doSeq
{
	standbyGate = doSeq;
		
	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 simply gets rid of everything we created
// 	This is how nice objects clean up.
//
//*****************************************************************************

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

    return [super free];
}

@end

//*****************************************************************************
//
//		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];
}
