
#import "FireworksView.h"
#import "FireworksWraps.h"
#import <math.h>
#import <libc.h>
#import <dpsclient/wraps.h>
#import <appkit/color.h>
#import <appkit/Button.h>
#import <appkit/Application.h>
#import <appkit/NXColorWell.h>

@implementation FireworksView

- newOne
{
	oldy = 0; newy = 0;
	oldx = RND(1000) * 2.0;
	newx = oldx;
	xvel = (RND(2000) - oldx) / 50.0;
	yvel = (RND(15) + 23) * 1.2;
	i = 0;
	if (numWorks++ > MAXWORKS) {
		PSsetgray(0.0); numWorks = 0;
		NXRectFill(&bounds); // black screen
	}
	return self;
}

- initFrame:(NXRect *)frameRect
{
	[super initFrame:frameRect];
	srandom(getpid());
	grav = 0.8; // set gravity
	[self newOne];
	[NXApp loadNibSection:"FireworksPrefs.nib" owner:self];
	winNum=[[self window] windowNum];

	return self;
}

- sizeTo:(NXCoord)width :(NXCoord)height
{
	[super sizeTo:width :height];
	mywidth = width;
	myheight = height;
	return self;
}

- oneStep
{
	if (i++ < 50) {
		drawWork(oldx, oldy, newx, newy);
		oldx = newx;
		oldy = newy;
		newx = oldx + xvel;
		newy = oldy + yvel;
		yvel = yvel - grav;
	} else {
		blowUp(oldx, oldy, newx, newy);
		[self newOne];
	}
	
	PScurrentmouse(winNum, &curX, &curY);
	/* Test the cursor */
	if (mywidth - curX + myheight - curY <= 10.0)
		[self show]; // get Pref panel if in upper rt. corner

	return self;
}

- drawSelf:(const NXRect *)rects :(int)rectCount
{
	if (!rects || !rectCount) return self;
	
	PSsetgray(0.0);
	NXRectFill(rects); // black screen
	
	PScurrentmouse(winNum, &curX, &curY);
	/* Test the cursor */
	if (mywidth - curX + myheight - curY <= 10.0)
		[self show]; // get Pref panel if in upper rt. corner

	return self;
}

- (BOOL)isBoringScreenSaver
{	// I hope I set this right...
	return NO;
}

- (BOOL) useBufferedWindow
{ // unbuffered, so a call to update will always call drawself
  // to fill the window with black...
	return NO;
}

- show
{
	[myPrefWindow orderFront:nil];
	return self;
}


@end
