//----------------------------------------------------------------------------------------------------
//
//	AnalogClock
//
//	Inherits From:		Clock
//
//	Declared In:		AnalogClock.h
//
//	Disclaimer
//
//		You may freely copy, distribute and reuse this software and its
//		associated documentation. I disclaim any warranty of any kind, 
//		expressed or implied, as to its fitness for any particular use.
//
//----------------------------------------------------------------------------------------------------
#import "AnalogClock.h"
#import <math.h>


#define HOUR_HAND_LENGTH 			15.0
#define MINUTE_HAND_LENGTH 		22.0
#define SECOND_HAND_LENGTH		24.0

#define HOUR_HAND_LINE_WIDTH 		2.0
#define MINUTE_HAND_LINE_WIDTH 	2.0
#define SECOND_HAND_LINE_WIDTH	0.0


@implementation AnalogClock

//----------------------------------------------------------------------------------------------------
//	Initialization and Freeing
//----------------------------------------------------------------------------------------------------
- initFrame: (const NXRect *)aRect
{
	//  Give default characteristics.  Fix size to 64 x 64. 
	
	NXRect	frameRect;
	NXSetRect (&frameRect, NX_X(aRect), NX_Y(aRect), 64.0, 64.0);
	[super initFrame:&frameRect];
	
	[self  hourAndMinuteHandColor: NX_COLORBLACK];
	[self  secondHandColor: NX_COLORRED];
	
	[self clockFace: [NXImage findImageNamed: "AnalogFaceMarble"]];
	[self wantsDate: NO];
	
	return self;
}


//----------------------------------------------------------------------------------------------------
//	Accessing Clock Hand Colors
//----------------------------------------------------------------------------------------------------
- hourAndMinuteHandColor: (NXColor) aColor
{
	hourAndMinuteHandColor = aColor;
	[self update];
	return self;
}


- secondHandColor: (NXColor) aColor
{
	secondHandColor = aColor;
	[self update];
	return self;
}


- (NXColor) hourAndMinuteHandColor
{
	return hourAndMinuteHandColor;
}


- (NXColor) secondHandColor
{
	return secondHandColor;
}


//----------------------------------------------------------------------------------------------------
//	Action Methods
//----------------------------------------------------------------------------------------------------
- takeHourAndMinuteHandColorFrom: sender
{
	if ([sender respondsTo: @selector (color)]) [self hourAndMinuteHandColor: [sender color]];
	return self;
}


- takeSecondHandColorFrom: sender
{
	if ([sender respondsTo: @selector (color)]) [self secondHandColor: [sender color]];
	return self;
}


//----------------------------------------------------------------------------------------------------
//	Draw Methods
//-----------------------------------------------------------------------------------------------------
- drawTime
{
	float  secondHandAngle = (float) (SECOND(displayTimeAndDate) * 6 * M_PI / 180.0);
	float  minuteHandAngle = (float) (MINUTE(displayTimeAndDate) * 6 * M_PI / 180.0);
	float  hourHandAngle = ((HOUR(displayTimeAndDate)) %12) * 30 * M_PI / 180.0 + 		minuteHandAngle / 12;

	PStranslate (NX_MIDX(&bounds), NX_MIDY(&bounds));

	//  second hand
	if (flags.wantsSeconds)
		{
		PSsetlinewidth (SECOND_HAND_LINE_WIDTH);
		NXSetColor (secondHandColor);
		PSmoveto (0.0,0.0);
		PSlineto (sin (secondHandAngle) * SECOND_HAND_LENGTH, 
			cos (secondHandAngle) * SECOND_HAND_LENGTH);
		PSstroke ();
		}

	NXSetColor (hourAndMinuteHandColor);
	
	//  minute hand
	PSsetlinewidth (MINUTE_HAND_LINE_WIDTH);
	PSmoveto (sin (minuteHandAngle) * MINUTE_HAND_LENGTH, 
		 cos (minuteHandAngle) * MINUTE_HAND_LENGTH);
	PSlineto (0.0, 0.0);
	PSstroke ();

	//  hour hand
	PSsetlinewidth (HOUR_HAND_LINE_WIDTH);
	PSmoveto (0.0, 0.0);
	PSlineto (sin (hourHandAngle) * HOUR_HAND_LENGTH, 
		cos (hourHandAngle) * HOUR_HAND_LENGTH);
	PSstroke ();
	PStranslate (-(NX_MIDX(&bounds)), -(NX_MIDY(&bounds)));
	    
	return self;
}


//----------------------------------------------------------------------------------------------------
//	Archiving Methods
//----------------------------------------------------------------------------------------------------
- read: (NXTypedStream*) stream
{
	[super read: stream];
	hourAndMinuteHandColor =  NXReadColor (stream);
	secondHandColor = NXReadColor (stream);
	return self;
}


- write: (NXTypedStream*) stream
{
	[super write: stream];
	NXWriteColor (stream, hourAndMinuteHandColor);
	NXWriteColor (stream, secondHandColor);
	return self;
}


//----------------------------------------------------------------------------------------------------
//	Superclass Overrides
//----------------------------------------------------------------------------------------------------
- wantsMilitaryTime: (BOOL) aFlag
{
	return [super wantsMilitaryTime: NO];
}


- wantsDate: (BOOL) aFlag
{
	return [super wantsDate: NO];
}


//----------------------------------------------------------------------------------------------------
//	IB Methods
//----------------------------------------------------------------------------------------------------
- getMinSize:(NXSize *)minSize maxSize:(NXSize *)maxSize from:(int)where
{
	//  Constrains resize within IB.
	
	minSize->width = maxSize->width = 64.0;
	minSize->height = maxSize->height = 64.0;
	return self;
}


- (const char*) getInspectorClassName
{
	//  Returns the name of the class responsible for managing custom 
	//  IB Attributes inspection.
		
	return "AnalogClockInspector";
}


@end