
#import "BouncingView.h"

@implementation BouncingView

-	init
{	
	const NXScreen	*mainScreen;
	int			windowNum;
	NXRect		windowRect;
		
	mainScreen = [NXApp mainScreen];
	myWindow = [[Window alloc] initContent: &(mainScreen->screenBounds)
		style: NX_PLAINSTYLE backing: NX_BUFFERED buttonMask: 0
		defer: YES];
	windowNum = [myWindow windowNum];
	[myWindow setBackgroundGray: NX_DKGRAY];
	PSsetwindowlevel( 50, windowNum );
	[[myWindow contentView] getBounds: &windowRect];
	[self initFrame: &windowRect];
	[[myWindow contentView] addSubview: self];
	xDelta = yDelta = DELTA;
	currentPoint.x = (frame.size.width/2) - (imageSize.width/2);
	currentPoint.y = (frame.size.height/2) - (imageSize.height/2);
	previousRect.origin = currentPoint;
	previousRect.size = imageSize;
	
	return self;
}

-	drawSelf: (const NXRect *)rects :(int)rectCount
{
    [background composite:NX_SOVER fromRect:&previousRect
		toPoint:&previousRect.origin];
	[slidingImage composite: NX_SOVER toPoint: &currentPoint];
	
	return self;
}

-	initFrame: (const NXRect *)frameRect
{
	NXRect	initRect;
	const char 	*imageFile;
	
	[super initFrame: frameRect];
	
	[self allocateGState];
	imageFile = NXReadDefault( [NXApp name], "ImageFile" );
	if( imageFile && *imageFile )
	{
		NXRunAlertPanel( "BV", "opening: %s", NULL, NULL, NULL, imageFile );
		slidingImage = [[NXImage allocFromZone: [self zone]] 
			initFromFile: imageFile];
	}
	else
	{
		slidingImage = [[NXImage allocFromZone: [self zone]] 
		initFromFile: "/usr/lib/NextStep/loginwindow.app/LoginPanel.tiff"];
	}
	[slidingImage getSize: &imageSize];
	if( slidingImage )
		[slidingImage free];
	initRect.origin.x =	initRect.origin.y = 0.0;
	initRect.size = imageSize;
	[[[NXApp mainWindow] contentView] lockFocus];
	slidingImageRep = [[NXBitmapImageRep allocFromZone: [self zone]]
		initData: NULL fromRect: (const NXRect *)&initRect];
	[[[NXApp mainWindow] contentView] unlockFocus];
	slidingImage = [[NXImage allocFromZone: [self zone]] 
		initSize: &imageSize];
	[slidingImage useRepresentation: slidingImageRep];
	PSsetgray( NX_DKGRAY );
	[(background = [[NXImage allocFromZone:[self zone]] init])
		setScalable:NO];
    [background useDrawMethod:@selector(drawDefaultBackground:)
		inObject:self];
	[background setSize: &(bounds.size)];
	
	return self;
}

- 	drawDefaultBackground:imageRep
{
	[background setSize: &(bounds.size)];
	PSsetgray( NX_DKGRAY );
	NXRectFill( &bounds );

	return self;
}

-	startScreenSaver
{
	[myWindow orderFront: self];
	[myWindow display];
	
	return self;
}

-	stopScreenSaver
{
	[myWindow orderOut: self];
	
	return self;
}

-	free
{
	[super free];
	[myWindow free];
	[slidingImage free];
	[background free];
	
	return self;
}

-	step
{
	previousRect.origin = currentPoint;
	currentPoint.x += xDelta;
	currentPoint.y += yDelta;
	
	if( currentPoint.x > (frame.size.width - imageSize.width) )
	{
		currentPoint.x = frame.size.width - imageSize.width;
		xDelta = -xDelta;
	}
	else if( currentPoint.x < 0 )
	{
		currentPoint.x = 0;
		xDelta = -xDelta;
	}
	if( currentPoint.y > (frame.size.height - imageSize.height) )
	{
		currentPoint.y = frame.size.height - imageSize.height;
		yDelta = -yDelta;
	}
	else if( currentPoint.y < 0 )
	{
		currentPoint.y = 0;
		yDelta = -yDelta;
	}
	[self display];
	[window flushWindow];
	
	return self;
}

@end
