
#import "WebsterSpaceView.h"
#import "Thinker.h"
#import <appkit/appkit.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 TIFFDIR		"/NextLibrary/References/Webster-Dictionary/pictures/"
//#define FADEDELTA	0.05
#define FADEDELTA	0.05

@implementation WebsterSpaceView

- didLockFocus {
	[myImage getSize: &imageRect.size];
	return self;
	}

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

	[super drawSelf:rects :rectCount];
	imageRect.origin.x = floor(randBetween(0, maxCoord.x));
	imageRect.origin.y = floor(randBetween(72, maxCoord.y));

	p.x = floor(imageRect.origin.x);
	p.y = floor(imageRect.origin.y);
	[theView moveTo:p.x :p.y-144];
	[theText setText:buf];
	[myImage dissolve: delta toPoint: &p];

	return self;
}

- oneStep
{
	int n;
	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);
		[myImage 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);
		[myImage dissolve: delta toPoint: &imageRect.origin];
		steptime = STEPTIME;
		break;

	case FV_Move:
		if(myImage)
			[myImage free];
		//toggle = !toggle;
		toggle = YES;
		if(toggle) {
			strcpy(buf, TIFFDIR);
			n = (int)floor(randBetween(0,i));
			strcat(buf, files[n]);
			} else {
			strcpy(buf, "/Users/rick/subst.tiff");
			}
		if((myImage = [[NXImage allocFromZone:[self zone]] init]) == nil) {
			printf("Failed - allocating image\n");
			exit(1);
			}
		if (![myImage useFromFile:buf]){
			printf("Hey - this is it.\n");
			exit(1);
			}
		strcpy(buf, files[n]);
		*rindex(buf, '.') = '\000';
		[self setImage:myImage];
		state = FV_FadeIn;
		steptime = STEPTIME;
		break;
	}

	return self;
}

- initFrame:(NXRect *)frameRect
{
	NXRect theRect;
	steptime = STEPTIME;
	state = FV_Move;
	if((dp = opendir(TIFFDIR)) == NULL ) {
		fprintf(stderr, "yadayadayada\n");
		exit(1);
		}
	i=0;
	while ((dir=readdir(dp)) != NULL) {
		if(dir ->d_ino == 0){
			continue;
			}
		if ((strstr(dir->d_name, "tiff")) 
			/*|| (strstr(dir->d_name, "eps"))*/
			) {
			files[i] = (char *)malloc(strlen(dir->d_name));
			strcpy(files[i++], dir->d_name);
			}
		}
	closedir(dp);
	i--;
	NXSetRect(&theRect, 0,0,832,144);
	theView = [[View alloc] initFrame:&theRect];
	[theView setFlipped:YES];
	theText=[[Text alloc] initFrame:&theRect];
	[theText setFont:[Font newFont:"Times-Roman" size:72]];
	[theText setTextGray:NX_DKGRAY];
	[theText setText:""];
	[theView addSubview:theText];
	[self addSubview:theView];
	myImage = [[NXImage alloc] init];
	return [super initFrame:frameRect];
}

- free {
	int m;
	for(m=0; m <= i; m++) {
		free(files[m]);			// free filename table
		}
	//[theText free];
	[theView free];
	[myImage free];
	return [super free];
	}

- inspector:sender
{
	char blah[256];
	if (!websterInspector) {
		[NXBundle getPath:blah forResource:"webster" ofType:"nib" 
		          inDirectory:[sender moduleDirectory:"WebsterSpace"] 
				  withVersion:0];
		[NXApp loadNibFile:blah owner:self withNames:NO];
		}
	return websterInspector;
	}

@end
