//
//  FadingImageView.m
//
//  Lennart Lovstrand, Rank Xerox EuroPARC, August 1991.
//	small modifications by sam 910904

#import "FadingImageView.h"
#import "Thinker.h"
#import <appkit/NXImage.h>
#import <dpsclient/wraps.h>
#import <libc.h>
#import <math.h>
//#import <appkit/Application.h>

#define STEPTIME	50	/* time between fades (millisec) */
#define WAITTIME	6000	/* wait before fading (millisec) */

#define FADEDELTA	0.05

@implementation FadingImageView

- drawSelf:(const NXRect *)rects :(int)rectCount
{
	NXPoint p;
	if (!rects || !rectCount) return self;

	[super drawSelf:rects :rectCount];
	
	p.x = floor(imageRect.origin.x);
	p.y = floor(imageRect.origin.y);

	[image dissolve: delta toPoint: &p];

	return self;
}

- oneStep
{
	if (![self timePassed: steptime]) return self;

	switch (state)
	{
	case FV_FadeIn:
		delta += FADEDELTA;
		if (delta >= 1.0)
		{
			delta = 1.0;
			state = FV_FadeOut;
			steptime = WAITTIME;
		}

		PSsetgray(0.0);
		NXRectFill(&imageRect);
		[image dissolve: delta toPoint: &imageRect.origin];
		break;

	case FV_FadeOut:
		delta -= FADEDELTA;
		if (delta <= 0.0)
		{
			delta = 0.0;
			state = FV_Move;
		}

		PSsetgray(0.0);
		NXRectFill(&imageRect);
		[image dissolve: delta toPoint: &imageRect.origin];
		steptime = STEPTIME;
		break;

	case FV_Move:
		imageRect.origin.x = floor(randBetween(0, maxCoord.x));
		imageRect.origin.y = floor(randBetween(0, maxCoord.y));
		state = FV_FadeIn;
		steptime = STEPTIME;
		break;
	}

	return self;
}

- initFrame:(NXRect *)frameRect
{
	steptime = STEPTIME;
	state = FV_Move;
	return [super initFrame:frameRect];
}

@end
